function selectDate ( calender ) {
  url = "/sfd/select_calender.php?calender=" + calender;
  var todayWindow = window.open(url,"DateSelection","width=200,height=250,resizable=yes,scrollbars=yes");
}
function selectDate2 ( calender ) {
  url = "/sfd/select_calender3.php?calender=" + calender;
  var todayWindow = window.open(url,"DateSelection","width=200,height=250,resizable=yes,scrollbars=yes");
}
function selectImage() {
  url = "/sfd/imagemanager.php";
  var imageWindow = window.open(url,"ImageSelection","width=850,height=600,resizable=yes,scrollbars=yes");
}
function selectImageAdmin(uid) {
  url = "/admin/imagemanager.php?uid=" + uid;
  var imageWindow = window.open(url,"ImageSelection","width=850,height=600,resizable=yes,scrollbars=yes,location=yes");
}
function selectObj() {
  url = "/admin/objmanager.php";
  var imageWindow = window.open(url,"ObjSelection","width=850,height=600,resizable=yes,scrollbars=yes,location=yes");
}
function selectObjAdmin(uid) {
  url = "/admin/objmanager.php?uid=" + uid;
  var imageWindow = window.open(url,"ObjSelection","width=850,height=600,resizable=yes,scrollbars=yes,location=yes");
}
function selectColor ( color ) {
  url = "../config/colortable.php?color=" + color;
  var colorWindow = window.open(url,"ColorSelection","width=550,height=250,resizable=yes,scrollbars=yes");
}
function sendImage( filename ) {
  window.opener.document.edit.body2.value=filename;
  window.close();
}
function sendImage2( username, udir, filename, size, align, altname ) {
	
	if( align == 1) {
		var align_src = "align=right ";
	} else if ( align == 2){
		var align_src = "align=left ";
	} else {
		var align_src = "";
	}
	
	//var result = "<a href=/" + username + "/" + udir + "/" + filename + " target=_blank><img src=/" + username + "/" + udir + "/" + filename + " width=" + size  + align_src + " border=0 alt=" + altname + "></a>";
	if( username == "admin" ){
		var result = "[img " + align_src + "width=" + size + " alt=" + altname + "]" + "/img/" + filename + "[/img]";
	} else {
		var result = "[img " + align_src + "width=" + size + " alt=" + altname + "]" + "/" + username + "/" + udir + "/" + filename + "[/img]";
	}
  
  var domobj = window.opener.document.edit.body;
  SFDInsertText(domobj, result);
  window.close();
}
function sendObj2( username, udir, filename, altname ) {
	if( username == "admin"){
		var result = "[obj alt=" + altname  + "]" +  "/img/" + filename + "[/obj]";
	} else {
		var result = "[obj alt=" + altname  + "]" + "/" + username + "/" + udir + "/" + filename + "[/obj]";
	}
  
  var domobj = window.opener.document.edit.body;
  SFDInsertText(domobj, result);
  window.close();
}
function SFDGetElementById(id){
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

function SFDSetElementProp(name, prop, val) {
	var elt=SFDGetElementById(name);
	if (elt) elt[prop]=val;
}

function SFDSetElementStyle(name, prop, val) {
	var elt=SFDGetElementById(name);
	if (elt && elt.style) elt.style[prop]=val;
}

function SFDGetFormElement(fname, ctlname) {
	var frm=document.forms[fname];
	return frm?frm.elements[ctlname]:null;
}

function justReturn() {
	return;
}

function openWithSelfMain(url,name,width,height) {
	var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";

	new_window = window.open(url, name, options);
	window.self.name = "main";
	new_window.focus();
}

function setElementColor(id, color){
	SFDGetElementById(id).style.color = "#" + color;
}

function setElementFont(id, font){
	SFDGetElementById(id).style.fontFamily = font;
}

function setElementSize(id, size){
	SFDGetElementById(id).style.fontSize = size;
}

function changeDisplay(id){
	var elestyle = SFDGetElementById(id).style;
	if (elestyle.display == "") {
		elestyle.display = "none";
	} else {
		elestyle.display = "block";
	}
}

function setVisible(id){
	SFDGetElementById(id).style.visibility = "visible";
}

function setHidden(id){
	SFDGetElementById(id).style.visibility = "hidden";
}

function makeBold(id){
	var eleStyle = SFDGetElementById(id).style;
	if (eleStyle.fontWeight != "bold" && eleStyle.fontWeight != "700") {
		eleStyle.fontWeight = "bold";
	} else {
		eleStyle.fontWeight = "normal";
	}
}

function makeItalic(id){
	var eleStyle = SFDGetElementById(id).style;
	if (eleStyle.fontStyle != "italic") {
		eleStyle.fontStyle = "italic";
	} else {
		eleStyle.fontStyle = "normal";
	}
}

function makeUnderline(id){
	var eleStyle = SFDGetElementById(id).style;
	if (eleStyle.textDecoration != "underline") {
		eleStyle.textDecoration = "underline";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function makeLineThrough(id){
	var eleStyle = SFDGetElementById(id).style;
	if (eleStyle.textDecoration != "line-through") {
		eleStyle.textDecoration = "line-through";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function appendSelectOption(selectMenuId, optionName, optionValue){
	var selectMenu = SFDGetElementById(selectMenuId);
	var newoption = new Option(optionName, optionValue);
	selectMenu.options[selectMenu.length] = newoption;
	selectMenu.options[selectMenu.length].selected = true;
}

function disableElement(target){
	var targetDom = SFDGetElementById(target);
	if (targetDom.disabled != true) {
		targetDom.disabled = true;
	} else {
		targetDom.disabled = false;
	}
}
function SFDCheckAll(formname, switchid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = SFDGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.name != switch_cbox.name) && (e.type == 'checkbox') ) {
			e.checked = switch_cbox.checked;
		}
	}
}

function SFDCheckGroup(formname, switchid, groupid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = SFDGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.type == 'checkbox') && (e.id == groupid) ) {
			e.checked = switch_cbox.checked;
			e.click(); e.click();  // Click to activate subgroups
									// Twice so we don't reverse effect
		}
	}
}

function SFDCheckAllElements(elementIds, switchId) {
	var switch_cbox = SFDGetElementById(switchId);
	for (var i = 0; i < elementIds.length; i++) {
		var e = SFDGetElementById(elementIds[i]);
		if ((e.name != switch_cbox.name) && (e.type == 'checkbox')) {
			e.checked = switch_cbox.checked;
		}
	}
}

function SFDSavePosition(id)
{
	var textareaDom = SFDGetElementById(id);
	if (textareaDom.createTextRange) {
		textareaDom.caretPos = document.selection.createRange().duplicate();
	}
}

function SFDInsertText(domobj, text)
{
	if (domobj.createTextRange && domobj.caretPos){
  		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? text + ' ' : text;  
	} else if (domobj.getSelection && domobj.caretPos){
		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charat(caretPos.text.length - 1)  
== ' ' ? text + ' ' : text;
	} else {
		domobj.value = domobj.value + text;
  	}
}

function SFDCodeSmilie(id, smilieCode) {
	var revisedMessage;
	var textareaDom = SFDGetElementById(id);
	SFDInsertText(textareaDom, smilieCode);
	textareaDom.focus();
	return;
}

function showImgSelected(imgId, selectId, imgDir, extra, SFDUrl) {
	if (SFDUrl == null) {
		SFDUrl = "./";
	}
	imgDom = SFDGetElementById(imgId);
	selectDom = SFDGetElementById(selectId);
	imgDom.src = SFDUrl + "/"+ imgDir + "/" + selectDom.options[selectDom.selectedIndex].value + extra;
}

function SFDCodeUrl(id, enterUrlPhrase, enterWebsitePhrase){
	if (enterUrlPhrase == null) {
		enterUrlPhrase = "Enter the URL of the link you want to add:";
	}
	var text = prompt(enterUrlPhrase, "");
	var domobj = SFDGetElementById(id);
	if ( text != null && text != "" ) {
		if (enterWebsitePhrase == null) {
			enterWebsitePhrase = "Enter the web site title:";
		}
		var text2 = prompt(enterWebsitePhrase, "");
		if ( text2 != null ) {
			if ( text2 == "" ) {
				var result = "[url=" + text + "]" + text + "[/url]";
			} else {
				var pos = text2.indexOf(unescape('%00'));
				if(0 < pos){
					text2 = text2.substr(0,pos);
				}
				var result = "[url=" + text + "]" + text2 + "[/url]";
			}
			SFDInsertText(domobj, result);
		}
	}
	domobj.focus();
}

function SFDCodeImg(id, enterImgUrlPhrase, enterImgPosPhrase, imgPosRorLPhrase, errorImgPosPhrase){
	if (enterImgUrlPhrase == null) {
		enterImgUrlPhrase = "Enter the URL of the image you want to add:";
	}
	var text = prompt(enterImgUrlPhrase, "");
	var domobj = SFDGetElementById(id);
	if ( text != null && text != "" ) {
		if (enterImgPosPhrase == null) {
			enterImgPosPhrase = "Now, enter the position of the image.";
		}
		if (imgPosRorLPhrase == null) {
			imgPosRorLPhrase = "'R' or 'r' for right, 'L' or 'l' for left, or leave it blank.";
		}
		if (errorImgPosPhrase == null) {
			errorImgPosPhrase = "ERROR! Enter the position of the image:";
		}
		var text2 = prompt(enterImgPosPhrase + "\n" + imgPosRorLPhrase, "");
		while ( ( text2 != "" ) && ( text2 != "r" ) && ( text2 != "R" ) && ( text2 != "l" ) && ( text2 != "L" ) && ( text2 != null ) ) {
			text2 = prompt(errorImgPosPhrase + "\n" + imgPosRorLPhrase,"");
		}
		if ( text2 == "l" || text2 == "L" ) {
			text2 = " align=left";
		} else if ( text2 == "r" || text2 == "R" ) {
			text2 = " align=right";
		} else {
			text2 = "";
		}
		var result = "[img" + text2 + "]" + text + "[/img]";
		SFDInsertText(domobj, result);
	}
	domobj.focus();
}

function SFDCodeEmail(id, enterEmailPhrase){
	if (enterEmailPhrase == null) {
		enterEmailPhrase = "Enter the email address you want to add:";
	}
	var text = prompt(enterEmailPhrase, "");
	var domobj = SFDGetElementById(id);
	if ( text != null && text != "" ) {
		var result = "[email]" + text + "[/email]";
		SFDInsertText(domobj, result);
	}
	domobj.focus();
}

function SFDCodeQuote(id, enterQuotePhrase){
	if (enterQuotePhrase == null) {
		enterQuotePhrase = "Enter the text that you want to be quoted:";
	}
	var text = prompt(enterQuotePhrase, "");
	var domobj = SFDGetElementById(id);
	if ( text != null && text != "" ) {
		var pos = text.indexOf(unescape('%00'));
		if(0 < pos){
			text = text.substr(0,pos);
		}
		var result = "[quote]" + text + "[/quote]";
		SFDInsertText(domobj, result);
	}
	domobj.focus();
}

function SFDCodeCode(id, enterCodePhrase){
	if (enterCodePhrase == null) {
		enterCodePhrase = "Enter the codes that you want to add.";
	}
	var text = prompt(enterCodePhrase, "");
	var domobj = SFDGetElementById(id);
	if ( text != null && text != "" ) {
		var result = "[code]" + text + "[/code]";
		SFDInsertText(domobj, result);
	}
	domobj.focus();
}

function SFDCodeText(id, hiddentext, enterTextboxPhrase){
	var textareaDom = SFDGetElementById(id);
	var textDom = SFDGetElementById(id + "Addtext");
	var fontDom = SFDGetElementById(id + "Font");
	var colorDom = SFDGetElementById(id + "Color");
	var sizeDom = SFDGetElementById(id + "Size");
	var SFDHiddenTextDomStyle = SFDGetElementById(hiddentext).style;
	var textDomValue = textDom.value;
	var fontDomValue = fontDom.options[fontDom.options.selectedIndex].value;
	var colorDomValue = colorDom.options[colorDom.options.selectedIndex].value;
	var sizeDomValue = sizeDom.options[sizeDom.options.selectedIndex].value;
	if ( textDomValue == "" ) {
		if (enterTextboxPhrase == null) {
			enterTextboxPhrase = "Please input text into the textbox.";
		}
		alert(enterTextboxPhrase);
		textDom.focus();
	} else {
		if ( fontDomValue != "FONT") {
			textDomValue = "[font=" + fontDomValue + "]" + textDomValue + "[/font]";
			fontDom.options[0].selected = true;
		}
		if ( colorDomValue != "COLOR") {
			textDomValue = "[color=" + colorDomValue + "]" + textDomValue + "[/color]";
			colorDom.options[0].selected = true;
		}
		if ( sizeDomValue != "SIZE") {
			textDomValue = "[size=" + sizeDomValue + "]" + textDomValue + "[/size]";
			sizeDom.options[0].selected = true;
		}
		if (SFDHiddenTextDomStyle.fontWeight == "bold" || SFDHiddenTextDomStyle.fontWeight == "700") {
			textDomValue = "[b]" + textDomValue + "[/b]";
			SFDHiddenTextDomStyle.fontWeight = "normal";
		}
		if (SFDHiddenTextDomStyle.fontStyle == "italic") {
			textDomValue = "[i]" + textDomValue + "[/i]";
			SFDHiddenTextDomStyle.fontStyle = "normal";
		}
		if (SFDHiddenTextDomStyle.textDecoration == "underline") {
			textDomValue = "[u]" + textDomValue + "[/u]";
			SFDHiddenTextDomStyle.textDecoration = "none";
		}
		if (SFDHiddenTextDomStyle.textDecoration == "line-through") {
			textDomValue = "[d]" + textDomValue + "[/d]";
			SFDHiddenTextDomStyle.textDecoration = "none";
		}
		SFDInsertText(textareaDom, textDomValue);
		textDom.value = "";
		SFDHiddenTextDomStyle.color = "#000000";
		SFDHiddenTextDomStyle.fontFamily = "";
		SFDHiddenTextDomStyle.fontSize = "12px";
		SFDHiddenTextDomStyle.visibility = "hidden";
		textareaDom.focus();
	}
}

function SFDValidate(subjectId, textareaId, submitId, plzCompletePhrase, msgTooLongPhrase, allowedCharPhrase, currCharPhrase) {
	var maxchars = 65535;
	var subjectDom = SFDGetElementById(subjectId);
	var textareaDom = SFDGetElementById(textareaId);
	var submitDom = SFDGetElementById(submitId);
	if (textareaDom.value == "" || subjectDom.value == "") {
		if (plzCompletePhrase == null) {
			plzCompletePhrase = "Please complete the subject and message fields.";
		}
		alert(plzCompletePhrase);
		return false;
	}
	if (maxchars != 0) {
		if (textareaDom.value.length > maxchars) {
			if (msgTooLongPhrase == null) {
				msgTooLongPhrase = "Your message is too long.";
			}
			if (allowedCharPhrase == null) {
				allowedCharPhrase = "Allowed max chars length: ";
			}
			if (currCharPhrase == null) {
				currCharPhrase = "Current chars length: ";
			}
			alert(msgTooLongPhrase + "\n\n" + allowedCharPhrase + maxchars + "\n" + currCharPhrase + textareaDom.value.length + "");
			textareaDom.focus();
			return false;
		} else {
			submitDom.disabled = true;
			return true;
		}
	} else {
		submitDom.disabled = true;
		return true;
	}
}
