// JavaScript Document
function form_contact_OnSubmit(form) {
	var error_msg = "";
	for (i = 0; i < form.length; i++) 
	{
		var forminfo = form[i].value.indexOf(".");
		if (form[i].getAttribute("id") == "required") 
		{
			if (form[i].getAttribute("type") == "text") 
			{
				if (form[i].value == "") 
				{
					error_msg = error_msg + form[i].getAttribute("name") + "\n";
					form[i].style.cssText = "background-color:#a20d23; color:#ffffff;";
				}
				else 
				{
					form[i].style.cssText = "";
				}
				if (form[i].getAttribute("name") == "email") 
				{
					if (!isValidEmail(form[i].value)) 
					{
						error_msg = error_msg + "Please enter a valid Email Address\n";
						form[i].style.cssText = "background-color:#a20d23; color:#ffffff;";
					}
					else 
					{
						form[i].style.cssText = "";
					}
				}
				if (form[i].getAttribute("name") == "web_pages") 
				{
					if (isValidString(form[i].value) == true) 
					{
						error_msg = error_msg + "Please enter a valid number for Web Pages\n";
						form[i].style.cssText = "background-color:#a20d23; color:#ffffff;";
					}
					else
					{
						form[i].style.cssText = "";
					}
				}
			}
		}
	}
	var chkbox = false;
	if (document.getElementById("form_contact")) 
	{
		var infoLength = form.information.length + 1;
		for (i = 0; i < form.information.length; i++) 
		{
			if (form.information[i].checked == true) 
			{
				chkbox = true;
				form.information_value.value = form.information_value.value + form.information[i].value + ", ";
			}
		}
		if (chkbox != true) 
		{
			error_msg = error_msg + "Please make sure at least one checkbox is checked\n";
			document.getElementById("information_section").style.cssText = "background-color:#a20d23; color:#ffffff;";
		}
		else 
		{
			document.getElementById("information_section").style.cssText = "";
		}
	}
	if (error_msg != "") 
	{
		error_intro_msg = "Please make sure the following fields are filled out correctly:\n\n";
		alert(error_intro_msg + error_msg);
		
		return false;
	}
	else 
	{
		
		return true;
	}
}

/*
Validate if a value is a string True or False
*/
function isValidString(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 true;
    }
    // All characters are numbers.
    return false;

}


/*
Copyright 2005, 4word systems
All rights reserved.

This software may not be reproduced or distributed in any form without the express 
written consent of 4word systems or it's designee.

Revision 1.1:  20050729 Added underscore to list of valid characters
*/

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

/**
 * Retrieve the value of an element within the supplied form.
 * 
 *  @param {Object} form The form object.
 *  @param {String} elementName The name of the element
 *  
 *  @return {String} The value of the element or empty.
 */
function getElementValue(form, elementName)
{
	var elementValue = "";
	try
	{
		var elementLength = form[elementName].length;
		if(elementLength != "undefined" && elementLength > 1 && form[elementName].type != "select-one")
		{
			try
			{
				for(var i = 0; i < elementLength; i++)
				{
					if (form[elementName][i].checked) 
					{
						elementValue += Form.Element.getValue(form[elementName][i]) + ", ";
					}
				}
				
				/**
				 * Trim trailing comma and space
				 */
				if(elementValue.length > 0)
				{
					elementValue = elementValue.substring(0, elementValue.length - 2);
				}
			}
			catch(e)
			{
				elementValue = Form.Element.getValue(form[elementName]);
			}
		}
		else
		{
			elementValue = Form.Element.getValue(form[elementName]);
		}
	}
	catch(e)
	{
		alert("An error in x occurred: " + elementName + " " + e.message)
	}
	return elementValue;
}

/**
 * Retrieve the first occurence of an element with the supplied name.
 * 
 *  @param {String} elementName The name of the element
 *  
 *  @return {Object} A reference to the element or null.	
 */
function getElementByName(elementName)
{
	var element = null;
	
	var elements = document.getElementsByName(elementName);
	if(elements.length > 0)
	{
		element = elements[0];
	}
	
	return element;
}