/*******************************************************************************\
|	Name:		Common functions												|
|	Filename:	common.js														|
|	Version:	1.0.0															|
|	Author:		Eric van Blokland												|
|	Project:	Footsteps2														|
|	Summary:	Common functions												|
|																				|
\*******************************************************************************/

// Fixing the opener stuff
try{
	window.getOpener=function() {
		if(window.opener)
			return window.opener;
		if(window.parent && window.parent.isDialog) 
			return window.parent.opener;
		return null;
	}
	var opener=null;
	if(window.parent && window.parent.isDialog) {
		window.close=function() {
			window.parent.close();
		}		
		window.opener=window.parent.opener;
		opener=window.parent.opener;
	}
	else if(window.opener) {
		opener=window.opener;
	}
	else if(parent.dialogArguments) {
		this.opener=parent.dialogArguments;
		opener=parent.dialogArguments;
	}

}
catch(e)	{

}
function setRelativeHref(href,location) {
	if(!location)
		location=window.location;	
	if(href.indexOf("/")===0) {
		location.href=href;
	}
	else if(href.indexOf("http://")===0) {
		location.href=href;
	}
	else {			
		location.href=location.pathname.substring(0,location.pathname.lastIndexOf('/')+1)+href;
	}
}
function FSAttachEvent(targetObject,eventName,eventFunction) {
	if(!targetObject)
		return;
	if(targetObject.attachEvent) {		
		targetObject.attachEvent('on'+eventName,eventFunction);	
	}
	else {
		targetObject.addEventListener(eventName,eventFunction,false);
	}
	
}
 function showDown(evt) {
        evt = evt || window.event;        
       // if(document.location.href.indexOf('fck')>-1) {        	
       // 	cancelKeyFCK(evt);
       // 	return;
       // }
        if (evt) {
        	var target=null;
        	if(evt.originalTarget)
        		target=evt.originalTarget;
        	else if(evt.relatedTarget)
        		target=evt.relatedTarget;
        	else if(evt.srcElement)
        		target=evt.srcElement;
        	else
        		target=new Object();        	    	
            if (evt.keyCode == 8 && (target.type != "text" && target.type != "textarea" && target.type != "password" && !target.contentEditable)) {
                // When backspace is pressed but not in form element
            	
            	
                cancelKey(evt);                
            }
            else if (evt.keyCode == 116) {
                // When F5 is pressed
                cancelKey(evt);                
            }
            else if (evt.ctrlKey && (evt.keyCode == 78 || evt.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 cancelKeyFCK(evt) {
        evt.cancelBubble=true;
    }

function disable_user_nav(oDoc)	{
	 // Additional code for NS
	window.history.forward(1);
	if(!oDoc)
		oDoc=document;
    FSAttachEvent(oDoc,'keydown', 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	{
		LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-height)/2 : 0;					
		var WinSettings = "center:yes;resizable:no;dialogWidth:"+width+"px;dialogHeight:"+height+"px;dialogTop:"+TopPosition+"px;dialogLeft:"+LeftPosition+"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);
if(!expiredays)
	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 + "=" + encodeURIComponent(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 decodeURIComponent(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";
}
}

