﻿function loadfile(filename, filetype) {
    var head = document.getElementsByTagName("head")[0];
    var fileref;
    
    if (filetype == "js") {
        fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
    } else if (filetype == "css") {
        fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    head.appendChild(fileref);
}

function removefile(filename, filetype) {
    var targetelement = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none" //determine element type to create nodelist from
    var targetattr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none" //determine corresponding attribute to test for
    var allsuspects = document.getElementsByTagName(targetelement)
    for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
        if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
            allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
    }
}

function setTextSizeActive(obj) {
    document.getElementById('lnkTextSmall').className = "";
    document.getElementById('lnkTextNormal').className = "";
    document.getElementById('lnkTextBig').className = "";
    obj.className = "active";
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function setVisibility(objID, show) {
    if (show) {
        document.getElementById(objID).style.display = "inline";
    } else {
        document.getElementById(objID).style.display = "none";
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function getCurrentDate() {
    var now = new Date();
    return now.getDate() + "." + (now.getMonth() + 1) + "." + now.getYear();
}

function showoverlay(e, objID) {
    var pos = getPosition(e);
    var box = document.getElementById(objID);
    document.getElementById(objID).style.display = 'block';
    document.getElementById(objID).style.top = (pos.y - 75) + 'px';
    document.getElementById(objID).style.left = (pos.x - document.getElementById('container').offsetLeft + 10) + 'px';
}

function getPosition(e) {
    if (!e) var e = window.event;
    e = e || window.event;
    var cursor = { x: 0, y: 0 };
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
				            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
				            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function justpop(text, title) {
    win = window.open(getBaseURL() + 'portaldata/1/Modules/Plain.asp' + '?message=' + text + '&title=' + title, 'justapopup', 'height=320,width=480,menubar=no,location=no,scrollbars=yes,resizable=yes');
    win.focus();
}

function getBaseURL() {
    var baseURL = '';
    switch (window.location.host) {
        case 'www.gastrosocial.ch':
            baseURL = window.location.protocol + '//' + window.location.host + '/';
            break;
        case '212.55.215.202': 
            baseURL = window.location.protocol + '//' + window.location.host + '/GastroSocial2009/';
            break;
    }
    return baseURL;
}

function mailto(s) {
    var left = '';
    var right = '';

    for (a = 0; a < s.length; a = a + 2)
        left += s.charAt(a);

    for (a = 1; a < s.length; a = a + 2)
        right = s.charAt(a) + right;

    window.location.href = "mailto:" + left + right;
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}
