// JScript source code

var strErrors = "";


function properName(fieldVal)
{
	var strInvalidChars = "0123456789+*!@#$%^&()={}<>?|";
	var i=0; 
	var strError="";
	while(i < fieldVal.length)
	{
		if (strInvalidChars.indexOf(fieldVal.charAt(i)) != -1)
		{
			strError += fieldVal.charAt(i);
		}
		i++;
	}
	return strError;
}

function isNumeric(fieldval)
{
		var strValidChars = "0123456789.+-";
		for (i = 0; i < fieldval.length; i++)
		{
			if (strValidChars.indexOf(fieldval.charAt(i)) == -1)
			{
				return false;
			}
	    }
		return true;
}

function fieldLength(fieldvalue)
{
	if (fieldvalue.valueOf() == 0)
		return false;
	else
		return true;
}	// end of function check

function validate()
{
	var fisrt_name = document.agent_form.fName;
	var last_name = document.agent_form.lName;
	var telNo = document.agent_form.telNo;
	var emailField = document.agent_form.email;
	var state = document.agent_form.StateProv;
	var city = document.agent_form.city;
	var zip = document.agent_form.ZipPC;
	var errorChecking = "";
	var fNm = true;
	var lNm = true;
	var tel = true;
	strErrors = "";

	if(fieldLength(zip.value) == 0)
	{
		strErrors = "Zip Code field cannot be empty\n";
		fNm = false;
		zip.focus();
	}
	if(fieldLength(city.value) == 0)
	{
		strErrors = "City field cannot be empty\n";
		fNm = false;
		city.focus();
	}
	if(fieldLength(fisrt_name.value) == 0)
	{
		strErrors = "First Name field cannot be empty\n";
		fNm = false;
		fisrt_name.focus();
	}
	if(fieldLength(last_name.value) == 0)
	{
		strErrors += "Last Name field cannot be empty\n";
		lNm = false;
		last_name.focus();
	}
	if(fieldLength(telNo.value) == 0)
	{
		strErrors += "Telephone field cannot be empty";
		tel = false;
		telNo.focus();
	}
	
	if(fNm == true)
	{
		if((errorChecking = properName(fisrt_name.value)) != "")
		{
			strErrors += "\nFirst Name can not contain " + errorChecking;
			errorChecking = "";
			fisrt_name.focus();
			fisrt_name.select();
		}
	}
	if(lNm == true)
	{
		if((errorChecking = properName(last_name.value)) != "")
		{
			strErrors += "\nLast Name can not contain " + errorChecking;
			errorChecking = "";
			last_name.focus();
			last_name.select();
		}
	}
	/*if(tel == true)
	{
		if((errorChecking = ValidatePhone(telNo.value)) != "")
		{
			strErrors += errorChecking;
			errorChecking = "";
			telNo.focus();
		}
	}
	
	if(tel == true)
	{
		var reg1  = /^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}|(\d{3}-\d{3}-\d{4}(\s(x)\d{1,4})?)$/;
		if (!reg1.test(telNo.value))
		{
			strErrors += "\nPlease enter valid phone number\n";
			strErrors += "Example (123)456-7890 | (123)456-8970 x12 | (910)456-8970 1211 | 416-429-3000 x1234\n\n";
			telNo.focus();
			telNo.select();
		}
	}*/
	
	if(fieldLength(emailField.value) == 0 )
	{
		//var reg1  = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		var reg1  = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		//alert("Regular expression is fine");
		if (reg1.test(emailField.value))
		{
			//alert("Successful");
		}
		else
		{
			//alert("Unsuccessful");
			strErrors += "\nPlease enter valid email address";
			emailField.focus();
			emailField.select();
		}
	}
	
	if(state.value == "0")
	{
		strErrors += "\nPlease select a state or province";
		state.focus();
	}
	
	
	if(strErrors != "")
	{
		alert(strErrors);
		return false;
	}
	return true;
}

// 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 ValidatePhone(fieldVal)
{
	var strError = "";
	if (checkInternationalPhone(fieldVal)==false)
	{
		strError = "\nPlease enter a valid phone number. Example 4164293000";
	}
	return strError;
 }

