

/*---START: General - load different Divs---*/
	jQuery(document).ready(function(){
		jQuery("#DepartureAirport").val('');
		jQuery("#addajetWrapper").show();
		jQuery("#WithFlight").val('1');
		jQuery("#hasflight").show();
		
		jQuery("#button1").click(function () { 
			jQuery("#hasflight").hide();
			jQuery("#WithFlight").val('0');
		});
		jQuery("#button2").click(function () {
			jQuery("#hasflight").show();
			jQuery("#WithFlight").val('1');
		});		
	});
/*---END: General - load different Divs---*/

	
/*---START: functions---*/
function DateAdd(objDate, strInterval, intIncrement)
    {
        if(typeof(objDate) == "string")
        {
            objDate = new Date(objDate);
 
            if (isNaN(objDate))
            {
                throw("DateAdd: Date is not a valid date");
            }
        }
        else if(typeof(objDate) != "object" || objDate.constructor.toString().indexOf("Date()") == -1)
        {
            throw("DateAdd: First parameter must be a date object");
        }
 
        if(
        strInterval != "M"
        && strInterval != "D"
        && strInterval != "Y"
        && strInterval != "h"
        && strInterval != "m"
        && strInterval != "uM"
        && strInterval != "uD"
        && strInterval != "uY"
        && strInterval != "uh"
        && strInterval != "um"
        && strInterval != "us"
        )
        {
            throw("DateAdd: Second parameter must be M, D, Y, h, m, uM, uD, uY, uh, um or us");
        }
 
        if(typeof(intIncrement) != "number")
        {
            throw("DateAdd: Third parameter must be a number");
        }
 
        switch(strInterval)
        {
            case "M":
            objDate.setMonth(parseInt(objDate.getMonth()) + parseInt(intIncrement));
            break;
 
            case "D":
            objDate.setDate(parseInt(objDate.getDate()) + parseInt(intIncrement));
            break;
 
            case "Y":
            objDate.setYear(parseInt(objDate.getYear()) + parseInt(intIncrement));
            break;
 
            case "h":
            objDate.setHours(parseInt(objDate.getHours()) + parseInt(intIncrement));
            break;
 
            case "m":
            objDate.setMinutes(parseInt(objDate.getMinutes()) + parseInt(intIncrement));
            break;
 
            case "s":
            objDate.setSeconds(parseInt(objDate.getSeconds()) + parseInt(intIncrement));
            break;
 
            case "uM":
            objDate.setUTCMonth(parseInt(objDate.getUTCMonth()) + parseInt(intIncrement));
            break;
 
            case "uD":
            objDate.setUTCDate(parseInt(objDate.getUTCDate()) + parseInt(intIncrement));
            break;
 
            case "uY":
            objDate.setUTCFullYear(parseInt(objDate.getUTCFullYear()) + parseInt(intIncrement));
            break;
 
            case "uh":
            objDate.setUTCHours(parseInt(objDate.getUTCHours()) + parseInt(intIncrement));
            break;
 
            case "um":
            objDate.setUTCMinutes(parseInt(objDate.getUTCMinutes()) + parseInt(intIncrement));
            break;
 
            case "us":
            objDate.setUTCSeconds(parseInt(objDate.getUTCSeconds()) + parseInt(intIncrement));
            break;
        }
        return objDate;
    }

function updateMinDate(){
	jQuery("#DateTo").datepicker('destroy');
	jQuery("#DateTo").val("");
	jQuery("#DateTo").datepicker({
		changeMonth: false,
		dateFormat: 'd-mm-yy',
		changeYear: false,
		minDate: DateAdd(jQuery("#DateFrom").datepicker('getDate'), "D", +1)
	});
}

function resetForm(id) {
	jQuery('#'+id).each(function(){
	        this.reset();
	});
}

jQuery(document).ready(function()
	{
		
	resetForm("myform");

	jQuery("#DateFrom").datepicker({
		changeMonth: false,
		dateFormat: 'dd-mm-yy',
		changeYear: false,
		minDate: +1,
		onSelect: updateMinDate
	});
	
	jQuery("#DateTo").datepicker({
		changeMonth: false,
		dateFormat: 'dd-mm-yy',
		changeYear: false,
		minDate: +2
	});

});

function dorooms(dropdown){
		var dd = document.getElementById(dropdown);
		var trooms = parseInt(dd.value);
		
		for (var r = 1; r <= 9; r++){
			var divroom = document.getElementById('room_' + r);
			divroom.style.display = 'none';

			
			if (r > trooms) {
				for (var a = 1; a <= 3; a++) {
					document.getElementById('Room' + r + 'ChildAge' + a).value = '-1';
				}
				
				document.getElementById('AdultsRoom' + r).value = '1';
				document.getElementById('ChildrenRoom' + r).value = '0';
				document.getElementById('room_' + r + '_children').style.display = 'none';
			}
		}
		
		for (var r = 1; r <= trooms; r++){
			var divroom = document.getElementById('room_' + r);
			//divroom.style.display = 'block';
			jQuery(divroom).fadeIn(r*300);
		}
	}

function doChildren(noofchildren, roomno)
	{
		var noc = parseInt(noofchildren);
		for (var x = 1; x <= 3; x++) {
			document.getElementById('Room' + roomno + 'ChildAge' + x).style.display = 'none';
			if (x > noofchildren)
			{
				document.getElementById('Room' + roomno + 'ChildAge' + x).value = '-1';
			}
		}
	
		
		document.getElementById('room_' + roomno + '_children').style.display = 'none';
		
		if (noc > 0) {
			
			document.getElementById('room_' + roomno + '_children').style.display = 'block';
			for (var x = 1; x <= noc; x++) {
				//document.getElementById('Room' + roomno + 'ChildAge' + x).style.display = 'inline';
				jQuery(document.getElementById('Room' + roomno + 'ChildAge' + x)).fadeIn();
			}
		}
	}

var fromDestination = false;

function checkHotelSearch(){
		if (fromDestination == false)
		{
			var hotelsearcherror = document.getElementById('hotelsearcherror');
			jQuery(hotelsearcherror).hide();

			var DepartureAirport = jQuery('#DepartureAirport').val();
			
			var Destination = "Mtmla00";
			var DateFrom = jQuery('#DateFrom').val();
			var DateTo = jQuery('#DateTo').val();
			var iswithflight = jQuery('#WithFlight').val();
			var founderror = false;
			myerror = "";
	
			if (iswithflight == '1'){
			
				if (DepartureAirport == '') {
					hotelsearcherror.innerHTML = 'Please select departure airport';
					jQuery(hotelsearcherror).slideDown();
					return false;
				}
				
			}
			
			if (Destination == '') {
					hotelsearcherror.innerHTML = 'Please select destination';
					jQuery(hotelsearcherror).slideDown();
					return false;
				}
			
			var dateerror = false;
			if ((DateFrom == '') || (DateTo == '')){
				hotelsearcherror.innerHTML = 'Please select travelling dates';
				jQuery(hotelsearcherror).slideDown();
				return false;
			}				
	
			var childerrorfound = false;
			
			for (var r = 1; r <= 9; r++){	
				var children = parseInt(jQuery("#ChildrenRoom"+r).val());
				if (children > 0)
				{
					for (var c = 1; c <= children; c++){
						if (jQuery("#Room"+r+"ChildAge"+c).val() == "-1") {
							if (!childerrorfound) {
								childerrorfound = true;
								hotelsearcherror.innerHTML = 'Kindly provide the age of each child at the time of travel.';
								jQuery(hotelsearcherror).slideDown();
								return false;
							}
						}
					}
				}
			}
		} 
		else 
		{
			fromDestination = false;
			return false;
		}
		return true;
	}
	
/*---END: functions---*/

/*---START: Departure---*/
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function Left(str, n){
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0,n);
}

/*---END: Departure---*/

/*---START: Airports---*/
	function change(passedval)
	{
		if (passedval == jQuery("#filter").val()){
			$.getJSON("http://airports.addajet.com/default.asp?searchQuery="+$("#filter").val()+"&rnd="+Math.random()+"&jsoncallback=?",function (data){																							
				jQuery("#airportslist").html('');
				$.each(data.airports, function(i,airport){
					$("#airportslist").append("<li>" + airport.AirportName + " ("+ airport.CountryCode +") - "+airport.AirportCode+"</li>");
					jQuery("#loadimg").hide();
					jQuery("#noairportsfound").hide();
				});
				$("#divscript").load("js/ajaxselect.html?rnd="+Math.random());	
				if(data.airports.length == 0){
					jQuery("#loadimg").hide();
					jQuery("#noairportsfound").show();
				}
			});	
		}
	}
	
	function check(pressedVal){
		if (jQuery("#filter").val().length >= 1){
			jQuery("#loadimg").show();
			jQuery("#airportslist").html('');
			jQuery(".innerdropbox").show();
			jQuery("#noairportsfound").hide();
			setTimeout(function(){change(pressedVal)},1000);
		}else{
			jQuery("#airportslist").html('');
			jQuery(".innerdropbox").hide();
			jQuery("#noairportsfound").hide();
		}
	}
/*---END: Airports---*/	

function showSearchBox(elementId){
	
	var newOptions;
	var select = jQuery("#NoOfRooms");
	
	var selectedOption = select.val();
	
		var options = select.attr('options');
		$('option', select).remove();
		
	if (elementId == 1){
		jQuery("#hasflight").show();
		jQuery("#WithFlight").val('1');
		jQuery("#DateFromLabel").text('Arrival Date');
		jQuery("#DateToLabel").text('Return Date');
		
		select.append('<option value="1" selected="selected">1</option>');
		select.append('<option value="2" selected="selected">2</option>');
		select.append('<option value="3" selected="selected">3</option>');
		
		if(selectedOption > 3)
		{
			select.val('3');
		}
		else
		{
			select.val(selectedOption);
		}
		dorooms('NoOfRooms');
		
	}else{
		jQuery("#hasflight").hide();
		jQuery("#WithFlight").val('0');
		jQuery("#DateFromLabel").text('Check-In');
		jQuery("#DateToLabel").text('Check-Out');
		
		select.append('<option value="1" selected="selected">1</option>');
		select.append('<option value="2" selected="selected">2</option>');
		select.append('<option value="3" selected="selected">3</option>');
		select.append('<option value="4" selected="selected">4</option>');
		select.append('<option value="5" selected="selected">5</option>');
		select.append('<option value="6" selected="selected">6</option>');
		select.append('<option value="7" selected="selected">7</option>');
		select.append('<option value="8" selected="selected">8</option>');
		select.append('<option value="9" selected="selected">9</option>');
		
		select.val(selectedOption);
		dorooms('NoOfRooms');
	}
}
