// JavaScript Document
/*submit form and get cpt */
function gotocpt(frm)
{
	document.basicfrm.getcpt.value= 'Y';
	document.basicfrm.submit();
}


/* for adding colon */
function fnaddColon(inName,e)
{
	var key_code = e.keyCode;  // For a Backspace key_code=8
	
	var strLength = inName.value.length;
	var strValue = inName.value;
	var firstcolon = strValue.indexOf(":"); 

	if(strLength == 2 && firstcolon ==-1 && key_code != 8)
	{
		//alert(firstcolon);
		strValue = strValue+":";
		inName.value = strValue; 
	}
}


function validateTime(tID)
{
	if (document.getElementById(tID).value=='')
	{
		alert('Please enter PACU in (hh : mm) 24 hrs format');
		document.getElementById(tID).focus();
		return false;
	}
	else if (document.getElementById(tID).value!='')
	{
		var txtval = document.getElementById(tID).value;
		var pac = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
		if (!pac.test(txtval))
		{
			alert('Please enter PACU in (hh : mm) 24 hrs format');
			document.getElementById(tID).focus();
			return false;
		}
	}
}

/* for adding slashes */
function fnaddSlash()
{
	var dateLength = document.frmLocaldata.operationdate.value.length;
	var dateValue = document.frmLocaldata.operationdate.value;
	var firstslash = dateValue.indexOf("/");
	var lastslash = dateValue.lastIndexOf("/");
	/*alert(countSlash(dateValue));*/
	if((dateLength == 2) && firstslash ==-1)
	{
		dateValue = dateValue+"/";
		document.frmLocaldata.operationdate.value = dateValue; 
	}
	else if((dateLength == 5) && countSlash(dateValue) <2 )
	{
		dateValue = dateValue+"/";
		document.frmLocaldata.operationdate.value = dateValue; 
	}
	
}

function countSlash(str){
	
	var cnt =0;
	for(var i=0; i<str.length; i++){
		if(str[i] == "/")
			cnt +=1;		
	}	
	return cnt;
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function IsValidTime(field,timeStr,label) {
// Checks if time is in HH:MM:SS AM/PM format.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	if(trim(timeStr) && timeStr.length>1){
		var matchArray = timeStr.match(timePat);
		if (matchArray == null) {
			alert("Time is not in a valid format.");
			//alert("Please complete required fields shown in red");
			field.focus();
			label.addClass("error_message");
			return false;
		}
		hour = matchArray[1];
		minute = matchArray[2];
		second = matchArray[4];
		ampm = matchArray[6];
		
		if (second=="") { second = null; }
		if (ampm=="") { ampm = null }
		
		if (hour < 0  || hour > 23) {
			alert("Hours must be between 0 and 23.");
			//alert("Please complete required fields shown in red");
			//field.value='';
			field.focus();
			label.addClass("error_message");
			return false;
		}
		/*if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
		alert("You must specify AM or PM.");
		return false;
		   }
		}
		if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
		}*/
		if (minute<0 || minute > 59) {
			alert ("Minutes must be between 0 and 59.");
			//alert("Please complete required fields shown in red");
			//field.value='';
			field.focus();
			label.addClass("error_message");
			return false;
		}
		if (second != null && (second < 0 || second > 59)) {
			//alert ("Seconds must be between 0 and 59.");
			alert("Please complete required fields shown in red");
			//field.value='';
			field.focus();
			label.addClass("error_message");
			return false;
		}
		label.removeClass("error_message");
		return true;
	}
	else{
		return true;	
	}
}

