// do not use PopDebug here as it may not be defined, eg if utility_a.js has not been included in the page

var redirect=1;      // 0=do not redirect, 1=automatic redirect if required, 2=write a redirect link instead
var reload_debug=0;  // define a separate debug variable just for use here, not to conflict with the general debug and quit_debug

var browser_type=(navigator.userAgent); // not used yet. need work on.
if (reload_debug) alert("This is RELOAD_IN_TOP_FRAME.JS\ndocument.URL="+document.URL
	+"\nlocation.protocol="+location.protocol+"\nlocation.host="+location.host+"\nlocation.hostname="+location.hostname
	+ "\nlocation.pathname="+location.pathname+"\nlocation.search="+location.search+"\nlocation.hash="+location.hash
	+"\nwindow.name="+window.name);

// the test below for window.name!="main" does not work on Safari, as only frames have window.name, and opening and naming a window leaves
// window.name blank. However top==self will usually catch this in the live environment
if ((top == self | window.name != "main") && window.name != 'fullscreen' && window.name != 'fullscreen2'
	&& window.name != 'popwindow') {
	// this document is not framed in the anvil site, so reload the default page with this page as the main content.
	// netscape navigator requires full protocol etc.
	var path=location.pathname.split("/"); // path.length is 1 based, but array path[] is 0 based
	var new_loc=location.protocol+"//"+location.host;
	var slash='/';
	for (var i=1; i<path.length; i++) {
		if ((location.host=='localhost' && (path[i-1]=='anvil chorus' || path[i-1]=='anvil%20chorus')) || (location.host!='localhost' && i==1)) {
			new_loc+="/default.php?mainpage=";
			slash='';
		}
		if (path[i].length!=0) {new_loc+=slash+path[i];}
		slash='/';
		if (reload_debug) {alert('i='+i+'\npath['+i+']='+path[i]+'\nnew_loc='+new_loc);}
	}
	if (location.search != "") {
		if (reload_debug) {alert("location.search.substr(1)="+location.search.substr(1));}
		new_loc+="&"+location.search.substr(1);
	}
	new_loc+="&screenwidth="+screen.width
	if (location.hash != "") {new_loc+="&hash="+location.hash.substr(1)};
	if (reload_debug) alert("reload_in_top_frame:\ntop==self is "+(top==self)+"\nwindow.name="+window.name+
		"\npage is not framed\nredirect="+redirect+"\nnew location will be:\n"+new_loc);
	if (redirect==1) {
		if (reload_debug) alert('about to execute top.location.replace on\n'+new_loc);
		top.location.replace(new_loc);
	}
	else if (redirect==2) {document.write('<a href="'+new_loc+'">'+new_loc+'</a>');}
	else {alert("redirect="+redirect+"\nPage would be reloaded but redirect is overriden");}
}
else {
	if (reload_debug) alert("Reload_in_top_frame:\ntop == self is "+(top == self)+"\nwindow.name="+window.name+
		"\nno reload required");
	// top.main.focus() does not work and causes the focus from latest.html not to work either
	// main.focus() does not affect the latest load, but does not actually work 
	//if (reload_debug) alert('3='+window.frames[0]);
//	if (top.main) {main.focus();} // this sometimes causes 'done but with errors'
}