// JavaScript Document
// Create by Saurabh Goyal
// Organization Fervent Software Solution Pvt. Ltd.
// Create Date 28-Dec-07

/* Login Form validation */
function LoginValidate(theForm)
 { 
 	 //alert("testr");
	 if (document.login.username.value == "")
      {
		alert("Please Enter Your Email!");
		document.login.username.focus();
		return (false);
	  }
  /* Email validation */
	// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = document.login.username.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
		  if (EmailAt && EmailPeriod)
			break;
		  if (j == checkEmail.length)
			break;
		}
		// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid)
	{
	alert("The \"email\" field must contain an \"@\" and a \".\".");
	document.login.username.focus();
	return (false);
	}
	
	/* End email validation */ 
	if (document.login.password.value == "")
      {
		alert("Please Enter Your Password!");
		document.login.password.focus();
		return (false);
	  }
 }

/* User Contact Validation */
function RegValidate(theForm)
 { 
	if (document.register.First_Name.value == "")
      {
		alert("Please Enter First Name!");
		document.register.First_Name.focus();
		return (false);
	  }
   if (document.register.Last_Name.value == "")
      {
		alert("Please Enter Last Name!");
		document.register.Last_Name.focus();
		return (false);
	  } 	  
  
  
/*  if (document.register.Phone_1.value == "")
      {
		alert("Please Enter Phone 1 !");
		document.register.Phone_1.focus();
		return (false);
	  } */
  /* phone validation*/
  	if ((document.register.Phone_1.value==null)||(document.register.Phone_1.value=="")){
		alert("Please Enter your Phone Number")
		document.register.Phone_1.focus()
		return false
	}
	if (checkInternationalPhone(document.register.Phone_1.value)==false){
		alert("Please Enter a Valid Phone Number")
		document.register.Phone_1.value=""
		document.register.Phone_1.focus()
		return false
	}
  /* end validation*/
  
  
  if (document.register.Address.value == "")
      {
		alert("Please Enter Address !");
		document.register.Address.focus();
		return (false);
	  }
    if (document.register.City.value == "")
      {
		alert("Please Enter City !");
		document.register.City.focus();
		return (false);
	  }	 
    if (document.register.State.value == "")
      {
		alert("Please Select State !");
		document.register.State.focus();
		return (false);
	  }
    if (document.register.Country.value == "")
      {
		alert("Please Enter Country !");
		document.register.Country.focus();
		return (false);
	  }	
    if (document.register.Zip.value == "")
      {
		alert("Please Enter Zip !");
		document.register.Zip.focus();
		return (false);
	  }	
   if (document.register.Referred_by.value == "")
      {
		alert("Please Select hear about inSHAPE? !");
		document.register.Referred_by.focus();
		return (false);
	  }		  
	  
    if (document.register.Email.value == "")
      {
		alert("Please Enter Your Email!");
		document.register.Email.focus();
		return (false);
	  }
  /* Email validation */
	// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = document.register.Email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
		  if (EmailAt && EmailPeriod)
			break;
		  if (j == checkEmail.length)
			break;
		}
		// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid)
	{
	alert("The \"email\" field must contain an \"@\" and a \".\".");
	document.register.Email.focus();
	return (false);
	}
	
	/* End email validation */  
   if (document.register.Password.value == "")
      {
		alert("Please Enter Password !");
		document.register.Password.focus();
		return (false);
	  }
  
  //repassword

	var pw1 = document.register.Password.value;
    var pw2 = document.register.RePassword.value;
	
	if (pw1 != pw2)
	{
	 alert("Please enter correct password and repassword");
	 document.register.RePassword.focus();
    return false;
	}
  //End repassword 
 }

function shipping(theForm)
 { 

	if (isNaN(document.shopping_cart.qty.value))
      {
		alert("Please Vaild Qty!");
		document.shopping_cart.qty.focus();
		return (false);
	  }
 }
/* Phone number validation */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
//var phoneNumberDelimiters = "()- ";
var phoneNumberDelimiters = "";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


