//Show/hide Settings panels (found currently hidden at bottom of "online.php")
// 0 = Settings (Background Image/Color)
// 1 = Configuration (Themes, Modules etc)
// 2 = Information (Author etc)
//
function setPanel(p) {
	var thisPanel = 'settingsPanel'+p;
	if (panels[p] == 'hidden') {
		originalBGColor = document.getElementById('bdy').style.backgroundColor;
		for (var wini=1;wini<parseFloat(modules);wini++) {
			var tdiv = 'div'+wini;
			var idiv = 'indiv'+wini;
			if (document.getElementById(tdiv).offsetHeight > minimisedHeight) {
				document.getElementById(tdiv).style.visibility = 'hidden'; // hide modules
				document.getElementById(idiv).style.visibility = 'hidden'; // hide modules
				}
			}
		document.forms[0].reset(); // reset form (with "on visible" as "on hide" blanks fields and stops SAVE working
		document.getElementById('themeinfo').style.visibility = 'visible';
		document.getElementById('infoholder').innerHTML = document.getElementById(thisPanel).innerHTML;
		for (var i=0;i<hiddenPanels;i++) {
			panels[i] = 'hidden';
			if (i==p) {
				panels[i] = 'visible';
				}
			}
		}
	else if (panels[p] == 'visible') {
		for (var wini=1;wini<parseFloat(modules);wini++) {
			var tdiv = 'div'+wini;
			var idiv = 'indiv'+wini;
			document.getElementById(tdiv).style.visibility = 'visible'; // hide modules
			document.getElementById(idiv).style.visibility = 'visible'; // hide modules
			}
		document.getElementById('themeinfo').style.visibility = 'hidden';
		document.getElementById('infoholder').innerHTML = "";
		panels[p] = 'hidden';
		if (p == '0') {	// Background/Image Only			
			restoreOriginalSettings();
			}
		}
	}
	
function destroyCookies() {
	if (confirm('This will reset all window positions, background colors and images to the Theme\'s original defaults. Are you sure?')) {
		eval('document.'+hiddenFrameName+'.location.href = "/desktop/destroycookies.php"');
		}
	}
	
function restoreOriginalSettings() {
	document.getElementById('bdy').style.backgroundColor = originalBGColor;
	document.getElementById('bdy').style.color = originalBGTextColor;
	document.getElementById('logoplaceholder1').style.color = originalBGTextColor;
	document.getElementById('logoplaceholder2').style.color = originalBGTextColor;
	document.getElementById('infoholder').style.backgroundColor = originalModuleColor;
	var winiAsString;
	document.getElementById('bdy').style.backgroundImage = "url(" + originalBGImage + ")";
	for (var wini=1;wini<parseFloat(modules);wini++) {
		winiAsString = 'div'+wini+'';
		if (winStatus[wini] == 'N') { // Not minimised
			document.getElementById(winiAsString).style.filter= 'alpha(opacity='+moduleOpacityMax+')';
			}
		}
	for (var wini=0;wini<4;wini++) {
		winiAsString = 'button'+wini+'';
		document.getElementById(winiAsString).style.filter= 'alpha(opacity='+moduleOpacityMax+')';
		}
	document.forms[0].reset();
	}
	
//Save bgcolor and bgimage from online.php panel
function saveSettings(setAsCookie) {
	bgc 		= document.forms[0].bgc.value;
	bgt 		= document.forms[0].bgt.value;
	bgl 		= document.forms[0].bgl.value;	
	bgi 		= document.forms[0].file.value;
	var modc 		= document.forms[0].modc.value;
	var modopac		= document.forms[0].modopac.value;
	var altbgi		= document.forms[0].altbgimage.options[document.forms[0].altbgimage.options.selectedIndex].value;
	
	//test opacity
	if (isNumber(modopac) && modopac.length == 2 && parseInt(modopac) > 9 && parseInt(modopac) < 91) {
		moduleOpacityMax = modopac;
		}
	else if (!isNumber(modopac) || parseInt(modopac) < 0 || parseInt(modopac) > 99) {
		alert ('Opacity must be numeric between 00 and 99');
		document.forms[0].modopac.value = 90;
		return false;
		}
			
	if (altbgi && bgi && bgi != "keepAsIs") {
		if (confirm('You have selected a local background image and a web-based one. If you wish to use the web-based image, please click OK. To use the local image, click CANCEL')) {
			bgi = altbgi;
			}
		}
	else if (altbgi && (!bgi || bgi == "keepAsIs")) {
		bgi = altbgi;
		}
	if ((!bgi && confirm('Are you sure you don\'t want a background image?')) || bgi) {
		//Test bgcolor
		if (!bgi) {
			bgi = 'none';
			}
		if (isAlpha(bgc) && bgc.length == 6) {
			document.getElementById('bdy').style.backgroundColor = '#'+bgc;
			originalBGColor = bgc;
			}
		else if (!isAlpha(bgc) && bgc != "transparent") {
			alert ('Color must be alpha-numeric 0-9,A-F');
			var ln = bgc.length - 1;
			bgc = bgc.substr(0,ln);
			document.forms[0].bgc.value = bgc;
			}
		//Test text color
		if (isAlpha(bgt) && bgt.length == 6) {
			document.getElementById('bdy').style.color = '#'+bgt;
			document.getElementById('logoplaceholder1').style.color = '#'+bgt;
			document.getElementById('logoplaceholder2').style.color = '#'+bgt;
			originalBGTextColor = bgt;
			}
		else if (!isAlpha(bgt)) {
			alert ('Text color must be alpha-numeric 0-9,A-F');
			var ln = bgt.length - 1;
			bgt = bgt.substr(0,ln);
			document.forms[0].bgt.value = bgt;
			}
		//test link color
		if (isAlpha(bgl) && bgl.length == 6) {
			originalBGLinkColor = bgl;
			}
		else if (!isAlpha(bgl)) {
			alert ('Link text color must be alpha-numeric 0-9,A-F');
			var ln = bgl.length - 1;
			bgt = bgl.substr(0,ln);
			document.forms[0].bgl.value = bgl;
			}		
		//Test moduleColor
		if (!modc) {
			modc = 'none';
			}
		if (isAlpha(modc) && modc.length == 6) {
			document.getElementById('infoholder').style.backgroundColor = '#'+modc;
			originalModuleColor = modc;
			}
		else if (!isAlpha(modc)) {
			alert ('Module color must be alpha-numeric 0-9,A-F');
			var ln = modc.length - 1;
			modc = modc.substr(0,ln);
			document.forms[0].modc.value = modc;
			}
		if (bgi != "keepAsIs") {
			document.getElementById('bdy').style.backgroundImage = 'none';
			document.getElementById('bdy').style.backgroundImage = "url(" + bgi + ")";
			originalBGImage = bgi;
			}		
		if (setAsCookie != "N") {
			window.open('/desktop/savesettingsascookies.php?bgc='+bgc+'&bgt='+bgt+'&bgl='+bgl+'&file='+bgi+'&modc='+modc+'&opacity='+modopac,hiddenFrameName); // target to hidden frame and set cookie
			}
		setPanel('0');		
		panels[0] = 'hidden';
		}
	}
	
function testBGColor() {	
	var bgct = document.forms[0].bgc.value;
	if (bgct = "t") {
		document.forms[0].bgc.value = "transparent";
		bgct = "transparent";
		}
	if (isAlpha(bgct) && bgct.length == 6) {
		document.getElementById('bdy').style.backgroundColor = '#'+bgct;
		}
	else if (!isAlpha(bgct) && bgct != "transparent") {
		alert ('Color must be alpha-numeric 0-9,A-F');
		var ln = bgct.length - 1;
		bgct = bgct.substr(0,ln);
		document.forms[0].bgc.value = bgct;
		}
	}
	
function testBGTextColor() {	
	var bgct = document.forms[0].bgt.value;
	if (isAlpha(bgct) && bgct.length == 6) {
		document.getElementById('bdy').style.color = '#'+bgct;
		document.getElementById('logoplaceholder1').style.color = '#'+bgct;
		document.getElementById('logoplaceholder2').style.color = '#'+bgct;
		}
	else if (!isAlpha(bgct)) {
		alert ('Text color must be alpha-numeric 0-9,A-F');
		var ln = bgct.length - 1;
		bgct = bgct.substr(0,ln);
		document.forms[0].bgt.value = bgct;
		}
	}
	
function testBGLinkColor() {	
	var bgcl = document.forms[0].bgl.value;
	if (isAlpha(bgcl) && bgcl.length == 6) {
		document.getElementById('bdy').style.linkColor = '#'+bgcl;
		}
	else if (!isAlpha(bgcl)) {
		alert ('Link color must be alpha-numeric 0-9,A-F');
		var ln = bgcl.length - 1;
		bgcl = bgcl.substr(0,ln);
		document.forms[0].bgl.value = bgcl;
		}
	}
	
function testModuleColor() {	
	var modl = document.forms[0].modc.value;
	if (isAlpha(modl) && modl.length == 6) {
		document.getElementById('infoholder').style.backgroundColor = '#'+modl;
		}
	else if (!isAlpha(modl)) {
		alert ('Module color must be alpha-numeric 0-9,A-F');
		var ln = modl.length - 1;
		modl = modl.substr(0,ln);
		document.forms[0].bgl.value = modl;
		}
	}
	
//This function is used but largely irrelevant as windows are hidden during the alteration process
//anyway. But it could be used in other scenarios to instantly update module opacity.
function testModuleOpacity() {	
	var modopac = document.forms[0].modopac.value;
	var winiAsString;
	if (isNumber(modopac) && modopac.length == 2 && parseInt(modopac) > 9 && parseInt(modopac) < 91) {
		//Change module opacities
		for (var wini=1;wini<parseFloat(modules);wini++) {
			winiAsString = 'div'+wini+'';
			if (winStatus[wini] == 'N') { // Not minimised
				document.getElementById(winiAsString).style.filter= 'alpha(opacity='+modopac+')';
				}
			}
		//Change Theme button opacities
		for (var wini=0;wini<4;wini++) {
			winiAsString = 'button'+wini+'';
			document.getElementById(winiAsString).style.filter= 'alpha(opacity='+modopac+')';
			}
		}
	else if (!isNumber(modopac) && parseInt(modopac) < 10 && parseInt(modopac) > 90) {
		alert ('Module Opacity must be numeric between 10 and 90');
		var ln = modopac.length - 1;
		modopac = modopac.substr(0,ln);
		document.forms[0].modopac.value = modopac;
		}
	}
	
// Preview background image from local drive
function testBGImage() {
	var bgit = document.forms[0].file.value;
	if (bgit) {
		document.getElementById('bdy').style.backgroundImage = 'none';
		document.getElementById('bdy').style.backgroundImage = "url(" + bgit + ")";
		document.forms[0].altbgimage.options.selectedIndex = 1; // option "use local background"
		}
	else {
		document.getElementById('bdy').style.backgroundImage = 'none';
		document.forms[0].altbgimage.options.selectedIndex = 3; // option "use default background"
		}
	}
	
// Preview background image from dropdown
function testAltBGImage() {
	var bgit = document.forms[0].altbgimage.options[document.forms[0].altbgimage.options.selectedIndex].value;
	if (bgit) {
		document.getElementById('bdy').style.backgroundImage = 'none';
		document.getElementById('bdy').style.backgroundImage = "url(" + bgit + ")";
		}
	else {
		document.getElementById('bdy').style.backgroundImage = 'none';
		}
	}
	
function isAlpha(txt) {
	return ValidString(txt,'ABCDEFabcdef0123456789');
	}

function isNumber(txt) {
	return ValidString(txt,'0123456789');
	}
	
function ValidString(ChkString,ValidString) {
	for (i=0; i<ChkString.length; i++) {
		if (ValidString.indexOf(ChkString.substring(i,i+1)) == -1) return false;
		}
	return true;
	}
	
function getModulePositions() {
	var moduleSettings = "Current Module Screen Positions:<br>";
	for (var wini=1;wini<parseFloat(modules);wini++) {
		var tdiv = 'div'+wini;
		moduleSettings += "Mod "+wini+": ";
		moduleSettings += "L: "+document.getElementById(tdiv).style.left+"; ";
		moduleSettings += "T: "+document.getElementById(tdiv).style.top+"; ";
		moduleSettings += "W: "+document.getElementById(tdiv).offsetWidth+"px; ";
		moduleSettings += "H: "+document.getElementById(tdiv).offsetHeight+"px;<br>";
		}
	// Removed but reinstate for development
	//document.getElementById('moduleSettings').innerHTML = moduleSettings;
	}
	
	
//Save user defined modules
function saveModuleSettings() {
	var modurl1 		= document.forms[0].modURL1.value;
	var modtitle1 		= document.forms[0].modTitle1.value;
	var modurl2 		= document.forms[0].modURL2.value;
	var modtitle2 		= document.forms[0].modTitle2.value;
	var modurl3 		= document.forms[0].modURL3.value;
	var modtitle3 		= document.forms[0].modTitle3.value;
	var modurl4 		= document.forms[0].modURL4.value;
	var modtitle4 		= document.forms[0].modTitle4.value;
	modtitle1 = modtitle1.replace(/\'/g,"");
	modtitle2 = modtitle2.replace(/\'/g,"");
	modtitle3 = modtitle3.replace(/\'/g,"");
	modtitle4 = modtitle4.replace(/\'/g,"");
	modtitle1 = modtitle1.replace(/&/g,"\&");
	modtitle2 = modtitle2.replace(/&/g,"\&");
	modtitle3 = modtitle3.replace(/&/g,"\&");
	modtitle4 = modtitle4.replace(/&/g,"\&");
	modtitle1 = modtitle1.replace(/=/g,"\=");
	modtitle2 = modtitle2.replace(/=/g,"\=");
	modtitle3 = modtitle3.replace(/=/g,"\=");
	modtitle4 = modtitle4.replace(/=/g,"\=");
	window.open('/desktop/saveusermodules.php?mu1='+modurl1+'&mt1='+modtitle1+'&mu2='+modurl2+'&mt2='+modtitle2+'&mu3='+modurl3+'&mt3='+modtitle3+'&mu4='+modurl4+'&mt4='+modtitle4,hiddenFrameName); // target to hidden frame and set cookie
	setPanel('3');
	panels[3] = 'hidden';

	}
	
function launchCaedes() {
	var pcWidth = screen.width;
	var pcHeight = screen.height;
	alert('We will now take you to http://www.Caedes.net (popup window) to choose more background images. Find images you wish to use and save them to your PC. Then use the BROWSE button below to select the image you saved for use as your desktop background. Your screen resolution is currently '+pcWidth+'x'+pcHeight+' so we would suggest finding background images that are the same dimensions, or larger.');
	}
	
//This variable needs to have global scope for the callColorDlg function to persist the chosen color
function callColorDlg(fld){
//if sInitColor is null, the color dialog box has not yet been called
if (sInitColor == null) 
	var sColor = dlgHelper.ChooseColorDlg();
else
//call the dialog box and initialize the color to the color previously chosen
	var sColor = dlgHelper.ChooseColorDlg(sInitColor);
//change the return value from a decimal value to a hex value and make sure the value has 6
//digits to represent the RRGGBB schema required by the color table
	sColor = sColor.toString(16);
if (sColor.length < 6) {
  var sTempString = "000000".substring(0,6-sColor.length);
  sColor = sTempString.concat(sColor);
}
	document.getElementById(fld).value = sColor;
	//document.execCommand("ForeColor", false, sColor);
	if (fld == "bgc") {
		testBGColor();
		}
	else if (fld == "bgt") {
		testBGTextColor();
		}
	else if (fld == "bgl") {
		testBGLinkColor();
		}
	else if (fld == "modc") {
		testModuleColor();
		}
		
	
//set the initialization color to the color chosen
	sInitColor = sColor;
	
}