function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function check_get_mailing_list_info(theForm){
	if( is_email_valid(theForm.required_email.value) ){
		return true;
	}else{
		alert("The email address you provided is not valid.\n(eg. myemail@mydomain.com)\n\nPlease enter a valid email address");	
		theForm.required_email.focus();
		return false;
	}
}



// Validate the Mailing List Form
function check_mailing_list(theForm){
	var num_chars = 3;
	
	// Make sure they are adding themselves to something.
	if( theForm.isupdate.value == "0" ){
	
		if( !theForm.el.checked && !theForm.ml.checked ){
			alert("You must choose to be added to the Email List and/or the Snail Mail List\n\nCheck either Email List and/or Snail Mail List to add yourself to that mailing list.");
			theForm.el.focus();
			return false;
		}
	}
	
	var ret = check_req(theForm);
	if( !ret )
		return false;
	
		
	ret = is_email_valid(theForm.required_email.value);
	if( !ret ){
		alert("Your return email address is not valid (usually it is in the email@mydomain.com format).\nPlease enter your current email address in the field named \"Email\"");
		theForm.required_email.focus();
			
		return false;
	}
	
	// Check required snail mail fields....
	if( theForm.ml.checked ){
		for (i = 0; i < theForm.length; i ++){
			
			var tempobj = theForm.elements[i];
			if (tempobj.name.substring(0,num_chars) == "ml_") {
					
				shortFieldName = tempobj.name.substring(num_chars).toUpperCase();
				shortFieldName = Replace(shortFieldName, "_", " ");
				if ( tempobj.value == '' ) {
					alert("Please enter a value in the field named:\n" + shortFieldName);
					return false;  // STOP FORM SUBMIT HERE, NO NEED TO CONTINUE
		   		}
		   	}
			   	
		}
	}
	
	return true;
}


// Validate the Contact Form 
function check_contact_req(theForm){
	var ret = check_req(theForm);
	
	if( !ret )
		return false; // stop here.
		
	// otherwise check email..
	
	ret = is_email_valid(theForm.required_email.value);
	
	if( !ret )	{
		alert("Your return email address is not valid (usually it is in the email@mydomain.com format).\nPlease enter your current email address in the field named \"Your Email\" so that the contact may respond.\n\nMessage Not Sent!!\n");
		theForm.required_email.focus();
		
		return false;
	}
	
	return true;	
}

function check_send_email(theForm){
	var ret = check_req(theForm);
	var email_good = true;
	var arr;
	
	if( !ret )
		return false; // Stop form
		
	if( theForm.testonly.value == "1" ){
		// Check test email
		if( !is_email_valid(theForm.testemailaddr.value) ){
			alert("The email provided for the test email is not valid\nPlease provide a valid email address\n\nTest Not Sent");
			theForm.testemailaddr.focus();		
			return false;
		}
		
	}
	
	
	if( !confirm("Are you sure you want to send these mass emails?") )
		return false;
	
		
	// Everything is cool... Send....
	return true;
}


/**
* Check that required_ fields have 
* a value... 
**/

function check_req(which){
	var pass = true;
	var num_chars = 9;
	var lcase_name = "";
	var shortFieldName = "";
	
	for (i = 0; i < which.length; i ++){
		
		var tempobj = which.elements[i];
		if (tempobj.name.substring(0,num_chars) == "required_"){
			
			shortFieldName = tempobj.name.substring(num_chars).toUpperCase();
			shortFieldName = Replace(shortFieldName, "_", " ");
			if ( tempobj.value == '' ){
				alert("Please enter a value in the field named:\n" + shortFieldName);
				return false;  // STOP FORM SUBMIT HERE, NO NEED TO CONTINUE
	   		}
	   	}
	   	
	}
	
	return true;
}

/**
* Email address validation functions...
**/

function is_email_valid(strEmail){
	arr = strEmail.split("@");
			
	if( arr.length != 2 ){
		return false;
	}else if( !valid_domain( arr[1] )){
		return false;
	}else if( !valid_email_name(arr[0]) ){
		return false;
	}
		
	return true;
}

function valid_domain(strDomain){
	// These the only valid characters allowed in a domain name...
	// besides periods...
	var i = 0;
	//strDomain = strDomaun.toLowerCase();
	var arr = strDomain.split(".");
	
	if( arr.length < 2 ){
		return false;
	}
	
	for( i = 0; i < arr.length; i++ ) {
		if( !valid_domain_field( arr[i] ) ) {
			return false;
		}	
	}

	return true;
}

function valid_domain_field(strField){
	var valid = "abcdefghijklmnopqrstuvwxyx0124567890-";
	var i = 0, m = 0;
	var isValid = false;
	
	strField = strField.toLowerCase();
	
	if( strField == "" )
		return false; // if period get are together to at end or beginning, there will be blanks
		
	for( i = 0; i < strField.length; i++ ){
		isValid = false;
		for( m = 0; m < valid.length; m++ ){
			if( strField.charAt(i) == valid.charAt(m) ){
				isValid = true;
				break;
			}
		}
		if( !isValid )
			return false;
	}
	
	return true;
}

function valid_email_name(strName){
	var valid = "abcdefghijklmnopqrstuvwxyz0123456789-_.";
	var isValid = false;
	var i = 0;
	var m = 0;
	var c;
	
	strName = strName.toLowerCase();
	
	if( strName == "" )
		return false; // Bad @ placement...
	
	for( i = 0; i < strName.length; i++ ){
		
		c = strName.charAt(i);
		isValid = false
		for( m = 0; m < valid.length; m++ ){
			if( c == valid.charAt(m) ){
				isValid = true;
				break;
			}
		}
	
		if( !isValid )
			return false;
		
	}
	return true;
}

/**
* String replacement function..
**/
function Replace(Expression, Find, Replace){
	var temp = Expression;
	var a = 0;
	
	while( ( a = temp.indexOf(Find) ) > -1 ){
		temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length));
	}

	return temp;
}

function OnChangeShipSameAsOrd(){
	// Disables Shipping fields if the "Shipping info is same as billing" checkbox is checked
	if (document.frmPayment.shipSameAsOrd.checked == true){
		dControl = true
	}else{
		dControl = false
	}

	DisableControl(document.frmPayment.shipName, dControl)
	DisableControl(document.frmPayment.shipEmailAddress, dControl)
	DisableControl(document.frmPayment.shipPhoneNumber, dControl)
	DisableControl(document.frmPayment.shipAddress1, dControl)
	DisableControl(document.frmPayment.shipAddress2, dControl)
	DisableControl(document.frmPayment.shipCity, dControl)
	DisableControl(document.frmPayment.shipProvince, dControl)
	DisableControl(document.frmPayment.shipPostalCode, dControl)
	DisableControl(document.frmPayment.shipCountry, dControl)
}

function DisableControl(objName, dControl){
	// Disables a form object
	if (dControl == true){
		objName.disabled = true
		objName.style.background = "E5E5E5"
	}else{
		objName.disabled = false
		objName.style.background = "FFFFFF"
	}
}

function autoSubmit(){
     document.form.submit();
} 

function checkrequired(which) {

var pass = true;
var num_chars = 9;

if (document.images) {

	for (i = 0; i < which.length; i ++) {
	var tempobj = which.elements[i];

		if (tempobj.name.substring(0,num_chars) == "required_") {
			if (((tempobj.type == "text" || tempobj.type == "textarea") &&
				tempobj.value == '') || (tempobj.type.toString().charAt(0) == "s" &&
				tempobj.selectedIndex == 0)) {
				pass = false;
				break;
      }
    }
  }
}

if (!pass) {
	shortFieldName = tempobj.name.substring(num_chars,30).toUpperCase();
	alert("Please enter a value in the " + shortFieldName + " field.");
	return false;
}
else
	return true;
}// end function

// java pane window
function item_details(x) {
URL = "item_details.php?item_id="+x;
eval("page" + x + " = window.open(URL, '" + x + "', 'toolbar=0,scrollbars=1,location=no,statusbar=0,menubar=0,resizable=1,width=600,height=800,screenX=100,screenY=60,left=50,top=75');");
}

function map(){
open('map.php','Location','toolbar=no,menubar=no,scrollbars=0,width=600,height=400,resizable=yes')
}
// closes window
function closeresults() {
	resultsWindow = window.close("results");
}

// function for select box ordering
function move(index,to) {
 	var list = document.form.list;
 	var total = list.options.length-1;
 	if (index == -1) return false;
 	if (to == +1 && index == total) return false;
 	if (to == -1 && index == 0) return false;
 	var items = new Array;
 	var values = new Array;
 	for (i = total; i >= 0; i--) {
 		items[i] = list.options[i].text;
 		values[i] = list.options[i].value;
 	}
 	for (i = total; i >= 0; i--) {
 		if (index == i) {
 			list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
 			list.options[i] = new Option(items[i + to], values[i]);
 			i--;
 		}else{
 			list.options[i] = new Option(items[i], values[i]);
 	   	}
 	}
 	list.focus();
}

function submitFormCats() {
 var list = document.form.list;
 var cat_type = document.form.cat_type.value;
 
 var theList = "&";
 // start with a "?" to make it look like a real query-string
 for (i = 0; i <= list.options.length-1; i++) {
 	theList += "list" + list.options[i].value + "=" + escape(list.options[i].text.replace(/&/g,"&amp;"));
 	// a "&" only BETWEEN the items, so not at the end
 	if (i != list.options.length-1) theList += "&";
 }
 	location.href = "admin.php?content=mod_cats&cat_type="+cat_type+"&action=cat_update_order" + theList;
}

function submitFormSubCats() {
 var list = document.form.list;
 var cat_type = document.form.cat_type.value;
 var cat_id = document.form.cat_id.value;
 
  
 var theList = "&";
 // start with a "?" to make it look like a real query-string
 for (i = 0; i <= list.options.length-1; i++) {
 	theList += "list" + list.options[i].value + "=" + escape(list.options[i].text.replace(/&/g,"&amp;"));
 	// a "&" only BETWEEN the items, so not at the end
 	if (i != list.options.length-1) theList += "&";
 }
 	location.href = "admin.php?content=mod_cats&cat_type=" + cat_type + "&cat_id=" + cat_id + "&action=sub_cat_update_order" + theList; 	
}


function submitFormItems() {
 var list = document.form.list;
 var type_id = document.form.type_id.value;
 var cat_id = document.form.cat_id.value;
 
 
 var theList = "&";
 // start with a "?" to make it look like a real query-string
 for (i = 0; i <= list.options.length-1; i++) {
 	theList += "list" + list.options[i].value + "=" + escape(list.options[i].text.replace(/&/g,"&amp;"));
 	// a "&" only BETWEEN the items, so not at the end
 	if (i != list.options.length-1) theList += "&";
 }
 	location.href = "admin.php?content=items&cat_id=" + cat_id + "&type_id=" + type_id + "&action=item_update_order" + theList;
}