﻿// JScript File
var dates =[];

// INITIAZIE REQUEST *******************************************
  function InitializeRequest(sender, args){
      // needed for credit card processing.
         //Disables the page from making any callbacks if another one is already in progress
     if (prm.get_isInAsyncPostBack()) {
         //prm.abortPostBack();                 //cancels current request
         args.set_cancel(true);                 //cancels new request
     }
  }

// END REQUEST *******************************************
function EndRequestHandler(sender, args) 
{ 
    if (args.get_error() == undefined) 
    {         
       $("form.jqtransform").jqTransform('async');
    } 
}

 // PAGE CHANGE *******************************************
 function pageChange(index)
 {
    $get('ctl00_cphMain_hdnPageIndex').value = index;
    $get('ctl00_cphMain_btnPaging').click();
 }
 
 // VIEW MORE DETAILS *******************************************
 function viewMoreDetails(type, value)
 {
    if(type == 'room')
    {
      $get('ctl00_cphMain_hdnRoomId').value = value;
    }
    else
    {
      $get('ctl00_cphMain_hdnProductId').value = value;
    }
    
    $get('ctl00_cphMain_btnViewMore').click();
 }
 
 // UPDATE DATE SELECTION ****************************************
 function updateDateSelection(id, index, value)
 {
    if($get(id).checked)
    {
            addDateSelection(index, value);
    }
    else
    {
      removeDateSelection(index);

    } 
 }
 // ADD DATE SELECTION *******************************************
 function addDateSelection(index, value)
 { 
    dates[index] = value;
    if($get('txtStartDate'))
    {
        if($get('txtStartDate') && $get('txtEndDate'))
        {
            if($get('txtStartDate').value != '' && $get('txtEndDate').value != '')
            {
              //x = new Date (value);
              fixStartEnd(index,value);
            }
            else if($get('txtStartDate').value == '')
            {
                 $get('txtStartDate').value = value;
            }
            else
            {
                 $get('txtEndDate').value = value;  
                 
                 startDate = new Date ($get('txtStartDate').value);
                 endDate = new Date (value);
                 if (endDate < startDate)
                 {
                    $get('txtEndDate').value = $get('txtStartDate').value;
                    $get('txtStartDate').value = value;
                 }
            }
        }
        else
        {
             //Uncheck all other items.....
            for (var k = 0; k <14; k ++)
            {
              if(k != index && $get('chk' + k))
              {
                $get('chk' + k).checked = false;
              }
            }
             $get('txtStartDate').value = value;
        }
    }
     saveDateSelection();
 } 
 
 //FUNCTION MONTH
 function monthNm(name)
 {
    var	shortMonthName = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    
    for( i = 1; i < shortMonthName.length - 1; i ++)
    {
      if(shortMonthName[i] == name)
      {
        return i;
      } 
    }
 }
 
 // REMOVE DATE SELECTION ***********************************
 function removeDateSelection(index)
 {
   if($get('txtStartDate'))
   {
        if($get('txtStartDate') && $get('txtEndDate'))
        {
            if($get('txtStartDate').value != '' && $get('txtEndDate').value != '')
            {
                $get('txtEndDate').value = '';  
            }
            else if($get('txtStartDate') != '')
            {
                  $get('txtStartDate').value = '';  
            }
        }
        else
        {
             $get('txtStartDate').value = '';
        }
    }

	saveDateSelection();
 }
 
 // UPDATE DATE SELECTION ********************
 function saveDateSelection()
 {  
  
    $get('ctl00_cphMain_hdnDates').value = '';
    var i = 0;
 
    for (j = 0; j < dates.length; j ++)
    {
         if($get('chk' + j) != null && $get('chk' + j).checked)
         {
		        $get('ctl00_cphMain_hdnDates').value += dates[j] + '|';
		     }
		  i += 1;
	  }
 }   

// INIT DATE SELECTION ********************
function initaliseDateSelection()
{
    if( $get('ctl00_cphMain_hdnDates'))
    {
        var x = $get('ctl00_cphMain_hdnDates').value.split("|");
         
        for(i = 0; i < x.length;i++)
        {
            var data = x[i].split(",");
            if (data[0] > '')
            {
                addDateSelection(data[0], data[1]);
            }
        }
    }  
}

  // FIRST ITEM ******************************
  function firstItem()
  {
    for(i = 0; i < 14; i ++)
    {
        if($get('chk' + i) != null && $get('chk' + i).checked)
        {
          return i;
        }
    }
    return 0;
  }
  
  //LAST ITEM *******************************
  function lastItem()
  {
    for(i = 14; i > -1; i --)
    {
        if($get('chk' + i) != null && $get('chk' + i).checked)
        {
          return i;
        }
    }
    return 0;
  }
  
  // FUNCTION FIX START END DATE *****************
  function fixStartEnd(newStartPosition, dateStr)
  {
    newDate = new Date (dateStr);
    startDate = new Date ($get('txtStartDate').value);
    endDate = new Date ($get('txtEndDate').value);
    var first = firstItem();
    var last = lastItem();

    if(newDate > startDate)
    {
        $get('txtEndDate').value = dateStr;
         if(newDate < endDate)
         {
            first = newStartPosition;
            newStartPosition = last + 1;
         }
         
         for(i = first + 1; i < newStartPosition; i ++)
         {
            if($get('chk' + i))
            {
             
              $get('chk' + i).checked = false;
              clearDateList(i);
            }
         }
    }
    else if(newDate < startDate)
    {
        $get('txtStartDate').value = dateStr;
        for(i = newStartPosition + 1; i < last; i ++)
        {
          if($get('chk' + i) != null)
          {
            $get('chk' + i).checked = false;
            clearDateList(i); 
          }
        }
    }
  } 
   
   
  // CLEAR DATE LIST *****************************
function clearDateList(index)
{
    for (j = 0; j < dates.length; j ++)
    {
        if(j == index)
        {
            dates[j] = '';
        }
    }
}
  
  
  // REMOVE CART ITEM **************************
  function removeCartItem(id)
  {
    if(confirm('Are you sure you wish to remove this item?'))
    {
        $get('ctl00_cphMain_hdnItemId').value = id;
        $get('ctl00_cphMain_btnUpdateCart').click();
    }
  }
  
  // SHOW PRINT *************************************
  function showPrint()
  {
     window.open('Print_Pages/print_FromWeb.aspx');
  }