function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}

function clearDefault(el) {
	if (el.defaultValue == el.value) el.value = ""
}
function replaceDefault(el) {
	if ( el.value == "" ) el.value = el.defaultValue
}

//		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;
}

