// JavaScript Document
function checkPayType() {
var f = document.orderForm;

	if(f.PaymentType.selectedIndex == 1) {
		document.getElementById("credit_info").style.display = "block";
		var paypal_accept = document.getElementById("paypal_accept");
		if(paypal_accept != null) {paypal_accept.style.display = 'none';}
		
		f.CCNumber.disabled = false;
		f.CCMonth.disabled = false;
		f.CCYear.disabled = false;
		f.CCName.disabled = false;
		f.CCAddress.disabled = false;
		f.CCCity.disabled = false;
		f.CCCountry.disabled = false;
		f.CCState.disabled = false;
		f.CCZip.disabled = false;
		
		f.CCNumber.focus();
	}
}

function updateTotal() {
	var f = document.orderForm;
	if(f.Item[0].checked) {var total = 150;}
	if(f.Item[1].checked) {var total = 175;}
	if(!f.Item[0].checked && !f.Item[1].checked) {var total = 0;}
	
	f.hiddentotal.value = total;
	f.hiddensubtotal.value = total;
	f.Total.value = formatCurrency(total);
}

function checkCountry() {
	var f = document.orderForm;
	if ((f.Country.selectedIndex != 0) && (f.Country.selectedIndex != 1) && (f.Country.selectedIndex != 36)) {
		f.State.selectedIndex = 1;
	}
}
function checkCCCountry() {
	var f = document.orderForm;
	if ((f.CCCountry.selectedIndex != 0) && (f.CCCountry.selectedIndex != 1) && (f.CCCountry.selectedIndex != 36)) {
		f.CCState.selectedIndex = 1;
	}
}

function compilePayPal() {
	var f = document.orderForm;
	var p = document.paypal_fields;
	var sep = '~';
	
	// Regular form values
	var FirstName = f.FirstName.value;
	var LastName = f.LastName.value;
	var Company = f.Company.value;
	var Address = f.Address1.value;
	var Address2 = f.Address2.value;
	var City = f.City.value;
	var State = f.State.options[f.State.selectedIndex].value;
	var Country = f.Country.options[f.Country.selectedIndex].value;
	var Zip = f.Zip.value;
	var Phone = f.PhoneNumber.value;
	var Email = f.Email.value;
	var hiddenTotal = f.hiddentotal.value;
	
	if(f.Item[0].checked) {var ItemSelected = 'TrifectaNormal';}
	if(f.Item[1].checked) {var ItemSelected = 'TrifectaPremium';}
	
	var CustomStr = Phone + sep + Company + sep + ItemSelected;
	
	// -----------------------------------------------------------------------------------
	// PayPal specific form values
	// all values have been gathered, now just set all of the paypal values to the normal form values
	p.p_FirstName.value = FirstName;
	p.p_LastName.value = LastName;
	p.p_Company.value = Company;
	p.p_Address1.value = Address;
	p.p_Address2.value = Address2;
	p.p_City.value = City;
	p.p_State.value = State;
	p.p_Zip.value = Zip;
	p.p_Country.value = Country;
	p.p_Email.value = Email;
	p.p_Custom.value = CustomStr;
	p.p_hiddenTotal.value = hiddenTotal;
}

function paymentInfo() {
	var f = document.orderForm;
	document.getElementById("credit_info").style.display = "none";
	
	if(f.PaymentType.selectedIndex == 1) {
		document.getElementById("credit_info").style.display = "block";
		var paypal_accept = document.getElementById("paypal_accept");
		if(paypal_accept != null) {paypal_accept.style.display = 'none';}

		f.CCNumber.focus();
	}
	
	if(f.PaymentType.selectedIndex == 2) {
		var paypal_accept = document.getElementById("paypal_accept");
		if(paypal_accept != null) {paypal_accept.style.display = 'block';}
		
		f.CCNumber.value = '';
		f.CCMonth.selectedIndex = 0;
		f.CCYear.selectedIndex = 0;
		f.CCName.value = '';
		f.CCAddress.value = '';
		f.CCCity.value = '';
		f.CCCountry.selectedIndex = 0 ;
		f.CCState.selectedIndex = 0;
		f.CCZip.value = '';
		
		compilePayPal();
		if(f.PayerID.value == '') {document.paypal_fields.submit();}
	}
}


function sameInfo() {
	 var f = document.orderForm;
	 if(f.same_info.checked) {
	 	cc_name = f.FirstName.value + " " + f.LastName.value;
		cc_address = f.Address1.value + " " + f.Address2.value;
		cc_city = f.City.value;
		cc_state = f.State.selectedIndex;
		cc_country = f.Country.selectedIndex;
		cc_zip = f.Zip.value;

		f.CCName.value = cc_name;
		f.CCAddress.value = cc_address;
		f.CCCity.value = cc_city;
		f.CCState.selectedIndex = cc_state;
		f.CCCountry.selectedIndex = cc_country;
		f.CCZip.value = cc_zip;
	 }
	 else {
	 	f.CCName.value = '';
		f.CCAddress.value = '';
		f.CCCity.value = '';
		f.CCState.selectedIndex = 0;
		f.CCCountry.selectedIndex = 0;
		f.CCZip.value = '';
	 }
}

function validateForm() {
	var f = document.orderForm;
	var selectedCountry = document.orderForm.Country.options[document.orderForm.Country.selectedIndex].value;
	var selectedCCCountry = document.orderForm.CCCountry.options[document.orderForm.CCCountry.selectedIndex].value;
	var addr_1 = f.Address1.value.toLowerCase();
	var addr_2 = f.Address2.value.toLowerCase();
	
	var po_error1 = false;
	var po_error2 = false;
	var fail_po = new Array();
	fail_po[0] = "p.o";
	fail_po[1] = "box#";
	fail_po[2] = "box #";
	fail_po[3] = "box no";
	fail_po[4] = "boxno";
	fail_po[5] = "box num";
	fail_po[6] = "boxnum";
	fail_po[7] = "box ";
	fail_po[8] = "po b";
	fail_po[9] = "po #";
	fail_po[10] = "po.";
	
	if(!f.Item[0].checked && !f.Item[1].checked) {
		alert("Please select either Trifecta or Trifecta Premium");
		f.Item[0].focus();
		return false;
	}
	if (!f.FirstName.value){
		alert("Please fill in your First Name");
		f.FirstName.focus();
		return false;
	}
	if (!f.LastName.value){
		alert("Please fill in your Last name.");
		f.LastName.focus();
		return false;
	}
	if (!f.Address1.value){
		alert("Please fill in the Address 1 field.");
		f.Address1.focus();
		return false;
	}
	if (!f.City.value){
		alert("Please fill in the City field.");
		f.City.focus();
		return false;
	}
	if (f.State.selectedIndex == 0){
		alert("Please select the State / Province of your Shipping Address.");
		f.State.focus();
		return false;
	}
	if (!f.Zip.value){
		alert("Please fill in the Zip / Postal code field.");
		f.Zip.focus();
		return false;
	}
	if(f.Country.selectedIndex == 0) {
		alert("Please choose the country of your Shipping Address");
		f.Country.focus();
		return false;
	}
	if((f.Country.selectedIndex != 1) && (f.Country.selectedIndex != 36)) {
		if(f.State.selectedIndex != 1) {
		alert("Please select \"Click Here for International\" in the Shipping Address State / Province dropdown list.");
		f.State.focus();
		return false;
		}
	}
	if((f.Country.selectedIndex == 1) || (f.Country.selectedIndex == 36)) {
		if(f.State.selectedIndex == 1) {
		alert("Please select the State / Province of your Shipping Address.");
		f.State.focus();
		return false;
		}
	}
	if(!f.PhoneNumber.value) {
		alert("Please fill in your Phone Number to continue.");
		f.PhoneNumber.focus();
		return false;
	}
	if(!isEmailAddr(f.Email.value)) {
		alert("Please fill in a correctly formatted Email Address to continue.");
		f.Email.focus();
		return false;
	}
	if(f.Email.value != f.EmailConfirm.value) {
		alert("Your Email Address does not match the Confirm Email address, please correct this to continue.");
		f.Email.focus();
		return false;	
	}

	if(f.Country.selectedIndex == 1) {
		for(var i=0;i<fail_po.length;i++) {
			if(addr_1.indexOf(fail_po[i]) != -1) {
				po_error1 = true;
				break;
			}
			else {
				po_error1 = false;
				continue;
			}
		}
		
		for(var j=0;j<fail_po.length;j++) {
			if(addr_2.indexOf(fail_po[j]) != -1) {
				po_error2 = true;
				break;
			}
			else {
				po_error2 = false;
				continue;
			}
		}
	}
	
	if(po_error1 || po_error2) {
		alert("The address you have entered has been identified as a P.O. Box. FedEx will not deliver to P.O. Boxes inside the United States, please enter a street address to continue.");
		f.Address1.focus();
		return false;
	}
	if(f.PaymentType.selectedIndex == 0) {
		alert("Please select a Payment Method.");
		f.PaymentType.focus();
		return false;
	}
	if(f.PaymentType.selectedIndex == 1) {
		if (!f.CCNumber.value){
			alert("Please fill in your Credit Card Number");
			f.CCNumber.focus() ;
			return false;
		}
		if (f.CCMonth.options[f.CCMonth.selectedIndex].value == "--"){
			alert("Please choose your Expiration Date: Month for your credit card");
			return false;
		}
		if (f.CCYear.options[f.CCYear.selectedIndex].value == "--"){
			alert("Please choose your Expiration Date: Year for your credit card");
			return false;
		}
		if(f.CCYear.selectedIndex != 0) {
			var start_month = f.CCMonth.options[f.CCMonth.selectedIndex].value;
			var start_day = '01';
			var start_year = '20'+f.CCYear.options[f.CCYear.selectedIndex].value;
			var current = new Date();
			var curr_month = current.getMonth();
			
			// converts curr_month to a proper 2-digit month code
			if(curr_month == 0) {var end_month = '01';}
			if(curr_month == 1) {var end_month = '02';}
			if(curr_month == 2) {var end_month = '03';}
			if(curr_month == 3) {var end_month = '04';}
			if(curr_month == 4) {var end_month = '05';}
			if(curr_month == 5) {var end_month = '06';}
			if(curr_month == 6) {var end_month = '07';}
			if(curr_month == 7) {var end_month = '08';}
			if(curr_month == 8) {var end_month = '09';}
			if(curr_month == 9) {var end_month = '10';}
			if(curr_month == 10) {var end_month = '11';}
			if(curr_month == 11) {var end_month = '12';}
			
			var end_day = '01';
			var end_year = current.getFullYear();
			
			var s_date = start_month + '/' + start_day + '/' + start_year;
			var c_date = end_month + '/' + end_day + '/' + end_year;
			var start_date = new Date(s_date);
			var current_date = new Date(c_date);
			
			if (Date.parse(start_date) < Date.parse(current_date)) {
				alert("The credit card expiration date you have chosen appears to be earlier than the current date. Please adjust this.");
				f.CCMonth.focus();
				return false;
			}
		}
		if (!f.CCName.value){
			alert("Please fill in the \"Name on Card\" field.");
			f.CCName.focus() ;
			return false;
		}
		if (!f.CCAddress.value){
			alert("Please fill in the Credit Card Address field");
			f.CCAddress.focus();
			return false;
		}
		if (!f.CCCity.value){
			alert("Please fill in the Credit Card City field");
			f.CCCity.focus();
			return false;
		}
		if (f.CCState.selectedIndex == 0){
			alert("Please choose the State / Province of your Billing address.");
			f.CCState.focus();
			return false;
		}
		if (!f.CCZip.value){
			alert("Please fill in the Credit Card Zip / Postal field");
			f.CCZip.focus();
			return false;
		}
		if (f.CCCountry.selectedIndex == 0){
			alert("Please choose the country of your Billing address.");
			f.CCCountry.focus();
			return false;
		}
		if((f.CCCountry.selectedIndex != 1) && (f.CCCountry.selectedIndex != 36)) {
			if(f.CCState.selectedIndex != 1) {
				alert("Please select \"Click Here for International\" in the Billing Address State / Province dropdown list.");
				f.CCState.focus();
				return false;
			}
		}
		
		if((f.CCCountry.selectedIndex == 1) || (f.CCCountry.selectedIndex == 36)) {
			if(f.CCState.selectedIndex == 1) {
				alert("Please select the State / Province of your Billing Address.");
				f.CCState.focus();
				return false;
			}
		}
	}
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function formatCurrency(num) {
   // Courtesy of http://www7.brinkster.com/cyanide7/
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	   num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
			  num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function isEmailAddr(Email) {
 	var result = false
 	var theStr = new String(Email)
	var index = theStr.indexOf("@");
	if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
 	result = true;
  }
 return result;
}

