﻿/*!
 * KeyVisions eSite JavaScript Library v1.1.0
 * http://www.keyvisions.it
 *
 * Copyright 2011, Giancarlo Trevisan
 *
 * NOTE: Requires jQuery v1.5.1 or later
 *
 * Date: Fri Dec 16 2011 13:14:56 +0100
 */
var eSiteClick = null;
$(function() {
	eSiteSetup();
});

// Setup the refresh mechanism
function eSiteSetup() {
	$('a[href*="*r="], area[href*="*r="]').each(function() {
		$(this).bind("click", function() { eSiteLoad($(this).data("href"), $(this).parents('div[id^="CC"]')[0]); });
		$(this).data("href", $(this).attr("href").replace(" ", "+"));
		$(this).attr("href", "javascript:void(0)");
	});
	$('form.eSiteRefreshable').each(function() {
		$('#' + this.id + ' input[name^="\\*a"]').each(function() { 
			$(this).removeClass("eSiteRefreshable");
			$(this).bind("click", function() { eSiteClick = $(this).attr("name"); });
		});

		$(this).removeClass("eSiteRefreshable");
		$(this).submit(function() {
			var data = $(this).serializeArray();
			if (eSiteClick) {
				data.push({ name: eSiteClick, value: "*" });
				eSiteClick = null;
			}
			var jqXHR = $.ajax({ type: 'POST', async: false, cache: false,
				url: $(this).attr("action") + "&*r=-1", data: data
			});
			eSiteLoad(jqXHR.responseText);
			return false;
		});
	});
}

function eSiteLoad(url, contentId) {
	url = url.replace(/%3E/gi, ">").replace(/%2C/gi, ","); // URLDecode
	var refresh = url.match(/(\*r=)(([1-9][0-9]*(>_?[1-9][0-9]*)?|>[1-9][0-9]*),?)+/);
	if (refresh) {
		var p = refresh[0];
		url = url.replace(p, "*r=-1");
		var a = p.replace("*r=", "").split(",");
		$.ajaxSetup({ cache: false });
		for (var i=0; i<a.length; i++) {
			if (contentId) contentId = contentId.id.substr(2); else contentId = "";
			var b = (a[i] + ">" + contentId).split(">");
			if (b[0] == "") { b[0] = contentId; $("#CC" + contentId).remove(); }
			$("#CC"+(b[1] == "" ? b[0] : b[1])).load(url.replace("*r=-1", "*r=" + b[0]), function() { eSiteSetup(); $(this).show("highlight"); });
		}
	}
}

function eSitePopup(url, target, width, height) {
	desktop = window.open(url, target, "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width="+width+",height="+height+",resizable=no");
	desktop.focus();
}

function eSiteHandlePopup(name, closeIt) {
	if (closeIt == 'true') {
		opener.location.reload();
		window.close();
	} else
		window.name = name;
}

function eSiteSC(n, v) {
    var e = new Date();
    e.setDate(e.getDate() + 365);
    document.cookie = n + '=' + escape(v) + ';expires=' + e.toGMTString();
}

function eSiteGC(n) {
    if (document.cookie.length > 0) {
        s = document.cookie.indexOf(n + '=');
        if (s != -1) {
            s = s + n.length + 1;
            e = document.cookie.indexOf(';', s);
            if (e == -1)
                e = document.cookie.length;
            return unescape(document.cookie.substring(s, e));
        }
    }
    return '';
}

function eSiteCS(obj, id) {
    state = eSiteGC('eSiteCS').replace(id + ',', '');
    e = document.getElementById(id);
    if (e.style.display == 'none') {
        e.style.display = 'block';
        obj.src = 'images/esite/minus.gif';
        eSiteSC('eSiteCS', state + id + ',');
    } else {
        e.style.display = 'none';
        obj.src = 'images/esite/plus.gif';
        eSiteSC('eSiteCS', state);
    }
}

function eSiteT(i, p) {
    for (n = 1; document.getElementById('TB' + i + '_' + n) != null; ++n) {
        document.getElementById('TB' + i + '_' + n).className = 'eSiteDeselected';
        if (document.getElementById('TP' + i + '_' + n))
            document.getElementById('TP' + i + '_' + n).style.display = 'none';
    }
    document.getElementById('TB' + i + '_' + p).className = 'eSiteSelected';
    if (document.getElementById('TP' + i + '_' + p))
        document.getElementById('TP' + i + '_' + p).style.display = 'block';
    document.cookie = 'TP' + i + '=' + p;
}

function eSiteCK(o) {
    for (i = 0; i < o.form.elements.length; ++i)
        if (o.form.elements[i].name == o.name.slice(1)) {
            if (o.checked) 
				o.form.elements[i].value |= o.value;
            else
                o.form.elements[i].value &= ~o.value;
            break;
        }
}

function eSiteCKV(o) {
    for (i = 0; i < o.form.elements.length; ++i)
        if (o.form.elements[i].name == o.name.slice(1)) {
            v = o.form.elements[i];
            break;
        }
    v.value = ' ';
    for (i = 0; i < o.form.elements.length; ++i)
        if (o.form.elements[i].name == o.name)
            if (o.form.elements[i].checked)
                v.value += ((v.value == ' ') ? '' : ', ') + o.form.elements[i].value;
    if (v.value == ' ')
        v.value = '';
}

function eSiteS(o) {
    bit = 0; for (i = 0; i < o.length; ++i)
        if (o[i].selected) bit += parseInt(o[i].value);
    for (i = 0; i < o.form.elements.length; ++i)
        if (o.form.elements[i].name == o.name.slice(1)) {
            o.form.elements[i].value = bit;
            break;
        }
}
function eSiteSV(o) {
    for (i = 0; i < o.form.elements.length; ++i)
        if (o.form.elements[i].name == o.name.slice(1)) {
            v = o.form.elements[i];
            break;
        }
    v.value = ' ';
    for (i = 0; i < o.options.length; ++i)
        if (o.options[i].selected)
            v.value += ((v.value == ' ') ? '' : ', ') + o.options[i].value;
}

var openedMenu = new Array(), timeoutId;
function eSiteShowMenu(option, horizontal) {
    clearTimeout(timeoutId);
    if (option.id.substr(0, 3) == 'PLM') {
        timeoutId = (event.type == 'mouseout') ? setTimeout('eSiteHideMenu(null, false)', 900) : null;
    } else {
        var openMenu = document.getElementById(option.id.replace('PLO', 'PLM'));
        openMenu.style.pixelTop = eSiteTop(option) + (horizontal ? option.offsetParent.offsetHeight : option.offsetTop);
        openMenu.style.pixelLeft = eSiteLeft(option) + (horizontal ? option.offsetLeft : option.offsetParent.offsetWidth);
        eSiteHideMenu((horizontal == null) ? openMenu : null, null); openMenu.style.display = 'block'; openedMenu.push(openMenu); timeoutId = setTimeout('eSiteHideMenu(null, false)', 900);
    }
}

function eSiteHideMenu(openMenu, single) {
    var menu; if (openMenu == null) {
        for (; menu = openedMenu.pop(); menu.style.display = 'none');
    } else {
        while (menu = openedMenu.pop())
            if (single && menu.style.pixelLeft == openMenu.style.pixelLeft) {
                openedMenu.push(menu);
                break;
            } else if (menu.style.pixelLeft >= openMenu.style.pixelLeft) {
                menu.style.display = 'none';
            } else {
                openedMenu.push(menu);
                break;
            }
    }
}

function eSiteTop(obj) {
    var curTop;
    for (curTop = 0; obj = obj.offsetParent; curTop += obj.offsetTop);
    return curTop;
}

function eSiteLeft(obj) {
    var curLeft;
    for (curLeft = 0; obj = obj.offsetParent; curLeft += obj.offsetLeft);
    return curLeft;
}

function eSiteRefresh(url, d) {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            var o = document.getElementById('CC' + d);
            if (o) o.innerHTML = xmlHttp.responseText;
        }
    };
    var s = new Date();
    url = url.replace(/&\*stamp=([^&]*)/g, "") + "&*stamp=" + s.getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

