
/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
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);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
 
 
 /***********************************
// Function to Check for valid email. User can check multiple email ids
**********************************/
	function isEmail(pmStr, pmMultiple)
	{
		/******************************
		// pmStr = String contain email
		// pmMultiple = TURE or FALSE. Whether this email string contains multiple emails
		******************************/
		vResValidate = false;
		if (pmMultiple)
		{
			aEmailIds = pmStr.split(",")
			vResValidate = true;
			for(vLoopEmail = 0; vLoopEmail < aEmailIds.length; vLoopEmail++)
			{
				if (!isEmailOne(aEmailIds[vLoopEmail]))
				{
					vResValidate = false;
					break ;
				}
			}
		}
		else
			vResValidate = isEmailOne(pmStr)	
		
		return vResValidate;
	} //#-- close of isEmail()
	
	
	function isEmailOne(pmStr)
	{
		/******************************
		// @pmStr = String contain email
		******************************/
		//#-- trim the string
			pmStr = trim(pmStr);
		
		//#-- is regular expressions supported
			if (!isRegExpSupported()) 
				return (pmStr.indexOf(".") > 2) && (pmStr.indexOf("@") > 0);
	
		//#-- if regular expresson supported
			var vR1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
			var vR2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		
		//#-- return staus
			return (!vR1.test(pmStr) && vR2.test(pmStr));
	}
	function isRegExpSupported()
	{
		//#-- are regular expressions supported?
			if (window.RegExp)
			{
				//#-- assign expression
					var vTempStr = "a";
					var vTempReg = new RegExp(vTempStr);
				
				//#-- return status
					return (vTempReg.test(vTempStr));
			}
		
		//#-- return status
			return (false);
	} //#-- close of isRegExpSupported()

/************************************************
// Function to remove complete white spaces string
************************************************/
	function trim(pmStr)
	{
		/*****************************
		// @pmStr = String to be trimed
		*****************************/

		//#-- is regular expressions supported and return string with removed spaces
			if (isRegExpSupported()) 
				return pmStr.replace( /^ +/, "").replace( / +$/, "");
		
		//#-- check all the string for white space	
			var vI = 0, vJ = pmStr.length - 1;
			while (pmStr.charAt(vI) == ' ') vI++;
			while (pmStr.charAt(vJ) == ' ') vJ--;
			vJ++;
		
		//#-- return string with removed spaces
			return pmStr.substring(vI, vJ);
	} //#-- close of trim()

