// This script is intended for use with a minimum of Netscape 4 or IE 4.

//// Function Name	:	CheckNumber
// Purpose			:	This function is used to validates the values entered into textboxes which 
//                      are type integer.
function mfpCheckNumber()
{
    var frmObject=document.forms[0];
	
       if(isInteger(frmObject.txtNumofEmpsCovered.value) == false || frmObject.txtNumofEmpsCovered.value < 0 ) 
       {

         alert("Please enter valid Number of employees Covered.")   
         frmObject.txtNumofEmpsCovered.focus();
         return false;
         
       }
       
      if(frmObject.txtNumofEmpsCovered.value == "0")
      {
      
         alert("Please enter value greater than zero for Number of Employees.")   
         frmObject.txtNumofEmpsCovered.focus();
         return false;
      
      }
      
      else
         return true;
}         

//All the following fields are Money fields

var txtCurrPlanAnnPreObj=document.getElementById("txtCurrPlanAnnPremium")
var txtHSAPlanAnnPreObj=document.getElementById("txtHSAPlanAnnPremium")
var txtConttoHSAperEmpObj=document.getElementById("txtConttoHSAperEmp")
	
function ValidateCurrPlanAnnPre(txtCurrPlanAnnPreObj)
{

	var tabObj = document.getElementById("tabHSACalculator");

     if((txtCurrPlanAnnPreObj!=null) &&(txtCurrPlanAnnPreObj!=''))
	 {
		if((txtCurrPlanAnnPreObj.value !=null) &&(txtCurrPlanAnnPreObj.value !=''))
		{
			if (CheckField(txtCurrPlanAnnPreObj,'Current Plan Annual Premium',tabObj)==false)
			{
			return false;
			}
		}
	 }
	return true;
}

function ValidateHSAPlanAnnPre(txtHSAPlanAnnPreObj)
{
	var tabObj = document.getElementById("tabHSACalculator");

     if((txtHSAPlanAnnPreObj!=null) &&(txtHSAPlanAnnPreObj!=''))
	 {
		if((txtHSAPlanAnnPreObj.value !=null) &&(txtHSAPlanAnnPreObj.value !=''))
		{
			if (CheckField(txtHSAPlanAnnPreObj,'HSA Plan Annual Premium',tabObj)==false)
			{
			 return false;
			}
		}
	 }
	return true;
}
   
function ValidateConttoHSAperEmp(txtConttoHSAperEmpObj)
{
	var tabObj = document.getElementById("tabHSACalculator");

	 if((txtConttoHSAperEmpObj!=null) &&(txtConttoHSAperEmpObj!=''))
	 {
		if((txtConttoHSAperEmpObj.value !=null) &&(txtConttoHSAperEmpObj.value !=''))
		{
			if (mCheckField(txtConttoHSAperEmpObj,'Contribution to HSA per Employee',tabObj)==false)
			{
              return false;
			}
		}
	 }
	return true;
}

function CheckField(field,aStrMessage,tabObj)
{
		
	if (mFormatField(field, true, true, true,aStrMessage,false,tabObj)==false)
	{
		return false;
	}
	
	return true;
}

function mCheckField(field,aStrMessage,tabObj)
{
		
	if (FormatField(field, true, true, true,aStrMessage,false,tabObj)==false)
	{
		return false;
	}
	
	return true;
}

function mFormatField(aObjField, aBlnNoZero, aBlnNoNegative, aBlnDollarIt, aStrMessage, aBlnFlushIfInvalid,tabObj)
{   

		var pStrTempVal; 
		var pIntDecIndex;
		var pIntDollarIndex;
		var pStrTempCompleteVal;
		var pStrRetVal;
		var pBlnDollarFound;
		var pStrDecimalPart;
		re=/,/g
		
		/* Remove the Dollar Sign if already Exist
		   if more than one dollar sign is found then it will be
		   trapped in the next validation for not a numeric value*/
	
		pStrTempVal = aObjField.value;
			
		pStrTempVal = pStrTempVal.replace(re,"");			

		/*	Check Whether this field is mandatory or not
		If yes then pass the message  also that need to be prompted
		when the field is empty	*/	
		
		pStrTempVal = pStrTempVal.replace(re,"");		
					
		if (isNaN(pStrTempVal))
		{
	
			//aObjField.value='';
			//Checking for the Charecters values								
			alert("Please enter valid " + aStrMessage + " value.");
	
			return false;			
		}	
		
		var loopCounter = 0;
		
		//Remove ZERO at the Starting Position.

		for (loopCounter=0;loopCounter<pStrTempVal.length;loopCounter++)
		{		
			if(pStrTempVal.length > 1)
			{
				if (pStrTempVal.substring(0,1) == '0')
				{
					pStrTempVal = pStrTempVal.substring(1);				
					loopCounter = 0;
				}
			}
		}

		var zeroFound = 0;
		for (loopCounter=0;loopCounter<pStrTempVal.length;loopCounter++)
		{
			if (zeroFound == 0)
			{
				if (eval(pStrTempVal) == 0)
				{					
					zeroFound = 1;								
					break;				
				}
			}
		}
		
		
		//Handle the Decimal case also
			
		if (pStrTempVal.length>13)
		{
		    aObjField.focus();
			alert("Please enter valid " + aStrMessage + " value.");
			return false;
		}
		
		/* Check to see if zeroes are allowed or not */
		
		if (aBlnNoZero == true)
		{			
			if (zeroFound == 1)	
			{		
		    	aObjField.focus();
				alert("Please enter a numeric value greater than zero in " + aStrMessage + '.');
				return false;
			}    
		}
		
			
		if (pStrTempVal=='')
		{
			return false;
		}
		
		/*Check to See that negative value is not allowed*/
		if (aBlnNoNegative == true)
		{
			if ( eval(pStrTempVal) < 0)	
			{	
				aObjField.focus();
				alert("Please enter a numeric value greater than zero.");
				return false;
			}    
		}
	
		pStrTempCompleteVal=pStrTempVal;
		if(pStrTempVal.indexOf('.')!=-1)
		{
			pStrDecPart=pStrTempVal.substring(pStrTempVal.indexOf('.'),pStrTempVal.length)
			if (pStrTempVal.indexOf('.') == 0)
				pStrTempVal = 0;
			else
				pStrTempVal = parseFloat(pStrTempVal.substring(0,pStrTempVal.indexOf('.')))
		}
	
	   if(isInteger(pStrTempVal)==false)
		{
		  
		  alert("Please enter valid " + aStrMessage + '.');
		  return false;
		  
		}
	
		tmpStr=new String(pStrTempVal)	
		var newStr='';
		var k=0;
		for (i=tmpStr.length-1;i>=0;i--)
		{
			newStr=newStr + tmpStr.charAt(i);
			k=k+1;
			if (k==3)
			{
				newStr=newStr; // + ',';
				k=0;
			}
			
		}
		if(newStr.charAt(newStr.length-1)==',')
		{
			newStr=newStr.substring(0,newStr.length-1)
		}
		
		tmpStr=new String(newStr)
		newStr=''; 
	
		for (i=tmpStr.length-1;i>=0;i--)
		{
			newStr=newStr + tmpStr.charAt(i);
		}
	
		pStrDecimalPart='';
		if(pStrTempCompleteVal.indexOf('.')!=-1)
		{
			pStrDecimalPart='';
			pStrDecimalPart=pStrTempCompleteVal.substring(pStrTempCompleteVal.indexOf('.') ,pStrTempCompleteVal.length)
		}
		
		if (pStrDecimalPart!='')
		{
		if (pStrDecimalPart.length > 3)
			{
				aObjField.focus();
				alert('Please enter only 2 digits after decimal.');
				return false;
			}
			else
			{
			pStrRetVal = newStr +pStrDecimalPart;						
			}
		}
		else
			pStrRetVal=newStr; 
		
		/* Place the dollar sign based on the flag */
		aObjField.value = pStrRetVal;
	
		return true;  	   
}

function FormatField(aObjField, aBlnNoZero, aBlnNoNegative, aBlnDollarIt, aStrMessage, aBlnFlushIfInvalid,tabObj)
{   

		var pStrTempVal; 
		var pIntDecIndex;
		var pIntDollarIndex;
		var pStrTempCompleteVal;
		var pStrRetVal;
		var pBlnDollarFound;
		var pStrDecimalPart;
		re=/,/g
		
		/* Remove the Dollar Sign if already Exist
		   if more than one dollar sign is found then it will be
		   trapped in the next validation for not a numeric value*/
	
		pStrTempVal = aObjField.value;
			
		pStrTempVal = pStrTempVal.replace(re,"");			

		/*	Check Whether this field is mandatory or not
		If yes then pass the message  also that need to be prompted
		when the field is empty	*/	
		
		pStrTempVal = pStrTempVal.replace(re,"");		
					
		if (isNaN(pStrTempVal))
		{
	
			aObjField.focus();
			//Checking for the Charecters values								
			alert("Please enter valid " + aStrMessage + " value.");
	
			return false;			
		}	
		
		var loopCounter = 0;
		
		//Remove ZERO at the Starting Position.

		for (loopCounter=0;loopCounter<pStrTempVal.length;loopCounter++)
		{		
			if(pStrTempVal.length > 1)
			{
				if (pStrTempVal.substring(0,1) == '0')
				{
					pStrTempVal = pStrTempVal.substring(1);				
					loopCounter = 0;
				}
			}
		}

		var zeroFound = 0;
		for (loopCounter=0;loopCounter<pStrTempVal.length;loopCounter++)
		{
			if (zeroFound == 0)
			{
				if (eval(pStrTempVal) == 0)
				{					
					zeroFound = 1;								
					break;				
				}
			}
		}
		
		
		//Handle the Decimal case also
			
		if (pStrTempVal.length>13)
		{
		    //aObjField.value='';
			alert("Please enter valid " + aStrMessage + " value.");
			return false;
		}
		
		/* Check to see if zeroes are allowed or not */
		
		//if (aBlnNoZero == true)
		//{			
		//	if (zeroFound == 1)	
		//	{		
		//		aObjField.value='';
		//		alert("Please enter a numeric value greater than zero in " + aStrMessage + '.');
	//			return false;
	//		}    
	//	}
		
			
		if (pStrTempVal=='')
		{
			return false;
		}
		
		/*Check to See that negative value is not allowed*/
		if (aBlnNoNegative == true)
		{
			if ( eval(pStrTempVal) < 0)	
			{	
				aObjField.focus();
				alert("Please enter a numeric value greater than zero.");
				return false;
			}    
		}
	
		pStrTempCompleteVal=pStrTempVal;
		if(pStrTempVal.indexOf('.')!=-1)
		{
			pStrDecPart=pStrTempVal.substring(pStrTempVal.indexOf('.'),pStrTempVal.length)
			if (pStrTempVal.indexOf('.') == 0)
				pStrTempVal = 0;
			else
				pStrTempVal = parseFloat(pStrTempVal.substring(0,pStrTempVal.indexOf('.')))
		}
	
	   if(isInteger(pStrTempVal)==false)
		{
		  
		  alert("Please enter valid " + aStrMessage + '.');
		  return false;
		  
		}
	
		tmpStr=new String(pStrTempVal)	
		var newStr='';
		var k=0;
		for (i=tmpStr.length-1;i>=0;i--)
		{
			newStr=newStr + tmpStr.charAt(i);
			k=k+1;
			if (k==3)
			{
				newStr=newStr; // + ',';
				k=0;
			}
			
		}
		if(newStr.charAt(newStr.length-1)==',')
		{
			newStr=newStr.substring(0,newStr.length-1)
		}
		
		tmpStr=new String(newStr)
		newStr=''; 
	
		for (i=tmpStr.length-1;i>=0;i--)
		{
			newStr=newStr + tmpStr.charAt(i);
		}
	
		pStrDecimalPart='';
		if(pStrTempCompleteVal.indexOf('.')!=-1)
		{
			pStrDecimalPart='';
			pStrDecimalPart=pStrTempCompleteVal.substring(pStrTempCompleteVal.indexOf('.') ,pStrTempCompleteVal.length)
		}
		
		if (pStrDecimalPart!='')
		{
		if (pStrDecimalPart.length > 3)
			{
				aObjField.focus();
				alert('Please enter only 2 digits after decimal.');
				return false;
			}
			else
			{
			pStrRetVal = newStr +pStrDecimalPart;						
			}
		}
		else
			pStrRetVal=newStr; 
		
		/* Place the dollar sign based on the flag */
		aObjField.value = pStrRetVal;
	
		return true;  	   
}

function ValidateControls()
{
   
   	var valString='';
    	
   	valString = valString + "txtCurrPlanAnnPremium~Current Plan Annual Premium";
	valString = valString + ",txtHSAPlanAnnPremium~HSA Plan Annual Premium";
	valString = valString + ",txtNumofEmpsCovered~Number of Employees Covered";
	valString = valString + ",txtConttoHSAperEmp~Contribution to HSA per Employee";
	valString = valString + ",txtBusinessTaxRate~Business Tax Rate";
	
	if (CheckMandatoryFields(valString)==true)
	{
	 
	  var txtCurrPlanAnnPreObj=document.getElementById("txtCurrPlanAnnPremium")
      var txtHSAPlanAnnPreObj=document.getElementById("txtHSAPlanAnnPremium")
      var txtConttoHSAperEmpObj=document.getElementById("txtConttoHSAperEmp") 
	  	  
      if (txtCurrPlanAnnPreObj=='[object]'|| txtCurrPlanAnnPreObj == '[object HTMLInputElement]' )
	  {
	  	if (ValidateCurrPlanAnnPre(txtCurrPlanAnnPreObj)==false)
			return false;
	  }	
	  if (txtHSAPlanAnnPreObj=='[object]'|| txtHSAPlanAnnPreObj == '[object HTMLInputElement]' )
	  {
		if (ValidateHSAPlanAnnPre(txtHSAPlanAnnPreObj)==false)
			return false;
	  }	
	  
	   if (mfpCheckNumber()==false)
		 		{return false;}
	  
	  if (txtConttoHSAperEmpObj=='[object]'|| txtConttoHSAperEmpObj == '[object HTMLInputElement]' )
	  {
		if (ValidateConttoHSAperEmp(txtConttoHSAperEmpObj)==false)
			return false;
	  }	
	  if(CheckFloating()==false)
	         {return false;}
	   
	   return true;
	}
	else
	{	
	  return false;
	}
	return true;
}

function CheckFloating()
 {
   ndot = 0
   var frmObject=document.forms[0];
   var thisfloat = frmObject.txtBusinessTaxRate.value;

   for (i=0; i < thisfloat.length; i++)
	{
      var ch = thisfloat.substring(i,i+1)
	   if ( (ch < "0" || "9" < ch) && ch !=".") 
	   {
          alert ("Please enter a valid Business Tax Rate.")
          frmObject.txtBusinessTaxRate.focus();
          return false;
       }
       if (ch == "."){ndot = ndot + 1}    
    }
    if (ndot > 1)
	{
     alert ("Please enter a valid Business Tax Rate.")
     frmObject.txtBusinessTaxRate.focus();
     return false;
    }
    if (thisfloat > 100 || thisfloat < 0 || thisfloat == 0)
    {
      alert ("Please enter a valid Business Tax Rate.")
      frmObject.txtBusinessTaxRate.focus();
      return false;
    }
   return true;
 }
 
 // Open form in a new window 

function OpenPrint()
{
 //window.open("PrintEmployerHSA.aspx")
 MM_openBrWindow('PrintEmployerHSA.aspx','','scrollbars=yes,width=825,height=425,left=90,top=70,menubar=yes,resizable=yes');
}

function GetConfirm()
{
	//alert(confirm('Do you want to Close?'));
	if (confirm("Do you want to close?")== true)
		window.close();
	else
		return false;
}

