/*------------------------------------------------------------------------------------------------------------------------------------------------------
  FileName:			    commonDate.js
  Description:		  Contains common javascript functions for various date isssues.

  Created By:			  ubzoi
  Date Created:		  12/09/2006
  Last Modified By:	$Author: $
  Last Modified On:	$Modtime: $

  Maintenance History:
  ------------------------------------------------------------------------------------------------------------------------------------------------------
  Date	      Who		          What
  ------------------------------------------------------------------------------------------------------------------------------------------------------
  12/09/2006	ubzoi	          Created
----------------------------------------------------------------------------------------------------------------------------------------------------*/

//Stops date control from accepting dates later then todays date
  function isDateGreater(date) 
  {
    var myDate = new Date(Date.parse(date));
    var todaysDate = new Date();
    
    if (myDate > todaysDate)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  //Compare dates to ensure to date is equal or after from date
  function DateCompare( date1, date2, ddlValue )
  {
    var startDate = FormatDate(date1.value);  
    
    var endDate =  FormatDate(date2.value);          
    //-----------------------------------------------------------------
    
    //Test dates
    if(date1.value == '' || date2.value == '')
    {
      ddlValue.selectedIndex = 0;
     }
      
    if(date1.value != '' && date2.value != '')
    {
      testDate = new Date();
      Today = new Date();    
      
      //Test if drop down list needs to be set
      if(endDate.getDate() == Today.getDate() && endDate.getMonth() == Today.getMonth()
          && endDate.getFullYear() == Today.getFullYear())
      {
        CheckForDropListMatch(startDate, ddlValue );
      } 
      else
      {
        ddlValue.selectedIndex = 0;
      }    
      /*Can be used to compare date diff
      if ( endDate < startDate )
      {
        alert("'Date To' must be greater then 'Date from'");
        date2.value = '';
      }  */    
    }
  }  
  
  //Sets the text boxes if a value is selected from range drop down list
  function SetDateFields( date1, date2, ddlValue )
  {
    var mm;
    var dd;
    var yyyy;
    Today = new Date();
    
    //1 is the current year value in drop down list
    //cant avoid hard coding this value
    if(ddlValue != 1)
    {
      var mm;
      var dd;
      var yyyy;
      Today = new Date();
     
      mm = Today.getMonth();   
      mm = FormatNumDate(mm + 1);  
      dd = FormatNumDate(Today.getDate());   
      yyyy = Today.getFullYear();      
     
      date2.value = dd + "/" + mm + "/" + yyyy;      
      
      Today.setMonth(Today.getMonth() - ddlValue);
      
      dd = FormatNumDate(Today.getDate()); 
      mm = Today.getMonth();
      mm = FormatNumDate(mm + 1);
      yyyy = Today.getFullYear();   
           
      date1.value = dd + "/" + mm + "/" + yyyy;   
    }
    
    if(ddlValue == 1)
    {       
      yyyy = Today.getFullYear();
      
      date1.value = "01/01/" + yyyy;  
           
      mm = Today.getMonth();
      mm = FormatNumDate(mm + 1);
      dd = FormatNumDate(Today.getDate());
      
      date2.value = dd + "/" + mm + "/" + yyyy;      
    }
      
  }
  
  //Formats date into dd/mm/yyyy
  function FormatDate(dteValue)
  {
    //break up the date string so we can parse it properly----------- 
    var mm;
    var dd;
    var yy;
    
    mm = dteValue.substr(3,2);
    dd = dteValue.substr(0,2);
    yy = dteValue.substr(6,4);
    
    return new Date(Date.parse(mm + "/" + dd + "/" + yy));
  }
  
  //Formats number to have leading 0 if single digit
  function FormatNumDate(intValue)
  {
     var tmpNumStr = new String(intValue);
      
      if(tmpNumStr.length == 1)
      {
        intValue = "0" + tmpNumStr;
      }
      
      return intValue;
  }
  
  //Checks date values that are entered to see if they match any
  //of the drop down list values
  function CheckForDropListMatch( date1, ddlValue )
  {
    var mm;
    var dd;
    var yyyy;   
    
    var test3Date = SetTestDate(3);   
    
    var test6Date =  SetTestDate(6);   
    
    var test12Date = SetTestDate(12);
    
    var test16Date = SetTestDate(16);        
    
    mm = date1.getMonth();    
  
    dd = date1.getDate();
   
    yyyy = date1.getFullYear();
    
    var actualDate = new Date(Date.parse(mm + "/" + dd + "/" + yyyy)); 
        
    ddlValue.selectedIndex = 0;
    SetDropDownList( test3Date.toString(), actualDate.toString(), 3, ddlValue);
    SetDropDownList( test6Date.toString(), actualDate.toString(), 6, ddlValue);
    SetDropDownList( test12Date.toString(), actualDate.toString(), 12, ddlValue);
    SetDropDownList( test16Date.toString(), actualDate.toString(), 16, ddlValue);  
  }
  
  //returns a date value with a specified number value taken of the 
  //current date's month
  function SetTestDate(setValue)
  {
    var mm;
    var dd;
    var yyyy;
    
    Today = new Date();
    
    Today.setMonth(Today.getMonth() - setValue);
   
    mm = Today.getMonth();    
  
    dd = Today.getDate();
   
    yyyy = Today.getFullYear();
    
    return new Date(Date.parse(mm + "/" + dd + "/" + yyyy));
  }
  
  //Sets the value of a drop down list
  function SetDropDownList( testValue, actualValue, setValue, ddList  )
  {
    if(testValue == actualValue)
    {
      for (var i=0;i<ddList.options.length;i++) 
      {
          if (ddList.options[i].value == setValue)
          {
            ddList.options[i].selected = true;
            break;
          }
      }
    }  
  }

  //Shows and hides calendar
  function CalendarDisplay( displayControl, lnkControl, txtClassDisplay )
  {
    var displayID = document.getElementById(displayControl);
    var linkID = document.getElementById(lnkControl);
    var txtID = document.getElementById(txtClassDisplay);
    var str = linkID.innerHTML
    str = str.replace("Hide Search", "")
    str = str.replace("Show Search", "")
   
    linkID.innerHTML = str + ' ' + displayID.className + ' Search';
    
    if(displayID.className == 'Hide')
    {
      txtID.value =  'Show';
      displayID.className = 'Show';
    }
   else
    {
      displayID.className = 'Hide';
      txtID.value =  'Hide';
    }
    return false;
    
  }
