//////////////////END DISABLE RIGHT CLICK/////////////////////////////
function clickIE4(){
if (event.button==2){
//alert(message);
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){
			if(i.value >= 20){
				
				if (confirm("Are you sure you want to order " + i.value + " items?")){
					i.form.billing_fname.focus();
					return true;		
				}
				else {
					i.form.qty.focus();
					return false;
				}
			}
	}
	
	
	function fillForm(theForm){
		theForm.billing_fname.value =  "Blue";
		theForm.billing_lname.value =  "Crowley";
		theForm.billing_add1.value =  "19 sagamore road";
		theForm.billing_add2.value =  "test";
		theForm.billing_city.value =  "Beverly";
		theForm.billing_state.value =  "MA";
		theForm.billing_zip.value =  "01244";
		theForm.billing_phone.value =  "805-754-1512";
		theForm.billing_email.value =  "lcrowley@itvdirect.com";
		
		theForm.cc_type.value = "1";
		theForm.billing_expmo.value =  "10";
		theForm.billing_ccnum.value =  "4111111111111111";
		theForm.billing_expyr.value =  "2008";
		theForm.billing_cvv.value = '111';
		}




	function validForm(theForm){		
			//validate BILLING inputs
	
				if(Trim(theForm.billing_fname.value).length == 0){
					alert("Please enter your FIRST NAME.");
					theForm.billing_fname.focus();
					return false;
				}
				if(Trim(theForm.billing_lname.value).length == 0){
					alert("Please enter your LAST NAME.");
					theForm.billing_lname.focus();
					return false;
				}
				if(Trim(theForm.billing_add1.value).length == 0){
					alert("Please enter your STREET ADDRESS.");
					theForm.billing_add1.focus();
					return false;
				}
				if(Trim(theForm.billing_city.value).length == 0){
					alert("Please enter your CITY.");
					theForm.billing_city.focus();
					return false;
				}
				if(Trim(theForm.billing_state.value).length <= 1){
					alert("Please select your STATE/PROVINCE.");
					theForm.billing_state.focus();
					return false;
				}
				if(Trim(theForm.billing_state.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.billing_state.focus();
					return false;
				}
				if(Trim(theForm.billing_zip.value).length == 0){
					alert("Please enter your ZIP/POSTAL CODE.");
					theForm.billing_zip.focus();
					return false;
				}
		
				if(Trim(theForm.billing_phone.value).length < 9){
					alert("Your phone number does not have enough digits.\n  Please enter a valid phone number.");
					theForm.billing_phone.focus();
					return false;
				}

				if(Trim(theForm.billing_email.value).length == 0){
					alert("Please enter your BILLING EMAIL ADDRESS\nIn the Billing Address form.");
					theForm.billing_email.focus();
					return false;
				}
				
				if(!validateEmail(theForm.billing_email.value)){
					alert('Your EMAIL ADDRESS does not appear to be valid.\nPlease correct any errors and submit again.');
					theForm.billing_email.focus();
					return false;
				}
				
			
				if(theForm.cc_type.value == 0){
					alert("Please enter your CREDIT CARD TYPE.");
					theForm.cc_type.focus();
					return false;
				}
				if(Trim(theForm.billing_ccnum.value).length == 0){
					alert("Please enter your CREDIT CARD NUMBER.");
					theForm.billing_ccnum.focus();
					return false;
				}
				if(!luhnCheckCC( Trim(theForm.billing_ccnum.value) ) ){
					alert("The CREDIT CARD NUMBER you've entered is incorrect.\nPlease check your entry for errors and try again.");
					theForm.billing_ccnum.focus();
					return false;
				}
							
				if(!validateCCExpDate(theForm.billing_expmo.value, theForm.billing_expyr.value)){
					alert('Your credit card appears to be expired.');
					theForm.billing_expmo.focus();
					return false;
				}
				
				if(Trim(theForm.billing_cvv.value).length == 0){
					alert("Please enter your CREDIT CARD CVV NUMBER.");
					theForm.billing_cvv.focus();
					return false;
				}
			
			
									
				//finally ... if all is well ... submit the form and move on...			
				return true;
			}












function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}
// 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];
  }
}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   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;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

 function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  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++)
			} else {
				output += c_char;
			}
			i++;
		}

		form.value = output; //output to form
	}
}

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;
}