var error_str="" ;
var val = new Array();  // Decalre a array for storing current field value


function isDate(fieldvalue,fieldname) {
        // Rules for the date regular expression:
        // The format of the date is:
        // dd/mm/yyyy
        // Days: Valid between 1 and 31
        // The first digit may either be a 3 followed by a 0 or a 1 
        // or it could be a 0 followed by 1 to 9, or a 1 or 2, 
        // followed by any digit.
        // Then follows a backslash
        // Months: Valid between 1 and 12
        // The month may be a 0 followed by 1 to 9, or a 1 followed by 
        // either a 0, 1 or 2. 
        // Then follows a backslash
        // The year must comprise of any 4 digits.

        var re = /^([0-9]|0[1-9]|1[0-9]|2[0-9]|3[01])\/([1-9]|0[1-9]|1[012])\/\d{4}/;
		var months=[0,31,28,31,30,31,30,31,31,30,31,30,31]

        if (re.test(fieldvalue) != true) {
			error_str=error_str+"* "+fieldname+"  is invalid Date\n";
			return_tag=false
			return false
	}
	
	var dts = fieldvalue.split("/")

	if (((dts[2] % 4 == 0) && (dts[2] % 100 != 0)) || (dts[2] % 400 == 0)) dts[2]=29;
		
	if (dts[1]=="02" && parseInt(dts[0],10) > months[2]) {
			error_str=error_str+"* "+fieldname+" is invalid Date (Valid day is 1 to "+months[2]+")\n";
			return_tag=false
			return false
	}

	if (parseInt(dts[0],10) > months[parseInt(dts[1],10)]) {
			error_str=error_str+"* "+fieldname+" is invalid Date (Valid day is 1 to "+months[parseInt(dts[1],10)]+")\n";
			return_tag=false
			return false	
	} 
return true
}

function trim(str){
     return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


function isName(fieldvalue,fieldname,Option) { 
	if(!Option) Option=""
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		if (Option!="") {
			error_str=error_str+"* " +fieldname+" is Empty\n";
			return
		} else return
	}

	for(var i=0; i<fieldvalue.length; i++) {
		var c = fieldvalue.charAt(i);
		if (!((c >= "a" && c<="z") ||(c >= "A" && c<="Z") || (c == "'") || (c==" ") || (c=="-") || (c=="."))) {
			error_str=error_str+"* "+fieldname+"  is not an Alphabets\n";
			return
	    	}
	}
}

function isEmpty(fieldvalue,fieldname) {
	if (trim(fieldvalue)=="")  error_str=error_str+"* " +fieldname+" is Empty\n";
}

function isInteger(fieldvalue,fieldname,min,max) { 
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}

	for (i = 0; i < fieldvalue.length; i++){   
        	// Check that current character is number.
	        var c = fieldvalue.charAt(i);
        	if (((c < "0") || (c > "9"))) {
		error_str=error_str+"* "+fieldname+" is invalid \n";
		return false
		}
    	}
	if (min>0 && max>0)  
		if (fieldvalue < min || fieldvalue > max) error_str=error_str+"* "+fieldname+" allowed between "+ min + " and " + max + " \n";
	if (min==0 && max>0)
		if (fieldvalue > max) error_str=error_str+"* "+fieldname+"'s upper limit is " + max + " \n";
	if (min>0 && max==0)
		if (fieldvalue < min) error_str=error_str+"* "+fieldname+"'s lower limit is " + min + " \n";
	return true;
}

function isAlpha(fieldvalue,fieldname) { 
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}

	for(var i=0; i<fieldvalue.length; i++) {
		var c = fieldvalue.charAt(i);
		if (!((c >= "a" && c<="z") ||(c >= "A" && c<="Z") || (c >= "0" && c<="9") )) {
			error_str=error_str+"* "+fieldname+"  is not Alphanumeric\n";
			return
	    }
	}
}


function isAddress(fieldvalue,fieldname) { 
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}

	for(var i=0; i<fieldvalue.length; i++) {
		var c = fieldvalue.charAt(i);
		if (!((c >= "a" && c<="z") ||(c >= "A" && c<="Z") || (c >= "0" && c<="9")||(c=="#")||(c==",")||(c==".")||(c=="-")||(c=="'")||(c==" "))) {
			error_str=error_str+"* "+fieldname+"  is invalid\n";
			return
	    }
	}
}

function isPhone(fieldvalue,fieldname) { 
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}

	for(var i=0; i<fieldvalue.length; i++) {
		var c = fieldvalue.charAt(i);
		if (!((c >= "0" && c<="9")||(c==")")||(c=="(")||(c==",")||(c=="-")||(c==" "))) {
			error_str=error_str+"* "+fieldname+" invalid Phone Number\n";
			return
	    }
	}
}

function isChar(fieldvalue,fieldname) { 
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}

	for(var i=0; i<fieldvalue.length;i++) {
		var c = fieldvalue.charAt(i);
		if (!((c >= "a" && c<="z") ||(c >= "A" && c<="Z"))) {
			error_str=error_str+"* "+fieldname+"  is not Alphabet(s)\n";
			return_tag=false
			return
	    }
	}
}

function isDecimal(fieldvalue,fieldname) {
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}
   for(var i=0; i<fieldvalue.length;i++) {
		var c = fieldvalue.charAt(i);
		if (c ==".") 
			continue
		if (!(c >= "0" && c<="9")) {
			error_str=error_str+"* "+fieldname+"  is not a number\n";
			return_tag=false
			return
	    }
	}
}

//decimal - with number of dots only one 
function isDec(fieldvalue,fieldname) {
	fieldvalue=trim(fieldvalue)
	if (fieldvalue=="") {
		error_str=error_str+"* " +fieldname+" is Empty\n";
		return
	}
var count_points=0
    for(var i=0; i<fieldvalue.length;i++) {
		var c = fieldvalue.charAt(i);
		if (c ==".") {
			count_points=count_points+1
			if (count_points>1) {
				error_str=error_str+"* "+fieldname+"  is not a Decimal\n";
				return_tag=false
				return
			}
			continue
		}
		if (!(c >= "0" && c<="9")) {
			error_str=error_str+"* "+fieldname+"  is not a Decimal\n";
			return_tag=false
			return
	    }
	}
}


function isEmail(fieldvalue) {
var checkTLD=1;
var emailPat=/^(.+)@(.+)$/
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=fieldvalue.match(emailPat);

if (matchArray==null) {
error_str=error_str+"* Email address format incorrect (check @ and .'s)\n"
return_tag = false
return false;
}

var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
error_str=error_str+"* Invalid Email ID\n   Ths username contains invalid characters.";
return_tag = false
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
error_str=error_str+"* Invalid Email ID\n   Ths domain name contains invalid characters.";
return_tag = false
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid
error_str=error_str+"* Invalid Email ID\n   The username doesn't seem to be valid.";
return_tag = false
return false;
}


var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
error_str=error_str+"* Invalid Email ID\n   Destination IP address is invalid!";
return_tag = false
return false;
   }
}
return true;
}

 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
error_str=error_str+"* Invalid Email ID\n   The domain name is not valid.";
return_tag = false
return false;
   }
}


if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
error_str=error_str+"* Invalid Email ID\n   The address must end in a well-known domain or two letter " + "country.";
return_tag = false
return false;
}

if (len<2) {
error_str=error_str+"* Invalid Email ID\n   This address is missing a hostname!";
return_tag = false
return false;
}
return true;
}
//Email Check
