/* XHConn - Simple XMLHTTP Interface */
function XHConn()
{
    var xmlhttp, bComplete = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { try { xmlhttp = new XMLHttpRequest(); }
    catch (e) { xmlhttp = false; }}}
    if (!xmlhttp) return null;
    this.connect = function(sURL, sMethod, sVars, fnDone)
    {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();

        try {
            if (sMethod == "GET")
            {
                xmlhttp.open(sMethod, sURL+"?"+sVars, true);
                sVars = "";
            }
            else
            {
                xmlhttp.open(sMethod, sURL, true);
                xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
                xmlhttp.setRequestHeader("Content-Type",
                "application/x-www-form-urlencoded");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && !bComplete)
                {
                    bComplete = true;
                    fnDone(xmlhttp);
                }};
                xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
    };
    return this;
}

function hinweis()
{
    Fhinweis = window.open("/hinweise.html","hinweisPopup","width=468,height=460,scrollbars=AUTO");
}

function registerpop()
{
    Firp = window.open("/registerpop.htm","rpop","width=468,height=460,scrollbars=AUTO");
}

function download()
{
    F = window.open("/vergleich.html","Popup","width=420,height=377");
}

function dpopup(url, w, h, fname)
{
    FF = window.open(url,fname,"width="+w+",height="+h+",scrollbars=yes");
}

function rkbox() {
    Fh = window.open("/rk_popup.html","rk","width=260,height=190,scrollbars=AUTO");
    Fh.moveTo(300,150);
}

function ts_quickjump() {
    var sIndex;
    var sValue;
    sIndex=document.quickjump.qs.selectedIndex;
    sValue=document.quickjump.qs.options[sIndex].value;
    if(sValue!='') {
        window.location.href="/?" + sValue;
    }
}
/* Search Field on Landing Page */
if (this.searchBoxDefaultValue == undefined || this.searchBoxDefaultValue == '') {
    /* Please leave this in place for WL. Thanks! */
    var searchBoxDefaultValue='Suche Vorlagen';
}

function setSearchBoxDefaultValue() {
    if (document.getElementById('searchform') && document.getElementById('searchbox') && document.searchform.searchbox.value == '') {
        var f0rm = document.getElementById('searchform');
    } else if (document.getElementById('searchform_nz_start') && document.getElementById('searchbox')) {
        var f0rm = document.getElementById('searchform_nz_start');
    } else {
        var f0rm = false;
    }
    if ((f0rm) && f0rm.searchbox.value == '') {
        f0rm.searchbox.value = this.searchBoxDefaultValue;
    }
}
function emptySearchBox() {
    if (document.searchform.searchbox.value == this.searchBoxDefaultValue) {
        document.searchform.searchbox.value = '';
    }
}

function submitSearch(formid) {
    if (document.getElementById('searchbox') != 'undefined' && document.getElementById('searchbox').value != searchBoxDefaultValue && document.getElementById('searchbox').value != '') {
        document.forms.searchform.submit();
    }
}

function submitSearch2(formid, formfieldid) {
    if (document.getElementById(formfieldid) != 'undefined' && document.getElementById(formfieldid).value != searchBoxDefaultValue && document.getElementById(formfieldid).value != '') {
        document.getElementById(formid).submit();
    }
}

/* Formular Page -> Preview for different filetypes */
function toggle_preview(app, pic_link) {
    document.getElementById('preview').src='/formular/'+pic_link;
    document.getElementById(app).style.color='#000000';
    document.getElementById(app).style.fontWeight='bold';
    var app_types = new Array('PDF','Word','XLS','ZIP');

    for (var i=0;i < app_types.length;i++) {
        if (document.getElementById(app_types[i]) && app_types[i] != app) {
            document.getElementById(app_types[i]).style.color='#1A338F'
            document.getElementById(app_types[i]).style.fontWeight='normal'
        }
    }
}

/* Formular Page -> Request to check favorites */
function addFav(formid) {
    var con = new XHConn();
    if (!con) { alert('Ihr Browser ist zu alt!\nEr unterstuetzt die WEB 2.0 Technik nicht.\n\nBitte steigen Sie auf Firefox, Safari oder Opera um.\n\n\thttp://www.getfirefox.de/'); return false; }

    var addFav = function (oXML) {
        d = document.getElementById("fav_save");
        d.innerHTML = oXML.responseText;
    };

    con.connect("/index.php", "GET", "add_favorite="+true+"&form_id="+formid, addFav);

}

/* Formular Page -> Request to check favorites */
function showPicture(imgUrl, formid, popupTitle) {
    $('#FBpopupImg').attr('src', imgUrl);
    $('#FBFSprevnr').css('display', 'block');
    $('#FBpreviewTitel').attr('innerHTML', popupTitle);
    $('#FBFSprevnr').css('top', ($(window).attr('scrollY')+10)+'px');
}

/* protect email-adresses from spambots */
function epostWriter(mailsvr, mailnam, maildom, mailtext, mailsubject) {
    var mailaddress = mailnam + '@' + mailsvr + '.' + maildom;
    if(mailtext == '' || typeof(mailtext) == "undefined") {
        mailtext = mailaddress;
    }
    if(mailsubject != '' && typeof(mailsubject) != "undefined") {
        mailaddress += '?subject=' + mailsubject;
    }
    document.write('<a href="mailto:' + mailaddress + '">' + mailtext + '</a>');
}

/* General pop-up function */
function NewWindow(url, fname, w, h, resize, scroll, tibar, locbar, bmenu) {
    if (resize == 'undefined' || resize == 'false' ) { var rz   = 'no'; } else { var rz   = 'yes';    }
    if (scroll == 'undefined' || scroll == 'false' ) { var scrol= 'no'; } else { var scrol= 'yes';    }
    if (tibar  == 'undefined' || tibar  == 'false' ) { var tbar = 'no'; } else { var tbar = 'yes';    }
    if (locbar == 'undefined' || locbar == 'false' ) { var lbar = 'no'; } else { var lbar = 'yes';    }
    if (bmenu  == 'undefined' || bmenu  == 'false' ) { var menu = 'no'; } else { var menu = 'yes';    }
    MyWindow=window.open(url, fname, "height="+h+",width="+w+",top=50,left=50,resizable="+rz+",scrollbars="+scrol+",toolbar="+tbar+",locationbar="+lbar+",menubar="+menu);
    MyWindow.focus();
}

function switchBasketPopUp(nr) {
    document.getElementById('FBFSprevnr'+nr).style.display='block';
    /*wtf
     if (nr == 1) {
        document.getElementById('FBFSprevnr1').style.display='block';
        document.getElementById('FBFSprevnr2').style.display='none';
        document.getElementById('FBFSprevnr3').style.display='none';
    } else if (nr == 2) {
        document.getElementById('FBFSprevnr1').style.display='none';
        document.getElementById('FBFSprevnr2').style.display='block';
        document.getElementById('FBFSprevnr3').style.display='none';
    } else {
        document.getElementById('FBFSprevnr1').style.display='none';
        document.getElementById('FBFSprevnr2').style.display='none';
        document.getElementById('FBFSprevnr3').style.display='block';
    }*/
}

function changeProductType(boxId, typeNr) {
    document.getElementById('FBbox' + boxId + 'Type' + typeNr).style.display='block';
    document.getElementById(actualVisible[boxId]).style.display='none';
    actualVisible[boxId] = 'FBbox' + boxId + 'Type' + typeNr;
}

/* START Drag & Drop Code */
var dragobjekt = null;        /* Objekt zum Bewegen */
var dragx = 0; var dragy = 0; /* Position, an der das Objekt angeklickt wurde. */
var posx = 0; var posy = 0;   /* Mausposition */
function draginit() { /* Initialisierung der Überwachung der Events */
    document.onmousemove = drag;
    document.onmouseup = dragstop;
}
function dragstart(element) { /* Wird aufgerufen, wenn ein Objekt bewegt werden soll. */
    dragobjekt = element;
    dragx = posx - dragobjekt.offsetLeft;
    dragy = posy - dragobjekt.offsetTop;
}
function dragstop() { /* Wird aufgerufen, wenn ein Objekt nicht mehr bewegt werden soll. */
    dragobjekt=null;
}
function drag(ereignis) { /* Wird aufgerufen, wenn die Maus bewegt wird und bewegt bei Bedarf das Objekt. */
    posx = document.all ? window.event.clientX : ereignis.pageX;
    posy = document.all ? window.event.clientY : ereignis.pageY;
    if (dragobjekt != null && posx >0 && posy >0) {
        dragobjekt.style.left = (posx - dragx) + 'px';
        dragobjekt.style.top = (posy - dragy) + 'px';
    }
}
/* ENDE Drag & Drop Code */

/* function checklist(object von ul,ol,menu oder dir; checked)
* Die Funktion checked input[type=checkbox]-Felder innerhalb des Listenelements li an bzw. aus.
* Mögliche Struktur:
* <input type="checkbox" onClick="checklist(document.getElementById('checkdir'),this.checked)" />
* <ul id="checkdir">
*   <li><input type="checkbox"></li>
*   .
*   .
*   .
*   <li><input type="checkbox"></li>
* </ul>
*/
function checklist(dir,checked) {
//    alert(checked);
    for (i=0; i<dir.childNodes.length; i++) {
        if (dir.childNodes[i].nodeName == 'LI') {
            for (j=0; j<dir.childNodes[i].childNodes.length; j++) {
                if (dir.childNodes[i].childNodes[j].nodeName == 'INPUT') {
                    if (dir.childNodes[i].childNodes[j].type == 'checkbox') {
                        dir.childNodes[i].childNodes[j].checked = checked;
                    }
                }
            } // END for j
        }
    } // END for i
}

/* function submitForm(string von #form)
* Die Funktion versendet das Formular, wenn im Eingabeelement <Enter> gedrückt wurde.
* Mögliche Struktur:
* <input type="text" onClick="checklist(document.getElementById('checkdir'),this.checked)" />
* <ul id="checkdir">
*   <li><input type="checkbox"></li>
*   .
*   .
*   .
*   <li><input type="checkbox"></li>
* </ul>
*/
function submitForm(sFormular) {
  e = window.event;
  if(e.which == 13)
    document.getElementById(sFormular).submit();
  elseif (e.keyCode == 13)
    document.getElementById(sFormular).submit();
}

/* +++ hide empty ads +++ hide empty ads +++ hide empty ads +++ hide empty ads */

/* put ads to proof if empty in this array... */
var adArray = new Array();

/* ...with this function. */
function addAd(obj) {
    adArray.push(obj);
}

/* this function hides empty ads. call it at the end of BODY. */
function disableEmptyAds() {
    var display = 'none';
    for (var i = 0; i < adArray.length; i++) {
        display = 'none';
        /* alert(adArray[i].childNodes.length); */
        for (var j = 0; j < adArray[i].childNodes.length; j++) {
            if (adArray[i].childNodes[j].nodeName != 'SCRIPT' && adArray[i].childNodes[j].nodeName != '#comment' && adArray[i].childNodes[j].nodeName != '#text') {
                /* alert(adArray[i].childNodes[j].nodeName); */
                display = 'block';
            }
        }
        adArray[i].style.display = display;
    }
}
