// Trims the Input string

	function Trim(s) 
	{
	// Remove leading spaces and carriage returns
	  
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns

	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}
	return s;
	}
	
// 	function validateUSZipCode checks if it's a valid zip code.

	function validateUSZipCode( field, isCountryUS )
	{			
	/// for blank check ///
	if(Trim(field)== "") {
		return 1;
	}
	if( isCountryUS == true ) 
	{
		if( Trim(field).length < 5 )
		{
			return 2;
		}
	}
	return 3;
}

//Function ValidateMobileNumber is used to verify if the given value is a possible valid 
//mobile phone number : 
//This function first removes all non-digit characters which are allowed in phone numbers. 
//These delimiters are declared in the lines (found in the beginning of the code) :


var phoneNumberDelimiters = "()- " 
var validWorldPhoneChars = phoneNumberDelimiters + "+"
var digits = "0123456789";
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);
}

function checkUSCanadaPhone(strPhone){
	s=stripCharsInBag(strPhone,phoneNumberDelimiters);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateMobileNumber(Phone, isUSCanada){
	if(isUSCanada == true) {
		if (checkUSCanadaPhone(Phone)==false){	
			return 	1;	
		}
	} else {
		if (checkInternationalPhone(Phone)==false){
			return 2;
        }
    }
    return 3;
 }