var xmlHttpRequest;
var xmlHttpGlobal;

function newXMLHttpRequest() {
    var obj = null ;
    if (window.ActiveXObject) {
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        obj = new XMLHttpRequest();
    }

    return obj;
}

function createXMLHttpRequestG()
{
    xmlHttpGlobal = newXMLHttpRequest();
}

function createXMLHttpRequest()
{
    xmlHttpRequest = newXMLHttpRequest();
}

// This function is used instead of location.href, when the referer page needs
// to be passed on
function setPage(url)
{
    var a = document.createElement("a");

    if (!a.click) { //only IE has this (at the moment);
        window.location = url;
    }
    else
    {
        a.setAttribute("href", url);
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
    }
}

// show an object
function show(id) {
    if (id)
    {
        var obj = document.getElementById(id);

        if (obj) {
            obj.style.visibility = "visible";
            obj.style.display = "block";
        }
    }
}

// hide an object
function hide(id) {
    if (id)
    {
        var obj = document.getElementById(id);
        if (obj) {
            obj.style.visibility = "hidden";
            obj.style.display = "none";
        }
    }
}


function showdivbyid(idshow, classgroup) {
    var myregexp = new RegExp(classgroup);
    var divs = document.getElementsByTagName('div');
    for (i = 0; i < divs.length; i++) {
        if (myregexp.exec(divs[i].className)) // if the div classname matches the given class then decide whether to show or hide it
        {
            if (divs[i].id.match(idshow)) //if the div ID equals the idshowed value then show it
            {
                if (document.getElementById) // DOM3 = IE5, NS6
                {
                    divs[i].style.display = "block";// show/hide
                    divs[i].style.visibility = "visible";
                } else
                    if (document.layers) // Netscape 4
                    {
                        document.layers[divs[i]].display = 'block';
                        document.layers[divs[i]].style.visibility = 'visible';
                    } else // IE 4
                    {
                        document.all.divs[i].display = 'block';
                        document.all.style.visibility = 'visible';
                    }
            }
            else //otherwise hide it
            {
                if (document.getElementById)
                    divs[i].style.display = "none";
                else
                    if (document.layers) // Netscape 4
                        document.divs[i].display = 'none';
                    else // IE 4
                        document.all.divs[i].display = 'none';
            }
        }
    }
}

function logoff(topURL) {
    //    if (confirm("Do you want to save drugs that you have into evaluation with name DD/MM/YYYY?")) {

    //    }

    DeleteCookie("conditions");

    try {
        for (var i = 0; i < _listOfPopups.length; i++) {
            try {
                _listOfPopups[i].clear();
            } catch (e) {
            }
        }
    } catch (e) {
    }

    top.location = topURL;
}

function SetCookie(cookieName, cookieValue, nDays, domain) {
    var cookieString = cookieName + "=" + escape(cookieValue);

    // Session cookie is default
    if (nDays >= 1)
    {
        var today = new Date();
        var expire = new Date();
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        cookieString += ";expires=" + expire.toGMTString();
    }

    if (domain) {
        cookieString += ";domain=" + domain;
    }

    document.cookie = cookieString;
}

function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    var val = unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
    if (val == ";") val = "";    // because of IE

    return val;
}

function DeleteCookie(name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function validateEmail(email)
{
    email = trim(email);

    if (email.length <= 0)
    {
        return false;
    }
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.\+]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
    }
    if (splitted[2] != null)
    {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null)
        {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        }// if
        return true;
    }
    return false;
}

// Removes leading whitespaces
function leftTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function rightTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value) {
    return leftTrim(rightTrim(value));
}

// Fading an object in/out from www.robpoyntz.com
//
function Fader(el, fadeIn)
{
    var me = this;
    this.el = (typeof(el) == "object") ? el : (typeof(el) == "string") ? document.getElementById(el) : null;
    this.fadeIn = fadeIn;
    this.doFade = doFade;
    this.setOpacity = setOpacity;
    this.showElement = showElement;
    this.opacity = 0;

    this.doFade();

    function setOpacity()
    {
        if (typeof(this.el.style.opacity) != null) this.el.style.opacity = this.opacity / 100;
        if (typeof(this.el.style.filter) != null) this.el.style.filter = "alpha(opacity=" + this.opacity + ")";
        this.opacity += (this.fadeIn) ? 1 : -1;
    }

    function showElement(show)
    {
        if (this.el && this.el.style) this.el.style.display = (show) ? "block" : (this.fadeIn) ? "block" : "none";
    }

    function doFade()
    {

        this.showElement(true);
        if (this.fadeIn)
        {
            this.opacity = 0;
            this.setOpacity();

            for (var i = 0; i <= 100; i++)
            {
                var newFunc = function() {
                    me.setOpacity();
                };
                setTimeout(newFunc, 8 * i);
            }
        }
        else
        {
            this.opacity = 100;
            this.setOpacity();

            for (var i = 0; i <= 100; i++)
            {
                var newFunc = function() {
                    me.setOpacity();
                };
                setTimeout(newFunc, 8 * i);
            }
        }

        /* hide object
         var newFunc = function () { me.showElement(); };
         setTimeout(newFunc, 810);
         */
    }
}

function fade(obj, fadeIn)
{
    if (obj) new Fader(obj, fadeIn);
}

function fadeIn(objID)
{
    fade(objID, true);
}

function fadeOut(objID)
{
    fade(objID, false);
}
