//////////////////END DISABLE RIGHT CLICK/////////////////////////////
function updateTotals(){
	var elem=getElementByIdCompatible("Contact0State");
	var elem2=getElementByIdCompatible("qty");
	var productPrice=19.95;
	var addproductPrice=9.95;
	var shippingCost=9.95;
	var taxes=0.1;
	if (elem.options[elem.selectedIndex].value=="AZ"){
		getElementByIdCompatible("taxinfo").style.display="";
		taxes=0.056;
	} else if (elem.options[elem.selectedIndex].value=="UT"){
		getElementByIdCompatible("taxinfo").style.display="";
		taxes=0.0475;
	} else{
		getElementByIdCompatible("taxinfo").style.display="none";
		taxes=0;
	}
	if (isNaN(elem2.value)){
		elem2.value=1;
	} else {
		elem2.value=parseInt(elem2.value);
	}
	var quantity=elem2.value;
	if (elem2.value < 1){
		elem2.value =1;
		quantity = 1;
	}
	if (quantity > 1){
		shippingCost = formatCurrency(shippingCost+((quantity-1)*3.95));
	}
	var subtotal=formatCurrency(productPrice+(addproductPrice*(quantity-1)));
	var taxtotal=formatCurrency(subtotal*taxes);
	var total=formatCurrency(subtotal*1+taxtotal*1+shippingCost*1);

	getElementByIdCompatible("SubTotal").innerHTML = "$"+subtotal;
	getElementByIdCompatible("TaxTotal").innerHTML = "$"+taxtotal;
	getElementByIdCompatible("ShippingCost").innerHTML = "$"+shippingCost;
	getElementByIdCompatible("InvoiceTotal").innerHTML = "$"+total;
}
function formatCurrency(num) {
	num = isNaN(num) || num === '' || num === null ? 0.00 : num;
	return parseFloat(num).toFixed(2); 
} 
function clickIE4(){
	if (event.button==2)
		return false;
}
function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			//alert(message);
			return false;
		}
	}
}
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
} else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("return false")
//////////////////END DISABLE RIGHT CLICK/////////////////////////////
function checkQuantity(i){
	updateTotals();
	if(i.value >= 20){
		if (confirm("Are you sure you want to order " + i.value + " items?")){
			i.form.Contact0FirstName.focus();
			return true;		
		} else {
			i.form.qty.focus();
			return false;
		}
	}
	
}
function fillForm(theForm){
	theForm.Contact0FirstName.value =  "Blue";
	theForm.Contact0LastName.value =  "Crowley";
	theForm.Contact0StreetAddress1.value =  "19 sagamore road";
	theForm.Contact0StreetAddress2.value =  "test";
	theForm.Contact0City.value =  "Beverly";
	theForm.Contact0State.value =  "MA";
	theForm.Contact0PostalCode.value =  "01244";
	theForm.Contact0Phone1.value =  "805-754-1512";
	theForm.Contact0Email.value =  "lcrowley@itvdirect.com";
	theForm.CreditCard0CardType.value = "1";
	theForm.CreditCard0ExpirationMonth.value =  "10";
	theForm.CreditCard0CardNumber.value =  "4111111111111111";
	theForm.CreditCard0ExpirationYear.value =  "2008";
	theForm.CreditCard0VerificationCode.value = '111';
}
function validCd(theForm)
{
	if(Trim(theForm.textfield.value).length == 0){
		alert("Please enter your NAME.");
		theForm.textfield.focus();
		return false;
	}
	if(Trim(theForm.textfield2.value).length == 0){
		alert("Please enter your EMAIL.");
		theForm.textfield2.focus();
		return false;
	}
	return true;
}
function validForm(theForm){
	if(Trim(theForm.CreditCard0CardNumber.value).length == 0){
		alert("Please enter your CREDIT CARD NUMBER.");
		theForm.CreditCard0CardNumber.focus();
		return false;
	}
	if (!checkCC(Trim(theForm.CreditCard0CardNumber.value))){
		alert("The CREDIT CARD NUMBER you've entered is incorrect.\nPlease check your entry for errors and try again.");
		theForm.CreditCard0CardNumber.focus();
		return false;
	}
	if(Trim(theForm.CreditCard0ExpirationMonth.value).length < 1){
		alert('Choose Your Credit Card Expiration Month.');
		theForm.CreditCard0ExpirationMonth.focus();
		return false;
	}
	if(Trim(theForm.CreditCard0ExpirationYear.value).length < 1){
		alert('Choose Your Credit Card Expiration Year.');
		theForm.CreditCard0ExpirationMonth.focus();
		return false;
	}
	if(!validateCCExpDate(theForm.CreditCard0ExpirationMonth.options[theForm.CreditCard0ExpirationMonth.selectedIndex].value, theForm.CreditCard0ExpirationYear.options[theForm.CreditCard0ExpirationYear.selectedIndex].value)){
		alert('Your credit card appears to be expired.');
		theForm.CreditCard0ExpirationMonth.focus();
		return false;
	}
	if(Trim(theForm.CreditCard0VerificationCode.value).length == 0){
		alert("Please enter your CREDIT CARD CVV NUMBER.");
		theForm.CreditCard0VerificationCode.focus();
		return false;
	}
	//validate BILLING inputs
	if(Trim(theForm.Contact0FirstName.value).length == 0){
		alert("Please enter your FIRST NAME.");
		theForm.Contact0FirstName.focus();
		return false;
	}
	if(Trim(theForm.Contact0LastName.value).length == 0){
		alert("Please enter your LAST NAME.");
		theForm.Contact0LastName.focus();
		return false;
	}
	if(Trim(theForm.Contact0StreetAddress1.value).length == 0){
		alert("Please enter your STREET ADDRESS.");
		theForm.Contact0StreetAddress1.focus();
		return false;
	}
	if(Trim(theForm.Contact0City.value).length == 0){
		alert("Please enter your CITY.");
		theForm.Contact0City.focus();
		return false;
	}
	if(Trim(theForm.Contact0State.value).length <= 1){
		alert("Please select your STATE/PROVINCE.");
		theForm.Contact0State.focus();
		return false;
	}
//	if(Trim(theForm.Contact0State.value) == 'Outside US/Canada'){
//		alert("Sorry, but we can only accept payment from accounts based in the United States and Canada at this time.");
//		theForm.Contact0State.focus();
//		return false;
//	}
	if(Trim(theForm.Contact0PostalCode.value).length == 0){
		alert("Please enter your ZIP/POSTAL CODE.");
		theForm.Contact0PostalCode.focus();
		return false;
	}
	if(Trim(theForm.Contact0Phone1.value).length < 9){
		alert("Your phone number does not have enough digits.\n  Please enter a valid phone number.");
		theForm.Contact0Phone1.focus();
		return false;
	}	
	if(Trim(theForm.Contact0Email.value).length == 0){
		alert("Please enter your BILLING EMAIL ADDRESS\nIn the Billing Address form.");
		theForm.Contact0Email.focus();
		return false;
	}
	if(!validateEmail(theForm.Contact0Email.value)){
		alert('Your EMAIL ADDRESS does not appear to be valid.\nPlease correct any errors and submit again.');
		theForm.Contact0Email.focus();
		return false;
	}
	if(theForm.CreditCard0CardType.value == 0){
		alert("Please enter your CREDIT CARD TYPE.");
		theForm.CreditCard0CardType.focus();
		return false;
	}
	//finally ... if all is well ... submit the form and move on...			
	return true;
	//return false;
}
function switchImage(imgName, imgSrc) {
	if (document.images){
		if (imgSrc != "none"){
			document.images[imgName].src = imgSrc;
		}
	}
}
function validateEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false
	if (str.indexOf(at,(lat+1))!=-1)
		return false
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false
	if (str.indexOf(dot,(lat+2))==-1)
		return false
	if (str.indexOf(" ")!=-1)
		return false
	return true					
	
}
function validateCCExpDate(month, year){
	if (month.length > 0 && year.length > 0) {
		var now = new Date();
		if (year < now.getYear())
			return false;
		else if (year == now.getYear() && month < now.getMonth())
			return false;
		else
			return true;
	} else
		return false;
}
// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload() { 
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++){
		document.imageArray[i] = new Image;	
		document.imageArray[i].src = args[i];
	}
}
function LTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...
		var j=0, i = s.length;		
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;	
		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}
function RTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}
function Trim(str){
	return RTrim(LTrim(str));
}
function isNumeric(str){
	var len= str.length;
	if (len==0)
		return false;
	//else
	var p=0;
	var ok= true;
	var ch= "";
	while (ok && p<len){
		ch= str.charAt(p);
		if (('0'<=ch && ch<='9')||ch=="."||ch=="-")
			p++;
		else
			ok= false;
	}
	return ok;
}

/*
===================================================================
formatFilter(form, format, len, name)

form 	= document.formName.elementName
format 	= Use '#' to represent numbers, any char for separators. (EG. (###)###-#### or ###-##-#### )
len		= number of DIGITS that are required (Do not count separators or grouping symbols)
name	= The 'name' of the element. This is for human eyes ... not the ELEMENT name.

Recommended usage:
<input type="text" name="PHONE_NUM" onBlur="javascript:formatFilter(this, '(###)###-####', 10, Phone Number);">
===================================================================
*/
function formatFilter(form, format, len, name) {
	var input = form.value;
	if(input.length > 0) { //do not perform if empty input
		var numbers = ""; //store all the numbers here
		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var c_char = input.charAt(i);
			if(!(isNaN(c_char) || c_char == " ")) numbers += c_char;
		}
		if (numbers.length < len) {
			alert(name+" must be "+len+" digits");
			form.focus();
		}
		var output = ""; //assign numbers here
		//assign numbers to chosen format
		var n = 0, i = 0;
		while(i < format.length && n < numbers.length) {
			var c_char = format.charAt(i);
			if(c_char == "#") {
				output += numbers.charAt(n);
				n++;
			} else {
				output += c_char;
			}
			i++;
			form.value = output; //output to form
		}
	}
}
function getElementByIdCompatible (the_id) {
	//alert("getElementByIdCompatible called");
	if (typeof the_id != 'string') {
		return the_id;
	}
	if (typeof document.getElementById != 'undefined') {
		return document.getElementById(the_id);
	} else if (typeof document.all != 'undefined') {
		return document.all[the_id];
	} else if (typeof document.layers != 'undefined') {
		return document.layers[the_id];
	} else {
		return null;
	}
}
function checkCC(s) {
	var i, n, c, r, t;
	// First, reverse the string and remove any non-numeric characters.
	r = "";
	for (i = 0; i < s.length; i++) {
		c = parseInt(s.charAt(i), 10);
		if (c >= 0 && c <= 9)
			r = c + r;
	}
	// Check for a bad string.
	if (r.length <= 1)
		return false;
	// Now run through each single digit to create a new string. Even digits
	// are multiplied by two, odd digits are left alone.
	t = "";
	for (i = 0; i < r.length; i++) {
		c = parseInt(r.charAt(i), 10);
		if (i % 2 != 0)
			c *= 2;
		t = t + c;
	}
	// Finally, add up all the single digits in this string.
	n = 0;
	for (i = 0; i < t.length; i++) {
		c = parseInt(t.charAt(i), 10);
		n = n + c;
	}
	// If the resulting sum is an even multiple of ten (but not zero), the
	// card number is good.
	if (n != 0 && n % 10 == 0)
		return true;
	else
		return false;
}
