﻿// JScript-Datei
function setCursorWait()
{
    document.body.style.cursor = 'wait';
    return true;
}

function setCursorDefault()
{
    document.body.style.cursor = 'default';
    return true;
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}

function PrintContent() 
{
    var DocumentContainer = document.getElementById('DIV_PRINT');
    var WindowObject = window.open('', "Einaussteigeliste", "width=740,height=325,top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no");
    var html = "";
    html = html + "<head><link rel='stylesheet' type='text/css' href='http://www.tournet.ch/css/Main.css' /></head><body bgcolor='white' style='background-color:white;'>" + DocumentContainer.innerHTML + "</body>";
    WindowObject.document.writeln(html);        
    WindowObject.document.close();        
    WindowObject.focus();        
    WindowObject.print();        
    WindowObject.close();    
}

function openPopup( bLocationBar, bMenuBar, bStatusBar, bResizable, strURL, strTargetName, iWidth, iHeight)
{
    strParam = ""
    strParam = strParam + "width=" + iWidth + ","
    strParam = strParam + "height=" + iHeight + ","
    strParam = strParam + "dependent=yes," 
    //strParam = strParam + "alwaysRaised=yes,"

    if ( bLocationBar == false )
        strParam = strParam + "locationbar=no,"
    if (bMenuBar) 
        strParam = strParam + "menubar=yes,"
    if (bResizable== false)
        strParam = strParam + "resizable=no,"
    else
        strParam = strParam + "resizable=yes,"
    if (bStatusBar== false)
        strParam = strParam + "status=no,"
    	
    strParam = strParam + "scrollbars=yes"   
    Neufenster = window.open( strURL, strTargetName, strParam );
    Neufenster.focus();
}

function openPopupModal(strURL, strTargetName, iWidth, iHeight) 
{
    strParam = ""
    if (getInternetExplorerVersion() > 0) 
    {
        strParam = strParam + "dialogWidth:" + iWidth + "px;"
        strParam = strParam + "dialogHeight:" + iHeight + "px"
        
        var args = new Object;
        args.window = window;
        showModalDialog(strURL, args, strParam);
    }
    else 
    {
        strParam = strParam + "width=" + iWidth + ","
        strParam = strParam + "height=" + iHeight + ","
        strParam = strParam + "dependent=yes,locationbar=no,menubar=no,status=no,modal=yes,resizable=no,scrollbars=yes"        
        Neufenster = window.open(strURL, strTargetName, strParam);
        Neufenster.focus();
        //return false;
    }
}

function OpenZimmerRes(pTD, pResStrPtr, pLLPtr) 
{
    document.getElementById("UpdateProgress").style.display = 'block';
    var args = new Object;
    args.window = window;
    var oReturnValue = showModalDialog("Dialog/DLG_ZimmerRes.aspx?ResStdPtr=" + pResStrPtr + "&PtrResLL=" + pLLPtr, args, "dialogWidth:275px;dialogHeight:400px");
    if (oReturnValue != "-1") 
    {
        document.getElementById("UpdateProgress").style.display = 'block';
        window.location = window.location;
    }
    else
        document.getElementById("UpdateProgress").style.display = 'none';
    //pTD.innerHTML = oReturnValue;
}

function OpenZimmerplan(pPtrDsLandleistung,width,height)
{
    w = window.open("Dialog/DLG_Zimmerplan.aspx?PtrDsLandleistung=" + pPtrDsLandleistung,'',"width=" + width + ",height=" + height + ",locationbar=no,resizable=no,status=no,dependent=yes");
    w.focus();
}

function OpenWinRoutenhalt1(pResStrPtr, pTrpPtr)
{
    openPopupModal("Dialog/DLG_EinAusstieg.aspx?ResStdPtr=" + pResStrPtr + "&TrpPtr=" + pTrpPtr, '', 480, 630);
}

function OpenWinSitzplatz1(pResStrPtr,pSBPtr,flug)
{
    if (flug)
        window.openPopup( false, false, false, false, "DLG_Sitzplatz.aspx?ResStdPtr=" + pResStrPtr + "&SBPtr=" + pSBPtr ,'',650,380);
    else
        window.openPopup( false, false, false, false, "DLG_Sitzplatz.aspx?ResStdPtr=" + pResStrPtr + "&SBPtr=" + pSBPtr ,'',550,380);
}

function OpenWinSitzplatz2(pDsPtr,pTrpPtr,flug)
{
    if (flug)
        window.openPopup( false, false, false, false, "DLG_Sitzplatz.aspx?DsPtr=" + pDsPtr + "&TrpPtr=" + pTrpPtr ,'',650,420);
    else
        window.openPopup( false, false, false, false, "DLG_Sitzplatz.aspx?DsPtr=" + pDsPtr + "&TrpPtr=" + pTrpPtr ,'',550,420);
}

function OpenKommentarBild(prefix,mandant,koid,koalaufnr)
{
    window.openPopup( false, false, false, true, "Dialog/DLG_DiaShow.aspx?Prefix=" + prefix + "&KoMandant=" + mandant + "&KoId=" + koid + "&KoaLaufnr=" + koalaufnr,'',460,550);
}

function showToolTip()
{
    s = '<table width="100%" cellspacing="2" cellpadding="0" border="0">';
    s += '<tr><td>Jamann</td></tr>';
    s += '</table>';
    toolTip(s);
}

function changeDate(diff,pEdit)
{
    var d = getDate(pEdit.value);
    if (!isNaN(d))
    {
        d.setDate(d.getDate() + diff);
        pEdit.value = getDateStr(d);
    }
}

function getDate(value)
{
    var str = value;
    var weekday = "";
    var t=Number.NaN,m=Number.NaN,j=Number.NaN;
    if (str.indexOf(',') >= 0) // Fr, Weekday Filtern
        str = Trim(str.substr(str.indexOf(',')+1));
    if (str.indexOf('.') >= 0) // Tag Filtern
    {
        t = toInt(str.substr(0,str.indexOf('.')));
        str = str.substr(str.indexOf('.')+1);
        if (str.indexOf('.') >= 0) // Monat Filtern
        {
            m = toInt(str.substr(0,str.indexOf('.')));
            str = Trim(str.substr(str.indexOf('.')+1));
            if (str != "")
            {
                j = toInt(str);
                if (j < 100)
                    j += 2000;
            }
        }
        else
            m = toInt(str);
        if (isNaN(j)) // Falls Kein Jahr eingegeben
        {
            var now = new Date();            
            if ((m > 0) && (m <= 12))
            {
                j = now.getFullYear();
                if (m < now.getMonth()+1)
                    j++;
                else
                if (m == now.getMonth()+1)
                    if (t < now.getDate())
                        j++;        
            }        
        }
    }
    else
    {
        if (str != "") // Falls Nur Wochentag eingegeben, dann nächstes wählen
        {
            var now = new Date();
            var i=0;
            while (i < 7)
            {
                if (str.substr(0,2) == getWeekDay(now))
                    return now;
                now.setDate(now.getDate() + 1);
                i++;
            }            
        }
    }
    if ((!isNaN(t)) && (!isNaN(m)) && (!isNaN(j)))
        return new Date(j, m-1, t);
    return Number.NaN;
}

function checkDate(pEdit)
{
    var d = getDate(pEdit.value);
    pEdit.value = getDateStr(d);
    if (isNaN(d))
        pEdit.focus();
}

function getWeekDay(d)
{    
    var days = new Array("So","Mo","Di","Mi","Do","Fr","Sa");
    var dayOfWeek = d.getDay();
    return days[dayOfWeek];
}

function getDateStr(d)
{    
    if (!isNaN(d))
        return getWeekDay(d) + ", " + Format(d.getDate(),2) + "." + Format(d.getMonth()+1,2)+ "." + Format(d.getFullYear(),4);
    return "";
}

function toInt(wert)
{
    while (wert.substr(0,1) == '0')
        wert = wert.substr(1);
    if (wert.length == 0)
        wert = "0";
   return parseInt(wert);
}

function checkTime(pEdit)
{
    var str = pEdit.value;
    var h=Number.NaN,m=Number.NaN;
    
    if (str.indexOf(':') >= 0)
    {
        h = toInt(str.substr(0,str.indexOf(':')));
        m = toInt(str.substr(str.indexOf(':')+1));
    }
    else
    if (str.indexOf('.') >= 0)
    {
        h = toInt(str.substr(0,str.indexOf('.')));
        m = toInt(str.substr(str.indexOf('.')+1));
    }
    else
    if (str.indexOf(',') >= 0)
    {
        h = toInt(str.substr(0,str.indexOf(',')));
        m = toInt(str.substr(str.indexOf(',')+1));
    }
    else
    {
        h = toInt(str);
        m = 0;
    }
    if ((!isNaN(h)) && (!isNaN(m)))
    {
        var s = Format(h,2) + ":" + Format(m,2);
        pEdit.value = s;
    }
    else
        pEdit.value = "";
}

function Format(m,format)
{
    var ret = "";
    var test = 10;
    for (var i=1;i<format;i++)
    {
        if (m < test)
            ret = ret + "0";    
        test = test * 10;
    }
    ret = ret + m.toString();
    return ret;
}

function Trim(sString)
{
    while (sString.substring(0,1) == ' ')
        sString = sString.substring(1,sString.length);
    while (sString.substring(sString.length-1,sString.length) == ' ')
        sString = sString.substring(0,sString.length-1);  
    return sString;
}

function toggleLayer( whichLayer )
{  
    var elem, vis;  
    if( document.getElementById ) // this is the way the standards work    
        elem = document.getElementById( whichLayer );  
    else if( document.all ) // this is the way old msie versions work      
        elem = document.all[whichLayer];  
    else if( document.layers ) // this is the way nn4 works    
    elem = document.layers[whichLayer];  
    vis = elem.style;  // if the style.display value is blank we try to figure it out here  
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
        vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';  
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function toggleLayerVS(img,whichLayer) {
    var elem, vis;
    if (document.getElementById) // this is the way the standards work    
        elem = document.getElementById(whichLayer);
    else if (document.all) // this is the way old msie versions work      
        elem = document.all[whichLayer];
    else if (document.layers) // this is the way nn4 works    
        elem = document.layers[whichLayer];
    vis = elem.style;  // if the style.display value is blank we try to figure it out here
    if (vis.visibility == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        vis.visibility = (elem.offsetWidth != 0 && elem.offsetHeight != 0) ? 'visible' : 'collapse';
    vis.visibility = (vis.visibility == '' || vis.visibility == 'visible') ? 'collapse' : 'visible';
    if (vis.visibility == 'visible')
        img.src = "img/expand.jpg";
    else
        img.src = "img/collapse.jpg";
}

// Repeater mit Radiobuttons geht sonst nicht
function SetUniqueRadioButton(nameregex, current)
{    
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i];
      if (elm.type == 'radio')
       if (elm.name != current.name)
      {
        //alert(elm.value); //.name
        if (re.test(elm.value))
            elm.checked = false;
      }
   }
   current.checked = true;
}

function getRechnungsTable(width,Titel,arPreiszeilen)
{
    var str;
    str = "<table style='" + width + "' class='Detail'>";
    str = str + "<tr><td colspan='6' class='HeaderTable2'>" + Titel + "</td></tr>";
    str = str + "<tr>";
    str = str + "<td style=\"width:  58%;\"></td>";
    str = str + "<td style=\"width:   7%;\"></td>";
    str = str + "<td style=\"width:   1%;\"></td>";
    str = str + "<td style=\"width:   8%;\"></td>";
    str = str + "<td style=\"width:   8%;\"</td>";
    str = str + "<td style=\"width:  18%;\"</td>";
    str = str + "</tr>";
    for(var i = 0; i < arPreiszeilen.length;i++)
    {
        if (i%2==0)
            str = str + "<tr class='RowPair'>";
        else
            str = str + "<tr class='RowUnpair'>";
        str = str + "<td style=\"white-space:nowrap;\" align=\"left\">" + arPreiszeilen[i]["Bezeichnung"] + "</td>";
        str = str + "<td align=\"right\">" + arPreiszeilen[i]["Menge"] + "</td>";
        str = str + "<td></td>";
        str = str + "<td>" + arPreiszeilen[i]["BetragsCode"] + "</td>";
        if (arPreiszeilen[i]["Trennstrich"] != "1")
        {
            str = str + "<td align=\"right\">" + arPreiszeilen[i]["Ansatz"] + "</td>";
            str = str + "<td  align=\"right\">" + arPreiszeilen[i]["Betrag"] + "</td>";
        }
        else
        {
            str = str + "<td  colspan=\"2\" align=\"right\">" + arPreiszeilen[i]["Betrag"] + "</td>";
        }
        str = str + "</tr>";        
    }
    str = str + "</table>";
    return str;
}