function ResetShipTo(){
	try{
		var shipcountry = document.getElementById("ShipTo_CountryCode").value;
		if (shipcountry != 'United States' && shipcountry != 'Canada') {
			document.getElementById("ShipToStateProvField").style.display = '';
			document.getElementById("ShipToStateProvSelect").style.display = 'none';
			document.getElementById("ShipToStateProvSelect").disabled = true;
			//var shiptostatefield = new Spry.Widget.ValidationTextField("ShipToStateProvField_span", "custom", {minChars:1, maxChars:50, validateOn:["blur","change"], regExpFilter:/^[a-z0-9 \-\.\@\,\\\(\)]*$/i , useCharacterMasking:true});
				try{
					document.getElementById("ShipTo_CountryCode_Field").value = shipcountry;
				}catch(e){}
			
			//if (shiptostateselect){
			//	shiptostateselect.reset();
			//	shiptostateselect.destroy();
			//}
		}else if (shipcountry == 'United States' || shipcountry == 'Canada'){
			document.getElementById("ShipToStateProvField").style.display = 'none';
			document.getElementById("ShipToStateProvSelect").style.display = 'block';
			//var shiptostateselect = new Spry.Widget.ValidationSelect("ShipToStateProvSelect_span",{isRequired:true, validateOn:["blur","change"], invalidValue:"0"});
				try{
					document.getElementById("ShipTo_CountryCode_Select").value = shipcountry;
				}catch(e){}
			//if (shiptostatefield){
			//	shiptostatefield.reset();
			//	shiptostatefield.destroy();
			//}
		}
	}catch(e){
		alert(e);
	}
}
function ResetBillTo(){
	try{
		var billcountry = document.getElementById("BillTo_CountryCode").value;
		
		if (billcountry != 'United States' && billcountry != 'Canada') {
			document.getElementById("BillToStateProvField").style.display = '';
			document.getElementById("BillToStateProvSelect").style.display = 'none';
			document.getElementById("BillToStateProvSelect").disabled = true;
			//var billtostatefield = new Spry.Widget.ValidationTextField("BillToStateProvField_span", "custom", {minChars:1, maxChars:50, validateOn:["blur","change"], regExpFilter:/^[a-z0-9 \-\.\@\,\\\(\)]*$/i , useCharacterMasking:true});
				try{
					document.getElementById("BillTo_CountryCode_Field").value = billccountry;
				}catch(e){}
			//if (billtostateselect){
			//	billtostateselect.reset();
			//	billtostateselect.destroy();
			//}
		}else if (billcountry == 'United States' || billcountry == 'Canada'){
			document.getElementById("BillToStateProvField").style.display = 'none';
			document.getElementById("BillToStateProvField").disabled = true;
			document.getElementById("BillToStateProvSelect").style.display = 'block';
			//var billtostateselect = new Spry.Widget.ValidationSelect("BillToStateProvSelect_span",{isRequired:true, validateOn:["blur","change"], invalidValue:"0"});
				try{
					document.getElementById("BillTo_CountryCode_Select").value = billcountry;
				}catch(e){}
			//if (billtostatefield){
			//	billtostatefield.reset();
			//	billtostatefield.destroy();
			//}
		}
	}catch(e){
		alert(e);
	}
}
function setHiddenfield(fieldid,value){
	try{
		document.getElementById(fieldid).value = value;
	}catch(e){
		alert(e);
	}
}
function SaveAccount(){
	if (document.Main.SaveAccountInfo.checked == true){
		document.getElementById("BillTo_Password").disabled = false;
		document.getElementById("BillTo_Password_Confirm").disabled = false;
		//var BillTo_Password = new Spry.Widget.ValidationTextField("BillToPassword_span", "none", {isRequired: true, minChars:4, maxChars:15, validateOn:["blur"]});
		//var BillTo_Password_Confirm = new Spry.Widget.ValidationConfirm("BillToPasswordConfirm_span", "BillToPassword_span", {isRequired: true, validateOn: ['blur']});
	}else{
		if (BillTo_Password){
			//BillTo_Password_Confirm.reset();
			//BillTo_Password_Confirm.destroy();
			//BillTo_Password.reset();
			//BillTo_Password.destroy();
		}
		document.getElementById("BillTo_Password").disabled = true;
		document.getElementById("BillToPassword_span").className = ''
		document.getElementById("BillTo_Password_Confirm").disabled = true;
		document.getElementById("BillToPasswordConfirm_span").className = ''
	}
}
var errorsOnSubmit = function(form){
	//var ret = Spry.Widget.Form.validate(form);
	//if (!ret){
	//	setMessage("error","There are errors in your information, please check the items marked in red.");
	//}else{
		//closeMessage('message_wrapper');
		//alert('submit');
		document.Main.submit();
	//}
	//alert(ret);
	return true;
}

var paymenterrorsOnSubmit = function(form){
	//var cc = isValidExpdate();
	//var ret = Spry.Widget.Form.validate(form);
	//alert(ret + cc);
	//if (ret || cc){
	//	closeMessage('message_wrapper');
	//}else{
		//alert(ret + cc);
	//	if (getCheckedValue(document.forms['Main'].elements['PaymentMethod']) == 'PayPal'){
	//		setMessage('info','Conatcting the PayPal server, please wait....');
	//	}
		document.Main.submit();
	//}
	return true;
}



function submit_form(){
	if (!errorsOnSubmit(document.Main)) {
		
	}
}

function toggleCCfields(condition){
	try{
		if (condition == true){
			document.getElementById("Payment_Type").disabled = true;
			document.getElementById("Payment_Number").disabled = true;
			document.getElementById("Payment_Ver").disabled = true;
			document.getElementById("Payment_ExpDate_Month").disabled = true;
			document.getElementById("Payment_ExpDate_Year").disabled = true;
			document.getElementById("Payment_Name").disabled = true;
			//destroy the validation object
			//if (PaymentType){PaymentType.reset();PaymentType.destroy();}
			//if (PaymentVer){PaymentVer.reset();PaymentVer.destroy();}
			//if (PaymentExp){PaymentExp.reset();PaymentExp.destroy();}
			//if (paymentname){paymentname.reset();paymentname.destroy();}
			//if (PaymentNumber){PaymentNumber.reset();PaymentNumber.destroy();}
		
		}else{
			setPayment();
			document.getElementById("Payment_Type").disabled = false;
			document.getElementById("Payment_Ver").disabled = false;
			document.getElementById("Payment_ExpDate_Month").disabled = false;
			document.getElementById("Payment_ExpDate_Year").disabled = false;
			document.getElementById("Payment_Name").disabled = false;
		}
	}catch(e){
		alert(e);
	}
}

function setPayment(){
	try{
		//alert('setPayment');
		var value = document.getElementById("Payment_Type").value;
		if (getCheckedValue(document.forms['Main'].elements['PaymentMethod']) == 'CreditCard'){
			if (value != 0){
			//alert(value);
				document.getElementById("Payment_Number").disabled = false;
			}
		}else{
			document.getElementById("Payment_Type").value = '0';
			toggleCCfields(true);
		}
	}catch(e){
		alert(e);
	}
}

function setPaymentValidation(){
	//var PaymentType = new Spry.Widget.ValidationSelect("PaymentType_span",{isRequired:true, validateOn:["blur","change"], invalidValue:"0"});
	//var PaymentVer = new Spry.Widget.ValidationTextField("PaymentVer_span", "none", {isRequired: true, validateOn:["blur"]});
	//var PaymentExp = new Spry.Widget.ValidationSelect("PaymentDate_span",{isRequired:true, validateOn:["blur","change"]}); 
	//var paymentname = new Spry.Widget.ValidationTextField("PaymentName_span", "custom", {minChars:1, maxChars:50, validateOn:["blur","change"], regExpFilter:/^[a-z0-9 \-\.\@\,\\\(\)]*$/i , useCharacterMasking:true});
}

function isValidExpdate(){
if (!document.getElementById("Payment_ExpDate_Month").disabled){
	var d = new Date();
	var thisyear = d.getFullYear();
	var selmonth = document.getElementById("Payment_ExpDate_Month").value;
	var selyear = document.getElementById("Payment_ExpDate_Year").value;
		if (selmonth != 0 && selyear != 0){
			var iyear = parseInt(selyear);
			var imonth = parseInt(selmonth,10);
				if(iyear == thisyear) {
					if(imonth<(d.getMonth()+1)) {
						Spry.Utils.addClassName("Payment_ExpDate_Month","selectRequiredState");
						Spry.Utils.addClassName("Payment_ExpDate_Year","selectRequiredState");
						Spry.$$("span#PaymentDate_span>span").setAttribute("class", "selectRequiredState");
						return false;
					}
				}
				if(iyear < thisyear) {
					Spry.Utils.addClassName("Payment_ExpDate_Month","selectRequiredState");
					Spry.Utils.addClassName("Payment_ExpDate_Year","selectRequiredState");
					Spry.$$("span#PaymentDate_span>span").setAttribute("class", "selectRequiredState");
					return false;
				}else{
					Spry.Utils.removeClassName("Payment_ExpDate_Month","selectRequiredState");
					Spry.Utils.addClassName("Payment_ExpDate_Month","selectValidState");
					Spry.Utils.removeClassName("Payment_ExpDate_Year","selectRequiredState");
					Spry.Utils.addClassName("Payment_ExpDate_Year","selectValidState");
					Spry.$$("span#PaymentDate_span>span").setAttribute("class", "selectRequiredMsg");
					return true;
				}
		}else{
			return false;
		}
	}	
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


