
/* THIS BELOW FUNCTION CHECKS THE DATE WETHER IT IS CORRECT DATE OR NOT. 
INTERNALLY IT CALLS chkdate(objName) function  */

var tempdate;

function checkdate(objName)
{
 
    var datefield = objName;
 
    if (objName.value != 'dd/mm/yyyy' && objName.value != '')
    {
       if (chkdate(objName) == false)
       {
          datefield.select();
          alert("That date is invalid.  Please fill in correct date.");
          datefield.value = "dd/mm/yyyy";
          datefield.focus();
          return false;
       }
       else 
       {  
          return true;
       }
    }
}

function chkdate(objName) {
  	
  	 var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
        var curdate=new Date();
         var comparedate;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
      
        

 	if (strDate.length < 1) {
	return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
	strDateArray = strDate.split(strSeparatorArray[intElementNr]);
	if (strDateArray.length != 3) {
	err = 1;
	return false;
	}
	else {
	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];
	}
	booFound = true;
 	  }
	}
	if (booFound == false) {
	if (strDate.length>5) {
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(2, 2);
	strYear = strDate.substr(4);
	   }
	}
      

        if (strYear.length == 3) {
	return false;
	}
	if (strYear.length == 2) {
	strYear = '20' + strYear;
	}

  
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
	err = 2;
	return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
	intMonth = i+1;
	strMonth = strMonthArray[i];
	i = 12;
	   }
	}
	if (isNaN(intMonth)) {
	err = 3;
	return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
	err = 4;
	return false;
	}
	if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
	err = 8;
	return false;
	}
	if (LeapYear(intYear) == true) {
	if (intday > 29) {
	err = 9;
	return false;
	}
	}
	else {
	if (intday > 28) {
	err = 10;
	return false;
	}
	}
	}
  
     tempdate=strDay +"/"+intMonth+"/"+strYear;         
//     datefield.value = strDay +"/"+intMonth+"/"+strYear;
     datefield.value = strMonth +"/"+strDay+"/"+strYear;
       
	return true;
	}


	function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
	}
// whitespace characters only.

function isWhitespace (s)

{   var i;



    // whitespace characters
    var whitespace = " \t\n\r";	

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }


    // All characters are whitespace.
    return true;
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
// THIS BELOW FUNCTION CHECKS WETHER ENTERED DATE IS BEFORE SYSDATE OR NOT

function doSysdateCheck(from)
{ 
 var passmonth;
var passyear;
var passday;     
 var curdate=new Date();
 var curmonth=parseInt(curdate.getMonth()+1);

 var curyear=parseInt(curdate.getYear());
 
 var curday=parseInt(curdate.getDate());
 var todayDate = curday+"/"+curmonth+"/"+curyear;
 if (chkdate(from) == false)
    {
     alert("Date is invalid.  Please fill in correct date.");
     from.value = "";
     return false;
     }
    else  
    {
    if (!fnCompareDates(from.value,todayDate))
    {
   
       alert("Date must occur before Today's  date.");
       from.value="";
       return false;
    }
    else
       { 
      return true;
       }
    
   } 
}

// THIS BELOW FUNCTION CHECKS WETHER ENTERED DATE IS BEFORE SYSDATE OR NOT

function doSysdateFutureCheck(from)
{ 
 var passmonth;
var passyear;
var passday;     
 var curdate=new Date();
 var curmonth=parseInt(curdate.getMonth()+1);

 var curyear=parseInt(curdate.getYear());
 
 var curday=parseInt(curdate.getDate());
 var todayDate = curday+"/"+curmonth+"/"+curyear;
 if (chkdate(from) == false)
    {
     alert("Date is invalid.  Please fill in correct date.");
     from.value = "";
     return false;
     }
    else  
    {
    if (!fnCompareDates(todayDate,from.value))
    {
   
       alert("Date must occur after Today's  date.");
       from.value="";
       return false;
    }
    else
       { 
      return true;
       }
    
   } 
}


/* start of  Functions written by Jayasheela */

// following function checks whether given is a leap year or not  
function leapYear(year)
{
  if (year % 4 == 0)
     return true;

  return false;
}

// following function gives no.of days in the given month of given year
function getDays(month,year)
{
    var ar = new Array(12)

    ar[0] = 31
    ar[1] = (leapYear(year)) ? 29 : 28 //February
    ar[2] = 31
    ar[3] = 30
    ar[4] = 31
    ar[5] = 30
    ar[6] = 31
    ar[7] = 31
    ar[8] = 30
    ar[9] = 31
    ar[10] = 30
    ar[11] = 31

   // return number of days in the specified month(parameter)
   return ar[month] 
}
// following function checks given date is valid instrument date or not
function checkInstrDate(instrdt)
{ 
  
   var priordate,postdate;  
   var curdate=new Date();
   var curmonth=parseInt(curdate.getMonth()+1);
   var curyear=parseInt(curdate.getYear());
   var curday=parseInt(curdate.getDate());


    if (chkdate(instrdt) == false)
    {
        alert("Date is invalid.  Please fill in correct date.");
        return false;
    }
    else  
    {
       // post date checking (end of the current month)
          postdate=curmonth+"/"+getDays(curmonth-1,curyear)+"/"+curyear; 

       // date before six months
         if ( curmonth <= 6)
         {
           curmonth= (curmonth - 6)+12;
           curyear = curyear -1;
         }   
         else
	 {
           curmonth = curmonth - 6;
         }
 
         priordate=curmonth+"/"+curday+"/"+curyear; 
            

        if (  (Date.parse(priordate) < Date.parse(tempdate))  && 
                   (Date.parse(postdate) >= Date.parse(tempdate)) )
        {
           return true;
        }
        else
        {
           alert("instrument date is invalid.Please enter valid date");
           return false;
        }
   }

} 



/* Following function checks whether the first date is greater than the 
   second date or not. if first date is greater than second date it returns
   false and else it returns true

   date values should be passed as first and second
    (ex:  fnCompareDates(txt_fromdate.value,txt_todate.value); ) */


/***   start of fnCompareDates ****/

function fnCompareDates(first,second)
{
        var val1;
        var val2;

	var k = first.indexOf("/");
        var t = first.indexOf("/",3);     
        val1 = first.substr(k+1,t-k-1) +"/"+first.substr(0,k)+"/"+first.substr(t+1,first.length);
      
	k = second.indexOf("/");
	t = second.indexOf("/",3);
	val2 = second.substr(k+1,t-k-1) +"/"+second.substr(0,k)+"/"+second.substr(t+1,second.length);
	
        if (Date.parse(val1) > Date.parse(val2))
         { 
          // alert("in if");
           return false; 
        }
        else
        { 
          //alert("in else");
          return true;           
          }
}

/***  end of fnCompareDates ****/

function dateCheck(dt)
{
   if(dt.value.length != 0)
   {
     if (dt.value != "dd/mm/yyyy")
     {
      if((dt.value.length < 6) || (dt.value.length >= 6 && !chkdate(dt)))
      {
         alert("Please fill in a valid date ");
         dt.value = "";
         return false;
      }
     } 
   }   
   return true;
}


function valFromToDate(from, to, fromName, toName, whichFld) 
{
  var name;
  if (from.value != "dd/mm/yyyy")
  {
    if (doSysdateCheck(from) == false)
    {
      from.value="dd/mm/yyyy";
      return false;
    }
  }
  if (to.value != "dd/mm/yyyy")
  {
    if (doSysdateCheck(to) == false)
    {
      to.value="dd/mm/yyyy"; 
      return false;
    }
  }
  if (from.value != "dd/mm/yyyy" && to.value != "dd/mm/yyyy")
  if (Date.parse(from.value) > Date.parse(to.value) )
  {
    {
      alert(toName+" date must occur after the "+fromName+" date.");
      if (whichFld == "from")
        from.value="dd/mm/yyyy";
      else
        to.value = "dd/mm/yyyy";
      return false;
    }
    
    return true;
  }
}


/* Following function gives the difference between the dates
   in the months

   date values should be passed as first and second
    (ex:  fn_DateDiff(txt_fromdate.value,txt_todate.value); ) */


/***   start of fn_DateDiff ****/

function fn_DateDiff(first,second)
{
        var strDate1;
        var strDate2;
	var idateDiff = 0;

	var k = first.indexOf("/");
        var t = first.indexOf("/",3);     
        strDate1 = first.substr(k+1,t-k-1) +"/"+first.substr(0,k)+"/"+first.substr(t+1,first.length);
      
	k = second.indexOf("/");
	t = second.indexOf("/",3);
	strDate2 = second.substr(k+1,t-k-1) +"/"+second.substr(0,k)+"/"+second.substr(t+1,second.length);
	
    
        datDate1= Date.parse(strDate1);
        datDate2= Date.parse(strDate2);
    
        idateDiff = Math.round((datDate2-datDate1)/(29*24*60*60*1000));    
    
        return idateDiff;           
}

/***   End of fn_DateDiff ****/








/* Following function gives the difference between the date and the system date
   in the years
   date value should be passed as date value
    (ex:  fn_getDiffinYears(txt_date.value); ) */


/********* Start of fn_getDiffinYears ******************/

function fn_getDiffinYears(datevalue)
{
     var diff = 0 ;

     var k = datevalue.indexOf("/");
     var t = datevalue.indexOf("/",3);     
     var val1 = datevalue.substr(k+1,t-k-1) +"/"+datevalue.substr(0,k)+"/"+datevalue.substr(t+1,datevalue.length);
	
     var firstDate = new Date() ;
     var secondDate = new Date(val1);

     var firstYear = firstDate.getYear();
     var secondYear = secondDate.getFullYear();
     if (firstYear < secondYear) { fisrtYear = firstYear + 100; } //Account for some browsers & Y2K
     var years = firstYear - secondYear;

     var firstMonth = firstDate.getMonth();
     var secondMonth = secondDate.getMonth();

     if (firstMonth < secondMonth) { diff = (years - 1); }
     if (firstMonth > secondMonth) { diff = years; }
     if (firstMonth == secondMonth) 
     {  
         var firstDay = firstDate.getDate();
         var secondDay = secondDate.getDate();
         if (firstDay < secondDay) { diff = (years - 1); } 
         if (firstDay >= secondDay) { diff =years; }  
     }
     return diff;
}

/******************* End of fn_getDiffinYears ******************/


/* Following function checks the difference between the date given and the system date
   is greater than 18 years or not . If not gives an alert.

   date object  should be passed as parameter

    (ex:  fn_checkBirthDay(txt_Bdate); ) */

/*******************  Start of fn_checkBirthDay ****************/
function fn_checkBirthDay(Birthday)
{
  if (doSysdateCheck(Birthday) && fn_getDiffinYears(Birthday.value) < 18)
	{
            alert("Age of the person is less than 18 years");
            return false;
	}
  return true;
  
}
/*******************  End of fn_checkBirthDay  ***************/


/* Following function checks whether the VAT commencement date is after
   the entered date or not. If the VAT Commencement date is after the entered
   date then error message is shown

   date object of the entered date and VAT Commencement date value  should be passed as parameters

    (ex:  fn_checkVATCmncDate(txt_effDate,csVATCmncDt); ) */
    
/*******************  Start of fn_checkVATCmncDate ****************/
function fn_checkVATCmncDate(EntdDt,VatCmncDt)
{
  if (!fnCompareDates(VatCmncDt,EntdDt.value))
  {
    alert(arrstrAlert[67]);
    EntdDt.value = "";
  }
}
/*******************  End of fn_checkVATCmncDate  ***************/

/*******************  MTDC - checking dd/mm/yyyy format *********/
var tmpdate;
function checkdateFormat(objName)
{
 
    var datefield = objName;
 
    if (objName.value != 'dd/mm/yyyy' && objName.value != '')
    {
       if (chkdateFormat(objName) == false)
       {
          datefield.select();
          alert("That date is invalid.  Please fill in correct date.");
          datefield.value = "dd/mm/yyyy";
          datefield.focus();
          return false;
       }
       else 
       {  
          return true;
       }
    }
}

function chkdateFormat(objName) {
  	
  	 var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
        var curdate=new Date();
         var comparedate;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
      
        

 	if (strDate.length < 1) {
	return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
	strDateArray = strDate.split(strSeparatorArray[intElementNr]);
	if (strDateArray.length != 3) {
	err = 1;
	return false;
	}
	else {
	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];
	}
	booFound = true;
 	  }
	}
	if (booFound == false) {
	if (strDate.length>5) {
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(2, 2);
	strYear = strDate.substr(4);
	   }
	}
      

        if (strYear.length == 3) {
	return false;
	}
	if (strYear.length == 2) {
	strYear = '20' + strYear;
	}

  
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
	err = 2;
	return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
	intMonth = i+1;
	strMonth = strMonthArray[i];
	i = 12;
	   }
	}
	if (isNaN(intMonth)) {
	err = 3;
	return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
	err = 4;
	return false;
	}
	if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
	err = 8;
	return false;
	}
	if (LeapYear(intYear) == true) {
	if (intday > 29) {
	err = 9;
	return false;
	}
	}
	else {
	if (intday > 28) {
	err = 10;
	return false;
	}
	}
	}
  
     tmpdate=strDay +"/"+intMonth+"/"+strYear;         
//     datefield.value = strDay +"/"+intMonth+"/"+strYear;
     datefield.value = tmpdate;
       
	return true;
	}


	function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
	}