function validate() {
	var bgBad = "red";
  	var bgGood = "black";
  	
  	//Loop through all form elements to validate required fields
	for(var i = 0; i < mainform.elements.length; i++) {
        	var e = mainform.elements[i];
        	//alert (e.name + e.alt);
        	if(e.alt != "optional")
        	{
        		if (e.type == "select-one")
	        	{
	        		if (e.value == 0)
	        			changeColor(e.name+'Label',bgBad);
	        		else
	        			changeColor(e.name+'Label',bgGood);
	        	}
	        	else
	        	if (e.type == "textarea")
	        	{
		        	var labelName = e.name + 'Label';	        		
		        	if (e.value  == "")
		        	{
					changeColor(labelName,bgBad);
					event.returnValue=false;
				}
				else
					changeColor(labelName,bgGood);	        		
	        	}
	        	else
	        	if (e.type == "text" && e.type != "hidden"){
		        	var labelName = e.name + 'Label';        	
	        		if( e.name == "areacode" || e.name == "citycode" || e.name == "lastfour")
	        			labelName = "phoneLabel";
		        	if (e.value  == "")
		        	{
					changeColor(labelName,bgBad);
					event.returnValue=false;
				}
				else
					changeColor(labelName,bgGood);
			}
		}
	}
	//Check email format
	mNv=mainform.email.value;
	if (isEmail(mNv)==0) {
		changeColor('emailLabel',bgBad);
		event.returnValue=false;
	}
	else
		changeColor('nameLabel',bgGood);
		
	if(event.returnValue == false) {
		alert("Please correct the fields colored red and submit the form again.");
	}
	else{
		document.mainform.phone.value = document.mainform.areacode.value + document.mainform.citycode.value + document.mainform.lastfour.value;
	}
	
	
}
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
if(isNN)
  document.captureEvents(Event.KEYPRESS);
function autoTab(input,len, e){
  var keyCode = (isNN)?e.which:e.keyCode; 
  var filter = (isNN)?[0,8,9]:[0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode) ){
    input.value = input.value.slice(0,len);
    input.form[(getIndex(input)+1)%input.form.length].focus();
  }
  function containsElement(arr, ele){
    var found = false, index = 0;
    while(!found && index < arr.length)
      if(arr[index]==ele)
        found = true;
      else
        index++;
    return found;
  }
  function getIndex(input){
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index==-1)
      if (input.form[i] == input)index = i;
      else i++;
    return index;
  }
  return true;
}
function changeColor(id,color) {
       if (document.getElementById) {
       	//alert(id);
       		document.getElementById(id).style.color = color;
       	}
        else if (document.layers)    document[id].color = color;
        else if (document.all)       document.all[id].style.color = color;
}

function CheckPhoneNumber(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789"
	var i = 0
	if (TheNumber=="") {
		// Return false if number is empty
		valid = 0
	}
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
// Note: Remove the comments from the following line to see this
// for loop in action.
// alert(TheNumber.charAt(i) + " is no good.")
			valid = 0
		} // End if statement
	} // End for loop
	return valid
}
function CheckNumber(TheNumberObj) {
	var valid = 1
	var GoodChars = "0123456789"
	var i = 0
	var TheNumber = TheNumberObj.value;
	if (TheNumber!="") {
		for (i =0; i <= TheNumber.length -1; i++) {
			if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
	// Note: Remove the comments from the following line to see this
	// for loop in action.
	// alert(TheNumber.charAt(i) + " is no good.")
				valid = 0
			} // End if statement
		} // End for loop
		if (valid == 0){
			alert("Please enter integer [0-9] only");
			TheNumberObj.focus();
			TheNumberObj.select();
		}
	}
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}
