function selectNew(){
	document.getElementById("choice").style.display = "none";
	document.getElementById("new").style.display = "block";
}

function selectReturn(){
	document.getElementById("choice").style.display = "none";
	document.getElementById("return").style.display = "block";
}

function back(display, sessionID, type){
	document.getElementById("error").innerHTML = "";
	document.getElementById("window").style.display = "none";
	
	switch (display) {
		case "choice": 
			document.getElementById("choice").style.display = "block";
			document.getElementById("new").style.display = "none";
			document.getElementById("return").style.display = "none"; 
			break;
		case "step2": 
			removeCoupon(sessionID);
			getOrderDetails(sessionID,type,'0.00');
			document.getElementById("step1").style.display = "block";
			document.getElementById("step2").style.display = "none";
			break;
		case "step3": 
			removeMembership(sessionID);
			getOrderDetails(sessionID,type,'0.00');
			document.getElementById("step1").style.display = "none";
			document.getElementById("step2").style.display = "block";
			document.getElementById("step3").style.display = "none";
			break;
		default: 
			document.getElementById("choice").style.display = "block";
			document.getElementById("new").style.display = "none";
			document.getElementById("return").style.display = "none";
	}
}

function checkNew(sessionID, referrer){
	var e = document.getElementById("newEmail").value;
	var p = document.getElementById("newPassword").value;
	var p2 = document.getElementById("newPassword2").value;
	var err = "";
	document.getElementById("error").innerHTML = "";
	
	if(!checkemail(e)){
		err += "Your email address must be valid<br>";
	}
	
	if(p == ""){
		err += "Please choose a password<br>";
	} else if (p.length < 6){
		err += "Your password must be at least 6 characters long<br>";
	}
	
	if(p != p2){
		err += "Your confirmed password must match your password<br>";
	}
	
	if(err != ""){
		document.getElementById("error").innerHTML = err;
	} else {
		$.post("checkout/login.php",{
		   email: e,
		   cus_password: p,
		   sessionID: sessionID,
		   referrer: referrer,
		   action: "new"
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				window.location = "checkout.php?sessionID="+sessionID;
			}else{
				document.getElementById("error").innerHTML = txt;
			}
		});
	}
	
	//return false;
}

function checkReturn(sessionID, referrer){
	var e = document.getElementById("returnEmail").value;
	var p = document.getElementById("returnPassword").value;
	var err = "";
	document.getElementById("error").innerHTML = "";
	
	if(!checkemail(e)){
		err += "Your email address must be valid<br>";
	}
	
	if(p == ""){
		err += "Please choose a password<br>";
	} else if (p.length < 6){
		err += "Your password was incorrect<br>";
	}
	
	if(err != ""){
		document.getElementById("error").innerHTML = err;
	} else {
		$.post("checkout/login.php",{
		   email: e,
		   cus_password: p,
		   sessionID: sessionID,
		   referrer: referrer,
		   action: "return"
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				window.location = "checkout.php?sessionID="+sessionID;
			}else{
				document.getElementById("error").innerHTML = txt;
			}
		});
	}
	
	//return false;
}

function checkemail(str){
	var testresults = false;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (filter.test(str)){
			testresults=true;
		}
	return (testresults);
}

function fgtPswd(){
	document.getElementById("window").style.display = "block";
}

function clsWindow(){
	document.getElementById("fgtError").innerHTML = "";
	document.getElementById("window").style.display = "none";
}

function clsMembership(){
	//document.getElementById("membership").style.display = "none";
	$('#membership').hide();
}

function fgtEmailPs(){
	var e = document.getElementById("fgtEmail").value;
	var err = "";
	document.getElementById("fgtError").innerHTML = "";
	
	if(!checkemail(e)){
		err += "Your email address must be valid<br>";
	}
	
	if(err != ""){
		document.getElementById("fgtError").innerHTML = err;
	} else {
		$.post("checkout/fgtEmail.php",{
		   email: e
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				document.getElementById("fgtError").innerHTML = "An email with your password has been sent to "+e;
			}else{
				document.getElementById("fgtError").innerHTML = txt;
			}
		});
	}
}

function getOrderDetails(sessionID,type,baseCost){
	document.getElementById("orderDetails").innerHTML = "Loading...";
	
	$.post("checkout/getDetails.php",{
		   sessionID: sessionID,
		   type: type,
		   baseCost: baseCost
		 }, function(html) {
				document.getElementById("orderDetails").innerHTML = html;
	});
}

function showCodes(){
	$('#codes').show();
	document.getElementById("whatchaneed").style.margin = "20px 0px 0px 0px";
}

function showShip(email, sessionID){
	removeMembership(sessionID);
	document.getElementById("error").innerHTML = "";
	document.getElementById("step2").innerHTML = "<div align='center'>Loading...</div>";
	$('#step1').hide();
	$('#step2').show();
		$.post("checkout/cusInfo.php",{
		   email: email,
		   sessionID: sessionID
		 }, function(html) {
				document.getElementById("step2").innerHTML = html;
		});
}

function removeCoupon(sessionID){
		$.post("checkout/removeCoupon.php",{
		   sessionID: sessionID
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				// do nothing for now
			}
		});
}

function insertCodes(sessionID,type,baseCost,email) {
	var c = document.getElementById("coupon_code").value;
	var g = document.getElementById("gift_code").value;
	document.getElementById("error").innerHTML = "";
	if(c == '' && g == ''){
		document.getElementById("error").innerHTML = "You have not entered a coupon code or a gift code";
	}else{
		//alert(type);
		$.post("checkout/addCoupon.php",{
		   sessionID: sessionID,
		   couponCode: c,
		   giftCode: g,
		   type: type
		 }, function(txt) {
			var chk = txt.search('success');
			//alert(txt);
			if(chk >= 0){
				document.getElementById("orderDetails").innerHTML = "";
				showShip(email,sessionID);
				getOrderDetails(sessionID,type,baseCost);
			}else{
				document.getElementById("error").innerHTML = txt;
			}
		});
	}
}


/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::: P H O N E   F O R W A R D I N G :::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
var isNN = ( navigator.appName.indexOf( "Netscape" ) != -1 ); 
 
function autoTab( input,len, e ) { 
	var keyCode	= ( isNN ) ? e.which : e.keyCode; 
	var filter	= ( isNN ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; 
	if( input.value.length >= len && !containsElement( filter, keyCode )) { 
	input.value = input.value.slice( 0, len ); 
	input.form[( getIndex( input ) + 1 ) % input.form.length].focus(); 
	} 
	return true; 
} 
 
function containsElement( arr, ele ) { 
	var found = false, index = 0; 
	while( !found && index < arr.length ) 
	if( arr[index] == ele ) { 
		found = true; 
	} else { 
		index++; 
	} 
	return found; 
} 
 
function getIndex( input ) { 
	var index = -1, i = 0, found = false; 
	while ( i < input.form.length && index == -1 ) 
	if ( input.form[i] == input ) { 
		index = i; 
	} else { 
		i++; 
	} 
	return index; 
}

function prePopBill(){
	var clicker = document.getElementById('sameAs');

	if(clicker.value == "Y"){
		document.getElementById("bill_first").value = document.getElementById("ship_first").value;
		document.getElementById("bill_MI").value = document.getElementById("ship_MI").value;
		document.getElementById("bill_last").value = document.getElementById("ship_last").value;
		document.getElementById("bill_address").value = document.getElementById("ship_address").value;
		document.getElementById("bill_address2").value = document.getElementById("ship_address2").value;
		document.getElementById("bill_city").value = document.getElementById("ship_city").value;
		document.getElementById("bill_zip").value = document.getElementById("ship_zip").value;
		document.getElementById("bill_state").value = document.getElementById("ship_state").value;
		document.getElementById("bill_country").value = document.getElementById("ship_country").value;
		document.getElementById("bill_province").value = document.getElementById("ship_province").value;
		if( document.getElementById("ship_country").value == "CANADA" ){
			$("#provinceFieldsB").show();
			$("#stateFieldsB").hide();
		}else if ( document.getElementById("ship_country").value == "USA") {
			$("#provinceFieldsB").hide();
			$("#stateFieldsB").show();
		}
	}else{
		document.getElementById("bill_first").value = "";
		document.getElementById("bill_MI").value = "";
		document.getElementById("bill_last").value = "";
		document.getElementById("bill_address").value = "";
		document.getElementById("bill_address2").value = "";
		document.getElementById("bill_city").value = "";
		document.getElementById("bill_state").value = "";
		document.getElementById("bill_country").value = "";
		document.getElementById("bill_zip").value = "";
		if( document.getElementById("ship_country").value == "CANADA" ){
			$("#provinceFieldsB").show();
			$("#stateFieldsB").hide();
		}else if ( document.getElementById("ship_country").value == "USA") {
			$("#provinceFieldsB").hide();
			$("#stateFieldsB").show();
		}
	}
}

function showMedia(){
	var shipType = document.getElementById('shipping_type').value;
	if(shipType == "media_mail"){
		fgtPswd();
		document.getElementById("windowHd").innerHTML = "USPS Media Mail";
		document.getElementById("fgtError").innerHTML = "USPS Media Mail is usually cheaper but takes longer; sometimes as long as 2-4 weeks.<br><br>We <b>DO NOT</b> accept returns for items damaged in transit via USPS Media Mail.";
	}else{
		clsWindow();
	}
}

function shippingCountry(){
	var country = document.getElementById('ship_country').value;
	if( country == "CANADA"){
		$("#provinceFields").show();
		$("#stateFields").hide();
		//$("#provinceFieldsB").show();
		//$("#stateFieldsB").hide();
	} else if( country == "USA" ){
		$("#provinceFields").hide();
		$("#stateFields").show();
		//$("#provinceFieldsB").hide();
		//$("#stateFieldsB").show();
	}
}

function billingCountry(){
	var country = document.getElementById('bill_country').value;
	if( country == "CANADA"){
		$("#provinceFieldsB").show();
		$("#stateFieldsB").hide();
		//$("#provinceFieldsB").show();
		//$("#stateFieldsB").hide();
	} else if( country == "USA" ){
		$("#provinceFieldsB").hide();
		$("#stateFieldsB").show();
		//$("#provinceFieldsB").hide();
		//$("#stateFieldsB").show();
	}
}

function calcShip(sessionID) {
	var sz = document.getElementById('ship_zip').value;
	var scoty = document.getElementById('ship_country').value;
	var err = "";
	clsWindow();
	
	if(sz == ""){
		err += "Please enter a zip code in your shipping info.<br>";
	}
	
	if(scoty == ""){
		err += "Please choose a country in your shipping info.<br>";
	}
	
	if(err != ""){
		fgtPswd();
		document.getElementById("windowHd").innerHTML = "Shipping System";
		document.getElementById("fgtError").innerHTML = err;
		//document.getElementById("error").innerHTML = err;
	}else{
		$.post("checkout/calcShip.php",{
		   sessionID: sessionID,
		   zip: sz,
		   country: scoty
		 }, function(html) {
			fgtPswd();
			document.getElementById("windowHd").innerHTML = "Shipping System";
			document.getElementById("fgtError").innerHTML = html;
		});
	}
}

function addMembership(sessionID,type,baseCost){
		$.post("checkout/addMembership.php",{
		   sessionID: sessionID
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				clsMembership();
				getOrderDetails(sessionID,type,baseCost);
			}else{
				clsMembership();
				document.getElementById("error").innerHTML = txt;
			}
		});	
}

function removeMembership(sessionID){
		$.post("checkout/removeMembership.php",{
		   sessionID: sessionID
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				clsMembership();
			}else{
				clsMembership();
				document.getElementById("error").innerHTML = txt;
			}
		});	
}

function showPayment(sessionID, type, baseCost){
	//declare variables
	var sf = document.getElementById('ship_first').value;
	var sl = document.getElementById('ship_last').value;
	var sm = document.getElementById('ship_MI').value;
	var sa = document.getElementById('ship_address').value;
	var sa2 = document.getElementById('ship_address2').value;
	var sc = document.getElementById('ship_city').value;
	var ss = document.getElementById('ship_state').value;
	var scoty = document.getElementById('ship_country').value;
	var sprov = document.getElementById('ship_province').value;
	var sz = document.getElementById('ship_zip').value;
	
	var bf = document.getElementById('bill_first').value;
	var bl = document.getElementById('bill_last').value;
	var bm = document.getElementById('bill_MI').value;
	var ba = document.getElementById('bill_address').value;
	var ba2 = document.getElementById('bill_address2').value;
	var bc = document.getElementById('bill_city').value;
	var bs = document.getElementById('bill_state').value;
	var bcoty = document.getElementById('bill_country').value;
	var bprov = document.getElementById('bill_province').value;
	var bz = document.getElementById('bill_zip').value;
	
	var e = document.getElementById('email').value;
	var p1 = document.getElementById('phone').value;
	var p2 = document.getElementById('phone2').value;
	var p3 = document.getElementById('phone3').value;
	var shipType = document.getElementById('shipping_type').value;
	
	document.getElementById("error").innerHTML = "";
	
	var err = "";
	
	if(sf == ""){
		err += "Please provide your shipping first name.<br>";
	}
	
	if(sl == ""){
		err += "Please provide your shipping last name.<br>";
	}
	
	if(sa == "" || sc == "" || sz == ""){
		err += "Please provide your full shipping address.<br>";
	}
	
	if(bf == ""){
		err += "Please provide your billing first name.<br>";
	}
	
	if(bl == ""){
		err += "Please provide your billing last name.<br>";
	}
	
	if(ba == "" || bc == "" || bz == ""){
		err += "Please provide your full billing address.<br>";
	}
	
	if(!checkemail(e)){
		err += "Your email address must be valid<br>";
	}
	
	if(p1 == "" || p2 == "" || p3 == ""){
		err += "Please provide your phone number.<br>";
	}
	
	if(shipType == ""){
		err += "Please select a shipping method<br>";
	}
	
	if(scoty != "USA" && shipType != "priority_mail"){
		err += "International orders must ship via USPS Priority Mail.<br>";
	}
	
	if(err == ""){
	
		if(scoty == "USA" && ss == ""){
			err += "Please choose a state for shipping.<br>";
		}
		
		if(bcoty == "USA" && bs == ""){
			err += "Please choose a state for billing.<br>";
		}
	
	}
		
	if(err != ""){
		document.getElementById("error").innerHTML = err;
	}else{
		clsWindow();
		var phone = p1+"-"+p2+"-"+p3;

		$.post("checkout/cusEnter.php",{
		   sessionID: sessionID,
		   ship_first: sf,
		   ship_last: sl,
		   ship_MI: sm,
		   ship_address: sa,
		   ship_address2: sa2,
		   ship_city: sc,
		   ship_state: ss,
		   ship_country: scoty,
		   ship_province: sprov,
		   ship_zipcode: sz,
		   bill_first: bf,
		   bill_last: bl,
		   bill_MI: bm,
		   bill_address: ba,
		   bill_address2: ba2,
		   bill_city: bc,
		   bill_state: bs,
		   bill_country: bcoty,
		   bill_province: bprov,
		   bill_zipcode: bz,
		   email: e,
		   phone: phone,
		   shipType: shipType
		 }, function(txt) {
		 	var chk = txt.search('success');
			if(chk >= 0){
				$('#step2').hide();
				$('#step3').show();
				$('#membership').show();
				getOrderDetails(sessionID,type,baseCost);
				
			}else{
				document.getElementById("error").innerHTML = txt;
			}
		});
		
		//display payment html
		$.post("checkout/paymentInfo.php",{
		   sessionID: sessionID
		 }, function(html) {
				document.getElementById("paymentInfo").innerHTML = html;
		});
	}
	
	//return false;

}

function switchCard(slap){
	var type = document.getElementById('payType').value;
	if(slap == 'on'){
		if(type == ""){
			alert('Please select a credit card type.');
		}
	}
	
		switch(type){
			case "001":
				document.getElementById('credit').style.display='none';
				document.getElementById('securityCodeText').style.display='inline';
				document.getElementById('visa').style.display='inline';
				document.getElementById('amex').style.display='none';
				break;    
			case "002":
				document.getElementById('credit').style.display='none';
				document.getElementById('securityCodeText').style.display='inline';
				document.getElementById('visa').style.display='inline';
				document.getElementById('amex').style.display='none';
				break;    
			case "003":
				document.getElementById('credit').style.display='none';
				document.getElementById('securityCodeText').style.display='inline';
				document.getElementById('visa').style.display='none';
				document.getElementById('amex').style.display='inline';
				break;    
			case "004":
				document.getElementById('credit').style.display='none';
				document.getElementById('visa').style.display='none';
				document.getElementById('amex').style.display='none';
				document.getElementById('invoice').style.display='inline';
				document.getElementById('paypal').style.display='none';
				break; 
			case "005":
				document.getElementById('credit').style.display='none';
				document.getElementById('visa').style.display='none';
				document.getElementById('amex').style.display='none';
				document.getElementById('invoice').style.display='none';
				document.getElementById('paypal').style.display='inline';
				break; 
			default:
				document.getElementById('visa').style.display='none';
				document.getElementById('amex').style.display='none';
				document.getElementById('securityCodeText').style.display='none';
				document.getElementById('credit').style.display='inline';
		}//end switch
}

function placeOrder(sessionID){
	// turn off submit button
	$('#placeOrderBtn').hide();

	//declare variables
	var pt = document.getElementById('payType').value;
	var cnumber = document.getElementById('cardNumber').value;
	var cname = document.getElementById('cardName').value;
	var cmonth = document.getElementById('card_month').value;
	var cyear = document.getElementById('card_year').value;
	var security = document.getElementById('securityCode').value;
	var err = '';
	
	document.getElementById("error").innerHTML = "";
	
	if(pt == ""){
		err += "Please select a payment method.<Br>";
	}
	
	if(pt != "000" && pt != "004" && pt != "005"){
	
		if(cnumber == ""){
			err += "Please provide your credit card number.<Br>";
		}
		
		if(cname == ""){
			err += "Please provide the name on your credit card.<Br>";
		}
		
		if(cmonth == "" || cyear == ""){
			err += "Please enter your expiration date.<Br>";
		}
		
		if(security == ""){
			err += "Please provide your security code.<Br>";
		}
	
		if(err == ""){
			if(pt == "003" && security.length < 4){
				err += "Please provide a valid security code<br>";
				alert(security.length);
			} else if (pt != "003" && security.length != '3'){
				err += "Please provide a valid security code<br>";
			}
		}
	} // end payment type if
	
	if(pt == "005"){
		err += "Paypal is not available at this time.<Br>";
	}
	
	if(err != ""){
		document.getElementById("error").innerHTML = err;
		$('#placeOrderBtn').show("slow");
	}else{
		document.getElementById("error").innerHTML = "Processing Order";
		$.post("checkout/submitOrder.php",{
		   sessionID: sessionID,
		   payType: pt,
		   cardNumber: cnumber,
		   cardName: cname,
		   cardMonth: cmonth,
		   cardYear: cyear,
		   securityCode: security
		 }, function(txt) {
			var chk = txt.search('success');
			if(chk >= 0){
				clsMembership();
				document.getElementById("error").innerHTML = '';
				document.getElementById("paymentInfo").innerHTML = "";
				$('#step2').hide();
				$('#step3').hide();
				$('#step4').show();
				$('#paymentInfo').hide();
				document.getElementById("step4").innerHTML = txt;
			}else{
				clsMembership();
				document.getElementById("error").innerHTML = txt;
				$('#placeOrderBtn').show("slow");
			}
		});	
	}
}
