// External master script for order/specials pages
// =============================================================
// =============================================================


// checks if the fields are not empty and properly formatted
// =============================================================


function isFname() {
if (document.orderform.first_name.value == "") {
	alert ("The First Name field is blank.\n\nPlease enter your first name.");
	document.orderform.first_name.focus();
	return false;
}
return true;
}

function isLname() {
	if (document.orderform.last_name.value == "") {
	alert ("The Last Name field is blank.\n\nPlease enter your last name.");
	document.orderform.last_name.focus();
	return false;
}
return true;
}

function isCompany() {
	if (document.orderform.company_name.value == "") {
	alert ("The Company Name field is blank.\n\nPlease enter your company's name.");
	document.orderform.company_name.focus();
	return false;
}
return true;
}

function isAddress() {
	if (document.orderform.street_address.value == "") {
	alert ("The Street Address field is blank.\n\nPlease enter your street address.");
	document.orderform.street_address.focus();
	return false;
}
return true;
}

function isCity() {
	if (document.orderform.city.value == "") {
	alert ("The City field is blank.\n\nPlease enter your City.");
	document.orderform.city.focus();
	return false;
}
return true;
}

function isState() {
	if (document.orderform.state.value == "") {
	alert ("The State field is blank.\n\nPlease enter your State.");
	document.orderform.state.focus();
	return false;
}
return true;
}

function isZip() {
	if (document.orderform.zip_code.value == "") {
	alert ("The Zip code field is blank.\n\nPlease enter your Zip code.");
	document.orderform.zip_code.focus();
	return false;
}
//if (isNaN(document.orderform.zip_code.value)) {
//	alert ("The Zip code field should contain only numbers.");
//	document.orderform.zip_code.select();
//	document.orderform.zip_code.focus();
//	return false;
//}
return true;
}

function isCountry() {
	if (document.orderform.country.value == "") {
	alert ("The Country field is blank.\n\nPlease enter your Country.");
	document.orderform.country.focus();
	return false;
}
return true;
}

function isEmail() {
	if (document.orderform.email.value == "") {
	alert ("The Email field is blank.\n\nPlease enter your email address.");
	document.orderform.email.focus();
	return false;
}
if (document.orderform.email.value.indexOf ('@',0) == -1 ||
	document.orderform.email.value.indexOf ('.',0) == -1) {
	alert ("The Email field requires a \"@\" and a \".\"be used.\n\nPlease re-enter your email address.");
	document.orderform.email.select();
	document.orderform.email.focus();
	return false;
}
return true;
}

function isPhone() {
// Check if the phone field is blank
//	if (document.orderform.phone.value == "") {
//	alert ("The Phone field is blank.\n\nPlease enter the phone number.");
//	document.orderform.phone.focus();
//	return false;
//	}
// Strip out non-numeric characters
//	var stripped = document.orderform.phone.value.replace( /[\(\)\.\-\ ]/g, '' );
//	if (isNaN(stripped)) {
//	alert ("The phone number should contain only numbers.");
//	document.orderform.phone.select();
//	document.orderform.phone.focus();
//	return false;
//	}
// Verify length of phone field
//	if (stripped.length != 10) {
//	alert ("The Phone field is incorrect.\n\nPlease re-enter the phone number.");
//	document.orderform.phone.select();
//	document.orderform.phone.focus();
//	return false;
//	}
// Reformat phone field
//var newString = stripped.substring(0,3) + "-" + stripped.substring(3,6) + "-" + stripped.substring(6,10);
//document.orderform.phone.value = newString;
return true;
}

function isSFname() {
if (document.orderform.ship_first_name.value == "") {
	alert ("The First Name field is blank.\n\nPlease enter your first name.");
	document.orderform.ship_first_name.focus();
	return false;
}
return true;
}

function isSLname() {
	if (document.orderform.ship_last_name.value == "") {
	alert ("The Last Name field is blank.\n\nPlease enter your last name.");
	document.orderform.ship_last_name.focus();
	return false;
}
return true;
}

function isSCompany() {
	if (document.orderform.ship_company_name.value == "") {
	alert ("The Company Name field is blank.\n\nPlease enter your company's name.");
	document.orderform.ship_company_name.focus();
	return false;
}
return true;
}

function isSAddress() {
	if (document.orderform.ship_street_address.value == "") {
	alert ("The Street Address field is blank.\n\nPlease enter your street address.");
	document.orderform.ship_street_address.focus();
	return false;
}
return true;
}

function isSCity() {
	if (document.orderform.ship_city.value == "") {
	alert ("The City field is blank.\n\nPlease enter your City.");
	document.orderform.ship_city.focus();
	return false;
}
return true;
}

function isSState() {
	if (document.orderform.ship_state.value == "") {
	alert ("The State field is blank.\n\nPlease enter your State.");
	document.orderform.ship_state.focus();
	return false;
}
return true;
}

function isSZip() {
	if (document.orderform.ship_zip_code.value == "") {
	alert ("The Zip code field is blank.\n\nPlease enter your Zip code.");
	document.orderform.ship_zip_code.focus();
	return false;
}
//if (isNaN(document.orderform.ship_zip_code.value)) {
//	alert ("The Zip code field should contain only numbers.");
//	document.orderform.ship_zip_code.select();
//	document.orderform.ship_zip_code.focus();
//	return false;
//}
return true;
}

function isSCountry() {
	if (document.orderform.ship_country.value == "") {
	alert ("The Country field is blank.\n\nPlease enter your Country.");
	document.orderform.ship_country.focus();
	return false;
}
return true;
}

function isSEmail() {
	if (document.orderform.ship_email.value == "") {
	alert ("The Email field is blank.\n\nPlease enter your email address.");
	document.orderform.ship_email.focus();
	return false;
}
if (document.orderform.ship_email.value.indexOf ('@',0) == -1 ||
	document.orderform.ship_email.value.indexOf ('.',0) == -1) {
	alert ("The Email field requires a \"@\" and a \".\"be used.\n\nPlease re-enter your email address.");
	document.orderform.ship_email.select();
	document.orderform.ship_email.focus();
	return false;
}
return true;
}

function isSPhone() {
// Check if the phone field is blank
//	if (document.orderform.ship_phone.value == "") {
//	alert ("The Phone field is blank.\n\nPlease enter the phone number.");
//	document.orderform.ship_phone.focus();
//	return false;
//	}
// Strip out non-numeric characters
//	var stripped = document.orderform.ship_phone.value.replace( /[\(\)\.\-\ ]/g, '' );
//	if (isNaN(stripped)) {
//	alert ("The phone number should contain only numbers.");
//	document.orderform.ship_phone.select();
//	document.orderform.ship_phone.focus();
//	return false;
//	}
// Verify length of phone field
//	if (stripped.length != 10) {
//	alert ("The Phone field is incorrect.\n\nPlease re-enter the phone number.");
//	document.orderform.ship_phone.select();
//	document.orderform.ship_phone.focus();
//	return false;
//	}
// Reformat phone field
//var newString = stripped.substring(0,3) + "-" + stripped.substring(3,6) + "-" + stripped.substring(6,10);
//document.orderform.ship_phone.value = newString;
return true;
}

function isProgramSelection() {
	for (i=0; i<2; i++) {
    if (document.orderform.program_sel[i].checked) {
		var choice = document.orderform.program_sel[i].value;
    }
	}
	if (document.orderform.programQuantity.value > 0) {
		if (!choice) {
			alert("Please select a media type for the Complete Program.");
			document.orderform.programQuantity.focus();
			return false;
		}
	}
return true;
}

function isVid21Selection() {
	for (i=0; i<2; i++) {
    if (document.orderform.vid21_sel[i].checked) {
		var choice = document.orderform.vid21_sel[i].value;
    }
	}
	if (document.orderform.vid21Quantity.value > 0) {
		if (!choice) {
			alert("Please select a media type for the 21 Minute Video.");
			document.orderform.vid21Quantity.focus();
			return false;
		}
	}
return true;
}

function isVid28Selection() {
	for (i=0; i<2; i++) {
    if (document.orderform.vid28_sel[i].checked) {
		var choice = document.orderform.vid28_sel[i].value;
    }
	}
	if (document.orderform.vid28Quantity.value > 0) {
		if (!choice) {
			alert("Please select a media type for the 28 Minute Video.");
			document.orderform.vid28Quantity.focus();
			return false;
		}
	}
return true;
}

function isVid14Selection() {
	for (i=0; i<2; i++) {
    if (document.orderform.vid14_sel[i].checked) {
		var choice = document.orderform.vid14_sel[i].value;
    }
	}
	if (document.orderform.vid14Quantity.value > 0) {
		if (!choice) {
			alert("Please select a media type for the 14 Minute Video.");
			document.orderform.vid14Quantity.focus();
			return false;
		}
	}
return true;
}

function isPayType() {
	for (i=0; i<2; i++) {
		if (document.orderform.pay_type[i].checked) {
			var payment = document.orderform.pay_type[i].value;
		}
	}
//	alert (payment);
	if (!payment) {
	alert ("The Payment Type field is blank.\n\nPlease select the method by which you wish to pay.");
	document.orderform.productListTotal.focus();
	return false;
	}
	if (payment == "po") {
//	return isPO();
	}
	if (payment == "cc") {
	return validateCC();
	}
return true;
}

function disable() {
	for (i=0; i<2; i++) {
	if (document.orderform.pay_type[i].checked) {
		var payment = document.orderform.pay_type[i].value;
		}
	}
	if (payment == "po") {
	document.orderform.po_number.disabled=false;
	document.orderform.credit_card_type.disabled=true;
	document.orderform.credit_card_number.disabled=true;
	document.orderform.credit_card_month.disabled=true;
	document.orderform.credit_card_year.disabled=true;
//	alert ("po");
	}
	if (payment == "cc") {
	document.orderform.po_number.disabled=true;
	document.orderform.credit_card_type.disabled=false;
	document.orderform.credit_card_number.disabled=false;
	document.orderform.credit_card_month.disabled=false;
	document.orderform.credit_card_year.disabled=false;
//	alert ("cc");
	}
return true;
}

function isPO() {
	if (document.orderform.po_number.value == "") {
	alert ("The PO Number field is blank.\n\nPlease enter your PO Number.");
	document.orderform.po_number.focus();
	return false;
}
return true;
}


// calculate prices and shipping charges
// =============================================================


function CalculateValues() {
// Calculates new values if quantities are changed in product list

var qtyOrdered = 0.0;
var productDiscount = 0.0;
var productListQuantity = 0.0;
var productListShipping = 0.0;
var runningSubTotal = 0.0;
var runningTotal = 0.0;
var index;

// First Item
document.orderform.programSubtotal.value = Math.round((document.orderform.programQuantity.value * programPrice)*100)/100;
	if(isNaN(document.orderform.programSubtotal.value)) 
	{
		alert("You must enter a numerical value!");
		Clearfields();
		return true;
	}

index = document.orderform.programSubtotal.value.indexOf(".");
	if (index < 0) document.orderform.programSubtotal.value = document.orderform.programSubtotal.value + ".00";
		runningSubTotal = parseFloat(document.orderform.programSubtotal.value);
		qtyOrdered = parseFloat(document.orderform.programQuantity.value);

// Second Item
document.orderform.vid14Subtotal.value = Math.round((document.orderform.vid14Quantity.value * vid14Price)*100)/100;
	if(isNaN(document.orderform.vid14Subtotal.value)) 
	{
		alert("You must enter a numerical value!");
		Clearfields();
		return true;
	}

index = document.orderform.vid14Subtotal.value.indexOf(".");
	if (index < 0) document.orderform.vid14Subtotal.value = document.orderform.vid14Subtotal.value + ".00";
		runningSubTotal += parseFloat(document.orderform.vid14Subtotal.value);
		qtyOrdered += parseFloat(document.orderform.vid14Quantity.value);

// Third Item
document.orderform.vid21Subtotal.value = Math.round((document.orderform.vid21Quantity.value * vid21Price)*100)/100;
	if(isNaN(document.orderform.vid21Subtotal.value)) 
	{
		alert("You must enter a numerical value!");
		Clearfields();
		return true;
	}

index = document.orderform.vid21Subtotal.value.indexOf(".");
	if (index < 0) document.orderform.vid21Subtotal.value = document.orderform.vid21Subtotal.value + ".00";
		runningSubTotal += parseFloat(document.orderform.vid21Subtotal.value);
		qtyOrdered += parseFloat(document.orderform.vid21Quantity.value);

// Forth Item
document.orderform.vid28Subtotal.value = Math.round((document.orderform.vid28Quantity.value * vid28Price)*100)/100;
	if(isNaN(document.orderform.vid28Subtotal.value)) 
	{
		alert("You must enter a numerical value!");
		Clearfields();
		return true;
	}

index = document.orderform.vid28Subtotal.value.indexOf(".");
	if (index < 0) document.orderform.vid28Subtotal.value = document.orderform.vid28Subtotal.value + ".00";
		runningSubTotal += parseFloat(document.orderform.vid28Subtotal.value);
		qtyOrdered += parseFloat(document.orderform.vid28Quantity.value);

// Last Item
document.orderform.manualSubtotal.value = Math.round((document.orderform.manualQuantity.value * manualPrice)*100)/100;
	if(isNaN(document.orderform.manualSubtotal.value)) 
	{
		alert("You must enter a numerical value!");
		Clearfields();
		return true;
	}

index = document.orderform.manualSubtotal.value.indexOf(".");
	if (index < 0) document.orderform.manualSubtotal.value = document.orderform.manualSubtotal.value + ".00";
	else 
	{
		document.orderform.manualSubtotal.value = document.orderform.manualSubtotal.value.substring(0, index + 3);
		if (document.orderform.manualSubtotal.value.length < (index + 3)) document.orderform.manualSubtotal.value += "0";
	}
	runningSubTotal += parseFloat(document.orderform.manualSubtotal.value);
	qtyOrdered += parseFloat(document.orderform.manualQuantity.value);

//  Quantity total field
document.orderform.productListQuantity.value = qtyOrdered;
index = document.orderform.productListQuantity.value.indexOf(".");
if (index < 0) document.orderform.productListQuantity.value = document.orderform.productListQuantity.value;
	else 
	{
		document.orderform.productListQuantity.value = document.orderform.productListQuantity.value.substring(0, index + 3);
		if (document.orderform.productListQuantity.value.length < (index + 3)) document.orderform.productListQuantity.value += "0";
	}

//  Subtotal field
document.orderform.productListSubtotal.value = runningSubTotal;
index = document.orderform.productListSubtotal.value.indexOf(".");
if (index < 0) document.orderform.productListSubtotal.value = document.orderform.productListSubtotal.value + ".00";
	else 
	{
		document.orderform.productListSubtotal.value = document.orderform.productListSubtotal.value.substring(0, index + 3);
		if (document.orderform.productListSubtotal.value.length < (index + 3)) document.orderform.productListSubtotal.value += "0";
	}
	runningTotal = parseFloat(document.orderform.productListSubtotal.value);
  
// Tax field
if (document.orderform.ship_state.value == "CA" || document.orderform.ship_state.value == "ca")
document.orderform.productListTax.value = Math.round((runningSubTotal * .0825)*100)/100;
	else
document.orderform.productListTax.value = 0.0;
index = document.orderform.productListTax.value.indexOf(".");
if (index < 0) document.orderform.productListTax.value = document.orderform.productListTax.value + ".00";
	else 
	{
		document.orderform.productListTax.value = document.orderform.productListTax.value.substring(0, index + 3);
		if (document.orderform.productListTax.value.length < (index + 3)) document.orderform.productListTax.value += "0";
	}
	runningTotal += parseFloat(document.orderform.productListTax.value);


// Discount calculation table
// calculation based on quantity
if (document.orderform.discount.value == "quantity") {
if (document.orderform.productListQuantity.value < 2)
	{
	var discount = 0;
	};
else if (document.orderform.productListQuantity.value >= 2 && document.orderform.productListQuantity.value <= 5)
	{
	var discount = 5;
	};
else if (document.orderform.productListQuantity.value >= 6 && document.orderform.productListQuantity.value <= 8)
	{
	var discount = 7;
	};
else if (document.orderform.productListQuantity.value > 8)
	{
	var discount = 10;
	};
}

// calculation based on flat rate
else if (document.orderform.discount.value == "flat") {
if (document.orderform.discountCode.value == "")
	{
	var discount = 0.00;
	};
else if (document.orderform.discountCode.value == "mgd")
	{
	var discount = 50.00;
	};
else if (document.orderform.discountCode.value == "code")
	{
	var discount = 75.00;
	};
else if (document.orderform.discountCode.value == "superdeal")
	{
	var discount = 100.00;
	};
else
	{
	var discount = 0.00;
	};
}

// calculation based on a percentage
else if (document.orderform.discount.value == "percent") {
if (document.orderform.discountCode.value == "")
	{
	var discount = 0;
	};
else if (document.orderform.discountCode.value == "aa1" || document.orderform.discountCode.value == "AA1")
	{
	var discount = 10;
	};
else if (document.orderform.discountCode.value == "aa2" || document.orderform.discountCode.value == "AA2")
	{
	var discount = 15;
	};
else if (document.orderform.discountCode.value == "aa3" || document.orderform.discountCode.value == "AA3")
	{
	var discount = 20;
	};
else if (document.orderform.discountCode.value == "aa4" || document.orderform.discountCode.value == "AA4")
	{
	var discount = 30;
	};

else if (document.orderform.discountCode.value == "aa5" || document.orderform.discountCode.value == "AA5")
	{
	var discount = 40;
	};
else if (document.orderform.discountCode.value == "aa6" || document.orderform.discountCode.value == "AA6")
	{
	var discount = 50;
	};
else if (document.orderform.discountCode.value == "aa7" || document.orderform.discountCode.value == "AA7")
	{
	var discount = 60;
	};
else
	{
	var discount = 0;
	};
}

// Discount calculation
// calculation based on quantity
if (document.orderform.discount.value == "quantity") {
	document.orderform.productDiscount.value = Math.round( runningSubTotal * (discount/100) );
}
// calculation based on flat rate
else if (document.orderform.discount.value == "flat") {
	document.orderform.productDiscount.value = discount;
}
// calculation based on a percentage
if (document.orderform.discount.value == "percent") {
	document.orderform.productDiscount.value = Math.round( runningSubTotal * (discount/100) );
}

index = document.orderform.productDiscount.value.indexOf(".");
if (index < 0) document.orderform.productDiscount.value = document.orderform.productDiscount.value + ".00";
	else 
	{
		document.orderform.productDiscount.value = document.orderform.productDiscount.value.substring(0, index + 3);
		if (document.orderform.productDiscount.value.length < (index + 3)) document.orderform.productDiscount.value += "0";
	}
	runningTotal -= parseFloat(document.orderform.productDiscount.value);


// Variable shipping cost table
if (document.orderform.productListQuantity.value == 0)
	{
	var groundShipping = 0.00;
	var twoDayShipping = 0.00;
	var nextDayShipping = 0.00;
	};
if (document.orderform.productListQuantity.value >= 1 && document.orderform.productListQuantity.value <= 4)
	{
	var groundShipping = 9.65;
	var twoDayShipping = 5.95;
	var nextDayShipping = 12.95;
	};
else if (document.orderform.productListQuantity.value >= 5 && document.orderform.productListQuantity.value <= 10)
	{
	var groundShipping = 12.75;
	var twoDayShipping = 14.25;
	var nextDayShipping = 23.75;
	};
else if (document.orderform.productListQuantity.value >= 6 && document.orderform.productListQuantity.value <= 25)
	{
	var groundShipping = 17.25;
	var twoDayShipping = 24.95;
	var nextDayShipping = 44.75;
	};
else if (document.orderform.productListQuantity.value > 25)
	{
	var groundShipping = 21.25;
	var twoDayShipping = 29.75;
	var nextDayShipping = 85.25;
	};

// Shipping field
switch (document.orderform.ShippingMethod.value)
	{
	case "fedexGround":
		document.orderform.productListShipping.value = groundShipping; // + ".00";
		break;
	case "ship2day":
		document.orderform.productListShipping.value = twoDayShipping; // + ".00";
		break;
	case "ship1day":
		document.orderform.productListShipping.value = nextDayShipping; // + ".00";
		break;
	default:
		document.orderform.productListShipping.value = "0.00";
	}
	runningTotal += parseFloat(document.orderform.productListShipping.value);

// Total price field
document.orderform.productListTotal.value = runningTotal;
index = document.orderform.productListTotal.value.indexOf(".");
	if (index < 0) document.orderform.productListTotal.value = document.orderform.productListTotal.value + ".00";
	else 
	{
		document.orderform.productListTotal.value = document.orderform.productListTotal.value.substring(0, index + 3);
		if (document.orderform.productListTotal.value.length < (index + 3)) document.orderform.productListTotal.value += "0";
	}

if (document.orderform.productListQuantity.value == 0) {
	alert ("Please select the products which you would like to order.");
	document.orderform.programQuantity.focus();
	return false;
}

return true;
}


// check if a shipping method has been selected
// =============================================================
function shipPick() {
if (document.orderform.ShippingMethod.value == "") {
	alert ("The Shipping Method field is blank.\n\nPlease select your shipping method.");
	return false;
}
return true;
}


// validates credit card number and expiration date
// =============================================================

function validateCC() {
	if(!document.orderform.credit_card_type.value) {
		alert("Please select a credit card type.");
		return false;
	}
	resultName = validateCCName();
	if (resultName==false) {
		return false;
	}
	resultNumber = validateCCNumber(ccNum);
	if (resultNumber==false) {
		alert("Something is amiss with your credit card number, please check.");
		document.orderform.credit_card_number.focus();
		return false;
	}
	if (!document.orderform.credit_card_month.value || !document.orderform.credit_card_year.value) {
		alert("Please select your credit card expiration date.");
		return false;
	}
	resultDate = ValidateCCDate();
	if (resultDate==false) {
		return false;
	}
return true;
}


// validate credit card type
// ===============================================

function validateCCName() {
	errorMsg="Good";
	results = true;
	cardType = document.orderform.credit_card_type.selectedIndex;
	vendor = document.orderform.credit_card_type[cardType].text;
	cardNumber = document.orderform.credit_card_number.value;
	ccNum = "";
	for (i=0; i<cardNumber.length; i++) {
		if("0123456789".indexOf(cardNumber.substring(i,i+1)) > -1) {
			ccNum += cardNumber.substring(i,i+1)
		}
	}
	if (vendor ==  "MasterCard") {
			checkNum = ccNum.substring(0,2);
			if (checkNum*1 < 51 || checkNum*1 > 55) {
				errorMsg="Please type in a valid MasterCard number.";
				results="false";
			}
			if (ccNum.length != 16) {
				errorMsg="Please type in a valid MasterCard number.";
				results="false";
			}
	} else if (vendor == "Visa")  {
			checkNum = ccNum.substring(0,1);
			if (checkNum*1 != 4) {
				errorMsg="Please type in a valid Visa number.";
				results="false";
			}
			if ((ccNum.length != 16) && (ccNum.length != 13)) {
				errorMsg="Please type in a valid Visa number.";
				results="false";
			}
	} else if (vendor == "Discover")  {
			checkNum = ccNum.substring(0,4);
			if (checkNum*1 != 6011) {
				errorMsg="Please type in a valid Discover number.";
				results="false";
			}
			if (ccNum.length != 16) {
				errorMsg="Please type in a valid Discover number.";
				results="false";
			}
	} else if (vendor == "American Express")  {
			checkNum = ccNum.substring(0,2);
			if ((checkNum*1 != 34) && (checkNum*1 != 37)) {
				errorMsg="Please type in a valid American Express number.";
				results="false";
			}
			if (ccNum.length != 15) {
				errorMsg="Please type in a valid American Express number.";
				results="false";
			}
	} 
        if (errorMsg != "Good") {
		alert(errorMsg);
		document.orderform.credit_card_number.focus();
		return false;
	}
	return results;
}


// validate credit card number
// ===============================================

//function validateCCNumber(ccNum) {
//	results=true;
//	accum = 0;
//	cardNumber = document.orderform.credit_card_number.value;
//	ccNum = "";
//	for (i=0; i<cardNumber.length; i++) {
//		if("0123456789".indexOf(cardNumber.substring(i,i+1)) > -1) {
//			ccNum += cardNumber.substring(i,i+1)
//		}
//	}
//	for (i = 0; i < ccNum.length; i++) {
//		if(i%2==0) {
//			value = ccNum.substring(i,i+1)*2;
//			if (value > 9) {
//				value -= 9;
//			}
//		accum += value;
//		}
//		else {
//			accum += ccNum.substring(i,i+1)*1;
//		}
//	}
//	if (accum % 10 == 0) {
//		results = true;
//	}
//	else {
//		results = false;
//	}
//	return results;
//}


// validate credit card number
// ===============================================

function validateCCNumber(ccNum) {
	results=true;
//	accum = 0;
	cardNumber = document.orderform.credit_card_number.value;
	ccNum = "";
//	for (i=0; i<cardNumber.length; i++) {
//		if("0123456789".indexOf(cardNumber.substring(i,i+1)) > -1) {
//			ccNum += cardNumber.substring(i,i+1)
//		}
//	}
//	for (i = 0; i < ccNum.length; i++) {
//		if(i%2==0) {
//			value = ccNum.substring(i,i+1)*2;
//			if (value > 9) {
//				value -= 9;
//			}
//		accum += value;
//		}
//		else {
//			accum += ccNum.substring(i,i+1)*1;
//		}
//	}
//	if (accum % 10 == 0) {
//		results = true;
//	}
//	else {
//		results = false;
//	}

var v = "0123456789";
	var w = "";
	for (i=0; i < cardNumber.length; i++) {
		x = cardNumber.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
	}
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);

	return results;
}


// validate credit card expiration date
// ===============================================

function ValidateCCDate() {
	results=true;

	ExpMonth=document.orderform.credit_card_month.value;
	ExpYear=document.orderform.credit_card_year.value;

	now=new Date;
	curYear=now.getYear();
	if (curYear < 1900) {
		curYear += 1900;
	}
	if(ExpYear > curYear) {
		results=true;
	}
	else if(ExpYear == curYear) {
		if(ExpMonth >= (now.getMonth())) {
			results=true;
		}
		else {
			alert("Your card has already expired.");
			results=false;
		}
	}
	else {
		alert("Your card has already expired.");
		results=false;
	}
	return results;
}


// miscellenious scripts
// =============================================================

function Clearfields() { 
	document.orderform.fdQuantity.value = "";
	document.orderform.fdSubtotal.value = "";
	document.orderform.fdavQuantity.value = "";
	document.orderform.fdavSubtotal.value = "";
	document.orderform.sfQuantity.value = "";
	document.orderform.sfSubtotal.value = "";
	document.orderform.productListSubtotal.value = "";
	document.orderform.productListTax.value = "";
	document.orderform.productListQuantity.value = "";
	document.orderform.productListShipping.value = "";
	document.orderform.productListTotal.value = "";
}

function FillShippingAddressFields() {
	document.orderform.ship_first_name.value = document.orderform.first_name.value;
	document.orderform.ship_last_name.value = document.orderform.last_name.value;
	document.orderform.ship_title.value = document.orderform.title.value;
	document.orderform.ship_company_name.value  = document.orderform.company_name.value;
	document.orderform.ship_street_address.value = document.orderform.street_address.value;
	document.orderform.ship_city.value = document.orderform.city.value;
	document.orderform.ship_state.value = document.orderform.state.value;
	document.orderform.ship_zip_code.value = document.orderform.zip_code.value;
	document.orderform.ship_country.value = document.orderform.country.value;
	document.orderform.ship_email.value = document.orderform.email.value;
	document.orderform.ship_phone.value = document.orderform.phone.value;
}

