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];

var todays    = parseInt(ChkOutday,10);
var thismonth =  parseInt(ChkOutthismonth,10); 
var thisyear  =  parseInt(ChkOutthisyear); 

var myDate    = new Date( thisyear , thismonth, todays, 0,0,0,0 ); 
var limitDate = new Date( thisyear , thismonth, todays, 0,0,0,0 );

myDate.setDate(myDate.getDate()-1);
limitDate.setDate(limitDate.getDate()+365);

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  );



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];

var intval = window.setInterval("myinit()",800);

function myinit(){
    
    if( document.getElementById('search_country') && document.getElementById('search_subregion') ){
        if( (document.getElementById('search_country').value > 0 ) && (document.getElementById('search_subregion').value == '') ){
            var tested = handle_subregion_dropdown(document.getElementById('search_country').value);
        
	}
	
	if( (document.getElementById('search_country').value > 0 )
        && (document.getElementById('search_subregion').value > 0)
        && (document.getElementById('search_subregion').disabled == true )
        ){
                document.getElementById('search_subregion').disabled = false;
        }
	
    }
    window.clearInterval(intval);
}

function myinit_OLD(){
	if( document.getElementById('search_country') && document.getElementById('search_subregion') ){
		if( (document.getElementById('search_country').value > 0 ) && (document.getElementById('search_subregion').value == '') ){
			var tested = handle_subregion_dropdown(document.getElementById('search_country').value);
		}else{
			window.clearInterval(intval);
		}
	}
}

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' ){
        if ( document.getElementById('partnerSite1') ) {
            display_occupant_country_form_partner();
        }else{
            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 display_occupant_country_form_partner(){
    
    var current_room_amount = document.getElementById( room_amount_dd_id  ).value;
    var occupant_area = document.getElementById( 'occupant_area' );
    occupant_area.removeChild(occupant_area.firstChild);
    
    var html = [];
    
    if( current_room_amount == 1 ){
        
        html.push('<div class=desinationleft_style03>');
        html.push('	<div style="float:left;width:75px;padding-top:1px;">');
        html.push('		<select class="adult_more" id="room_amount_id" name="room_amount" onchange="do_display_occupant(this.form.name);" > ');
        html.push('			<option value="1" selected="selected">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('			<option value="5">5</option>');
        html.push('		</select> ');
        html.push('	</div>');
        html.push('	');
        html.push('	<div style="float:left;width:75px;padding-top:1px;">');
        html.push('		<select class="adult_more" 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="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('	');
        html.push('	<div style="float:left;width:75px;padding-top:1px;">');
        html.push('		<select class="child_more" id="room1_child" name="room1_child" onchange="hidden_occupant(\'child\',1,this.value);" >');
        html.push('     <option value="0" selected="selected">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>');
           
    }else{
    
        //Room Amount   
        html.push('<div class=desinationleft_style03>');
	    html.push('<div style="float:left;width:75px;padding-top:1px;">');
		html.push('<select class="adult_more" id="room_amount_id" name="room_amount" onchange="do_display_occupant(this.form.name);">');
		html.push('<option value="1" selected="selected">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('<option value="5">5</option>');
		html.push('</select> ');
	    html.push('</div>');
	    html.push('<div style="float:left;width:75px;padding-top:1px;"></div>');
	    html.push('<div style="float:left;width:75px;padding-top:1px;"></div>');
        html.push('</div> ');     
        
        //Occupant
        
        var height_px = new Array('6px','5px');
        var style_px;
        
        for(var node_index=1; node_index <= current_room_amount; node_index++ ){
            
            if( node_index==1 ){
                style_px = height_px[0];
            }else{
                style_px = height_px[1];
            }
            
            html.push('<DIV class=desinationleft_style03>');
            html.push('	<DIV style="float:left;width: 75px;padding-top: '+ style_px +';">');
            html.push('		Room ' + node_index +' :');
            html.push('	</DIV>');
            html.push('	<DIV style="float:left;width: 75px;padding-top: '+ style_px + ';">');
            html.push('		<select class="adult_more" 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> ');
            html.push('			<OPTION value=2 selected="selected" >2</OPTION> ');
            html.push('			<OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION> ');
            html.push('			<OPTION value=5>5</OPTION>');
            html.push('		</SELECT> ');
            html.push('	</DIV>');
            html.push('	<DIV style="float:left;width: 75px;padding-top: '+ style_px +';">');
            html.push('		<select class="child_more" id="room' + node_index + '_child" 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('	</DIV>');
            html.push('</DIV>');
            
        }

    }
    
    
    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 ){

    var room_amount = document.getElementById( room_amount_dd_id  );
    room_amount.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) ) {

        var compare_partner = new Array();
        // test for partner checkbox
        if ( document.getElementById('partnerSite1') ) {
            compare_partner = open_compare_partner();
            //alert(compare_partner.length + ': ' + compare_partner.join());
        }

        if (compare_partner.length == 0) {
            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;
}


function open_compare_partner() {

    var baseUrl = 'http://directrooms.com/cgi/compare/transit.cgi?';

    var subregion = '&subregion=' + document.getElementById('search_subregion').value;
    var checkInDate = '&checkin=' + document.getElementById('check_in_date').value;
    var checkOutDate = '&checkout=' + document.getElementById('check_out_date').value;
    var roomAmount = '&room_amount=' + document.getElementById('room_amount_id').value;

    var roomGuest = '';
    for (var a=1; a<=document.getElementById('room_amount_id').value; a++) {
        if (document.getElementById('room' + a + '_adult')) {
            roomGuest += '&room' + a + '_adult=' + document.getElementById('room' + a + '_adult').value;
            roomGuest += '&room' + a + '_child=' + document.getElementById('room' + a + '_child').value;
        }
    }

    var searchParam = subregion + checkInDate + checkOutDate + roomAmount + roomGuest ;

    var winfeatures="[location=1,scrollbars=1,toolbar=1,menubar=1,directories=0,resizable=1,status=1]";
    var max_partner_sites= 3;

    var compare_partner = new Array();

    for (var s=1; s<=max_partner_sites; s++) {
        if ( document.getElementById('partnerSite' + s) ) {
            if(document.getElementById('partnerSite' + s).checked==true) {

                var partnerName = document.getElementById('partnerSite' + s).value;
                compare_partner.push(partnerName);

                var partnerWindow = new Object;
                partnerWindow=window.open(baseUrl + 'partnerName=' + partnerName + searchParam,partnerName,winfeatures);
                partnerWindow.blur();
                partnerWindow.opener.focus();
                window.focus();
            }
        }
        else {
            break;
        }

    }

    //array of all compare partner checked
    return compare_partner;
}

