 function trim(str) {
     while(str.charAt(0) == (" ") ) {
       str = str.substring(1);
     }

	 while(str.charAt(str.length-1) == " " ) {
       str = str.substring(0,str.length-1);
     }

	 return str;
 }

 function validateAlphabets(x, y) {
     var S=document.getElementById(x).value;

     //document.getElementById(x).value=S.toUpperCase();
   
     for (var i=0;i<S.length;i++) {
         switch (y) {
             case 1:  /* only alphabets */
			     if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122)) {
			     } else {
				     document.getElementById(x).value=S.replace(S.charAt(i),"");
			     }
			     break;

             case 2:  /* alphabets + whitespace */
			     if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) == 32)) {
			     } else {
				     document.getElementById(x).value=S.replace(S.charAt(i),"");
			     }
			     break;

             case 3:  /* alphabets + . */
			     if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) == 46)) {
			     } else {
				     document.getElementById(x).value=S.replace(S.charAt(i),"");
			     }
			     break;
         }
     }
 }

 function validateAlphaNumeric(x) {
     var S=document.getElementById(x).value;
   
     for (var i=0;i<S.length;i++) {
         if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) >= 47 && S.charCodeAt(i) <= 57)) {
         } else {
   	         document.getElementById(x).value=S.replace(S.charAt(i),"");
         }
     }
 }

 function validateAlphaNumericWS(x) { // with whitespace (32) allowed
     var S=document.getElementById(x).value;
   
     for (var i=0;i<S.length;i++) {
         if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) >= 47 && S.charCodeAt(i) <= 57) || (S.charCodeAt(i) == 32)) {
         } else {
   	         document.getElementById(x).value=S.replace(S.charAt(i),"");
         }
     }
 }

 function validateAlphaNumericHyphen(x) { // with hyphen (45) allowed
     var S=document.getElementById(x).value;
   
     for (var i=0;i<S.length;i++) {
         if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) >= 47 && S.charCodeAt(i) <= 57) || (S.charCodeAt(i) == 45)) {
         } else {
   	         document.getElementById(x).value=S.replace(S.charAt(i),"");
         }
     }
 }

 function validateAlphaNumericWSHyphen(x) {
     var S=document.getElementById(x).value;
   
     for (var i=0;i<S.length;i++) {
         if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) >= 47 && S.charCodeAt(i) <= 57) || (S.charCodeAt(i) == 32) || (S.charCodeAt(i) == 45)) {
         } else {
   	         document.getElementById(x).value=S.replace(S.charAt(i),"");
         }
     }
 }

 function validateAlphaNumericWSHyphenStar(x) {
     var S=document.getElementById(x).value;
   
     for (var i=0;i<S.length;i++) {
         if ((S.charCodeAt(i) >= 65 && S.charCodeAt(i) <= 90) || (S.charCodeAt(i) >= 97 && S.charCodeAt(i) <= 122) || (S.charCodeAt(i) >= 47 && S.charCodeAt(i) <= 57) || (S.charCodeAt(i) == 32) || (S.charCodeAt(i) == 45) || (S.charCodeAt(i) == 42)) {
         } else {
   	         document.getElementById(x).value=S.replace(S.charAt(i),"");
         }
     }
 }

 function validateNumeric(x) {
     var N=document.getElementById(x).value;

     for (var i=0;i<N.length;i++) {
         if (N.charCodeAt(i) < 47 || N.charCodeAt(i) > 57) {
   	         document.getElementById(x).value=N.replace(N.charAt(i),"");
         }
     }

     if (isNaN(document.getElementById(x).value) || trim(document.getElementById(x).value) == "") {
         document.getElementById(x).value = "";
     }
 }

 function validateDecimal(x) {
     var D=document.getElementById(x).value;

     for (var i=0;i<D.length;i++) {
         if ((D.charCodeAt(i) < 48 || D.charCodeAt(i) > 57) && D.charCodeAt(i) != 46) {
   	         document.getElementById(x).value=D.replace(D.charAt(i),"");
         }
     }
 }

 function validateEmail(x) {
     if (x.length < 1) {
         return "Email address is mandatory";
     }

     var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';

     for (i=0; i<invalidChars.length; i++) {
         if (x.indexOf(invalidChars.charAt(i),0) > -1) {
             return "Email address contains invalid characters";
         }
     }

     for (i=0; i<x.length; i++) {
        if (x.charCodeAt(i)>127) {
           return "Email address contains non ascii characters";
        }
     }

     var atPos = x.indexOf('@',0);

     if (atPos == -1) {
        return "Email address must contain an @";
     }

     if (atPos == 0) {
        return "Email address must not start with @";
     }

     if (x.indexOf('@', atPos + 1) > - 1) {
        return "Email address must contain only one @";
     }

     if (x.indexOf('.', atPos) == -1) {
        return "Email address must contain a period in the domain name";
     }

     if (x.indexOf('@.',0) != -1) {
        return "Period must not immediately follow @ in email address";
     }

     if (x.indexOf('.@',0) != -1){
        return "Period must not immediately precede @ in email address";
     }

     if (x.indexOf('..',0) != -1) {
        return "Two periods must not be adjacent in email address";
     }

     var suffix = x.substring(x.lastIndexOf('.') + 1);

     if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
        return "Invalid primary domain in Email address";
     }

     return "";
 }

 function validationMessage(x, y, z) {
     var backgndColor = "#f00";

     document.getElementById(x).style.borderColor = backgndColor;
     document.getElementById(y).innerHTML = z;
 }

 function clearPreviousValidation(x) {
     var elementLength = document.getElementById(x).elements.length;

     for (var i=0;i<elementLength;i++) {
		 var elementType = document.getElementById(x).elements[i].type;

         if (elementType == "text" || elementType == "password" || elementType == "textarea") {
             var y = document.getElementById(x).elements[i].id;
             var msgSpan = "err_msg_" + y;

             document.getElementById(y).style.borderColor = "#a3c8f8";

             if (document.getElementById(msgSpan)) { /* (typeof document.getElementById(msgSpan) == "undefined") */
                 document.getElementById(msgSpan).innerHTML = "";
             }

             /*
             var spanEls = document.getElementsByTagName("span");

             for (j=0;j<spanEls.length;j++) {
                 alert(spanEls[j].id);
             }
             */
		}
	}
 }

 function clearSpecificValidation(x) { /* special condition for AJAX */
    if (document.getElementById(x)) {
        //document.getElementById(x).innerHTML = "";
        //document.getElementById(x).style.border = "none";
        //document.getElementById(x).style.background = "none";
        document.getElementById(x).style.visibility = "hidden";
    }
 }

 /**
  * DHTML date validation script
  */

 // Declaring valid date character, minimum year and maximum year
 var dtCh= "-";
 var minYear=1900;
 var maxYear=2100;

 function isInteger(s) {
     var i;

     for (i = 0; i < s.length; i++) {   
         // Check that current character is number
         var c = s.charAt(i);
         if (((c < "0") || (c > "9"))) return false;
     }

     // All characters are numbers
     return true;
 }

 function stripCharsInBag(s, bag) {
     var i;
     var returnString = "";

     // Search through string's characters one by one.
     // If character is not in bag, append to returnString
     for (i = 0; i < s.length; i++) {   
         var c = s.charAt(i);
         if (bag.indexOf(c) == -1) returnString += c;
     }

	 return returnString;
}

 function daysInFebruary (year) {
     // February has 29 days in any year evenly divisible by four,
     // EXCEPT for centurial years which are not also divisible by 400

     return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
 }

 function DaysArray(n) {
     for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
     }

     return this;
 }

 function validateDate(x) {
     var daysInMonth = DaysArray(12);
     var pos1=x.indexOf(dtCh);
     var pos2=x.indexOf(dtCh,pos1+1);
     var strYear=x.substring(0,pos1);
     var strMonth=x.substring(pos1+1,pos2);
     var strDay=x.substring(pos2+1);

     strYr=strYear;

     if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
     if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);

     for (var i = 1; i <= 3; i++) {
	     if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
     }

     month=parseInt(strMonth);
     day=parseInt(strDay);
     year=parseInt(strYr);

     if (pos1==-1 || pos2==-1) {
		return ("The date format should be : yyyy-mm-dd");
     }

     if (strMonth.length<1 || month<1 || month>12) {
		return ("Please enter a valid month");
     }

     if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
		return ("Please enter a valid day");
     }

     if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		return ("Please enter a valid 4 digit year between " + minYear + " and " + maxYear);
     }

     if (x.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(x, dtCh)) == false) {
		return ("Please enter a valid date");
     }

     return "";
 }

 // data validation script ends here

 function validatePasswordRule(x) {
     var lowerCase = /[a-z]/;
     var upperCase = /[A-Z]/;
     var number = /[0-9]/;
     var punctuation = /[\W]/;
     var passwdLen = 8;

     if (!(lowerCase.test(x) && upperCase.test(x) && number.test(x) && punctuation.test(x))) {
         return "Password does not match rule";
     }

     if (x.length < passwdLen) {
         return "Password should be at least 8 characters long";
     }

     return "";
 }