function getElem(x) {
	var elem;
	if(document.getElementById) elem = document.getElementById(x);
	else if(document.all) elem = document.all[x];
	else if(document.layers) elem = document.layers[x];
	return elem;
}
var shipOn=0;
function ckMail() {
	var el=getElem('mailing_list');
	var di=getElem('shipping_info');
	var ed=getElem('emailpref');
	var ep=getElem('email_preference');
	if (el.selectedIndex==0) {
		di.style.display = 'none';
		ed.style.display = 'block';
		shipOn=0;
	}
	else if (el.selectedIndex==1) {
		di.style.display = 'block';
		ed.style.display = 'none';
		shipOn=1;
	}
	else if (el.selectedIndex==2) {
		di.style.display = 'block';
		ed.style.display = 'block';
		shipOn=1;
	}
	else {
		di.style.display = 'none';
		ed.style.display = 'none';
		shipOn=0;
	}
}
function ckState(whi) {
	var sd=getElem('us_'+whi);
	var ds=getElem(whi+'_st');
	var rs=getElem(whi+'_state');
	rs.value=sd.options[sd.selectedIndex].value;
	ds.style.display = 'none';
}
function ckStateNOTUSED(whi) {
	var sd=getElem('us_'+whi);
	var ds=getElem(whi+'_st');
	var rs=getElem(whi+'_state');
	if (sd.selectedIndex==2) {
		rs.value='';
		ds.style.display = 'block';
	}
	else {
		rs.value=sd.options[sd.selectedIndex].value;
		ds.style.display = 'none';
	}
}
function ckInterests(y) {
	var fn=document.forms['customer_action'];
	var allst=fn.interests[0].checked;
	
	if (y==1) {
		for (var i=0; i<fn.interests.length; i++) {
			fn.interests[i].checked=true;
		}
	}
	else fn.interests[0].checked=false;
}


function emailCheck (emailStr) {
	if (emailStr==null||emailStr=="") {
		alert("Please enter an e-mail address.");
		return false;
	}
	emailStr = emailStr.toLowerCase();
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;\\'+:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Your e-mail address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths e-mail username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The e-mail username doesn't seem to be valid.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Please check the last part of your e-mail address.\n\nThe address must end in a well known domain or two letter country.");
		return false;
	}
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	return true;
}


var allReqText = new Array('shipping_first_name','shipping_last_name','email','confirm_email','shipping_postal_code','pword','password_answer');
//allReqCust:email-confirm_email
var shipReqText = new Array('shipping_address1','shipping_city');
//,'shipping_phone'
var billReqText = new Array('billing_first_name','billing_last_name','billing_address1','billing_city','billing_postal_code','billing_phone');
function ckForm() {
	var allGo=0;var billGo=0;var shipGo=0;

//ck general
	for (var x=0;x<allReqText.length;x++) {
		var telem=getElem(allReqText[x]);
		if (telem.value=='') {
			//telem.style.borderColor="orange";
			allGo=1;
		}
		//else telem.style.borderColor="grey";
	}
	var telem=getElem('pword');
	var telem2=telem.value;
	if (telem2.length<8) {
		//telem.style.borderColor="orange";
		allGo=1;
	}
	else telem.style.borderColor="grey";
	var telem=getElem('email');
	var telem2=getElem('confirm_email');
	if (telem.value!=telem2.value) {
		//telem.style.borderColor="orange";
		//telem2.style.borderColor="orange";
		allGo=1;
	}

if (shipOn==1) {
//ck shipping
	for (var y=0;y<shipReqText.length;y++) {
		var telem=getElem(shipReqText[y]);
		if (telem.value=='') {
			//telem.style.borderColor="orange";
			shipGo=1;
		}
		//else telem.style.borderColor="grey";
	}
	var telem=getElem('us_shipping');
	var telem2=getElem('shipping_state');
	//IF GO BACK TO NON US CHANGE 2 TO 3
	if (telem.selectedIndex==0||telem.selectedIndex==2) {
		//telem.style.backgroundColor="orange";
		shipGo=1;
	}
	//09/09 EDIT NOW DEFAULT TO NY
	//else telem.style.backgroundColor="white";
	//if (telem.selectedIndex==2&&telem2.value=='') {
		//telem2.style.borderColor="orange";
		//shipGo=1;
	//}
	//else telem2.style.borderColor="white";
}
if (billOn==1) {
//ck billing
	for (var z=0;z<billReqText.length;z++) {
		var telem=getElem(billReqText[z]);
		if (telem.value=='') {
			//telem.style.borderColor="orange";
			billGo=1;
		}
		//else telem.style.borderColor="grey";
	}
	var telem=getElem('us_billing');
	var telem2=getElem('billing_state');
	//IF GO BACK TO NON US CHANGE 2 TO 3
	if (telem.selectedIndex==0||telem.selectedIndex==2) {
		//telem.style.backgroundColor="orange";
		billGo=1;
	}
	//09/09 EDIT
	//else telem.style.background.Color="white";
	//if (telem.selectedIndex==2&&telem2.value=='') {
		//telem2.style.borderColor="orange";
	//	billGo=1;
	//}
	//else telem2.style.borderColor="grey";
}
//ck tally
	if (allGo==0&&shipGo==0&&billGo==0) {
		if (emailCheck(document.customer_action.email.value)) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		alert('Please be sure to fill out all required fields');
		return false;
	}
		
		//allGo=1;document.forms['customer_action'].submit();
	//else alert('allGo'+allGo+', shipGo'+shipGo+', billGo'+billGo);
	
}
