//start autofill
var autoBillText = new Array('firstname','lastname','company','address1','address2','city','zip','phone');
var autoShipText = new Array('shipfirstname','shiplastname','shipcompany','shipaddress1','shipaddress2','shipcity','shipzip','shipphone');
//var afpref="bothways";
var afpref="billtoship";
function fillOut(whi) {
	var af=getElem('shipto');
	if (whi!=1) if (afpref!="bothways") af.checked=false;
	if (af.checked==true) {
		for (var x=0;x<autoBillText.length;x++) {
			var bf=getElem(autoBillText[x]);
			var sf=getElem(autoShipText[x]);
			if (whi==1) sf.value = bf.value;
			else bf.value = sf.value;
		}
		ckStateCountry(whi,'s');
		ckStateCountry(whi,'c');
	}
}
//end autofill

var Rollimage = new Array();
Rollimage[0] = new Image(122,18) ;
Rollimage[0].src = "images/wait.gif";


//start state/country
function ckStateCountry(whi,wha) {
	var af = getElem('shipto');
	var statedrop= new Array(getElem('bill_us_state'),getElem('ship_us_state'));
	var statehide= new Array(getElem('bill_state'),getElem('ship_state'));
	var statetext= new Array(getElem('state'),getElem('shipstate'));
	var countrydrop= new Array(getElem('country'),getElem('shipcountry'));
	var b=0; var s=1;
	if (whi!=1) { //if shipping
		var b=1; var s=0;
		if (afpref!="bothways") af.checked=false;
	}
	if (countrydrop[b].selectedIndex==1||countrydrop[b].selectedIndex==224) { //if country is us
		countrydrop[b].selectedIndex=1;
		if (statedrop[b].selectedIndex==1) { //if state is not us
			if (wha=='c') { //if changing country
				statedrop[b].selectedIndex=0;
				statetext[b].value='';
				statehide[b].style.display = 'none';
				if (af.checked==true) statehide[s].style.display = 'none';
			}
			else { //if changing state
				countrydrop[b].selectedIndex=0;
				statehide[b].style.display = 'block';
				if (af.checked==true) statehide[s].style.display = 'block';
			}
		}
	}
	else if (countrydrop[b].selectedIndex==0) { //if country is not selected
		if (wha=='s') { //if changing state
			if (statedrop[b].selectedIndex!=1) { //if state is us (or none)
				if (statedrop[b].selectedIndex==0) countrydrop[b].selectedIndex=0;
				else countrydrop[b].selectedIndex=1;
				statetext[b].value='';
				statehide[b].style.display = 'none';
				if (af.checked==true) statehide[s].style.display = 'none';
			}
			else { //if state is not us
				statetext[b].value='';
				statehide[b].style.display = 'block';
				if (af.checked==true) statehide[s].style.display = 'block';
			}
		}
		else { //if changing country
			if (statedrop[b].selectedIndex!=1) { //if state is us (or none)
				if (statedrop[b].selectedIndex==0) countrydrop[b].selectedIndex=0;
				else countrydrop[b].selectedIndex=1;
			}
		}
	}
	else { //if country is not us
		if (wha=='c') { //if changing country
			statetext[b].value='';
			statedrop[b].selectedIndex=1;
			statehide[b].style.display = 'block';
			if (af.checked==true) statehide[s].style.display = 'block';
		}
		else { //if changing state
			if (statedrop[b].selectedIndex!=1) { //if state is us (or none)
				if (statedrop[b].selectedIndex==0) countrydrop[b].selectedIndex=0;
				else countrydrop[b].selectedIndex=1;
				statetext[b].value=statedrop[b].options[statedrop[b].selectedIndex].value;
				statehide[b].style.display = 'none';
				if (af.checked==true) statehide[s].style.display = 'none';
			}
		}
	}
	if (af.checked==true) {
		statedrop[s].selectedIndex=statedrop[b].selectedIndex;
		statetext[s].value=statetext[b].value;
		countrydrop[s].selectedIndex=countrydrop[b].selectedIndex;
	}
}
//end state/country


//start login validation
function ckLogin(){
	var curField = getElem("login_password");
	if (curField.value == "") {
		alert("Please enter your login information.");
		return false;
	}
	else {
		if (emailCheck(getElem("login_email").value)) {
			document.logbut.src=Rollimage[0].src;
			document.loginform.submit();
		}
		else return false;
	}
}
//end login validation


//start customer validation
var baseReqText = new Array('firstname','lastname','address1','city','phone','email','shipfirstname','shiplastname','shipaddress1','shipcity','shipphone');
var baseReqSelect = new Array('bill_us_state','country','ship_us_state','shipcountry');
function ckCustomer(){
	//reset fields
	var em = getElem("errormessage");
	em.innerHTML = "&nbsp;";
	var em2 = getElem("errormessage2");
	em2.innerHTML = "&nbsp;";
	for (var i=0;i<baseReqText.length;i++) { 
		var curField = getElem(baseReqText[i]);
		curField.style.background="#ffffff";
	}
	for (var i=0;i<baseReqSelect.length;i++) { //reset selects
		var curSel = getElem(baseReqSelect[i]);
		curSel.style.background="#ffffff";
	}
	var oktogo=0;
	
	// check fields
	for (var i=0;i<baseReqText.length;i++) {
		var curField = getElem(baseReqText[i]);
		if (curField.value == "") {
			curField.style.background="#FBF17A";
			oktogo+=1;
		}
	}
	for (var i=0;i<baseReqSelect.length;i++) {
		var curSel = getElem(baseReqSelect[i]);
		if (curSel.selectedIndex == 0) {
			curSel.style.background="#FBF17A";
			oktogo+=1;
		}
	}
	var billzip=getElem("zip");
	var billcountry=getElem("country");
	if ((billcountry.selectedIndex==1||billcountry.selectedIndex==224)&&billzip.value=="") {
		billzip.style.background="#FBF17A";
		oktogo+=1;
	}
	else {
		billzip.style.background="#ffffff";
	}
	var shipzip=getElem("shipzip");
	var shipcountry=getElem("shipcountry");
	if ((shipcountry.selectedIndex==1||shipcountry.selectedIndex==224)&&shipzip.value=="") {
		shipzip.style.background="#FBF17A";
		oktogo+=1;
	}
	else {
		shipzip.style.background="#ffffff";
	}
	if (oktogo>0) {
		em.innerHTML = 'Please complete the required fields.';
		em2.innerHTML = 'Please complete the required fields.';
		return false;
	}
	else {
		if (emailCheck(getElem("email").value)) {
			document.conbut.src=Rollimage[0].src;
			var usstate=getElem("bill_us_state");
			var usshipstate=getElem("ship_us_state");
			var state=getElem("state");
			var shipstate=getElem("shipstate");
			if (usstate.selectedIndex!=0||usstate.selectedIndex!=1) state.value=usstate.options[usstate.selectedIndex].value;
			if (usshipstate.selectedIndex!=0||usshipstate.selectedIndex!=1) shipstate.value=usshipstate.options[usshipstate.selectedIndex].value;
			document.customerform.submit();
		}
		else return false;
	}
}
//end customer validation


//start credit validation
var ccReqText = new Array('cardnumber','avs');
var ccReqSelect = new Array('cardtype','ccmonth','ccyear');
function ckCredit(){
	//reset fields
	var em = getElem("errormessage");
	em.innerHTML = "&nbsp;";
	for (var i=0;i<ccReqText.length;i++) { 
		var curField = getElem(ccReqText[i]);
		curField.style.background="#ffffff";
	}
	for (var i=0;i<ccReqSelect.length;i++) { //reset selects
		var curSel = getElem(ccReqSelect[i]);
		curSel.style.background="#ffffff";
	}
	var oktogo=0;
	
	// check fields
	for (var i=0;i<ccReqText.length;i++) {
		var curField = getElem(ccReqText[i]);
		if (curField.value == "") {
			curField.style.background="#FBF17A";
			oktogo+=1;
		}
	}
	for (var i=0;i<ccReqSelect.length;i++) {
		var curSel = getElem(ccReqSelect[i]);
		if (curSel.selectedIndex == 0) {
			curSel.style.background="#FBF17A";
			oktogo+=1;
		}
	}
	// validate exp. date
	var month = getElem("ccmonth");
	var year = getElem("ccyear");
	var mm = month.options[month.selectedIndex].value;
	var yyyy = (year.options[year.selectedIndex].value*1)+2000;
	if (today_yyyy > yyyy) {
		year.style.background="#FBF17A";
		oktogo+=1;
	}
	var mmM = 1*mm;
	if (today_yyyy == yyyy && today_m > mmM) {
		month.style.background="#FBF17A";
		oktogo+=1;
	}
	// validate card type and number
	if (oktogo==0) {
		var cardno = getElem("cardnumber");
		var carnoval = cardno.value;
		cardno.value = carnoval.replace(/ /g, "");
/* remove after testing */
		if (cardno.value != "4242424242424242") {
			var cardtype = getElem("cardtype");
			var cardtype2 = cardtype.options[cardtype.selectedIndex].value;
			var retval = eval(cardtype2 + ".checkCardNumber(\"" + cardno.value + "\", " + yyyy + ", " + mmM + ");");
			if (retval) {}
			else {
				cardno.style.background="#FBF17A";
				cardtype.style.background="#FBF17A";
				oktogo+=1;
			}
		}
	}
	if (oktogo>0) {
		em.innerHTML = 'Please check your credit card information.';
		//location.hash="messagetop";
		return false;
	}
	else {
		document.subbut.src=Rollimage[0].src;
		document.checkoutfinal.submit();
	}
}
//end credit validation
