/**
 * @author Nathaniel Robinson
 */
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

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 validatefields()
{
	try
	{
		hideBranch('firstNameError');
		hideBranch('lastNameError');
		hideBranch('cityError');
		hideBranch('stateError');
		hideBranch('locationZipcodeError');
		hideBranch('phoneError');
		hideBranch('altPhoneError');
		hideBranch('noContactError');
		hideBranch('noEmailError');
		hideBranch('noCaseDescription');
		hideBranch('confirmationError');
		
			// name
		if(document.forms.genericform.firstname.value=="")
		{showBranch('firstNameError');	return false;}
		else if(document.forms.genericform.lastname.value=="")
		{  showBranch('lastNameError'); return false;}
		
			// location
		if(document.forms.genericform.city.value=="")
		{showBranch('cityError');	return false;}
		
		if(document.forms.genericform.statename.value=="0")
		{showBranch('stateError');	return false;}
		
		if( document.forms.genericform.zipcode.value=="")
		{
			showBranch('locationZipcodeError'); return false;
		}
		else if (document.forms.genericform.zipcode.value.length<5)
		{
			showBranch('locationZipcodeError'); return false;
		}
		else if (document.forms.genericform.zipcode.value.length>5)
		{
			showBranch('locationZipcodeError'); return false;
		}
		
		// is there contact info?
		if ( document.forms.genericform.email.value=="" && 
				   (document.forms.genericform.phone1.value=="" && 
					document.forms.genericform.phone2.value=="" && 
					document.forms.genericform.phone3.value=="")
				)
		{
			showBranch('noContactError'); return false;
		}
		
		
		
		if (!document.forms.genericform.phone1.value=="")
		{
			if (document.forms.genericform.phone1.value.length<3)
			{
				showBranch('phoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('phoneError'); 
			return false;
		}
		
		if (!document.forms.genericform.phone2.value=="")
		{
			if (document.forms.genericform.phone2.value.length<3)
			{
				showBranch('phoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('phoneError'); 
			return false;
		}
		
		if (!document.forms.genericform.phone3.value=="")
		{
			if (document.forms.genericform.phone3.value.length<4)
			{
				showBranch('phoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('phoneError'); 
			return false;
		}

/*		
		if (!document.forms.genericform.altphone1.value=="")
		{
			if (document.forms.genericform.altphone1.value.length<3)
			{
				showBranch('altPhoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('altPhoneError'); 
			return false;
		}
		
		if (!document.forms.genericform.altphone2.value=="")
		{
			if (document.forms.genericform.altphone2.value.length<3)
			{
				showBranch('altPhoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('altPhoneError'); 
			return false;
		}
		
		if (!document.forms.genericform.altphone3.value=="")
		{
			if (document.forms.genericform.altphone3.value.length<4)
			{
				showBranch('altPhoneError'); 
				return false;
			}
		}
		else 
		{
			showBranch('altPhoneError'); 
			return false;
		}
		*/
		
		if (!document.forms.genericform.email.value=="")
		{
			if (document.forms.genericform.email.value.indexOf('@')<0)
			{
				showBranch('noEmailError'); 
				return false;
			}		
		}
			
		if(document.forms.genericform.casedescription.value=="")
		{ showBranch('noCaseDescription'); return false;}
	}	
	catch (E) 
	{
		alert(E);
		return false;
	}
}

function validateFieldNotEmpty(field, name) {

    if(field.value == "") 
	{
        // alert("Please fill out the " + name + " field.");
        // field.focus();
        return false;
    }
    else {
        return true;
    }
} // end validateFieldNotEmpty

function showBranch(branch){
	var objBranch = document.getElementById(branch).style;
	if(objBranch.display=="block") {
		objBranch.display="none";
	}
	else
	{
		objBranch.display="block";
	}
}

function hideBranch(branch)
{
	var objBranch = document.getElementById(branch).style;
		objBranch.display="none";
}

function setFocus(branch)
{
	var objBranch = document.getElementById(branch);
	objBranch.focus();
}
