function trim(field){//Params: field to trim
	//This removes all the leading and trailing spaces from the field.
	while(''+field.value.charAt(0)==' ')
		field.value=field.value.substring(1,field.value.length);
	while(''+field.value.charAt(field.value.length-1)==' ')
		field.value=field.value.substring(0,field.value.length-1);
}
function msg_focus(field, msg){//Params: field to make the focus to and the error message
	//moves the focus to the first form field where an error occured
	if (!message) field.focus()
	return msg + '\n'
}
function text(field, msg){//Params: Name of the text field of form, Error to be displayed
	//Check if data is entered in the passed field
	//alert(field.value.length);
	trim(field)
	if (field.value.length == 0){
		return msg_focus(field, msg)
	}
	/*else{
		var st = field.value.toString()
		for(var i=0;i<st.length;i++){
			if (st.charAt(i) == '\'' || st.charAt(i) == '"' || st.charAt(i) == '\\'){
				return msg_focus(field, msg + ' should not contain \' or \" or \\')
			}			
		}	
	}*/
	return '';
}

function newtext(field, msg, typedvalue){//Params: Name of the text field of form, Error to be displayed,typedval
	//Check if data is entered in the passed field
	trim(field)
	if (field.value == typedvalue){
		return msg_focus(field, msg)
	}
	return '';
}

function deci(field, msg){//Params: Name of the decimal field of form, Error to be displayed
	//Check if data is entered, and is numeric(may be decimal value)(+ve or -ve)
	trim(field)
	if(isNaN(field.value) || field.value == false)
		return msg_focus(field, msg)
	return '';
}
function inte(field, msg){//Params: Name of the integer field of form, Error to be displayed
	//Check if data is entered and is integer(+ve or -ve)
	trim(field)
	if(isNaN(field.value) || field.value == "")
		return msg_focus(field, msg)
	else{
		var st = field.value.toString()
		for(var i=0;i<st.length;i++){
			if (i == 0 && (st.charAt(i) == '-' || st.charAt(i) == '+')){
			}
			else if (isNaN(st.charAt(i))){
				return msg_focus(field, msg)
			}			
		}
	}
	return '';
}
function chck(field, msg){//Params: Name of the checkbox field of form, Error to be displayed
	//Check if a checkbox is checked (of the passed name).  Can be used for Terms of Service
	if (field.checked == false){
		return msg_focus(field, msg)
	}
	return '';
}

function radi(field, msg){//Params: Name of the radio field of form, Error to be displayed
	//Check if a radio button is checked (of the passed name)
	var length = field.length
	var truth = 0
	for(var i=0;i<length;i++){
		if (field[i].checked == true){
			truth = 1
		}
	}
	if (truth == 0)
		return msg_focus(field[0], msg)
	return '';
}

function name(field, msg){
	trim(field);
	if (field.value.length < 2){
		return msg_focus(field, msg+'-Must be at least two characters in length')
	}
	var truth = 1;
	var strPattern = /^([0-9A-Za-z])[0-9A-Za-z\-]+$/i
	truth = strPattern.test(field.value)
	if (truth == 0)
		return msg_focus(field, msg)
	return '';
}

//dat(frm.year.value '-' + frm.month.value + '-' + frm.day.value)
function dat(field, msg){
	trim(field);
	var truth = 1;
	var strPattern = /[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}/
	truth = strPattern.test(field.value)
	dat = field.value.split('-')
	if (dat[0] < 1900 || dat[1] < 0 || dat[1] > 12 || dat[2] < 0 || dat[2] > 31){
		truth = 0
	}
	if ((dat[1] == 4 || dat[1] == 6 || dat[1] == 9 || dat[1] == 11) && dat[2] == 31){
		truth = 0
	}
	if (dat[1] == 2 && dat[2] > 29){
		truth = 0
	}
	if (dat[1] == 2 && dat[2] == 29 && dat[0]%4 != 0){
		truth = 0
	}
	if (truth == 0){
		return msg_focus(field, msg)
	}
	return '';
}
/*function tele(field, msg){
	trim(field);
	var truth = 1;
	var strPattern = /[0-9]\-\(\)\./
	truth = strPattern.test(field.value)
	/*dat = field.value.split('-')
	if (dat[0] < 1900 || dat[1] < 0 || dat[1] > 12 || dat[2] < 0 || dat[2] > 31){
		truth = 0
	}* /
	if (truth == 0){
		return msg_focus(field, msg)
	}
	return '';
}*/
function img(field, msg){
	trim(field);
	var truth = 1;
	var strPattern = /\.jpg$|\.jpeg$|\.gif$|\.png$|\.pjpeg$|\.tif$/i
	truth = strPattern.test(field.value)
	if (truth == 0){
		return msg_focus(field, msg)
	}
	return '';
}
function imgdoc(field, msg){
	trim(field);
	var truth = 1;
	var strPattern = /\.jpg$|\.doc$|\.pdf$|\.txt$|\.zip$|\.bmp$|\.ppt$|\.pps$|\.swf$|\.xls$|\.jpeg$|\.gif$|\.png$|\.pjpeg$|\.tif$/i
	truth = strPattern.test(field.value)
	if (truth == 0){
		return msg_focus(field, msg)
	}
	return '';
}
function imgsize(field, msg){
	var oas = new ActiveXObject("Scripting.FileSystemObject");
	var d = field.value;
	var e = oas.getFile(d);
	var f = e.size;
	var imagesize=10*1024*1024
	if (f > imagesize){
		return msg_focus(field, msg)
	}
	return '';
}

function url(field, msg){
	trim(field);
	var truth = 1;
	var strPattern = /^http:\/\/[A-Za-z\-_0-9\.]+\.[A-Za-z]{2,3}|^http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
	truth = strPattern.test(field.value)
	
	strPattern1 = /^[^A-Za-z0-9_]|\s+/
	truth1 = strPattern1.test(field.value)
	
	if (truth == 0 || truth1 != 0){
		return msg_focus(field, msg)
	}
	return '';
}
function mail(field, msg){//Params: Name of the email field of form, Error to be displayed
	//Check if data is entered, and is like an email id
	trim(field);
	var truth = 1;
	var strPattern = /^[A-Za-z0-9][A-Za-z\-_0-9\.]+@[A-Za-z\-_0-9\.]+\.[A-Za-z]{2,3}$/	
	truth = strPattern.test(field.value)
	if (truth == 0)
		return msg_focus(field, msg)
	return '';
}


function edu(field, msg){//Params: Name of the email field of form, Error to be displayed
	//Check if data is entered, and is like an email id
	trim(field);
	var truth = 1;
	var strPattern = /^[A-Za-z0-9][A-Za-z\-_0-9\.]+@[A-Za-z\-_0-9\.]+\.edu$/	
	truth = strPattern.test(field.value)
	if (truth == 0)
		return msg_focus(field, msg)
	return '';
}
function tele(field, msg){//Params: Name of the email field of form, Error to be displayed
	//Check if data is entered, and is like an email id
	trim(field);
	var truth = 1;
	var strPattern = /^[0-9\+\.\-\(\)\s]+[0-9\.\-\(\)\s]$/	
	truth = strPattern.test(field.value)
	//alert(field.value)
	if (truth == false)
		return msg_focus(field, msg)
	return '';
}
function mini(field, num, msg){
	trim(field);
	if (field.value.length < num){
		return msg_focus(field, msg)
	}
	return ''
}
function maxi(field, num, msg){
	trim(field);
	if (field.value.length > num){
		return msg_focus(field, msg)
	}
	return ''
}
function equi(field, cfield, msg){
	trim(field); 
	trim(cfield);
	if (field.value != cfield.value){
		return msg_focus(cfield, msg)
	}
	return '';
}
function notequ(field, cfield, msg){
	trim(field); 
	trim(cfield);
	if (field.value == cfield.value){
		return msg_focus(cfield, msg)
	}
	return '';
}
var months=new Array(13);
months[1]="Jan";
months[2]="Feb";
months[3]="Mar";
months[4]="Apr";
months[5]="May";
months[6]="Jun";
months[7]="Jul";
months[8]="Aug";
months[9]="Sep";
months['01']="Jan";
months['02']="Feb";
months['03']="Mar";
months['04']="Apr";
months['05']="May";
months['06']="Jun";
months['07']="Jul";
months['08']="Aug";
months['09']="Sep";
months[10]="Oct";
months[11]="Nov";
months[12]="Dec";

function loop(dname, name, start, end, select, reverse){
	monthPattern = /month/i;

    ret = '<SELECT name='+name+'>'; 
    ret += '<OPTION value="">'+dname;
	if(reverse){
		for(i=end;i>=start;i--){
			ret += '<OPTION value="'
			if (i < 10) ret += '0';
			ret += i + '"';
			if (i == select) {
				ret += ' selected';
			}
			ret += '>';

			if (monthPattern.test(name)) {
				ret += months[i]
			}
			else {
				if (i < 10) ret += '0';
				ret += i;
			}
		}
	}else{
		for(i=start;i<=end;i++){
			ret += '<OPTION value="'
			if (i < 10) ret += '0';
			ret += i + '"';
			if (i == select) {
				ret += ' selected';
			}
			ret += '>';

			if (monthPattern.test(name)) {
				ret += months[i]
			}
			else {
				if (i < 10) ret += '0';
				ret += i;
			}
		}
	}
    
    ret += '</SELECT>';
    return ret;
}

function retu(message){//Params: message to be displayed
	//Gives alert if error occured
	if (message){
		alert('Please enter/select\n\n' + message)
		return false
	}
	return true
}
function winopen(url, w, h, scrollable){
	window.open(url, 'url', "left=10,top=10,toolbar=no,menubar=no,location=no,scrollbars="+scrollable+", directories=no,resizable=no,width="+w+",height="+h);
	return false
}

//alert if there are specialcharecters in the field
function specialcharacterscheck(field,msg){
 var message = msg.replace('*',field.name);
 //var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
 var iChars = "<>";

  for (var i = 0; i < field.value.length; i++) {
  	if (iChars.indexOf(field.value.charAt(i)) != -1) {
  		return msg_focus(field, message)
  	}
  }
  return '';
}
//alert if there are alphabets in the field
function numbersonly(field,msg){
	var message = msg.replace('*',field.name);
    var alphabets = /[a-zA-Z]<>$/;
	if (alphabets.test(field.value)) {
		 return msg_focus(field, message);
	}
	return '';
}

//alert if there are numbers in the field
function alphabetsonly(field,msg){
	var message = msg.replace('*',field.name);
	var strPattern = /^([A-Za-z])[A-Za-z\s]*$/i
	if (!strPattern.test(field.value)) {
		 return msg_focus(field, message);
	}
	return '';
}

//alert if it is not a hexadecimal charater - for background colors
function hexadecimalcheck(field,msg){
 var message = msg.replace('*',field.name);
 var iChars = "0123456789ABCDEF";

  for (var i = 0; i < field.value.length; i++) {
  	if (iChars.indexOf(field.value.charAt(i)) != -1) {
  		return msg_focus(field, message)
  	}
  }
  return '';
}

//alert if there are specialcharecters in the field except . and _
function splCharCheckForUsername(field,msg){
 var message = msg.replace('*',field.name);
 var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?";

  for (var i = 0; i < field.value.length; i++) {
  	if (iChars.indexOf(field.value.charAt(i)) != -1) {
  		return msg_focus(field, message)
  	}
  }
  return '';
}

//alert if there are specialcharecters in the field except . and _ in onBlur
function SplCharAlertForUsername(field){
 var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?";

  for (var i = 0; i < field.length; i++) {
  	if (iChars.indexOf(field.charAt(i)) != -1) {
  		alert('Special characters are not allowed for User Name except . and _');
  	}
  }
  
}

//alert if the first letter is a special character
function splCharCheckForFirstCharacter(field,msg){
var message = msg.replace('*',field.name);
 var iChars = "!@#$%^&*()+=-[]\\\';._,/{}|\":<>?0123456789";
   	if (iChars.indexOf(field.value.charAt(0)) != -1) {
  		return msg_focus(field, message)
  	}
  
  return '';
}

//alert if the first letter is a special character in OnBlur
function SplCharAlertForFirstLetter(field){
var iChars = "!@#$%^&*()+=-[]\\\';._,/{}|\":<>?0123456789";
	if (iChars.indexOf(field.charAt(0)) != -1) {
  		alert('First letter should be a character for First Name !');
  	}
 
}


//alert if there are not alphanumerics and start with numeric in the field
function alphanumbericonly(field,msg){
	var message = msg.replace('*',field.name);
	trim(field);
    var truth = 1;
	var strPattern = /^([A-Za-z])[A-Za-z0-9<>\_\s]*$/i
	truth = strPattern.test(field.value)
	//alert(field.value)
	if (truth == false)
		return msg_focus(field, message)
	return '';
}
function validphonenumber(field,msg){
	var message = msg.replace('*',field.name);
	trim(field);
    var truth = 1;
	var strPattern = /^([0-9])[0-9<>\-+\s]*$/i
	truth = strPattern.test(field.value)
	//alert(field.value)
	if (truth == false)
		return msg_focus(field, message)
	return '';
}

//alert if there are not alphanumerics and start with numeric in the field and allow comma and dot
function alphanumbericcommadotonly(field,msg){
	var message = msg.replace('*',field.name);
	trim(field);
    var truth = 1;
	var strPattern = /^([A-Za-z])[A-Za-z0-9\_,.\s]*$/i
	truth = strPattern.test(field.value)
	//alert(field.value)
	if (truth == false)
		return msg_focus(field, message)
	return '';
}
//alert if there are not alphanumerics and start with numeric in the field and allow underscore and dot
function alphanumbericunderscoredotonly(field,msg){
	var message = msg.replace('*',field.name);
	trim(field);
    var truth = 1;
	var strPattern = /^([A-Za-z])[A-Za-z0-9\_.\s]*$/i
	truth = strPattern.test(field.value)
	//alert(field.value)
	if (truth == false)
		return msg_focus(field, message)
	return '';
}

// This function will allow only . , / -
function validateaddress(field,msg){
 var message = msg.replace('*',field.name);
 var iChars = "!@#$%^&*()+=[]\\\';{}|\":<>?";

  for (var i = 0; i < field.value.length; i++) {
  	if (iChars.indexOf(field.value.charAt(i)) != -1) {
  		return msg_focus(field, message)
  	}
  }
  return '';
}

//This function will allow only . , - ! _ :
function validatesubject(field,msg){
 var message = msg.replace('*',field.name);
 var iChars = "@#$%^&*()+=[]\\\';{}|\"/<>?";

  for (var i = 0; i < field.value.length; i++) {
  	if (iChars.indexOf(field.value.charAt(i)) != -1) {
  		return msg_focus(field, message)
  	}
  }
  return '';
}




var dtCh= "/";
var minYear=1900;
var maxYear=2100;


//validating a date (fulldate should be in format mm/dd/yyyy) 
function isDate(day,month,year,fulldate,message){
	var dtStr;
	if(fulldate){
		dtStr = fulldate
	}
	else{
		dtStr = month+'/'+day+'/'+year
	}
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.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 message+'\n'
	}
	if (strMonth.length<1 || month<1 || month>12){
		return message+'\n'
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return message+'\n'
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return message+'\n'
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return message+'\n'
	}
	return ''
}

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 toggle(node) {
	var nextDIV = node.nextSibling;
	while(nextDIV.nodeName != "DIV") {
		nextDIV = nextDIV.nextSibling;
	}
	if (nextDIV.style.display == 'none') {
		if (node.childNodes.length > 0) {
			if (node.childNodes.item(0).nodeName == "IMG") {
				node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "minus.gif";
			}
		}
		nextDIV.style.display = 'block';
	}
	else {
		if (node.childNodes.length > 0) {
			if (node.childNodes.item(0).nodeName == "IMG") {
  				node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "plus.gif";
			}
		}
		nextDIV.style.display = 'none';
	}
}

function getImgDirectory(source) {
    return source.substring(0, source.lastIndexOf('/') + 1);
}

//var switcher = 0;
function changeField(frmname, textfieldname, passwordfieldname,switcher) {
	var textfield = document.forms[frmname].elements[textfieldname];
	var passfield = document.forms[frmname].elements[passwordfieldname];

	if (switcher == 0) {
		textfield.style.display = "none"
		passfield.value = ""
		passfield.style.display = "block"
		passfield.focus()
		switcher++
	}

	else {
		if(passfield.value == ""){
			textfield.style.display = "block"
			//textfield.value = "Password"
			passfield.style.display = "none"
			switcher--
		}
	}

}

//This function used to Selected record display in different view...

function highlightstatus(chk,val,status){
		id = 'status'+val;	
		var assignchk = '';			
		if((chk.checked == true && status == 'true') ||(chk.checked == false && status == 'false')){
			var assignchk = 1
		}			
		if(assignchk !=1){				
			document.getElementById(id).className ='highlightmsg';					
		}
		else{ 
			document.getElementById(id).className ='td, tr ';		
		}
		
	}