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
function phoneValidator(phone)
{
	if ((phone == null) || (phone == ""))
		return false;
	
	if ((phone.indexOf('"') != -1) || (phone.indexOf("'") != -1))
		return false;

	var checkOK = "0123456789";
	
	switch(phone.length)
	{
		case 8:
			if (phone.charAt(3) != '-')
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(1)) == -1)
				|| (checkOK.indexOf(phone.charAt(2)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(5)) == -1) || (checkOK.indexOf(phone.charAt(6)) == -1)
				|| (checkOK.indexOf(phone.charAt(7)) == -1))
				return false;

			return true;
		
		case 12:
			if ((phone.charAt(3) != '-') || (phone.charAt(7) != '-'))
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(1)) == -1)
				|| (checkOK.indexOf(phone.charAt(2)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(5)) == -1) || (checkOK.indexOf(phone.charAt(6)) == -1)
				|| (checkOK.indexOf(phone.charAt(8)) == -1) || (checkOK.indexOf(phone.charAt(9)) == -1)
				|| (checkOK.indexOf(phone.charAt(10)) == -1) || (checkOK.indexOf(phone.charAt(11)) == -1))
				return false;

			return true;

		case 14:
			if ((phone.charAt(1) != '-') || (phone.charAt(5) != '-') || (phone.charAt(9) != '-'))
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(2)) == -1)
				|| (checkOK.indexOf(phone.charAt(3)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(6)) == -1) || (checkOK.indexOf(phone.charAt(7)) == -1)
				|| (checkOK.indexOf(phone.charAt(8)) == -1) || (checkOK.indexOf(phone.charAt(10)) == -1)
				|| (checkOK.indexOf(phone.charAt(11)) == -1) || (checkOK.indexOf(phone.charAt(12)) == -1)
				|| (checkOK.indexOf(phone.charAt(13)) == -1))
				return false;

			return true;
		
		default:
			return false;
	}
}

//		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)
{

	if ((email == null) || (email == ""))
		return false;

	if ((email.indexOf('"') != -1) || (email.indexOf("'") != -1))
		return false;

	var atCharIndex = email.indexOf('@');
	var dotCharIndex = email.indexOf('.');

	if ((atCharIndex <= 0) || (atCharIndex == 0) || (dotCharIndex <= 2) || (atCharIndex > dotCharIndex))
		return false;
		
	if ((email.indexOf('.com') == -1) && (email.indexOf('.org') == -1) && (email.indexOf('.net') == -1))
		return false;
	
	var checkOK = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789-_.";

	for(j = 0; j < atCharIndex; j++)
	{
		if (checkOK.indexOf(email.charAt(j)) == -1)
			return false;
	}

	for(j = atCharIndex + 1; j < dotCharIndex; j++)
	{
		if (checkOK.indexOf(email.charAt(j)) == -1)
			return false;
	}

	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)
{
	if ((zip == null) || (zip == ""))
		return false;
	
	if ((zip.indexOf('"') != -1) || (zip.indexOf("'") != -1))
		return false;

	var checkOK = "0123456789";

	switch(zip.length)
	{
		case 5:
			for(j = 0; j < 5; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}
			return true;
			
		case 10:
			for(j = 0; j < 5; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}

			for(j = 6; j < 10; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}

			return true;

		default:
			return false;
	}
	
	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
	%> */

	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. Please use one of the following formats:\n\t1-234-567-8901\n\t123-456-7890\n\t123-4567");
							fieldNamesArray.elements[i].focus();
							return false;
						}
					}
					else if (fieldNameLC.indexOf('email') != -1)
					{
						if (emailValidator(fieldValue) == false)
						{
							alert("Invalid email address. Please use the 'name@domain' format, for example:\n\ttjones@rayindustries.org\n\tbillray89@myemaildomain.com\n\tthewiz@techwizards.net");
							fieldNamesArray.elements[i].focus();
							return false;
						}
					}
					else if (fieldNameLC.indexOf('zip') != -1)
					{
						if (zipValidator(fieldValue) == false)
						{
							alert("Invalid zip code. Please use the '01234' or '98765-1234' format.");
							fieldNamesArray.elements[i].focus();
							return false;
						}
					}
					else
					{
						// do nothing
					}
					j = reqFieldsAry.length;
				}
			}
		}
	}

	return true;
}
