
	var translation = [];

	translation["milano"] 	= "Milan";
	translation["torino"] 	= "Turin";	
	translation["venezia"] 	= "Venice";	
	translation["napoli"] 	= "Naples";	
	translation["roma"] 	= "Rome";	
	translation["firenze"] 	= "Florence";

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

	function xSearch( city )
	{
		city = trim( city.toLowerCase() );

		for ( c in translation ) 
		{ 		
			if ( c == city )
			{
				return translation[c];
			}
		}

		return '';
	}


//extract (first!) value from querystring for the passed name
function GetQSVal(qsName) {
	try
	{
		var qsPair
		var i
		var qString = location.search.substr(1)
		
		if (qString == null || qString.length == 0)
			qString = query;
		    
		var arrNameVal = qString.split('&')
		//for (i in arrNameVal) {	
		for(i=0; i<arrNameVal.length; i++){
			qsPair = arrNameVal[i].split('=')
			if (URLDecode(qsPair[0]).toLowerCase() == qsName.toLowerCase()) return URLDecode(qsPair[1])
		}
	}
	catch(e)
	{
	}
	return ""
}

function getDate(dateString) {
    var year = dateString.substr(0,4)
    var month = dateString.substr(5,2) - 1 // 0 - 11
    var day = dateString.substr(8,2)
    return new Date(year,month,day)
}

// validate dates
function ValidateDates() {

    var inDate = getDate(document.getElementById("checkinValue").value)
    var outDate = getDate(document.getElementById("checkoutValue").value)
    var currentDate = new Date();
   
    
	//validate checkin - checkout difference (date range too big)
	if ((outDate - inDate)/86400000 >= 31) {  //86400000 is one days in milliseconds
		alert(typeof(JavaScriptPeriodOfStay) == 'undefined' ? 'Your period of stay should be no longer than 30 nights.' : JavaScriptPeriodOfStay)
		return false
	}

    // validate checkout <= checkin
	if (outDate - inDate <= 0) {
		alert(typeof(JavaScriptEnsureCheckoutAfterCheckin) == 'undefined' ? 'Please ensure that the check-out date is after the check-in date.' : JavaScriptEnsureCheckoutAfterCheckin)
		return false
	}

    //validate checkin/checkout is less than one year in advance
    if((outDate - currentDate)/86400000 >= 363) {
		alert(typeof(JavaScriptBookWithinOneYear) == 'undefined' ? 'You cannot book more than 1 year in advance.' : JavaScriptBookWithinOneYear)
        return false;
    }
    
	return true
}


function SearchKeyPress(eventObject, languageCode, target, affiliateId, city, domain, brandId) {
    if (eventObject.keyCode == 13) {
       eventObject.returnValue=false;
       eventObject.cancel = true;
       return DoSearch(languageCode, target, affiliateId, city, domain, brandId);
    }
}

function CitySelect(cityFileName) {
    document.getElementById(cityFileName).checked = "checked";
    document.getElementById("citySearch").value = ""
    document.getElementById("selectedFileName").value = cityFileName
}

function DoSearch(languageCode, target, affiliateId, city, domain, brandId) {

    var redirection;
    var affiliateParam;
    var brandParam;
    var host;
    var tmp;

    if (document.getElementById('hotelCheckin') != null)
    {
        if(document.getElementById('hotelCheckin').value == 0 || document.getElementById('hotelCheckout').value == 0 )
        {
            alert(typeof(JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
            return false
        } 
    }
    
    if (!ValidateDates())
        return false

    if ( languageCode == 'IT' )        
    {
    	tmp = xSearch( city );
    	if ( tmp != '' )
		city = tmp;
    }

    city = escape( city );

    if (typeof(brandId) == 'undefined' || brandId == "0")
    {
		brandParam = '';
    }
    else
    {
		brandParam = '&brandId=' + brandId
    }
      
    if (affiliateId == null || affiliateId == '')
    {
        affiliateParam = '';
        host = '';
    }
    else
    {
        affiliateParam = '&a_aid=' + affiliateId;   
		// backwards compatability for existing affiliate scripts
		if (typeof(domain) == 'undefined' || domain == '')
		{
			host = 'http://www.hotelscombined.com'	
		}
		else
		{
			host = 'http://' + domain;
		}
    }
    
    var defaultSort = GetQSVal("sort");
    if (defaultSort.length > 0)
    {
		defaultSort = "&sort=" + defaultSort;
    }
    //else
    //{
    //		defaultSort = "&sort=MinRate-asc";
    //}

    //if come from home page or come from city/searchResult pages with a new city name entered in the 'City' textbox
    if ((document.getElementById("citySearchRadio") != null && document.getElementById("citySearchRadio").checked) || document.getElementById("M_C_SearchResultCity") != null) {
     
        if (city == null || city.length < 3) {
            alert(typeof(JavaScriptEnterCityName) == 'undefined' ? 'Please enter a city name that is at least 3 characters in length' : JavaScriptEnterCityName);
            return false;
        }
        redirection = "/Search.aspx?search=" + city + "&checkin=" + document.getElementById("checkinValue").value + "&checkout=" + document.getElementById("checkoutValue").value + "&languageCode=" + languageCode + affiliateParam + brandParam + defaultSort;
        
        //if come from city or search result pages, add query info
        if (document.getElementById("M_C_SearchResultCity") != null)
        {        
            var currency = document.getElementById("M_C_currencies");
            redirection += currency == null? "":"&currencyCode=" + currency.options[currency.selectedIndex].id;               
	
            redirection += "&lowRate=" + escape(document.getElementById("M_C_LowRate").value);
            redirection += "&highRate=" + escape(document.getElementById("M_C_HighRate").value);            
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star5"), "star5");
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star4"), "star4");
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star3"), "star3");
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star2"), "star2");
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star1"), "star1");
            redirection += GetStarValue(redirection, document.getElementById("M_C_Star0"), "star0"); 
            redirection += "&cityName=" + city; 
            
            if (document.getElementById("M_C_HotelName") != null)
            {
                if(document.getElementById("M_C_HotelName").value != null && document.getElementById("M_C_HotelName").value != 0)
                {
                    redirection += "&hotelName=" + escape(document.getElementById("M_C_HotelName").value);                
                }            
            }      
            
            if (document.getElementById("M_C_ShowSoldOut") != null) {
                redirection += document.getElementById("M_C_ShowSoldOut").checked? "":"&showSoldOut=false"; 
            }        
	         
	        if(document.getElementById("M_C_ShowOnRequest") != null){
	            redirection += document.getElementById("M_C_ShowOnRequest").checked? "":"&showOnRequest=false"; 
	        }	        
        }        
    } else {
        redirection = "/SearchResults.aspx?fileName=" + document.getElementById("selectedFileName").value + "&checkin=" + document.getElementById("checkinValue").value + "&checkout=" + document.getElementById("checkoutValue").value + "&languageCode=" + languageCode + affiliateParam + brandParam + defaultSort;
    }
        
    redirection += document.getElementById("guestValue") == null? "":"&Adults=" + document.getElementById("guestValue").value;  
    redirection += document.getElementById("roomValue") == null? "":"&Rooms=" + document.getElementById("roomValue").value;

    if (document.getElementById("M_C_HotelFacility1") != null) {
        var facility = "";
        for (var i=1; i<=11; i++){
            if(document.getElementById("M_C_HotelFacility"+ i).checked){
                facility += i + ',';
            }
        }	
        if(facility != ""){
            var strLen = facility.length;
            facility = facility.slice(0,strLen-1);
            redirection += "&facilities="+ facility;
        }
    }
    
    
    redirection = host + redirection;
    
    switch (target)
    {
        case "_blank":
            window.open(redirection).focus();
            break;
        case "_parent":
            window.parent.location = redirection;
            break;
        case "_top":
            window.top.location = redirection;
            break;
        default:
            window.location = redirection; // _self
    }
    
    return false;
}


function setGuestValue(select)
{
    document.getElementById('guestValue').value = select.options[select.selectedIndex].value;
    return false;
}

function setRoomValue(select, isAffiliate)
{
    if(select.value == "5" && isAffiliate == false)
    {
        select.selectedIndex = 0;
        var r = confirm(typeof(JavaScriptSearchMoreRoom) == 'undefined' ? 'We only support searching for up to four rooms at once, would you like to use our partner site Hotelplanner.com to search for more rooms?' : JavaScriptSearchMoreRoom);
        if (r== true)
        {
            var qString =  "?sc=HotelsCombined"; 
                city = "",
                checkin = "",
                checkout = "";
            
            //if on city or searchResults page
            if ( document.getElementById("M_C_SearchResultCity") != null && document.getElementById("M_C_SearchResultCity").value != 0) 
            {
                city = document.getElementById("M_C_SearchResultCity").value;
            }
            else
            {
                //if on hotel page
                if ( document.getElementById("cityNameValue") != null && document.getElementById("cityNameValue").value != 0) 
                {
                    city = document.getElementById("cityNameValue").value;
                }
            }
            
            //get user selected checkin and ckeckout value
            if (( document.getElementById("hotelCheckin") != null && document.getElementById("hotelCheckin").value != 0) ||
                ( document.getElementById("detailTabCheckin") != null && document.getElementById("detailTabCheckin").value != 0) ||
                ( document.getElementById("rateTabCheckin") != null && document.getElementById("rateTabCheckin").value != 0))
            {
                var checkinDom = document.getElementById("checkinValue");
                checkin = checkinDom == null? "" : checkinDom.value == 0? "" : reFormatDate (checkinDom.value);
            }              
            if (( document.getElementById("hotelCheckout") != null && document.getElementById("hotelCheckout").value != 0) ||
                ( document.getElementById("detailTabCheckout") != null && document.getElementById("detailTabCheckout").value != 0) ||
                ( document.getElementById("rateTabCheckout") != null && document.getElementById("rateTabCheckout").value != 0))
            {
                var checkoutDom = document.getElementById("checkoutValue");
                checkout = checkoutDom == null? "" : checkoutDom.value == 0? "" : reFormatDate (checkoutDom.value);
            }  
            
            qString += city == ""? "" : "&City=" + city;
            qString += checkin == ""? "" : "&Checkin=" + checkin;
            qString += checkout == ""? "" : "&Checkout=" + checkout;
            window.open("/ProviderRedirect.aspx?key=2.HPL.EN.USD.0.0.0&url=" + escape("http://www.hotelplanner.com" + qString), "_self");
            return false;
        }
    }
    else
    {
        document.getElementById('roomValue').value = select.options[select.selectedIndex].value;
       
    }
     return false;
}
