
/**************************************************
* Common js utils
***************************************************/


/**************************************************
* Change the LefttabFocus on control arrows
***************************************************/
function changeLeftTabFocus(obj,selTabValue,c)
{
var str;
switch (selTabValue)
		{
		
		case "Administrative":
			if (c==40)
			{
			/*	obj.selTab.value="NewPatient";
				obj.LeftTabNav_btnNewPatient.focus();*/
				obj.selTab.value="SearchComplete";
				obj.LeftTabNav_btnSearchComplete.focus();
				
			}
			else
			{
				/*obj.selTab.value="DataCollectionComplete";
				obj.LeftTabNav_btnDataCollectionComplete.focus();*/
				obj.selTab.value="DischargeMedication";
				if (obj['LeftTabNav_btnDischargeMedication'] != null)
					obj.LeftTabNav_btnDischargeMedication.focus();
				else
					changeLeftTabFocus(obj,"DischargeMedication",c);
			}
			break;
			
		case "SearchComplete":
			if (c==40)
			{
				/*obj.selTab.value="Admissions"
				obj.LeftTabNav_btnAdmissions.focus();*/
				obj.selTab.value="NewPatient";
				obj.LeftTabNav_btnNewPatient.focus();
			}
			else
			{
				/*obj.selTab.value="ViewInComplete"
				obj.LeftTabNav_btnViewInComplete.focus();*/
				obj.selTab.value="Administrative";
				obj.LeftTabNav_btnAdministrative.focus();
			}
			break;
			
		case "NewPatient":
			if (c==40)
			{
			/*	obj.selTab.value="ViewInComplete"
				obj.LeftTabNav_btnViewInComplete.focus();*/
				obj.selTab.value="Admissions";
				obj.LeftTabNav_btnAdmissions.focus();
			}
			else
			{
				/*obj.selTab.value="Administrative"
				obj.LeftTabNav_btnAdministrative.focus();*/
				obj.selTab.value="SearchComplete";
				obj.LeftTabNav_btnSearchComplete.focus();
			}
			break;
			
		case "ViewInComplete":
			if (c==40)
			{
				obj.selTab.value="SearchComplete";
				obj.LeftTabNav_btnSearchComplete.focus();
			}
			else
			{
				obj.selTab.value="NewPatient";
				obj.LeftTabNav_btnNewPatient.focus();
			}
			break;
		
	

		case "Admissions":
			if (c==40)
			{
				obj.selTab.value="AdmitMedications";
				if (obj['LeftTabNav_btnAdmitMedications'] != null)
					obj.LeftTabNav_btnAdmitMedications.focus();
				else
					changeLeftTabFocus(obj,"AdmitMedications",c);
			}
			else
			{
			/*	obj.selTab.value="SearchComplete"
				obj.LeftTabNav_btnSearchComplete.focus();*/
				obj.selTab.value="NewPatient";
				obj.LeftTabNav_btnNewPatient.focus();
			}
			break;
		
		case "AdmitMedications":
			if (c==40)
			{
				obj.selTab.value="CathLabVisit";
				obj.selLabVisit.value=convert2Int(0);
				obj.LeftTabNav_btnCathLabVisit_0.focus();
				
			}
			else
			{
				obj.selTab.value="Admissions";
				obj.LeftTabNav_btnAdmissions.focus();
			}
			break;
		
	
			
		case "CathLabVisit":
			if (c==40)
			{
				obj.selTab.value="DiagnosticCath";
				str='LeftTabNav_btnDiagnosticCath_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
				{
					str='LeftTabNav_btnPCIProcedure_' + obj.selLabVisit.value;
					if (obj[str] != null)
						obj[str].focus();
					else
						changeLeftTabFocus(obj,"AdverseOutcomes",c);
					
				}
			}
			else
			{	
			if (convert2Int(obj.selLabVisit.value) > 0) 
				{
					obj.selLabVisit.value=convert2Int(obj.selLabVisit.value)-1;
					str='LeftTabNav_btnAdverseOutcomes_' + obj.selLabVisit.value;
					obj.selTab.value="AdverseOutcomes";
					if (obj[str] != null)
						obj[str].focus();
					else
						changeLeftTabFocus(obj,"PCIProcedure",c);
					}
				else
				{
				obj.selTab.value="AdmitMedications";
				if (obj['LeftTabNav_btnAdmitMedications'] != null)
					obj.LeftTabNav_btnAdmitMedications.focus();
				else
					changeLeftTabFocus(obj,"AdmitMedications",c);
				}	
				
			}
			break;
			
		case "DiagnosticCath":
			if (c==40)
			{
				obj.selTab.value="PCIProcedure";
				
				str='LeftTabNav_btnPCIProcedure_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
					changeLeftTabFocus(obj,"AdverseOutcomes",c);
				
			}
			else
			{	
				obj.selTab.value="CathLabVisit";
				str='LeftTabNav_btnCathLabVisit_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
					changeLeftTabFocus(obj,"CathLabVisit",c);
				
			}
			break;		
			
		case "PCIProcedure":
			if (c==40)
			{
				//obj.selTab.value="AdverseOutcomes"
				obj.selTab.value="Lesions";
				//obj.LeftTabNav_btnAdverseOutcomes_0.focus();
				//str='LeftTabNav_btnAdverseOutcomes_' + obj.selLabVisit.value;
				str='LeftTabNav_btnLesions_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
			}
			else
			{	
				obj.selTab.value="DiagnosticCath";
				//obj.LeftTabNav_btnDiagnosticCath_0.focus();
				str='LeftTabNav_btnDiagnosticCath_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
					changeLeftTabFocus(obj,"DiagnosticCath",c);
			}
			break	
		case "Lesions":
			if (c==40)
			{
				obj.selTab.value="AdverseOutcomes";
				//obj.LeftTabNav_btnAdverseOutcomes_0.focus();
				str='LeftTabNav_btnAdverseOutcomes_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
			}
			else
			{	
				obj.selTab.value="PCIProcedure";
				//obj.LeftTabNav_btnDiagnosticCath_0.focus();
				str='LeftTabNav_btnPCIProcedure_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
					changeLeftTabFocus(obj,"PCIProcedure",c);
			}
			break;	
		
		
		case "AdverseOutcomes":
			if (c==40)
			{
				if (nVisitCount > convert2Int(obj.selLabVisit.value))
					{
					obj.selLabVisit.value=convert2Int(obj.selLabVisit.value)+1;
					obj.selTab.value="CathLabVisit";
					str='LeftTabNav_btnCathLabVisit_' + obj.selLabVisit.value;
					if (obj[str] != null)
					obj[str].focus();
					}
				else
					{
						obj.selTab.value="Discharge";
						obj.LeftTabNav_btnDischarge.focus();
					}
			}
			else
			{	
				/*obj.selTab.value="PCIProcedure"
				str='LeftTabNav_btnPCIProcedure_' + obj.selLabVisit.value;
				if (obj[str] != null)
				obj[str].focus();
				else
				changeLeftTabFocus(obj,"PCIProcedure",c)*/
				obj.selTab.value="Lesions";
				str='LeftTabNav_btnLesions_' + obj.selLabVisit.value;
				if (obj[str] != null)
					obj[str].focus();
				else
					changeLeftTabFocus(obj,"Lesions",c);
			}
			break;			
		
		case "Discharge":
			if (c==40)
			{
				obj.selTab.value="DischargeMedication";
				if (obj['LeftTabNav_btnDischargeMedication'] != null)
					obj.LeftTabNav_btnDischargeMedication.focus();
				else
					changeLeftTabFocus(obj,"DischargeMedication",c);
					
			}
			else
			{	
				obj.selTab.value="AdverseOutcomes";
				//obj.LeftTabNav_btnAdverseOutcomes_0.focus();
				obj.selLabVisit.value=nVisitCount;
				str='LeftTabNav_btnAdverseOutcomes_' + nVisitCount;
				if (obj[str] != null)
				obj[str].focus();
				else
				changeLeftTabFocus(obj,"AdverseOutcomes",c);
			}
			break;
			
		case "DischargeMedication":
			if (c==40)
			{
				obj.selTab.value="DataCollectionComplete";
				obj.LeftTabNav_btnDataCollectionComplete.focus();
			}
			else
			{	
				obj.selTab.value="Discharge";
				obj.LeftTabNav_btnDischarge.focus();
			}
			break;	
			
		case "DataCollectionComplete":
			if (c==40)
			{
				obj.selTab.value="Administrative";
				obj.LeftTabNav_btnAdministrative.focus();
			}
			else
			{	
				obj.selTab.value="DischargeMedication";
				if (obj['LeftTabNav_btnDischargeMedication'] != null)
					obj.LeftTabNav_btnDischargeMedication.focus();
				else
					changeLeftTabFocus(obj,"DischargeMedication",c)
			}
			break;	
				
		default:
			break;
			
		}
}


/**************************************************
* Change the ICD LefttabFocus on control arrows
***************************************************/
function changeICDLeftTabFocus(obj,selTabValue,c)
{
var str;

switch (selTabValue)
		{
		
		case "Administrative":
			if (c==40)
			{
				obj.selTab.value="SearchInComplete";
				obj.LeftTabNav_btnSearchInComplete.focus();
				
			}
			else
			{
				obj.selTab.value="FinalizeAdmission";
				obj.LeftTabNav_btnFinalizeAdmission.focus();		
			}
			break
		
		case "SearchInComplete":
			if (c==40)
			{
				obj.selTab.value="SearchComplete";
				obj.LeftTabNav_btnSearchComplete.focus();
			}
			else
			{
				obj.selTab.value="Administrative";
				obj.LeftTabNav_btnAdministrative.focus();
			}
			break
			
		case "SearchComplete":
			if (c==40)
			{
				obj.selTab.value="PatientManagement";
				obj.LeftTabNav_btnPatientManagement.focus();
			}
			else
			{
				obj.selTab.value="SearchInComplete";
				obj.LeftTabNav_btnSearchInComplete.focus();
			}
			break
	
		case "PatientManagement":
			if (c==40)
			{
				obj.selTab.value="Admission";
				obj.LeftTabNav_btnAdmission.focus();
			}
			else
			{
				obj.selTab.value="SearchComplete";
				obj.LeftTabNav_btnSearchComplete.focus();
			}
			break
			
		case "Admission":
			if (c==40)
			{
				obj.selTab.value="ImplantInformation";
				obj.LeftTabNav_btnImplantInformation.focus();
			}
			else
			{
				obj.selTab.value="PatientManagement";
				obj.LeftTabNav_btnPatientManagement.focus();
			}
			break
		
		case "ImplantInformation":
			if (c==40)
			{
				obj.selTab.value="AdverseEvents";
				obj.LeftTabNav_btnAdverseEvents.focus();
				
			}
			else
			{
				obj.selTab.value="Admission";
				obj.LeftTabNav_btnAdmission.focus();
			}
			break
		
	
			
		case "AdverseEvents":
			if (c==40)
			{
				obj.selTab.value="Discharge";
				obj.LeftTabNav_btnDischarge.focus();
			}
			else
			{	
				obj.selTab.value="ImplantInformation";
				obj.LeftTabNav_btnImplantInformation.focus();	
			}
			break;
			
		case "Discharge":
			if (c==40)
			{
				obj.selTab.value="Medications";
				obj.LeftTabNav_btnMedications.focus();				
			}
			else
			{	
				obj.selTab.value="AdverseEvents";
				obj.LeftTabNav_btnAdverseEvents.focus();
			}
			break		
			
		case "Medications":
			if (c==40)
			{
				obj.selTab.value="FinalizeAdmission";
				obj.LeftTabNav_btnFinalizeAdmission.focus();
			}
			else
			{	
				obj.selTab.value="Discharge";
				obj.LeftTabNav_btnDischarge.focus();
			}
		break	
		case "FinalizeAdmission":
			if (c==40)
			{
				obj.selTab.value="Administrative";
				obj.LeftTabNav_btnAdministrative.focus();
			}
			else
			{
				obj.selTab.value="Medications";
				obj.LeftTabNav_btnMedications.focus();
			}
		break
			
				
		default:
			break
			
		}
}
/********************************************************************
* Convert the value in to integer
*********************************************************************/
function convert2Int(val){
return val-0
}

/**************************************************
* Disables the textbox
***************************************************/
function DisableTextBox(obj)
{
	obj.value="";
	obj.className='disable';
	obj.disabled=true;
}

function EnableTextBox(obj)
{
	obj.className='enable';
	obj.disabled=false;
}

function DisableFormControl(obj)
{
	obj.value="";
	obj.className='disablecontrol';
	obj.disabled=true;
}
function DisableFormRBControl(obj)
{
	obj.checked=false;
	obj.className='disablecontrol';
	obj.disabled=true;
}

function EnableFormRBControl(obj)
{
	obj.className='enablecontrol';
	obj.disabled=false;
}

function EnableFormControl(obj)
{
	obj.className='enablecontrol';
	
	obj.disabled=false;
}

function resetform(obj)
{
	obj.reset;
}

function DisableButton(obj)
{
	obj.className='disable';
	obj.disabled=true;
}

function EnableButton(obj)
{
	obj.className='enablebtn';
	obj.disabled=false;
}

function DisableLinkButton(obj)
{
	obj.className='disableLinkBtn';
	obj.disabled=true;
}

function EnableLinkButton(obj)
{
	obj.className='enableLinkBtn';
	obj.disabled=false;
}

function checkdate2(obj)
{
		var str;
		var strOutput;
		str=obj.value;
		str=str.replace("/","")
		str=str.replace("/","")
	
		var i;
		strOutput=""
		for (i = 0; i <= str.length; i++)
			{
				if (i==2 || i==4)
					strOutput =  strOutput + "/";
					
				strOutput = strOutput + str.substring(i, i+1) ;
			}
		
		obj.value=strOutput;
		
}
/*
Checks the Date for 8 digits and converts it to the standard MM/DD/YYYY format
else if the date is contains 2 slashes with a valid date then also it accepts it.
*/
function checkdate(obj)
{
		var str;
		var strMonth;
		var strDate;
		var strYear;
		var nMonth;
		var nDate;
		var nYear;
		var bDate;
		bDate=true;
		var i;
		var arrString;

		str=obj.value;		
		
		if (str == "") 
		{
			bDate=true;
			strMonth="";
			strDate="";
			strYear="";
		}
		
		if (str.length > 1 && str.length < 8)
		{
			
			bDate=false;
		}
		else
		{	
			arrString=str.split("/");
			
			if (arrString.length > 2)
			{
				strMonth=arrString[0];
				strDate=arrString[1];
				strYear=arrString[2];
			
				if (strYear.length != 4)
					bDate=false;
				
				nMonth=convert2Int(strMonth);
				nDate=convert2Int(strDate);
				nYear=convert2Int(strYear);
				if (strMonth.length==1)
				  strMonth='0' + strMonth;
				
				if (strDate.length==1)
				  strDate='0' + strDate;
				
				if (strYear.length==1)
				  strYear='0' + strYear;
			}
			else
			{
				str=str.replace("/","")
				str=str.replace("/","")
				i = str.length;
			
				if ((i < 8  && i >= 1) || i > 8 )
					bDate=false;
				else if(i==8)
				{	
					strYear=str.substring(i-4, i);
					if (strYear.length != 4)
						bDate=false;
				
					if (i==8)
					{
						strDate=str.substring(i-6, i-4);
						strMonth=str.substring(i-8, i-6);
						nMonth=convert2Int(strMonth);
						nDate=convert2Int(strDate);
						nYear=convert2Int(strYear);
					}
				}
			}
	
			 if (parseInt(strYear) <1850)
	            bDate=false;
	   		if (nMonth >12 || nMonth <1)
				bDate=false;
		
			if (nDate > 31 || nDate < 1)
				bDate=false;
			
			if (nMonth==2)
			{
				if (nYear == Math.round(nYear / 4) * 4)
					{
						//leap year
						if (nDate > 29)
							{
								alert("This Date does not exist. Please enter a valid date.");
								return false;
							}
					}
				else
				{
					//non-leap year
						if (nDate > 28)
						{
								alert("This Date does not exist. Please enter a valid date.");
								return false;
						}
					}
			}
			if (bDate==true)
			{
			//Set the two dates
				var ActualTime=new Date(nYear,nMonth-1,nDate) //Month is 0-11 in JavaScript
		
				//Set 1 day in milliseconds
				var one_day=1000*60*60*24
				var today=new Date();
				var DateDiff;
				DateDiff=Math.ceil((today.getTime()-ActualTime.getTime())/(one_day));
			
				if(DateDiff <=0) 
					{
					alert("This Date should be less than or equal to the Current Date.");
					obj.focus();
					}
			}
			
		}
			
			if (bDate==false)
				{
					alert("Invalid date.Date should be in MM/DD/YYYY format");
					//obj.value="";
					obj.focus();
				}
			else
			 {
			 	if (strMonth !="" || strDate !=""  || strYear !="" )
					obj.value=strMonth + "/" + strDate + "/"  + strYear;
				else
					obj.value="";
			 }
				
		
}

/*
Checks the SSN for 9 digits and converts it to the standard XXX-XX-XXXX format
*/
function checkssnsize(obj)
	{
		var str;
		var strOutput;
		str=obj.value;
		str=str.replace("-","")
		str=str.replace("-","")
		str=str.replace(" ","")
		str=str.replace(" ","")
		var i;
		strOutput=""
		if (str.length < 9 && str.length >= 1 )
		{
			alert("Invalid SSN");
			obj.value=""
			obj.focus();
		}
		else if(str.length==9)
		{
			for (i = 0; i <= str.length; i++)
				{
					if (i==3 || i==5)
						strOutput =  strOutput + "-";
						
					strOutput = strOutput + str.substring(i, i+1) ;
				}
			
				obj.value=strOutput;
		}
	}
function checkTime2(obj)
{
	var str;
	var strOutput;
	str=obj.value;
	// regular expression to match required time format
    re = /^(\d{1,2}):(\d{2})([ap]m)?$/;
	
   if(str != '') 
    {
	 if(regs = str.match(re)) 
      {
		if(regs[3])
        {
          if(regs[1] < 1 || regs[1] > 12)
          {
            alert("Invalid value for hours: " + regs[1]);
            obj.focus();
            return false;
          }
        } 
        else 
        {
          if(regs[1] > 23) 
          {
            alert("Invalid value for hours: " + regs[1]);
            obj.focus();
            return false;      
          }
        }
        if(regs[2] > 59)
        {
          alert("Invalid value for minutes: " + regs[2]);
		  obj.focus();
          return false;      
        }
     }
    else
    {
        alert("Invalid time format.  Time should be in hh:mm format: " + str);
        obj.focus();
        return false;
     }
}
}
function checkTime(obj)
	{
	var str;
		var strOutput;
		str=obj.value;
		str=str.replace(":","")
		var i;
		strOutput=""
		if (str.length < 4 && str.length >= 1 )
		{
			alert("Invalid Time.Time should be in hh:mm format");
			obj.value=""
			obj.focus();
		}
		else if (str.length > 4)
		{
			alert("Invalid Time.Time should be in hh:mm format");
			obj.value=""
			obj.focus();
		}
		else if (str.length == 4)
		{
			for (i = 0; i <= str.length; i++)
			{
				if (i==2)
					strOutput =  strOutput + ":";
					
					strOutput = strOutput + str.substring(i, i+1) ;
				}
			
				obj.value=strOutput;
		
		}
		
	}
	
function popHelp(link) 
	    {
			window.status="Get Help for the Field";
    		winFieldHelp = window.open(link, "winFieldHelp", "width=600,height=400,top=0,left=0,screenX=10,screenY=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,dependent=no");
    		winFieldHelp.focus();
	    }	

function popPageHelp(link) 
	    {
	   		winFieldHelp = window.open(link, "winPageHelp", "width=800,height=300,top=0,left=0,screenX=10,screenY=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,dependent=no");
    		winFieldHelp.focus();
	    }

	function popPageDCHelp(u,n,w,h,p) 
	    {
	    window.status="Get Help for the Field";
    		winFieldHelp = window.open(u, "winFieldHelp", "width=600,height=400,top=0,left=0,screenX=10,screenY=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,dependent=no");
    		winFieldHelp.focus();
	  /* 
	    var a,j,k,x,y,f='';if(!n){n='';}if(w){f+='width='+w+',';}if(h){f+='height='+h+',';}

		if(p){p=p.split(':');if(p[0]!='z'){p[0]=='c'?(x=(screen.width-w)/2):x=p[0];f+='left='+x+',';}

		if(p[1]!='z'){if(p[0]=='c'){y=(screen.height-h-p[1])/2;
		
		if(navigator.appName.indexOf('Op')!=-1)
			{
				y-=96;y=y<0?0:y;
			}
		}
		else{y=p[1];}f+='top='+y+',';}}a=arguments.length;if(a>5){for (k=5;k<a;k++){

		switch(arguments[k]){case 'all':f+='toolbar,menubar,location,scrollbars,status,resizable,';break;

	  case 't':f+='toolbar,';break; case 'm':f+='menubar,';break;case 'l':f+='location,';break;

		case 'sc':f+='scrollbars,';break;case 's':f+='status,';break;case 'r':f+='resizable,';}}}
	
	  if(f.charAt(f.length-1)==','){f=f.slice(0,-1);}j=window.open(u,n,f);j.focus();

  return false;
*/
}


function popPageHelpWithButtons(link) 
	    {
    		winFieldHelp = window.open(link, "winPageHelp", "width=800,height=300,top=0,left=0,screenX=10,screenY=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,dependent=no");
    		winFieldHelp.focus();
	    }
function popUPINSearch(link) 
	    {
    		winFieldHelp = window.open(link, "winUPINSearch", "top=0,left=0,screenX=10,screenY=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,dependent=no");
    		winFieldHelp.focus();
	    }	

function WinHelpStatus()
		{
			window.status="Get Help for the Field";
		}

function ClearHelpStatus()
		{
			window.status=" ";
		}
		
function CollapseDDL(obj)		
{
	obj.size=1;
}
function ExpandDDL(obj,size)		
{
	obj.size=convert2Int(size);
}
/*Disable Validators */

function ValidatorUpdateDisplay(val) {
    if (typeof(val.display) == "string") {    
        if (val.display == "None") {
            return;
        }
        if (val.display == "Dynamic") {
            val.style.display = val.isvalid ? "none" : "inline";
            return;
        }
    }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
}
function ValidatorUpdateIsValid() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        if (!Page_Validators[i].isvalid) {
            Page_IsValid = false;
            return;
        }
   }
   Page_IsValid = true;
}
function ValidatorHookupControlID(controlID, val) {
    if (typeof(controlID) != "string") {
        return;
    }
    var ctrl = document.all[controlID];
    if (typeof(ctrl) != "undefined") {
        ValidatorHookupControl(ctrl, val);
    }
    else {
        val.isvalid = true;
        val.enabled = false;
    }
}
function ValidatorHookupControl(control, val) {
    if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number") {
        var i;
        for (i = 0; i < control.length; i++) {
            var inner = control[i];
            if (typeof(inner.value) == "string") {
                ValidatorHookupControl(inner, val);
            } 
        }
        return;
    }
    else if (control.tagName != "INPUT" && control.tagName != "TEXTAREA" && control.tagName != "SELECT") {
        var i;
        for (i = 0; i < control.children.length; i++) {
            ValidatorHookupControl(control.children[i], val);
        }
        return;
    }
    else {
        if (typeof(control.Validators) == "undefined") {
            control.Validators = new Array;
            var ev;
            if (control.type == "radio") {
                ev = control.onclick;
            } else {
                ev = control.onchange;
            }
            if (typeof(ev) == "function" ) {            
                ev = ev.toString();
                ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
            }
            else {
                ev = "";
            }
            var func = new Function("ValidatorOnChange(); " + ev);
            if (control.type == "radio") {
                control.onclick = func;
            } else {            
                control.onchange = func;
            }
        }
        control.Validators[control.Validators.length] = val;
    }    
}
function ValidatorGetValue(id) {
    var control;
    control = document.all[id];
    if (typeof(control.value) == "string") {
        return control.value;
    }
    if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number") {
        var j;
        for (j=0; j < control.length; j++) {
            var inner = control[j];
            if (typeof(inner.value) == "string" && (inner.type != "radio" || inner.status == true)) {
                return inner.value;
            }
        }
    }
    else {
        return ValidatorGetValueRecursive(control);
    }
    return "";
}
function ValidatorGetValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.status == true)) {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.children.length; i++) {
        val = ValidatorGetValueRecursive(control.children[i]);
        if (val != "") return val;
    }
    return "";
}


function ValidatorEnable(val, enable) {

    val.enabled = (enable != false);
    ValidatorValidate(val);
    ValidatorUpdateIsValid();
}
function ValidatorOnChange() {
    var vals = event.srcElement.Validators;
    var i;
    for (i = 0; i < vals.length; i++) {
        ValidatorValidate(vals[i]);
    }
    ValidatorUpdateIsValid();    
}
function ValidatorValidate(val) {    
    val.isvalid = true;
    if (val.enabled != false) {
        if (typeof(val.evaluationfunction) == "function") {
            val.isvalid = val.evaluationfunction(val); 
        }
    }
    ValidatorUpdateDisplay(val);
}
function ValidatorOnLoad() {
    if (typeof(Page_Validators) == "undefined")
        return;
    var i, val;
    for (i = 0; i < Page_Validators.length; i++) {
        val = Page_Validators[i];
        if (typeof(val.evaluationfunction) == "string") {
            eval("val.evaluationfunction = " + val.evaluationfunction + ";");
        }
        if (typeof(val.isvalid) == "string") {
            if (val.isvalid == "False") {
                val.isvalid = false;                                
                Page_IsValid = false;
            } 
            else {
                val.isvalid = true;
            }
        } else {
            val.isvalid = true;
        }
        if (typeof(val.enabled) == "string") {
            val.enabled = (val.enabled != "False");
        }
        ValidatorHookupControlID(val.controltovalidate, val);
        ValidatorHookupControlID(val.controlhookup, val);
    }
    Page_ValidationActive = true;
}
function ValidatorConvert(op, dataType, val) {
    function GetFullYear(year) {
        return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    }
    var num, cleanInput, m, exp;
    if (dataType == "Integer") {
        exp = /^\s*[-\+]?\d+\s*$/;
        if (op.match(exp) == null) 
            return null;
        num = parseInt(op, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType == "Double") {
        exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + val.decimalchar + "(\\d+))?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    } 
    else if (dataType == "Currency") {
        exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + val.groupchar + ")*)(\\d+)"
                        + ((val.digits > 0) ? "(\\" + val.decimalchar + "(\\d{1," + val.digits + "}))?" : "")
                        + "\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        var intermed = m[2] + m[5] ;
        cleanInput = m[1] + intermed.replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    }
    else if (dataType == "Date") {
        var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
        m = op.match(yearFirstExp);
        var day, month, year;
        if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.dateorder == "ymd"){
                return null;		
            }						
            var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
            m = op.match(yearLastExp);
            if (m == null) {
                return null;
            }
            if (val.dateorder == "mdy") {
                day = m[3];
                month = m[1];
            }
            else {
                day = m[1];
                month = m[3];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    }
    else {
        return op.toString();
    }
}
function ValidatorCompare(operand1, operand2, operator, val) {
    var dataType = val.type;
    var op1, op2;
    if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
        return false;    
    if (operator == "DataTypeCheck")
        return true;
    if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
        return true;
    switch (operator) {
        case "NotEqual":
            return (op1 != op2);
        case "GreaterThan":
            return (op1 > op2);
        case "GreaterThanEqual":
            return (op1 >= op2);
        case "LessThan":
            return (op1 < op2);
        case "LessThanEqual":
            return (op1 <= op2);
        default:
            return (op1 == op2);            
    }
}
function CompareValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    var compareTo = "";
    if (null == document.all[val.controltocompare]) {
        if (typeof(val.valuetocompare) == "string") {
            compareTo = val.valuetocompare;
        }
    }
    else {
        compareTo = ValidatorGetValue(val.controltocompare);
    }
    return ValidatorCompare(value, compareTo, val.operator, val);
}
function CustomValidatorEvaluateIsValid(val) {
    var value = "";
    if (typeof(val.controltovalidate) == "string") {
        value = ValidatorGetValue(val.controltovalidate);
        if (ValidatorTrim(value).length == 0)
            return true;
    }
    var args = { Value:value, IsValid:true };
    if (typeof(val.clientvalidationfunction) == "string") {
        eval(val.clientvalidationfunction + "(val, args) ;");
    }        
    return args.IsValid;
}
function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;        
    var rx = new RegExp(val.validationexpression);
    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 RequiredFieldValidatorEvaluateIsValid(val) {
    return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))
}
function RangeValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0) 
        return true;
    return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
            ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
}
function ValidationSummaryOnSubmit() {
    if (typeof(Page_ValidationSummaries) == "undefined") 
        return;
    var summary, sums, s;
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";
        if (!Page_IsValid) {
            if (summary.showsummary != "False") {
                summary.style.display = "";
                if (typeof(summary.displaymode) != "string") {
                    summary.displaymode = "BulletList";
                }
                switch (summary.displaymode) {
                    case "List":
                        headerSep = "<br>";
                        first = "";
                        pre = "";
                        post = "<br>";
                        final = "";
                        break;
                    case "BulletList":
                    default: 
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        final = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        final = "<br>";
                        break;
                }
                s = "";
                if (typeof(summary.headertext) == "string") {
                    s += summary.headertext + headerSep;
                }
                s += first;
                for (i=0; i<Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                        s += pre + Page_Validators[i].errormessage + post;
                    }
                }   
                s += final;
                summary.innerHTML = s; 
                window.scrollTo(0,0);
            }
            if (summary.showmessagebox == "True") {
                s = "";
                if (typeof(summary.headertext) == "string") {
                    s += summary.headertext + "<BR>";
                }
                for (i=0; i<Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                        switch (summary.displaymode) {
                            case "List":
                                s += Page_Validators[i].errormessage + "<BR>";
                                break;
                            case "BulletList":
                            default: 
                                s += "  - " + Page_Validators[i].errormessage + "<BR>";
                                break;
                            case "SingleParagraph":
                                s += Page_Validators[i].errormessage + " ";
                                break;
                        }
                    }
                }
                span = document.createElement("SPAN");
                span.innerHTML = s;
                s = span.innerText;
                alert(s);
            }
        }
    }
}


/* to check if Value is numeric*/
function IsInteger(val)
{
   var IsInt=true;
	if (!/^\d*$/.test(val))
	{
	IsInt=false;
	}
	return IsInt;
}
function IsNumeric(val)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < val.length && IsNumber == true; i++) 
      { 
      Char = val.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   
   function IsEmpty(val) {
   if ((val.value.length==0) ||
   (val.value==null)) {
      return true;
   }
   else { return false; }
} 
/* Rouding to decimal point*/
function round_decimals(original_number, decimals)
{
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places)
{
    // Convert the number to a string
    var value_string = rounded_value.toString();
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");

    // Is there a decimal point?
    if (decimal_location == -1) 
    {
       // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0;
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : "";
    }
    else 
    {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length;
    
    if (pad_total > 0) 
    {
         // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0";
     }
    return value_string;
}

/* Get JavaScript Engine Build Version (IE Specific) */
function getJSEngineVersion_IfAvailable() {
	var jsEngineVersion = ""; // empty string by default

	var t1 = typeof (ScriptEngineMajorVersion),
	    t2 = typeof (ScriptEngineMinorVersion),
	    t3 = typeof (ScriptEngineBuildVersion);
	if (t1 !== "null" && t1 !== "undefined" && t2 !== "null" && t2 !== "undefined" && t3 !== "null" && t3 !== "undefined")
		jsEngineVersion = ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion() + "." + ScriptEngineBuildVersion();

	return jsEngineVersion;
}