/************************************************************************************************************
 *  *  *  *  *  *  *  *  *  *  *   I D E A V A T E   S O L U T I O N S   *  *  *  *  *  *  *  *   *  *  *  * 
************************************************************************************************************
 * Filename					: validate.js
 * Description 		  		: This file is responsible for define all validation function for clint side.
 * External Files called	: N/A
 * Global Variables	  		: N/A
 * Author                   : Mahendra Kumar Patidar
 * Modification Log
	 * 	Date             Author                  Description
	 * ---------------------------------------------------------------------------------------------------------
	 * 12-july-2008	    Mahendra Kumar Patidar	function for credit card validation.
*/
function validateEmail(obj){
		var str = obj.value;
		var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
		if(patn.test(str)){
  		  return 0;
		}else{
   		 return 1; //incorrect format
		}
	}

function isWhitespace (s){   // Is s empty?
     var reWhitespace = /^\s+$/ ;
    
     return (reWhitespace.test(s) || (s == null) || (s.length == 0));
}

function isWhitespaceinStr (s){   // Is s empty?
     var whitespace = " \t\n\r";

       var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in whitespace, return false otherwise true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) != -1) return true;
    }

    return false;
}

function isValidStr(value){
	
	var patn =  /^([a-zA-Z0-9+\s+$+%+\'+?+.+,+?+@])+$/; 
	var str = value;		
	if(patn.test(str)) {
		return 0;
	}else{
        return 1;
	}
}

function isValidVen(obj){
	
	var patn =  /^([a-zA-Z0-9+\s+#])+$/; 
	var str = obj.value;		

	if(patn.test(str)) {
		return 1;
	}else{
        return 0;
	}
}

function isFloat(obj){
	
	var patn = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
	var str = obj.value;		

	if(patn.test(str)) {
		return 1;
	}else{
        return 0;
	}
}

function isNumber(obj){
	
	var patn = /^\d+$/; 
	var str = obj.value;		

	if(patn.test(str)) {
		return 1;
	}else{
        return 0;
	}
}

function setCur(obj){
	obj.style.cursor = 'pointer'; 
}

function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion)
/*
  This function takes 5 paramaters:
  whichForm -- the NAME of the form to be validated, a string
  whichCheckBoxArray -- the NAME of the checkbox to be checked, a string
  myMax -- the most you want the user to be able to check, an integer
  myMin -- the least you want the user to be able to check, an integer
  whichQuestion -- a short description of the question, a string
  
  example use:
  howManyChecked('myform','cb_industry',6,1,'Industry');
*/
{
	var _countChecked = 0;
	var err = 0; alert(document[whichForm][whichCheckBoxArray].length);
	/* iterate through all the elements in the checkbox array */
	for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		if(document[whichForm][whichCheckBoxArray][i].checked==true)
			/* if it is, increment a counter */
			{ _countChecked++; }
	}
	/* is the count too high? */
	if(_countChecked > myMax)
		{ //alert('Limit '+myMax+' checks for the '+whichQuestion+' question.');
			err = 1;}
	/* of is the count too low */
	else if(_countChecked < myMin)
		{ //alert('You must fill out at least '+myMin+' entry(s) for the '+whichQuestion+' question.');
			err = 1;}
	if (err == 1) { return false; }
	return true;
}

function isValidLogin(obj){
	
	var patn = /^[a-zA-Z0-9]+$/; 
	var str = obj.value;
	var len = str.length;
	
	if (len <= 20){ //alert(len);
		if(len >= 4){
			
		}else{
		 return 0;
		}
	}else{
		return 0;
	}

	if(patn.test(str)) {
		
		return 1;
	}else{
        return 0;
	}
}

function isChar(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z+\s]+$/; 
	if(patn.test(str)){
	    var blen = str.length;
		if (blen <= 20 && blen >= 6) {
			return true;
		}
	}    
	return false; 
}

function isAddress(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z0-9+\s]+$/; 
	if(patn.test(str)){
	    var blen = str.length;
		if (blen <= 30 && blen >=8) {
			return true;
		}
	}    
	return false; 
}

function ValidateNMFC(obj){
	var str = obj.value; 
	var patn = /[a-zA-Z-.!@#$%^&*()_+={}]/; 
	var blen = str.length;

	if(patn.test(str)) {
		

	    return true;}

	else if (blen < 4) {
			return true;
		}
    
	else if (blen > 7) {
			return true;
		}
	else return false; 

	}


function validateLoginId(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z0-9]+$/; 
	if(patn.test(str)){
	    var blen = str.length;
		if (blen <= 20 && blen >= 4) {
			return true;
		}
	}    
	return false; 
}

function validatePassword(obj){
	var str = obj.value;
	var patn = /^[a-zA-Z0-9]+$/; 
	var patn1 = /[0-9]/; 
	var patn2 = /[a-z]/; 
	var patn3 = /[A-Z]/; 
	var blen = str.length;

	if (blen <= 10 && blen >= 6) {
		//return 1;
	}else{
		return 1;
	}
	if(!patn.test(str)){
		return 1;
	} 
	if(!patn1.test(str)){
		return 1;
	} 
	if(!patn2.test(str) && !patn3.test(str)){
		return 1;
	} 
	return 0; 
}


function checkByteLength(str,minlen,maxlen) {
	if (str == null) return false;
	var blen = str.length;
	
	if (blen <= maxlen && blen >= minlen) {
		return true;
	}
	return false;
}

function isZip(s) 
{
 
     // Check for correct zip code
     //reZip = new RegExp(/(^\d{5}$)|(^\d{5}(-|\s)*\d{4}$)/);
     reZip = new RegExp(/(^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]|\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d$)/);
     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }
 
	return true;
}
//function for canadian zipcode
function isZipCodeCan(s) 
{
 
     // Check for correct zip code
     //reZip = new RegExp(/(^\d{5}$)|(^\d{5}(-|\s)*\d{4}$)/);
	 ///(^\d{5}$)|(^\d{5}-\d{4}$)/;
     reZip = new RegExp(/(^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]|\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d$)/);
     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }
 
	return true;
}
//function for canadian zipcode

function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}

function isPhoneNumber(s) 
{
 
     // Check for correct phone number
    rePhoneNumber = new RegExp(/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/);
 
     if (!rePhoneNumber.test(s)) {
          //alert("Phone Number Must Be Entered As: (555) 555-1234");
          return true;
     }
 
return false;
}
//function for get  country and state by ajax

var http_request = false;
var currentdivid="";

function makeRequest(url,divid) {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE

		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	//http_request.responseText="";
	http_request.onreadystatechange = alertContents;
	currentdivid=divid;
	http_request.open('GET', url, true);
	http_request.send(null);

}

function alertContents() {

	if (http_request.readyState == 4) {
		
		document.getElementById(currentdivid).innerHTML=http_request.responseText;
		/*if (http_request.status == 200) {
			document.write(http_request.responseText);
		} else {
			alert('There was a problem with the request.');
		}*/
	}

}
//function for get  country and state by ajax	

function isCanadianzipCode(obj){
	
	var codeFormat = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
	
	var str = obj.value;		

	if(codeFormat.test(str)) {
		return 1;
	}else{
        return 0;
	}
}
//function for ckeck  integer only .
function isValidIntegerNumber(s) 
{
     // Check for correct phone number
    reValidIntegerNumber = new RegExp(/^ *[0-9]+ *$/ );
 
     if (!reValidIntegerNumber.test(s)) {
          return true;
     }
 
return false;
} 
//function for ckeck  integer only .	
//function for ckeck  float number only .
function isValidFloatNumber(s) 
{
     // Check for correct phone number
    reValidFloatNumber = new RegExp(/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/);
 
     if (!reValidFloatNumber.test(s)) {
          return true;
     }
 
return false;
} 

//fuction for check credit card
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};
   cards [2] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,650",
               checkdigit: true};
   cards [3] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};   
  
  /*cards [2] = {name: "DinersClub", 
               length: "14,16", 
               prefixes: "300,301,302,303,304,305,36,38,55",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [4] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [5] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,650",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "15,16", 
               prefixes: "3,1800,2131",
               checkdigit: true};
  cards [7] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [8] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334, 6767",
               checkdigit: true};
  cards [9] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [10] = {name: "Maestro", 
               length: "16,18", 
               prefixes: "5020,6",
               checkdigit: true};
  cards [11] = {name: "VisaElectron", 
               length: "16", 
               prefixes: "417500,4917,4913",
               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 isPhoneNumbertenDigit(s) 
{
 
     // Check for correct phone number
    rePhoneNumber = new RegExp(/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4})$/);
 
     if (!rePhoneNumber.test(s)) {
          //alert("Phone Number Must Be Entered As: (555) 555-1234");
          return true;
     }
 
return false;
}

//Functions Added by Pramod On [03-11-2009]

function validateContactName(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z0-9-+,.';:‘&#*)(\]\[\\’\/+\s]+$/; 
	if(patn.test(str)){
		var blen = str.length;
		if (blen <= 20 && blen >= 3) {
			return true;
		}
	}    
	return false; 
}//END OF validateContactName

//END OF FUNCTION ADD FOR [03-11-2009]

//Added On [04-11-2009]
function validateCompanyName(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z0-9-+,.';:‘&#*)(\]\[\\’\/+\s]+$/; 
	if(patn.test(str)){
		return true;		
	}    
	return false; 
}//END OF validateCompanyName

function validateCityName(obj){
	var str = obj.value; 
	var patn = /^[a-zA-Z-+,.';:‘&#*)(\]\[\\’\/+\s]+$/; 
	if(patn.test(str)){
		return true;		
	}    
	return false; 
}//END OF validateCityName

function validateAddress(myObj){
	var str = myObj.value; 
	var patn = /^[a-zA-Z0-9-+,.';:‘&#*)(\]\[\\’\/+\s]+$/; 
	if(patn.test(str)){
		return true;		
	}    
	return false; 
}//END OF validateAddress 

//END OF FUNCTION ADD FOR [04-11-2009]

//ADDED ON [31-03-2010]
function showBox(){
	$(document).ready(function(){
	tb_show("Truckload","truckload.php?keepThis=true&modal=true&TB_iframe=true&height=525&width=450",false)
	});
}
//END ADD

//ADDED ON [12-07-2010]
//FOR INTERNATIONAL QUOTING
function showBoxInternational(){
	$(document).ready(function(){
	tb_show("International Quote","international_quoting.php?keepThis=true&modal=true&TB_iframe=true&height=525&width=500",false)
	});
}
//END ADD