var alphaNumExp = /^[0-9a-zA-Z]+$/;
var numExp = /^[0-9]+$/;
var alphaExp = /^[a-zA-Z' ]+$/;
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

function openCartPage(page) {
	//if(page == 0 && userid != 0) {
		//page = 1;
		//populateBillingShipping();
	//}
	for(var i=0;i<numPages;i++) {
		if(i != page) document.getElementById("goo_shoppingcart"+i).style.display = 'none';
	}
	document.getElementById("goo_shoppingcart"+page).style.display = 'block';
}

function saveCartData(theform) {
	var uform = eval("document."+theform);
	goojax_savedata(uform,'goodirector/shoppingcart/display_engine.php?site_id='+site_id);
}

function continueShopping() {
	var url = goosite_readCookie('goo_page_catalog_url');
	window.location = url;
}

function saveCartQuantities(remid) {
	if(!remid) remid = 0;
	var cartids = document.getElementById('cartids').value;
	var cartqs = cartids.split(",");
	var cart_id = document.cartcontentform.cart_id.value;
	var cqstring = "";
	for(var i=0;i<cartqs.length;i++) {
		if(document.getElementById('cq_'+cartqs[i])) {
			cqstring += document.getElementById('cq_'+cartqs[i]).value+",";
		}
	}
	goojax_savedata(document.cartcontentform,'/goodirector/shoppingcart/display_engine.php?site_id='+site_id+'&action=1&cartids='+cartids+'&qs='+cqstring+'&cart_id='+cart_id+'&remid='+remid);
	
	//saveCartData("cartcontentform");
}

function updateCartItems() {
	var cart_id = document.cartcontentform.cart_id.value;
	goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=7&cart_id='+cart_id);
}

function removeCartItem(id) {
	saveCartQuantities(id);
}

function registerNewUser() {
	var errorString = "";
	
	var theform = document.userregister;
	if(theform.sc_user_name.value.length < 6) {
		errorString += "Username must be at least 6 characters long.<br>";
	} else if(!theform.sc_user_name.value.match(alphaNumExp)) {
		errorString += "Username can only contain letters and numbers.<br>";
	}
	
	if(theform.sc_first_name.value.length == 0) {
		errorString += "First Name is required.<br>";
	} else if(!theform.sc_first_name.value.match(alphaExp)) {
		errorString += "First Name can only contain letters.<br>";
	}
	
	if(theform.sc_last_name.value.length == 0) {
		errorString += "Last Name is required.<br>";
	} else if(!theform.sc_last_name.value.match(alphaExp)) {
		errorString += "Last Name can only contain letters.<br>";
	}
	
	if(theform.sc_email.value.length == 0) {
		errorString += "Email is required.<br>";
	} else if(!theform.sc_email.value.match(emailExp)) {
		errorString += "Email address is invalid.<br>";
	} else if(theform.sc_email.value != theform.sc_email_confirm.value) {
		errorString += "Email addresses do not match.<br>";
	}
	
	if(theform.sc_user_pass.value.length < 6) {
		errorString += "Password must be at least 6 characters long.<br>";
	} else if(theform.sc_user_pass.value != theform.sc_user_pass_confirm.value) {
		errorString += "Passwords do not match.<br>";
	}
	
	if(errorString.length == 0) {
		saveCartData('userregister');
	}
	document.getElementById('registerErrors').innerHTML = errorString;
	
}

function populateBillingShipping() {
	goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=2&id='+userid);
}



function sameAsBilling() {
	goojax_load_data('/goodirector/shoppingcart/display_engine.php','site_id='+site_id+'&action=5&uid='+userid);
	
	/*for(var i=0;i<11;i++) {
		document.shippinginfo.elements[i+1].value = document.billinginfo.elements[i].value;
	}*/
	//checkDeliveryZip();
}

function saveBilling() {
	var theform = document.billinginfo;
	
	var errorString = "";
	
	if(theform.site_fname.value.length == 0) {
		errorString += "First Name is required.<br>";
	} else if(!theform.site_fname.value.match(alphaExp)) {
		errorString += "First Name can only contain letters.<br>";
	}
	
	if(theform.site_lname.value.length == 0) {
		errorString += "Last Name is required.<br>";
	} else if(!theform.site_lname.value.match(alphaExp)) {
		errorString += "Last Name can only contain letters.<br>";
	}
	
	if(theform.site_address1.value.length < 3) {
		errorString += "Address is required.<br>";
	}
	
	if(theform.site_country.value.length == 0) {
		errorString += "Country is required.<br>";
	}
	
	if(theform.site_city.value.length == 0) {
		errorString += "City is required.<br>";
	}
	
	if((theform.site_state2.value.length == 0 && theform.site_country.value == "United States") || (theform.site_state1.value.length == 0 && theform.site_country.value != "United States")) {
		errorString += "State/Province is required.<br>";
	}
	
	if(theform.site_zip.value.length == 0) {
		errorString += "Zipcode is required.<br>";
	}
	
	if(theform.site_phone.value.length == 0) {
		errorString += "Phone number is required.<br>";
	}
	
	
	
	document.getElementById("billingErrors").innerHTML = errorString;
	
	
	
	if(errorString.length == 0) {
		if(theform.site_country.value == "United States") theform.site_state.value = theform.site_state2.value;
		else theform.site_state.value = theform.site_state1.value;
		document.billinginfo.userid.value = userid;
		saveCartData('billinginfo');
	}
}


function saveShipping() {
	var theform = document.shippinginfo;
	var errorString2 = "";
	
	if(theform.ship_fname.value.length == 0) {
		errorString2 += "First Name is required.<br>";
	} else if(!theform.ship_fname.value.match(alphaExp)) {
		errorString2 += "First Name can only contain letters.<br>";
	}
	
	if(theform.ship_lname.value.length == 0) {
		errorString2 += "Last Name is required.<br>";
	} else if(!theform.ship_lname.value.match(alphaExp)) {
		errorString2 += "Last Name can only contain letters.<br>";
	}
	if(theform.deliverytype.value == "Shipping - Standard" || theform.deliverytype.value == "Shipping - USPS Priority" || theform.deliverytype.value == "Shipping - USPS Express") {
		
		if(document.getElementById("ship_address1_1").value.length == 0) {
			errorString2 += "Address is required.<br>";
		}
		if(document.getElementById("ship_country").value.length == 0) {
			errorString2 += "Country is required.<br>";
		}
		if((theform.ship_state2.value.length == 0 && theform.ship_country.value == "United States") && (theform.ship_state1.value.length == 0 && theform.ship_country.value != "United States")) {
			errorString2 += "State/Province is required.<br>";
		}
		if(document.getElementById("ship_zip_Shipping").value.length == 0) {
			errorString2 += "Zipcode is required.<br>";
		}
		
		
	}
	
	if(theform.deliverytype.value == "Delivery") {

		if(document.getElementById("ship_address1_2").value.length == 0) {
			errorString2 += "Address is required.<br>";
		}
		
		if(document.getElementById("ship_zip_Delivery").value.length == 0) {
			errorString2 += "Zipcode is required.<br>";
		}
	}
	/*if(theform.ship_phone.value.length == 0) {
		errorString2 += "Phone number is required.<br>";
	}*/
	
	if(theform.ship_email.value.length == 0) {
		errorString2 += "Email is required.<br>";
	} else if(!theform.ship_email.value.match(emailExp)) {
		errorString2 += "Email address is invalid.<br>";
	}
	document.getElementById("shippingErrors").innerHTML = errorString2;
	var errored = 0;
	if(document.shippinginfo.deliverytype.value == "Delivery" && (document.getElementById("zipErrors").innerHTML != "" || document.shippinginfo.timeError.value == 1)) errored = 1;
	if(document.shippinginfo.deliverytype.value == "Pick-up" && !document.getElementById('pickup_time2')) errored = 1;
	if(errorString2.length == 0 && errored != 1) {
		if(theform.deliverytype.value == "Shipping - Standard" || theform.deliverytype.value == "Shipping - USPS Priority" || theform.deliverytype.value == "Shipping - USPS Express" || theform.deliverytype.value == "Delivery") {
			if(theform.ship_country.value == "United States") theform.ship_state.value = theform.ship_state2.value;
			else theform.ship_state.value = theform.ship_state1.value;
			if(theform.deliverytype.value == "Shipping - Standard" || theform.deliverytype.value == "Shipping - USPS Priority" || theform.deliverytype.value == "Shipping - USPS Express") {
				document.shippinginfo.ship_zip.value = document.getElementById("ship_zip_Shipping").value;
				document.shippinginfo.ship_address1.value = document.getElementById("ship_address1_1").value;
				document.shippinginfo.ship_address2.value = document.getElementById("ship_address2_1").value;
				if(theform.deliverytype.value == "Shipping - USPS Priority") {
					theform.ship_price.value = theform.ship_price_usps_priority.value;
					theform.delivery_split.value = theform.shipping_split_usps_priority.value;
				} else if(theform.deliverytype.value == "Shipping - USPS Express") {
					theform.ship_price.value = theform.ship_price_usps_express.value;
					theform.delivery_split.value = theform.shipping_split_usps_express.value;
				} else if(theform.deliverytype.value == "Shipping - Standard") {
					theform.ship_price.value = theform.ship_price_standard.value;
				}
				//if(theform.ship_price.value <= 0) errored = 1;
			} else {
				document.shippinginfo.ship_zip.value = document.getElementById("ship_zip_Delivery").value;
				document.shippinginfo.ship_address1.value = document.getElementById("ship_address1_2").value;
				document.shippinginfo.ship_address2.value = document.getElementById("ship_address2_2").value;
				//alert(document.getElementById("ship_address2_1").value);
			}

			
			//document.shippinginfo.ship_carrier.value = document.getElementById("shipping_rate_"+document.shippinginfo.ship_method.value).value;

		}
        //alert(errored);
		document.shippinginfo.userid.value = userid;
		if(document.shippinginfo.deliverytype.value == "Delivery") {
			document.getElementById("delivery_time").value = document.getElementById('delivery_time2').value;
			document.getElementById("shipping_rate2").value = document.getElementById('delivery_rate').value;
		}
		if(document.shippinginfo.deliverytype.value == "Pick-up") document.getElementById("delivery_time").value = document.getElementById('pickup_time2').value;
		if(errored != 1) saveCartData('shippinginfo');
		
	}
}

function saveExtras() {
	saveCartData('extrasinfo');
}

function selectDeliveryType() {
	document.getElementById("shippingErrors").innerHTML = '';
	var dtype = document.shippinginfo.deliverytype.value;
	document.getElementById("address_inputs_1").style.display = 'none';
	document.getElementById("address_inputs_2").style.display = 'none';
	document.getElementById("address_inputs_4").style.display = 'none';
	document.getElementById("options_USPS_express").style.display = 'none';
	document.getElementById("options_USPS_priority").style.display = 'none';
	if(dtype != "") {
		document.getElementById("address_inputs_neg").style.display = 'block';
		document.getElementById("address_inputs_neg2").style.display = 'block';
		document.getElementById("delivery_title").style.display = 'block';
	} else {
		document.getElementById("address_inputs_neg").style.display = 'none';
		document.getElementById("address_inputs_neg2").style.display = 'none';
		document.getElementById("delivery_title").style.display = 'none';
	}
	if(dtype == "Shipping - Standard" || dtype == "Shipping - USPS Priority" || dtype == "Shipping - USPS Express") {
		document.getElementById("delivery_delivery").style.display = 'none';
		document.getElementById("delivery_pickup").style.display = 'none';
		if(dtype == "Shipping - Standard") {
			document.getElementById("delivery_shipping_standard").style.display = 'block';
			document.getElementById("delivery_shipping_usps").style.display = 'none';
		} else {
			document.getElementById("delivery_shipping_standard").style.display = 'none';
			document.getElementById("delivery_shipping_usps").style.display = 'block';
			if(dtype == "Shipping - USPS Priority") {
				document.getElementById("options_USPS_priority").style.display = 'block';
			} else if(dtype == "Shipping - USPS Express"){
				document.getElementById("options_USPS_express").style.display = 'block';
			}
		}
		document.getElementById("address_inputs_1").style.display = 'block';
		document.getElementById("del_policy_delivery").style.display = 'none';
		document.getElementById("del_policy_pickup").style.display = 'none';
		document.getElementById("del_policy_shipping").style.display = 'block';
	} else if(dtype == "Delivery") {
		document.getElementById("delivery_delivery").style.display = 'block';
		document.getElementById("delivery_pickup").style.display = 'none';
		document.getElementById("delivery_shipping_standard").style.display = 'none';
		document.getElementById("delivery_shipping_usps").style.display = 'none';
		document.getElementById("address_inputs_2").style.display = 'block';
		document.getElementById("del_policy_delivery").style.display = 'block';
		document.getElementById("del_policy_pickup").style.display = 'none';
		document.getElementById("del_policy_shipping").style.display = 'none';
	} else if(dtype == "Pick-up") {
		document.getElementById("delivery_delivery").style.display = 'none';
		document.getElementById("delivery_pickup").style.display = 'block';
		document.getElementById("delivery_shipping_standard").style.display = 'none';
		document.getElementById("delivery_shipping_usps").style.display = 'none';
		document.getElementById("address_inputs_4").style.display = 'block';
		document.getElementById("del_policy_delivery").style.display = 'none';
		document.getElementById("del_policy_pickup").style.display = 'block';
		document.getElementById("del_policy_shipping").style.display = 'none';
	} else {
		document.getElementById("delivery_title").style.display = 'none';
		document.getElementById("delivery_delivery").style.display = 'none';
		document.getElementById("delivery_pickup").style.display = 'none';
		document.getElementById("delivery_shipping_standard").style.display = 'none';
		document.getElementById("delivery_shipping_usps").style.display = 'none';
		document.getElementById("del_policy_delivery").style.display = 'none';
		document.getElementById("del_policy_pickup").style.display = 'none';
		document.getElementById("del_policy_shipping").style.display = 'none';
	}
	//goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=13&dtype='+dtype);
	//checkDeliveryZip();
}

function checkDeliveryZip() {
	if(document.shippinginfo.ship_zip_Delivery.value.length >= 5 && document.shippinginfo.deliverytype.value == 'Delivery') {
		goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=3&zipcode='+document.shippinginfo.ship_zip_Delivery.value);
	} else {
		document.getElementById("zipErrors").innerHTML = "";
	}
	//updateDeliveryTimes();
}



/*function updatePickupTimes() {
	
}*/


function changeCCType(elem) {
	if(elem.value == "PayPal") {
		document.getElementById("paypal_payment").style.display = "block";
		document.getElementById("cc_payment").style.display = "none";
	} else if (elem.value == "Check / Cash") {
		document.getElementById("paypal_payment").style.display = "none";
		document.getElementById("cc_payment").style.display = "none";
	} else {
		document.getElementById("paypal_payment").style.display = "none";
		document.getElementById("cc_payment").style.display = "block";
	}
}


function submitCheckout(skipcc) {
	var elem = document.getElementById("checkoutButton");
	if(skipcc == 0) document.getElementById("cc_expire_month").value = document.getElementById("cc_expire_month_temp").value;
	elem.disabled = true;
	elem.value = "Please Wait...";
	saveCartData('checkoutinfo');
}

function removeCoupon(id) {
	goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=10&couponid='+id);
}

var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function checkExpirationMonths(elem) {
	var myDate=new Date();
	var sMonth = document.getElementById("cc_expire_month_temp").value;
	var newhtml = '<table border="0" cellpadding="0" cellspacing="0"><tr><td><select name="cc_expire_month_temp" id="cc_expire_month_temp">';
	var monthvalue = 0;

	if(elem.selectedIndex == 0) {
		var month = myDate.getMonth();
		for(var i=month;i<12;i++) {
			if(i+1 < 10) monthvalue = '0'+(i+1);
			else monthvalue = i+1;
			var selected = monthvalue == sMonth ? "selected" : "";
			newhtml += '<option value="'+monthvalue+'" '+selected+'>'+m_names[i]+'</option>';
		}
	} else {
		for(var i=0;i<12;i++) {
			if(i+1 < 10) monthvalue = '0'+(i+1);
			else monthvalue = i+1;
			var selected = monthvalue == sMonth ? "selected" : "";
			newhtml += '<option value="'+monthvalue+'" '+selected+'>'+m_names[i]+'</option>';
		}
	}
	document.getElementById("expiremonth").innerHTML = newhtml+"</td></tr></table>";
	
}

function showCVVExplanation(ext) {
	var cc = document.getElementById("sc_payment_method").value;
	var w = 450;
	var h = 450;
	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);
	window.open('/goodirector/shoppingcart/display_cvv.php?cvv=1'+ext,'CVVExpl','width='+w+',height='+h+',scrollbars=1,status=0,left='+left+',top='+top);
}

function selectCountry(elem,type) {
	if(elem.value == "United States") {
		document.getElementById(type+"statedrop").style.display = 'block';
		document.getElementById(type+"statetext").style.display = 'none';
	} else {
		document.getElementById(type+"statedrop").style.display = 'none';
		document.getElementById(type+"statetext").style.display = 'block';
	}
}


function updateOrders() {
}

function forgotUserPassDisp() {
	document.getElementById("forgotuserpass").style.display = 'none';
	document.getElementById("forgotemailspan").style.display = 'block';
	document.getElementById("forgotemail").focus();
}

function sendUserPassDisp() {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var email = document.getElementById("forgotemail").value;
	if(!email.match(emailExp)) {
		document.getElementById("forgoterror").innerHTML = "E-mail address is invalid, please try again.";
		document.getElementById("forgotemail").focus();
	} else {
		goojax_load_data('/goodirector/shoppingcart/display_engine.php','site_id='+site_id+'&email='+email+'&action=13');
	}
}


function selectShippingMethod(which) {
	var whichAr = Array('STD','USPS');
	for(var i=0;i<whichAr.length;i++) {
		if(whichAr[i] == which) document.getElementById("options_"+whichAr[i]).style.display = 'block';
		else document.getElementById("options_"+whichAr[i]).style.display = 'none';
	}
	curMethod = which;
}

function cartLogin() {
	var uname = document.userlogin.sc_login_name.value;
	var pass = document.userlogin.sc_login_pass.value;
	goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=1&uname='+uname+'&pass='+pass+'&cartid='+cart_id);
	
}

function sendToSecure(sid) {
	//alert("wut");
	window.location = 'https://secure.goozmo.com/goodirector/useraccounts/secure.php?site_id='+site_id+'&sid='+sid+'&cart_id='+cart_id;
}

function updateDeliveryTimes() {
	if(document.shippinginfo.deliverytype.value == "Shipping - USPS Priority" || document.shippinginfo.deliverytype.value == "Shipping - USPS Express") {
		var zipVal = document.shippinginfo.ship_zip_Shipping.value;
		if(zipVal.length >= 5) {
			//alert(document.shippinginfo.ship_usps_priority_ids.value);
			var ship_usps_priority_ids = document.shippinginfo.ship_usps_priority_ids.value;
			var ship_usps_express_ids = document.shippinginfo.ship_usps_express_ids.value;
			goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=16&zip='+zipVal+'&cart_id='+cart_id+'&ship_usps_priority_ids='+ship_usps_priority_ids+'&ship_usps_express_ids='+ship_usps_express_ids);
		}
	} else if(document.shippinginfo.deliverytype.value == "Delivery") {
		var curVal = document.shippinginfo.delivery_date.value;
		if(curVal.length < 10) return;
		var zipVal = document.shippinginfo.ship_zip_Delivery.value;
		goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=4&curdate='+curVal+'&zip='+zipVal+'&cart_id='+cart_id);
	} else if(document.shippinginfo.deliverytype.value == "Pick-up") {
		var curVal = document.shippinginfo.pickup_date.value;
		if(curVal.length < 10) return;
		goojax_load_data('/goodirector/shoppingcart/display_data.php','site_id='+site_id+'&action=12&curdate='+curVal);
	}
}

function addCoupon() {
	//alert('hi'+document.getElementById('coupon_code').value);
	var code = document.getElementById('coupon_code').value;
	if(code.length == 0) {
		document.getElementById('couponerror').innerHTML = "Please enter a code first.";
	} else {
		document.getElementById('coupon_code').value = "";
		goojax_load_data('/goodirector/shoppingcart/display_engine.php','site_id='+site_id+'&action=12&code='+code+'&cartid='+cart_id);
	}
}

function gotoPage(pg,suf) {
	var ending = "";
	if (suf) ending = "&"+suf;
    //alert(ending);
	goojax_load_data('/goodirector/shoppingcart/display_engine.php','site_id='+site_id+'&action=gotopage&page='+pg+ending);
}
