//Form Validation

function FormFocus(obj)
{	
  obj.focus();
}

function eCheckEMail(sn){
    s= sn.value;
    if (s.indexOf("@") == -1) return false;
    if (s.indexOf(".") == -1) return false;
    at=false;
    dot=false;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1);
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                if (ch == "@"){
                  if (at) return false;
                  else at=true;
                }
                if ((ch==".") && at)
                   dot=true;
        }
        else return false;
    }
   return dot;
}

function CheckEMail(theForm,theEmailFields,theMsg){
	for(var i=0; i<theEmailFields.length; i++)
		if (theForm.elements[theEmailFields[i]].value != "")
		{      
			if (!eCheckEMail(theForm.elements[theEmailFields[i]])){
				alert(theMsg);
				FormFocus(theForm.elements[theEmailFields[i]]);
				return false;
			}
		}   
return true;
}

function CheckRequiredFields(theForm,theRequiredFields,theMsg)
{
	var whitespace = " \t\n\r";
	
	for(i=0; i<theRequiredFields.length; i++)
	{
  		var isletter = false;
  		
		if(theForm.elements[theRequiredFields[i]].value=="")
  		{
	  		alert(theMsg);
    		FormFocus(theForm.elements[theRequiredFields[i]]);
			return false;
       	}
   		for (j = 0; j < theForm.elements[theRequiredFields[i]].value.length; j++)
    	{
        	var c = theForm.elements[theRequiredFields[i]].value.charAt(j);
			
        	if (whitespace.indexOf(c) == -1) 
        		isletter = true;	      	
    	}
    	if (isletter == false)
    	{
   			alert(theMsg);
   			FormFocus(theForm.elements[theRequiredFields[i]]);
   			return false;
    	}
	}
	return true;
}
function CheckCompare(theForm,theCompareFields,theMsg){
	i = 0;
	if (theCompareFields.length > 1)
	{
		if(theForm.elements[theCompareFields[i]].value!=theForm.elements[theCompareFields[i+1]].value)
  		{
	 		alert(theMsg);
    		theForm.elements[theCompareFields[i+1]].value = "";
	  		FormFocus(theForm.elements[theCompareFields[i+1]]);
			return false;
     	}
	}
     return true;
}
function CheckRequiredLists(theForm,theRequiredLists,theMsg){
	for(var i=0; i<theRequiredLists.length; i++)
		if(theForm.elements[theRequiredLists[i]].selectedIndex == 0)
		{
			
			alert(theMsg);
			FormFocus(theForm.elements[theRequiredLists[i]]);
			return false;
		}
	return true;
}
function CheckRequiredDate(theForm,theRequiredDate,theMsg){
	for(var i=0; i<theRequiredDate.length; i++)
	{
		var day = document.getElementById(theRequiredDate[i]+"_day");
		var mount = document.getElementById(theRequiredDate[i]+"_mount");
		var year = document.getElementById(theRequiredDate[i]+"_year");
		if (day.selectedIndex == 0)
		{
			alert(theMsg);
			day.focus();
			return false;
		}
		if (mount.selectedIndex == 0)
		{
			alert(theMsg);
			mount.focus();
			return false;
		}
		if (year.selectedIndex == 0)
		{
			alert(theMsg);
			year.focus();
			return false;
		}
	}
	return true;
}

function CheckSpecialValues(theForm,theSpecialValues,theMsg)
{
	theMsg = theMsg.replace("##","\n");
	
	for(i=0; i<theSpecialValues.length; i++)
	{
		if (theForm.elements[theSpecialValues[i][0]].value != "")
		{
	  		var unallowable = false;
	  		var allowable = theSpecialValues[i][1];
	  		var min_length = theSpecialValues[i][2];
	  		var max_length = theSpecialValues[i][3];
	  		
	  		if (max_length == 0) 
	  			max_length = 255;
	  		
	  		for (j = 0; j < theForm.elements[theSpecialValues[i][0]].value.length; j++)
	    	{
	    		var c = theForm.elements[theSpecialValues[i][0]].value.charAt(j);
				
	        	if (allowable.indexOf(c) == -1) 
	        		unallowable = true;	      	
	    	}
	    	
	    	ln = theForm.elements[theSpecialValues[i][0]].value.length;
	    	
	    	if (unallowable == true || ln < min_length || ln > max_length)
	    	{
	    		alert(theMsg);
	    		FormFocus(theForm.elements[theSpecialValues[i][0]]);
	    		return false;
	    	}
		}
	}
	return true;
}

function CheckForm(theForm,theEmailFields,theRequiredFields,theCompareFields,theRequiredLists,theRequiredDate,theSpecialValues,theEmailMsg,theGlobalMsg,theCompareMsg,theSpecialMsg){
    var theForm = document.forms[theForm];
	if(CheckRequiredFields(theForm,theRequiredFields,theGlobalMsg))
    if(CheckEMail(theForm,theEmailFields,theEmailMsg))
	if(CheckCompare(theForm,theCompareFields,theCompareMsg))
	if(CheckRequiredLists(theForm,theRequiredLists,theGlobalMsg))
	if(CheckRequiredDate(theForm,theRequiredDate,theGlobalMsg))
	if(CheckSpecialValues(theForm,theSpecialValues,theSpecialMsg))
    	return true;
  return false;
}

//End