/*******************************************************************************\
|	Name:		Common functions												|
|	Filename:	common.js														|
|	Version:	1.0.0															|		
|	Author:		Eric van Blokland												|	
|	Project:	Footsteps2														|
|	Summary:	Common functions												|
|																				|
\*******************************************************************************/
try{
	if(parent.dialogArguments)
		this.opener=parent.dialogArguments;
}
catch(e)	{

}

 function showDown(evt) {
        evt = (evt) ? evt : ((event) ? event : null);
        if (evt) {
            if (event.keyCode == 8 && (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password")) {
                // When backspace is pressed but not in form element
                cancelKey(evt);
            }
            else if (event.keyCode == 116) {
                // When F5 is pressed
                cancelKey(evt);
            }
            else if (event.ctrlKey && (event.keyCode == 78 || event.keyCode == 82)) {
                // When ctrl is pressed with R or N
                cancelKey(evt);
            }
        }
    }

    function cancelKey(evt) {
        if (evt.preventDefault) {
            evt.preventDefault();
            return false;
        }
        else {
            evt.keyCode = 0;
            evt.returnValue = false;
        }
    }

function disable_user_nav()	{
	 // Additional code for NS
	window.history.forward(1);

    if (navigator.appName=="Netscape") {
        document.addEventListener("keypress",showDown,true);
    }
    document.onkeydown  = showDown;
}

function get_base_href()	{
	if(window.document.getElementsByTagName('BASE'))	{
		aBase=window.document.getElementsByTagName('BASE');
		for(i=0;i<aBase.length;i++)	{
			if(aBase[i].href)
				return aBase[i].href;
		}
	}
	return '';
}
function openChild(hreference,name,width,height,modal,noloading) {	
	if(!noloading)
		noloading='';
	if(!width)
		width=640;
	if(!height)
		height=540;	
	if(!modal)	{
		LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
		var WinSettings = 'height='+height+',width='+width+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,resizable=no,toolbar=no,status=no,alwaysRaised=yes;edge:raised;';
		return window.top.open(hreference,name,WinSettings);
	} else	{		
		var WinSettings = "center:yes;resizable:no;dialogWidth:"+width+"px;dialogHeight:"+height+"px;status:no;edge:raised;scroll:0;";    
		return window.showModalDialog(get_base_href()+'dialog.php?noloading='+noloading+'&hreference='+hreference, window, WinSettings);
	}
   
}
function initArray() {
    this.length = initArray.arguments.length;
    for (var i = 0; i < this.length; i++)
        this[i] = initArray.arguments[i];
}
function from10toradix(value,radix){
    var retval = '';
    var ConvArray = new initArray(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F');
    var intnum;
    var tmpnum;
    var i = 0;

    intnum = parseInt(value,10);
    if (isNaN(intnum)){
        retval = 'NaN';
    }else{
        while (intnum > 0.9){
            i++;
            tmpnum = intnum;
            // cancatinate return string with new digit:
            retval = ConvArray[tmpnum % radix] + retval;  
            intnum = Math.floor(tmpnum / radix);
            if (i > 100){
                // break infinite loops
                retval = 'NaN';
                break;
            }
        }
    }
    return retval;
}

function setCookie(NameOfCookie, value, expiredays) 
{

// Three variables are used to set the new cookie. 
// The name of the cookie, the value to be stored,
// and finally the number of days until the cookie expires.
// The first lines in the function convert 
// the number of days to a valid date.
//alert(NameOfCookie+ " " + value);
expiredays=2;
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

// The next line stores the cookie, simply by assigning 
// the values to the "document.cookie" object.
// Note the date is converted to Greenwich Mean time using
// the "toGMTstring()" function.
try {
myCookie=NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + ExpireDate.toGMTString())+ ";path=/" + "";
document.cookie = myCookie;

}
catch(e)	{
	alert('Failed to set cookie: '+e.description+ ' ('+NameOfCookie+')');
}
}

function getCookie(NameOfCookie)
{

// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.

if (document.cookie.length > 0) 
{ 

// Second we check to see if the cookie's name is stored in the
// "document.cookie" object for the page.

// Since more than one cookie can be set on a
// single page it is possible that our cookie
// is not present, even though the "document.cookie" object
// is not just an empty text.
// If our cookie name is not present the value -1 is stored
// in the variable called "begin".


begin = document.cookie.indexOf(NameOfCookie+"="); 
if (begin != -1) // Note: != means "is not equal to"
{ 

// Our cookie was set. 
// The value stored in the cookie is returned from the function.

begin += NameOfCookie.length+1; 
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); } 
}
return null; 

// Our cookie was not set. 
// The value "null" is returned from the function.

}

function delCookie (NameOfCookie) 
{

// The function simply checks to see if the cookie is set.
// If so, the expiration date is set to Jan. 1st 1970.

if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
