function cardval(s) {
// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.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); }

function visaormc(s)
{
   var f = s.charAt(0);
   if (f == "4") {
      return true;
   }
   if (f == "5") {
      return true;
   }
   return false;
}

function SetBackJoinButton(theForm)
{
	$('#greyOut').hide();
    return false;
    this.submit.disabled=0;
    this.submit.src="images/join_now.gif";
}

function CheckSignupForm2(theForm)	//this function will replace back the submit image with the original
{
	var cn  = theForm.card_number.value;
   if (theForm.login.value.length < 6 || theForm.login.value.length > 20) {
      alert("Please enter a valid username (6-20 characters)");      
      return SetBackJoinButton(theForm);
   }
   if (theForm.pass.value.length < 6 || theForm.pass.value.length > 20) {
      alert("Please enter a valid password (6-20 characters)");
      return SetBackJoinButton(theForm);
   }
   
   if(theForm.repass.value!=137){
	   if (theForm.pass.value != theForm.repass.value) {
	      alert("Your password does not match");
	      return SetBackJoinButton(theForm);
	   }
	}
  
   if (!isEmailAddr(theForm.email.value)) {
	      alert("Please enter a valid e-mail address");
	      return SetBackJoinButton(theForm);
   }   
   if (theForm.first_name.value.length < 2) {      
	   alert("Please enter your first name");
      return SetBackJoinButton(theForm);
   }  
   if (theForm.last_name.value.length < 2) {
      alert("Please enter your last name");
      return SetBackJoinButton(theForm);
   }
   if (theForm.id_country.selectedIndex == 0) {
      alert("Please select your country");
      return SetBackJoinButton(theForm);
   }
   if (theForm.zip.value.length < 5) {
      alert("Please enter your zip or postal code");
      return SetBackJoinButton(theForm);
   }
   /*
   if (theForm.username.value.length < 6) {
      alert("Please enter a username of at least 6 characters");
      return false;
   }
   */
   /* if (!isEmailAddr(theForm.email.value)) {
      alert("Please enter a valid e-mail address");
      return false;
   } */
   if (!visaormc(theForm.card_number.value)) {
      alert("Please enter a valid Visa or Mastercard number");
      return SetBackJoinButton(theForm);
   }
   if (!cardval(theForm.card_number.value)) {
      alert("Please enter a valid credit card number");
      return SetBackJoinButton(theForm);
   }
   if (theForm.card_cvv.value.length < 3) {
      alert("Please enter a valid credit card CVV");
      return SetBackJoinButton(theForm);
   }
   if (!theForm.terms.checked) {
	  alert('You must agree to the terms and conditions.');
	  return SetBackJoinButton(theForm);
   }   
   //theForm.submit.disabled=true;
   //theForm.submit.value = "processing...";
   //theForm.submit.src="images/please-wait.gif";
   VirtualAgentSuspend='FormSubmit';
   return true;
}

function CheckSignupForm(theForm)
{
   if (!theForm.terms.checked) {
      alert('You must agree to the terms and conditions.');      
      return false;
   }
   if (theForm.login.value.length < 5 || theForm.login.value.length > 20) {
      alert("Please enter a valid username (5-20 characters)");
      return false;
   }
   if (!isEmailAddr(theForm.email.value)) {
      alert("Please enter a valid e-mail address");
      return false;
   }
   if (theForm.pass.value.length < 6 || theForm.pass.value.length > 20) {
      alert("Please enter a valid password (6-20 characters)");
      return false;
   }
   if(theForm.repass.value!=137){
	   if (theForm.pass.value != theForm.repass.value) {
	      alert("Your password does not match");
	      return false;
	   }
   }
   return CheckForm(theForm);
}

function CheckUpgradeForm(theForm) {
   if (!theForm.terms.checked) { 
      alert('You must agree to the terms and conditions.'); 
      return false; 
   }
   return CheckForm(theForm);
}

function CheckForm(theForm)
{
   var cn  = theForm.card_number.value;
//   if (cn.charAt(0) == 5 && cn.charAt(1) == 1) {
//      alert("sorry, we are not currently accepting Mastercard transactions");
//      return false;
//   }
   if (theForm.first_name.value.length < 2) {      alert("Please enter your first name");
      return false;
   }     if (theForm.last_name.value.length < 2) {
      alert("Please enter your last name");      return false;
   }
   /* if (theForm.address1.value.length < 2) {
      alert("Please enter your address");
      return false;
   }
   if (theForm.city.value.length < 2) {
      alert("Please enter your city");
      return false;
   }
   if (theForm.country.value.length < 2) {
      alert("Please choose a country");
      return false;
   }
   if (theForm.state.value.length < 2) {
      alert("Please choose a state");
      return false;
   }
   */
   if (theForm.zip.value.length < 2) {
      alert("Please enter your zip or postal code");
      return false;
   }
   /*if (theForm.country.selectedIndex == 0) {
      alert("Please select your country");
      return false;
   }*/
   /*
   if (theForm.username.value.length < 6) {
      alert("Please enter a username of at least 6 characters");
      return false;
   }
   */
   /* if (!isEmailAddr(theForm.email.value)) {
      alert("Please enter a valid e-mail address");
      return false;
   } */
   if (!visaormc(theForm.card_number.value)) {
      alert("Please enter a valid Visa or Mastercard number");
      return false;
   }
   if (!cardval(theForm.card_number.value)) {
      alert("Please enter a valid credit card number");
      return false;
   }
   if (theForm.card_cvv.value.length < 3) {
      alert("Please enter a valid credit card CVV");
      return false;
   }
   theForm.submit.disabled=true;
   theForm.submit.value = "processing...";
   theForm.submit.src="images/please-wait.gif";
   VirtualAgentSuspend='FormSubmit';
   return true;
}


function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
   result = true;
  }
  return result;
}

