// 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;
var testresults;
var namemask = "* Your Name:";
var phonemask = "* Your Phone:";
var emailmask = "* Your E-mail:";

function targetopener(mylink, closeme, closeonly)
{
	if (! (window.focus && window.opener))return true;
		window.opener.focus();
	if (! closeonly)window.opener.location.href=mylink.href;
	if (closeme)window.close();
	return false;
}

function stripMask(docValue)
{
	var strOut = "";
	var strArr = new Array(2);

	if(docValue != null)
	{
		strArr =  docValue.split(':');
	
		if (strArr[1] != null)
			strOut=strArr[1];
		else
			strOut=strArr[0];	
	};

	return(strOut);
}

function isInvalidEmail(docValue)
{
	var str=docValue
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(str))
		testresults=false;
	else
		testresults=true;
		
	return (testresults)
}
			
function validate()
{			
	var docValue = "";

	docValue = stripMask(document.emailform.yourname.value);
	
		if(docValue.length == 0)
		{
			alert("Please enter your name");
			document.emailform.yourname.value=namemask;
			document.emailform.yourname.focus();
			return false;
		}

	docValue = stripMask(document.emailform.yourphone.value);
			
		if(docValue.length == 0)
		{
			alert("Please enter your contact phone");
			document.emailform.yourphone.value=phonemask;
			//document.emailform.yourphone.focus();
			return false;
		}
		else if (checkInternationalPhone(docValue)==false)
		{
			alert("Please enter a valid phone number: (###)###-####");
			document.emailform.yourphone.value=phonemask;
			//document.emailform.yourphone.focus();
			return false;
		}

	docValue = stripMask(document.emailform.youremail.value);

		if(docValue.length == 0)
		{
			alert("Please enter your e-mail address");
			document.emailform.youremail.value=emailmask;
			//document.emailform.youremail.focus();
			return false;
		}
		else if(isInvalidEmail(docValue))
		{
			alert("Please enter a valid e-mail address: name@domainname");
			document.emailform.youremail.value=emailmask;
			//document.emailform.youremail.focus();
			return false;
		}
		
		return true;
}
	
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);
}
