// JavaScript Document
function toggle_state_visibility(countryObj) {
      // alert(">"+countryObj.value+"<");  
	   var a = document.getElementById('australianState');
	   var o = document.getElementById('otherState');
       if(String(countryObj.value) == 'Australia'){
		 //change the field name of the selectbox to match the form post values 
		  a.name = "state";
		  o.name="otherState";
         //display the australian selectbox
		 a.style.display = 'block';
		  o.style.display = 'none';
	   } else {
		  a.name = "australianState";
		  o.name="state";
          a.style.display = 'none';
		  o.style.display = 'block';;
	   }
	}
	
function checkemail(){

		var str=document.getElementById('contactForm').email.value;
		var msgstr = "Please input a valid email address!"

	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str) && str != "username@mydomain.com"){
		testresults=true;
	}else{
		testresults=false;
	}
	return (testresults);
}

function checkEmailEntered()
{
	if (document.getElementById('emailAddy').value == '')
		return false;
	else
		return true;
}

function isValidEmail(str) 
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

function checkCreditCard (cardnumber, cardname) {
     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};

               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {
    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
	 ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}


/*function checkemailSignup(){

		var str=document.getElementById('signUp').contactEmailAddress.value;
		var msgstr = "Please input a valid email address!"

	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str) && str != "username@mydomain.com"){
		testresults=true;
	}else{
		testresults=false;
	}
	return (testresults);
}*/

function validateMembership()
{
	myForm = document.getElementById('membership');
	var msg = 'The following required fields need to be completed:\n\n';
	var ok = true;
	if (myForm.membershipLength.value=='')
	{
		msg += '- The Length of your membership\n';	
		ok = false;
	}
	if (myForm.cardName.value=='')
	{
		msg += '- The Name on your card\n';	
		ok = false;
	}
	var cardNo = myForm.cardNumber.value;
	var cardname = myForm.cardType.value;
	if (!checkCreditCard (cardNo, cardname ))
	{
		msg += '- A valid credit card number\n';	
		ok = false;	
	}
	if (myForm.cardCode.value=='')
	{
		msg += '- The 3 digit Verification Number on the back of your card\n';	
		ok = false;
	}
	if (ok)
		return true;
	else
	{
		alert(msg);
		return false;
	}
}

function validateSignUp(type)
{
	myForm = document.getElementById('signUp');
	var msg = 'The following required fields need to be completed:\n\n';
	var ok = true;
	if (type == 'signUp')
	{
		if (myForm.screenName.value=='')
		{
			msg += '- A Screen Name\n';	
			ok = false;
		}
	}
	if (myForm.contactPassword.value=='')
	{
		msg += '- A Password\n';	
		ok = false;
	}
	if (myForm.contactName1.value=='')
	{
		msg += '- A First Name\n';	
		ok = false;
	}
	if (myForm.contactName2.value=='')
	{
		msg += '- A Last Name\n';	
		ok = false;
	}
	if (myForm.Country.value == "Australia")
	{
		if (myForm.state.value == "Blank")
		{
			msg += '- Your State\n';	
			ok = false;
		}
	}
	if (myForm.Country.value == "Blank")
	{
		msg += '- Your Country\n';	
		ok = false;
	}
	/*if (myForm.postCode.value=='')
	{
		msg += '- A Postcode\n';	
		ok = false;
	}*/
	if (isValidEmail(myForm.contactEmailAddress.value) == false)
	{
		msg += '- A valid Email Address\n';	
		ok = false;	
	}
	if (!myForm.sex[0].checked && !myForm.sex[1].checked)
	{
		msg += '- Your Sex\n';	
		ok = false;
	}

	if (!myForm.agree.checked)
	{
		msg += '- You must agree with the Terms and Conditions\n';	
		ok = false;
	}
	if (myForm.idealGender.value=='')
	{
		msg += '- Your ideal parnter\n';	
		ok = false;
	}
	if (ok)
		return true;
	else
	{
		alert(msg);
		return false;
	}
}

function ValidateLogin()
{
	myForm = document.getElementById('login');
	var msg = 'The following fields need to be completed:\n';
	var ok =true;
	if (myForm.user.value == '')
	{
		msg += '-Your Username\n';
		ok =false;
	}
	if (myForm.pass.value == '')
	{
		msg += '-Your Password\n';
		ok =false;
	}
	if (ok)
		myForm.submit();
	else
	{
		alert(msg);
		return false;
	}
}

function Validate(){
	myForm = document.getElementById('contactForm');
	var msg = 'The following required fields need to be completed:\n\n';
	var ok =true;
	if(myForm.name.value==''){
		msg += '-Your Name\n';
		ok = false;
	}
	if (checkemail() == false){
		msg += '-A valid email address \n';
		ok = false;
	}
	if(myForm.query.value==''){
		msg += '-A Query\n';
		ok = false;
	}
	if (ok)
		return true;
	else
	{
		alert(msg);
		return false;
	}
}


function checkemailForm(){

		var str=document.getElementById('theOrderForm').EmailAddress.value;
		var msgstr = "Please input a valid email address!"

	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str) && str != "username@mydomain.com"){
		testresults=true;
	}else{
		alert(msgstr);
		testresults=false;
	}
	return (testresults);
} 

function orderValidate(){
	myForm = document.getElementById('theOrderForm');

	if(myForm.Total.value=='' || myForm.Total.value=='$0.00'){
		alert('Please select the number of bottles you are ordering');
		return false;
	}

	if(myForm.FirstName.value==''){
		alert('Please enter your First name');
		myForm.FirstName.focus();
		return false;
	}

	if(myForm.Surname.value==''){
		alert('Please enter your Surname');
		myForm.Surname.focus();
		return false;
	}

	if(myForm.Address.value==''){
		alert('Please enter your Address');
		myForm.Address.focus();
		return false;
	}

	if(myForm.Suburb_City.value==''){
		alert('Please enter your Suburb');
		myForm.Suburb_City.focus();
		return false;
	}

	if(myForm.Country.value==''){
		alert('Please enter your Country');
		myForm.Country.focus();
		return false;
	}
	
	if(myForm.Country.value == 'Australia'){
		if(myForm.State.value=='Select State'){
			alert('Please select your State');
			myForm.State.focus();
			return false;
		}
	}else{
		if(myForm.OtherState.value==''){
			alert('Please enter your International State');
			myForm.OtherState.focus();
			return false;
		}
	}
	
	if(myForm.Postcode.value==''){
		alert('Please enter your Postcode');
		myForm.Postcode.focus();
		return false;
	}

	if(myForm.Phone.value==''){
		alert('Please enter your Phone Number');
		myForm.Phone.focus();
		return false;
	}

	if (checkemailForm() == false){
			myForm.EmailAddress.focus();
			return false;
	}

		return true;	
}

//newsletter

function checkemail2(){

		var str=document.getElementById('newsForm').Email.value;
		var msgstr = "Please input a valid email address!"

	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str) && str != "username@mydomain.com"){
		testresults=true;
	}else{
		alert(msgstr);
		testresults=false;
	}
	return (testresults);
} 

function validate3(){
	myForm = document.getElementById('newsForm');

	if(myForm.name.value==''){
		alert('Please enter your First Name');
		myForm.name.focus();
		return false;
	}
	if(myForm.last.value==''){
		alert('Please enter Your Last Name');
		myForm.last.focus();
		return false;
	}
	if (checkemail2() == false){
			myForm.Email.focus();
			return false;
	}
	
		return true;	
}

function disable(disableIt)
{
	document.forms[0].ft.disabled = disableIt;
	document.forms[0].inches.disabled = disableIt;
}

