//		phoneValidator:	Input: string 'phone' containing the phone number to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the phone number string, true otherwise

// 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 = 8;

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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function phoneValidator(phone)
{
	if ((phone == null) || (phone == ""))
		return false;
	
	if ((phone.indexOf('"') != -1) || (phone.indexOf("'") != -1))
		return false;

	return checkInternationalPhone(phone);
}

//		emailValidator:	Input: string 'email' containing the email address to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the email address string, true otherwise
function emailValidator(email)
{

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	if(reg.test(email) == false) 
	{
      	return false;
   	}
	else
	{
		return true;
	}
}

//		zipValidator:	Input: string 'zip' containing the zip code to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the zip code string, true otherwise
function zipValidator(zip)
{
	return true;
}

//		modForm:	Input: array of field names from the form
//					Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the appropriate string variable, true otherwise. It will also
//		create an alert with a message containing the error, if there is one
function modForm (fieldNamesArray)
{

//	<% fieldary = array("chrName","txtDescription","chrPhone","chrEmail","chrZip","boolDisabled") %>
	var fieldName, fieldValue, reqFields, reqFieldsAry;

/*	Use ASP to write out the javascript array
	<%
	jscriptarraystring = "var jFieldArray = new Array("
	jscriptarraystring = jscriptarraystring & "'" & fieldary(0) & "'"
	for i=1 to	ubound(fieldary)
		jscriptarraystring = jscriptarraystring & ", " & "'" & fieldary(i) & "'"
	next

	jscriptarraystring = jscriptarraystring & ");"
	Response.write jscriptarraystring
	%> */
	if (fieldNamesArray._requiredFields)
	{
		reqFields = fieldNamesArray._requiredFields.value
		reqFieldsAry = reqFields.split(",")
	
		for(i = 0; i < fieldNamesArray.elements.length; i++)
		{
			if (fieldNamesArray.elements[i].type == 'text')
			{
				fieldName = fieldNamesArray.elements[i].name;
				fieldValue = fieldNamesArray.elements[i].value;
		
				var fieldNameLC = fieldName.toLowerCase();
				
				for(j = 0; j < reqFieldsAry.length; j++)
				{
					if (reqFieldsAry[j].toLowerCase() == fieldNameLC)
					{
						fieldNameString = new String(fieldName);
		   				fieldNameString = fieldNameString.replace(/_/g, " ");
						fieldNameString = fieldNameString.toUpperCase();
						if ((fieldValue == null) || (fieldValue == ""))
						{
							alert("Please enter a value for the " + fieldNameString + " field.");
							fieldNamesArray.elements[i].focus();
							return false;
						}
						
						if ((fieldValue.indexOf('"') != -1) || (fieldValue.indexOf("'") != -1))
						{
							alert("Please remove any single or double quotes from the " + fieldNameString + " field.");
							fieldNamesArray.elements[i].focus();
							return false;
						}
			
						if (fieldNameLC.indexOf('phone') != -1)
						{
							if (phoneValidator(fieldValue) == false)
							{
								alert("Invalid phone number.");
								fieldNamesArray.elements[i].focus();
								return false;
							}
						}
						else if (fieldNameLC.indexOf('email') != -1)
						{
							if (emailValidator(fieldValue) == false)
							{
								alert("Invalid email address.");
								fieldNamesArray.elements[i].focus();
								return false;
							}
						}
						else if (fieldNameLC.indexOf('zip') != -1)
						{
							if (zipValidator(fieldValue) == false)
							{
								alert("Invalid zip code.");
								fieldNamesArray.elements[i].focus();
								return false;
							}
						}
						else
						{
							// do nothing
						}
						j = reqFieldsAry.length;
					}
				}
			}
		}
	}
	return true;
}

