// added on 3rd Nov. 2004
function Close(handle){
	eval(handle.close());
}

// added on 29 October 2004

function ShowElementInFrame(frameName,elementID) {
	if (document.all){
		//alert("document.all supported");
		//top.frames[frameName].document.all(elementID).style.display = "none"
		top.frames[frameName][elementID].style.display = ""
	}	
	else{
		//alert("document.all collection is not supported...");
		top.frames[frameName].document.getElementById(elementID).style.display = "";	
	}
}		

// added on 28 October 2004

function HideElementInFrame(frameName,elementID) {
	if (document.all){
		//alert("document.all supported");
		//top.frames[frameName].document.all(elementID).style.display = "none"
		top.frames[frameName][elementID].style.display = "none"
	}	
	else{
		//alert("document.all collection is not supported...");
		top.frames[frameName].document.getElementById(elementID).style.display = "none";	
	}
}		



// added on 21 October 2004

function ValidateAmount(amount){
	//validate = 	/(^\d+$)/;		
	
	//validate = 	/(\d+[.][0][0]$S)/;		
	validate = 	/(\d+[.][0-9][0-9])/;		
		
	if ((validate.test(amount) > 0))
		return true;
	else
		return false;	
}			

// added on 19th October 2004
function popUp(url,width,height,left,top) {
	sealWin=window.open(url,"win",'x=0,y=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,'+'left='+left+',top='+top+',width='+width+',height='+height);
	self.name = "mainWin";
}

// added on 15th October 2004
function PostForm(frmName,action){
	eval("document.forms[0].action='" + action + "'");
	alert(document.forms[0].action);
	eval("document.forms[0].submit()");
	return false;
}


// added on 15h October 2004
function Back()
{
	history.back();
	// to pervent the browser from posting back.
	return false;
}

// added on 23rd September 2004
// mode = {ICD,CPT}
function ShowLookUpWindow(mode,ctlName){

 //var url = "/PulseNet/PulseTaskNet/Common/CptDiagLookup.aspx?USERNO=DAK&MODE=" + mode + "&FORMNO=STD";
 var url = "/PulseNet/PulseTaskNet/Common/CptDiagLookup.aspx?USERNO=DAK&MODE=" + mode + "&FORMNO=STD";
 var result = window.showModalDialog(url,this,'help: No; status: no; dialogWidth: 900px; dialogHeight: 610px; ');
 
 //alert(result);

if (result!=null){ 
	//alert(result[0]);
	// listArray contains the list of all the codes inclusing delimiter ':' 001.0: CHOLERA DUE TO VIBRI
	var listArray	= result;
	// valueArray contains the list of values for instance 001.0 , CHOLERA DUE TO VIBRI
	var valueArray	= listArray[0].split(':');
	//alert(valueArray[0] + "rizwan" + valueArray[1]);
	eval("document.forms[0]['" + ctlName + "'].value='" + valueArray[0] + "'");
}
else
	alert('result was undefined');
                  //document.Form1.ButtonSelectDiag_result.value = result.join('~');
}

// added on 21 Sep. 2004
function SetValue(formName,ctlName,data){
	
	var s;
	//alert('document.' + formName +'.' + ctlName + '.' + 'checked');
	alert(formName + ':' + ctlName + ':' + data);	
	//var s = eval('document.' + formName +'.' + ctlName + '.value=' + data );
	
	if (document.all)
		s = eval("document.all('" + ctlName + "').value = " + data );
	else
		s = eval("document." + "getElementById('" + ctlName + "').value = " + data);
	
	alert(s);
}

// added on 20th Oct. 2004
function AppendValue(formName,ctlName,data){
	
	var s;
	var oldValue;
	//alert('document.' + formName +'.' + ctlName + '.' + 'checked');
	alert(formName + ':' + ctlName + ':' + data);	
	//var s = eval('document.' + formName +'.' + ctlName + '.value=' + data );
	
	if (document.all)
		oldValue = eval("document.all('" + ctlName + "').value");
	else
		oldValue = eval("document." + "getElementById('" + ctlName + "').value");

	if (document.all)
		s = eval("document.all('" + ctlName + "').value = " + oldValue + data );
	else
		s = eval("document." + "getElementById('" + ctlName + "').value = " + oldValue + data);
	
	alert(s);
}


function Toggle(formName,ctlName,ctlToHide){
	
	//alert('document.' + formName +'.' + ctlName + '.' + 'checked');
		
	if (eval('document.' + formName +'.' + ctlName + '.' + 'checked')){
		eval("document." + "getElementById('" + ctlToHide + "').style.display = 'none'");
	}	
	else{
		eval("document." + "getElementById('" + ctlToHide + "').style.display = 'block'");		
	}		
	
	return true;
}

function ToggleDisplay(formName,ctlName,todo) {
	if (todo == 'no') {
		// if document.all is supported
		if (document.all)
			eval("document.all('" + ctlName + "').style.display = 'none'");
		else
			eval("document." + "getElementById('" + ctlName + "').style.display = 'none'");
	} else {
		if (document.all)
			eval("document.all('" + ctlName + "').style.display = ''");
		else
			eval("document." + "getElementById('" + ctlName + "').style.display = ''");
	}
}

// compares string values in two controls and displays message if they are not same

function match(form,ctl1,ctl2,msg){
	//var ctrlvalue;
	var ctrlName1;
	var ctrlName2;
	
	//ctrlValue = eval('document.' + frmName + '.' + ctrlName + '.value');
	ctrlName1 = 'document.' + form + '.' + ctl1;
	ctrlName2 = 'document.' + form + '.' + ctl2;
					
	if (eval(ctrlName1 + '.value') !=  eval(ctrlName2 + '.value'))
	{
		alert(msg);
		eval(ctrlName2 + '.focus()');
		return false; 
	}		
	return true;		
}

function IsEmptyWithMessage(frmName,ctrlName,lblMessage){
	//var ctrlvalue;
	var ctrlName;
	var frmArray,ctrlArray,msgArray;
	var i;
	
	frmArray	= frmName.split("#");
	ctrlArray	= ctrlName.split("#");
	msgArray	= lblMessage.split("#");

	//ctrlValue = eval('document.' + frmName + '.' + ctrlName + '.value');
	for(i=0;i<frmArray.length;i++)
	{
		ctrlName = 'document.' + frmArray[i] + '.' + ctrlArray[i];
					
		if (eval(ctrlName + '.value') == "" )
		{
			alert(msgArray[i]);
			eval(ctrlName + '.focus()');
			return false; 
		}		
	}
	
	return true;		
}

function redirect(pageName){
	window.open(pageName);
}

function ToggleElementVisibility(frmName,elementID,visibility) {
	
	if (toggle) {
		if (document.all)
			document.all(elementID).style.display = "none";
		else
			document.getElementById(elementID).style.display = "none";
		
		toggle = false;
	} 
	else {
		if (document.all)
			document.all(elementID).style.display = "";
		else
			document.getElementById(elementID).style.display = "";
		
		toggle = true;
	}
}

function HideElement(frmName,elementID) {
	
	//alert(elementID);
	
	if (document.getElementById(elementID) != null)
	{
		if (document.all)
			document.all(elementID).style.display = "none";	
		else
			document.getElementById(elementID).style.display = "none";
	}				
}



function ShowElement(frmName,elementID) {
	
	if (document.all)
		document.all(elementID).style.display = "";	
	else
		document.getElementById(elementID).style.display = "";

}

function IsEmpty1(frmName,ctrlName){
	//var ctrlvalue;
	
	var ctrlName;
	var frmArray,ctrlArray,msgArray;
	var i;
	
	frmArray	= frmName.split("#");
	ctrlArray	= ctrlName.split("#");
	
	//msgArray	= lblMessage.split("#");
	//ctrlValue = eval('document.' + frmName + '.' + ctrlName + '.value');
	
	for(i=0;i<frmArray.length;i++)
	{
		ctrlName = 'document.' + frmArray[i] + "['" + ctrlArray[i] + "']";
					
		if (eval(ctrlName + '.value') == "" )
			return true; 
	}
	
	return false;		
}

function IsNumericWithMessage(frmName,ctrlName,lblMessage)
{
	var ctrlName;
	var frmArray,ctrlArray,msgArray;
	var i;
	var validate;
	
	frmArray	= frmName.split("#");
	ctrlArray	= ctrlName.split("#");
	msgArray	= lblMessage.split("#");
	
	for(i=0;i<frmArray.length;i++)
	{
		ctrlName = 'document.' + frmArray[i] + '.' + ctrlArray[i];
		
		validate = 	/(^\d+$)/;		
		
		if ((validate.test(eval(ctrlName + '.value'))) && (eval(ctrlName + '.value') > 0))
			return true;
		else
		{
			alert(msgArray[i]);
			eval(ctrlName + '.focus()');
			return false;			
		}
	}
}

function IsNumeric(frmName,ctrlName)
{
	var ctrlName;
	var frmArray,ctrlArray,msgArray;
	var i;
	var validate;
	
	frmArray	= frmName.split("#");
	ctrlArray	= ctrlName.split("#");
	//msgArray	= lblMessage.split("#");
	
	for(i=0;i<frmArray.length;i++)
	{
		ctrlName = "document." + frmArray[i] + "['" + ctrlArray[i] + "']";
		
		validate = 	/(^\d+$)/;		
		
		if ((validate.test(eval(ctrlName + '.value'))) && (eval(ctrlName + '.value') > 0))
			return true;
		else
			return false;			
	}
}

function Verify()
{
	var len,value,ctlName;
	len = document.Form1.elements.length;
				
	var returnValue,arr,arr1;
	
	frmName = 'Form1';
		
	// do empty validation
	if (document.Form1.elements["_EMPTY_LIST"].value != ""){
		arr = document.Form1.elements["_EMPTY_LIST"].value.split(",");
		
		for(i=0;i<arr.length;i++){
			arr1 = arr[i].split("#")
			returnValue = IsEmpty1('Form1',arr1[0]);
			if (returnValue == true){
				alert(arr1[1]);
				//eval('document.' + frmName + '.' + arr1[0] + '.focus()');
				eval("document." + frmName + "['" + arr1[0] + "'].focus()");
				return false;
			}	
		}	
	}
		
	// do numeric validation
	if (document.Form1.elements["_NUMERIC_LIST"].value != ""){
			
		arr = document.Form1.elements["_NUMERIC_LIST"].value.split(",");
	
		for(i=0;i<arr.length;i++){
			arr1 = arr[i].split("#");
						
			if (IsEmpty1('Form1',arr1[0]) == false)
				returnValue = IsNumeric('Form1',arr1[0]);
				
			if (returnValue == false){
				alert(arr1[1]);
				eval("document." + frmName + "['" + arr1[0] + "'].focus()");
				eval("document." + frmName + "['" + arr1[0] + "'].select()");
				return false;
			}	
		}
	}
		
	// do date validation 
	if (document.Form1.elements["_DATE_LIST"].value != ""){
		arr = document.Form1.elements["_DATE_LIST"].value.split(",");
		
		for(i=0;i<arr.length;i++){
			arr1 = arr[i].split("#")
		
			value = eval("document." + frmName + "['" + arr1[0] + "'].value");
			if (IsEmpty1('Form1',arr1[0]) == false)
				returnValue = isValidDate(value,false);
								
			if (returnValue == false){
				// display the message and set the focus
				alert(arr1[1]);
				//eval('document.' + frmName + '.' + arr1[0] + '.focus()');
				eval("document." + frmName + "['" + arr1[0] + "'].focus()");
				return false;
			}
		}
	}
				
	return true;
}

/*
function RegularExpressionValidatorEvaluateIsValid(frmName,ctlName,lblMessage){
    var value;
    value = eval('document.' + frmName + '.' + ctlName + '.value');
            
    if (ValidatorTrim(value).length == 0)
        return true;
    
    var rx = new RegExp("\d{2}-\d{2}-\d{4}");
    //var rx = new RegExp("[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]");
    var matches = rx.exec(value);
       
    return (matches != null && value == matches[0]);
}

function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function GetFullYear(year) {
	return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
}
*/

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function isValidDate(d,convert) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intDay;
	var intMonth;
	var intYear;
	var booFound = false;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	strDate = d;
	
	
	if (strDate.length < 1) {
		return false;
	}
	
	if (strDate.toLowerCase()=="today" || strDate.toLowerCase()=="now"){return true;}
	
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
	
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
					
			if (strDateArray.length != 3) 
			{
				err = 1;
				return false;
			}
			else 
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
		else
			return false;
	}
	
	// verify year part	2 or 4 digits
	if (strYear.length != 2 && strYear.length != 4) {return false;}
	if (isNaN(strYear)){return false;}
	// US style (swap month and day)
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}

	// verify 1 or 2 digit integer day
	if (strDay.length<1 || strDay.length>2) {return false;}
	if (isNaN(strDay)){return false;}
	
	// month may be digits of characters, hence following check
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}

	intDay=parseInt(strDay,10);
	intYear = parseInt(strYear, 10);
	
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	
	// day in month check
	if (intDay < 1 || intDay > 31){return false;}
		
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) {
		return false;
	}
	if (intMonth == 2) {
		if (LeapYear(intYear)) {
			if (intDay > 29) {return false;}
		}
		else 
		{
			if (intDay > 28) {return false;}
		}
	}
	
	if (!convert)
		return true;
	else
	{
		if (intYear<=99){intYear=intYear+2000;}
		return intDay+"/"+intMonth+"/"+intYear;
	}
}
