/*

ePeoples.com
common Javascript functions

Karl Glasgow
code@keg4.com
05/2010

*/


var intDefaultPopUpWidth = 590;
var intDefaultPopUpHeight = 450;
function OpenPopUp(strURL, intPopUpWidth, intPopUpHeight) {
	var reURL = /^([^\.\/\\]*)\.[^\.]*$/;
	var strWinstrName
	if (reURL.test(strURL)) {
		aryURL = reURL.exec(strURL);
		strWinstrName = aryURL[1];
	} else {
		strWinstrName = 'newWin';
	}
	var intX = parseInt((screen.width - intPopUpWidth) / 2);
	var intY = parseInt((screen.height - intPopUpHeight) / 2);
	var strWinAttrib = 	'width=' + intPopUpWidth + ',height=' + intPopUpHeight + ',menubar=no,toolbar=no,scrollbars=no,resizable=no,top=' + intY + ',left=' + intX;
	window.open(strURL, strWinstrName, strWinAttrib);
}

function OpenScrollablePopUp(strURL, intPopUpWidth, intPopUpHeight) {
	var reURL = /^([^\.\/\\]*)\.[^\.]*$/;
	var strWinstrName
	if (reURL.test(strURL)) {
		aryURL = reURL.exec(strURL);
		strWinstrName = aryURL[1];
	} else {
		strWinstrName = 'newWin';
	}
	var intX = parseInt((screen.width - intPopUpWidth) / 2);
	var intY = parseInt((screen.height - intPopUpHeight) / 2);
	var strWinAttrib = 	'width=' + intPopUpWidth + ',height=' + intPopUpHeight + ',menubar=no,toolbar=no,scrollbars=yes,resizable=no,top=' + intY + ',left=' + intX;
	window.open(strURL, strWinstrName, strWinAttrib);
}


function ViewPopUp(strPopUpId) 
{
    var intMaxPopUpHeight = 400;
    var intMinTopPadding = 20;

    var intPageHeight = (typeof document.body.scrollHeight != 'undefined'? document.body.scrollHeight : 0);
    var intWindowHeight = (typeof document.body.clientHeight != 'undefined'? document.body.clientHeight : 0);
    var intScrollPosition = (typeof document.body.scrollTop != 'undefined'? document.body.scrollTop : 0);

    var divPopUp_Shadow = document.getElementById('PopUp_' + strPopUpId + '_Shadow');
    var divPopUp_Window = document.getElementById('PopUp_' + strPopUpId + '_Window');
    if (divPopUp_Shadow == null) alert('could not find: PopUp_' + strPopUpId + '_Shadow element');
    if (divPopUp_Window == null) alert('could not find: PopUp_' + strPopUpId + '_Window element');
    if (divPopUp_Shadow == null || divPopUp_Window == null) 
    {
        return false;
    }
    
    if (intPageHeight > 0) 
    {
        divPopUp_Shadow.style.height = (intPageHeight + 'px');
    }

    if (isIE() == true) // Quirk fix for IE
    {
        if (typeof document.body.clientWidth != 'undefined') 
        {
            divPopUp_Shadow.style.width = (document.body.clientWidth + 'px');
            divPopUp_Window.style.width = (document.body.clientWidth + 'px');
        }
        divPopUp_Shadow.style.height = '2000px';
    }
    
    var intPopUpPosition = 0;
    if ((intMaxPopUpHeight + intMinTopPadding) > intWindowHeight) 
    {
        intPopUpPosition = (intScrollPosition + intMinTopPadding);
    }
    else 
    {
        intPopUpPosition = (((intWindowHeight - intMaxPopUpHeight) / 2) + intScrollPosition);
    }
    
    if (intPopUpPosition + intMaxPopUpHeight > intPageHeight && isIE() == false) // Do not overwrite the IE quirk fix value
    {
        divPopUp_Shadow.style.height = ((intPopUpPosition + intMaxPopUpHeight) + 'px');
    }
    divPopUp_Window.style.top = (intPopUpPosition + 'px');

    divPopUp_Shadow.style.display = 'block';
    divPopUp_Window.style.display = 'block';
    
    return true;
}

function ClosePopUp(strPopUpId) 
{
    var divPopUp_Shadow = document.getElementById('PopUp_' + strPopUpId + '_Shadow');
    var divPopUp_Window = document.getElementById('PopUp_' + strPopUpId + '_Window');
    if (!divPopUp_Shadow || !divPopUp_Window) 
    {
        return false;
    }

    divPopUp_Window.style.display = 'none';
    divPopUp_Shadow.style.display = 'none';

    return true;   
}


function setCookie(strName, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = ('; expires=' + date.toGMTString());
	}
	else var expires = '';
	document.cookie = (strName + '=' + value + expires + '; path=/');
}

function getCookie(strName) {
	var strNameEQ = (strName + '=');
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(strNameEQ) == 0) return (c.substring(strNameEQ.length, c.length));
	}
	return null;
}

function deleteCookie(strName) {
	setCookie(strName, '', -1);
}


function isIE()
{
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}


function keyPressed(objElement, objEvent) 
{
    var intAsciiCode;
    
    if (objEvent && objEvent.which) 
    {
        intAsciiCode = objEvent.which;
    } 
    else if (window.event) 
    {
        objEvent = window.event;
        intAsciiCode = objEvent.keyCode;
    }
    return intAsciiCode;
}

function enterPressed(objElement, objEvent) 
{
    return (keyPressed(objElement, objEvent) == 13);
}
