var check_in_dd_id		= INIT.hotel_check_availability.element_id.check_in_dropdown_id;
var check_out_dd_id		= INIT.hotel_check_availability.element_id.check_out_dropdown_id;
var room_amount_dd_id   = INIT.hotel_check_availability.element_id.room_amount_dropdown_id;
var check_out_error_id  = INIT.hotel_check_availability.element_id.check_out_sec_id;
var error_class_name    = INIT.hotel_check_availability.element_name.error_class_name;

var strat_check_in_date	  = INIT.hotel_check_availability.initial_value.start_check_in_value;
var end_check_in_date     = INIT.hotel_check_availability.initial_value.end_check_in_value;
var default_check_in_date = INIT.hotel_check_availability.initial_value.default_check_in_value;

var strat_check_out_date  = INIT.hotel_check_availability.initial_value.start_check_out_value;
var end_check_out_date    = INIT.hotel_check_availability.initial_value.end_check_out_value;
var default_check_out_date= INIT.hotel_check_availability.initial_value.default_check_out_value;

var start_check_in_date_object  = create_date_object_from_string( strat_check_in_date );
var end_check_in_date_object    = create_date_object_from_string( end_check_in_date );
var start_check_out_date_object = create_date_object_from_string( strat_check_out_date );
var end_check_out_date_object   = create_date_object_from_string( end_check_out_date );

var room1_adult_id = INIT.hotel_check_availability.element_id.room_adult_dropdown_id;
var room1_child_id = INIT.hotel_check_availability.element_id.room_child_dropdown_id;
var check_in_cal_id = INIT.hotel_check_availability.element_id.check_in_cal_output_id;

var check_out_cal_id = 'calendarInMain';
var occupant_area_id = 'occupant_area'
var min_occupant_area_id = 'min_occupant_area'

check_in_value  = new Array();
check_out_value = new Array();
check_in_desc   = new Array();
check_out_desc  = new Array();
deafult_room_value = new Array("1");
deafult_adult_value = new Array("2","2","2","2","2");
deafult_child_value = new Array("0","0","0","0","0"); 

/*Checkin Date*/
var ChkIndateExploded = strat_check_in_date.split('-');
var ChkInthismonth = (ChkIndateExploded[1] - 1);
var ChkInthisyear  = ChkIndateExploded[0]; 
var ChkInday       = ChkIndateExploded[2];

/*CheckOut Date*/
var ChkOutdateExploded = strat_check_out_date.split('-');
var ChkOutthismonth =  (ChkOutdateExploded[1] - 1);
var ChkOutthisyear  =  ChkOutdateExploded[0]; 
var ChkOutday       =  ChkOutdateExploded[2];

check_in_value       = js_enumerate_dates_value( ChkInday   , ChkInthismonth , ChkInthisyear );
check_out_value      = js_enumerate_dates_value( ChkOutday   , ChkOutthismonth , ChkOutthisyear );

check_in_desc		 = js_enumerate_dates_description( ChkInday   , ChkInthismonth , ChkInthisyear );
check_out_desc       = js_enumerate_dates_description( ChkOutday   , ChkOutthismonth , ChkOutthisyear  );

/*
check_in_value  = js_enumerate_dates_value( start_check_in_date_object    , end_check_in_date_object );
check_out_value = js_enumerate_dates_value( start_check_out_date_object   , end_check_out_date_object );
check_in_desc   = js_enumerate_dates_desc( start_check_in_date_object     , end_check_in_date_object );
check_out_desc  = js_enumerate_dates_desc( start_check_out_date_object    , end_check_out_date_object );
*/

var check_in_option = do_create_option_dropdown(
                        check_in_value          ,
                        check_in_desc           ,
                        default_check_in_date   ,
                        check_in_dd_id );
                            
var check_out_option = do_create_option_dropdown(
                        check_out_value         ,
                        check_out_desc          ,
                        default_check_out_date  ,
                        check_out_dd_id );


document.getElementById(room_amount_dd_id).value = deafult_room_value[0];
document.getElementById( room1_adult_id ).value = deafult_adult_value[0];
document.getElementById( room1_child_id ).value = deafult_child_value[0];

function hideCalendar() {
	if(document.getElementById('CheckinCalContainer').style.display=='block'){
		setTimeout( "hideCheckinCalendar()"  , 80);
		setTimeout( "hideCheckoutCalendar()" , 80);
	}
}
function hideCheckinCalendar(){
	document.getElementById( check_in_cal_id ).style.display='none';
}

function hideCheckoutCalendar(){
	document.getElementById( check_out_cal_id ).style.display='none';
}  

function syncDate( main_dropdown_id , sec_dropdown_id ) {

    var main_dropdown 	= document.getElementById( main_dropdown_id );
    var sec_dropdown 	= document.getElementById( sec_dropdown_id );
    
    var main_date_value = main_dropdown.value;
    var sec_date_value 	= sec_dropdown.value;
    
    var main_date_object = create_date_object_from_string( main_date_value );
    var sec_date_object  = create_date_object_from_string( sec_date_value );
    
    var sync_date_value;
    
    if( main_date_object <=  sec_date_object ){
    	sync_date_value = sec_date_value;
    }
    else {
    	sync_date_value = create_next_date_string( main_date_value , 3 );
    }
    
    var set_date_value 	 = document.getElementById( sec_dropdown_id );
    set_date_value.value = sync_date_value;
    CheckoutError();
}

function CheckoutError(){

	var check_in_value  = document.getElementById( check_in_dd_id  ).value;
	var check_out_value = document.getElementById( check_out_dd_id ).value;

	var check_in_value_object  	= create_date_object_from_string( check_in_value  );
	var check_out_value_object  = create_date_object_from_string( check_out_value );
	
	var check_out_sec = document.getElementById( check_out_error_id );
	
	if( check_in_value_object > check_out_value_object ){
        check_out_sec.style.color = 'red';
	}else{
	    check_out_sec.style.color = '';
	}

}  

function do_display_occupant(form_name){

    if( form_name == 'country_check_rates' ){
        display_occupant_country_form();        
    }else if( form_name == 'hotel_check_rates' ){
        display_occupant_hotel_form();         
    }else if( form_name == 'min_country_search' ){
        display_occupant_country_min_form();    //size 156*280
    }else if( form_name == 'min2_country_search' ){
        display_occupant_country_min_form2();   //size 160*280
    }
    else{
        //Nothing   
    }
}  

function display_occupant_country_min_form(){
    //size 156*280
    var current_room_amount = document.getElementById( room_amount_dd_id  ).value;
    var occupant_area = document.getElementById( min_occupant_area_id );
    var html = [];
    
    var str_option = '';
    for(var option_value=1; option_value<=5; option_value++ ){
        if( current_room_amount == option_value ){
            str_option = str_option +  '<option value="' + option_value +'" selected=selected >'+ option_value +'</option>\n';
        }else{
            str_option = str_option +  '<option value="' + option_value +'">'+ option_value +'</option>\n';
        }
    }
        
    if( current_room_amount == 1 ){
        html.push('<div class="sectioncol1">');
        html.push('    <select class="selectroomcol1" name="room_amount" id="room_amount_id" onChange="do_display_occupant(this.form.name);" >');
        html.push(str_option);
        html.push('    </select>');
        html.push('</div>');
        html.push('<div class="section">');
        html.push('    <select class="selectroomcol2" id="room1_adult" name="room1_adult" onchange="hidden_occupant( \'adult\',1,this.value);">');
        html.push('        <option value="1">1</option>');
        html.push('        <option value="2" selected>2</option>');
        html.push('        <option value="3">3</option>');
        html.push('        <option value="4">4</option>');
        html.push('        </select>');
        html.push('</div>');
        html.push('<div class="section">');
        html.push('    <select class="selectroomcol3" id="room1_child" name="room1_child" onchange="hidden_occupant( \'child\',1,this.value);">');
        html.push('        <option value="0" selected>0</option>');
        html.push('        <option value="1">1</option>');
        html.push('        <option value="2" >2</option>');
        html.push('    </select>');
        html.push('</div>');
        
    }else{
        //Room Amount
        html.push('<div class="sectioncol1">');
        html.push('<select class="selectroomcol1" name="room_amount" id="room_amount_id" onChange="do_display_occupant(this.form.name);" >');
        html.push(str_option);
        html.push('</select></div>');
        html.push('<div class="section">&nbsp;</div>');
        html.push('<div class="section">&nbsp;</div>');
		
		//Adult and Child    
        for(var node_index=1; node_index <= current_room_amount; node_index++ ){
			html.push('<div class="sectioncol1">Room ' + node_index +  ':</div>');
			html.push('<div class="section">');
			html.push('<select class="selectroomcol2" id="room' + node_index + '_adult" name="room' + node_index + '_adult" onchange="hidden_occupant( \'adult\', ' + node_index + ',this.value);">');
			html.push('<option value="1">1</option><option value="2" selected>2</option><option value="3">3</option><option value="4">4</option>');
			html.push('</select></div>');
			html.push('<div class="section">');
			html.push('<select class="selectroomcol3" id="room' + node_index + '_child" name="room' + node_index + '_child" onchange="hidden_occupant( \'child\', ' + node_index + ',this.value);">');
			html.push('<option value="0" selected>0</option><option value="1">1</option><option value="2">2</option>');
			html.push('</select></div>');
        }
    }

    occupant_area.innerHTML = html.join('');
    set_occupant_value(current_room_amount);    
}

function display_occupant_country_min_form2(){
    //size 160*280
    var current_room_amount = document.getElementById( room_amount_dd_id  ).value;
    var occupant_area = document.getElementById( min_occupant_area_id );
    var html = [];
    
    var str_option = '';
    for(var option_value=1; option_value<=5; option_value++ ){
        if( current_room_amount == option_value ){
            str_option = str_option +  '<option value="' + option_value +'" selected=selected >'+ option_value +'</option>\n';
        }else{
            str_option = str_option +  '<option value="' + option_value +'">'+ option_value +'</option>\n';
        }
    }
    
    if( current_room_amount == 1 ){
        html.push('<div class="sectioncol1">');
        html.push('    <select class="selectroomcol1" name="room_amount" id="room_amount_id" onChange="do_display_occupant(this.form.name);" >');
        html.push(str_option);
        html.push('    </select></div>');
        html.push('<div class="leftsearch_section">');
        html.push('    <select class="selectroomcol2" id="room1_adult" name="room1_adult" onchange="hidden_occupant(\'adult\',1,this.value);">');
        html.push('        <option value="1">1</option>');
        html.push('        <option value="2" selected>2</option>');
        html.push('        <option value="3">3</option>');
        html.push('        <option value="4">4</option>');
        html.push('    </select>');
        html.push('</div>');    
        html.push('<div class="leftsearch_section">');
        html.push('    <select class="selectroomcol3" id="room1_child" name="room1_child" onchange="hidden_occupant(\'child\',1,this.value);">');
        html.push('        <option value="0" selected>0</option>');
        html.push('        <option value="1">1</option>');
        html.push('        <option value="2" >2</option>');
        html.push('    </select>');
        html.push('</div>');
    }else{
        html.push('<div>');
		html.push('	<div class="sectioncol1"><select class="selectroomcol1" name="room_amount" id="room_amount_id"  onChange="do_display_occupant(this.form.name);" >');
		html.push(str_option);
		html.push('</select></div>');
		html.push('	<div class="leftsearch_section">&nbsp;</div>');
		html.push('	<div class="leftsearch_section">&nbsp;</div>');
		html.push('</div>');
		
		for(var node_index=1; node_index <= current_room_amount; node_index++ ){
		    html.push('<div>');
    		html.push('	<div class="sectioncol1">Room '+ node_index +' :</div>');
    		html.push('	<div class="leftsearch_section">');
    		html.push('<select class="selectroomcol2" id="room' + node_index + '_adult" name="room' + node_index + '_adult" onchange="hidden_occupant( \'adult\', ' + node_index + ',this.value);">');
    		html.push('<option value="1">1</option><option value="2" selected>2</option><option value="3">3</option><option value="4">4</option>');
    		html.push('</select>');
    		html.push('</div>');
    		html.push('	<div class="leftsearch_section">');
    		html.push('<select class="selectroomcol3" id="room' + node_index + '_child" name="room' + node_index + '_child" onchange="hidden_occupant( \'child\', ' + node_index + ',this.value);">');
    		html.push('<option value="0" selected>0</option><option value="1">1</option><option value="2">2</option>');
    		html.push('</select></div>');
		    html.push('</div> ');
		}
    }
    occupant_area.innerHTML = html.join('');
    set_occupant_value(current_room_amount);   
}

function display_occupant_country_form(){
    
    var current_room_amount = document.getElementById( room_amount_dd_id  ).value;
    var occupant_area = document.getElementById( occupant_area_id );
    var html = [];
    
    for(var node_index=1; node_index <= current_room_amount; node_index++ ){

        html.push('<div style="visibility: visible;" id="room' + node_index + '">');
        html.push('<div class="room_text_number">Room ' + node_index + ':  </div>');
        html.push('<div class="sub_adult">');
        html.push('<div class="add_text_line04">');
        html.push('<select class="adult_more" id="room' + node_index + '_adult" name="room' + node_index + '_adult" ');
        html.push('onchange="hidden_occupant( \'adult\', ' + node_index + ',this.value);">');
        html.push('<option value="1">1</option>');
        html.push('<option value="2">2</option>');
        html.push('<option value="3">3</option>');
        html.push('<option value="4">4</option>');
        html.push('</select>');
        html.push('</div>');
        html.push('</div>');
        html.push('');
        html.push('<div class="sub_child">');
        html.push('<div class="add_text_line04">');
        html.push('<select class="child_more" id="room' + node_index + '_child" name="room' + node_index + '_child" '); 
        html.push('onchange="hidden_occupant( \'child\', ' + node_index + ',this.value);">');
        html.push('<option value="0">0</option>');
        html.push('<option value="1">1</option>');
        html.push('<option value="2">2</option>');
        html.push('</select>');
        html.push('</div>');
        html.push('</div>');
        html.push('</div>');
        
    }
    occupant_area.innerHTML = html.join('');
    set_occupant_value(current_room_amount);    
}

function display_occupant_hotel_form(){

    var current_room_amount = document.getElementById( room_amount_dd_id  ).value; 
    var occupant_area = document.getElementById( occupant_area_id );   
        
    var html = [];
    
    for(var node_index=1; node_index <= current_room_amount; node_index++ ){
        html.push('<!--box' + node_index + 'st-->');
        html.push('<div style="float:left;padding-bottom:3px;>');
        html.push('<!-- adults ' + node_index + 'start-->');
        html.push('<span style="width:75px;height:22px;">');									
        html.push('<select class="adult_more_h"  id="room' + node_index + '_adult"');		 
        html.push('name="room' + node_index + '_adult" onchange="hidden_occupant(\'adult\', '+ node_index + ',this.value);">');
        html.push('<option value="1">1</option>');
        html.push('<option value="2">2</option>');
        html.push('<option value="3">3</option>');
        html.push('<option value="4">4</option>');
        html.push('</select>');									
        html.push('</span>');
        html.push('<!-- adults '+ node_index + 'stop-->');
        html.push('');	
        html.push('<!-- child '+ node_index + 'start-->');
        html.push('<span style="width:75px;padding-left:56px;">');									
        html.push('<select class="child_more_h"  id="room' + node_index + '_child"'); 
        html.push('name="room' + node_index + '_child" onchange="hidden_occupant(\'child\', ' + node_index + ',this.value);">');
        html.push('<option value="0">0</option>');
        html.push('<option value="1">1</option>');
        html.push('<option value="2">2</option>');
        html.push('</select>');								
        html.push('</span>');
        html.push('<!-- child '+ node_index + 'stop-->');
        html.push('</div>');		
        html.push('<!--box end-->');
    }
    occupant_area.innerHTML = html.join('');
    set_occupant_value(current_room_amount);
    
}
 
function hidden_occupant( dd_type , dd_index , dd_value ){
        
    if( dd_type == 'adult' ){
        deafult_adult_value[dd_index-1] = dd_value;
    }
    
    if( dd_type == 'child'){
        deafult_child_value[dd_index-1] = dd_value;
    }          
} 

function set_occupant_value( room_amount_value ){
    
    for( room_index=1; room_index <= room_amount_value; room_index++ ){
        var current_room = room_index+1; 
        document.getElementById('room' + room_index + '_adult').value = deafult_adult_value[room_index-1];
        document.getElementById('room' + room_index + '_child').value = deafult_child_value[room_index-1];
    }
    
}

function form_validate_search( form_name ){

    var result_form_validate = true;
    var error_message = '';
    
    var result_destination = true;
    if( form_name == 'country_check_rates' ){
        result_destination = validate_destination();        
    }
    
    var result_departure_date    = validate_departure_date();
    var result_occupant_per_room = validate_occupant_per_room();
    var result_over_occupant     = validate_over_occupant();

    if ( (result_destination == true) && (result_departure_date == true) && (result_occupant_per_room == true) && (result_over_occupant == true) ){
        if ( is_valid_url_to_open_compare_window() ) {

            open_compare_window();
        }
                
    }else{
        
        if ( result_destination == false ) {
            error_message += error_message_destination();
        }
        
        if ( result_departure_date == false ) {
            error_message += 'Please correct the Check in and/or Check out dates\n';
        }

        if ( result_occupant_per_room == false ) {
            error_message += 'Maximum number of guests per room must not exceed 4\n';
        }

        if ( result_over_occupant == false ) {
            error_message += 'Maximum number of guests per booking must not exceed 9\n';
        }

        alert(error_message);
        result_form_validate = false;
        
    }
    
    return result_form_validate;

}
function validate_destination(){
    var result_validate_destination = true;
    
    var country_dd_value   = document.getElementById( country_dd_id ).value;
    var subregion_dd_value = document.getElementById( subregion_dd_id ).value;
    
    if( (country_dd_value == '') || (subregion_dd_value == '') ){
        result_validate_destination = false;
    }
    
    return result_validate_destination;
    
}

function validate_departure_date(){
    
    var result_departure_date = true;   
    
    var check_in_value  = document.getElementById(check_in_dd_id ).value;
    var check_out_value = document.getElementById(check_out_dd_id).value;

    var check_in_value_object  = create_date_object_from_string( check_in_value  );
    var check_out_value_object = create_date_object_from_string( check_out_value );
    
    if( check_out_value_object <= check_in_value_object ){
        result_departure_date = false;
    }

    return result_departure_date;
    
}

function validate_occupant_per_room(){
    
    var result_occupant_per_room = true;
    var room_amount_value = document.getElementById( room_amount_dd_id ).value;
    
    for(room_current = 1;room_current<= room_amount_value;room_current++){
        room_adult_value = parseInt(document.getElementById('room' + room_current + '_adult').value)
        room_child_value = parseInt(document.getElementById('room' + room_current + '_child').value)
        
        if(     ( (room_adult_value == 3) && (room_child_value == 2) )
           ||   ( (room_adult_value == 4) && (room_child_value == 1) )
           ||   ( (room_adult_value == 4) && (room_child_value == 2) )
          )
        {
            result_occupant_per_room =  false;
        }
         
    }
    return result_occupant_per_room;  
    
}

function validate_over_occupant(){
    var result_validate_over_occupant = true;   
    
    var room_amount_value = document.getElementById( room_amount_dd_id ).value;
    var sum_all_occupant = 0;
    
    for(room_current = 1;room_current<= room_amount_value;room_current++){
        
        var occupan_per_room = 0;
        room_adult_value = parseInt(document.getElementById('room' + room_current + '_adult').value)
        room_child_value = parseInt(document.getElementById('room' + room_current + '_child').value)
        
        occupan_per_room = room_adult_value + room_child_value;
        sum_all_occupant = sum_all_occupant + occupan_per_room;
        
    }
    
    if ( sum_all_occupant > 9 ) {
        result_validate_over_occupant =  false;
    }
    
    return result_validate_over_occupant; 
}

function error_message_destination(){
    
    var country_dd_value   = document.getElementById( country_dd_id ).value;
    var subregion_dd_value = document.getElementById( subregion_dd_id ).value;
    var error_string = 'Please select ';
    
    if( country_dd_value == "" && subregion_dd_value == "" ){
        error_string += 'Country and Subregion\n';
    }else if( country_dd_value != "" && subregion_dd_value == "" ){
        error_string += 'Subregion\n';
    }else if( country_dd_value == "" && subregion_dd_value != "" ){
        error_string += 'Country\n';
    }
    return error_string;

}

function open_compare_window() {
    
    var location_info;
    var inquiryType;
    var valid_to_open=false;
    
    if ( document.getElementById('search_subregion') ) {
        location_info = '?subregion_id=' + document.getElementById('search_subregion').value;
        inquiryType =  '&inquiry_type=subregion';
        valid_to_open=true;
    }
    else if ( document.getElementById('subregion_id') ) {
        location_info = '?subregion_id=' + document.getElementById('subregion_id').value;
        inquiryType =  '&inquiry_type=subregion';
        valid_to_open=true;
    }
    else {
        var hotel_id = get_hotel_id_from_url();

        if (hotel_id) {
            location_info = '?hotel_id=' + hotel_id;
            inquiryType = '&inquiry_type=hotel';
            valid_to_open=true;
        }
    }

    var checkInDate = '&checkin=' + document.getElementById('check_in_date').value;
    var checkOutDate = '&checkout=' + document.getElementById('check_out_date').value;
    var roomAmountId = '&room_amount=' + document.getElementById('room_amount_id').value;
    
    var room ='';
    for (var a=1; a<=document.getElementById('room_amount_id').value; a++) {
        if(document.getElementById('room' + a + '_adult')){
            room += '&room' + a + '_adult=' + document.getElementById('room' + a + '_adult').value;
            room += '&room' + a + '_child=' + document.getElementById('room' + a + '_child').value;
        }
    }

    var popUnderUrl = location_info + checkInDate+ checkOutDate + roomAmountId + room + inquiryType;

    var popunderProtocol = 'http://directrooms.com/cgi/compare/portal.cgi';
    var winfeatures="[location=0,scrollbars=0,toolbar=0,menubar=0,directories=0,resizable=1]";

    if (valid_to_open) {

        var win2 = new Object;
        win2=window.open(popunderProtocol+popUnderUrl,"affiliates",winfeatures);
        win2.blur();
        win2.opener.focus();
        window.focus();
    }
}


function is_valid_url_to_open_compare_window() {

return true;

    var valid_url = new RegExp("^((http(s?))\://)?directrooms.com/(egypt|morocco|seychelles|south-africa|tunisia|australia|fiji|new-zealand|vanuatu)", "i");

    if ( valid_url.test(window.top.location) ) {
        return true;
    }
    else {
        return false;
    }
}

function get_hotel_id_from_url() {
    
    var loc = window.top.location.pathname;
    var dot =  loc.split(/\./);
    var beforedot = dot[0];
    var minus =  beforedot.split(/-/);
    var lengthMinus = Math.floor(minus.length)-1;

    var hotelId;
    
    for(i=lengthMinus; i>=0; i--) {
        var fragment = minus[i];
        var anychar = String(fragment.match(/.*/));
        var digits = String(fragment.match(/\d*/));
        if(anychar.length == digits.length){
            hotelId = digits;
            break;
        }
    }
    return hotelId;
}
