// JavaScript Document

//order.asp
function checkform(){
	if(document.form.qtyxprod.value < 1){
		alert("Please enter a value greater than 0.");
		document.form.qtyxprod.value = 1;
		document.form.qtyxprod.focus();
		return false;
	}
	if(document.form.attrcode.value ==""){
		alert("Please select a size from the dropdown list.");
		document.form.attrcode.focus();
		return false;
	}
}



//cart
function onSubmit(buttonPressed) {
    if (buttonPressed == 'promo') {
        document.form.action = "cart_promo.asp";
        //document.form.submit();
    }else{
		if(buttonPressed == 'summary'){
			nx = "SUM";	
		}else{
			nx = "SIP";
		}
        document.form.action = "cart_hearus.asp?nx="+nx;
        document.form.submit();
    }
}

//payment method selection
function onSubmit_pay(pmd) {
	ok = chk_form();

	if(ok == false){
		return false;
	}else{
		for(i =0; i < 2; i++){
			if(document.form.paymethod[i].checked == true){
				pm = i;
			}
		}
		if (pm == 0) { //credit card
			document.form.action = "cart_shipping2.asp?pm=0";
			document.form.submit();
		}else if (pm == 1) {	//paypal
			document.form.action = "cart_shipping2.asp?pm=1";
			document.form.submit();
		}else{	//billing, shipping form edit only
			document.form.action = "cart_shipping2.asp?pm=2";
			document.form.submit();
		}
	}
	
}

//cart_shipping.asp
var ShipFirst = "";
var ShipLast = "";
var ShipEmail = "";
var ShipAddress = "";
var ShipCity = "";
var ShipState = "";
var ShipStateIndex = 0;
var ShipZip = "";
var ShipPhone = "";

function InitSaveVariables(form) {

	ShipFirst = form.x_ship_to_first_name.value;
	ShipLast = form.x_ship_to_last_name.value;	
	ShipCountryIndex = form.x_ship_to_country.selectedIndex;
	ShipCountry = form.x_ship_to_country.value;
	ShipAddress = form.x_ship_to_address.value;
	ShipCity = form.x_ship_to_city.value;
	ShipZip = form.x_ship_to_zip.value;
	//ShipStateIndex = form.x_ship_to_state.selectedIndex;
	//ShipState = form.x_ship_to_state[ShipStateIndex].value;
	ShipPhone = form.x_ship_to_phone.value;
}

function copyBill(form) {
	if (form.sameasbill.checked) {
		InitSaveVariables(form);
		form.x_ship_to_first_name.value = form.x_first_name.value;
		form.x_ship_to_last_name.value = form.x_last_name.value;
		form.x_ship_to_country.selectedIndex = form.x_country.selectedIndex;
		form.x_ship_to_country.value = form.x_country.value;
		form.x_ship_to_address.value = form.x_address.value;
		form.x_ship_to_city.value = form.x_city.value;
		//form.x_ship_to_state.selectedIndex = form.x_state.selectedIndex;
		//form.x_ship_to_state.value = form.x_state.value;
		form.x_ship_to_zip.value = form.x_zip.value;
		form.x_ship_to_phone.value = form.x_phone.value;
		
		if(form.x_ship_to_country.value == "US"){
			//form.x_ship_to_state.selectedIndex =  form.x_state.selectedIndex;
			form.x_ship_to_state.value = form.x_state.value;
		}else{
			form.x_ship_to_state_a.value = form.x_state_a.value;
		}
		
		st_pr2(form);
	}else{
		form.x_ship_to_first_name.value = "";
		form.x_ship_to_last_name.value = "";
		form.x_ship_to_country.selectedIndex = 0;
		form.x_ship_to_country.value = "";
		form.x_ship_to_address.value = "";
		form.x_ship_to_city.value = "";
		form.x_ship_to_state.selectedIndex = 0;
		form.x_ship_to_state.value = "";
		form.x_ship_to_zip.value = "";
		form.x_ship_to_phone.value = "";
		document.form.codesmtd[0].disabled = false;
		document.form.codesmtd[1].disabled = false;
		document.form.codesmtd[0].checked = false;
		document.form.codesmtd[1].checked = false;
		st_pr2(form);
   }
}

//cart_shipping.asp
function chk_form(){
	
	//billing
	if(document.form.x_first_name.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_first_name.focus();
      	return false;
	}
	if(document.form.x_last_name.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_last_name.focus();
      	return false;
	}
	if(document.form.x_address.value== ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_address.focus();
      	return false;
	}
	if(document.form.x_city.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_city.focus();
      	return false;
	}	
	/*
	if(document.form.x_state.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_state.focus();
		return false;
	}*/	
	/* 8/25/09 removed from required field, because Vietnum customer do not have zip code.
	/*if(document.form.x_zip.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_zip.focus();
      	return false;
	}*/
	if(document.form.x_phone.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_phone.focus();
      	return false;
	}
	if(document.form.x_email.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_email.focus();
      	return false;
	}else{
		var theEmail = document.form.x_email.value;
		var atLoc = theEmail.indexOf("@", 1);
		var dotLoc = theEmail.indexOf(".", atLoc+2);
		var len = theEmail.length;
		if(atLoc > 0 && dotLoc > 0 && len > dotLoc+2){
			
		}
		else{
			alert("Please enter your e-mail address properly.");
			document.form.x_email.focus();
			return false;
		}	
	}
  	
	//shipping
	if(document.form.codesmtd.value == ""){
		alert("Please select the shipping opions.");
		document.form.codesmtd.focus();
      	return false;
	}
	if(document.form.x_ship_to_first_name.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_first_name.focus();
      	return false;
	}
	if(document.form.x_ship_to_last_name.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_last_name.focus();
      	return false;
	}
	if(document.form.x_ship_to_country.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_country.focus();
		return false;
	}
	if(document.form.x_ship_to_address.value== ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_address.focus();
      	return false;
	}
	if(document.form.x_ship_to_city.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_city.focus();
      	return false;
	}
	
	if(document.form.x_ship_to_country.value == "US"){
		if(document.form.x_ship_to_state.value == ""){
			alert("Please fill out the required fields and try again.");
			document.form.x_ship_to_state.focus();
			return false;
		}
	}else{
		/*if(document.form.x_ship_to_state_a.value == ""){
			alert("Please fill out the required fields and try again.");
			document.form.x_ship_to_state_a.focus();
			return false;
		}*/
	}
	/*
	if(document.form.x_ship_to_zip.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_zip.focus();
      	return false;
	}
	*/
	if(document.form.x_ship_to_phone.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_ship_to_phone.focus();
      	return false;
	}
	
	pm = false;
	for(i =0; i < 2; i++){
		if(document.form.paymethod[i].checked == true){
			pm = true;
		}
	}
	if(pm == false){
		alert("Please select the payment method.");
		document.form.paymethod[0].focus();
		return false;	
	}
	
}

function ck_payform(){
	if(document.form.card_co.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.card_co.focus();
      	return false;
	}
	if(document.form.x_card_num.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_card_num.focus();
      	return false;
	}else if(document.form.x_card_num.value.length < 15){
		alert("Credidt card number you entered is incorrect.");
		document.form.x_card_num.focus();
      	return false;
	}
	if(document.form.x_cardholder_name.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.x_cardholder_name.focus();
      	return false;
	}
	if(document.form.exp_mo.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.exp_mo.focus();
      	return false;
	}
	if(document.form.exp_yr.value == ""){
		alert("Please fill out the required fields and try again.");
		document.form.exp_yr.focus();
      	return false;
	}
	if(document.form.x_card_code.value == ""){
		alert("Please fill out the credit card code.");
		document.form.x_card_code.focus();
		return false;
	}	
}

function submitonce(theform){
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++){
			var tempobj=theform.elements[i]
			if(tempobj.type.toLowerCase()=="submit")
				//disable em
				tempobj.disabled=true
		}
	}
}

function st_pr2(me){
	if(me.x_ship_to_country.value == "US"){
		document.getElementById("state2").style.visibility = "visible";
		document.getElementById("province2").style.visibility = "hidden";
		showSP("US");
	}else{
		document.getElementById("state2").style.visibility = "hidden";
		document.getElementById("province2").style.visibility = "visible";
		showSP(document.form.x_ship_to_country.value);
	}
}

function st_pr1(me){
	if(me.x_country.value == "US"){
		document.getElementById("state2-b").style.visibility = "visible";
		document.getElementById("province2-b").style.visibility = "hidden";
		showSP("US");
	}else{
		document.getElementById("state2-b").style.visibility = "hidden";
		document.getElementById("province2-b").style.visibility = "visible";
		showSP(document.form.x_country.value);
	}
}

function showSP(ctry){
	//document.form.sp[0].disabled = true;
	if(ctry == "US"){
		
		ShipStateIndex = document.form.x_ship_to_state.selectedIndex;
		ShipState = document.form.x_ship_to_state[ShipStateIndex].value;
		
		if(ShipState == ""){
			document.form.codesmtd[0].disabled = false;
			document.form.codesmtd[0].checked = true;
		}else{
			document.form.codesmtd[0].disabled = false;
			document.form.codesmtd[0].checked = true;
		}

		document.form.codesmtd[1].disabled = true;
		document.form.codesmtd[1].checked  = false; //deselect radio button
	}else{
		//canada only
		document.form.codesmtd[0].disabled = true;
		document.form.codesmtd[1].disabled = false;
		//deselect radio button
		document.form.codesmtd[0].checked = false;  
		document.form.codesmtd[1].checked = true;  //select radio button
	}
}


