var startURL = "";
var lastURL = "";
var refererUrl = document.referrer;
if (refererUrl != "" && refererUrl.search(window.location.hostname) > 0) refererUrl = "";
if (document.location.search){
	var s = document.location.search;
	var pos;

	if (s.indexOf("?lp=") == 0){
		pos = s.indexOf("&");
		if (pos > 0){
			startURL = unescape(s.substring(4, pos));
			if ((pos=s.indexOf("&pp=")) > 0)
				lastURL  = unescape(s.substr(pos + 4));
		}else startURL = unescape(s.substr(4));
			
		if (startURL.search(/\.cgi\?/i) > 0 && startURL.indexOf(".cgi?e=") == -1){
		var res = startURL.match(/^([^#]+)#([a-zA-Z0-9_-]+)$/);
		  if (res != null) startURL = res[1];
		     var d = new Date();
		  startURL += "&cache=" + d.getTime();
			
		  if (res != null) startURL += "#" + res[2];
		}
	}

	if ((pos=s.search(/[?&]redir=/)) >= 0) refererUrl = unescape(s.substr(pos+7));
	else if ((pos=s.search(/[?&]framed=/)) >= 0) refererUrl = unescape(s.substr(pos+8));
}


// Make sure we're not inside someone else's frame and load the main URL
function makeTop(optURL){
	if (parent && top != window){
		var newURL = window.location.href;
		top.location.href = newURL;
		return;
	}
	
	// Prevent loading an alias page
	var realHost = getHost();
	if (realHost != window.location.hostname){
		var newURL = "http://" + realHost + window.location.pathname;
		if (refererUrl != "" && newURL.indexOf("?") < 0){
			if (window.location.pathname.length > 1) newURL += "?redir=" + escape(refererUrl);
			else newURL += "/index.html?redir=" + escape(refererUrl);
		}

		window.location.href = newURL;
	}
	
	loadMain(optURL);
}

function saveLast(saveURL){
	var f = window.main;
	if (!f) f = document.main;
	if (f){
		if (saveURL != "") lastURL = saveURL;
		else lastURL = f.location.href;
	}
}

function vaLast(){
	var f = window.main;
	if (!f) f = document.main;
	if (f){
		f.location.href = lastURL;
		lastURL = startURL;
	}
}

function isSecure(){
	return (document.location.href.indexOf("https") == 0);
}

function getHost(){
	var host = window.location.hostname;
	return host;
}

function loadSecure(url){
	var newURL = "https://" + getHost() + document.location.pathname + "?lp=" + escape(url);
	document.location.href = newURL;
}

function loadNormal(url){
	var newURL = "http://" + getHost() + document.location.pathname + "?lp=" + escape(url);
	document.location.href = newURL;	
}

function loadMain(optURL){
	var f = window.main;
	if (!f) f = document.main;
	if (f.document) f = f.document;
	if(startURL == ""){
		startURL = "http://" + document.location.hostname;
		if(optURL && optURL != "") startURL += optURL;
		else return;
	}
	if (f && f.location.href != startURL) f.location.href = startURL;
}

function changeLangue(URL){
	var newURL = "http://" + document.location.hostname + URL;
	document.location.href = newURL;
}

