function searchOptions(mode)
{
    var searchtext;
    var searchurl;
    var responsediv;
    var sessionid;
    
    if(mode=="D")
    {
    searchtext="Destination";    
    searchurl="destination_generate.php";
    responsediv="destination_search_name";
    sessionid="sessiondestination";
    }
    if(mode=="P")
    {
    searchtext="Property";    
    searchurl="property_generate.php";
    responsediv="destination_search_propertyname";
    sessionid="sessionproperty";
    }
    if(mode=="G")
    {
    searchtext="Destination";    
    searchurl="destination_generate.php";
    responsediv="package_search_name";
    sessionid="sessionpackage";
    }

    
    var sessionval=$("#"+sessionid).val();

$.ajax({
            type:'GET',
            url:searchurl,
            dataType:'json',
            cache:false,
            success:function(data)
            {
            var selectdefault="<option value='' selected>Select "+searchtext+"</option>";    
            var searchoptions=$.map(data['results'], function( value, index ) 
                    {
                        if(sessionval==value.value)
                        {
                        val="<option value=\""+value.value+"\" selected>"+value.value+"</option>";    
                        }
                        else
                        {
                        val="<option value=\""+value.value+"\">"+value.value+"</option>";    
                        }
                        
                    return val;
                    });
           
           $('#'+responsediv).html(selectdefault+searchoptions.join(''));         
            }
        }
        );

}

$(document).ready(function()
{
searchOptions("D");    
searchOptions("P");
searchOptions("G");
   $("#destination_search_name").change(function(){
        //please note that I am grabbing the text of the select box rather than the value
        // because I dont want to disturb any functionality of the existing code
        //the default selection does not have any value assciated with it 
        // and I dont want to put value =0
      $destichosen=$("#destination_search_name option:selected").text();
      if($destichosen!="Select Destination"){
        $("#state").val('0');
        $("#district").val('0');
        $("#state").attr("disabled", true);
        $("#district").attr("disabled", true);
        
      }else{
        $("#state").attr("disabled", false);
        $("#district").attr("disabled", false);
      }  
    });
    
 
    
    $("#state").change(function(){
        var myText="Alibag";
        $("#destination_search_name").val(null) ;
        
    });   
});
$("#destination_search_name").load(function(){
 $destichosen=$("#destination_search_name option:selected").text();
 if($destichosen!="Select Destination"){
    alert($destichosen);
        $("#state").attr("disabled", true);
        $("#district").attr("disabled", true);
        
      }else{
        $("#state").attr("disabled", false);
        $("#district").attr("disabled", false);
      }     
})    
    


function load_dates()
{
//alert("1");
    var today=date();
    var todayyear=today.substring(0,4);
    var todaymonth=today.substring(5,7);
    var todaydate=today.substring(8,10);    
    var todayformat=new Date(todaymonth+"/"+todaydate+"/"+todayyear); 
    
    todayformat.setDate(todayformat.getDate()+1);    
    var proper_month=todayformat.getMonth()+1;
    if (todayformat.getDate()<10)
    {
        date_addzero="0"+todayformat.getDate();
    }
    else
    {
        date_addzero=todayformat.getDate();
    }
    if (proper_month<10)
    {
        month_addzero="0"+proper_month;
    }
    else
    {
        month_addzero=proper_month;
    }
    
    var todayformat_format=date_addzero+"-"+month_addzero+"-"+todayformat.getFullYear(); 
    //alert(todayformat_format);
    document.getElementById("destination_search_checkin").value=todayformat_format;
    
    var destination_search_checkin=document.getElementById("destination_search_checkin").value;
    var chk_in=destination_search_checkin;
    var chk_inyear=chk_in.substring(6,10);
    var chk_inmonth=chk_in.substring(3,5);
    var chk_indate=chk_in.substring(0,2);   
    var chkinformat=new Date(chk_inmonth+"/"+chk_indate+"/"+chk_inyear);
    chkinformat.setDate(chkinformat.getDate()+2); 
    chkinformat_propermonth=chkinformat.getMonth()+1;   
    
    if (chkinformat.getDate()<10)
    {
        chkin_date_addzero="0"+chkinformat.getDate();
    }
    else
    {
        chkin_date_addzero=chkinformat.getDate();
    }
    if (chkinformat_propermonth<10)
    {
        chkin_month_addzero="0"+chkinformat_propermonth;
    }
    else
    {
         chkin_month_addzero=chkinformat_propermonth;
    }
    
    var todayplustwo_format=chkin_date_addzero+"-"+chkin_month_addzero+"-"+chkinformat.getFullYear();
    document.getElementById("destination_search_checkout").value=todayplustwo_format; 


$(function(){
   
$("#destination_search_checkin").datepicker({
     minDate: +1, 
     changeMonth: true, 
     dateFormat: 'dd-mm-yy'  ,
     maxDate:+90,
     monthNamesShort: ['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.'],
     onSelect: function(dateText, inst){
               var destination_search_checkin=document.getElementById("destination_search_checkin").value;
               var selected=destination_search_checkin;              
               var selectedyear=selected.substring(6,10);
               var selectedmonth=selected.substring(3,5);
               var selecteddate=selected.substring(0,2);
    
               var the_date=new Date(selectedmonth+"/"+selecteddate+"/"+selectedyear);
               //alert(the_date);
               the_date.setDate(the_date.getDate()+1);
               
               $("#destination_search_checkout").datepicker('option', 'minDate', the_date);
               $("#destination_search_checkout").datepicker('option', 'dateFormat','dd-mm-yy');
               $("#destination_search_checkout" ).datepicker( "option", "changeMonth", true );
               var max=new Date(the_date);
               max.setDate(max.getDate()+13);
               $("#destination_search_checkout").datepicker('option', 'maxDate', max);
               document.getElementById('destination_search_checkout').value="";
               
               checkout_fill();
               } 
 });
  var destination_search_checkout=document.getElementById('destination_search_checkout').value;
 
  var chk_out=destination_search_checkout;
  
  var chk_outyear=chk_out.substring(6,10);
  var chk_outmonth=chk_out.substring(3,5);
  var chk_outdate=chk_out.substring(0,2);   
  var chkoutformat=new Date(chk_outmonth+"/"+chk_outdate+"/"+chk_outyear);
  
  var mindate=new Date(chkoutformat);
  mindate.setDate(mindate.getDate()-1);
  
  var maxdate=new Date(chkoutformat);
  maxdate.setDate(maxdate.getDate()+12);  
  $("#destination_search_checkout").datepicker({dateFormat: 'dd-mm-yy',changeMonth: true, minDate: mindate, maxDate: maxdate});
  $( "#destination_search_checkout" ).datepicker( "option", "monthNamesShort", ['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.'] );
  
});

}
function load_date()
{
    return false;
}
function checkout_fill()
{
    var destination_search_checkin=document.getElementById("destination_search_checkin").value;
    var selected=destination_search_checkin;
    var selectedyear=selected.substring(6,10);
    var selectedmonth=selected.substring(3,5);
    var selecteddate=selected.substring(0,2);
    
    var checkin=new Date(selectedmonth+"/"+selecteddate+"/"+selectedyear);
    checkin.setDate(checkin.getDate()+2);
    var monthplus=checkin.getMonth()+1;
    
    if (checkin.getDate()<10)
    {
        chkin_date_addzero="0"+checkin.getDate();
    }
    else
    {
        chkin_date_addzero=checkin.getDate();
    }
    if (monthplus<10)
    {
        chkin_month_addzero="0"+monthplus;
    }
    else
    {
         chkin_month_addzero=monthplus;
    }
    
    document.getElementById("destination_search_checkout").value=chkin_date_addzero+"-"+chkin_month_addzero+"-"+checkin.getFullYear();
}   
    
function findhotel()
{
var destination_search_name=document.getElementById("destination_search_name").value;
var destination_search_checkin=document.getElementById("destination_search_checkin").value;
var destination_search_checkout=document.getElementById("destination_search_checkout").value;

var totalroom=document.getElementById("destination_search_noofrooms").value;

var destination_search_room1_noofadults=document.getElementById("destination_search_room1_noofadults").value;
var destination_search_room1_noofchildrens=document.getElementById("destination_search_room1_noofchildrens").value;
var destination_search_room2_noofadults=document.getElementById("destination_search_room2_noofadults").value;
var destination_search_room2_noofchildrens=document.getElementById("destination_search_room2_noofchildrens").value;
var destination_search_room3_noofadults=document.getElementById("destination_search_room3_noofadults").value;
var destination_search_room3_noofchildrens=document.getElementById("destination_search_room3_noofchildrens").value;


var today=date();
var todayyear=today.substring(6,10);
var todaymonth=today.substring(3,5);
var todaydate=today.substring(0,2);    
var todayformat=new Date(todaymonth+"/"+todaydate+"/"+todayyear);  

var chk_in=destination_search_checkin;
var chk_inyear=chk_in.substring(6,10);
var chk_inmonth=chk_in.substring(3,5);
var chk_indate=chk_in.substring(0,2);    
var chkinformat=new Date(chk_inmonth+"/"+chk_indate+"/"+chk_inyear);

var chk_in14=new Date(chk_inmonth+"/"+chk_indate+"/"+chk_inyear);
chk_in14.setDate(chkinformat.getDate()+14);

var chk_out=destination_search_checkout;
var chk_outyear=chk_out.substring(6,10);
var chk_outmonth=chk_out.substring(3,5);
var chk_outdate=chk_out.substring(0,2);    
var chk_outformat=new Date(chk_outmonth+"/"+chk_outdate+"/"+chk_outyear);

if (removeSpaces(destination_search_name)==0)
 {
 alert('Enter City Name'); 
 return false;
 }
 else if (removeSpaces(destination_search_checkin)==0)
 {
  alert('Select Check in Date'); 
  return false;
 }
 else if (chkinformat<=todayformat)
 {
   alert('Check In Date should not be lesser than or equal to Today\'s Date'); 
   document.getElementById("destination_search_checkin").value="";
   document.getElementById("destination_search_checkout").value="";
   return false;
 }
 else if (removeSpaces(destination_search_checkout)==0)
 {  
  alert('Select Check Out Date'); 
   return false;
 }
 else if (chk_outformat>chk_in14)
 {
   alert('Check Out Date should not be greater than 14days from Check In Date'); 
   document.getElementById("destination_search_checkout").value="";
   return false;
 }
 else if (chk_outformat<=chkinformat)
 {
   alert('Check Out Date should not be lesser or equal to Check In Date'); 
   document.getElementById("destination_search_checkout").value="";
   return false;
 }
 else if ((totalroom==2)&&(destination_search_room2_noofadults==0))
 {
    alert('Select Number of Adults for Room 2'); 
     return false;
 }
 else if ((totalroom==3)&&(destination_search_room2_noofadults==0))
 {
     alert('Select Number of Adults for Room 2'); 
     return false;
 }
 else if ((totalroom==3)&&(destination_search_room3_noofadults==0))
 {
     alert('Select Number of Adults for Room 3'); 
     return false;
 }
  else
 {
    //alert('Searching');
    return true;   
 } 
}

function removeSpaces(string)
 {
 var removed=string.split(' ').join('');
 return removed.length;
}

function date()
{
var d = new Date();
var date=d.getDate();
if (date<10)
{
    date="0"+date;
}
else
{
    date=date;
}
var month=d.getMonth()+1;
if (month<10)
{
    month="0"+month;
}
else
{
    month=month;
}
var year=d.getFullYear();

return year+"-"+month+"-"+date;
}





function checkin_date_validation()
{
var destination_search_checkin=document.getElementById("destination_search_checkin").value;
var destination_search_checkout=document.getElementById("destination_search_checkout").value;
if (removeSpaces(destination_search_checkin)==0)
    {
    alert("Select Date-Check In");
    document.getElementById("destination_search_checkout").value="";
    }    
else
    {
    var real=date();    
    var realyear=real.substring(6,10);
    var realmonth=real.substring(3,5);
    var realdate=real.substring(0,2);
    var selected=destination_search_checkin;
    var selectedyear=selected.substring(6,10);
    var selectedmonth=selected.substring(3,5);
    var selecteddate=selected.substring(0,2);
    
    var checkin=new Date(selectedmonth+"/"+selecteddate+"/"+selectedyear);
    checkin.setDate(checkin.getDate()+2);
    var monthplus=checkin.getMonth()+1;
    document.getElementById("destination_search_checkout").value=checkin.getDate()+"-"+monthplus+"-"+checkin.getFullYear();
    

        if (selectedyear<realyear)
        {
        alert('Year should not be lesser than current year');
        document.getElementById("destination_search_checkin").value="";
        document.getElementById("destination_search_checkout").value="";
        }
        else if ((selectedyear==realyear)&&(selectedmonth<realmonth))
        {
        alert('Month should not be lesser than current month');
        document.getElementById("destination_search_checkin").value="";
        document.getElementById("destination_search_checkout").value="";
        }
        else if ((selectedyear==realyear)&&(selectedmonth==realmonth)&&(selecteddate<=realdate))
        {
        alert('Date should not be lesser than or equal to Today\'s Date');
        document.getElementById("destination_search_checkin").value="";
        document.getElementById("destination_search_checkout").value="";
        }
    }
}




/*
function checkout_date_validation()
{
var destination_search_checkin=document.getElementById("destination_search_checkin").value;
var destination_search_checkout=document.getElementById("destination_search_checkout").value;
    if(removeSpaces(destination_search_checkin)==0)
    {
    alert("Select Check-In-Date");
    document.getElementById("destination_search_checkout").value="";
    } 
    else if(removeSpaces(destination_search_checkout)==0)
    {
    alert("Select Date-CheckOut");
    }   
    else
    {
    checkin_out();  
    }
    
}

function checkin_out()
{
    var destination_search_checkin=document.getElementById("destination_search_checkin").value;
    var checkin=destination_search_checkin;
    var checkinyear=checkin.substring(0,4);
    var checkinmonth=checkin.substring(5,7);
    var checkindate=checkin.substring(8,10);
    
    var checkin14=new Date(checkinmonth+"/"+checkindate+"/"+checkinyear);
    checkin14.setDate(checkin14.getDate()+14);
    
    var destination_search_checkout=document.getElementById("destination_search_checkout").value;
    var checkout=destination_search_checkout;
    var checkoutyear=checkout.substring(0,4);
    var checkoutmonth=checkout.substring(5,7);
    var checkoutdate=checkout.substring(8,10);
    
    var checkoutprocess=new Date(checkoutmonth+"/"+checkoutdate+"/"+checkoutyear);
    var checkinprocess=new Date(checkinmonth+"/"+checkindate+"/"+checkinyear);
    
    if (checkoutprocess>checkin14)
    {
        alert('Check-Out-Date should not be more than 14days from Check-In-Date');
        document.getElementById("destination_search_checkout").value="";
    }
    else if (checkoutprocess<=checkinprocess)
    {
        alert('Check-Out-Date should not be less than or equal to Check-In-Date');
        document.getElementById("destination_search_checkout").value="";
    }
}

*/
function validate_search() 
{
///By M Gopi Vignesh
var destination=document.getElementById("destination_search_name");
var property=document.getElementById("destination_search_propertyname");
var tag=document.getElementById("tag_search_name");

var places=new Array(destination,property,tag);
var msgs=new Array("Please select, where you are going?","Please select, where do you want to stay?","Please select, what do you want to do/see?");

var optionlength=document.getElementsByName("searchtype").length;
    
    var invalidsearch=0;
    for (var i=0;i<optionlength;i++)
    {
        var check=document.getElementsByName("searchtype")[i].checked;
        var place=places[i].value; 
        if (check==true && place=="")
        {
            alert(msgs[i]); 
            places[i].focus();
            invalidsearch++;
            break;
        }
    }
    
    if (invalidsearch>0)
    {
    return false;    
    }


var today=new Date();
    
var checkin=document.getElementById("destination_search_checkin").value;
var checkin_year=checkin.substring(6,10);
var checkin_month=checkin.substring(3,5);
var checkin_date=checkin.substring(0,2);    
var chkinformat=new Date(checkin_month+"/"+checkin_date+"/"+checkin_year);

var checkout=document.getElementById("destination_search_checkout").value;
var checkout_year=checkout.substring(6,10);
var checkout_month=checkout.substring(3,5);
var checkout_date=checkout.substring(0,2);    
var chkoutformat=new Date(checkout_month+"/"+checkout_date+"/"+checkout_year);

var checkoutmax=new Date(checkin_month+"/"+checkin_date+"/"+checkin_year);
checkoutmax.setDate(chkinformat.getDate()+14);

    if(chkinformat<today || chkinformat==today || checkin=="")
    {
        alert("Check In Date cannot be lesser than today's date!");
        document.getElementById("destination_search_checkin").focus();
        invalidsearch++;
        return false;
    }
    if(chkoutformat<today || chkoutformat==today || checkout=="")
    {
        alert("Invalid Check Out Date!");
        document.getElementById("destination_search_checkout").focus();
        invalidsearch++;
        return false;
    }
    if (chkoutformat>checkoutmax)
    {
        //alert(chkoutformat+">"+checkoutmax)
        alert("Check Out Date cannot be greater 14days from Check In Date!");
        document.getElementById("destination_search_checkout").focus();
        invalidsearch++;
        return false;
    }
    
var totalroom=document.getElementById("destination_search_noofrooms"); 
var destination_search_room2_noofadults=document.getElementById("destination_search_room2_noofadults").value;
var destination_search_room3_noofadults=document.getElementById("destination_search_room3_noofadults").value;   
    
     if (totalroom.value>1 && destination_search_room2_noofadults==0)
     {
     alert('Please select number of Adults for Room 2!'); 
     invalidsearch++;
     return false;
     }
     if (totalroom.value>2 && destination_search_room3_noofadults==0)
     {
     alert('Please select number of Adults for Room 3!'); 
     invalidsearch++;
     return false;
     }
    
    if (invalidsearch==0)
    {
    //return true;    
    document.getElementById("booking").submit();
    }
}



function destination_search_room_change()
{ 
    var noofrooms=document.getElementById('destination_search_noofrooms').value;
    
   //var noofrooms="1";
    //alert(noofrooms);
    if(parseInt(noofrooms)==1)
    {
       
        document.getElementById('destination_search_room2_div').style.display="none";
        document.getElementById('destination_search_room3_div').style.display="none";
        document.getElementById('destination_search_room2_noofadults').value="0";
        document.getElementById('destination_search_room2_noofchildrens').value="0";
        document.getElementById('destination_search_room3_noofadults').value="0";
        document.getElementById('destination_search_room3_noofchildrens').value="0";
        
    }
    else if(parseInt(noofrooms)==2)
    {
        
        document.getElementById('destination_search_room2_div').style.display="block";
        document.getElementById('destination_search_room3_div').style.display="none";
        document.getElementById('destination_search_room3_noofadults').value="0";
        document.getElementById('destination_search_room3_noofchildrens').value="0";

    }
    else if(parseInt(noofrooms)==3)
    {
        //THIRD ROOM - MGopiVignesH
        
        document.getElementById('destination_search_room2_div').style.display="block";
        document.getElementById('destination_search_room3_div').style.display="block";       
    }
    else
    {
        
        document.getElementById('destination_search_room2_div').style.display="none";
        document.getElementById('destination_search_room3_div').style.display="none";
        document.getElementById('destination_search_room2_noofadults').value="0";
        document.getElementById('destination_search_room2_noofchildrens').value="0";
        document.getElementById('destination_search_room3_noofadults').value="0";
        document.getElementById('destination_search_room3_noofchildrens').value="0";
    }
}
