// JavaScript Document

function  checkdate(datval)
			{
		
			
			var datefiled , timefiled, datearr,datval;
		
				datearr = 	datval.value.split(" ");
				
				if(datearr[0]  == ""   || datearr[1] == ""  || datearr.length <2  )
				
				{
					alert("Please Enter Date in mm/dd/yyyy H:i:s Format");
					return false;	
				}
				
			var allowBlank = true;
			var field1;
			var minYear = 1902;
			var maxYear = (new Date()).getFullYear();
			var errorMsg = "";

 		   re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
			field1  = datearr[0];
			if(field1 != '') {
			  if(regs = field1.match(re)) {
			  
			   if(regs[1] < 1 || regs[1] > 12) {
				  errorMsg = "Invalid value for month: " + regs[1];
				} else if(regs[2] < 1 || regs[2] > 31) {
				  errorMsg = "Invalid value for day: " + regs[2];
				} else if(regs[3] < minYear ) {
				  errorMsg = "Invalid value for year: " + regs[3] ;
				}
			  } else {
				errorMsg = "Invalid date format: " + field1;
			  }
			} else if(!allowBlank) {
			  errorMsg = "Empty date not allowed!";
			}
			
			if(errorMsg != "") {
			  alert(errorMsg);
			  datval.focus();
			  return false;
			}
		
				
			 var errorMsg = "";
			 var field = "";
		 re = /^[0-9]{2}:[0-9]{2}:[0-9]{2}$/;
	
		if(datearr[1] != "")
		{
			field =	datearr[1];
			if(field.match(re))
				{
				timearray = field.split(":");
				h = timearray[0];
				i = timearray[1];
				s = timearray[2];
				
							  if(h < 0 || h > 24) {
									errorMsg = "Invalid value for hours: " + h;
							  }
							  
							  if(!errorMsg && i > 59) {
									  errorMsg = "Invalid value for minutes: " + i;
								}
							 if(!errorMsg && s > 59) {
									 errorMsg = "Invalid value for minutes: " + s;
								}
				}
			else
				
				{errorMsg = "Invalid time format: " + field; }
			}
		else
		{
			errorMsg  = "Please Enter Date in mm/dd/yyyy H:i:s Format";
		}

			if(errorMsg != "") {
			  alert(errorMsg);
			  datval.focus();
			  return false;
			}
					
			return true;
			
			}
//REMOVES THE LEADING AND TRAILING BLANK SPACES IN STRING
function trim(inputString) {
	var returnString = inputString;
	var removeChar =' ';

	if (removeChar.length){
	  while(''+returnString.charAt(0)==' ')	{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==' '){
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

//CHECKS FOR A VALID EMAIL....
function isEmail(str) {
  // are regular expressions supported?
  var str1 = trim(str,' ');
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str1.indexOf(".") > 2) && (str1.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str1) && r2.test(str1));
}
function ValidFloat(objFieldValue){
			
	  var validFormatRegExp = /^\d*(\.\d+)?$/;
	
	  var isValid = validFormatRegExp.test(objFieldValue);
	
	  return isValid;
	
}

	
function isFloatKey(evt)
	{
		var charCode = (evt.which) ? evt.which : evt.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45 && charCode != 46 )
		{
			return false;
		}
		
	
	}


function selectbox(ctr,val)
{
	for(var i=0;i<ctr.options.length;++i)
	{
		if(ctr.options[i].value == val)
		{				
			ctr.options[i].selected=true;
			break;
		}
	}
}


function SortField()
{			
		var arrControls = SortField.arguments;
		var arrLen = arrControls.length;
		var frm = eval(arrControls[0])
		var strFld = arrControls[1]
		
		var strUrl;
		var ctr,imgSrc;		
		if(frm.txtCtr.value==""){ctr=0;}else{ctr=frm.txtCtr.value;}
		ctr++; 
		var sortOrder;
		if(ctr%2==1)
		{
			sortOrder="Desc";
			//imgSrc="images/desc.jpg"
		}	
		else{
			sortOrder="Asc";
			//imgSrc="images/asc.jpg"
		}
		if(strFld!=""){
			frm.txtSortFld.value=strFld + " " + sortOrder;
			frm.txtCtr.value=ctr;
			//frm.txtImgSrc.value=imgSrc;
			frm.submit(); 
		}
}

/**
		 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
		 */
		// Declaring valid date character, minimum year and maximum year
		var dtCh= "/";
		var minYear=1900;
		var maxYear=2200;
		
		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 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 isDate(dtStr){			
			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){
				alert("The date format should be : mm/dd/yyyy")
				return false
			}
			
			if (strMonth.length<1 || month<1 || month>12){
				alert("Please enter a valid month")
				return false
			}
			if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
				alert("Please enter a valid day")
				return false
			}
			if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
				alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
				return false
			}
			if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
				alert("Please enter a valid date")
				return false
			}
		return true
		}
		function reDate(dmy){
          var d=dmy.split(/\D+/);
          return new Date(d[2],d[1]-1,d[0]);
     	 }
	
	//CHANGE COLOR OF THE BUTTON
	//Status=1 means change color with new one and Status=0 means change color with old one
		function changeBtnBgcolor(btnObject,Status)
		{
			if(Status==1){
			   document.getElementById(btnObject).style.backgroundColor = "#bb4fd8"; 
			   document.getElementById(btnObject).style.color = "#FFFFFF";
			}else{
			  document.getElementById(btnObject).style.backgroundColor = "#c893de"; 
			  document.getElementById(btnObject).style.color = "";
			}		
		}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}
/// this function is used for sorting the fields in asc and desc manner
function dosort(sortby,sortorder){					
		var qs = window.location.search.toString();
		qs= qs.substr(1);
		var params="";						
		var i;
		if(qs.length > 0 ){
			params = qs.split("&");
			qs = "";
			for(i =0;i<params.length;i++){
				if(params[i].search(/^sortby=(.)*$/i)== -1 && params[i].search(/^sortorder=(.)*$/i)==-1)
					qs = qs + "&"+ params[i];
			}
		}
		window.location.search = "?sortby="+sortby+"&sortorder="+ sortorder + qs;
	}
	