﻿var popupTimerId = 0;

function showPopup(objId, delay) {
    if (!document.getElementById(objId))
        return;
    if (delay == 0) {
        document.getElementById(objId).style.display = 'block';
    }
    else {
        popupTimerId = setTimeout("document.getElementById('" + objId + "').style.display='block';", delay);
    }
}

function hidePopup(objId) {
    var obj = document.getElementById(objId);
    if (obj)
        obj.style.display = "none";
    if (popupTimerId != 0) {
        clearTimeout(popupTimerId);
        popupTimerId = 0;
    }
}


function rowMouseOver(obj, color, popupId, popupDelay, includePreviousSibling, includeNextSibling) {
    obj.style.backgroundColor = color;
    if (popupId)
        showPopup(popupId, popupDelay);
    if (includePreviousSibling)
        rowMouseOver(getPreviousSibling(obj), color, null, 0, false, false);
    if (includeNextSibling)
        rowMouseOver(getNextSibling(obj), color, null, 0, false, false);
}

function rowMouseOut(obj, popupId, includePreviousSibling, includeNextSibling) {
    obj.style.backgroundColor = "";
    if (popupId)
        hidePopup(popupId);
    if (includePreviousSibling)
        rowMouseOut(getPreviousSibling(obj), null, false, false);
    if (includeNextSibling)
        rowMouseOut(getNextSibling(obj), null, false, false);
}

function getPreviousSibling(obj) {
    var node = obj.previousSibling;
    while (node.nodeType != 1) {
        node = node.previousSibling;
    }
    return node;
}

function getNextSibling(obj) {
    var node = obj.nextSibling;
    while (node.nodeType != 1) {
        node = node.nextSibling;
    }
    return node;
}


function loginboxfocus(obj, defaulttext, ispsw) {
    obj.select();
    if (ispsw && obj.getAttribute("type") != "password") {
        obj = replaceObj(obj, "password");
    }
    if (obj.value == defaulttext)
    {
        obj.value = "";
        obj.style.color = "#000000";
        try {
            setTimeout("document.getElementById('"+obj.id+"').focus();", 10);
        } catch (err) { }
    }
}

function registerDefaultButton(objId) {
    //http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/
    var b = document.getElementById(objId);
    if (b && typeof (b.click) == 'undefined') {
        b.click = function() {
            var result = true;
            if (b.onclick) result = b.onclick();
            if (typeof (result) == 'undefined' || result) {
                eval(unescape(b.getAttribute('href')));
            }
        }
    }
}

function loginboxblur(obj, defaulttext, ispsw)
{
    if (obj.value == "") {
        if (ispsw) {
            obj = replaceObj(obj, "text");
        }
        obj.value = defaulttext;
        obj.style.color = "#777777";
    }
}

function replaceObj(sourceobj, newtype) {
    var newobj = getinput(sourceobj, newtype);
    if (sourceobj.parentNode.replaceChild)
        sourceobj.parentNode.replaceChild(newobj, sourceobj);
    else if (sourceobj.replaceNode)
        sourceobj.replaceNode(newobj);
    return newobj;
}

function getinput(sourceobj, newtype) {
    var obj = document.createElement("INPUT");
    obj.setAttribute("type", newtype);
    obj.setAttribute("id", sourceobj.getAttribute("id"));
    obj.setAttribute("name", sourceobj.getAttribute("name"));
    obj.className = sourceobj.className;
    obj.setAttribute("style", sourceobj.getAttribute("style"));
    obj.setAttribute("value", sourceobj.getAttribute("value"));
    obj.setAttribute("onblur", sourceobj.getAttribute("onblur"));
    obj.setAttribute("onfocus", sourceobj.getAttribute("onfocus"));
    return obj;
}

function registrationSetType(isCompany) {
    var oldclass, newclass, newname;
    var orgNoObj = document.getElementById(orgNoClientId);
    if (isCompany) {
        oldclass = "regrowcompany";
        newclass = "regrowcompanyvisible";
        newname = companyNameString + ":";
        if (orgNoObj && orgNoObj.value == "-")
            orgNoObj.value = "";
    }
    else {
        oldclass = "regrowcompanyvisible";
        newclass = "regrowcompany";
        newname = nameString + ":";
        if (orgNoObj && orgNoObj.value == "")
            orgNoObj.value = "-";
    }
    var objs = getElementsByClassName(oldclass);
    for (var i = 0; i < objs.length; i++)
        objs[i].className = newclass;
    var nameobjs = getElementsByClassName("regtitlename");
    for (var i = 0; i < nameobjs.length; i++)
        nameobjs[i].innerHTML = newname;
}

function registrationShowDelivery(showDelivery) {
    var obj = document.getElementById("deliveryaddress");
    if (showDelivery)
        obj.style.display = "block";
    else
        obj.style.display = "none";
}

/*
Developed by Robert Nyman, http://www.robertnyman.com
Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function(className, tag, elm) {
    if (document.getElementsByClassName) {
        getElementsByClassName = function(className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
				nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
            for (var i = 0, il = elements.length; i < il; i += 1) {
                current = elements[i];
                if (!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
            for (var j = 0, jl = classes.length; j < jl; j += 1) {
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
            for (var k = 0, kl = classes.length; k < kl; k += 1) {
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for (var l = 0, ll = elements.length; l < ll; l += 1) {
                current = elements[l];
                match = false;
                for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};