/** Added by Rajalakshmi.D.N *********/

/**
* By Raj check for valid numeric character in the entered IP 
**/

function validNumber(obj)
{
   var strString=obj.value;
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;
   var j=-1;
   
 if(obj.readOnly==false)
 {
   if (strString.length == 0) 
   	return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      	strChar = strString.charAt(i);
      	//alert("Char="+strChar);
      	j=j+1;
      	
      	if (strValidChars.indexOf(strChar) == -1)
        {
         	blnResult = false;         
        }
   }
   
   if(blnResult==false)
   {
     	alert("Please enter a valid number!!");
     	obj.value=strString.substr(0,j);
     	return false;
   }
     
     //return;
   return blnResult;
 } 
}


/**
* By Raj Validates the IP address
**/

function validateIP(obj)
{
	var ipStr;
	var str;
	var startI=0, endI=0;
	var partStr;
	var strlenGL="false";
	var invalid="false";
	var dot=0


   	var strString=obj.value;
   	var strValidChars = "0123456789.";
   	var strChar;
   	var blnResult = true;
   	var j=-1;
	
	//var validChar=validNumber(obj);
	
	if(obj.value!="" && obj.value!=null)
	{   
   	       if (strString.length == 0) return false;

   	       //  test strString consists of valid characters listed above
   		for (i = 0; i < strString.length && blnResult == true; i++)
   		{
   	   		strChar = strString.charAt(i);
   	  		j=j+1;
      	   		if (strValidChars.indexOf(strChar) == -1)
           		{
             			blnResult = false;         
           		}
        	}
  		
		ipStr=obj.value;
		if(blnResult== true)
		{
			if(obj.value.length<7 || obj.value.length>15)
			{
				invalid="true";
			}
		
			for(var i=0;i<ipStr.length && strlenGL=="false" && invalid=="false";i++)
			{	
				if(ipStr.charAt(i)==".")
				{
					dot++;
					endI=eval(i)-eval(startI);
					//alert("StartI="+startI+" endI="+endI);
					str=ipStr.substr(startI,endI);
					partStr=ipStr.substr(startI,eval(endI));
					startI=eval(i)+eval(1);
					//alert("str="+str+" partStr="+partStr);
					
					if(str.length>3 || str.length<=0 || str>255)
					{
						strlenGL="true";
					}
				}
			}
			if(dot>3)
			{
				invalid="true"
			}
		
			if(invalid=="true" || strlenGL=="true")
			{
				alert("Invalid IP Address  "+obj.value);
				obj.value="";
				obj.focus();
				//return false;
			}
	     	}

	}
	
	return;
} 
 
 
 function checkInteger(obj,amount)
 {
    
    var objt = obj;
    var amt = amount;
    if(amt != null)
    {
        if(amt < 0  )
        {
            alert("Please enter Integer for Cheque/DD/Card No.");
            objt.value="";
            objt.focus();
        }
        if(isNaN(amt))
        {
            alert("Please enter a Integer for Cheque/DD/Card No.");
            objt.value="";
            objt.focus();
        }
        var amtstr = new String(amt);
        if(amtstr.indexOf(".") > -1)
        {
            alert("Please enter a Integer for Cheque/DD/Card No.");
            objt.value="";
            objt.focus();
        }
        
    }
 }


/* checking for only two places after decimal
     in price fields
   param1 --object
   param2 -- value
     */

function checkdecimalval(obj,amount)
{
    var objt = obj;
    var amt = amount;
    if(amt != null && amt != "")
    {
        if(amt < 0 )
        {
            alert("Please enter positive value for the amount paid");
            objt.focus();
            return false;

        }
        if(isNaN(amt))
        {
            alert("Please enter a numeric value for the amount paid");
            objt.focus();
            return false;

        }
        
           objt.value= roundoff(amount);
    }
}
function checkdecimalvalwithneg(obj,amount)
{
    var objt = obj;
    var amt = amount;
    if(amt != null && amt != "")
    {
        if(isNaN(amt))
        {
            alert("Please enter a numeric value for the amount paid");
            objt.focus();
            return false;

        }
        
        objt.value= roundoff(amount);
    }
}


// to get back rounded to 2 decimal places 

function roundoff(amount)
{
    var amtstr = new String(amount);
        
    if(amtstr.indexOf(".") > -1)
    {
        var floatlen = amtstr.length - amtstr.indexOf(".");

      if(floatlen > 2)
      {
        amtstr = amtstr.substr(0 , amtstr.indexOf(".")+ 3)
      }
      if(floatlen == 2)
      {
        amtstr += "0";
      }
      if(floatlen == 1)
      {
        amtstr += "00";
      }
    }
    else
    {
            amtstr += ".00";
    }
    
    
   return amtstr;
}



/*  to check if value entered is a number
    param 1-- object
    param 2-- value
    */
function checkfornumber(obj,val)
{
    var objt = obj;
    var value = val;
    if ((value != null) || (value !="" ) )
    {
       if(isNaN(value))
       {
        alert("Please enter a numeric value.");
        objt.focus();
        return false;
       }

    }
    

}

    
function trim(obj,value)
{
    value = value;
    
   while (value.charAt(value.length-1) == " ")
   {
    value = value.substring(0,value.length-1);
   } 
   while(value.substring(0,1) ==" ")
   {
    value = value.substring(1,value.length);
   }
   obj.value = value;

   return value ;
}

var openwin = "false";
function checkward()
{
    
 if(document.FindForm2.wardid2.value == 0)
 {
   alert("Please select the Ward first");
 }
 else
 {
   openwin ="true";
 }
}
function checkward1()
{
    
 if(document.FindForm.wardid.value == 0)
 {
   alert("Please select the Ward first");
 }
 else
 {
   openwin ="true";
 }
}   
    

function checkforward()
{
  if(document.dateForm.wardid.value == 0)
 {
   alert("Please select the Ward first");
   return false;
 }
 else
 {
   return true;
 }
}   
    



function setlink()
{
  if(openwin =="true")
  {
    link ="/streetID.jsp?wardid="+document.FindForm2.wardid2.value+"&"+"formName="+document.FindForm2.formName.value;
    win1 =window.open(link,"Streetidwindow",'scrollbars=yes,resizable=yes,height=600,width=775,status=yes');
    win1.moveTo(10, 10);
    win1.focus();
  }

}

function fnSetAccountlink()
{
  if(openwin =="true")
  {
    link ="/administration/accountNumbers.jsp?branchid="+document.BankAccountForm.selBranchName.value+"&"+"formName="+document.BankAccountForm.formName.value;
    win1 =window.open(link,"AccountNumbersWindow",'scrollbars=yes,resizable=yes,height=600,width=775,status=yes');
    win1.moveTo(10, 10);
    win1.focus();
  }
}

function fnSetDeactivatedAccountlink()
{
  if(openwin =="true")
  {
	link ="/administration/deactivatedAccountNumbers.jsp?branchid="+document.BankAccountForm.selBranchName.value+"&"+"formName="+document.BankAccountForm.formName.value;
	win1 =window.open(link,"AccountNumbersWindow",'scrollbars=yes,resizable=yes,height=600,width=775,status=yes');
	win1.moveTo(10, 10);
	win1.focus();
  }
}


function fnCheckAccount()
{
    
 if(document.BankAccountForm.selBranchName.value == 0)
 {
   alert("Please select the Branch Name first");
 }
 else
 {
   openwin ="true";
 }
}


function setlink1()
{
  if(openwin =="true")
  {
    link ="/streetID.jsp?wardid="+document.FindForm.wardid.value+"&"+"formName="+document.FindForm.formName.value;
    win1 =window.open(link,"Streetidwindow",'scrollbars=yes,resizable=yes,height=600,width=775,status=yes');
    win1.moveTo(10, 10);
    win1.focus();
  }

}

function setlinkCitizen()
{
  if(openwin =="true")
  {
    link ="/propertyTaxCitizen/streetID.jsp?wardid="+document.FindForm.wardid.value+"&"+"formName="+document.FindForm.formName.value;
    win1 =window.open(link,"Streetidwindow",'scrollbars=yes,resizable=yes,height=600,width=775,status=yes');
    win1.moveTo(10, 10);
    win1.focus();
  }
    
}
function FindFormsubmit1()
{
    document.FindForm1.Submit1.focus();
    if(document.FindForm1.pidno1.value != "")
    {
        document.FindForm1.Submit1.disabled=true;
        return true;
    }
    else
    {
        alert("Please fill in all the values for this method");
        return false;
    }
    
}
function FindFormsubmit2()
{
    
    document.FindForm2.Submit2.focus();
    
    if(document.FindForm2.streetid.value != "" && !(validatePhoneNo(document.FindForm2.streetid.value)))
    {
        alert("Please enter Numeric StreetID");
        document.FindForm2.streetid.value = "";
        document.FindForm2.streetid.focus();
        return false;
    }
    
    if((document.FindForm2.wardid2.value != 0) && (document.FindForm2.streetid.value != ""))
    {
        document.FindForm2.Submit2.disabled=true;
        return true;
    }
    else if((document.FindForm2.wardid2.value != 0) && ((document.FindForm2.newmunicipalno.value != "")||(document.FindForm2.oldmunicipalno.value != "")))
    {
        document.FindForm2.Submit2.disabled=true;
        return true;
    }
    else if((document.FindForm2.rangeid2.value != 0) && ((document.FindForm2.newmunicipalno.value != "")||(document.FindForm2.oldmunicipalno.value != "")))
    {
        document.FindForm2.Submit2.disabled=true;
        return true;
    }
    else
    {
        alert("Please fill in all the values for this method");
        return false;
    }
    
}
function FindFormsubmit3()
{
    document.FindForm3.Submit3.focus();
    
    if((document.FindForm3.wardid3.value != 0) && (document.FindForm3.rangeid3.value != "") &&(document.FindForm3.phoneno.value != "") || (document.FindForm3.firstname.value != "") || (document.FindForm3.propertyAddress.value != ""))
    {
        document.FindForm3.Submit3.disabled=false;
        return true;
    }
    else if((document.FindForm3.phoneno.value != "") || (document.FindForm3.firstname.value != "") || (document.FindForm3.propertyAddress.value != ""))
    {
        document.FindForm3.Submit3.disabled=false;
        return true;
    }
    else
    {
        alert("Please fill in all the values for this method");
        return false;
    }   
    
}



//Global Variable
var doCalc = true;
function calculator()
{
    if(doCalc == true)
    {
        doCalc = false;
        if(document.payment.totalcollection.value != "" && document.payment.totalcollection.value != 0.00)
        {
            //The RHS values are hidden whose values are set in PTC-Payment.jsp these are now stored in respective LHS vars.


            var arrtot = eval(document.payment.arrearstot.value); 
            var arrpntydmd = eval(document.payment.arrearspenaltydmd.value);
            var curpntydmd = eval(document.payment.currentpenaltydmd.value);
            var dmdpttot = eval(document.payment.demandtot.value);
            var totaldemandamt = eval(document.payment.totaldemandamt.value);
            var collection = eval(document.payment.totalcollection.value);   //Not hidden the amt which is entered by the user

            var tempTotalCurrent = eval(document.payment.tempTotalCurrent.value);

            var m_totalbalanceamt = eval(document.payment.totalbalanceamt.value);

            var   m_arrearscollectionpayment = eval(document.payment.arrearscollectionpayment.value);
            var   m_arrearsbalancepayment = eval(document.payment.arrearsbalancepayment.value);
            var   m_arrearspenaltypaycollection = eval(document.payment.arrearspenaltypaycollection.value);
            var   m_arrearspenaltypaybalance = eval(document.payment.arrearspenaltypaybalance.value);
            var   m_currentpenaltypaycollection = eval(document.payment.currentpenaltypaycollection.value);
            var   m_currentpenaltypaybalance = eval(document.payment.currentpenaltypaybalance.value);
            var   m_currentcollection = eval(document.payment.totalcollection.value);


            var   m_currentbalance = eval(document.payment.currentbalance.value);
            var   m_totalamtpaid= 0.00;
                //alert('arrpntydmd'+arrtot);

            //When a collection is made the collected amt is distributed as Arrears,Penalty(arr),Penalty(curr)and finally PT
            //listed asper priority i.e whatever is the collnAmt first its taken for Arrears and then for penaltyArrs and so on..
            //Deduct Arrearsamt from Collecton made if it is Gt Arrearstotal payable,ie if Arrears Total > collection + Amt Already paid for Arrears


            if((collection + m_arrearscollectionpayment > arrtot )  && (collection > 0) )
            {
                if(arrtot > 0.0)
                {
                    document.payment.arrearscollection.value= roundoff(arrtot);                 
                    m_totalamtpaid +=  arrtot;
                    collection = collection - arrtot + m_arrearscollectionpayment;
                }               
                
                document.payment.arrearsbalance.value ="0.00";
            }
            // Else just subtract the collection amt from arrTotal So collection amt is entirely taken for arrears.
            else
            {
                document.payment.arrearscollection.value= roundoff(m_arrearscollectionpayment + collection);
                m_totalamtpaid +=  m_arrearscollectionpayment + collection;
                document.payment.arrearsbalance.value = roundoff(arrtot-collection - m_arrearscollectionpayment);
                collection = 0;
            }

            //Now deduct the Arrears Penalty from the Collection
            if((collection + m_arrearspenaltypaycollection > arrpntydmd) && (collection>0))
            {
                document.payment.arrearspenaltycollection.value = roundoff(arrpntydmd);
                m_totalamtpaid +=arrpntydmd;
                collection = collection-arrpntydmd + m_arrearspenaltypaycollection;
                document.payment.arrearspenaltybalance.value ="0.00";
            }
            else
            {
                document.payment.arrearspenaltycollection.value= roundoff(m_arrearspenaltypaycollection +collection);
                m_totalamtpaid +=m_arrearspenaltypaycollection +collection;
                document.payment.arrearspenaltybalance.value= roundoff(arrpntydmd-collection -m_arrearspenaltypaycollection);
                collection = 0;
            }

            //Now deduct the Current Penalty from the Collection

            if((collection +m_currentpenaltypaycollection > curpntydmd) && (collection>0))
            {
                document.payment.currentpenaltycollection.value =roundoff(curpntydmd);
                m_totalamtpaid += curpntydmd;
                collection = collection-curpntydmd +m_currentpenaltypaycollection;
                document.payment.currentpenaltybalance.value ="0.00";
            }
            else
            {
                document.payment.currentpenaltycollection.value=roundoff(m_currentpenaltypaycollection + collection);
                m_totalamtpaid += m_currentpenaltypaycollection + collection;
                document.payment.currentpenaltybalance.value=roundoff(curpntydmd-collection - m_currentpenaltypaycollection);
                collection = 0;

            }
            //Finally deduct the Current PT/currentpaymentcollection
        //  If the amount collected is greator than the demand total
        // The current payment balance will become zero and the collection value shud be negative
        if((collection + tempTotalCurrent > dmdpttot) && (collection>0))
        {
            //alert('>>document.payment.currentpaymentcollection.value 1:' + dmdpttot);
            document.payment.currentpaymentcollection.value =roundoff(dmdpttot);
            m_totalamtpaid +=dmdpttot;
            //collection = collection-dmdpttot + m_currentcollection;
            collection = collection-(dmdpttot - tempTotalCurrent);
            //alert('collection-(dmdpttot - tempTotalCurrent) : '+collection-(dmdpttot - tempTotalCurrent));
            document.payment.currentpaymentbalance.value ="0.00";
        }
        else //     If the amount collected is less than the demand total all the reamining money goes to demand and collection becomes 0
        {
            //alert('>>document.payment.currentpaymentcollection.value 2:' + roundoff(collection + tempTotalCurrent));
            document.payment.currentpaymentcollection.value= roundoff(collection + tempTotalCurrent);
            m_totalamtpaid += collection;
            //document.payment.currentpaymentbalance.value=roundoff(dmdpttot- collection - m_currentcollection);
            //alert('roundoff(dmdpttot- collection - tempTotalCurrent) : '+roundoff(dmdpttot- collection - tempTotalCurrent));
            document.payment.currentpaymentbalance.value=roundoff(dmdpttot- collection - tempTotalCurrent);
            collection = 0;
        }

        if (m_totalbalanceamt < 0)
        {
            //alert('>>1 totaldemandamt'+totaldemandamt);
            //alert('>>1 m_totalbalanceamt'+m_totalbalanceamt);         
            //alert('>>1 m_totalamtpaid'+m_totalamtpaid);
            //alert('>>1 collection'+collection);           
            //alert('>>1 tempTotalCurrent'+tempTotalCurrent);           
            
            document.payment.totalbalance.value =  roundoff(m_totalbalanceamt - collection);        
        }
        else
        {
            //alert('>>2 totaldemandamt'+totaldemandamt);
            //alert('>>2 m_totalamtpaid'+m_totalamtpaid);
            //alert('>>2 collection'+collection);           
            //alert('>>2 tempTotalCurrent'+tempTotalCurrent);           
            if(collection==0)
            {           
                document.payment.totalbalance.value =  roundoff(totaldemandamt - m_totalamtpaid - collection - tempTotalCurrent);       
            }
            else
            {   
                document.payment.totalbalance.value =  roundoff(totaldemandamt - m_totalamtpaid - collection);      
            }
        }
      }     
    }
}

function trimalltext(allelemobj)
{
    
    //alert("in trimall"+allelemobj.length);
    for(var i=0; i<allelemobj.length; i++)
    {
        if(allelemobj[i].type == "text")
        {
            trim(allelemobj[i], allelemobj[i].value);
        }
        //if(i==10)
        //{
        //  break;
        //}
    }   
}


function submitpaymentCheck()
{
    if(document.payment.assesementYear.value == 0)
    {
        alert('Please choose the Assessment year');
        document.payment.assesementYear.focus();
        return false;
    }   
    //Check the Field COllection Date Format
    if(document.payment.collectionLocation[1].checked==true &&
    trim(document.payment.fldcollectionDate,document.payment.fldcollectionDate.value) != "" )
    {
        if(!validateDate(document.payment.fldcollectionDate.value) ) 
        {
            alert('Invalid Collection Date');
            document.payment.fldcollectionDate.focus();
            return false;
        }           
        var assesementYear = document.payment.assesementYear.value;
        var fldCollectionDate = document.payment.fldcollectionDate.value; 
        if (!validateFinancialYear(assesementYear,fldCollectionDate))
        {
            alert('Not A Collection Valid Date');
            document.payment.fldcollectionDate.focus();
            return false;
        }
    }
    //Check the Bank COllection Date Format 
    if(document.payment.collectionLocation[2].checked==true && trim(document.payment.bankcollectionDate,document.payment.bankcollectionDate.value) != "" )
    {
        if(!validateDate(document.payment.bankcollectionDate.value) ) 
        {
            alert('Invalid BankCollection Date');
            document.payment.bankcollectionDate.focus();
            return false;
        }           
        var assesementYear =document.payment.assesementYear.value;
        //alert(">>>>assesementYear :"+assesementYear)
        var bankcollectionDate = document.payment.bankcollectionDate.value; 
        if (!validateFinancialYear(assesementYear,bankcollectionDate))
        {
            alert('Not A Valid BankCollection Date');
            document.payment.bankcollectionDate.focus();
            return false;
        }
    }
    
    
  
  if(document.payment.collectionLocation[0].checked == false && (document.payment.receiptNumber.value == "" && document.payment.challanNumber.value == ""))
  {
        alert("Please fill in the Receipt No./Challan No.");
        //document.payment.receiptNumber.focus();
        return false;   
  }
  if(document.payment.collectionLocation[0].checked == false && (document.payment.fldcollectionDate.value == "" && document.payment.bankcollectionDate.value == ""))
  {
    alert("Please fill in the Collection Date");
    document.payment.fldcollectionDate.focus();
      return false;     
  }
  if(document.payment.arrearscollection.value=="" || document.payment.arrearsbalance.value=="" || document.payment.arrearspenaltycollection.value=="" || document.payment.arrearspenaltybalance.value=="" || document.payment.currentpenaltycollection.value=="" || document.payment.currentpenaltybalance.value=="" || document.payment.currentpaymentbalance.value=="" )
  {
      alert("Please fill in the payment details");
      return false;
  }
  if(document.payment.totalcollection.value == "")
  {
    alert("Please fill in the amount to be paid");
    document.payment.totalcollection.focus();
    return false;
  }
  if(document.payment.arrearsduration[0].checked == false && document.payment.arrearsduration[1].checked == false )
  {
    if(document.payment.duration[0].checked == false && document.payment.duration[1].checked == false && document.payment.duration[2].checked == false && document.payment.duration[3].checked == false)
    {
      alert("Please select the type of payment");
      return false;
     } 
  }
  if(document.payment.modeOfPayment[0].checked == false && document.payment.modeOfPayment[1].checked == false && document.payment.modeOfPayment[2].checked == false && document.payment.modeOfPayment[3].checked == false && document.payment.modeOfPayment[4].checked == false )
  {
    alert("Please select the payment type(Cash,Cheque,DD,Credit/Debit Card)");
    return false;
  }
  if(document.payment.modeOfPayment[1].checked == true)
  {
    if(document.payment.chequeno.value=="")
    {
        alert("Please fill in the Cheque Number");
        document.payment.chequeno.focus();
        return false;
    }
    if(document.payment.chequedate.value=="")
    {
        alert("Please fill in the date of cheque");
        document.payment.chequedate.focus();
        return false;
    }
  }
 if(document.payment.modeOfPayment[2].checked == true)
  {
    if(document.payment.ddno.value=="")
    {
        alert("Please fill in the DD Number");
        document.payment.ddno.focus();
        return false;
    }
    if(document.payment.dddate.value=="")
    {
        alert("Please fill in the date of DD");
        document.payment.dddate.focus();
        return false;
    }
  }
  if(document.payment.modeOfPayment[2].checked == true || document.payment.modeOfPayment[1].checked == true)
  {
    if(document.payment.banksname.value=="")
    {
        alert("Please fill in the Name of bank where Cheque or DD was drawn");
        document.payment.banksname.focus();
        return false;
    }
    
  }
   if(document.payment.modeOfPayment[3].checked == true || document.payment.modeOfPayment[4].checked == true)
  {
  		/*if(document.payment.cardNo.value=="")
	    {
	        alert("Please fill in the Card Number");
	        document.payment.cardNo.focus();
	        return false;
	    }
	    else
	    {
	    	if(!validateCardNo(document.payment.cardNo))
	    		return false;
	    }*/
	    if(document.payment.authorizationCode.value=="")
	    {
	        alert("Please fill in the Authorization Code");
	        document.payment.authorizationCode.focus();
	        return false;
    	}
    	else if(document.payment.authorizationCode.value.length <5)
		{
			alert('Authorization Code Should be of 5 or more Character/Digits');
			 document.payment.authorizationCode.focus();
				return false;
	    }
    	if(document.payment.cardType.value==0)
		{
			alert("Please Select Card Type");
			document.payment.cardType.focus();
			return false;
    	}
    	
  }
    
  if(document.payment.arryear.value!="" && !validateArrearyear(document.payment.arryear.value))
  {
       
      alert("Please Enter a Valid Arrears Paid for the Year");
      document.payment.arryear.value="";
      document.payment.arryear.focus();
      return false;
  }
  
  if(document.payment.totalbalance.value < 0)
 {
    
    alert('Balance should not be Negative');
    document.payment.totalbalance.value = "";
    document.payment.totalbalance.focus();
    return false;
 }

  //return updateFinalVars();                       
  var ret = updateFinalVars();              
  
  if(ret == false)
    return false;
  
    var arrbal      =    eval(document.payment.arrearsbalance.value);
    var arrpntybal  =    eval(document.payment.arrearspenaltybalance.value);
    var curPnltyBal =    eval(document.payment.currentpenaltybalance.value);
    var curPTBal    =    eval(document.payment.currentpaymentbalance.value);
    var totBal      =    eval(document.payment.totalbalance.value);
    var sum         =    roundoff(arrbal + arrpntybal + curPnltyBal + curPTBal)
    
    
   // if(totBal > 0)
   // {
        if(totBal != sum) 
        {
            alert("Sum of individual Balances is not equal to TotalBalance");
            document.payment.totalbalance.focus();
            return false;

        }
   // }
    
  document.payment.submit.disabled = true;
}
/**********************************************************************************************************************************/
//This method is for pastyearpayment 
function submitpaymentCheckForPastYr()
{
    //alert('here in past payment');
    if(document.payment.assesementYear.value == 0)
    {
        alert('Please choose the Assessment year');
        document.payment.assesementYear.focus();
        return false;
    }   
    //Check the Field COllection Date Format
    if(document.payment.collectionLocation[1].checked==true &&
    trim(document.payment.fldcollectionDate,document.payment.fldcollectionDate.value) != "" )
    {
        if(!validateDate(document.payment.fldcollectionDate.value) ) 
        {
            alert('Invalid Collection Date');
            document.payment.fldcollectionDate.focus();
            return false;
        }           
        var assesementYear = document.payment.assesementYear.value;
        var fldCollectionDate = document.payment.fldcollectionDate.value; 
        if (!validateFinancialYear(assesementYear,fldCollectionDate))
        {
            alert('Not A Collection Valid Date');
            document.payment.fldcollectionDate.focus();
            return false;
        }
    }
    //Check the Bank COllection Date Format 
    if(document.payment.collectionLocation[2].checked==true && trim(document.payment.bankcollectionDate,document.payment.bankcollectionDate.value) != "" )
    {
        if(!validateDate(document.payment.bankcollectionDate.value) ) 
        {
            alert('Invalid BankCollection Date');
            document.payment.bankcollectionDate.focus();
            return false;
        }           
        var assesementYear = document.payment.assesementYear.value;
        var bankcollectionDate = document.payment.bankcollectionDate.value; 
        if (!validateFinancialYear(assesementYear,bankcollectionDate))
        {
            alert('Not A Valid BankCollection Date');
            document.payment.bankcollectionDate.focus();
            return false;
        }
    }
    
    
  
  if(document.payment.collectionLocation[0].checked == false && (document.payment.receiptNumber.value == "" && document.payment.challanNumber.value == ""))
  {
        alert("Please fill in the Receipt No./Challan No.");
        //document.payment.receiptNumber.focus();
        return false;   
  }
  if(document.payment.collectionLocation[0].checked == false && (document.payment.fldcollectionDate.value == "" && document.payment.bankcollectionDate.value == ""))
  {
    alert("Please fill in the Collection Date");
    document.payment.fldcollectionDate.focus();
      return false;     
  }
  if(document.payment.arrearscollection.value=="" || document.payment.arrearsbalance.value=="" || document.payment.arrearspenaltycollection.value=="" || document.payment.arrearspenaltybalance.value=="" || document.payment.currentpenaltycollection.value=="" || document.payment.currentpenaltybalance.value=="" || document.payment.currentpaymentbalance.value=="" )
  {
      alert("Please fill in the payment details");
      return false;
  }
  if(document.payment.totalcollection.value == "")
  {
    alert("Please fill in the amount to be paid");
    document.payment.totalcollection.focus();
    return false;
  }
  if(document.payment.arrearsduration[0].checked == false && document.payment.arrearsduration[1].checked == false )
  {
    if(document.payment.duration[0].checked == false && document.payment.duration[1].checked == false && document.payment.duration[2].checked == false && document.payment.duration[3].checked == false)
    {
      alert("Please select the type of payment");
      return false;
     } 
  }
  if(document.payment.modeOfPayment[0].checked == false && document.payment.modeOfPayment[1].checked == false && document.payment.modeOfPayment[2].checked == false )
  {
    alert("Please select the payment type(Cash,Cheque or DD)");
    return false;
  }
  if(document.payment.modeOfPayment[1].checked == true)
  {
    if(document.payment.chequeno.value=="")
    {
        alert("Please fill in the Cheque Number");
        document.payment.chequeno.focus();
        return false;
    }
    if(document.payment.chequedate.value=="")
    {
        alert("Please fill in the date of cheque");
        document.payment.chequedate.focus();
        return false;
    }
  }
 if(document.payment.modeOfPayment[2].checked == true)
  {
    if(document.payment.ddno.value=="")
    {
        alert("Please fill in the DD Number");
        document.payment.ddno.focus();
        return false;
    }
    if(document.payment.dddate.value=="")
    {
        alert("Please fill in the date of DD");
        document.payment.dddate.focus();
        return false;
    }
  }
  if(document.payment.modeOfPayment[2].checked == true || document.payment.modeOfPayment[1].checked == true)
  {
    if(document.payment.banksname.value=="")
    {
        alert("Please fill in the Name of bank where Cheque or DD was drawn");
        document.payment.banksname.focus();
        return false;
    }
    
  }
  
  
    
  if(document.payment.arryear.value!="" && !validateArrearyear(document.payment.arryear.value))
  {
       
      alert("Please Enter a Valid Arrears Paid for the Year");
      document.payment.arryear.value="";
      document.payment.arryear.focus();
      return false;
  }
  
  if(document.payment.totalbalance.value < 0)
 {
    
    alert('Balance should not be Negative');
    document.payment.totalbalance.value = "";
    document.payment.totalbalance.focus();
    return false;
 }

  //return updateFinalVars();                       
  var ret = updateFinalVars();              
  
  if(ret == false)
    return false;
  
    var arrbal      =    eval(document.payment.arrearsbalance.value);
    var arrpntybal  =    eval(document.payment.arrearspenaltybalance.value);
    var curPnltyBal =    eval(document.payment.currentpenaltybalance.value);
    var curPTBal    =    eval(document.payment.currentpaymentbalance.value);
    var totBal      =    eval(document.payment.totalbalance.value);
    var sum         =    roundoff(arrbal + arrpntybal + curPnltyBal + curPTBal)
    
    
   // if(totBal > 0)
   // {
        if(totBal != sum) 
        {
            alert("Sum of individual Balances is not equal to TotalBalance");
            document.payment.totalbalance.focus();
            return false;

        }
   // }
    
  document.payment.submit.disabled = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/*function validateFinancialYear(financialYear, dateObj)
{
    var month = dateObj.substr(3,2);
    var year = dateObj.substr(9,1);
    var nextYear = null;
    alert("financialYear");
    alert(parseInt(financialYear)+1);
    if ( month>3)
    {
        nextYear = eval(parseInt(financialYear) + 1);
        
    }
    if(financialYear <10)
    {
        financialYear = "0"+financialYear;
    }
    alert("nextYear");
    alert(nextYear);
    alert("nextYear1");
    if((year == financialYear) || (year == nextYear))
    {
        return true;
    }
    else
    {
        return false;
    }
    alert("4");
}*/

//Validates that the Date entered belongs to the givenfinancial year 
function validateFinancialYear(financialYear, collDateObj)
{
    //alert('Inside validateFinancialYear'+financialYear);
    var collMonth = eval(collDateObj.substr(3,2));
    var collYear = eval(collDateObj.substr(8,2));
    var finYear = eval(financialYear);
    //alert('collYear'+collYear);
   // alert('collMonth'+collMonth);
  //  alert('finYear'+finYear);
    
    //if(collMonth <= 3 && finYear == (collYear -1))
    
    if(collMonth > 3 && collYear == (finYear -1))
        return true;
    else if(collMonth <= 3 && finYear == collYear)
        return true
    else 
        return false;
    
}


//Temporarily validation of Financial year is been relaxed from 1st April to 1st Jan
function validateFinancialYearTemp(financialYear, collDateObj)
{
    //alert('Inside validateFinancialYear'+collYear);
    var collMonth = eval(collDateObj.substr(3,2));
    var collYear = eval(collDateObj.substr(8,2));
    var finYear = eval(financialYear);
    //alert('collYear'+collYear);
    //alert('collMonth'+collMonth);
    //alert('finYear'+finYear);
    
    //if(collMonth <= 3 && finYear == (collYear -1))
    
    if(collYear == (finYear -1))
        return true;
    else if(collMonth <= 3 && finYear == collYear)
        return true
    else 
        return false;
    
}



function updateFinalVars()
{
    
    
    var tempArrearsCollection        = eval(document.payment.arrearscollection.value);
    var tempArrearsPenaltyCollection = eval(document.payment.arrearspenaltycollection.value);
    var tempCurrentPenaltycollection = eval(document.payment.currentpenaltycollection.value);
    var tempCurrentPaymentCollection = eval(document.payment.currentpaymentcollection.value);   
    var balance = document.payment.totalbalance.value;  
    
    var totalAmtinHeads;
    if(balance >= 0)
    {
        totalAmtinHeads = tempArrearsCollection + tempArrearsPenaltyCollection+ tempCurrentPenaltycollection + tempCurrentPaymentCollection;
    }
    else
    {
        var tempTotBal = eval(document.payment.tempTotalBalance.value);
        if( tempTotBal >= 0 )       
            totalAmtinHeads = tempArrearsCollection + tempArrearsPenaltyCollection+ tempCurrentPenaltycollection + tempCurrentPaymentCollection - balance;      
        else
            totalAmtinHeads = tempArrearsCollection + tempArrearsPenaltyCollection+ tempCurrentPenaltycollection + tempCurrentPaymentCollection - balance + tempTotBal;
            
            
    }   
    

    //Round off the number as there are inconsistencies in the 4th. to 5th decimal places
    totalAmtinHeads = Math.round(totalAmtinHeads);  
        
    if(totalAmtinHeads != eval(document.payment.totalcollection.value))
      {
          //disabled temporarily.because the data is in such a bad state it is very
          //hard to debug and alter the script code and test all the 101 cases again.
          alert("The Total amount collected does not match the collection break up.");
          return false;         
          
         //return true;
      }
    
    //document.payment.arrearscollection.value = document.payment.arrearscollection.value - document.payment.tempTotalArrears.value;
    //document.payment.arrearspenaltycollection.value = document.payment.arrearspenaltycollection.value - document.payment.tempPenaltyArrears.value;
    //document.payment.currentpenaltycollection.value = document.payment.currentpenaltycollection.value - document.payment.tempPenaltyCurrent.value;
    //document.payment.currentpaymentcollection.value = document.payment.currentpaymentcollection.value - document.payment.tempTotalCurrent.value;  
    
    
}


function enabletext()
{
   // alerrt("here hello"+document.payment.modeOfPayment[4]);
    if(document.payment.modeOfPayment[0].checked == true)
    {
        document.payment.ddno.value="";
        document.payment.dddate.value="";
        document.payment.chequedate.value="";
        document.payment.chequeno.value="";
        document.payment.banksname.value="";
        document.payment.ddno.disabled = true;
        document.payment.dddate.disabled = true;
        document.payment.chequedate.disabled = true;
        document.payment.chequeno.disabled = true;
        document.payment.banksname.disabled = true;
        document.payment.cardNo.disabled = true;
		document.payment.authorizationCode.disabled = true;
		document.payment.cardType.disabled = true;
		document.payment.cardNo.value = "";
		document.payment.authorizationCode.value = "";
		document.payment.cardType.value=0;
    }
    if(document.payment.modeOfPayment[1].checked == true)
    {
        document.payment.ddno.value="";
        document.payment.dddate.value="";
        document.payment.chequedate.disabled = false;
        document.payment.chequeno.disabled = false;
        document.payment.ddno.disabled = true;
        document.payment.dddate.disabled = true;
        document.payment.banksname.disabled = false;
        document.payment.cardNo.disabled = true;
		document.payment.authorizationCode.disabled = true;
		document.payment.cardType.disabled = true;
		document.payment.cardNo.value = "";
		document.payment.authorizationCode.value = "";
		document.payment.cardType.value=0;
    }
    if(document.payment.modeOfPayment[2].checked == true)
    {
        document.payment.chequedate.value="";
        document.payment.chequeno.value="";
        document.payment.chequedate.disabled = true;
        document.payment.chequeno.disabled = true;
        document.payment.ddno.disabled = false;
        document.payment.dddate.disabled = false;
        document.payment.banksname.disabled = false;
        document.payment.cardNo.disabled = true;
		document.payment.authorizationCode.disabled = true;
		document.payment.cardType.disabled = true;
		document.payment.cardNo.value = "";
		document.payment.authorizationCode.value = "";
		document.payment.cardType.value=0;
    }
   if(document.payment.modeOfPayment[3].checked == true || document.payment.modeOfPayment[4].checked == true)
	{
		
		/*if(document.payment.totalcollection.value < 100)
		{	
			alert('Credit/Debit Card is not Accepted for Payment of Less than 100 Rs.');
			
			//document.payment.modeOfPayment[3].checked = false;
		}*/
		//else
		//{
			document.payment.ddno.value="";
			document.payment.dddate.value="";
			document.payment.chequedate.value="";
			document.payment.chequeno.value="";
			document.payment.chequedate.disabled = true;
			document.payment.chequeno.disabled = true;
			document.payment.ddno.disabled = true;
			document.payment.dddate.disabled = true;
			document.payment.cardNo.disabled = true;
			document.payment.authorizationCode.disabled = false;
			document.payment.cardType.disabled = false;
		//}
		
    }
}
//////////////////////////////////////////////////////////////////////////////////////
///////For Past Payment
  function enabletextForPastYrPayment()
  {
     // alert('Hi ');
      if(document.payment.modeOfPayment[0].checked == true)
      {
         //alert('cash checked');
         document.payment.ddno.value="";
          document.payment.dddate.value="";
          document.payment.chequedate.value="";
          document.payment.chequeno.value="";
          document.payment.banksname.value="";
          document.payment.ddno.disabled = true;
          document.payment.dddate.disabled = true;
          document.payment.chequedate.disabled = true;
          document.payment.chequeno.disabled = true;
          document.payment.banksname.disabled = true;
      }
      if(document.payment.modeOfPayment[1].checked == true)
      {
        // alert('chq checked');
         document.payment.ddno.value="";
          document.payment.dddate.value="";
          document.payment.chequedate.disabled = false;
          document.payment.chequeno.disabled = false;
          document.payment.ddno.disabled = true;
          document.payment.dddate.disabled = true;
          document.payment.banksname.disabled = false;
      }
      if(document.payment.modeOfPayment[2].checked == true)
      {
        // alert('DD checked');
         document.payment.chequedate.value="";
          document.payment.chequeno.value="";
          document.payment.chequedate.disabled = true;
          document.payment.chequeno.disabled = true;
          document.payment.ddno.disabled = false;
          document.payment.dddate.disabled = false;
          document.payment.banksname.disabled = false;
      }
}

///////////////////////////////////////////////////////////////////////////////////////////
function fillcollbal(obj)
{
    if(document.payment.totalcollection.value != "")
    {
        var collection = eval(document.payment.totalcollection.value);
        if(document.payment.duration[1] == obj)
        {
            document.payment.currentpaymentcollection.value =roundoff(collection);
            document.payment.currentpaymentbalance.value =roundoff(collection);
            document.payment.totalbalance.value =roundoff(collection);
        }
        if(document.payment.duration[3] == obj)
        {
            document.payment.currentpaymentcollection.value =roundoff(collection);
            document.payment.currentpaymentbalance.value ="0.00";
            document.payment.totalbalance.value ="0.00";
        }
    }   
}

function printpage()
{
 window.print();
 window.setInterval("window.close()",60);
}

function btnhiddenprintpage(obj)
{
	obj.style.visibility = "hidden";
	window.print();
	obj.style.visibility = "visible";
 	
}  

function btnbgcolorprintpage(obj,tablename,color)
{
	tablename.style.background = 'white';
	obj.style.visibility = "hidden";
	window.print();
	tablename.style.background = color;
	obj.style.visibility = "visible";
 	
}  

function newprintpage()
{
 window.print();
 
}

function openprintwindow(link)
{
  win1 =window.open(link,"receiptwindow",'scrollbars=yes,resizable=yes,height=200,width=400,status=yes');
}   
function chequecancelreceipt()
{
    if (document.dateForm.receiptno.value == "")
    {
        alert("please enter a Receipt Number");
        return false;
    }
    else
    {
        return true;
    }
}

function chequecancelreceiptconfirm()
{
    if (document.ReceiptCancellationConfirm.reason.value == "")
    {
        alert("please enter a Reason for cancellation of receipt.");
        return false;
    }
    else
    {
        return true;
    }
}
//Validates if the current date is greater than the date given


function validateAlpha( strValue )
{
    var objRegExp  = /^([a-zA-Z\ ]+)$/i;
    return objRegExp.test(strValue)
}

function validateNotEmpty( strValue ) 
{
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0)
   {
      return true;
   }  
   return false;
 }
 
 function trimAll( strValue ) 
 {
     var objRegExp = /^(\s*)$/;
 
     //check for all spaces
     if(objRegExp.test(strValue)) 
     {
        strValue = strValue.replace(objRegExp, '');
        if( strValue.length == 0)
           return strValue;
     }
     
     //check for leading & trailing spaces
     objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
     if(objRegExp.test(strValue)) 
     {
        //remove leading and trailing whitespace characters
        strValue = strValue.replace(objRegExp, '$2');
     }
     return strValue;
 }
 
 function validateZipCode( strValue ) 
 {
    var objRegExp  = /(^\d{6}$)/;
    return objRegExp.test(strValue);
 } 
 
 function validateEmail( strValue) 
 {
   var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
   //check for valid email
   return objRegExp.test(strValue);
 }
 
 function validatePhoneNo( strValue )
 {
   var objRegExp  = /^([0-9]+)$/;
   return objRegExp.test(strValue); 
 }
 function validateAmount( strValue )
  {
    var objRegExp  = /^([0-9]+)$/;
    return objRegExp.test(strValue); 
  }
 
  function validateNumeric( strValue )
   {
     var objRegExp  = /^([0-9]+)$/;
     return objRegExp.test(strValue); 
   }
 
 function validateEnteredDate(strValue)
 {
    var objRegExp  = /^([0-9]{2})-([0-9]{2})-([0-9]{4})$/i;
       //check for valid email
   	return objRegExp.test(strValue);
 }
 //As per BMP request check is been relaxed for 7 months instead of +/- 5months
function validateDateForNext6Months(strValue)
{
 	entereddate = strValue;
 
 	enteredyear = entereddate.substr(6,9);
 	enteredmonth = entereddate.substr(3,2);
 	entereddate = entereddate.substr(0,2);
 
 	entereddate1 = new Date(enteredyear,enteredmonth-1,entereddate);
 	
 	today = new Date();
 	//set upper limit to be 5 months later
 	today.setMonth(today.getMonth()+7);
 
 	thisdate = today.getDate();
 	thismonth = today.getMonth()+1;
 	thisyear = today.getYear();
 	
 	today1 = new Date(thisyear,thismonth-1,thisdate);
 	
 	
 	todayNew = new Date();
 	//set lower limit to be 5 months before current date
 	todayNew.setMonth(todayNew.getMonth()-7);

	thisdate1 = todayNew.getDate();
	thismonth1 = todayNew.getMonth()+1;
	thisyear1 = todayNew.getYear();
	
	today2 = new Date(thisyear1,thismonth1-1,thisdate1);
 	//alert('today2'+today2);
 	
 
 	if(entereddate1 > today1 || entereddate1 < today2)
 	{
 		//alert("Please enter valid Date");
 		return false;
 	}
 	else
 	{
 		return true;
	}
}
/** This method is as per clients request to change the effective date check
**  to 1 year instead of 7 months
**/
 function validateDateFor1Year(strValue)
 {
  	entereddate = strValue;
  
  	enteredyear = entereddate.substr(6,9);
  	enteredmonth = entereddate.substr(3,2);
  	entereddate = entereddate.substr(0,2);
  
  	entereddate1 = new Date(enteredyear,enteredmonth-1,entereddate);
  	
  	today = new Date();
  	//set upper limit to be 12 months later
  	today.setMonth(today.getMonth()+12);
  
  	thisdate = today.getDate();
  	thismonth = today.getMonth()+1;
  	thisyear = today.getYear();
  	
  	today1 = new Date(thisyear,thismonth-1,thisdate);
  	
  	
  	todayNew = new Date();
  	//set lower limit to be 12 months before current date
  	todayNew.setMonth(todayNew.getMonth()-12);
 
 	thisdate1 = todayNew.getDate();
 	thismonth1 = todayNew.getMonth()+1;
 	thisyear1 = todayNew.getYear();
 	
 	today2 = new Date(thisyear1,thismonth1-1,thisdate1);
  	//alert('today2'+today2);
  
  
  	if(entereddate1 > today1 || entereddate1 < today2)
  	{
  		//alert("Please enter valid Date");
  		return false;
  	}
  	else
  	{
  		return true;
 	}
 }

function validateArrearyear(strValue)
 {
    var objRegExp  = /^([0-9]{4})-([0-9]{4})$/i;
       //check for valid arrear year
   return objRegExp.test(strValue);
 }
function validateAlphaNumeric( strValue )
{
    var objRegExp  = /^([a-zA-Z0-9\ ]+)$/i;
    return objRegExp.test(strValue)
}


function validatePropertyID ( strValue )
{
    var objRegExp  = /^([0-9]{1,3})-([0-9]{1,4})-([a-z0-9_\-\\.\,\/()]+)$/i;
    return objRegExp.test(strValue)
}
function chkBoxValidate()
{
    if(document.editPaymentPage.edit.length >0)
    {
        for(var i=0;i<document.editPaymentPage.edit.length;i++)
        {
                
            if(document.editPaymentPage.edit[i].checked)
            {
                document.editPaymentPage.deleteIt[i].disabled = true;
                document.editPaymentPage.receiptNo[i].disabled = false;
                document.editPaymentPage.dateOfPayment[i].disabled = false;
                document.editPaymentPage.ptCurr[i].disabled = false;
                document.editPaymentPage.cessCurr[i].disabled = false;
                document.editPaymentPage.totalCurrent[i].disabled = false;
                document.editPaymentPage.totalCurrent[i].readOnly = true;
                document.editPaymentPage.ptArr[i].disabled = false;
                document.editPaymentPage.cessArr[i].disabled = false;
                document.editPaymentPage.totalArrears[i].disabled = false;
                document.editPaymentPage.totalArrears[i].readOnly = true;
                document.editPaymentPage.penalty[i].disabled = false;
                document.editPaymentPage.totalAmount[i].disabled = false;
                document.editPaymentPage.totalAmount[i].readOnly = true; 
                document.editPaymentPage.refNum[i].disabled = false;
            }
            else if(!document.editPaymentPage.edit[i].checked)
            {
                //alert('Here111'+i);//+document.editPaymentPage.delete[i].value);
                document.editPaymentPage.deleteIt[i].disabled = false;
                document.editPaymentPage.receiptNo[i].disabled = true;
                document.editPaymentPage.dateOfPayment[i].disabled = true;
                document.editPaymentPage.ptCurr[i].disabled = true;
                document.editPaymentPage.cessCurr[i].disabled = true;
                document.editPaymentPage.totalCurrent[i].disabled = true;
                document.editPaymentPage.ptArr[i].disabled = true;
                document.editPaymentPage.cessArr[i].disabled = true;
                document.editPaymentPage.totalArrears[i].disabled = true;
                document.editPaymentPage.penalty[i].disabled = true;
                document.editPaymentPage.totalAmount[i].disabled = true;
                document.editPaymentPage.refNum[i].disabled = true;
            }
        }
    }
    else
    {
        if(document.editPaymentPage.edit.checked)
        {
            document.editPaymentPage.deleteIt.disabled = true;
            
            document.editPaymentPage.receiptNo.disabled = false;
            document.editPaymentPage.dateOfPayment.disabled = false;
            document.editPaymentPage.ptCurr.disabled = false;
            document.editPaymentPage.cessCurr.disabled = false;
            document.editPaymentPage.totalCurrent.disabled = false;
            document.editPaymentPage.totalCurrent.readOnly = true;
            document.editPaymentPage.ptArr.disabled = false;
            document.editPaymentPage.cessArr.disabled = false;
            document.editPaymentPage.totalArrears.disabled = false;
            document.editPaymentPage.totalArrears.readOnly = true;
            document.editPaymentPage.penalty.disabled = false;
            document.editPaymentPage.totalAmount.disabled = false;
            document.editPaymentPage.totalAmount.readOnly = true;
            document.editPaymentPage.refNum.disabled = false
        }
        else if(!document.editPaymentPage.edit.checked)
        {
            document.editPaymentPage.deleteIt.disabled = false;

            document.editPaymentPage.receiptNo.disabled = true;
            document.editPaymentPage.dateOfPayment.disabled = true;
            document.editPaymentPage.ptCurr.disabled = true;
            document.editPaymentPage.cessCurr.disabled = true;
            document.editPaymentPage.totalCurrent.disabled = true;
            document.editPaymentPage.ptArr.disabled = true;
            document.editPaymentPage.cessArr.disabled = true;
            document.editPaymentPage.totalArrears.disabled = true;
            document.editPaymentPage.penalty.disabled = true;
            document.editPaymentPage.totalAmount.disabled = true;
            document.editPaymentPage.refNum.disabled = true
        }

    
    }

}
function chkBoxValidate1()
{
    
    if(document.editPaymentPage.deleteIt.length >0)
    {
        for (var i=0;i<document.editPaymentPage.deleteIt.length;i++)
        {
            if(document.editPaymentPage.deleteIt[i].checked)
            {
                //alert('Here111'+i);//+document.editPaymentPage.delete[i].value);
                document.editPaymentPage.edit[i].disabled = true;
            }
            else if(!document.editPaymentPage.deleteIt[i].checked)
	    {
		 document.editPaymentPage.edit[i].disabled = false;
            }

        }
    }
    else
    {
        if(document.editPaymentPage.deleteIt.checked)
        {
            document.editPaymentPage.edit.disabled = true;
        }
        else if(!document.editPaymentPage.deleteIt.checked)
	{
	 	document.editPaymentPage.edit.disabled = false;
	}
    
    }

}

function validateEditPaymentForm()
{
    var temp = 0;
    //alert('temp :'+temp);
    if(document.editPaymentPage.edit.length >0)
    {
        
        for(var i=0;i<document.editPaymentPage.edit.length;i++)
        {
            if(document.editPaymentPage.edit[i].checked)
            {
                temp = 1;
                if(document.editPaymentPage.receiptNo[i].disabled==false)
                {
                    var receiptNoObj = document.editPaymentPage.receiptNo[i];
                    //if(!validateNotEmpty(receiptNo)
                    if(trim(receiptNoObj,receiptNoObj.value) == "")
                    {
                        alert('Plase Enter ReceiptNo');
                        //document.editPaymentPage.receiptNo[i].value="";
                        document.editPaymentPage.receiptNo[i].focus();
                        return false;

                    }
                    
                }
                if(document.editPaymentPage.dateOfPayment[i].disabled==false)
                {
                    var dateOfPaymentObj = document.editPaymentPage.dateOfPayment[i];
                    if(trim(dateOfPaymentObj,dateOfPaymentObj.value) == "")
                    {
                        alert('Plase Enter Date Of Payment');
                        document.editPaymentPage.dateOfPayment[i].focus();
                        return false;
                    
                    }
                    if(!validateEnteredDate(document.editPaymentPage.dateOfPayment[i].value))
                    {
                        alert('Plase Enter Valid Date Of Payment');
                        document.editPaymentPage.dateOfPayment[i].value="";
                        document.editPaymentPage.dateOfPayment[i].focus();
                        return false;
                    }
                    if(!validateDate(document.editPaymentPage.dateOfPayment[i].value))
		    {
			alert('Date Of Payment should not be higher than Current Date');
			document.editPaymentPage.dateOfPayment[i].value="";
			document.editPaymentPage.dateOfPayment[i].focus();
			return false;
                    }
                }
                if(document.editPaymentPage.ptCurr[i].disabled==false && document.editPaymentPage.ptCurr[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.ptCurr[i].value))
                    {
                        alert('Current PT Should be Numeric');
                        document.editPaymentPage.ptCurr[i].value="";
                        document.editPaymentPage.ptCurr[i].focus();
                       return false;
                    }
                                
                }
                if(document.editPaymentPage.cessCurr[i].disabled==false && document.editPaymentPage.cessCurr[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.cessCurr[i].value))
                    {
                        alert('Current Cess Should be Numeric');
                        document.editPaymentPage.cessCurr[i].value="";
                        document.editPaymentPage.cessCurr[i].focus();
                        return false;
                    }
                                                
                }
                if(document.editPaymentPage.totalCurrent[i].disabled==false && document.editPaymentPage.totalCurrent[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.totalCurrent[i].value))
                    {
                        alert('Current Total Should be Numeric');
                        document.editPaymentPage.totalCurrent[i].value="";
                        document.editPaymentPage.totalCurrent[i].focus();
                        return false;

                    }
                
                }
                if(document.editPaymentPage.ptArr[i].disabled==false && document.editPaymentPage.ptArr[i].value!="")
                {
                    //alert('here '+document.editPaymentPage.ptArr[i].value);
                    if(isNaN(document.editPaymentPage.ptArr[i].value))
                    {
                        alert('Arrears PT Should be Numeric');
                        document.editPaymentPage.ptArr[i].value="";
                        document.editPaymentPage.ptArr[i].focus();
                        return false;
                    }

                }
                
                if(document.editPaymentPage.cessArr[i].disabled==false && document.editPaymentPage.cessArr[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.cessArr[i].value))
                    {
                        alert('Arrears Cess Should be Numeric');
                        document.editPaymentPage.cessArr[i].value="";
                        document.editPaymentPage.cessArr[i].focus();
                        return false;
                    }

                }
                if(document.editPaymentPage.totalArrears[i].disabled==false && document.editPaymentPage.totalArrears[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.totalArrears[i].value))
                    {
                        alert('ArrearsTotal Should be Numeric');
                        document.editPaymentPage.totalArrears[i].value="";
                        document.editPaymentPage.totalArrears[i].focus();
                        return false;
                    }

                }
                if(document.editPaymentPage.penalty[i].disabled==false && document.editPaymentPage.penalty[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.penalty[i].value))
                    {
                        alert('Penalty Should be Numeric');
                        document.editPaymentPage.penalty[i].value="";
                        document.editPaymentPage.penalty[i].focus();
                        return false;
                    }

                }   
                
                if(document.editPaymentPage.totalAmount[i].disabled==false && document.editPaymentPage.totalAmount[i].value!="")
                {
                    if(isNaN(document.editPaymentPage.totalAmount[i].value))
                    {
                        alert('TotalAmount Should be Numeric');
                        document.editPaymentPage.totalAmount[i].value="";
                        document.editPaymentPage.totalAmount[i].focus();
                        return false;
                    }
                
                }   
                if(document.editPaymentPage.refNum[i].disabled==false)
				{
					var refNumObj = document.editPaymentPage.refNum[i];
					if(trim(refNumObj,refNumObj.value) == "")
					{
						alert('Plase Enter Reference Number');
						//document.editPaymentPage.refNum[i].value="";
						document.editPaymentPage.refNum[i].focus();
						return false;

					}

                }
                
            }
            
        }
        
        if(document.editPaymentPage.deleteIt.length >0)
        {
            for (var j=0;j<document.editPaymentPage.deleteIt.length;j++)
            {
                if(document.editPaymentPage.deleteIt[j].checked)
                {
                    temp=1;
                }

            }
        }
        
        if(temp==0)
        {
            alert('Please choose Edit or Delete before proceding');
            return false;
        }
        return true;
    }
    else
    {
        //alert('here in else');
        if(document.editPaymentPage.edit.checked)
        {
            if(document.editPaymentPage.receiptNo.disabled==false)
            {
                var receiptNoObj = document.editPaymentPage.receiptNo;
                //if(!validateNotEmpty(receiptNo)
                if(trim(receiptNoObj,receiptNoObj.value) == "")
                {
                    alert('Plase Enter ReceiptNo');
                    //document.editPaymentPage.receiptNo.value="";
                    document.editPaymentPage.receiptNo.focus();
                    return false;
            
                }
            
            }
            if(document.editPaymentPage.dateOfPayment.disabled==false)
            {
                var dateOfPaymentObj = document.editPaymentPage.dateOfPayment;
                if(trim(dateOfPaymentObj,dateOfPaymentObj.value) == "")
                {
                    alert('Plase Enter Date Of Payment');
                    document.editPaymentPage.dateOfPayment.focus();
                    return false;
            
                }
                if(!validateEnteredDate(document.editPaymentPage.dateOfPayment.value))
                {
                    alert('Plase Enter Valid Date Of Payment');
                    document.editPaymentPage.dateOfPayment.value="";
                    document.editPaymentPage.dateOfPayment.focus();
                    return false;
                }
                if(!validateDate(document.editPaymentPage.dateOfPayment.value))
				{
					alert('Date Of Payment should not be higher than Current Date');
					document.editPaymentPage.dateOfPayment.value="";
					document.editPaymentPage.dateOfPayment.focus();
					return false;
				}
            
            }
            if(document.editPaymentPage.ptCurr.disabled==false && document.editPaymentPage.ptCurr.value!="")
            {
                if(isNaN(document.editPaymentPage.ptCurr.value))
                {
                    alert('Current PT Should be Numeric');
                    document.editPaymentPage.ptCurr.value="";
                    document.editPaymentPage.ptCurr.focus();
                   return false;
                }
        
            }
            if(document.editPaymentPage.cessCurr.disabled==false && document.editPaymentPage.cessCurr.value!="")
            {
                if(isNaN(document.editPaymentPage.cessCurr.value))
                {
                    alert('Current Cess Should be Numeric');
                    document.editPaymentPage.cessCurr.value="";
                    document.editPaymentPage.cessCurr.focus();
                    return false;
                }
        
            }
            if(document.editPaymentPage.totalCurrent.disabled==false && document.editPaymentPage.totalCurrent.value!="")
            {
                if(isNaN(document.editPaymentPage.totalCurrent.value))
                {
                    alert('Current Total Should be Numeric');
                    document.editPaymentPage.totalCurrent.value="";
                    document.editPaymentPage.totalCurrent.focus();
                    return false;
        
                }
        
            }
            if(document.editPaymentPage.ptArr.disabled==false && document.editPaymentPage.ptArr.value!="")
            {
                //alert('here '+document.editPaymentPage.ptArr.value);
                if(isNaN(document.editPaymentPage.ptArr.value))
                {
                    alert('Arrears PT Should be Numeric');
                    document.editPaymentPage.ptArr.value="";
                    document.editPaymentPage.ptArr.focus();
                    return false;
                }
        
            }
        
            if(document.editPaymentPage.cessArr.disabled==false && document.editPaymentPage.cessArr.value!="")
            {
                if(isNaN(document.editPaymentPage.cessArr.value))
                {
                    alert('Arrears Cess Should be Numeric');
                    document.editPaymentPage.cessArr.value="";
                    document.editPaymentPage.cessArr.focus();
                    return false;
                }
        
            }
            if(document.editPaymentPage.totalArrears.disabled==false && document.editPaymentPage.totalArrears.value!="")
            {
                if(isNaN(document.editPaymentPage.totalArrears.value))
                {
                    alert('ArrearsTotal Should be Numeric');
                    document.editPaymentPage.totalArrears.value="";
                    document.editPaymentPage.totalArrears.focus();
                    return false;
                }
        
            }
            if(document.editPaymentPage.penalty.disabled==false && document.editPaymentPage.penalty.value!="")
            {
                if(isNaN(document.editPaymentPage.penalty.value))
                {
                    alert('Penalty Should be Numeric');
                    document.editPaymentPage.penalty.value="";
                    document.editPaymentPage.penalty.focus();
                    return false;
                }
        
            }   
        
            if(document.editPaymentPage.totalAmount.disabled==false && document.editPaymentPage.totalAmount.value!="")
            {
                if(isNaN(document.editPaymentPage.totalAmount.value))
                {
                    alert('TotalAmount Should be Numeric');
                    document.editPaymentPage.totalAmount.value="";
                    document.editPaymentPage.totalAmount.focus();
                    return false;
                }
        
            }
            
        }
        if(document.editPaymentPage.refNum.disabled==false)
		{
			var refNumObj = document.editPaymentPage.refNum;
			if(trim(refNumObj,refNumObj.value) == "")
			{
				alert('Plase Enter Reference Number');
				document.editPaymentPage.refNum.focus();
				return false;

			}

		}
                
        else if((!document.editPaymentPage.edit.checked) && (!document.editPaymentPage.deleteIt.checked))
        {
            alert('Please choose Edit or Delete before proceding');
            return false;
        }
        return true;    
    }
    

}

function calculateCurrTotal()
{
    if(document.editPaymentPage.edit.length >0)
    {
        for(var i=0;i<document.editPaymentPage.edit.length;i++)
        {
                //alert('this'+document.editPaymentPage.edit[i].value);
            if(document.editPaymentPage.edit[i].checked)
            {
                var ptCurr = eval(document.editPaymentPage.ptCurr[i].value);
                var cessCurr = eval(document.editPaymentPage.cessCurr[i].value);
                if(ptCurr < 0)
                {
                   alert('Please Enter Positive Value');
                   document.editPaymentPage.ptCurr[i].value = "";
                   document.editPaymentPage.ptCurr[i].value.focus();
                }
                else if(cessCurr < 0)
                {
		   alert('Please Enter Positive Value');
		   document.editPaymentPage.cessCurr[i].value = "";
		   document.editPaymentPage.cessCurr[i].value.focus();
                }
                else
                {
		  var cess  = roundoff(0.34 * ptCurr);
		  var total = roundoff(ptCurr + eval(cess));
		   document.editPaymentPage.cessCurr[i].value= cess;
		   document.editPaymentPage.cessCurr[i].readOnly = true;
		   document.editPaymentPage.totalCurrent[i].value= total;
		   document.editPaymentPage.totalCurrent[i].readOnly = true;
               	}
            }
        }
    }
    else
    {
        if(document.editPaymentPage.edit.checked)
        {
            var ptCurr = eval(document.editPaymentPage.ptCurr.value);
            var cessCurr = eval(document.editPaymentPage.cessCurr.value);
           if(ptCurr < 0)
	   {
	      alert('Please Enter Positive Value');
	      document.editPaymentPage.ptCurr.value = "";
	      document.editPaymentPage.ptCurr.value.focus();
	   }
	   else if(cessCurr < 0)
	   {
	       alert('Please Enter Positive Value');
	       document.editPaymentPage.cessCurr.value = "";
	       document.editPaymentPage.cessCurr.value.focus();
           }
           else
           {
            	var cess  = roundoff(0.34 * ptCurr);
            	var total = roundoff(ptCurr + eval(cess));
            	//alert('cessCurr'+total);
            	document.editPaymentPage.cessCurr.value= cess;
            	document.editPaymentPage.cessCurr.readOnly = true;
            	document.editPaymentPage.totalCurrent.value= total;
		        document.editPaymentPage.totalCurrent.readOnly = true;
            }
        }
        
    }

}

function calculateArrTotal()
{
    if(document.editPaymentPage.edit.length >0)
    {
        for(var i=0;i<document.editPaymentPage.edit.length;i++)
        {
                //alert('this'+document.editPaymentPage.edit[i].value);
            if(document.editPaymentPage.edit[i].checked)
            {
                var ptArr = eval(document.editPaymentPage.ptArr[i].value);
                var cessArr = eval(document.editPaymentPage.cessArr[i].value);
                if(ptArr < 0)
		{
		   alert('Please Enter Positive Value');
		   document.editPaymentPage.ptArr[i].value = "";
		   document.editPaymentPage.ptArr[i].value.focus();
		}
		else if(cessArr < 0)
		{
		   alert('Please Enter Positive Value');
		   document.editPaymentPage.cessArr[i].value = "";
		   document.editPaymentPage.cessArr[i].value.focus();
                }
                else
		{	
			var cess  = roundoff(0.34 * ptArr);
			var total = roundoff(ptArr + eval(cess));
			 document.editPaymentPage.cessArr[i].value= cess;
		     document.editPaymentPage.cessArr[i].readOnly = true;
			document.editPaymentPage.totalArrears[i].value= total;
			document.editPaymentPage.totalArrears[i].readOnly = true;
                }
            }
        }
    }
    else
    {
        if(document.editPaymentPage.edit.checked)
        {
            var ptArr = eval(document.editPaymentPage.ptArr.value);
            var cessArr = eval(document.editPaymentPage.cessArr.value);
            if(ptArr < 0)
	    {
	       alert('Please Enter Positive Value');
	       document.editPaymentPage.ptArr.value = "";
	       document.editPaymentPage.ptArr.value.focus();
	    }
	    else if(cessArr < 0)
	    {
	       alert('Please Enter Positive Values');
	       document.editPaymentPage.cessArr.value = "";
	       document.editPaymentPage.cessArr.value.focus();
	    }
	    else
	    {
		    var cess  = roundoff(0.34 * ptArr);
		    var total = roundoff(ptArr + eval(cess));
		    //alert('cessCurr'+total);
		    document.editPaymentPage.cessArr.value= cess;
		    document.editPaymentPage.cessArr.readOnly = true;
		    
		    document.editPaymentPage.totalArrears.value= total;
		    document.editPaymentPage.totalArrears.readOnly = true;
           }
        }
    
    }


}

function calcTotalAmt()
{
    if(document.editPaymentPage.edit.length >0)
    {
        for(var i=0;i<document.editPaymentPage.edit.length;i++)
        {
                //alert('this'+document.editPaymentPage.edit[i].value);
            if(document.editPaymentPage.edit[i].checked)
            {
                var totalCurrent = eval(document.editPaymentPage.totalCurrent[i].value);
                var totalArrears = eval(document.editPaymentPage.totalArrears[i].value);
                var penaltyTotal = eval(document.editPaymentPage.penalty[i].value);
                if(penaltyTotal < 0)
                {
                	alert('Please Enter Positive Values');
                	document.editPaymentPage.penalty[i].value ="";
                	document.editPaymentPage.penalty[i].value.focus();
                }
                else
                {
					var total = roundoff(totalCurrent + totalArrears + penaltyTotal);
					document.editPaymentPage.totalAmount[i].value= total;
					document.editPaymentPage.totalAmount[i].readOnly = true;
				}
            }
        }
    }
    else
    {
        if(document.editPaymentPage.edit.checked)
        {
            //alert('here calc'+document.editPaymentPage.totalCurrent.value);
            var totalCurrent = eval(document.editPaymentPage.totalCurrent.value);
            var totalArrears = eval(document.editPaymentPage.totalArrears.value);
            var penaltyTotal = eval(document.editPaymentPage.penalty.value);
             	if(penaltyTotal < 0)
				{
					alert('Please Enter Positive Value');
					document.editPaymentPage.penalty.value ="";
					document.editPaymentPage.penalty.value.focus();
				}
				else
                {
            
					var total = roundoff(totalCurrent + totalArrears + penaltyTotal);
					document.editPaymentPage.totalAmount.value= total;
					document.editPaymentPage.totalAmount.readOnly = true;
				}
        }
        
    }
    

}

function goChangePastDemand()    
{
	    
	    var pid=document.DCBcorrection.propertyidForLink.value;
	    link ="/payment/changePastDemand.jsp?propertyid="+pid;
	    win1 =window.open(link,"ChangePastDemand",'scrollbars=yes,resizable=yes,height=520,width=958,status=yes');
	    win1.moveTo(10, 10);
	    win1.focus();
	    //window.close();
	
	
}


/* This function validates Date
   checks for validation of values for date,month and year
   doesn't allow string and any format accept dd-mm-yyyy
   
   Takes in two parameters dateobject and format specification
*/
	function ValiDate(obj, format)
	{
		
		 
		if(obj.value!="")
		{
			dateBits = DateComponents(obj.value, format);
			if (dateBits == null) 
			{
			  alert('Invalid Date, Please Re-enter');
			  obj.value="";
			  obj.focus();
			  return false;
			}

			day = dateBits[0];
			month = dateBits[1];
			year = dateBits[2];

			if ((month < 1 || month > 12) || (day < 1 || day > 31)) // check month range 
			{ 
				alert('Invalid Date, Please Re-enter');
				obj.value="";
			  	obj.focus();
				return false;
			} 
			if ((month==4 || month==6 || month==9 || month==11) && day==31)
			{
				alert('Invalid Date, Please Re-enter');
				obj.value="";
			  	obj.focus();
				return false;
			}
			if (month == 2) // check for february 29th 
			{

				var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
				if (day>29 || (day==29 && !isleap)) 
				{
					alert('Invalid Date, Please Re-enter');
					obj.value="";
			  		obj.focus();
					return false;
				}
			} 
			
			if (year < 1900 || year > 2100)
			{
				alert('Invalid Date, Please Re-enter');
				obj.value="";
				obj.focus();
				return false;
			}
		}
			return true;
			
	}

/* This funcation is called during the above function, is used to split the format string 
   in to an array that lets the validation routine know the numeric value of day, month and year
   
   Takes in two parameters dateobject value and format specification
*/

	function DateComponents(dateStr, format)
	{
		
		var results = new Array();
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		
		//alert(' matchArray : ' + matchArray);
		//alert(' matchArray : ' + matchArray[1]);
		//alert(' matchArray : ' + matchArray[3]);
		//alert(' matchArray : ' + matchArray[4]);
		
		if (matchArray == null)
			return null; 
			
		// parse date into variables
		/*This Block is commented as it checks whether the entered date is in dd/mm or mm/dd format
		  Since we specify and use dd-mm this need not be included now when needed can be uncommented*/
		/*if (format.charAt(0)=="d") //format=dd/mm 
		{                  
			results[0] = matchArray[1];
			results[1] = matchArray[3];
		}

		else 
		{ 
			results[1] = matchArray[1];
			results[0] = matchArray[3]; 
		}*/

		results[0] = matchArray[1];
		results[1] = matchArray[3];
		results[2] = matchArray[4];
		
		return results;
	}

/** This function checks whether the given date is 1st April or 1st October.
*** Requirement was effective date should always be either of 1st April or 1st October
***/
function checkEffectiveDate(obj)
 {
    // alert('function checkEffectiveDate'+obj);
     if(obj.value!="")
     {
		 if(ValiDate(obj,'dd-mm-yyyy'))
		 {
				var effectiveDate = obj.value;
				var day = effectiveDate.substr(0,2);
				var month = effectiveDate.substr(3,2);
				//alert('here effectiveDate'+effectiveDate);
				//alert('here day'+day);
				//alert('here month'+month);


			   if(day != 01)
			   {
				alert("Effective Date should  be 1st April or 1st October");
				obj.focus();
				obj.value="";
				return false;
			   }

			   if((month == 04) || (month == 10))
			   {

			   }
			   else
			   {
				 alert("Effective Date should  be 1st April or 1st October");
				 obj.focus();
				 obj.value="";
				 return false;

			   } 
			   return true;
		   }
	  }
}

function validateCardNo(obj)
{
	
	cardNo = obj.value;
	var len = cardNo.length;

	     if(len!=16)
	     {
			alert('Card No should be of 16 digits. Please Check');
			obj.focus();
			return false;
	     }
	      return true;
}



/*Function to validate receipt no
The function is tested but not code reviwed and is not used anywhere
The specifications are:

The Field Receipt number can only be of the following format. 
A -> Alphabet
1-> Numeric
a} Aplhabetic character followed by 4-6 numeric chracters.
b) NO aphabetic character. Only 4-6 numeric characters.
*/

function validateReceipt(obj)
{

	receiptNo = obj.value;
	var len = receiptNo.length;

	     if(len>7)
	     {
			alert('Invalid Receipt Number,length cannot be more than 7');

	     }

	     else
	     {

	 		var firstChar=receiptNo.substr(0,1); //extracting d first character
			var rest=receiptNo.substr(1);

			//check for alpha here
			if (validateAlpha(firstChar))
			{

				var restLength=rest.length;

				if (validateNumeric(rest))
				{
				  if (restLength<=3)
				  {
						alert('Invalid receipt No.');
				  }

					else
					{
						var restNum=receiptNo.substr(2);

						if (!validateNumeric(rest))
						{

							alert('Invalid receipt No.');
						}
					 }
				}

				else
				{
					alert('Invalid receipt No.');
				}

          }

		  //when first character is numeric
		  else if (validateNumeric(firstChar))
		  {

				   var rest=receiptNo.substr(1);
				   var restlength=rest.length;

				   if (validateNumeric(rest))
				   {

						if (restlength<3)
						{
							alert('Invalid receipt No.');
						}

						else
						{
							var restNum=receiptNo.substr(2);

							if (!validateNumeric(rest))
							{

								alert('Invalid receipt No.');
							}
						}

				   }

				   else
					{
						alert('Invalid receipt No.');
					}
		  }
	   }

}

