/* 
 * Company : Holidayzone Pty Ltd Melbourne Australia
 * Created on :25/05/2010   4:01:46 PM
  
 */
var date_display_counter=7;// display date limit.
var unavailable_rooms_date_display_counter=7;// display date limit for unavailable rooms.
var days_array=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var month_array=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var display=true;
var timer1,timer2;

// this will load checkin date picker in basic search part

$(function() {
		$("#checkin").datepicker(
                {

                    showOn: 'button',
                    buttonImage: 'images/cal.gif',
                    buttonImageOnly: true,
                     buttonText:"Calendar To Select Date",
                    duration: 'fast',
                    showAnim:'',
                    numberOfMonths: 1,
		    showButtonPanel: false,
                    maxDate: '+31D +14M',
                    //minDate: 0,
                    dateFormat:'yy-mm-dd',
                    altField: '#checkin_month_year',
                    altFormat: 'yy-mm',
                    onClose: update_checkin

                }
            );
	});

  // this will load checkout date picker in basic search part
        $(function() {
		$("#checkout").datepicker(
                {

                    showOn: 'button',
                    buttonImage: 'images/cal.gif',
                    buttonImageOnly: true,
                     buttonText:"Calendar To Select Date",
                    duration: 'fast',
                    showAnim:'',
                    numberOfMonths: 1,
		    showButtonPanel: false,
                    maxDate: '+31D +14M',
                    //minDate: 0,
                    dateFormat:'yy-mm-dd',
                    altField: '#checkout_month_year',
                    altFormat: 'yy-mm',
                    onClose: update_checkout

                }
            );
	});
        // This function will update checkin date and validate the checkin date in basic search part
function update_checkin(date) {
  // only take day part
    $('#checkin_day').val(date.substring(8));
     // clear all fields
    document.getElementById("checkin_day").className="";
    document.getElementById("checkin_month_year").className="";
    // clear all divs
    document.getElementById("error_checkin_day").innerHTML="";
    document.getElementById("error_checkin_month_year").innerHTML="";
    // get the checkin date
    var checkin_date=document.getElementById("checkin").value;
    // validate checkin date
    if(!check_checkin_date(checkin_date))
        {
                process_error("checkin_month_year:Checkin date cannot be in past.,checkin_day:");

        }

     else
         {
                            var checkout_date=document.getElementById("checkout").value;
                if(check_dates(checkin_date,checkout_date))
                {
                    var nights=days_between(checkin_date,checkout_date);
                   if(nights<180)
                       {
                            document.getElementById("nights").innerHTML=nights;
                       }
                       else
                           {
                              var info_string="Length of stay cannot be more than 180 nights.\nIf you want to book hotel for more than 180 nights then please contact the administrator.";
                                       alert(info_string);
                           }
                }

         }

}
// This function will update checkout date and validate the checkout date in basic search part
function update_checkout(date) {
    $('#checkout_day').val(date.substring(8));
    // clear all fields
    document.getElementById("checkout_day").className="autowidth";
    document.getElementById("checkout_month_year").className="autowidth";
    // clear all divs
    document.getElementById("error_checkout_day").innerHTML="";
    document.getElementById("error_checkout_month_year").innerHTML="";

    var checkin_date=document.getElementById("checkin").value;
    var checkout_date=document.getElementById("checkout").value;
    if(check_dates(checkin_date,checkout_date))
    {
        // calculate nights.
        var nights=days_between(checkin_date,checkout_date);
          if(nights<180)
                       {
                            document.getElementById("nights").innerHTML=nights;
                       }
                       else
                           {
                           var info_string="Length of stay cannot be more than 180 nights.\nIf you want to book hotel for more than 180 nights then please contact the administrator.";
                                        alert(info_string);
                           }
    }
    else
        {
            process_error("checkout_month_year:Checkout date must be after checkin date.,checkout_day:");
        }
}
/** This function will clear error divs and gets the value of checkin and checkout date from the hidden fields.
 * The function calls get_rooms function to get the room details.
 */
function view_rooms()
{
    document.getElementById("view_rooms").value="Please wait...";
    document.getElementById("view_rooms").disabled=true;
    var checkin=document.getElementById("checkin").value;
    var checkout=document.getElementById("checkout").value;
    var hotel_name=document.getElementById("hotel_name").value;

    // clear error div
     document.getElementById("checkout_day").className="autowidth";
    document.getElementById("checkout_month_year").className="autowidth";
    // clear all divs
    document.getElementById("error_checkout_day").innerHTML="";
    document.getElementById("error_checkout_month_year").innerHTML="";
     document.getElementById("checkin_day").className="autowidth";
    document.getElementById("checkin_month_year").className="autowidth";
    // clear all divs
    document.getElementById("error_checkin_day").innerHTML="";
    document.getElementById("error_checkin_month_year").innerHTML="";
    var argument="checkin="+encodeURIComponent(checkin)+"&checkout="+encodeURIComponent(checkout)+"&hotel_name="+encodeURIComponent(hotel_name);
    send_ajax_request(argument, "get_rooms.php", "process_rooms");
}
/*This function will send an ajax request to get_rooms.php file to get the room details.*/
function get_rooms(checkin,checkout,hotel_name)
{
    var argument="checkin="+encodeURIComponent(checkin)+"&checkout="+encodeURIComponent(checkout)+"&hotel_name="+encodeURIComponent(hotel_name);
    send_ajax_request(argument, "get_rooms.php", "process_rooms");
   
}
/*This function will process the ajax output. It takes output as an argument.*/
function process_rooms(output)
{
     document.getElementById("view_rooms").value="New Search";
                            document.getElementById("view_rooms").disabled=false;
                            if(output=="")
                                     {
                                         alert('An error has occurred while processing your request.\nPlease try again later or contact the administrator.');
                                     }
                                     else
                                         {
                                             //  alert(output);
                                                /*******************************************************************************
                                                 *              AJAX OUTPUT
                                                 *  The output will be an array of either error message/available rooms with table.
                                                 *  The key of an output array will decide which part of the code will get executed.
                                                 *  The output will be in json format.
                                                 *
                                                 **/
                                                // get the output in the array.
                                                   var output_array=eval("(" + output+ ")");

                                                   for(var status in output_array)
                                                       {

                                                               // if the key is success then display the rooms in available_rooms div.
                                                               if(status=="success")
                                                                   {
                                                                       document.getElementById("quote").innerHTML=output_array[status];
                                                                   }
                                                                // if the key is error then process the value and highlight the field/ display an error in alert.
                                                               if(status=='error')
                                                                   {
                                                                       process_error(output_array[status]);
                                                                   }
                                                       }
                                         }
}
/*This function will update configuration based on room id and number of nights.
 **/
function update_configuration(room_id)
{
    // build the id. the format is field_name_room_id. example num_rooms_1
    var num_rooms_id="num_rooms_"+room_id;
    var rates_id="rates_"+room_id;
    var num_adult_id="num_adult_"+room_id;
    var num_child_id="num_child_"+room_id;
    var max_sleep_id="max_sleep_"+room_id;
    var normal_sleep_id="normal_sleep_"+room_id;

    var total_display_id="total_display_"+room_id;

    var extra_child_cost_id="extra_child_cost_"+room_id;
    var extra_adult_cost_id="extra_adult_cost_"+room_id;
    var adult_charge_id="adult_charge_"+room_id;
    var child_charge_id="child_charge_"+room_id;
    var extra_adult_id="extra_adult_"+room_id;
    var extra_child_id="extra_child_"+room_id;
    var discount_amount_id="discount_amount_"+room_id;
    var discounted_total_display="discounted_total_display_"+room_id;
    var offer_long_term_discount_id="offer_long_term_discount_"+room_id;
    var discount_id="discount_"+room_id
    var extra_child_cost=document.getElementById(extra_child_cost_id).value;
    // if extra child or adult cost is not number then make it 0.
   if(extra_child_cost=="" || isNaN(extra_child_cost))
       {
           extra_child_cost=0;
       }
    var extra_adult_cost=document.getElementById(extra_adult_cost_id).value;
    if(extra_adult_cost=="" || isNaN(extra_adult_cost))
        {
            extra_adult_cost=0;
        }
    var num_rooms=document.getElementById(num_rooms_id).value;
    var num_adult=document.getElementById(num_adult_id).value;
    var num_child=document.getElementById(num_child_id).value;
    var max_sleep=document.getElementById(max_sleep_id).value;
    var normal_sleep=document.getElementById(normal_sleep_id).value;
    var offer_long_term_discount=document.getElementById(offer_long_term_discount_id).value;
    var discount=document.getElementById(discount_id).value;
    var rates=document.getElementById(rates_id).value;
if(num_rooms>0)
{
                ///////////////// ADJUST NUMBER OF ADULTS //////////////////////////////
                // new max_sleep is total number of rooms multiply by max_sleep .
                // set the number of adults to max sleep.
               var new_max_sleep=parseInt(max_sleep) * parseInt(num_rooms);
               var adult_selected=false;
               for(var j=document.getElementById(num_adult_id).options.length-1;j>=0;j--)
                                    {
                                            document.getElementById(num_adult_id).remove(j);
                                    }
                                    var optn = document.createElement("OPTION");


                                    for (i=1;i<=new_max_sleep;i++)
                                    {
                                            optn = document.createElement("OPTION");

                                            optn.text = i;
                                            optn.value = i;
                                            if(i==num_adult)
                                                {
                                                    optn.selected=true;
                                                    adult_selected=true;
                                                }
                                            document.getElementById(num_adult_id).options.add(optn);

                                    }
                    if(!adult_selected)
                        {
                            document.getElementById(num_adult_id).options.selectedIndex=(parseInt(normal_sleep)-1);
                        }
               ///////////////////////////  ADJUST NUMBER OF CHILDREN ////////////////////////////////
               // set the number of child to max_sleep - number of sleep.
            var child_selected=false;
            var num_children=new_max_sleep-parseInt(num_adult);
            for(j=document.getElementById(num_child_id).options.length-1;j>=0;j--)
                                    {
                                            document.getElementById(num_child_id).remove(j);
                                    }
                                    optn = document.createElement("OPTION");


                                    for (i=0;i<=num_children;i++)
                                    {
                                            optn = document.createElement("OPTION");

                                            optn.text = i;
                                            optn.value = i;
                                           if(num_child==i)
                                               {
                                                   optn.selected=true;
                                                   child_selected=true;
                                               }
                                            document.getElementById(num_child_id).options.add(optn);

                                    }
            if(!child_selected)
                {
                    num_child=0;
                }

                ///////////////////////// CALCULATE EXTRA CHILD & ADULT COST ///////////////////////////
               var new_normal_sleep=parseInt(num_rooms)*parseInt(normal_sleep);
               var extra_adult=0;
               var extra_child_charge=0;
               var extra_adult_charge=0;
               // if number of adult is greater than normal sleep then calculate extra adult.
               if(new_normal_sleep<num_adult)
                   {
                       extra_adult=num_adult - new_normal_sleep;
                   }

               extra_adult_charge=parseFloat(extra_adult_cost)*extra_adult ;
              // alert(extra_adult_charge);
               if(extra_adult>0)
                   {
                       document.getElementById(adult_charge_id).innerHTML=extra_adult_charge;
                        document.getElementById(extra_adult_id).innerHTML=extra_adult;
                   }
                   else
                        {
                            document.getElementById(extra_adult_id).innerHTML='-';
                             document.getElementById(adult_charge_id).innerHTML=extra_adult_charge;
                        }
               var extra_child=0;
               // if number of child greater than 0 then calculate extra child.
                 if(num_child>0)
               {
                       // if number of adult is greater than normal sleep then extra child=num_child.
                       if(new_normal_sleep<num_adult)
                            {
                                extra_child=num_child;
                            }
                            else
                                {
                                    // else calculate new normal sleep.
                                    var temp=new_normal_sleep - num_adult;
                                    if(temp<num_child)
                                        {
                                           // extra child equal to num_child minus new normal sleep.
                                           extra_child=num_child - temp;
                                        }

                                }
               }
                extra_child_charge=parseFloat(extra_child_cost)*extra_child ;
                if(extra_child>0)
                    {
                        document.getElementById(extra_child_id).innerHTML=extra_child;
                        document.getElementById(child_charge_id).innerHTML=extra_child_charge;
                    }
                    else
                        {
                            document.getElementById(child_charge_id).innerHTML=extra_child_charge;
                            document.getElementById(extra_child_id).innerHTML='-';
                        }
                
                ////////////////// CALCULATE TOTAL ///////////////////////////////
                // get the rate staring (comma separated string of rates stored in the hidden field) and split it on comma.
                // calculate total cost and store it in the hidden field.
                var rates_array=rates.split(',');
                var temp_total=0;
                for(var i=0; i<rates_array.length; i++)
                    {
                        temp_total=temp_total+parseFloat(rates_array[i]);
                        temp_total=temp_total+extra_adult_charge+extra_child_charge;
                    }

                temp_total=temp_total*parseInt(num_rooms);

                temp_total=temp_total.toFixed(2);
               // discount calculation
               var discount_amount=0;
               var discounted_total=0;
               if(offer_long_term_discount=="y")
                   {
                       discount_amount=temp_total*(parseInt(discount)/100);
                       discounted_total=temp_total-discount_amount;
                       discounted_total=discounted_total.toFixed(2);
                       discount_amount=discount_amount.toFixed(2);
                       if(discount_amount>0)
                           {
                               document.getElementById(discounted_total_display).innerHTML=discounted_total;
                               document.getElementById(discount_amount_id).innerHTML=discount_amount;
                           }
                   }
               // alert(temp_total);
               
                document.getElementById(total_display_id).innerHTML=temp_total;
             
}
}
/*This function will update num_adult/num_child based on room id .
 **/
function update_adult_and_child(room_id)
{
    // build the id. the format is field_name_room_id. example num_rooms_1
    var num_rooms_id="num_rooms_"+room_id;

    var num_adult_id="num_adult_"+room_id;
    var num_child_id="num_child_"+room_id;
    var max_sleep_id="max_sleep_"+room_id;
    var normal_sleep_id="normal_sleep_"+room_id;
 // if extra child or adult cost is not number then make it 0.


    var num_rooms=document.getElementById(num_rooms_id).value;
    var num_adult=document.getElementById(num_adult_id).value;
    var num_child=document.getElementById(num_child_id).value;
    var max_sleep=document.getElementById(max_sleep_id).value;
    var normal_sleep=document.getElementById(normal_sleep_id).value;

if(num_rooms>0)
{
                ///////////////// ADJUST NUMBER OF ADULTS //////////////////////////////
                // new max_sleep is total number of rooms multiply by max_sleep .
                // set the number of adults to max sleep.
               var new_max_sleep=parseInt(max_sleep) * parseInt(num_rooms);
               var adult_selected=false;
               for(var j=document.getElementById(num_adult_id).options.length-1;j>=0;j--)
                                    {
                                            document.getElementById(num_adult_id).remove(j);
                                    }
                                    var optn = document.createElement("OPTION");


                                    for (i=1;i<=new_max_sleep;i++)
                                    {
                                            optn = document.createElement("OPTION");

                                            optn.text = i;
                                            optn.value = i;
                                            if(i==num_adult)
                                                {
                                                    optn.selected=true;
                                                    adult_selected=true;
                                                }
                                            document.getElementById(num_adult_id).options.add(optn);

                                    }
                    if(!adult_selected)
                        {
                            document.getElementById(num_adult_id).options.selectedIndex=(parseInt(normal_sleep)-1);
                        }
               ///////////////////////////  ADJUST NUMBER OF CHILDREN ////////////////////////////////
               // set the number of child to max_sleep - number of sleep.
            var child_selected=false;
            var num_children=new_max_sleep-parseInt(num_adult);
            for(j=document.getElementById(num_child_id).options.length-1;j>=0;j--)
                                    {
                                            document.getElementById(num_child_id).remove(j);
                                    }
                                    optn = document.createElement("OPTION");


                                    for (i=0;i<=num_children;i++)
                                    {
                                            optn = document.createElement("OPTION");

                                            optn.text = i;
                                            optn.value = i;
                                           if(num_child==i)
                                               {
                                                   optn.selected=true;
                                                   child_selected=true;
                                               }
                                            document.getElementById(num_child_id).options.add(optn);

                                    }
            if(!child_selected)
                {
                    num_child=0;
                }


}
}
////////////////////////////////////// FUNCTIONS FOR DATE SCROLLING ////////////////////////////////

/* This function will display next dates 7 dates in rates table.
 * The function takes comma separated date value in  string format, number of nights value and checkout date
 **/
function display_nextdates(dates_string,nights,checkout_date)
{
   
       var dates_array=dates_string.split(','); // build an array of dates
       var dates_array_length=dates_array.length;
     // set the default values of the variable
     var limit=0;
     var start=0;
     var remaning_days=0;
   
     var end=0;
       var i=0;
       var j=0;
       
       var rates_array;
        // get rate information from the rates_array hidden field. Please note that this information is in the json format.
          var rate_string= document.getElementById("rates_array").value;
          var rate_information_array=eval("("+rate_string+")");
       
         for(var room_id in rate_information_array)
         {
             if((dates_array_length-date_display_counter)>7)
            {
                // check the dates length. if we have more than 7 dates then display next seven dates
                limit=date_display_counter+7;
              
                for(i=date_display_counter; i<limit; i++)
                    {
                        display_dates(room_id,dates_array[i],j)
                        
                                rates_array=rate_information_array[room_id];
                                display_rates(room_id,rates_array,dates_array[i],j,dates_array[0],checkout_date);
                             
                        j++;
                    }

            }
            else
                {
                     // if we have less than seven dates then add remaning dates for example if we have only 3 dates to display then
                     // add previous 4 dates to make total of 7 as 7 dates will be displayed in rates table.
                 
                    remaning_days=(dates_array_length-date_display_counter);
                    if(remaning_days>0)
                   {
                        var temp=7-remaning_days;
                        start=date_display_counter-temp;
                        end=start+7;
                        for(i=start; i<end; i++)
                            {
                               display_dates(room_id,dates_array[i],j);

                                     rates_array=rate_information_array[room_id];
                                    display_rates(room_id,rates_array,dates_array[i],j,dates_array[0],checkout_date);

                               j++;
                            }
                   }
                   

                }
     j=0;
     }
   
   
 if((dates_array_length-date_display_counter)>7)
     {
           date_display_counter= date_display_counter+7; // if we still have more dates then increase counter by 7

     }
     else
         {
              date_display_counter=nights; // else set counter to number of nights
         }
}
/* This function will display previous dates in rates table.
 * The function takes comma separated dates value in string format and checkout date.
 **/
function display_previousdates(dates_string,checkout_date)
{

       var dates_array=dates_string.split(','); // build an array of dates

       // set the default values of the variables

     var start=0;

    
     var end=0;
       var i=0;
       var j=0;
      
       var rates_array;


      // get rate information from the rates_array hidden field. Please note that this information is in the json format.
          var rate_string= document.getElementById("rates_array").value;
          var rate_information_array=eval("("+rate_string+")");
      for(var room_id in rate_information_array)
      { 
           // we want to display previous 7 dates so start will be current - 14 and end will be current -7
           // for example if we have displayed 14 dates than the counter will be on 14.
           // if we want to display previous 7 dates that mean we want to display dates from 0-7 in the array.
           start=date_display_counter-14;
           end=date_display_counter-7;
             if(start>0 && end>=7)
            {
              // if we have more than 7 values then display previous 7 dates

              
                for(i=start; i<end; i++)
                    {
                       //alert(dates_array[i]);
                       display_dates(room_id,dates_array[i],j)
                      

                               rates_array=rate_information_array[room_id];
                                display_rates(room_id,rates_array,dates_array[i],j,dates_array[0],checkout_date);
                            
                        j++;
                    }

            }
            else
                {

                 // else add remaning next dates to make total value of 7 dates.
               if(dates_array.length>7)
                   {
                            for(i=0; i<7; i++)
                                {

                                   display_dates(room_id,dates_array[i],j);

                                       rates_array=rate_information_array[room_id];
                                        display_rates(room_id,rates_array,dates_array[i],j,dates_array[0],checkout_date);

                                   j++;
                                }
                   }

                }
    j=0;
    }

 if(start>=0 && end>=7)
     {
           date_display_counter= date_display_counter-7; // if we have more than 7 dates then  decrease  counter by 7

     }
     else
         {
              date_display_counter=7; // else set counter =7
         }

}
/* This function will display dates in rates table.
 * The function takes room id, display date, position (position of cell (TD tag) in the table i.e 1 for first TD)
 **/
function display_dates(room_id,display_date,position)
{
    var div_id="room_date_"+room_id+"_"+position;
  
    var date_array=display_date.split('-');
     // convert javascript format i.e. YYYY/MM/DD so replace '-' with '/'

   var date1=display_date.replace(/-/gi, '/');
    var javascript_date=new Date(date1);
    var day=javascript_date.getDay();
    var month=javascript_date.getMonth();
    var date_string = date_array[2]+" "+month_array[month]+"<br><span class='day'>"+days_array[day]+"</span>";
  document.getElementById(div_id).innerHTML=date_string;
}
/* This function will display rates in rates table.
 * The function takes room id, rates_array,display date, position (position of  cell (TD tag) in the table i.e 1 for first TD), checkin date and checkout date.
 **/
function display_rates(room_id,rates_array,date,position,checkin_date,checkout_date)
{
   
    var  rates_details=rates_array[date];
    
    var allotment=0;
    var stopsell="";
    var rate=0;
    var rate_string="";
    var rate_display_class="room-rate";
    var div_id="room_rate_"+room_id+"_"+position; // buid the id
    // if the date is not in the array then javascript will return undefined in that case  just display '-'
  if(typeof(rates_details)== 'undefined')
  {
      rate_string="SOLD";
       rate_display_class="room-rate-sold";
  }
  else
   {
        allotment=rates_details['availability'];
        stopsell=rates_details['avail_flag'];
        rate=rates_details['room_rate'];
    if(allotment==0 || stopsell=='n')
        {
            rate_string="SOLD";
            rate_display_class="room-rate-sold";
        }
      else
          {
                        rate_display_class="room-rate";
                        var function_inclusion='get_inclusion(this,'+room_id+',"'+checkin_date+'","'+checkout_date+'","'+date+'")';
                        rate_string="<a href='#' onMouseOver='"+function_inclusion+"' onMouseOut='hide_inclusion()'>$"+rate+"</a>";

          }
   }
      document.getElementById(div_id).innerHTML=rate_string;
      document.getElementById(div_id).className=rate_display_class;
}
/*This function will display next dates for unavailable rooms. (rooms for which date and rate is not set)
 The function takes comma separated date value in  string format, number of nights value and comma separated string of room id.
 **/
function scroll_nextdates(dates_string,nights,room_id_string)
{
      var dates_array=dates_string.split(','); // build an array of dates
       var dates_array_length=dates_array.length;
     // set the default values of the variable
     var limit=0;
     var start=0;
     var remaning_days=0;

     var end=0;
       var i=0;
       var j=0;

       var room_id_array=room_id_string.split(',');

         for(var k=0; k<room_id_array.length; k++)
         {
             var room_id=room_id_array[k];
               
            if((dates_array_length-unavailable_rooms_date_display_counter)>7)
            {
                // check the dates length. if we have more than 7 dates then display next seven dates
               
                limit=unavailable_rooms_date_display_counter+7;

                for(i=unavailable_rooms_date_display_counter; i<limit; i++)
                    {
                        display_dates(room_id,dates_array[i],j)

                        j++;
                    }

            }
            else
                {
                     // if we have less than seven dates then add remaning dates for example if we have only 3 dates to display then
                     // add previous 4 dates to make total of 7 as 7 dates will be displayed in rates table.

                    remaning_days=(dates_array_length-unavailable_rooms_date_display_counter);
                    if(remaning_days>0)
                    {
                    var temp=7-remaning_days;
                    start=unavailable_rooms_date_display_counter-temp;
                    end=start+7;
                    for(i=start; i<end; i++)
                        {
                           display_dates(room_id,dates_array[i],j);
                           j++;
                        }
                    }

                }
     j=0;
     }


 if((dates_array_length-unavailable_rooms_date_display_counter)>7)
     {
           unavailable_rooms_date_display_counter= unavailable_rooms_date_display_counter+7; // if we still have more dates then increase counter by 7

     }
     else
         {
              unavailable_rooms_date_display_counter=nights; // else set counter to number of nights
         }

}
/*This function will display previous dates for unavailable rooms.(rooms for which date and rate is not set)
 *The function takes comma separated date value in  string format and comma separated string of room id.
 **/
function scroll_previousdates(dates_string,room_id_string)
{

       var dates_array=dates_string.split(','); // build an array of dates

       // set the default values of the variables

     var start=0;


     var end=0;
       var i=0;
       var j=0;

     

      var room_id_array=room_id_string.split(',');

      for(var k=0; k<room_id_array.length; k++)
      {
           // we want to display previous 7 dates so start will be current - 14 and end will be current -7
           // for example if we have displayed 14 dates then the counter will be on 14.
           // if we want to display previous 7 dates that mean we want to display dates from 0-7 in the array.
           var room_id=room_id_array[k];
           start=unavailable_rooms_date_display_counter-14;
           end=unavailable_rooms_date_display_counter-7;
             if(start>0 && end>=7)
            {
              // if we have more than 7 values then display previous 7 dates


                for(i=start; i<end; i++)
                    {
                       //alert(dates_array[i]);
                       display_dates(room_id,dates_array[i],j)

                        j++;
                    }

            }
            else
                {

                 // else add remaning next dates to make total value of 7 dates.

                   if(dates_array.length>7)
                   {
                    for(i=0; i<7; i++)
                        {

                           display_dates(room_id,dates_array[i],j);


                           j++;
                        }
                   }

                }
    j=0;
    }

 if(start>=0 && end>=7)
     {
           unavailable_rooms_date_display_counter= unavailable_rooms_date_display_counter-7; // if we have more than 7 dates then  decrease  counter by 7

     }
     else
         {
              unavailable_rooms_date_display_counter=7; // else set counter =7
         }

}
///////////////////////////// DATE SCROLLING ENDS ///////////////////////////////////////////
/**
 * This function will display/hide room details .
 * It takes room id as an argument.
 */
function toggle_room_details(room_id)
{
    var link_id='link_room_details'+room_id;
    var row_id="room_"+room_id;
    var status=document.getElementById(row_id).style.display;
    if(status=='none')
        {
            document.getElementById(link_id).innerHTML="Hide Room Details";
            document.getElementById(link_id).title="Hide Room Details";
            document.getElementById(row_id).style.display="table-row";
        }
      if(status=='table-row')
        {
            document.getElementById(link_id).innerHTML="View Room Details";
            document.getElementById(link_id).title="View Room Details";
            document.getElementById(row_id).style.display="none";
        }
}
/*This function will get the inclusion */
function get_inclusion(obj,room_id,date)
{
          var rate_string= document.getElementById("rates_array").value;
          var rate_information_array=eval("("+rate_string+")");
          var rates_array=rate_information_array[room_id];
          var rate_details=rates_array[date];
          var normal_sleep_id="normal_sleep_"+room_id;
          var max_sleep_id="max_sleep_"+room_id;
          var extra_child_cost_id="extra_child_cost_"+room_id;
          var extra_adult_cost_id="extra_adult_cost_"+room_id;
          var room_name_id="room_name_"+room_id;
          var room_name=document.getElementById(room_name_id).value;
          var normal_sleep=document.getElementById(normal_sleep_id).value;
          var max_sleep=document.getElementById(max_sleep_id).value;
          var extra_child_cost=document.getElementById(extra_child_cost_id).value;
          var extra_adult_cost=document.getElementById(extra_adult_cost_id).value;
         
          var text = "<table align=center cellspacing = 0 cellpadding = 0 width = 100%><tr><td class=popup-box_heading align=center>"+room_name+"</td></tr><tr><td class=popup-box_info>Includes : </b>"+normal_sleep+" guest(s), "+rate_details['room_inc']+"</td></tr><tr><td class=popup-box_info><b>Min Stay : </b>"+rate_details['room_minstay']+" Night(s)<br> Extra Adult $"+extra_adult_cost+" | Extra Child $"+extra_child_cost+"</td></tr><tr><td class=popup-box_info><b>Max Sleeps : </b>"+max_sleep+ " </td></tr><tr><td class=popup-box_info><b>Availability : </b>"+rate_details['availability']+ " Room(s)</td></tr></table>";
          document.getElementById("room_inc").innerHTML=text;
         var x=top_pos(obj);
              var y=left_pos(obj);
              x=x+20;
              y=y+20;
              // display tooltip window
               document.getElementById("room_inc").style.display="block";
              document.getElementById("room_inc").style.top=x+"px";
              document.getElementById("room_inc").style.left=y+"px";
}
/* This function will return x co-ordinate of the mouse pointer.
 * This function takes event object as an argument.
 **/
function top_pos(objName)
{
  //var obj = eval("document.formm." + objName);
  var objTop=0,tempObj=objName;
  while(tempObj.offsetParent){
    objTop+=tempObj.offsetTop;
    tempObj=tempObj.offsetParent
  }
  //alert(objTop); // This returns 2 - when I expect it to return a larger number
  //alert(objName.value);
  return objTop;
}
/* This function will return y co-ordinate of the mouse pointer.
 * This function takes event object as an argument.
 **/
function left_pos(objName)
{
  //var obj = eval("document.formm." + objName);
  var objLeft=0,tempObj=objName;
  while(tempObj.offsetParent){
    objLeft+=tempObj.offsetLeft;
    tempObj=tempObj.offsetParent
  }
  //alert(objTop); // This returns 2 - when I expect it to return a larger number
  //alert(objName.value);
  return objLeft;
}
/* This function will hide inclusion div
 **/
function hide_inclusion()
{
  
  document.getElementById("room_inc").style.display="none";
  
}
/*This function will send an ajax request to insert_quote_details.php*/
function send_quote_request()
{
    // clear error divs

    document.getElementById("guest_name").className="input-style";
    document.getElementById("error_guest_name").innerHTML="";
    
     document.getElementById("email").className="input-style";
     document.getElementById("re_email").className="input-style";
    document.getElementById("error_email").innerHTML="";
    document.getElementById("error_re_email").innerHTML="";
     document.getElementById("phone").className="input-style";
    document.getElementById("error_phone").innerHTML="";
     document.getElementById("comments").className="input-style";
    document.getElementById("error_comments").innerHTML="";
     document.getElementById("unavailable_rooms").className="";
    document.getElementById("error_unavailable_rooms").innerHTML="";
    // get the details.
    var hotel=document.getElementById("hotel_name").value;
    var guest_name=document.getElementById("guest_name").value;
   
    var email=document.getElementById("email").value;
    var re_email=document.getElementById("re_email").value;
    var phone=document.getElementById("phone").value;
    var comments=document.getElementById("comments").value;
    var checkin=document.getElementById("selected_checkin").value;
    var checkout=document.getElementById("selected_checkout").value;
    var argument="";
    var i=0;
    var obj=document.getElementById("room_type"+i);
    while(obj)
        {
             var room_id=obj.value;
                   
                    var num_adult_id="num_adult_"+room_id;
                    var num_rooms_id="num_rooms_"+room_id;
                    var num_child_id="num_child_"+room_id;
                    var room_name_id="room_name_"+room_id;
                    // clear error div
                    document.getElementById(num_adult_id).className="";
                    document.getElementById("error_"+num_adult_id).innerHTML="";
                    document.getElementById(num_child_id).className="";
                    document.getElementById("error_"+num_child_id).innerHTML="";
                    document.getElementById(num_rooms_id).className="";
                    document.getElementById("error_"+num_rooms_id).innerHTML="";
            if(obj.checked)
                {
                   
                     argument=argument+"room_type[]="+encodeURIComponent(room_id);
                    var num_rooms=document.getElementById(num_rooms_id).value;
                    var num_adult=document.getElementById(num_adult_id).value;
                    var num_child=document.getElementById(num_child_id).value;
                    var room_name=document.getElementById(room_name_id).value;
                    argument=argument+"&num_rooms[]="+encodeURIComponent(num_rooms)+"&num_adult[]="+encodeURIComponent(num_adult)+"&num_child[]="+encodeURIComponent(num_child)+"&room_name[]="+encodeURIComponent(room_name)+"&";
                }
                i++;
                obj=document.getElementById("room_type"+i);
    }
    
    argument=argument+"guest_name="+encodeURIComponent(guest_name)+"&email="+encodeURIComponent(email)+"&re_email="+encodeURIComponent(re_email)+"&phone="+encodeURIComponent(phone)+"&comments="+encodeURIComponent(comments)+"&hotel="+encodeURIComponent(hotel)+"&checkin="+encodeURIComponent(checkin)+"&checkout="+encodeURIComponent(checkout);
   // alert(argument);
    var httpxml;


	try
	{
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			httpxml=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httpxml=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	function request()
	{


		if(httpxml.readyState==4)
		{
			var output=httpxml.responseText;
                      //  alert(output);
                        if(output=="")
                            {
                                alert("An error has occurred while processing your request.");
                            }
                            else
                                {
                                    if(output==1)
                                        {
                                            window.location="quote_request_complete.php?hotel="+encodeURIComponent(hotel);
                                        }
                                        else
                                            {
                                                process_error(output);
                                            }
                                }
		}
	}


     var url="insert_quote_details.php";
	httpxml.onreadystatechange=request;
	httpxml.open("POST",url,true);

//Send the proper header information along with the request
httpxml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpxml.setRequestHeader("Content-length", argument.length);
httpxml.setRequestHeader("Connection", "close")

httpxml.send(argument);
}
/*This function will display tooltip for arrow button. It takes pointer object and type.
 *if the type is 1 then it will display tooltip of left arrow and if the type is 2 then it will display tooltip of right arrow.*/
function display_tooltip(obj,message,delay,div_id,x_offset,y_offset,type,effect)
{
  
  if(display)
  {
           if(delay<=0)
        {
            delay=1000;
        }
        if(effect=='')
            {
                effect='scale';
            }
        var img='';
        var color='#FFFFAA';
        var border='1px solid #FFAD33';
        switch(type)
        {
            case 'error': img='<img src="images/critical.png" alt="Error" height="48" width="48" />';
                           color='#FFCCAA';
                           border='1px solid #FF3334';
                           break;
           case 'help': img='<img src="images/help.png" alt="Help" height="48" width="48" />';
                           color='#9FDAEE';
                           border='1px solid #2BB0D7';
                           break;
           case 'info': img='<img src="images/info.png" alt="Information" height="48" width="48" />';
                           color='#9FDAEE';
                           border='1px solid #2BB0D7';
                           break;
           case 'warning': img='<img src="images/warning.png" alt="Warning" height="48" width="48" />';
                           color='#FFFFAA';
                           border='1px solid #FFAD33';
                           break;
        }
        message=img+message;
        document.getElementById(div_id).style.backgroundColor=color;
        document.getElementById(div_id).style.border=border;
         document.getElementById(div_id).innerHTML=message;
         var x=top_pos(obj);
              var y=left_pos(obj);
              x=x+x_offset;
              y=y+y_offset;
             
              // display tooltip window
               document.getElementById(div_id).style.display="block";
               document.getElementById(div_id).style.visibility='visible';
              document.getElementById(div_id).style.top=x+"px";
              document.getElementById(div_id).style.left=y+"px";
             clearTimeout(timer2);
              timer1=setTimeout(function(){/*document.getElementById(div_id).style.display='none';*/$('#'+div_id).hide(effect);display=false; },delay);
  }
   
}
/*This function will hide the tooltip.
 *It takes div id and delay as an argument.*/
function hide_tooltip(div_id,delay,effect)
{
    clearTimeout(timer1);
    if(delay<=0)
        {
            delay=1000;
        }
       if(effect=='')
            {
                effect='scale';
            }
   if(display)
   {
        //alert(c);

        timer2=setTimeout(function(){/*document.getElementById(div_id).style.display='none';*/$('#'+div_id).hide(effect);
            },delay);
   }

    display=true;
}

