﻿var SelectedTab = 0;
var Container;
var LoadingPanel;
var CurrentTimeout;
var OfferId;
var Url = "";
var Language = 0;
var Company  = 0;
var Offer    = 0;
function Tab_onSelect( TabId, TabName, LanguageId, CompanyId, sender ){    
    if(TabId)       { SelectedTab = TabId; }      else { return false; }
    if(LanguageId)  { Language    = LanguageId; } else { return false; }
    if(CompanyId)   { Company     = CompanyId; }  else { return false; }
    if((TabName != "") && $("TabName")) { $("TabName").innerHTML = TabName; }    
    Offer = 0;
    ReloadDetailPage();
}
function Offer_onSelect( TabId, LanguageId, CompanyId, OfferId, sender ){
    if(TabId)       { SelectedTab = TabId; }      else { return false; }
    if(LanguageId)  { Language    = LanguageId; } else { return false; }
    if(CompanyId)   { Company     = CompanyId; }  else { return false; }
    if(OfferId)     { Offer       = OfferId; }    else { return false; }    
    ReloadDetailPage();
}
function ReloadDetailPage(){   
    if(!Container)    { Container    = $("DetailPageContainer"); }  
    if(!LoadingPanel) { LoadingPanel = $("LoadingPanel"); }
    Url = "";  
    switch( Language ) {
        case 1:  Url = "/de/"; break;
        case 2:  Url = "/it/"; break;
        case 3:  Url = "/en/"; break;
        default: return false;
    }    
    Url += "data/includes/detailpage-ax.asp?ActiveTab=" + SelectedTab + "&IDCompany=" + Company;    
    if( Offer != 0 ) { Url += "&IDOffer=" + Offer; }      
    ShowLoadingPanel();
    new Ajax.Request(Url, {
        onSuccess: function(transport) {
            Container.innerHTML = transport.responseText;
            HideLoadingPanel();
                if(SelectedTab == 2) {
                    LoadGoogleMap2(CompanyName,Address,ZipCode,Location,Telephone,Fax,Email,GoogleMapX,GoogleMapY,GoogleMapRedirectPage);
                }
                EventHandler();
            }
        });
    return false;  
}
function GetLeft(Object) {
    return (Object.offsetParent)? Object.offsetLeft + GetLeft(Object.offsetParent) : Object.offsetLeft;
}
function GetTop(Object) {
    return (Object.offsetParent)? Object.offsetTop + GetTop(Object.offsetParent) : Object.offsetTop;
}
function ShowLoadingPanel(){
    LoadingPanel.style.width      = Container.offsetWidth + "px";
    LoadingPanel.style.height     = parseInt(Container.getStyle("height")) - 30 + "px";        
    LoadingPanel.style.left       = GetLeft(Container) + "px";
    LoadingPanel.style.top        = GetTop(document.getElementById("TableDetailPage"))  + 30 + "px";
    LoadingPanel.style.visibility = "visible";
    LoadingPanel.style.display    = "";
    LoadingPanel.setOpacity(0);
    clearTimeout(CurrentTimeout);
    CurrentTimeout = setTimeout( 'FadeIn()', 50 );      
}
function HideLoadingPanel(){
    clearTimeout(CurrentTimeout);    
    CurrentTimeout = setTimeout( 'FadeOut()', 50 );
}
var CurrentOpacity      = 0;
var AdditionalOpacity   = 0.1;
var MaximumOpacity      = 0.5;
var Reloop              = false;
function FadeIn(){
    Reloop = false;    
    CurrentOpacity += AdditionalOpacity;
    if( CurrentOpacity <= MaximumOpacity ){
        LoadingPanel.setOpacity( CurrentOpacity );
        Reloop = true;
    }
    if( Reloop )
        CurrentTimeout = setTimeout( 'FadeIn()', 50 );
}
function FadeOut(){
    Reloop = false;    
    CurrentOpacity -= AdditionalOpacity;
    if( CurrentOpacity >= 0 ){
        LoadingPanel.setOpacity( CurrentOpacity );
        Reloop = true;
    }
    if( Reloop ){
        CurrentTimeout = setTimeout( 'FadeOut()', 50 );
    } else {
        LoadingPanel.style.visibility = "hidden";
        LoadingPanel.style.display    = "none";
    }
}
function LoadGoogleMap( _Name, _Address, _ZipCode, _Place, _Telephone, _Telefax, _Email, _GoogleMapX, _GoogleMapY, _RedirectPage ) {
    LoadGoogleMap2( _Name, _Address, _ZipCode, _Place, _Telephone, _Telefax, _Email, _GoogleMapX, _GoogleMapY, _RedirectPage );
}
function LoadGoogleMap2( _Name, _Address, _ZipCode, _Place, _Telephone, _Telefax, _Email, _GoogleMapX, _GoogleMapY, _RedirectPage ) {
    if (GBrowserIsCompatible()) { 
    var GoogleMapX = parseFloat( _GoogleMapX );
    var GoogleMapY = parseFloat( _GoogleMapY );
    var GoogleMap = new GMap2(document.getElementById("GoogleMap"));
        //GoogleMap.addControl(new GOverviewMapControl());
    var CompanyIcon                  = new GIcon();
	    CompanyIcon.image            = "/SharedR/Portale/Images/Icons/haus.png";
	    CompanyIcon.iconSize         = new GSize(32, 32);
	    CompanyIcon.shadowSize       = new GSize(18, 20);
	    CompanyIcon.iconAnchor       = new GPoint(8, 22);
	    CompanyIcon.infoWindowAnchor = new GPoint(5, 1);			
	    GoogleMap.setCenter (new GLatLng( GoogleMapY, GoogleMapX ), 10);
        GoogleMap.addControl(new GLargeMapControl());
        GoogleMap.addControl(new GMapTypeControl());
    var Marker = new GMarker(new GLatLng( GoogleMapY, GoogleMapX ), CompanyIcon);
    var MarkerManager = new GMarkerManager(GoogleMap);
        MarkerManager.addMarker(Marker,1,17);  
        if ( _Telefax!= "" ) { _Telefax = "Fax: " + _Telefax; } else { _Telefax = ""; }
        if ( Language == 1 ) {
            var InformationTab = [
    		    new GInfoWindowTab("Info", '<h2>' + _Name + '</h2><br /><font class="euCell">' + _Address + ', ' + _ZipCode + ' ' + _Place + '<br>Tel.:  ' + _Telephone + '<br>' + _Telefax + "<br /><br /><a href='" + _RedirectPage + "' onclick='SelectedTab=4;ReloadDetailPage();return false;'>Anfrage senden</a><br></font>"),
    		    new GInfoWindowTab("Route", '<font class="euCell"><form action="http://maps.google.de/maps" target="_blank"><br>Geben Sie Ihre Startadresse ein:<br>(Strasse Nr, PLZ Ort)<br><input name="saddr" class="euInput" value="" type="text"><br><br><input name="daddr" value="' + _Name + ' @' + _GoogleMapY + ',' + _GoogleMapX + '" type="hidden"><input class="euSubmit" name="send" value="Route anzeigen" type="submit"><br><br></form><font>')
    	    ];
    	} else if ( Language == 2 ) {
            var InformationTab = [
    		    new GInfoWindowTab("Info", '<h2>' + _Name + '</h2><br /><font class="euCell">' + _Address + ', ' + _ZipCode + ' ' + _Place + '<br>Tel.:  ' + _Telephone + '<br>' + _Telefax + "<br /><br /><a href='" + _RedirectPage + "' onclick='SelectedTab=4;ReloadDetailPage();return false;'>Invia richiesta</a><br></font>"),
    		    new GInfoWindowTab("Route", '<font class="euCell"><form action="http://maps.google.it/maps" target="_blank"><br>Partenza<br>(Via Nr, CAP, Località)<br><input name="saddr" class="euInput" value="" type="text"><br><br><input name="daddr" value="' + _Name + ' @' + _GoogleMapY + ',' + _GoogleMapX + '" type="hidden"><input class="euSubmit" name="send" value="cerca" type="submit"><br><br></form><font>')
    	    ];
    	}    	
        GEvent.addListener(Marker, "click", 
            function() {
                Marker.openInfoWindowTabsHtml(InformationTab);
            }
        );
    }
}

function AddNotePaper( ID ){

    var NPUP    = document.getElementById( "NPUP" );
    var NPCT    = NPUP ? parseInt(NPUP.innerHTML) : 0;
    var SP      = document.getElementById( "Sp_" + ID );
    var SPHTML  = SP.innerHTML;
    var IMG     = document.getElementById( "Img_" + ID );
    var IMGF    = document.getElementById( "NPFILE" );
    var RND     = Math.random();
    //var ACT     = document.getElementById("Action").value;

    if (( SPHTML.indexOf("zum") != -1) || (SPHTML.indexOf("agg") != -1) ) {
        SP.innerHTML    = NPDEL;
        IMG.src         = "/SharedR/Portale/Images/Icons/Notepaper_delete.gif";
        IMGF.src        = "/SharedR/Portale/Notepaper/Session.asp?IDCompanyCategory=" + ID + "&RND=" + RND;
        NPUP ? NPUP.innerHTML  = NPCT + 1 : false;
    } else {
        SP.innerHTML    = NPADD;
        IMG.src         = "/SharedR/Portale/Images/Icons/Notepaper.gif";
        IMGF.src        = "/SharedR/Portale/Notepaper/Session.asp?IDCompanyCategory=" + ID + "&RND=" + RND;
        NPUP ? NPUP.innerHTML  = NPCT - 1 : false;
        //if(ACT != "") {
        //    document.getElementById("Row_" + ID ).style.display = "none";
        //    document.getElementById("NameRow_" + ID ).style.display = "none";
        //}
    }    
    if(NPUP) { NPCT  = parseInt(NPUP.innerHTML); }
    var BTNSD;
    if(document.getElementById("SendSiForm")) { BTNSD = document.getElementById("SendSiForm");}
    if( BTNSD && document.getElementById("NPWRN") ) {
        if( NPCT > MXCMP ) {
            BTNSD.disabled = true;
        } else {
            BTNSD.disabled = false;
            document.getElementById("NPWRN").innerHTML = ACCTTL;
        }
    }
}

