
//building the selected date in the select inputs.
//build the select toDay  and fromDay dynamically 
//(with respect to the specific month and the specific year).


//the calendar is displayed in files "generalDetails.asp" and "modNewEditDest.asp"

//----------------------------------------------------------

function ShowCalendar(oText, urlModal ,objDay, objMonth, objYear)
{
   sFeatures=fnSetValues()
   var sReturn=showModalDialog(urlModal , "" , sFeatures)
   oText.value=sReturn
   arrDate = oText.value.split("/")
   
   month = convertMonth(arrDate[1])
   
   selectedDate(arrDate[0], objDay);
   selectedDate(month, objMonth);
   selectedDate(arrDate[2], objYear);
}

//---------------------------------------------------------

//the calendar in file "customer.asp"
function ShowCalendarInCustomer(oText, index)
{
  
   var i
   
   var oSrc = window.event.srcElement
   
   while (oSrc.tagName != "TR")
             oSrc = oSrc.parentElement
   
   oTr = oSrc
  
   var oTd = oTr.cells[index]
    
   sFeatures=fnSetValues()
   var sReturn=showModalDialog("dialogCalendar.htm" , "" , sFeatures)
   
   oText.value=sReturn
   arrDate = oText.value.split("/")
   
   month = convertMonth(arrDate[1])
   
   selectedDate(arrDate[0], oTd.children[0]);  
   selectedDate(month , oTd.children[1] );
   selectedDate(arrDate[2], oTd.children[2]);;
}

//----------------------------------------------------

function convertMonth(month)
{
   if (month == "01") month = "Jan"
   if (month == "02") month = "Feb"
   if (month == "03") month = "Mar"
   if (month == "04") month = "Apr"
   if (month == "05") month = "May"
   if (month == "06") month = "Jun"
   if (month == "07") month = "Jul"
   if (month == "08") month = "Aug"
   if (month == "09") month = "Sep"
   if (month == "10") month = "Oct"
   if (month == "11") month = "Nov"
   if (month == "12") month = "Dec"
   return month
}

//------------------------------------------------------

	function convertMonthToNumber(month)
{
   if  (month == "Jan") {month=1;return month}
   if  (month == "Feb") {month=2;return month}
   if  (month == "Mar") {month=3;return month}
   if  (month == "Apr") {month=4;return month}
   if  (month == "May") {month=5;return month}
   if  (month == "Jun") {month=6;return month}
   if  (month == "Jul") {month=7;return month}
   if  (month == "Aug") {month=8;return month}
   if  (month == "Sep") {month=9;return month}
   if  (month == "Oct") {month=10;return month}
   if  (month == "Nov") {month=11;return month}
   if  (month == "Dec") {month=12;return month}
  
}//convertMonthToNumber(month)

//--------------------------------------------------------------

  function compareDates(strDate1,strDate2)
  {
  
   /*
      Return value:
      
       1  : date2 is higher
      -1  : date 1 is higher
       0  : dates are equal
   */
  
    var arrDate1
    var strDay1
    var strMonth1
    var strYear1
    var arrDate2
    var strDay2
    var strMonth2
    var strYear2
  
    //split the dates
    
    arrDate1=strDate1.split("/")
    strDay1=parseInt(arrDate1[0],10)
    strMonth1=parseInt(convertMonthToNumber(arrDate1[1]),10)
    strYear1=parseInt(arrDate1[2],10)
    
    arrDate2=strDate2.split("/")
    strDay2=parseInt(arrDate2[0],10)
    strMonth2=parseInt(convertMonthToNumber(arrDate2[1]),10)
    strYear2=parseInt(arrDate2[2],10)
   
   //compare years
    
    if (strYear2 > strYear1)
        return 1;
    
    if (strYear2 < strYear1)
        return -1;

    // if here year are equal so we compare months
    
    if(strMonth2 > strMonth1)
       return 1;
       
     if(strMonth2 < strMonth1)
       return -1;   
     
      // if here monthsr are equal so we compare days
      
     if(strDate2 > strDate1)
         return 1;
         
      if(strDate2 < strDate1)
         return -1;    
     
      //if here dates are equal
      
      return 0;
    
  }//function

//----------------------------------------------------------

function selectedDate(date1, objSelect)
{
  for(i=0;objSelect.length>i;i++)
   {
     if(objSelect.options[i].value == date1)
      {
          objSelect.options[i].selected = true
          break
      }//if     
   }
}//function  

//------------------------------------------------------------

function buildSelectDays(numDays, objSelDay)
{
 var currentNumDays
 currentNumDays = objSelDay.options.length
 var i
 
  while(objSelDay.options.length>1)
       objSelDay.options.remove(1)
  
  for(i=1;i<=numDays;i++)   
  {
     var dayOption = document.createElement("OPTION");
	 objSelDay.options.add(dayOption);
	 if(i<10)
	 {
	   dayOption.innerText = "0"+i;
	   dayOption.value ="0"+i;
	 }
	 else
	 {
	   dayOption.innerText = i;
	   dayOption.value = i;
	 }
  }//for
   
}  //buildSelectDays()     

//----------------------------------------------------------------

function shanaMeooberet(objSelDay, objSelYear , objCalling)
{
   
   if(objSelDay.value == 29)
    {
		 if (((objSelYear.value % 4 == 0) && (objSelYear.value % 100 != 0)) || (objSelYear.value % 400 == 0))
		        return
		 else
		 {
		      alert("This date is not valid")
		      objCalling.value = ""
		      return
		 }//else 
     }//if		     
}//function

//-----------------------------------------------------------------

function checkDate(objSelDay, objSelMonth ,objSelYear, objCalling)
{

  if (objSelDay.value != "" && objSelMonth.value != "")
  {
		if(objSelDay.value == 31)
		{
		   if(objSelMonth.value == "Feb" || objSelMonth.value == "Apr" || objSelMonth.value == "Jun" ||
		      objSelMonth.value == "Sep" || objSelMonth.value == "Nov")  
		       {  
		         alert("This date is not valid")
		         objCalling.value = ""
		         return
		        }//if 
		}//if
  
		if(objSelDay.value == 30)
		{
		   if(objSelMonth.value == "Feb")
		    {
		       alert("This date does not exist")
		       objCalling.value = ""
		       return
		    }
         }//if
         
         if(objSelDay.value == 29 && objSelMonth.value == "Feb")
         {
           if(objSelYear.value != "")
           {
              shanaMeooberet(objSelDay, objSelYear, objCalling)
           }//if
         }//if
   }//if      
   
}//function

//---------------------------------------------------------------------


