//		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)
{
	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)
{
	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;
				}
			}
		} 
		else if (fieldNamesArray.elements[i].type == 'select-one')
		{
			fieldName = fieldNamesArray.elements[i].name;
			fieldValue = fieldNamesArray.elements[i].options[fieldNamesArray.elements[i].selectedIndex].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;
					}	
					else
					{
						// do nothing
					}
					j = reqFieldsAry.length;
				}
			}						
		}
	}

	return true;
}

function updatestars($num) {
        var staronsrc = "images/star-1.gif";
        
      switch($num)
      {
        case 1:
            document.getElementById("1star").src = staronsrc;
            break;
        case 2:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            break;
        case 3:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            break;
        case 4:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            document.getElementById("4star").src = staronsrc;
            break;
        case 5:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            document.getElementById("4star").src = staronsrc;
            document.getElementById("5star").src = staronsrc;
            break;
      }
   }
   function clickstars($num)
   {
    var staronsrc = "images/star-1.gif";
        spotfield = document.getElementById("faqrating");
      switch($num)
      {
        case 1:
            document.getElementById("1star").src = staronsrc;
            if(spotfield.value == "1")
            {
                spotfield.value = "";
            }
            else{
                spotfield.value = "1";
            }
            break;
        case 2:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            if(spotfield.value == "2")
            {
                spotfield.value = "";
            }
            else{
                spotfield.value = "2";
            }
            break;
        case 3:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            if(spotfield.value == "3")
            {
                spotfield.value = "";
            }
            else{
                spotfield.value = "3";
            }
            break;
        case 4:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            document.getElementById("4star").src = staronsrc;
            if(spotfield.value == "4")
            {
                spotfield.value = "";
            }
            else{
                spotfield.value = "4";
            }
            break;
        case 5:
            document.getElementById("1star").src = staronsrc;
            document.getElementById("2star").src = staronsrc;
            document.getElementById("3star").src = staronsrc;
            document.getElementById("4star").src = staronsrc;
            document.getElementById("5star").src = staronsrc;
            if(spotfield.value == "5")
            {
                spotfield.value = "";
            }
            else{
                spotfield.value = "5";
            }
            break;
      }
   }
   function clearstars()
   {
        var staroffsrc = "images/star-0grey.gif";
        var staronsrc = "images/star-1.gif";
        var currvalue = parseInt(document.getElementById("faqrating").value);
        if(isNaN(currvalue)){ currvalue = -1; }
        //alert(currvalue);
        switch(currvalue)
        {
            case -1:
                //alert("in case -1");
                document.getElementById("1star").src = staroffsrc;
                document.getElementById("2star").src = staroffsrc;
                document.getElementById("3star").src = staroffsrc;
                document.getElementById("4star").src = staroffsrc;
                document.getElementById("5star").src = staroffsrc;
                break;
            case 1:
                //alert("in case 1");
                document.getElementById("1star").src = staronsrc;
                document.getElementById("2star").src = staroffsrc;
                document.getElementById("3star").src = staroffsrc;
                document.getElementById("4star").src = staroffsrc;
                document.getElementById("5star").src = staroffsrc;
                break;
            case 2:
                //alert("in case 2");
                document.getElementById("3star").src = staroffsrc;
                document.getElementById("4star").src = staroffsrc;
                document.getElementById("5star").src = staroffsrc;
                break;
            case 3:
                //alert("in case 3");
                document.getElementById("4star").src = staroffsrc;
                document.getElementById("5star").src = staroffsrc;
                break;
            case 4:
                //alert("in case 4");
                document.getElementById("5star").src = staroffsrc;
                break;
            case 5:
                break;
        }
        
   }