


//this function removes pre spaces and post spaces and more than one from between two character.
function Trim(inputString)
{
 //inputString = theForm.name.value; 
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

		 
//-->

function validateEmail(str)
{	
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID");
		   return false;
		}		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr ){
		   alert("Invalid E-mail ID");
		   return false;
		}		
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Enter E-mail ID");
		    return false;
		 }

 		 return true;				
}

function validateData(strValidateStr,objValue,strError) 
{ 
	//alert("qwq");
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
		command  = strValidateStr.substring(0,epos); 
		cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
		command = strValidateStr; 
    }
    //alert(objValue.value);
    switch(command) 
    { 
		case "dateDiff": 
        case "DiffDate": 
         { 
           //var dateDiff = checkDateDiff(year,month,date)	  
		   var valueReturn = Trim(objValue.value)
           if(valueReturn.length == 0) 
           { 
		      objValue.value="";
              alert(strError); 
              return false; 
           }//if 
		   else
		   objValue.value = valueReturn;
           break;             
         }//case 
         
        case "req": 
        case "required": 
         {
		  var strVal = Trim(objValue.value); 
		 
		   if(eval(strVal.length) == 0) 
           { 		   
              if(!strError || strError.length ==0) 
              { 
                strError = "You must enter value for '" + objValue.name + "' field"; 
              }
              alert(strError);               
              return false; 
           }
           break;             
         }
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = "Input data length exceeded. \nMaximum input length allowed is : " + cmdvalue;
               }
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false;
             }
             break; 
          }
        case "minlength":
        case "minlen":
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = "You must enter minimum data. \nMinimum input length allowed is : " + cmdvalue;
               }
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false;
             }
             break; 
            }
        case "email": 
          { 
               if(!validateEmail(objValue.value)) 
               { 
                 return false;
               }
           break; 
          }
		case "phone": 
        case "fax": 
           { 
              var charpos = objValue.value.search("[^-()+ A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Enter valid Phone/Fax Number (US Format)"; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false;
              }
              break; 
           }
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]");
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + ": Enter only alpha-numeric data."; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false;
              }
              break; 
           }
        case "zip": 
        case "ZIP":
        case "zipcode":
        case "ZIPCODE":
           { 
              var charpos = objValue.value.search("[^- A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Enter valid Zip code Format: 99999-9999"; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false; 
              }
              break;               
           }
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^.0-9]"); 
              if(objValue.value.length > 0 && charpos >= 0)
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Enter only positive integers."; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false; 
              }
              break;
           }
        case "alphabetic": 
        case "alpha": 
			{ 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Enter only alphabates."; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false; 
              }
              break; 
			}
	case "alnumhyphen":
			{
              var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Input characters allowed are A-Z,a-z,0-9,- and _"; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false;
				}
				break;
			}
		case "validChar":
			{
              var charpos = objValue.value.search("[^A-Za-z0-9\-_.]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : Input characters allowed are A-Z, a-z, 0-9, -, _ and ."; 
                }
                alert(strError + "\n[Error at character position " + eval(charpos+1)+"]"); 
                return false;
				}
				break;
			}	
        case "lt":
        case "lessthan":
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name + " : Should be a number "); 
              return false;
            }
            if(eval(objValue.value) >=  eval(cmdvalue))
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }
              alert(strError);
              return false;
             }
            break; 
         }
        case "gt": 
        case "greaterthan":
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name + " : Should be a number "); 
              return false;
            }
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }
               alert(strError); 
               return false;                 
             }
            break; 
         }
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item");
              return false;
            }
            if(objValue.selectedIndex == eval(cmdvalue))
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = "Please select one option."; 
              }
              alert(strError); 
              return false;                                   
             } 
             break; 
         }
		 case "select": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item");
              return false;
            }
            if(objValue.value == "Sel")
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = "Please select one option."; 
              }
              alert(strError); 
              return false;                                   
             } 
             break; 
         }
    }
    return true; 
} 

function validateForm(objFrm,arrObjDesc) 
{ 

 for(var itrobj=0; itrobj < arrObjDesc.length; itrobj++) 
 { 

   if(objFrm.elements.length <= itrobj) 
   { 
        alert("BUG: Obj descriptor for a non existent form element"); 
        return false;
   }
   for(var itrdesc=0; itrdesc < arrObjDesc[itrobj].length; itrdesc++) 
   { 

	if(validateData(arrObjDesc[itrobj][itrdesc][0], 
                 objFrm[itrobj],arrObjDesc[itrobj][itrdesc][1]) == false) 
       	{ 
			if (objFrm[itrobj].type != "hidden")
				objFrm[itrobj].focus();
			return false; 
       	}
   }
 }
 return true;
} 

