﻿// Gobal zIndex to insure no more then one element in each layer
var zIndexGlobal = 1000;

// Insure reposition of layers if browser is resized
// TODO: make it relative to its start position.
var divRepositionArray = new Array();

function resizePage()
{
    for (var i = 0; i < divRepositionArray.length; i++)
    {
        divReposition(divRepositionArray[i]);
    }
}

// SHOW/HIDE DIV
// Will show a hidden layer and optional hide old layer 
// "divName"			Name of target layer
// "xPos"				The position of the layer from left
// "yPos"				The position of the layer from top
// "closeLowerAccess"	If there should be access to elements below the layer(s)
function divShow(divName, xPos, yPos, closeLowerAccess, darkOutBackground)
{
    var target = document.getElementById(divName);
    var targetLower = document.getElementById('lowerAccessImg');

    target.style.display = "block";

    divRepositionArray.push(divName);
    divReposition(divName, xPos, yPos);

    target.style.zIndex = zIndexGlobal;
    zIndexGlobal++;

    if (closeLowerAccess)
        document.getElementById('lowerAccessDiv').style.display = "block";

    if (darkOutBackground)
    {
        targetLower.src = "Gfx/_000000_alpha50.png";

        if (document.all)
            targetLower.className = "ieAlfa";
    }
    else
    {
        targetLower.src = "Gfx/_blank.gif";
        targetLower.className = "";
    }
}
// END

// Will hide a layer
// "divName"			Name of target layer
// "openLowerAccess"	If access to the elements below the layer(s) should be allowed
function divHide(divName, openLowerAccess)
{
    if (document.getElementById('lowerAccessDiv') && openLowerAccess)
        document.getElementById('lowerAccessDiv').style.display = "none";

    document.getElementById(divName).style.display = "none";

    for (var i = 0; i < divRepositionArray.length; i++)
    {
        if (divRepositionArray[i] == divName)
        {
            divRepositionArray.splice(i, 1);
            break;
        }
    }
}
// END


// Reposition popLayer
function divReposition(divName, xPos, yPos)
{
    var targetX = Math.ceil((document.body.clientWidth - document.getElementById(divName).clientWidth) / 2);
    var targetY = Math.ceil((document.body.clientHeight - document.getElementById(divName).clientHeight) / 2);

    if (xPos && xPos != 0)
        targetX = xPos;

    if (yPos && yPos != 0)
        targetY = yPos;

    document.getElementById(divName).style.left = targetX;
    document.getElementById(divName).style.top = targetY;
}
// END

// Change row color
function changeRowColor(target, color)
{
	target.bgColor = color;
}
// END

// Toogle selection on select box
function toogleCheckbox(id)
{
	d = document.getElementById(id);
	
	if(d.checked)
		d.checked = false;
	else
		d.checked = true;
}
// END

// Get elements offest from top/left
function rootOffset(target)
{
	var pos = {left: 0, top: 0};
	var e = target;

	//while(e.offsetParent)
	while(e)
	{
		pos.left += e.offsetLeft;
		pos.top += e.offsetTop;
		e = e.offsetParent;
	}
	
	return pos;
}
// END

// POPUP
function openPopUp(url, nameWindow, width, height, clean, resizable)
{
    var strAgent=navigator.userAgent;

	if(clean)
		mywin=window.open(url,nameWindow,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=' + resizable + ',width=' + width + ',height=' + height);
	else
		mywin=window.open(url,nameWindow,'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
		
    if (strAgent.indexOf('MSIE 3')==-1 && strAgent.length!=0)
    {
        mywin.focus();
    }
    
    return mywin;
}
// END

// Get url from page
function getUrl(includeHttp, includeFilename, includeQureystring)
{
	var pageUrl = window.location.hostname;
	
	if(includeHttp)
		pageUrl = "http://" + pageUrl;
	
	if(includeFilename)
	{
		if(includeQureystring)
			pageUrl += window.location.pathname + "?" + window.location.search.substring(1).toLowerCase();
		else
			pageUrl += window.location.pathname;
	}
	else
	{
		if(window.location.pathname.length > 0)
			pageUrl += window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/") + 1);
	}
	
	return pageUrl;
}
// END

function isEmail(target)
{
    var re = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);

    if (document.getElementById(target).value.match(re))
        return true
    else
        return false;
}

function toggleDivLink(target, sender)
{
	var t = document.getElementById(target);
	var s = document.getElementById(sender);

	if (t.style.display == "none")
	{
		t.style.display = "block";
		s.innerHTML = "&raquo; Hide";
	}
	else
	{
		t.style.display = "none";
		s.innerHTML = "&raquo; Show";
	}
}

function toggleDivLinkCookie(target, sender)
{
	var t = document.getElementById(target);
	var s = document.getElementById(sender);

	if (t.style.display == "none")
	{
		t.style.display = "block";
		s.innerHTML = "&raquo; Hide";
		
		dataRequest("Ajax.ashx?action=SettingsCookie&area=" + target + "&value=true");
	}
	else
	{
		t.style.display = "none";
		s.innerHTML = "&raquo; Show";
		
		dataRequest("Ajax.ashx?action=SettingsCookie&area=" + target + "&value=false");
	}
}
