var debug = false;
function updateWaitText(){   
    var waitSpacer = $('waitspacer');
    if(waitSpacer) {
        $('waitspacer').style.height = "5px";
    }
    var additionalWait = $('additional_wait');
    if(!additionalWait)
        return false;

    if($('max_distance_address') && ! $('max_distance_address').disabled)
        $('additional_wait').innerHTML = $('50kmMessage').value.replace(/50/, $F('max_distance_address'));
    else if($('max_distance_cities') && ! $('max_distance_cities').disabled)
        $('additional_wait').innerHTML = $('50kmMessage').value.replace(/50/, $F('max_distance_cities'));
    else if($('max_distance_city') && ! $('max_distance_city').disabled)
        $('additional_wait').innerHTML = $('50kmMessage').value.replace(/50/, $F('max_distance_city'));
    if($('max_distance_city') && ! $('max_distance_city').disabled)
        $('additional_wait').innerHTML += label_selected_city;
    else if($('max_distance_address') &&! $('max_distance_address').disabled)
        $('additional_wait').innerHTML += label_selected_address;
    return false;
}
function hideWaiter(){
    Element.hide('result_container_wait');
}
function checkNumeric(e, withCommas){   
    var unicode = e.charCode? e.charCode : e.keyCode;
    if (unicode!= 8 && unicode != 39 && unicode != 37 && unicode != 9){
        if((unicode<48||unicode>57) && unicode != 46 ){
            if(unicode == 44 && withCommas){
	            return true;
            }
            return false;
        }
        if(unicode == 46){
            return false;
        }
    }
    return true;
}
function reportError(request){
  alert(popup_providerUnavailable);
}
function updateReturnDate(){
    var daysToAdd = 2,
        inDay = new String($F('outboundDay')),
        inMonthYear = $F('outboundMonthYear'),
        inYear = new String(inMonthYear.substring(2,inMonthYear.length)),
        inMonth = parseInt(inMonthYear.substring(0,2), 10) - 1,
        endDate = new Date(inYear,inMonth,inDay)
	;
    endDate.setTime(endDate.getTime() +daysToAdd*1000*3600*24 );
    
    var returnDay = endDate.getDate();
	returnDay = returnDay < 10 ? '0' + returnDay : returnDay;
    var returnMonth = parseInt(endDate.getMonth()) + 1;
    returnMonth = returnMonth < 10 ? '0' + returnMonth : returnMonth;
    var returnMonthYear = "" + returnMonth + endDate.getFullYear();
    $('returnDay').value = returnDay;
    $('returnMonthYear').value = returnMonthYear;
    return;
}
function checkDate() {
    var Today = new Date();
    Today.setHours(0,0,0,0);
    
    var inDay = new String($F('outboundDay')),
        inMonthYear = $F('outboundMonthYear'),
        inYear = new String(inMonthYear.substring(2,inMonthYear.length)),
        inMonth = parseInt(inMonthYear.substring(0,2), 10) - 1,
        inDate = new Date(inYear,inMonth,inDay),
        outDay = new String($F('returnDay')),
        outMonthYear = $F('returnMonthYear'),
        outYear = new String(outMonthYear.substring(2,outMonthYear.length)),
        outMonth = parseInt(outMonthYear.substring(0,2), 10) - 1,
        outDate = new Date(outYear,outMonth,outDay)
	;
    if(inDate < Today){
        var new_inDay =  Today.getDate();
        new_inDay = new_inDay<10 ? '0' + new_inDay : new_inDay;
        var new_inMonth = parseInt(Today.getMonth()) + 1;
        new_inMonth = new_inMonth < 10 ? '0' + new_inMonth : new_inMonth;
        var new_inMonthYear = "" + new_inMonth + Today.getFullYear();
        $('outboundDay').value = new_inDay;
        $('outboundMonthYear').value = new_inMonthYear;      
        checkDate();
        return;
    }
    if(outDate <= inDate){
        updateReturnDate();
        return;
    }  
    return;  
}
function refreshBanner(){/* todo: remove? */}
function hide_select(){
	var sels = document.getElementsByTagName("select"),
		s
	;
	for(var i = 0, l = sels.length; i < l; i++){
		s = sels[i];
		s.type == "select-one" && (s.style.visibility = "hidden"); 
	}
}
function show_select(){
	var sels = document.getElementsByTagName("select"),
		s
	;
	for(var i = 0, l = sels.length; i < l; i++){
		s = sels[i];
		s.type == "select-one" && (s.style.visibility = "visible"); 
	}
}
function CreateBookmarkLink(){
    var title = document.title,
        url = location.href
	;
    if(window.sidebar){ // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    }else if(window.external){ // IE Favorite
        window.external.AddFavorite( url, title);
    }else if(window.opera && window.print){ // Opera Hotlist
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click(); }
} 
function updateImages(list, idSearch){
    var pars = "ids=";
    for (var i = 0, l = list.length; i < l; i++) {
        i && (pars += ",");
        pars += list[i];
    }
    if(idSearch)
    pars += "&idSearch="+idSearch;
    new Ajax.Request('addinfo.do', {method: 'get', parameters: pars, onFailure: function(originalRequest){handleError(originalRequest);},
    evalScripts : true });   
}
function changeSearchType(type){
    if(type == 0){
        $('date_fields').style.display = "block";
        $('dynamic_button').innerHTML = label_butt_search_avail;                
        $('dynamic_text').innerHTML = label_insert_date;
        $('searchTypeSpan0').innerHTML = "<b>"+label_search_with_date+"</b>";
        $('searchTypeSpan1').innerHTML = label_search_without_date;
        $('searchtype_avail').checked = true;
        $('searchtype_all').checked = false;
        $('withAvailability').value = "Y";
        $('availability_span').style.display = "block";
    }else{
        closeCalendar();
        $('date_fields').style.display = "none";
        $('dynamic_button').innerHTML = label_butt_search_all;
        $('dynamic_text').innerHTML = "";
        $('searchTypeSpan1').innerHTML = "<b>"+label_search_without_date+"</b>";
        $('searchTypeSpan0').innerHTML = label_search_with_date;
        $('searchtype_all').checked = true;
        $('searchtype_avail').checked = false;
        $('withAvailability').value = "N";
        $('availability_span').style.display = "none";       
    }
}
function searchAllHotels(){    
    $('searchtype_avail').checked = false;
    $('withAvailability').value = "N";
    if(checkForm()) {
        $('search_form').submit();
        hide_select();
        Element.show('waitDiv');
    }else{
        alert(label_select_city);
    }
}
function openWindow(popupURL, width, height){
   window.open(popupURL,"popupWindowDoc","scrollbars=yes,location=no,toolbar=no,directories=no,status=no,resizable=no,height=" + height + ",width=" + width);
}
function LoadJS(file){
	var head = document.getElementsByTagName('head').item(0),
		scriptTag = $('loadScript'+file)
    ;
	if(!scriptTag){
		var script = document.createElement('script');
        script.src = file;
        script.type = 'text/javascript';
        script.id = 'loadScript'+file;
        head.appendChild(script);
  }
}
function tryDisableElementUntilExists(elementId, timeout){
	if(typeof timeout  == 'undefined'){
		timeout = 500
    }
	try{ Form.Element.disable($(elementId)); } catch (e) {setTimeout('tryDisableElementUntilExists(\''+elementId+'\','+timeout+')', timeout);};
}
function validateMail(form_id,email, alertMessage) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
        address = document.forms[form_id].elements[email].value
	;
	if(reg.test(address) == false) {
        alert(alertMessage);
        return false;
   }
   return true;
}

var onloads=[];
function bodyOnLoad(){
	for(var i=0;i<onloads.length;i++){
		onloads[i]();
	}
}

