//main.js controls and functions for Evenas website copyright (c) K.J. Petrie 2001

//Controls
//UnDev();
BackgroundColour='rgb(255,255,255)';
BackgroundImage='url(evenas3.jpg)';
CGIPath="http://cgi.evenas.org/";
CoarseStep=5;
CoarseInterval=10;
Cright="";
CRightYear="2001";
DebugCGI=0;
FineStep=1;
FineInterval=100;
FormAddress="editor@evenas.org";
HeadingColour='rgb(208,208,162)';
MailAddressee="editor";
MailDomain="evenas.org";
PageLogger='/cgi-bin/instabook/evenas/log.cgi?referrer='+((document.referrer)?escape(document.referrer):'');
TextColour='rgb(116,162,127)';
TopBotLinks=[['Home','index.htm'],['Topics','topics.htm'],['Other Sites','links.htm'],['Forum','forum.htm'],['Contact','feedback.htm'],['Postulates','ecclesly/statemnt/postulat.htm']];
URLPath=(location.protocol=='http:')?'/evenas':'/';

//Globals
Action='';
Attrib='';
Blue=255;
CF='';
Enable=true;
Green=255;
NavName=navigator.appName.toLowerCase();
NavVer=navigator.appVersion;
NavNum=parseInt((NavName=='microsoft internet explorer')?NavVer.substring(NavVer.indexOf('MSIE')+5):NavVer);
Nav=(NavName=='netscape')?((NavNum>4)?'moz':'net'):(NavName=='microsoft internet explorer')?'ie':(NavName=='konqueror')?'konq':'other';
NavOld=(Nav=='net')||((NavNum<5)&&(Nav=='ie'));
NewTitle=document.title;
OldBrowserDisplayed=0;
Open_correspondence=Open_critique=Open_ecclesiology=Open_reflect=0;//Open_anything reserved and defined in function OpenUp
Red=255;
ScriptVersion=1.2;


function AdjustColours(path){
	if((NavNum>4)&&(self.document.body)){
		OutNow('self.document.body,\''+path+'\',\'background\');">Background</a></span>',';">Text</a></span>','<span class="button"><a class="nounderline" href="javascript:edthis(','\n<span class="button"><a class="nounderline" href="javascript:edthis(self.document.body,\''+path+'\',\'text\')');
		KeepColours();
	}
}

function CantParse(){
	alert('NON-STANDARD BROWSER\n\nSorry, we cannot understand your browser\'s Document Object Model.\n\nThis page requires a DOM level one capable browser in order to set colours.');
	return false;
}

function CheckForm(num){
	var retval=false;
	with(document.forms[num]){
		for(var i=0;i<length;i++){
			with(elements[i]){
				while(((type.toLowerCase()=="text")||(type.toLowerCase()=="textarea"))&&(SubString(name,name.length-1)!="O")&&(!value ||(value=="null")||(value=="undefined"))){
					value=prompt('Please enter '+name+'.','');
				}
				if((name)&&(name=='validateO')){
					value='';
				}
			}
		}
		retval=true;
	}
	return retval;
}

function ChooseView(url){
	if(confirm("Would you like to see the document under discussion while reading this page?\n\n(Please note the opening of a window to enable you to view documents on third-party websites should not be construed as a claim of any rights or liability whatsoever in the websites or documents displayed. These remain the property of their owners. We are simply assisting you to view information published elsewhere on the web at:\n"+url+"\nin the belief it will be helpful.)")){
		DualView(url);
	}
	else{
//		self.resizeTo(screen.availWidth,screen.availHeight);
	}
}

function Clip(colarray){
	for(var i=0;i<3;i++){
		colarray[i]=(colarray[i]<0)?0:colarray[i];
		colarray[i]=(colarray[i]>255)?255:colarray[i];;
	}
	return colarray;
}

function CloseControl(){
	if(typeof Controls!="undefined"){
		Controls.close();
	}
}

function Cool(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
	if(!col[9]){
		if(!col[10]){
			col[(col[7]+1)%3]-=step;
			col[(col[7]+2)%3]+step;
		}
		else{
			col[col[6]]-=step;
			col[(col[6]+1)%3]+=step;
		}
		col=Clip(col);
		SetColour(box,col[0],col[1],col[2],attrib);
	}
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Action=setTimeout('Run("Cool",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

function Dark(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
	col[0]-=step;
	col[1]-=step;
	col[2]-=step;
	col=Clip(col);
	SetColour(box,col[0],col[1],col[2],attrib);
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Action=setTimeout('Run("Dark",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

function DualView(url){
	self.moveTo(0,0);
	if(screen.availWidth<1000){
		self.resizeTo(screen.availWidth,screen.availHeight*0.6);
	}
	else{
		self.resizeTo(screen.availWidth-680,screen.availHeight);
	}
	SeeDoc=window.open('',"SeeDoc","titlebar,scrollbars");
	if(typeof SeeDoc=="undefined"){
		alert('Sorry, your browser settings do not permit the new window to be opened automatically. Please adjust your settings or open a new window manually and visit '+url+'.');
		self.resizeTo(screen.availWidth,screen.availHeight);
	}
	else{
		if(screen.availWidth<1000){
			SeeDoc.moveTo(0,screen.AvailHeight*0.6);
			SeeDoc.resizeTo(screen.availWidth,screen.availHeight*0.4);
		}
		else{
			SeeDoc.moveTo(screen.availWidth-680,0);
			SeeDoc.resizeTo(680,screen.availHeight);
		}
	}
	SeeDoc.location.href=url;
	onunload=self.onunload=moveon;//IE 6.0 doesn't dereference self for event handlers
	self.focus();
}

function edthis(box,path,attribute){
	InitStyle(box);
	Controls=window.open('','Controls','titlebar,width=350,height=220');
	if(Controls){
		Controls.document.open();
		if((Nav=='ie')&&(NavNum<=4)){//IE4 bug
			Controls.document.writeln('<html>\n<head>\n<title>Adjust Colour</title>\n<link rel="stylesheet" type="text/css" href="'+path+'main.css">\n</head>');
			Controls.alert('Tip: Use the buttons to optimise the colour for comfortable viewing.');
		}
		else{
			Controls.document.writeln('<html>\n<head>\n<title>Adjust Colour</title>\n<link rel="stylesheet" type="text/css" href="'+path+'main.css">\n</head>');
		}
		Controls.document.writeln('<body>\n<p>Adjust the '+attribute+' colour using the buttons below.</p><form name="ConForm" id="ConForm">');
		Controls.document.writeln('<table>\n<tr>\n<td></td><th colspan=2>&#0150;&#0150;&#0150; Coarse &#0150;&#0150;&#0150;</th><th colspan=2>&#0150;&#0150;&#0150;&#0150; Fine &#0150;&#0150;&#0150;&#0150;</th></tr>');
		Controls.document.writeln('<tr>\n<td>Hue:</td><td><input type="button" class="button" name="hot" value="Warmer" onMouseDown="opener.Warm(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="cold" value="Cooler" onMouseDown="opener.Cool(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="warm" value="Warmer" onMouseDown="opener.Warm(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="cool" value="Cooler" onMouseDown="opener.Cool(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td></tr>');
		Controls.document.writeln('<tr>\n<td>Luminance:</td><td><input type="button" class="button" name="dark" value="Darker" onMouseDown="opener.Dark(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="dazzling" value="Lighter" onMouseDown="opener.Light(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="dim" value="Darker" onMouseDown="opener.Dark(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="light" value="Lighter" onMouseDown="opener.Light(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td></tr>');
		Controls.document.writeln('<tr>\n<td>Saturation:</td><td><input type="button" class="button" name="herculean" value="Stronger" onMouseDown="opener.Strong(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="thin" value="Paler" onMouseDown="opener.Pale(Box,\'coarse\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="strong" value="Stronger" onMouseDown="opener.Strong(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td><td><input type="button" class="button" name="pale" value="Paler" onMouseDown="opener.Pale(Box,\'fine\',\''+attribute+'\');" onMouseUp="opener.Stop();"></td></tr>');
		if(attribute=='background'){
			Controls.document.writeln('<tr>\n<td>Image:</td><td><input type="button" class="button" name="imageon" value="On" onClick="opener.ImageOn(Box);"></td><td><input type="button" class="button" name="imageoff" value="Off" onClick="opener.ImageOff(Box);"></td><td>&nbsp;</td><td>&nbsp;</td></tr>');
		}
		Controls.document.writeln('<tr>\n<td colspan=4>&nbsp;</td><td><input type="button" class="button" name="close" value="Finished" onClick="opener.StoreCookie();self.close();"></td>\n</tr></table>\n</form>\n</body>\n</html>');
		Controls.document.close();
		Controls.Box=box;
		Controls.focus();
	}
	else{
		alert('To display the controls your browser needs to be able to open windows automatically. It is currently unable to do so. Please review your browser settings and try again.');
	}
}

function getelsbytag(tagname){
//	return (document.all&&document.all.tags)?document.all.tags(tagname):[];
	return (document.getElementsByTagName)?document.getElementsByTagName(tagname):((document.tags)?document.tags.tagname:((document.all&&document.all.tags)?document.all.tags(tagname):[]));
}

function ImageOff(box){
	box.style.backgroundImage='none';
	self.document.cookie="Image=none;PATH="+URLPath;
}

function ImageOn(box){
	box.style.backgroundImage=BackgroundImage;
	document.cookie="Image="+escape(BackgroundImage)+";PATH="+URLPath;
}

function InitStyle(box){
	if(document.body){
		document.body.getElementsByTagName=(document.body.getElementsByTagName)?document.body.getElementsByTagName:getelsbytag;
	}
	else{
		CantParse();
	}
	self.document.body.style.backgroundImage=(self.document.body.style.backgroundImage)?self.document.body.style.backgroundImage:BackgroundImage;
	box.style.color=(box.style.color)?box.style.color:TextColour;
	box.style.backgroundColor=(box.style.backgroundColor)?box.style.backgroundColor:BackgroundColour;
}

function KeepColours(){
	if((getelsbytag("body"))&&(getelsbytag("body")[0])){
		self.onunload=getelsbytag("body")[0].onunload=CloseControl;
	}
	if(self.document.cookie){//alert(self.self.document.cookie.split(/; /));
//		var parts=self.self.document.cookie.split(/; /);
		var parts=self.document.cookie.split(/; /);
		for(var i=0;i<parts.length;i++){
			parts[i]=parts[i].split(/=|;/);
			parts[i][1]=unescape(parts[i][1]);
		}
		for(var j=0;j<2;j++){
			var firstcolour=ParseColour(parts[j][1])[0];
			var secondcolour=ParseColour(parts[j][1])[1];
			var thirdcolour=ParseColour(parts[j][1])[2];
			var docpart=(j==0)?'text':'background';
			if((firstcolour)&&(secondcolour)&&(thirdcolour)){
				SetColour(self.document.body,firstcolour,secondcolour,thirdcolour,docpart);
				self.document.body.style.backgroundImage=parts[2][1];
			}
			else{
				NoAdjustColours();
			}
		}
	}
	else{
		self.document.cookie='Text='+escape(TextColour)+';PATH='+URLPath;
		self.document.cookie='Background='+escape(BackgroundColour)+';PATH='+URLPath;
		self.document.cookie='Image='+escape(BackgroundImage)+';PATH='+URLPath;
	}
}

/* Original
function KeepColours(){
	if(self.document.cookie){
		var parts=self.self.document.cookie.split(/; /);
		for(var i=0;i<parts.length;i++){
			parts[i]=parts[i].split(/=|;/);
			parts[i][1]=unescape(parts[i][1]);
		}
		SetColour(self.document.body,ParseColour(parts[0][1])[0],ParseColour(parts[0][1])[1],ParseColour(parts[0][1])[2],'text');
		SetColour(self.document.body,ParseColour(parts[1][1])[0],ParseColour(parts[1][1])[1],ParseColour(parts[1][1])[2],'background');
		self.document.body.style.backgroundImage=parts[2][1];
	}
	else{
		self.document.cookie='Text='+escape(TextColour)+';PATH='+URLPath;
		self.document.cookie='Background='+escape(BackgroundColour)+';PATH='+URLPath;
		self.document.cookie='Image='+escape(BackgroundImage)+';PATH='+URLPath;
	}
}
*/

function Light(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
	col[0]+=step;
	col[1]+=step;
	col[2]+=step;//alert(col);
	col=Clip(col);
	SetColour(box,col[0],col[1],col[2],attrib);
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Action=setTimeout('Run("Light",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

function moveon(){//alert('moveon');
	if((typeof SeeDoc!="undefined") && (!SeeDoc.closed)){
		SeeDoc.close();
	}
	self.resizeTo(screen.availWidth,screen.availHeight);
}

function NoAdjustColours(){
	CloseControl();
	if((self.document.body)&&(self.document.body.style)&&(self.document.body.style.button)&&(self.document.body.style.button.visibility)){
		self.document.body.style.button.visibility='hidden';
	}
}

function OpenUp(cell){//opens hidden layers in contents pages
	if((document.cookie.indexOf('textomly=1')==-1)&&(location.search.indexOf('textomly=1')==-1)){
		eval('Open_'+cell+'=(typeof Open_'+cell+'=="undefined")?0:Open_'+cell);
		var statusquo=(eval('Open_'+cell))?-1:0;
		eval('Open_'+cell+'=!'+statusquo);
		if(document.getElementById){
			eval('document.getElementById("'+cell+'").style.display'+((statusquo)?'="none"':'="block"'));
		}
		else{
			if((Nav=='ie')&&(NavNum>3)){
				eval('document.all.'+cell+'.style.display'+((statusquo)?'="none"':'="block"'));
			}
			else{//alert('called here');
				OldBrowser(1);
			}
		}
	}
}

function PageBot(path){
	path=(path)?path:'';
	if(!NavOld){
		OutNow('faderight" src="'+path+'fr74A27F.gif">','</div>','<div class="wholeline"><img class="','<img class="fadeleft" src="'+path+'fl74A27F.gif"><span class="topspace">&nbsp;</span>');
	}
	TBLinks(path);
	DisFoot('../'+path);
//	OutNow(path+PageLogger,'">','<img src="',''); only used on file protocol
	if((Nav=='ie')&&(self!=top)){
		self.document.body.style.width='95%';
	}
}

function PageTop(path){
	path=(path)?path:'';
	TBLinks(path);
	if(!NavOld){
		OutNow('faderight" src="'+path+'fr74A27F.gif">','</div>','<div class="wholeline"><img class="','<img class="fadeleft" src="'+path+'fl74A27F.gif"><span class="topspace">&nbsp;</span>');
	}
}

function Pale(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
	if(!col[9]){
		if(col[8]){
			col[col[7]]+=step;
			col[(col[7]+1)%3]=col[(col[7]+2)%3]-=step;
		}
		else{
			if(col[10]){
				col[col[6]]-=step;
				col[(col[6]+1)%3]=col[(col[6]+2)%3]+=step;
			}
			else{
				col[col[6]]-=step;
				col[col[7]]+=step;
			}
		}
		SetColour(box,col[0],col[1],col[2],attrib);
	}
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Action=setTimeout('Run("Pale",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

function ParseColour(colour){
	var colarray=[];
	if((colour.charAt(0).toLowerCase()=='r')){
		colarray=colour.substring(colour.indexOf('(')+1,colour.lastIndexOf(')')).split(/,/);
		for(var i=0;i<3;i++){
			colarray[i]=parseInt(colarray[i],10);
		}
	}
	else{
		if((colour.length==7)||(colour.length==4)){
			colour=colour.substr(1);
		}
		if(colour.length==3){
			colarray=[parseInt(colour.substr(0,1)+colour.substr(0,1),16),parseInt(colour.substr(1,1)+colour.substr(1,1),16),parseInt(colour.substr(2,1)+colour.substr(2,1),16)];
		}
		else{
			if(colour.length==6){
				colarray=[parseInt(colour.substr(0,2),16),parseInt(colour.substr(2,2),16),parseInt(colour.substr(4,2),16)];
			}
			else{
				colarray=null;//Input not a colour
			}
		}
	}
	if(colarray){
		var size=colarray.slice(0);
		size.sort(SortMethod);
		colarray[3]=size[0];
		colarray[4]=colarray[0]+colarray[1]+colarray[2];
		colarray[5]=size[2];
		colarray[8]=(size[1]==size[2])?true:false;
		colarray[9]=(colarray[8]&&(size[0]==size[2]))?true:false;
		colarray[10]=(size[0]==size[1])?true:false;
		colarray[6]=(colarray[9])?1:SearchPrimary(colarray,colarray[5]);
		colarray[7]=(colarray[9])?0:SearchPrimary(colarray,colarray[3]);
	}
	return colarray;//[r,g,b,min,totlum,max,highprim,lowprim,heq2flag,eq3flag,leq2flag]
}

function RestoreCookie(){//IE 5.5 cookie path changed by closing window which initiated it
	for(var i=0;i<document.cookiestore.length;i++){
		document.cookie=document.cookiestore[i]+';PATH='+URLPath;
	}
}

function Run(func,box,cf,attrib){
	if(Enable){
		eval(func+'(box,cf,attrib)');
	}
}

function SearchPrimary(colarray,val){
	for(var i=0;(colarray[i]!=val)&&(i<3);i++){}
	return i;
}

function SetColour(box,r,g,b,attrib){
	with(box.style){
		if(attrib=='background'){
			backgroundColor='rgb('+r+','+g+','+b+')';
			self.document.cookie='Background='+escape('rgb('+r+','+g+','+b+')')+';PATH='+URLPath;
		}
		else{
			color='rgb('+r+','+g+','+b+')';
			self.document.cookie='Text='+escape('rgb('+r+','+g+','+b+')')+';PATH='+URLPath;
			var tcells=box.getElementsByTagName('TD');
			for(var i=0;i<tcells.length;i++){
				if(tcells.item){
					tcells.item(i).style.color=color;
				}
				else{
					tcells[i].style.color=color;
				}
			}
		}
	}
}

function SortMethod(a,b){
	return a-b;
}

function Stop(){
	clearTimeout(Action);
	Enable=false;
	setTimeout("Enable=true",300);
}

function StoreCookie(){//IE 5.5 window close problem
	document.cookiestore=document.cookie.split(/; /);
	setTimeout('RestoreCookie()',1000);
}

function Strong(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
//[r,g,b,min,totlum,max,highprim,lowprim,heq2flag,eq3flag,leq2flag]
	if(col[9]){
		col[6]=1;
		col[7]=0;
		col[9]=col[8]=false;
		col[10]=true;
	}
	if(col[8]){
		col[col[7]]-=step;
		col[(col[7]+1)%3]=col[(col[7]+2)%3]+=step;
	}
	else{
		if(col[10]){
			col[col[6]]+=step;
			col[(col[6]+1)%3]=col[(col[6]+2)%3]-=step;
		}
		else{
			col[col[6]]+=step;
			col[col[7]]-=step;
		}
	}
	SetColour(box,col[0],col[1],col[2],attrib);
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Action=setTimeout('Run("Strong",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

function TBLinks(path){
	for(var i=0;i<TopBotLinks.length;i++){
		LinkButton(TopBotLinks[i][0],path+TopBotLinks[i][1]);
		var pathname=ReplaceChars(location.pathname,'\\','/');
		if(!path&&(TopBotLinks[i][1]==SubString(pathname,pathname.lastIndexOf('/')+1))){
			var linknum=document.links.length-1;//alert('linknum='+linknum+'\n\n'+TopBotLinks[i][0]+'\n\n'+TopBotLinks[i][1]+'\n\n'+document.links[linknum].href);
			if((linknum>=0)&&document.links[linknum].href&&(document.links[linknum].href.lastIndexOf(TopBotLinks[i][1])!=-1)){
				document.links[linknum].className="thispage";
			}
			else{
				OldBrowser(1);
			}
		}
	}
//	AdjustColours(path); -only used when SSI and CGI not available
	OutNow('di','>','<','v class="endline"');
	OutNow('di','>','</','v');
}

function Warm(box,cf,attrib){
	var col=(attrib=='background')?ParseColour(box.style.backgroundColor):ParseColour(box.style.color);
	var step=(cf=='coarse')?CoarseStep:FineStep;
	if(!col[9]){
		if(!col[10]){
			col[(col[7]+2)%3]-=step;
			col[(col[7]+1)%3]+=step;
		}
		else{
			col[col[6]]-=step;
			col[(col[6]+2)%3]+=step;
		}
		col=Clip(col);
		SetColour(box,col[0],col[1],col[2],attrib);
	}
	Controls.Box=box;
	Attrib=attrib;
	CF=cf;
	Arg=[box,attrib,cf];
	Action=setTimeout('Run("Warm",Controls.Box,CF,Attrib);',(cf=='coarse')?CoarseInterval:FineInterval);
}

