//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//global Form Validation
//v.1
//joel peterson
//////////////////////////////////////////////////////////////////////////////////////
/*include this js your html file and return on submit.
<form name="formname" onsumbit="return validate('formname')">
For each input tag include an id parameter that contains TYP, REQ and MAX (in that order)
  1. REQ - required.
    	a. 1 - Is Required
    	b. 0 - Is Not Required    
  2. MAX - Total Chars Available.  
		a. any Integer
  3. TYP - the type of input to be validated as:
    	a. eml - email
    	b. dte - date (DDMMYYYY)
    	c. num - number
    	d. com - company
    	e. add - address line
    	f. cty - City
    	g. ste - state (drop down) (--)
    	h. zip - zip
    	i. cny - country
    	j. phn - phone
    	k. pas - password

i.e. <input type=text name=fname id=fnameTYPnmeREQ1MAX50>  
	will be validated as a 'name'/ is required/ and max character limit is 50*/


function validate(objForm) { 
eval('var formobj = document.' + objForm);
var totalElements = formobj.elements.length;
var whichitem = 1;
var myElNme = "";
var myPre = "";
var myParms = "";
var myType = "";
var myReq = "";
var myMax = "";
while (whichitem < totalElements) {
		if (formobj.elements[whichitem].id != '') {
			if (formobj.elements[whichitem].disabled == false) {
    			myParms = formobj.elements[whichitem].id;
    			if(myParms.indexOf('REQ') != -1) {
    				myReq = myParms.substr((myParms.indexOf('REQ') + 3), 1); 
    				if (myReq=="1") {
    				//required
        			 	if(formobj.elements[whichitem].value.length < 1) {
          				 	alert("Required information is missing");
          				 	formobj.elements[whichitem].focus();
          				 	formobj.elements[whichitem].select();
          					return false;
        				}
    				}
    			}
    			if(myParms.indexOf('MAX') != -1) {
    				myMax = myParms.substr((myParms.indexOf('MAX') + 3));
    				myMax = parseInt(myMax);
    				if (formobj.elements[whichitem].value.length > myMax) {
    					alert("Exceeded maximum length (" + myMax + ')');
          				 	formobj.elements[whichitem].focus();
          				 	formobj.elements[whichitem].select();
          					return false;
    				} 
    			}
    			if(myParms.indexOf('TYP') != -1) {
    				myType = myParms.substr((myParms.indexOf('TYP') + 3), 3); 
    				switch (myType) {
						case 'unm' :
							if (checkUser(formobj.elements[whichitem])==1){return false}
						break
                        case 'eml' :
                          	if (checkEmail(formobj.elements[whichitem])==1){return false}
                        break
                        case 'nme' :
                         	if (checkName(formobj.elements[whichitem])==1){return false}
                        break
        				case 'dte' :
                          	if (checkMMDDYYY(formobj.elements[whichitem])==1){return false}
                        break
    					case 'num' :
                        	if (checkInteger(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'com' :
                        	if (checkCompany(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'add' :
                        	if (checkAddress(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'cty' :
                        	//statement
                      	break
    					case 'drp' :
							if (checkDashesEmpty(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'zip' :
                        	if (checkZipCode(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'cny' :
                        	//statement
                      	break
    					case 'phn' :
                        	if (checkPhone(formobj.elements[whichitem])==1){return false}
                      	break
    					case 'pas' :
                        	//statement
                      	break
                     }
    			}
			}
		}
      whichitem++;
    }
	//return false;
}

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////Supporting Functions/////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

/*--------------------------Checking For The Empty Value------------------------------*/
  function checkEmpty(fieldValue){
   	var fldValue=fieldValue;
   	var flag=0;
   	var space=0;  
   	for ( var i=0; i < fldValue.length; i++ ){
	if (fldValue.charAt(i)==" "){space++;}else{break;}}
    if (fldValue.length==space){return 1;}else{return 0;}}

/*------------------------Checking For Null Values------------------------------------*/
   function checkNull(fieldObject){
     var fieldValue
     fieldValue=fieldObject.value
     if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
        alert("Missing required information.");
        fieldObject.focus();
        fieldObject.select();
        return 1;}else{return 0;}}

/*------------------------Checking For United States------------------------------------*/
   function checkUnitedStates(fieldObject,fieldObject2,fieldname){
     var fieldValue
     fieldValue=fieldObject.value
     var fieldValue2
     fieldValue2=fieldObject2.value	
    if ((fieldValue=="United States") && ((checkEmpty(fieldValue2)==1)||(fieldValue2==""))){	
        alert(fieldname + " Should Not Be Blank");
        fieldObject2.focus();
        fieldObject2.select();
        return 1;}else{ return 0;}
   }

/*------------------------Checking For Dashes (Empty)------------------------------------ */

    function checkDashesEmpty(fieldObject){
		var fieldValue
		var fieldValue=fieldObject.value

        if ((checkEmpty(fieldValue)==1)||(fieldValue=="")||(fieldValue=="--------")||(fieldValue=="-")||(fieldValue=="--")||(fieldValue=="---")){
			alert("Please make a selection from the drop down menu");
				fieldObject.focus();
        		//fieldObject.select();
        		return 1;}else{return 0; }
	}


/*------------------------Checking For PullDown (Empty)------------------------------------ */

   function checkPullDownEmpty(fieldObject,fieldName){
     var fieldValue
     fieldValue=fieldObject.value
     if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
        alert(fieldName+" Should Not Be Blank");
        fieldObject.focus();
        //fieldObject.select();
        return 1;}else{return 0;}
	}

/*------------------------Checking For Country equals NONE------------------------------------ */

    function checkCountryRequired(fieldObject,fieldName){
		var fieldValue
		var fieldValue=fieldObject.value
		if (fieldValue=="NONE"){
			alert("This is a required field, "+fieldName+" cannot be \"None\" ");
				fieldObject.focus();
        		//fieldObject.select();
        		return 1;
}else{return 0; }
	}     
   
/*--------------------------Checking for MaxLength -------------------------------------*/
  function checkmaxlength(fieldobject,fieldname,maxlength)
{
	var fielddata = fieldobject.value
	if( fielddata.length > maxlength)
	{
		alert('Excess field length in ' + fieldname + ' (Max:' + maxlength + ')')
		fieldobject.focus()
		return false
	}
	return true
}
  
/*---------------------------Checking For Integer Values------------------------------------*/
  function checkInteger(fieldObject){
     var myint='0123456789'
     var flag=0
     var fieldValue
     var testValue
     fieldValue=fieldObject.value;
     
     if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){
        for ( var i=0;i<fieldValue.length;i++){
            testValue=fieldValue.substring(i,i+1);
            if (myint.indexOf(testValue)==-1){
               flag=1;
               alert("Enter Only Numbers From 0 to 9");
               fieldObject.focus();
               break;}else{flag=0;}}return flag;}
  }

 /*---------------------------Checking For Alphabets------------------------------------*/
 
 function checkAlphabet(fieldObject){
    var fieldValue=fieldObject.value
    if (checkEmpty(fieldValue)==0){
      for (var i=0;i<fieldValue.length;i++){
         if ((((fieldValue.charCodeAt(i)>=65)&&(fieldValue.charCodeAt(i)<=90))||((fieldValue.charCodeAt(i)>=97)&&(fieldValue.charCodeAt(i)<=122)))||(fieldValue.charCodeAt(i)==32)){
         }else{
            alert("Enter Only Alphabetical Characters");
            fieldObject.focus();
            fieldObject.select();
            return 1;
         }   
      }
    }
 }
 
 /*---------------------------Checking For Name------------------------------------*/
  function checkName(fieldObject){
     var myint='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.-\' '
     var flag=0
     var fieldValue
     var testValue
     fieldValue=fieldObject.value;
     
     if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){
        for ( var i=0;i<fieldValue.length;i++){
            testValue=fieldValue.substring(i,i+1);
            if (myint.indexOf(testValue)==-1){
               flag=1;
               alert("Only letters are permitted in the name field.");
               fieldObject.focus();
               fieldObject.select();
               break;
            }else{
               flag=0;
            }
        }
        return flag;
     }
  }

 /*---------------------------Checking For Company------------------------------------*/
  function checkCompany(fieldObject){
     var myint='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,#&- '
     var flag=0
     var fieldValue
     var testValue
     fieldValue=fieldObject.value;
     
     if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){
        for ( var i=0;i<fieldValue.length;i++){
            testValue=fieldValue.substring(i,i+1);
            if (myint.indexOf(testValue)==-1){
               flag=1;
               alert("Only Alphabetical Characters, Pound(#), Period, Comma, and Hypen are permitted");
               fieldObject.focus();
               fieldObject.select();
               break;}else{flag=0;}}return flag;}
  }

 /*---------------------------Checking For Address------------------------------------*/
  function checkAddress(fieldObject){
     var myint='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#.- '
     var flag=0
     var fieldValue
     var testValue
     fieldValue=fieldObject.value;
     
     if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){
        for ( var i=0;i<fieldValue.length;i++){
            testValue=fieldValue.substring(i,i+1);
            if (myint.indexOf(testValue)==-1){
               flag=1;
               alert("Only Alphabetical Characters, Pound(#), Period and Hypens are permitted in the address lines");
               fieldObject.focus();
               fieldObject.select();
               break;
            }else{flag=0;}}return flag;}
  }
 
 /*---------------------------Checking For Phone ------------------------------------*/

function checkPhone(fieldObject){
   var fieldValue=fieldObject.value
   phoneText = "1234567890()-+ ";
   for (var i=0; i<fieldValue.length; i++){
	   if (phoneText.indexOf(fieldValue.substring(i,i+1)) < 0){
	      alert("Phone number must contain numbers only"); 
	      fieldObject.focus();
		  fieldObject.select();
	      return 1;}}
}		 

 /*---------------------------Checking For Zip Code------------------------------------*/
  function checkZipCode(fieldObject){
     var myint='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- '
     var fieldValue
     var flag=0
     fieldValue=fieldObject.value;
     /* if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){ */
     if (fieldValue!=""){
     	var re
     	re = /\d{1,}/;
     	if (re.test(fieldValue) == false){
       		alert("Zip Code Must Contain Numbers");
       		fieldObject.focus();
       		fieldObject.select();
    		flag=1
    		return 1;
     	}
	if (flag==0){
            var testValue
            for ( var i=0;i<fieldValue.length;i++){
            	testValue=fieldValue.substring(i,i+1);
            	if (myint.indexOf(testValue)==-1){
               		alert("Enter only Alphabetical Characters and Hypen");
               		fieldObject.focus();
               		fieldObject.select();
					return 1;
               		break;
            	}
            }
	}
     }else{
        return 0;
     }
  }

 /*------------------------------Checking For Email-----------------------------*/ 
    function checkEmail(fieldObject){
	var fieldValue
     	var fieldValue=fieldObject.value
	if (fieldValue!=""){
        	var re
        	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        	if (re.test(fieldValue) != true){
            		alert("Please make sure you entered a valid email address.");
        		fieldObject.focus();
        		fieldObject.select();
        		return 1;
		} 
        }else{
	  return 0;
	}
    }

/*------------------------Checking For YYYY------------------------------------*/
    function checkYYYY(fieldObject){
	var fieldValue
     	var fieldValue=fieldObject.value
     	if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
		return 0;
	}else{
        	var re
        	re = /\d{4}/;
        	if (re.test(fieldValue) == true){
	    		return 0;
		}else{
        		alert("Year Format is YYYY (2001).");
        		fieldObject.focus();
        		fieldObject.select();
        		return 1; 
       		}
    	}
     }

/*------------------------Checking For MM/YYYY------------------------------------*/
    function checkMMYYYY(fieldObject){
	var fieldValue
     	var fieldValue=fieldObject.value
        var re
        re = /^(0[1-9]|1[012])\/\d{4}/;
        if (re.test(fieldValue) == true){
	    return 0;
	}else{
        alert("Date format is MM/YYYY (01/2000).\n Please make sure you entered a valid date.");
        fieldObject.focus();
        fieldObject.select();
        return 1; 
        }
    }

/*------------------------Checking For MM/DD/YYYY------------------------------------*/
    function checkMMDDYYYY(fieldObject){
	var fieldValue
     	var fieldValue=fieldObject.value
     	if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
		return 0;
	}else{
        	var re
        	re = /^(0[1-9]|1[012])\/(3[01]|0[1-9]|[12]\d)\/\d{4}/;
        	if (re.test(fieldValue) == true){
	    		return 0;
		}else{
        		alert("Date format is MM/DD/YYYY (01/01/2000).\n Please make sure you entered a valid date.");
        		fieldObject.focus();
        		fieldObject.select();
        		return 1; 
       		}
    	}
     }	

/*------------------------Checking For MMDDYYYY less than 100 years------------------------------------ */

    function checkYearMMDDYYYY(fieldObject){
	var fieldValue
	var fieldValue=fieldObject.value
	     	if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
		return 0;
	}else{
		var now = new Date();
		var CurrYear = now.getFullYear();
		var YearValue
		var YearValue=fieldValue.substring(6, 10)
		var ChkRealYear = (CurrYear - YearValue)
		if ((ChkRealYear > 100)||(ChkRealYear < 0)){
			alert("Please enter a correct year");
			fieldObject.focus();
        		fieldObject.select();
        		return 1;
		}else{
        		return 0; 
    		}
     	}
    }	


/*------------------------Checking For MMYYYY less than 100 years------------------------------------ */

    function checkYearMMYYYY(fieldObject){
	var fieldValue
	var fieldValue=fieldObject.value
	     	if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
		return 0;
	}else{
		var now = new Date();
		var CurrYear = now.getFullYear();
		var YearValue
		var YearValue=fieldValue.substring(3, 7)
		var ChkRealYear = (CurrYear - YearValue)
		if ((ChkRealYear > 100)||(ChkRealYear < 0)){
			alert("Please enter a correct year");
			fieldObject.focus();
        		fieldObject.select();
        		return 1;
		}else{
        		return 0; 
    		}
     	}
    }


/*------------------------Checking For YYYY less than 100 years------------------------------------ */

    function checkYearYYYY(fieldObject){
	var fieldValue
	var fieldValue=fieldObject.value
	     	if ((checkEmpty(fieldValue)==1)||(fieldValue=="")){
		return 0;
	}else{
		var now = new Date();
		var CurrYear = now.getFullYear();
		var ChkRealYear = (CurrYear - fieldValue)
		if ((ChkRealYear > 100)||(ChkRealYear < 0)){
			alert("Please enter a correct year");
			fieldObject.focus();
        		fieldObject.select();
        		return 1;
		}else{
        		return 0; 
    		}
	}	
     }

 /*---------------------------Checking User Name------------------------------------*/
  function checkUser(fieldObject){
     var myint='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\' '
     var flag=0
     var fieldValue
     var testValue
     fieldValue=fieldObject.value;
     
     if ((checkEmpty(fieldValue)==0)&&(fieldValue!="")){
        for ( var i=0;i<fieldValue.length;i++){
            testValue=fieldValue.substring(i,i+1);
            if (myint.indexOf(testValue)==-1){
               flag=1;
               alert("Please enter only numbers, letters, and apostrophes");
               fieldObject.focus();
               fieldObject.select();
               break;
            }else{
               flag=0;
            }
        }
        return flag;
     }
  }