// JavaScript Document
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function chkEmail(objName){
// Check for @ and .
	var checkStr = objName;
	var hasAt = false;
	var hasDot = false;
	for (i = 0;  i < checkStr.value.length;  i++){
		ch = checkStr.value.charAt(i);
		if (ch == "@")
			hasAt = true;
		if (ch == ".")
			hasDot = true;
	}
	if (!hasAt){	
		return false;
	}
	if (!hasDot){	
		return false;
	}
}
function validateNewletter() {
	e = true;
	alert_str = 'Your Newsletter Signup had the following errors:';
	if(document.newsletter.name.value == '') {
		alert_str += '\nMISSING Name.';
		e = false;
	}
	if(document.newsletter.email.value == '') {
		alert_str += '\nMISSING E-Mail Address.';
		e = false;
	}else{
		if(chkEmail(document.newsletter.email) == false) {
			alert_str += '\nINVALID E-Mail Address.';
			e = false;
		}
	}
	if(e)
		return true;
	else {
		alert(alert_str);
		return false;
	}
}	
function validateRequestInfo() {
	e = true;
	alert_str = 'Your information request had the following errors:';
	if(document.requestForm.name.value == '') {
		alert_str += '\nMISSING Company Name.';
		e = false;
	}
	if(document.requestForm.address.value == '') {
		alert_str += '\nMISSING Address.';
		e = false;
	}
	if(document.requestForm.city.value == '') {
		alert_str += '\nMISSING City.';
		e = false;
	}
	if(document.requestForm.state.value == '') {
		alert_str += '\nMISSING State/Province.';
		e = false;
	}
	if(document.requestForm.country.value == '') {
		alert_str += '\nMISSING Country.';
		e = false;
	}				
	if(document.requestForm.home_phone.value == '') {
		alert_str += '\nMISSING Phone Number.';
		e = false;
	}
	else {
		if(document.requestForm.home_phone.value.length < 7) {
			alert_str += '\nINVALID Phone Number.';
			e = false;
		}	
	}
	if(document.requestForm.email.value == '') {
		alert_str += '\nMISSING E-Mail Address.';
		e = false;
	}
	else {
		if(chkEmail(document.requestForm.email) == false) {
			alert_str += '\nINVALID E-Mail Address.';
			e = false;
		}
	}
	if(e)
		return true;
	else {
		alert(alert_str);
		return false;
	}
}		

function open_win(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function close_window() {
    window.close();
}

function toggleshippinginfo(){

	if (document.checkoutform.sameasbillto.checked){
		copybillinginfo();
	} else{
		clearshippinginfo();
	}

}

function clearshippinginfo() {
		document.checkoutform.shiptofirstname.value = '';
		document.checkoutform.shiptolastname.value = '';
		document.checkoutform.shiptoaddress.value = '';
		document.checkoutform.shiptoaddress2.value = '';
		document.checkoutform.shiptocity.value = '';
		document.checkoutform.shiptostate.value = '';
		document.checkoutform.shiptozip.value = '';
		document.checkoutform.shiptocountry.value = '';
		document.checkoutform.shiptophone.value = '';
		document.checkoutform.shiptophone2.value = '';
		document.checkoutform.shiptoemail.value = '';

}

function copybillinginfo(){
	if(document.checkoutform.sameasbillto.checked){
		document.checkoutform.shiptofirstname.value = document.checkoutform.customerfirstname.value; 
		document.checkoutform.shiptolastname.value = document.checkoutform.customerlastname.value;
		document.checkoutform.shiptoaddress.value = document.checkoutform.customeraddress.value;
		document.checkoutform.shiptoaddress2.value = document.checkoutform.customeraddress2.value;
		document.checkoutform.shiptocity.value = document.checkoutform.customercity.value;
		document.checkoutform.shiptostate.value = document.checkoutform.customerstate.value;
		document.checkoutform.shiptozip.value = document.checkoutform.customerzip.value;
		document.checkoutform.shiptophone.value = document.checkoutform.customerphone.value;
		document.checkoutform.shiptoemail.value = document.checkoutform.customeremail.value;
		document.checkoutform.shiptophone2.value = document.checkoutform.customerphone2.value;
		document.checkoutform.shiptocountry.value = document.checkoutform.customercountry.value;
	}
}


function clearcheckoutform(){
	//if(confirm('Ok to delete all billing and shipping information?')){
		document.checkoutform.shiptofirstname.value = '';
		document.checkoutform.shiptofirstname.value = '';
		document.checkoutform.customerfirstname.value = '';
		document.checkoutform.customerlastname.value = '';
		document.checkoutform.customeraddress.value = '';
		document.checkoutform.customeraddress2.value = '';
		document.checkoutform.customercity.value = '';
		document.checkoutform.customerstate.value = '';
		document.checkoutform.customerzip.value = '';
		document.checkoutform.customercountry.value = '';
		document.checkoutform.shiptoaddress.value = '';
		document.checkoutform.shiptoaddress2.value = '';
		document.checkoutform.shiptocity.value = '';
		document.checkoutform.shiptostate.value = '';
		document.checkoutform.shiptozip.value = '';
		document.checkoutform.shiptocountry.value = '';
		document.checkoutform.shiptophone.value = '';
		document.checkoutform.customerphone.value = '';
		document.checkoutform.customeremail.value = '';
		/*
		document.checkoutform.nameoncard.value = '';
		document.checkoutform.cardno.value = '';
		document.checkoutform.cardid[0].selected = true;
		document.checkoutform.cardexpmonth[0].selected = true;
		document.checkoutform.cardexpyear[0].selected = true;
	//} */
}

function validatecheckoutinfo(){
	billing_error = false;
	shipping_error = false;
	payment_error = false;
	billing_alert = '     Billing Information:\n';
	shipping_alert = '     Shipping Information:\n';
	payment_alert = '     Payment Information:\n';
	alert_final = '';
	if(document.checkoutform.customerfirstname.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - First Name\n' 
	}
	if(document.checkoutform.customerlastname.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Last Name\n' 
	}
	if(document.checkoutform.customeraddress.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Address (line 1)\n' 
	}
	if(document.checkoutform.customercity.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - City\n' 
	}
	if(document.checkoutform.customerstate.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - State / Province\n' 
	}
	if(document.checkoutform.customerzip.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Postal Code\n' 
	}
	if(document.checkoutform.customercountry.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Country\n' 
	}
	if(document.checkoutform.customeremail.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Email Address\n' 
	}
	if(document.checkoutform.customerphone.value.length < 2){
		billing_error = true;
		billing_alert += '          Missing - Phone Number\n' 
	}
	// shipping info
	if(document.checkoutform.shiptofirstname.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - Recipient First Name\n' 
	}
	if(document.checkoutform.shiptolastname.value.length < 2){
		shipping_error = true;
		shipping_alert += '			 Missing - Recipient Last Name\n'
	}
	if(document.checkoutform.shiptoaddress.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - Address (line 1)\n' 
	}
	if(document.checkoutform.shiptocity.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - City\n' 
	}
	if(document.checkoutform.shiptostate.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - State / Province\n' 
	}
	if(document.checkoutform.shiptozip.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - Postal Code\n' 
	}
	if(document.checkoutform.shiptocountry.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - Country\n' 
	}
	if(document.checkoutform.shiptophone.value.length < 2){
		shipping_error = true;
		shipping_alert += '          Missing - Phone Number\n' 
	}
	//payment info
	if(document.checkoutform.cardname.value.length < 2){
		payment_error = true;
		payment_alert += '          Missing - Name on Credit Card\n' 
	}
	if(document.checkoutform.cardnumber.value.length < 2){
		payment_error = true;
		payment_alert += '          Missing - Credit Card Number\n' 
	}
	if(billing_error || shipping_error || payment_error){
		alert_final = 'Please correct the following before continuing:     \n';
		if(billing_error)
			alert_final += billing_alert;
		if(shipping_error)
			alert_final += shipping_alert;
		if(payment_error)
			alert_final += payment_alert;
		alert(alert_final);
		return false;
	}	
	else	
		return true;
}
