// JavaScript Document
function dGE(cId) {
	return document.getElementById(cId);
}

// ============================
// Temporary Scripts
// $("html").append("<div class='upgrading'></div>");

// Color Box
function createColorbox(themeArray) {
	themeList = "";
	for (i=0; i<themeArray.length; i++) {
		themeList += "<li style=\"display:inline; cursor:pointer;\" onClick=\"document.getElementById('csschanger').href='template/css/" + themeArray[i] + ".css';\">" + (i+1) + "</li>\n";
	}
	
	$("body").append("<div id=\"colorbox\" style=\"position:fixed; margin-left:10px; margin-top:30px; padding:2px; z-index:999; width:80px; height:40; border:1px solid #999999; background-color:#000; color:#FFFFFF; display:none;\">" +
			"<div>Select color:</div>" +
			"<ul style=\"list-style:none; text-align:left; padding:0; margin:0;\">" +
				themeList +
			"</ul>" +
		"</div>");
}

// Clear txt
function cleartxt(dId, dtxt) {
	dId.value = (dId.value == dtxt) ? "" : dId.value;
}
// Restore txt
function restoretxt(dId, dtxt) {
	dId.value = (dId.value == "") ? dtxt : dId.value;
}

// Copy Address
function copyAddress() {
	if ( $("input[name=txt_copy]:checked").length == 1) {
		if (confirm('Copy invoice address over?') == true) {
			$('[name=txt_s_name]').val($('[name=txt_name]').val());
			$('[name=txt_s_address]').val($('[name=txt_address]').val());
			$('[name=txt_s_email]').val($('[name=txt_email]').val());
			$('[name=txt_s_postcode]').val($('[name=txt_postcode]').val());
			$('[name=txt_s_state]').val($('[name=txt_state]').val());
			$('[name=txt_s_country]').val($('[name=txt_country]').val());
			$('[name=txt_s_contact]').val($('[name=txt_contact]').val());
		}
	} else {
		$('[name=txt_s_name]').val("");
		$('[name=txt_s_address]').val("");
		$('[name=txt_s_email]').val("");
		$('[name=txt_s_postcode]').val("");
		$('[name=txt_s_state]').val("");
		$('[name=txt_s_country]').val("");
		$('[name=txt_s_contact]').val("");
	}
}

// Clear Cart
function clearForm(formName) {
	$("form[name="+formName+"] input[type=text]").val("");
	$("form[name="+formName+"] textarea").val("");
}

function toggleColumns(showDiv, hideDiv) {
	$('#'+showDiv+'').show();
	$('#'+hideDiv+'').hide();
}

// Verify Field
function verifyField(currentField,formField,formMin) {
	var vField = $("[name="+formField+"]").val();
	vField = Trim(vField);
	// Checking
	if (vField==null || vField=="" || vField.length==0) {
		$("[name="+formField+"]").css("border","1px solid red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null) {
			if (currentField != 'none') {
				$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " is blank</span>");	
			}
		}
		return false;
	}
	
	if (vField.length < formMin) {
		$("[name="+formField+"]").css("border","1px solid red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null) {
			if (currentField != 'none') {
				$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " must have more than " + formMin + " characters</span>");
			}
		}
		return false;
	}
	$("[name="+formField+"]").css("border","1px solid #6699CC");
	if (dGE(formField) != null) $("#"+formField).remove();
	return true;
}

function VerifySame(currentField,formField,formField2) {
	var vField = $("[name="+formField+"]").val();
	var vField2 = $("[name="+formField2+"]").val();
	vField = Trim(vField);
	vField2 = Trim(vField2);
	
	// Checking
	if(vField != vField2) {
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField2+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null) {
			$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\"> "+currentField+" must be the same</span>");
		}
		return false;
	}
	
	$("[name="+formField+"]").css("borderColor","#6699CC");
	$("[name="+formField2+"]").css("borderColor","#6699CC");
	if (dGE(formField) != null) $("#"+formField).remove();
	if (dGE(formField2) != null) $("#"+formField2).remove();
	return true;
}

function verifyEmail(formField) {
	var szValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._";
	var nPos;
	var passed = true;
	var szString = $("[name="+formField+"]").val();
	nPos = szString.indexOf('@');
	szTmp = szString.substring(nPos+1, szString.length);
	
	// Verifying
	if (nPos==-1) passed = false;
	if (!verifyStringwKey(szString.substring(0, nPos), szValid)) passed = false;
	if (!verifyStringwKey(szTmp, szValid)) passed = false;
	
	nPos = szTmp.length;
	if (szTmp.charAt(0)=='.') passed = false;
	// check for a . 2/3/4 places b4 the end of string
	if(szTmp.charAt(nPos-3)!='.' && szTmp.charAt(nPos-4)!='.' && szTmp.charAt(nPos-5)!='.') passed = false;
		
	// Passed true
	if (passed == true) {
		$("[name="+formField+"]").css("borderColor","#6699CC");
		if (dGE(formField) != null) $("#"+formField).remove();
		return true;
	// Passed false
	} else {
		if (dGE(formField) == null)
			$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">Invalid E-mail address</span>");
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		return false;
	}
	return false;
}

function verifyString(currentField,formField) {
	var szValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	var szString = $("[name="+formField+"]").val();
	if(szString=='') return true;
	var passed = true;
	
	if (!verifyStringwKey(szString, szValid)) passed = false;
	// Passed true
	if (passed == true) {
		$("[name="+formField+"]").css("borderColor","#6699CC");
		if (dGE(formField) != null) $("#"+formField).remove();
		return true;	
	} else {
		if (dGE(formField) == null)
			$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " has invalid characters</span>");
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		return false;
	}
}

function verifyPassword(currentField,formField) {
	var szValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var szString = $("[name="+formField+"]").val();
	if(szString=='') return true;
	var passed = true;
	
	if (!verifyStringwKey(szString, szValid)) passed = false;
	// Passed true
	if (passed == true) {
		$("[name="+formField+"]").css("borderColor","#6699CC");
		if (dGE(formField) != null) $("#"+formField).remove();
		return true;	
	} else {
		if (dGE(formField) == null)
			$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " has invalid characters</span>");
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		return false;
	}
}

function verifyStringwKey(szString, szKey) {
	if(szString=='') return true;
	var i;
	for(i=0;i<szString.length;i++) {
		if(szKey.indexOf(szString.charAt(i))==-1)
			return false;
	}
	return true;
}

function verifyInteger(currentField, formField, nValMin, nValMax) {
	var szString = $("[name="+formField+"]").val();
	var szTmp = Trim(szString);
	if(szTmp=='') return true;
	// strip commas
	szTmp = ReplaceString(',','',szTmp)
	
	if(isNaN(szTmp)) {
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null)
			if (currentField != 'none') {
				$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " must contain numbers only</span>");
			}
		return false;
	}
	
	var nTmp = parseFloat(szTmp);
	// Less than min
	if(nValMin != "" && nTmp < nValMin) {
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null)
			if (currentField != 'none') {
				$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " must be more than " + nValMin + "</span>");
			}
		return false;
	}
	
	// More than min
	if(nValMax != "" && nTmp > nValMax) {
		$("[name="+formField+"]").css("borderColor","red");
		$("[name="+formField+"]").focus();
		if (dGE(formField) == null)
			$("[name="+formField+"]").parent().append(" <span id=\""+formField+"\" class=\"ct-red\">" + currentField + " must have less than " + nValMax + "</span>");
		return false;
	}
	$("[name="+formField+"]").css("borderColor","#6699CC");
	if (dGE(formField) != null) $("#"+formField).remove();
	return true;
}

// Trimming
function Trim(szString) {
	var res;
	res = TrimRight(szString);
	return TrimLeft(res);
}

function TrimRight(szString) {
	szlength = (szString ? szString.length : 0); 
	for(var i=szlength-1; i>=0; i--) {
		if(szString.charAt(i)!=' ')
			break;
	}
	if(i<0) return '';
	return szString.substring(i+1, 0);
}

function TrimLeft(szString) {
	szlength = (szString ? szString.length : 0); 
	for(var i=0; i<szlength; i++) {
		if(szString.charAt(i)!=' ')
			break;
	}
	if(szString.length==i) return '';
	return szString.substring(i, szString.length);
}

// ===================
// Replaces oldS with newS in the string fullS
function ReplaceString(oldS,newS,fullS) {
	fullSlength = (fullS ? fullS.length : 0); 
	for (var i=0; i<fullSlength; i++) {
		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
	return fullS;
}

// ===================
// Printing
function printSpecial(storeName, template, templateNo) {
	if (document.getElementById != null) {
		var htmlOutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n'+
		'<html xmlns="http://www.w3.org/1999/xhtml">\r\n'+
		'<head>\r\n'+
		'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\r\n'+
		'<meta name="Created By" content="ESyStore">\r\n'+
		'<link media="screen" rel="stylesheet" href="css/default.css" />\r\n' +
		'<link media="screen" rel="stylesheet" href="template/'+template+'/css/'+templateNo+'" />\r\n' +
		'<title>'+storeName+' :: Order Printout</title>\r\n'+
		'</head>\r\n'+
		'<body>\r\n' + 
		'<div class=\"printarea\">\r\n';
		
		var printReadyElem = document.getElementById("printme");
		if (printReadyElem != null) {
			htmlOutput += printReadyElem.innerHTML;
		} else {
			alert("Could not find the printing section in the HTML");
			return;
		}
		
		htmlOutput = htmlOutput + '</div>\r\n' +
		'</body>\r\n' +
		'</head>\r\n';
		
		var ver = navigator.appName;
		var num = parseInt(navigator.appVersion);
		var szPrintMe = '';
		if (!(ver == "Microsoft Internet Explorer")) {
			szPrintMe = "<script language='javascript'>setTimeout('window.print();', 2000);</script>";
		}
		
		var printWin = window.open("", "printSpecial", "left=20, top=20, width=800, height=600, scrollbars=yes, resizable=yes, location=no, address=no, toolbars=no");
		
		printWin.document.open();
		printWin.document.write(htmlOutput);
		printWin.document.close();
		printWin.location.reload();
		
		var ver = navigator.appName;
		var num = parseInt(navigator.appVersion);
		// don't auto print for mozilla
		
		if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
			printWin.print();
		}
	} else {
		alert("Sorry, the print ready feature is only available in modern browsers.");
	}
}
