/*COMMON*/
function dateToArray(string) {
  var s = string.split('-');
  return new Array(parseInt(s[0],10),(parseInt(s[1],10)),parseInt(s[2]));
}
function leadZero(n) {
	return (parseInt(n,10)<10) ? "0"+n : n;
}
function futureDate(value) {
	var d = new Date();
	d.setMilliseconds(d.getMilliseconds() + parseInt(value*86400000,10));
	return leadZero(d.getDate())+'-'+leadZero(d.getMonth()+1)+'-'+d.getFullYear();
}
function calculateNights(ret_nights) {
	var cin = dateToArray($('#cindate').val());
	var cinDate = new Date(cin[2],(cin[1]-1),cin[0]);

	var cot = dateToArray($('#cotdate').val());
	var cotDate = new Date(cot[2],(cot[1]-1),cot[0]);

	var dif = new Date();
	dif.setTime(cotDate.getTime()-cinDate.getTime());
	var nights = dif.getTime()/(86400*1000);
	if(ret_nights) {
		return nights;
	} else {
		return (nights>=1&&nights<=90) ? true : false ;
	}
}
function processFlightsForm() {
	var re_code = new RegExp("\[[A-Z]{3}\]$");
	var re_date = new RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$","i");
	var outdate = $('#outdate').val();
	var retdate = $('#retdate').val();
	var fc = $('#fromcity').val();
	var tc = $('#tocity').val();

	if(fc==''||tc=='') {
		alert(txt_select_cities);
		return false;
	} else if(re_date.test(outdate)==false||outdate=='') {
		alert(txt_select_outdate);
		return false;
	} else if(re_date.test(retdate)==false||retdate=='') {
		alert(txt_select_retdate);
		return false;
	} else {
		if(re_code.test(fc)==true) {
			$('#fromcity').val(fc.substr(parseInt(fc.length-4),3));
		}
		if(re_code.test(tc)==true) {
			$('#tocity').val(tc.substr(parseInt(tc.length-4),3));
		}
		$('#qsfFlightsForm').submit();
	}
}
function processHotelsForm() {
	var re = new RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$","i");
	var cindate = $('#cindate').val();
	var cotdate = $('#cotdate').val();

	if($('#city').val()=='') {
		alert(txt_select_city);
		return false;
	} else if(re.test(cindate)==false||cindate=='') {
		alert(txt_select_cindate);
		return false;
	} else if(re.test(cotdate)==false||cotdate=='') {
		alert(txt_select_cotdate);
		return false;
	} else if(!calculateNights()) {
		alert(txt_calculate_nights);
	} else {
		var cin = dateToArray(cindate);
		$('input[name="cinday"]').val(cin[0]);
		$('input[name="cinmonth"]').val(cin[1]);
		$('input[name="cinyear"]').val(cin[2]);
		var cot = dateToArray(cotdate);
		$('input[name="cotday"]').val(cot[0]);
		$('input[name="cotmonth"]').val(cot[1]);
		$('input[name="cotyear"]').val(cot[2]);

		$('#qsfHotelsForm').submit();
	}
}

function childrenControl(obj) {
	var age = '';
	for(i=2;i<=12;i++){age+='<option value="'+i+'">'+i+'</option>';}
	var n = obj.val();
	$('#h-select-age').remove();

	if(n>0 && $('#id_children').is(':checked')){
		$('#h-select-children').after($('<div></div>').attr('id','h-select-age').append($('<label></label>').text(txt_children)));
		for(i=0;i<n;i++){
			$('#h-select-age').append($('<select></select>').attr('name','children_age[0][]').html(age));
		}
	}
}
function formatItem(row) {
	return "<strong>" + row[1] + "</strong><br /><span class=\"ac_country\">" + row[2] + "</span><span class=\"ac_code\" style=\"display:none;\">" + row[3] + "</span>";
}
function setCity(field,input_value,code) {
	$('#'+field).val(input_value);
	if(code) { // <- Required by hotels QSF
		$('input[name="autoSelect"]').val('true');
		$('input[name="cityCode"]').val(code);
	}
	esky_cities_remove();
}

$(document).ready(function(){
	/* Setup initial dates */
	$('#outdate,#cindate').val(futureDate(30));
	$('#retdate,#cotdate').val(futureDate(37));

	/* Control city autocomplete in flight's QSF */
	$('#fromcity,#tocity').autocomplete('ws/ajax.php',{
		delay:300,
		minChars:3,
		matchSubset:1,
		matchContains:0,
		cacheLength:10,
		autoFill:false,
		maxItemsToShow:10,
		width:150,
		extraParams:{check:'flight'},
		formatItem:formatItem
	});

	/* Control city autocomplete in hotel's QSF */
	$('#city').autocomplete('ws/ajax.php',{
		delay:300,
		minChars:3,
		matchSubset:1,
		matchContains:0,
		cacheLength:10,
		autoFill:false,
		maxItemsToShow:10,
		width:150,
		extraParams:{check:'city'},
		formatItem:formatItem,
		onItemSelect:function(item){
			$('input[name="autoSelect"]').val('true');
			$('input[name="cityCode"]').val($('.ac_code', item).text());
		}
	});
	$('#city').change(function(){
      	$('input[name="autoSelect"]').val('false');
        $('input[name="cityCode"]').val('');
	});

	/* Reset children selector and room type every time page is loaded */
	$('#id_hotnumpersons option').removeAttr('selected').children(':first').attr('selected','selected');
	$('#id_children').removeAttr('checked').attr('disabled','true');
	$('#h-select-children').hide();

	/* Control room type (disable/enable children and hide children numer and age selects) */
	$('#id_hotnumpersons').change(function(){
		var v = $(this).val();
		if(v=='DB'||v=='TB') {
			$('#id_children').removeAttr('disabled');
		} else {
			$('#id_children').removeAttr('checked').attr('disabled','true')
			$('#h-select-children').hide();
			childrenControl($('#id_children_num'));
		}
	});

	/* Control children checkbox (show/hide children numer and age selects) */
	$('#id_children').click(function(){
		if($(this).is(':checked')) {
			$('#h-select-children').show();
		} else {
			$('#h-select-children').hide();
		}
		childrenControl($('#id_children_num'));
	});

	/* Control children number selector */
	$('#id_children_num').change(function(){
		childrenControl($(this));
	});

	/* Control calendars */
	$('a.pop-calendar').click(function(){
		$(this).esky_calendar({field:$(this).attr('rel'),loop:2,fade:1000});
		return false;
	});

	/* Control cities popup window */
	$('a.pop-cities').esky_cities();
});
