var WeekDays = new Array('S','M','T','W','T','F','S');
var MonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var MonthNamesShort = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var CalCloseText = 'Close';
var YearList = new Array(2011, 2012);
var CalBGColor = '##fafafa';
var TopRowBGColor = '##D5E1EE';
var DayBGColor = '##D5E1EE';
var CalFontFamily = 'Verdana,Arial,Helvetica';
var CalFontSize = '11px';
var CalDisplayMask = 'mmm dd yyyy';
var AutoDateSync = true;

/* OVERLAY WITH MESSAGE */
function loadBlock(block){
	$(block).block({message: $('.load')});
};

/* BTN ACTION */
function btnAction(btn){
$(btn).mousedown(function(){
$(this).find('img').attr('src', 'img/ui/hqs-btn-down.png');
}).mouseup(function(){
$(this).find('img').attr('src', 'img/ui/hqs-btn.png');
});
};

/* AUTOCOMPLETE */
var locError = false;
function locBox(city){
	var cache = {};
	var fst;
	
		$(city).autocomplete({
		
		source: function( request, response) {
			
			var st = request.q.slice(0, 3);
			st = st.toLowerCase();
			var fst = request.q.toLowerCase();
			
			if (st in cache) {
				
            response($.map(cache[st], function(item) {
            		    if( item.label.toLowerCase().indexOf( fst ) != -1 ){
                return {
                    label: item.label +', ' + item.country,
                    value: item.label +', ' + item.country,
					countryH: item.countryIso,
					cityH: item.label
                };
                	    }
            }))
            		}else{
			
			$.ajax({
				
				url: "/json/" + st + "_.txt",  // file named with first three letters of search term
				dataType: "json",
				success: function( data, status, xhr ) {	//now filter on search term
					$('.errorBlock').hide();
					$('#locError').hide();
					cache[st] = data;
						
					 response($.map(cache[st], function(item) {
								
						if( item.label.toLowerCase().indexOf( fst ) != -1 ){	
							
						return {
							
							label: item.label +', ' + item.country,
							value: item.label +', ' + item.country,
							countryH: item.countryIso,
							cityH: item.label
						}
						}
						
					}));	
				},
				error: function(){ 
					
					$('#locError').show();
					
					var locError = true;
					
				}
								
			});
			}
		},
		minLength: 3,
		select: function( event, ui ) {

			$('.cityH').val(ui.item.cityH);
			$('.countryH').val(ui.item.countryH);
	},
	open: function() {
		$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	},
	close: function() {
		$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	}
		
	});
};


/* CATCH INVALID USER INPUT */
function formCity(thisForm){
	$(thisForm).bind("submit", function(e) { 
		
		
		
		var city = $('#city').val(); //user input fld
		var cityH = $('.cityH').val(); //hidden fld, filled when loc selected from autocomplete
		var cityIni = "Enter your destination";
		var cityCheck = (city == "") || (city == cityIni)? true : false;
		
		if(cityCheck == true){ //empty input or ini. msg 
			$('#cityError').css('display','block');
			 $('#cityError').find('strong').html('Please enter your destination.');
			e.preventDefault();
		};
		
		if((cityH == "") && (cityCheck == false)){ //user input put no selection from autocomp.: either no autocomp or too fast
			var dc = city.match(/^(\w+)/);
			dc = dc[0];
			selLoc(dc);
			e.preventDefault();
		};
		
	//add cond. for date
		var calDate = $('#pudate').val();
		  var now = new Date();
		  var jetzt = new Date(calDate);
		   now.setDate(now.getDate()+5);
		   if(jetzt < now){
			
			   $('#cityError').css('display','block');
			   $('#cityError').find('strong').html('To book hotels within 5 days please call our agents at 1-877-444-2222.');
			   e.preventDefault();
		   }
		 
		//check if total occ. does not exceed 9
		  
		   var totOcc = 0;
		    $('.chOcc').each(function() {
		    	if($(this).is(':visible')){
		        totOcc += Number($(this).val());
		    	}
		    });
		    if(totOcc >9){
		    	 $('#cityError').css('display','block');
		    	 $('#cityError').find('strong').html('To book rooms for more than nine people please call our agents at 1-877-444-2222.');
		    	 e.preventDefault();
		    }
		   
		    
	});
	
};


/* DISAMBIGUATION */
function selLoc(rt){	
	var blockSubmit = $('#boxoftubes');
	var dialogWindow = '<div id="dialog-form" ><span class="dialog-formPrompt">Select your destination:</span><input name="city" type="text" id="cityR" class="hqs-city" value="" /><div class="errorBlock">cannot be found</div></div>'
	$('.hqs-pos').append(dialogWindow);
	
	$( "#dialog-form" ).dialog({
	autoOpen: false,
	height: 320,
	width: 400,
	position:[400,230],
	modal: true,
	buttons: {
		"Cancel": function() {
			$( "#city" ).val('');
			$( this ).dialog( "close" );
		},
		"Select": function() {
			var newCity = $('#cityR').val();
			$( "#city" ).val( newCity ); 
			
			if($('#cityError').is(':visible')){
				$( this ).dialog( "close" );
			
			}else{
				$( this ).dialog( "close" );
				$('button[name="submitNewSearch"]').trigger('click');
				
			}
			
		
	}
	}
});
	
	$( "#dialog-form" ).bind( "dialogopen", function(event, ui) {
		 $('#cityR').val(rt);
		 locBox('#cityR');
		 $('#cityR').click(function(){
			 $('#cityR').val('');
		 });
	});
	
	$( "#dialog-form" ).dialog( "open" );
	var newSearch = $('#cityR').val();
	$('#cityR').autocomplete('search', newSearch);
	if(locError == false){
		$('.errorBlock').show();
	}

};

/* CLOSE ALL ERROR MSG */
function closeError(){
	$('[id$=Error]').css('display','none');
}


function heightAdjust(sel, dis, ih, ic){
			   var rn = $(sel).val(); 
			   var dr = parseInt(rn) + 1;
			   var disHeight = ih + ic*rn;
			   
			   $(dis).height(disHeight);
			   $('.roomRowPlus').hide();
			for( i=1; i < dr; i++){
				$('#room'+[i]+'info').show(); 
			   }
};


function CheckOcc(room){
	var numAD = room.substr(0,5);
	var adNum = $('#'+numAD + 'adt').val();
	var chNum = $('#'+numAD + 'CHD').val();
	var totalOcc = parseInt(adNum) + parseInt(chNum);
	
	if(totalOcc>4){
		$('#occError').show();
		$('#'+numAD + 'adt').val('1');
		$('#'+numAD + 'CHD').val('0');
		$('#'+numAD + 'CHD_1_age').hide();
	}
}

function ChildAges(thisCHDRoom) {
	var numCHD =  $('#'+thisCHDRoom).val();
	if(numCHD == 0){
		$('#'+thisCHDRoom + "_1_age").hide();
	}else{
		$('#'+thisCHDRoom + "_1_age").show();
	}
		CheckOcc(thisCHDRoom);
	}
	

