/*
Forum Width CP - by Nadim
This code must not be reposted or modified without the permission of its creator.
Leave this header intact at all times.
*/

var Form = GetTag("Form");
var Input = GetTag("Input");
if(window.location.href.match(/action=admin$/i)){
	var List = document.getElementsByTagName("UL");
	for(L=0;L<List.length;L++){
		with(List[L]){
			if(innerHTML.match(/Modify Headers and Footers/)){
				innerHTML += '<a href="index.cgi?action=headersfooters&edit=width">Edit Forum Width</a><br/>';
			}
		}
	}
}
if(window.location.href.indexOf("index.cgi?action=headersfooters&edit=width")!=-1){
	var MainHeader = GetID("header");
	for(T=0;T<Tab.length;T++){
		with(Tab[T]){
			if(width=="100%" && cellSpacing=="1" && cellPadding=="4" && className=="bordercolor" && getElementsByTagName("TD")[0].innerHTML.match(/Board To Mimic/)){
				style.display = "none";
			}
		}	
	}
	function PreviewWidth(){
		for(i=0;i<Tab.length;i++){
			with(Tab[i]){
				if(className == "bordercolor"){
					if(id == "widthpanel" || i == 0){
						width = GetID("forumwidth").value+MainHeader.value.split(/var WidthType = "/)[1].split(/";/)[0];
					}
					else{
					style.display = "none";
					}
				}
			}
		}		
	}
	var PixelRadioStatus = "";
	var PercentRadioStatus = "";
	var PixPerRatio = 7.8260869565217391304347826086957;
	if(WidthType == "%"){
		var PercentRadioStatus = "true";
		var PixelRadioStatus = "false";
		GetID("percentwidth").checked = true;
			GetID("pixelwidth").checked = false;
	}
	else{
		var PixelRadioStatus = "true";
		var PercentRadioStatus = "false";
		GetID("pixelwidth").checked = true;	
		GetID("percentwidth").checked = false;
	}
	function SwitchToPixels(){
		if(PixelRadioStatus != "true"){
			var ValueCheck = "valid";
			if(GetID("forumwidth").value.match(/./)){
				if(GetID("forumwidth").value.match(/\D+/)){
					alert("Please enter a valid numerical figuer as your board width");
					ValueCheck = "invalid";
				}
				else if(GetID("forumwidth").value <= 0){
					alert("Please enter a width greater than 0");
					ValueCheck = "invalid";
				}
			}
			else{
				alert("You must enter a board width");
				ValueCheck = "invalid";
			}
			var OldType = 'var WidthType = "%";';
			var NewType = 'var WidthType = "";';
			MainHeader.value = MainHeader.value.replace(OldType,NewType);
			GetID("widthtype").innerHTML = "px";
			PixelRadioStatus = "true";
			PercentRadioStatus = "false";
			GetID("percentwidth").checked = false;
			if(ValueCheck != "invalid"){
				GetID("forumwidth").value = parseInt(parseInt(GetID("forumwidth").value) * PixPerRatio);
			}
			else{
				GetID("forumwidth").value = "";
			}
		}
	}
	function SwitchToPercent(){
		if(PercentRadioStatus != "true"){
			var ValueCheck = "valid";
			if(GetID("forumwidth").value.match(/./)){
				if(GetID("forumwidth").value.match(/\D+/)){
					alert("Please enter a valid numerical figuer as your board width");
					ValueCheck = "invalid";
				}
				else if(GetID("forumwidth").value <= 0){
					alert("Please enter a width greater than 0");
					ValueCheck = "invalid";
				}
			}
			else{
				alert("You must enter a board width");
				ValueCheck = "invalid";
			}
			var OldType = 'var WidthType = "";';
			var NewType = 'var WidthType = "%";';
			MainHeader.value = MainHeader.value.replace(OldType,NewType);
			GetID("widthtype").innerHTML = "%";
			PercentRadioStatus = "true";
			PixelRadioStatus = "false";
			GetID("pixelwidth").checked = false;
			if(ValueCheck != "invalid"){
				GetID("forumwidth").value = parseInt(parseInt(GetID("forumwidth").value) / PixPerRatio);
			}
			else{
				GetID("forumwidth").value = "";
			}
		}
	}
	function ReplaceWidthValue(){
		if(GetID("forumwidth").value.match(/./)){
			if(GetID("forumwidth").value.match(/\D+/)){
				alert("Please enter a valid numerical figuer as your board width");
				return false;
			}
			else if(GetID("forumwidth").value <= 0){
				alert("Please enter a width greater than 0");
				return false;
			}
			else{
				if(MainHeader.value.indexOf('var WidthType = "%";')!=-1 && GetID("forumwidth").value > 100){
					if(confirm('This width may stretch your forum page\n\nAre you sure you want to set your board width at '+GetID("forumwidth").value+'% ?')) {
					}
					else{
						return false;
					}		
				}
				if(MainHeader.value.indexOf('var WidthType = "%";')==-1 && GetID("forumwidth").value > 790){
					if(confirm('This width may stretch your forum page\n\nAre you sure you want to set your board width at '+GetID("forumwidth").value+' pixels?')) {
					}
					else{
						return false;
					} 		
				}
			}
		}
		else{
			alert("You must enter a board width");
			return false;
		}
		var OldWidth = MainHeader.value.split(/var BoardWidth = "/)[1].split(/";/)[0];
		OldWidth = 'var BoardWidth = "'+OldWidth+'";';
		var NewWidth = GetID("forumwidth").value;
		NewWidth = 'var BoardWidth = "'+NewWidth+'";';
		MainHeader.value = MainHeader.value.replace(OldWidth,NewWidth);
		var CurrentType = '';
		if(MainHeader.value.indexOf('var WidthType = "%";')!=-1){
			CurrentType = "%";
		}
		else{
			CurrentType = "";
		}
		var TextA = document.getElementsByTagName("Textarea");
		for(A=0;A<TextA.length;A++){
			with(TextA[A]){
				if(name.match(/footer-/)){
					if(value.match(/var BoardWidth/) && value.match(/var WidthType/)){
						var OldBoardWidth = value.split(/var BoardWidth = "/)[1].split(/";/)[0];
						OldBoardWidth = 'var BoardWidth = "'+OldBoardWidth+'";';				
						var NewBoardWidth = 'var BoardWidth = "'+GetID("forumwidth").value+'";';
						value = value.replace(OldBoardWidth,NewBoardWidth);
						var OldBoardType = value.split(/var WidthType = "/)[1].split(/";/)[0];
						OldBoardType = 'var WidthType = "'+OldBoardType+'";';
						var NewBoardType = 'var WidthType = "'+CurrentType+'";';
						value = value.replace(OldBoardType,NewBoardType);
					}				
				}
			}
		}
	}
	function ReplaceSubmit(){
		for(I=0;I<Input.length;I++){
			with(Input[I]){		
				if(value.match(/Save Headers and Footers/) && I!=Input.length-1){
					value = "Save Forum Width";
					parentNode.innerHTML += ' <input type="submit" value="Preview Forum Width" onClick="PreviewWidth(); return(false);"/>';
				}		
				if(value.match(/ Save Headers and Footers /) && I==Input.length-1){
					style.display = "none";
				}
			}
		}
	}
	ReplaceSubmit();
	for(F=0;F<Form.length;F++){
		with(Form[F]){
			if(action.indexOf("headersfooters2") && method.match(/^Post$/i)){
				action = "index.cgi?action=headersfooters2&width";
				onsubmit = ReplaceWidthValue;
			}
		}
	}
}
if(window.location.href.indexOf("index.cgi?action=headersfooters2&width")!=-1){
	for(i=0;i<TD.length;i++){
		with(TD[i]){
			if(className=="titlebg" && innerHTML.match(/Edit Headers \/ Footers/)){
				childNodes[0].childNodes[0].innerHTML = "Edit Forum Width";
			}
			if(className=="windowbg" && innerHTML.match(/edited successfully\./)){
				childNodes[1].innerHTML = "The width of your board has been changed successfully.";
			}
		}
	}
}