//
// Author: Steve Hanson
// Tasen Software eCommerce
//
// Copyright 2006 Tasen Software
// This code may not be reproduced or distributed without the explicit permission of Tasen Software
//
var ajaxID = "";
var CART_SUBTOTAL = "ecomTotalItemSubtotal";
var CART_SHIPPING = "ecomTotalItemShipping";
var CART_TOTAL = "ecomTotalItemTotal";

function removeItem(inventoryID, key) {
	//document.write("Removing Item.");
	document.getElementById("qty["+inventoryID+"]["+key+"]").value=0;
	document.updateform.submit();
}

function validateEmail(addr,man,db,field) {
	if (addr.length==0 && man) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address is required<br /></font>";
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address contains invalid characters<br /></font>";
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address contains invalid characters<br /></font>";
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address must contain @<br /></font>";   
	   return false;
	}
	if (atPos == 0) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address must not start with @<br /></font>";      
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address must contain only one @<br /></font>";      
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>E-mail address must contain a period<br /></font>";      
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>Period must not immediately follow @<br /></font>";      
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>Period must not immediately precede @<br /></font>";      
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) document.getElementById(field).innerHTML="<font color='#FF0000'>Two periods must not be adjacent<br /></font>";         
	   return false;
	}
	//var suffix = addr.substring(addr.lastIndexOf('.')+1);
	/*if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}*/
	if (db) document.getElementById(field).innerHTML="";         
	return true;
}

function toggleShipping(on) {
	if(on) {
		document.getElementById("ecom_shippingBlock").style.visibility = "visible";
		document.getElementById("ecom_shippingBlock").style.height = "auto";
	}
	else {
		document.getElementById("ecom_shippingBlock").style.visibility = "hidden";
		document.getElementById("ecom_shippingBlock").style.height = "0px";
	}
	return
}

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var response = xmlHttp.responseText;
		if(ajaxID == "login") {
			if(response == "Logged In As") {	// Valid login.
				setLoggedIn(true);			
			}
			else {								// Invalid login, display error message.
				setLoggedIn(false);			
				loginID = "";
			}
		}
		else if(ajaxID == "reg") {
			if(response == "Logged In As") {	// Valid login.
				setLoggedIn(true);			
			}
			else {								// Invalid login, display error message.
				setLoggedIn(false);			
				loginID = "";
			}		
		}
		else if(ajaxID == "logoutCheckout1") {
			if(response == "true") {	// It worked.
				setLoggedIn(false);
				//document.write( "<br/>logout successful<br/>");
			}
			else {	// It didn't work.
				//document.write( "<br/>logout unsuccessful<br/>");
			}			
			//document.write("<br/>Returned from AJAX logout<br/>");
		}		
		else if(ajaxID == "logoutCheckout2") {
			if(response == "true") {	// It worked.
				document.location.href = "http://www.coloradocookiekitchen.com/checkout1.php";	
			}
			else {	// It didn't work.
				//document.write( "<br/>logout unsuccessful<br/>");
			}			
		}
	} 
} 

function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

function togglePayment(type) {
	if(type == "paypal") {
		document.getElementById("ecom_step3").style.visibility = "hidden";
		document.getElementById("ecom_step3").style.height = "0px";
		document.getElementById("ecom_billingBlock").style.visibility = "hidden";
		document.getElementById("ecom_billingBlock").style.height = "0px";	
		//document.getElementById("ecom_submitButtonAction").href = "javascript:document.checkoutform.submit()";		
		document.getElementById("ecom_submitButtonAction").href = "javascript:validateForm();";
		document.getElementById("ecom_paymentMessage").innerHTML="";		
		//document.getElementById("ecom_submitButtonAction").href = "#";		
		//document.getElementById("ecom_paymentMessage").innerHTML="We are currently testing PayPal transactions thoroughly for your safety.<br/>Check back Firday 12/15/2006 to use this payment method.";		
		document.getElementById("ecom_paymentMessage").style.visibility = "visible";
		document.getElementById("ecom_paymentMessage").style.height = "auto";	
		//document.checkoutform.action = document.getElementById("ecom_paypal_checkout").value + document.getElementById("ecom_paypal_period").value + document.getElementById("ecom_paypal_ext").value;					
	}
	else if(type == "other") {
		document.getElementById("ecom_step3").style.visibility = "hidden";
		document.getElementById("ecom_step3").style.height = "0px";
		document.getElementById("ecom_billingBlock").style.visibility = "hidden";
		document.getElementById("ecom_billingBlock").style.height = "0px";		
		//document.getElementById("ecom_submitButtonAction").href = "javascript:document.checkoutform.submit()";
		document.getElementById("ecom_submitButtonAction").href = "javascript:validateForm();";
		document.getElementById("ecom_paymentMessage").innerHTML="";		
		//document.getElementById("ecom_submitButtonAction").href = "#";
		//document.getElementById("ecom_paymentMessage").innerHTML="We are currently testing credit cards transactions thoroughly for your safety.<br/>Check back Friday 12/15/2006 to use this payment method.";				
		document.getElementById("ecom_paymentMessage").style.visibility = "visible";
		document.getElementById("ecom_paymentMessage").style.height = "auto";				
		//document.checkoutform.action = document.getElementById("ecom_other_checkout").value + document.getElementById("ecom_other_period").value + document.getElementById("ecom_other_ext").value;					
	}
	else if(type == "amex") {
		document.getElementById("ecom_step3").style.visibility = "visible";
		document.getElementById("ecom_step3").style.height = "auto";
		document.getElementById("ecom_billingBlock").style.visibility = "visible";
		document.getElementById("ecom_billingBlock").style.height = "auto";		
		//document.getElementById("ecom_submitButtonAction").href = "javascript:document.checkoutform.submit()";
		document.getElementById("ecom_submitButtonAction").href = "javascript:validateForm();";		
		document.getElementById("ecom_paymentMessage").innerHTML="";
		document.getElementById("ecom_paymentMessage").style.visibility = "hidden";
		document.getElementById("ecom_paymentMessage").style.height = "0px";	
		//document.checkoutform.action = document.getElementById("ecom_amex_checkout").value + document.getElementById("ecom_amex_period").value + document.getElementById("ecom_amex_ext").value;			
	}

	document.getElementById("ecom_paymentType").value = type;
	
	return;
}

function prepCheckoutForm() {	
	//document.getElementById("ecom_rbPaymentAmex").checked = true;
	//document.getElementById("ecom_rbPaymentPaypal").checked = false;
	//document.getElementById("ecom_rbPaymentOther").checked = false;
	//togglePayment("amex");
	return;
}

function validateForm() {
	var type = document.getElementById("ecom_paymentType").value;
	
	if(type == "paypal") { }
	else if(type == "other") { }
	else if(type == "authorize") { }
	else if(type == "amex") {
		//alert("validating amex");
		
		var email1 = document.getElementById("ecom_billingEmail").value;
		if(! email1.length > 0) {
			//alert("email 1 too short");
			document.getElementById("ecom_billing_email_1").innerHTML="<font color='#FF0000'>E-mail address is required<br /></font>";
			//event.returnValue=false;
			return;
		}
		else {
			if(! (validateEmail(email1,1,1,"billing_email_1"))) {
				return;
			}
		}
		
		var email2 = document.getElementById("ecom_billingEmail2").value;
		if(! email2.length > 0) {
			//alert("email 2 too short");			
			document.getElementById("ecom_billing_email_2").innerHTML="<font color='#FF0000'>E-mail address is required<br /></font>";
			//event.returnValue=false;
			return;
		}
		else {
			if(! (validateEmail(email2,1,1,"billing_email_2"))) {
				return;
			}
		}
		
		if(email1 != email2) {
			//alert("emails do not match");
			document.getElementById("ecom_billing_email_2").innerHTML="<font color='#FF0000'>E-mail addresses do not match<br /></font>";
			//event.returnValue=false;
			return;
		}
		else {
			//document.getElementById("ecom_billing_email_2").innerHTML="";
		}			
	}
	
	document.checkoutform.submit();
}

function validateCheckout1Form() {
	document.checkoutform.submit();
}

function validateCheckout2Form() {
	document.checkoutform.submit();
}

function prepCheckout1(username) {
		if(username.length>1) {
			loginID = username;
			setLoggedIn(true);					
		}
		else {
			setLoggedIn(false);
			loginID = "";			
		}
}

function prepCheckout2(username) {
		if(username.length>1) {
			loginID = username;			
			setLoggedIn(true);
		}
		else {
			setLoggedIn(false);
			loginID = "";			
		}
}

function toggleCartShipping(fieldID) {
	//document.write("Toggle cart shipping, field ID: " + fieldID);
	var index = document.getElementById(fieldID).selectedIndex;
	var shipValue = document.getElementById(fieldID).options[index].value;	// Get the value related to selected shipping option
	document.getElementById(CART_SHIPPING).innerHTML = shipValue;		// Update the shipping total
	var itemSubtotal = document.getElementById(CART_SUBTOTAL).innerHTML;	// Get cart subtotal
	var total = parseFloat(itemSubtotal) + parseFloat(shipValue);
	document.getElementById(CART_TOTAL).innerHTML = total.toFixed(2);	// Update the cart total
	document.getElementById("txtShippingSelect").value = shipValue;			// Used when Updating the cart
}