/**
 *   DHTML phone number validation script. 
 *   Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 **/

// 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 = 6;

function isInteger(s)
{
	var i;

	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);

		if ( isNaN (c) == true )	// If current character is not number.
		{	
			return false;
		}
	}
	
	// All characters are numbers.
	return true;
}

//Strip characters "()-+" found in entered value
function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";

	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);

		if ( c == " " )			//if current character is whitespace.
		{
			continue;		//skip whitespace
		}
		else	
		{	
			if (bag.indexOf(c) == -1)		// If character is not in bag
				returnString += c;		// Append to returnString
		}
	}
	
	return returnString;
}

function checkInternationalPhone (strPhone)
{
	s = stripCharsInBag (strPhone,validWorldPhoneChars);

	// If all characters are numbers AND they are not less than 6 digits
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValPhone()
{
	var Phone = document.contact.phone;
	var msg = "The phone number you have entered is invalid. \nKindly check through it and re-enter. \nThank you.";

	if (Phone.value == "")	
	{
		return true;		//validate blank fields only during submit
	}
	else
	{
		if (checkInternationalPhone(Phone.value)==false)
		{
			alert (msg);
			Phone.focus();
			return false;
		}
		return true;
 	}
 }
