var oneDay=1000*60*60*24;
var max_no_of_age_of_children = 17;
var max_no_of_room = 3;
var max_no_of_adult = 3;
var max_no_of_children = 3;
var start_index = 0;
var no_of_faq_cat = 8;
var max_question_no = 10;
var alert_message = "";

function get_city_by_country(dest_type) {

	$("#country_status_classic").html("<img src='"+SiteAdi+"images/DialogBoxImages/ui-anim_basic_16x16.gif'>");
	var country = $("#country").val();
	$.ajax({url:'AjaxComponents/CalledByJS.php', 
		type : 'POST',
		data : 'fn=getcities&country=' + country + '&dest_type=' + dest_type,
		success : function(req){
			$("#dest_classic").html(req);
			$("#country_status_classic").html("");
		}
	});
}

function get_location_by_city() {

	$("#city_status_classic").html("<img src='"+SiteAdi+"images/DialogBoxImages/ui-anim_basic_16x16.gif'>");
	var city = $("#city").val();
	var state = "";
	var country = $("#country").val();
	if (!document.getElementById('state') || $("#state").val()==""){
		$('#destination').val( city + ", " + country);
	}
	else{
		state = $("#state").val();
		$('#destination').val(city + ", " + state + ", " + country);
	}
	$.ajax({url:'AjaxComponents/CalledByJS.php', 
		type : 'POST',
		data : 'fn=getlocations&city=' + city + '&country=' + country
				+ '&state' + state,
		success : fn2
	});
}

function fn2(req) {
	$("#locations").html(req);
	$("#city_status_classic").html("");
}


function initDatePicker(){
    calculate_check_out();
}

function addOption( selectbox,text,value ) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}


function format_string_label(day){
    // alert(day);
    // alert( );
    var date=format_string_input(day);
    var date_str = date.split("/");

    var month_names= arr_months[lang].split(",");
    //    alert (month_names[8]);
    //    alert( "check " +date_str[1]);
    var result_str="";
    result_str = date_str[0]+ ", "+month_names[date_str[1]-1] + ", "+date_str[2] ;
    //    var tmp = day.toString();
    //    var  temp = tmp.split(" ");
    //    var result_str="";
    //    if(whichBrs()=="Internet Explorer")
    //        result_str = temp[0] + ",  " + temp[1] + "  " + temp[2]+ "  " + temp[5];
    //    else{
    //        result_str = temp[0] + ",  " + temp[1] + "  " + temp[2]+ "  " + temp[3];
    //    }
    return result_str;
}

function format_string_input(day){
    var month = day.getMonth()+1;
    var year = "";

    if(whichBrs()=="Internet Explorer")
        year = day.getYear();
    else{
        year = day.getYear()+1900;
    }
    var tmp = day.toString();
    // alert(tmp);
    var  temp = tmp.split(" ");
    var day="";
    // alert(temp[2].length);
    if(temp[2].length==1)
    {
        day="0"+temp[2];
    }
    else{
        day=temp[2];
    }
    var formatted_string = day + "/" + month+ "/" + year;
    //alert(formatted_string);
    return formatted_string;
}

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}


function chkName(a, b){
    if(a.value==""){
        a.value=b;
    }else{
        a.value=a.value;
    }
}

function remName(a, b){
    if(a.value==b){
        a.value="";
    }else if(a.value==""){
        a.value=b;
    }else{
        a.value=a.value;
    }
}

function send_to_search_result_page_from_index(requester_page) {
	var destination_id = $("#destination_id").val();
	if(requester_page=="ClassicSearchBox"){
		if($("#country").val()==""){
			showDialog('Error',word_check_country[lang],'error',2);
			$('#country').focus();
			return false;
		} else if($("#city").val()==""){
			showDialog('Error',word_check_city2[lang],'error',2);
			$('#city').focus();
			return false;
		}

        var day = $('#day').val();
        var month = $('#month').val();
        var year = $('#year').val();

        if(day=='day' || month=='month' || year=='year' ){
            showDialog('Error',word_invalid_date[lang],'error',2);
            return false;
        }
        var check_in_day = new Date (year,month-1,day);
        var today = new Date();
        if(check_in_day < today){
         showDialog('Error',word_past_date[lang],'error',2);
         return false;
        }
	} else if(requester_page=="QuickSearchBox") {
		var	day 	= $('#day').val();
        var month 	= $('#month').val();
        var year 	= $('#year').val();

        if(day=='day' || month=='month' || year=='year' ){
        	showDialog('Error',word_invalid_date[lang],'error',2);
        	return false;
        }
        var check_in_day = new Date (year,month-1,day);
        var today = new Date();
        if(check_in_day < today){
        	showDialog('Error',word_past_date[lang],'error',2);
        	return false;
        }
        // alert($("#destination").val().length);
        if($("#destination").val().length<3){
        	alert('burda');
        	alert(word_specify_location[lang]);
        	showDialog('Warning',word_specify_location[lang],'warning',2);
        	return false;
        }else if(destination_id.length<10 &&$("#destination").val().length>15){
        	showDialog('Warning',word_specify_location[lang],'warning',2);
        	return false;
        }
	} else {
	  if(destination_id.length<10){
		  showDialog('Warning',word_specify_location[lang],'warning',2);
		  return false;
	  }
           var  day = $('#day').val();
            var  month = $('#month').val();
            var  year = $('#year').val();

            if(day=='day' || month=='month' || year=='year' ){
                showDialog('Error',word_invalid_date[lang],'error',2);
                return false;
            }
            var check_in_day = new Date (year,month-1,day);
            var today = new Date();
            if(check_in_day < today){
             showDialog('Error',word_past_date[lang],'error',2);
             return false;
            }
	}
       
        $('#search').submit();
      // $("#loader_dest").innerHTML= $("#destination").val();
	show_loader();
	window.scrollTo(0, 0);
}

function no_of_rooms() {

	var room_no = document.getElementById('room_no').value;
	var response = "";
	response += '<table id="room_table">';
	for ( var j = 1; j <= room_no; j++) {
		response += '<tr><td width="56" height="37" align="left" valign="middle" class="searchbox">Room' + j + ':</td>';
		response += '<td width="65" height="37" align="center" class="searchbox">'
				+ '<select name="adult'
				+ j
				+ '" id="adult'
				+ j
				+ '" class="slogan" >';
		for ( var i = 1; i <= max_no_of_adult; i++) {
			response += "<option value='" + i + "'>";
			response += i;
			response += "</option>";
		}
		response += ' </select></td> '
				+ ' <td width="61" height="37" align="center" class="searchbox"> '
				+ ' <select name=\"child' + j + '\" id=\"child' + j
				+ '\"  class="slogan" onchange="display_children_age(\'' + j
				+ '\')">';
		for ( var i = 0; i <= max_no_of_children; i++) {
			response += "<option value='" + i + "'>";
			response += i;
			response += "</option>";
		}
		response += ' </select></td> ';
		response += ' <td colspan="2" id="children_age_' + j + '" width="71" height="37" align="right" class="searchbox"></td> ';
		response += '</tr>';
	}
	response += '</table>';
	document.getElementById('room_row').innerHTML = response;

}

function display_children_age(no) {

	var children_no = document.getElementById('child' + no).value;
	var response = "";

	for ( var j = 1; j <= children_no; j++) {
		response += "<select id='child_age_" + no + "_" + j
				+ "' name='child_age_" + no + "_" + j + "' class='link_myaccount_act'>";
		for ( var i = 1; i <= max_no_of_age_of_children; i++) {
			response += "<option value='" + i + "' >";
			response += i;
			response += "</option>";
		}
		response += "</select>";
	}
	document.getElementById('word_age').style.display = '';
	document.getElementById('children_age_' + no).innerHTML = response;

}

//When you click on a link with class of poplight and the href starts with a #
function show_loader() {
    var popID = "popup3"; //Get Popup Name
    var popURL = "#?w=250"; //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"></a>');

    //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    //$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer

    return false;
}

function get_city_by_country_with_comma(country) {

	var continent = document.getElementById('hidden_continent').value;
	var coun = "<span class=\"dest_on_mouseover_hand\"  onclick=\"get_city_by_country_with_comma('"
			+ country + "')\" >" + country + "</span>" + " &#187; ";
	document.getElementById('world_map_part_continent').innerHTML = continent
			+ " " + coun;

	$.ajax({url:
			'AjaxComponents/CalledByJS.php',
				type : 'POST',
				data : 'fn=getcitiescomma&country=' + country,
				success : function(req) {
					document.getElementById('countries_by_continents').innerHTML = req.substr(0,req.length-2);
				}
			});
}

function get_city_by_country_with_comma_no_limit(country) {

	var continent = document.getElementById('hidden_continent').value;
	var coun = "<span class=\"dest_on_mouseover_hand\"  onclick=\"get_city_by_country_with_comma('"
			+ country + "')\" >" + country + "</span>" + " &#187; ";
	document.getElementById('world_map_part_continent').innerHTML = continent
			+ " " + coun;

	$.ajax({url:
			'AjaxComponents/CalledByJS.php',
				type : 'POST',
				data : 'fn=getcitiescommanolimit&country=' + country,
				success : function(req) {
					document.getElementById('countries_by_continents').innerHTML = req.substr(0,req.length-2);
				}
			});
}

function send_to_search_result_page(city, country) {
	
	$("#destination_id").val("2" + city + "----" + "" + "----" + country);
	$("#isMapOpen").val("1");
	send_to_search_result_page_from_index();
}

function send_to_search_result_from_top_dest(country,city,location) {
	 document.getElementById('search').destination_id.value="2" +  city + "----" + "" + "----" + country;
    
       $("#isMapOpen").val("0");
       
	send_to_search_result_page_from_index();
}
