function openPopUp( url, width, height ) {
    if( width == undefined )
        width = 500;

    if( height == undefined )
        height = 400;

    popupWindow = window.open( url, "popup", "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,status=no,menubar=no" );
    popupWindow.focus();
}

function openFixedPopUp( url, width, height ) {
    if( width == undefined )
        width = 500;

    if( height == undefined )
        height = 400;

    popupWindow = window.open( url, "_blank", "width=" + width + ",height=" + height + ",resizable=no,scrollbars=no,status=no,menubar=no" );
    popupWindow.focus();
}

function collapseElement(elementId) {
    var el = document.getElementById(elementId);

    if (el.style.display == 'none')
        el.style.display = '';
    else el.style.display = 'none';
}

function showElement(elementId) {
    var el = document.getElementById(elementId);
    el.style.display = '';
}

function hideElement(elementId) {
    var el = document.getElementById(elementId);
    if (el)
        el.style.display = 'none';
}

function manageAjaxNotification() {
     if (req.readyState == 4) {
        if (req.status == 200) {

            var result = trim(req.responseText);
            var message = eval(result);

            if (message.type == 'error') {
                //alert('error');
                document.getElementById('errorMessage').innerHTML=message.message;
                document.getElementById('errorMessage').style.display='inline';
                window.setTimeout("document.getElementById('errorMessage').style.display='none'", 2000);
            }

            else if (message.type == 'success') {
                document.getElementById('message').innerHTML=message.message;
                document.getElementById('message').style.display='inline';
                window.setTimeout("document.getElementById('message').style.display='none'", 2000);
            }
            //stopLoading();
        }
        else {
            //createErrorMessage?
        }
    }
}

function AjaxActionMessage(message,type) {
    this.message = message;
    this.type = type;
}

function changeRange(value) {
    if (value == "daily") {
        //se ha clickato su daily mostra daily, nasconde monthly
        showElement('dailyRange');
        if (document.getElementById('dailyRange1')) showElement('dailyRange1');
        hideElement('monthlyRange');
        if (document.getElementById('monthlyRange1')) hideElement('monthlyRange1');
        document.getElementById('radioDaily').checked = 'checked';
        if (document.getElementById('radioDaily1')) document.getElementById('radioDaily1').checked = 'checked';
    } else {
        showElement('monthlyRange');
        if (document.getElementById('monthlyRange1')) showElement('monthlyRange1');
        hideElement('dailyRange');
        if (document.getElementById('dailyRange1')) hideElement('dailyRange1');
        document.getElementById('radioMonthly').checked = 'checked';
        if (document.getElementById('radioMonthly1')) document.getElementById('radioMonthly1').checked = 'checked';
    }
}

function showEmailInputDiv(overElement,id) {
    var xPos = findPosX(overElement);
    var yPos = findPosY(overElement);
    var div = document.getElementById('sendToFriendInput_'+id);
    if (div.style.display == 'none') {
            div.style.display = 'inline';
            div.style.left = (xPos)+"px;";
            div.style.top = (yPos+20)+"px";
    }
}

function hideEmailInputDiv(id) {
    var div = document.getElementById('sendToFriendInput_'+id);
    div.style.display = 'none';
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}



function initForm() {
    if (document.getElementById("radioDaily"))
        document.getElementById("radioDaily").checked="checked";
    if (document.getElementById("radioDaily1")) document.getElementById("radioDaily1").checked="checked";
        hideElement("monthlyRange");
    if (document.getElementById("monthlyRange1"))
        hideElement("monthlyRange1");
}

function showOnMap( locationId ) {
    var location = locations[locationId];
    location["marker"].openInfoWindowHtml( location["description"] );
}

function highlightApt(id) {
    var aptDiv = document.getElementById('aptDiv_'+id);
    aptDiv.style.background = '#F5E2D0';
}

function obscureApt(id) {
    var aptDiv = document.getElementById('aptDiv_'+id);
    aptDiv.style.background = '#EBEBEB';

}

function switchTab(tabId) {

    var mapDiv = document.getElementById('mapContainer');
    var videoDiv = document.getElementById('videoGallery');
    var photoDiv = document.getElementById('photoGallery');
    var selectedDiv = document.getElementById(tabId);

    var videoTab = document.getElementById('videoGalleryTab');
    var photoTab = document.getElementById('photoGalleryTab');

    if (tabId == 'photoGallery') {

        mapDiv.style.display = 'none';
        videoDiv.style.display = 'none';
        photoDiv.style.display = 'block';

        //videoTab.style.background = 'url(../img/tab_dx_off.gif) no-repeat;';
        //photoTab.style.background = 'url(../img/tab_sx_on.gif) no-repeat;';
        videoTab.className = "tab_col_dx tab_dx_off";
        photoTab.className = "tab_col_dx tab_dx_on";

        document.getElementById('headerGallerySeparator').className = 'detail_apt_header';

    } else if (tabId == 'videoGallery') {

        //alert( "video" );
        //alert( photoDiv.style.display );
        mapDiv.style.display = 'none';
        photoDiv.style.display = 'none';
        videoDiv.style.display = 'block';

        videoTab.className = "tab_col_dx tab_dx_on";
        photoTab.className = "tab_col_dx tab_dx_off";

        document.getElementById('headerGallerySeparator').className = 'detail_apt_header';

    } else {

        videoDiv.style.display = "none";
        photoDiv.style.display = "none";
        videoTab.className = "tab_col_dx tab_dx_off";
        photoTab.className = "tab_col_dx tab_dx_off";

        mapDiv.style.display = '';

        document.getElementById('headerGallerySeparator').className = 'detail_apt_header_map';

        initMap();
    }
}

function populateMapInfo() {
    //alert(map.getZoom()+" "+map.getCenter().lat()+" "+map.getCenter().lng());
    var inputZoom = document.getElementsByName('mapZoom');
    var inputCenterLat = document.getElementsByName('mapCenterLat');
    var inputCenterLng = document.getElementsByName('mapCenterLng');

    for (var i = 0; i < inputCenterLat.length; i++ ) {
        inputCenterLat[i].value = map.getCenter().lat();
    }

    for (var i = 0; i < inputCenterLng.length; i++ ) {
        inputCenterLng[i].value = map.getCenter().lng();
    }

    for (var i = 0; i < inputZoom.length; i++ ) {
        inputZoom[i].value = map.getZoom();
    }
}

function goto(url) {
    if(map && map.isLoaded())
        url += "&mapZoom="+map.getZoom()+"&mapCenterLat="+map.getCenter().lat()+"&mapCenterLng="+map.getCenter().lng();
    window.location = url;
}

function checkMapStatus() {
    if (map.isLoaded())
        loadPoints();
    else window.setTimeout("checkMapStatus()",1000);
}

function changePhoto(id) {
    var photoBig = document.getElementById("photoBig");
    var fileName = document.getElementById(id).name;
    photoBig.src = "http://en.windowsonitaly.com/resources/"+fileName;
}

function displayPreviewImage(overElement,id) {
    var xPos = findPosX(overElement);
    var yPos = findPosY(overElement);
    var div = document.getElementById(id);
    div.style.display = 'inline';
}

function hidePreviewImage(id) {
    document.getElementById(id).style.display = 'none';
}

/**
disabilita le date del calendario precedenti ad oggi
*/
function calendarFromYesterdayDisable(date) {
    var today = new Date();
    today.setDate(today.getDate()-1);
    if (date < today )
        return "disabled";
    else
        return false;
}

/**
disabilita le date del calendario cui è associato a partire dalla data presente nel calendario con id "from"
*/
var done = false;
function calendarDisableFromFrom(date) {
    var otherDateString = document.getElementById("from").value;
    var otherDateStringSplit = otherDateString.split("/");

    var otherDate = new Date( otherDateStringSplit[1] + "/" + otherDateStringSplit[0] + "/" + otherDateStringSplit[2] );
    otherDate.setDate( otherDate.getDate()+1 );

    if (date < otherDate) {
        //alert("disable: "+date);
        return "disabled";
    }
    else {
        //alert("enable: "+date);
        return false;
    }
}

function calendarDisableFromFrom1(date) {
    var otherDateString = document.getElementById("from1").value;
    var otherDateStringSplit = otherDateString.split("/");

    var otherDate = new Date( otherDateStringSplit[1] + "/" + otherDateStringSplit[0] + "/" + otherDateStringSplit[2] );
    otherDate.setDate( otherDate.getDate()+1 );

    if (date < otherDate) {
        //alert("disable: "+date);
        return "disabled";
    }
    else {
        //alert("enable: "+date);
        return false;
    }
}



function sortResults(value) {
    goto(value);    
}

function setToDateAtTomorrow(value,toId,minRentDays) {

    var dateToSetSplit = value.split("/");
    var newDate = new Date( dateToSetSplit[1] + "/" + dateToSetSplit[0] + "/" + dateToSetSplit[2] );
    if (minRentDays != null && minRentDays > 0)
        newDate.setDate(newDate.getDate()+minRentDays);
    else
        newDate.setDate(newDate.getDate()+1);    
    document.getElementById(toId).value = newDate.print('%d/%m/%Y');

}

function showHideSortOptions() {
   if (document.getElementById('sortOptions')) {
      if (document.getElementById('sortOptions').style.visibility == 'hidden')
         document.getElementById('sortOptions').style.visibility ='visible';
      else
         document.getElementById('sortOptions').style.visibility ='hidden';
   }
}
