function showhidestate(val) {
  if(val=="NA") {
    document.getElementById('billstate2_label').style.display='inline';
    document.getElementById('billstate2_field').style.display='inline';
    document.getElementById('billcountry').value='';
  } else {
    document.getElementById('billstate2_label').style.display='none';
    document.getElementById('billstate2_field').style.display='none';
    document.getElementById('billstate2').value='';
  }
}
function showhidestate2(val) {
  if(val=="NA") {
    document.getElementById('shipstate2_label').style.display='inline';
    document.getElementById('shipstate2_field').style.display='inline';
    document.getElementById('shipcountry').value='';
  } else {
    document.getElementById('shipstate2_label').style.display='none';
    document.getElementById('shipstate2_field').style.display='none';
    document.getElementById('shipstate2').value='';
  }
}


  	function CheckOrderForm(form) {

  		if (form.billfirstname.value == "") {
  			alert('Please provide your first name');
  			form.billfirstname.focus();
  			return false;
  		}
      if (form.billlastname.value == "") {
  			alert('Please provide your last name');
  			form.billlastname.focus();
  			return false;
  		}
      if (form.billaddress1.value == "") {
  			alert('Please provide your billing address');
  			form.billaddress1.focus();
  			return false;
  		}
      if (form.billcity.value == "") {
  			alert('Please provide your billing city');
  			form.billcity.focus();
  			return false;
  		}
      if (form.billstate.value == "") {
  			alert('Please provide your billing state');
  			form.billstate.focus();
  			return false;
  		}
      if (form.billcode.value == "") {
  			alert('Please provide your billing post/zip code');
  			form.billcode.focus();
  			return false;
  		}
      if (form.billdayphone.value == "") {
  			alert('Please provide your daytime contact phone number');
  			form.billdayphone.focus();
  			return false;
  		}
  		if (form.billemail.value == "") {
  			alert('Please provide your email address');
  			form.billemail.focus();
  			return false;
  		}
  		if (!(checkValidation(form.billemail))) {
  			alert('Please provide a valid email address');
        form.billemail.focus();
  			return false;
  		}
  		return true;
  	}

  	function CheckOrderForm2(form) {
      if (form.cardname.value == "") {
  			alert('Please provide your the name on the credit card');
  			form.cardname.focus();
  			return false;
  		}
  		if (form.cardexpiry.value == "") {
  			alert('Please provide the credit card expiry date (mm/yy)');
  			form.cardexpiry.focus();
  			return false;
  		}
  		if (!(verify_ccard(form.cardnumber.value, form.cardtype.value)==0)) {
  			alert('Please check the credit card details you have provided');
  			form.cardnumber.focus();
  			return false;
  		}
      if(form.tac1.checked==false) {
        alert('Please indicate that you have read the terms and conditions by ticking the box');
  			form.tac1.focus();
  			return false;
  		}
      return true;
  	}


	function verify_ccard(inNumber, type) {
		// returns 0 if valid, positive number if invalid.

		//alert(inNumber);
		//alert(type);

        total = 1*0;
        tmp = 1*0;

        number = "";

        // make sure there are only numbers in the string...
        for(i = 0; i < inNumber.length; i++)
        {
		    if(inNumber.charAt(i) >= "0" && inNumber.charAt(i) <= "9")
                {
                        number = number + inNumber.charAt(i);
                }
        }

        if(number.length < 13) return 10; // too short for anything

        first = "" + number.charAt(0);
        second = "" + number.charAt(1);
        third = "" + number.charAt(2);
        firstTwo = first + second;
        firstFour = firstTwo + third + number.charAt(3);

        if (type == "MC")
        {
                if(first != "5" || second < "1" || second > "5")
                        return 11;// invalid Mastercard prefix
                if(number.length != 16)
                        return 21;
        }
        else if(type == "VISA")
        {
                if(first != "4")
                        return 12;// invalid Visa prefix
                if(number.length != 13 && number.length != 16)
                        return 22;
        }
        else if(type == "AMEX")
        {
                if(first != "3" || (second != "4" && second != "7"))
                        return 13;// invalid American Express Prefix
                if(number.length != 15)
                        return 23;
        }
        else if(type == "DISC")
        {
                        if(firstFour != "6011")
                                return 14;// invalid prefix.
                        if(number.length != 16)
                                return 24;
        }
        else if(type == "DCCB")
        {
                if(firstTwo != "36"
                        && firstTwo != "38"
                        && (firstTwo != "30" ||
                                (third < "0" || third > "5")))
                {
                        return 15;
                }
                if(number.length != 14)
                        return 25;
        }
        else if(type == "enRoute")
        {
                if(firstFour != "2014"
                        && firstFour != "2149")
                        return 16;// invalid enRoute card
                if(number.length != 15)
                        return 26;
		return 0; // no check sum calculation needed
        }
        else if(type == "JCB")
        {
                if(firstFour != "2131"
                        && firstFour != "1800"
                        && (first != "3") )
                        return 17;
                if(number.length != 16 && first =="3")
                        return 27;
                if(number.length != 15 && first != "3")
                        return 28;
        }

        // now check the credit card suffix and length vs. the type


         // do the check sum
        for(loc = number.length - 2; loc >= 0; loc -= 2)
        {
                total += 1 * number.charAt(loc +1);
                tmp = number.charAt(loc) * 2;
		if(tmp > 9) total += 1;
		total += tmp%10;
        }

		if(number.length % 2 > 0)
		total += 1 * number.charAt(0);


        return (total % 10);
	}

	function confirmCard(CardNumber, CardType) {
		//alert(CardType);
        errorStrings = new Array(
                "Hee Hee.  No error here!", //0
                "Check sum failed", //1
                "Check sum failed", //2
                "Check sum failed", //3
                "Check sum failed", //4
                "Check sum failed", //5
                "Check sum failed", //6
                "Check sum failed", //7
                "Check sum failed", //8
                "Check sum failed", //9
                "Number of digits must be greater than 12", //10
                "Invalid Master Card Prefix", //11
                "Invalid Visa Prefix", //12
                "Invalid American Express Prefix", //13
                "Invalid Discover Prefix", //14
                "Invalid Diner's Club/Carte Blanche Prefix", //15
                "Invalid enRoute Prefix", //16
                "Invalid JCB Prefix", //17
                "Why are you reading my source?", //18
                "Are you just a glutton for punishment?", //19
                "Insert comment here.", //20
                "Number of digits must be 16 for Master Card", //21
                "Number of digits must be 13 or 16 for Visa", //22
                "Number of digits must be 15 for American Express", //23
                "Number of digits must be 16 for Discover ", //24
                "Number of digits must be 14 for Diner's Club/Carte Blanche", //25
                "Number of digits must be 15 for enRoute", //26
                "Number of digits must be 16 for JCB with this prefix", //27
                "Number of digits must be 15 for JCB with this prefix") //28

        if ((reason = verify_ccard(CardNumber, CardType)) == 0) {
			//alert('succeeded!');
			return 0;
			}
        else {
			//alert(reason);
            return 1;
			}
	}

  function checkValidation ( addressField ) {
      if ( stringEmpty ( addressField.value ) )
          alert ( "Error! There is no E-Mail address entered" );
      else if ( noAtSign ( addressField.value ) )
          alert ( "Error! The E-Mail address does not contain an '@' character" );
      else if ( nothingBeforeAt ( addressField.value ) )
          alert ( "Error! An E-Mail address must contain at least one character before the '@' character" );
      else if ( noLeftBracket ( addressField.value ) )
          alert ( "Error! The E-Mail address contains a right square bracket ']',\nbut no corresponding left square bracket '['" );
      else if ( noRightBracket ( addressField.value ) )
          alert ( "Error! The E-Mail address contains a left square bracket '[',\nbut no corresponding right square bracket ']'" );
      else if ( noValidPeriod ( addressField.value ) )
          alert ( "Error! An E-Mail address must contain a period ('.') character" );
      else if ( noValidSuffix ( addressField.value ) )
          alert ( "Error! An E-Mail address must contain a two or three character suffix" );
      else
          return (true);

      return ( false );
  }

  function linkCheckValidation ( formField ) {
      if ( checkValidation ( formField ) == true ) {
          alert ( 'E-Mail Address Validates OK' );
      }

      return ( false );
  }

  function stringEmpty ( address ) {
      // CHECK THAT THE STRING IS NOT EMPTY
      if ( address.length < 1 ) {
          return ( true );
      } else {
          return ( false );
      }
  }

  function noAtSign ( address ) {
      // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
      if ( address.indexOf ( '@', 0 ) == -1 ) {
          return ( true )
      } else {
          return ( false );
      }
  }

  function nothingBeforeAt ( address ) {
      // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
      if ( address.indexOf ( '@', 0 ) < 1 ) {
          return ( true )
      } else {
          return ( false );
      }
  }

  function noLeftBracket ( address ) {
      // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
      if ( address.indexOf ( '[', 0 ) == -1 && address.charAt ( address.length - 1 ) == ']' ) {
          return ( true )
      } else {
          return ( false );
      }
  }

  function noRightBracket ( address ) {
      // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
      if ( address.indexOf ( '[', 0 ) > -1 && address.charAt ( address.length - 1 ) != ']' ) {
          return ( true );
      } else {
          return ( false );
      }
  }

  function noValidPeriod ( address ) {
      // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
      if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
          return ( false );

      // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
      if ( address.indexOf ( '.', 0 ) == -1 )
          return ( true );

      return ( false );
  }

  function noValidSuffix ( address ) {
      // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
      if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
          return ( false );

      // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
      var len = address.length;
      var pos = address.lastIndexOf ( '.', len - 1 ) + 1;
      if ( ( len - pos ) < 2 || ( len - pos ) > 3 ) {
          return ( true );
      } else {
          return ( false );
      }
  }
  
