//used for centering popup window
var sw = screen.width; var sh = screen.height;
if (sw <= 800) {var ww = 620; var wh = 460;}
if (sw <= 1024 && sw > 800) {var ww = 900; var wh = 650;}
if (sw > 1024) {var ww = 1000; var wh = 720;}
var st = (sh - wh) / 2; var sl = (sw - ww) / 2;

function gid(oid) { return document.getElementById(oid) }

function GetWindow(url) {var winHandle = window.open(url,'SimpleWindow','scrollbars=1,RESIZABLE=1,width='+ww+',height='+wh+',top='+st+',left='+sl);}
function OpenSimple(url) {window.open(url,'Sublinks','border=0,scrollbars=1,resizable=1,status=1,toolbar=0,width='+ww/2+',height='+wh+',left=10,top='+st);}
function PreviewClick(url) {window.open(url,'Preview','scrollbars=1,RESIZABLE=1');}

function hideID(ObjID) {
    if (document.getElementById(ObjID)) { document.getElementById(ObjID).style.visibility = "hidden"; document.getElementById(ObjID).style.display = "none"; }
}
function showID(ObjID, xOffset, yOffset) {
    if (document.getElementById(ObjID)) {
        var Obj = document.getElementById(ObjID);
        //xOffset = parseInt(xOffset); yOffset = parseInt(yOffset);
        if (xOffset == "center") {
            Obj.style.left = (parseInt(document.body.clientWidth) - parseInt(Obj.style.width)) / 2
            if (parseInt(Obj.style.left) < 0) Obj.style.left = "0px";
        } else { if (!isNaN(xOffset)) { Obj.style.left = (parseInt(Obj.style.left) + parseInt(xOffset)) + "px"; } }

        if (yOffset == "center") {
            Obj.style.top = (parseInt(document.body.clientHeight) - parseInt(Obj.style.height)) / 2
            if (parseInt(Obj.style.top) < 0) Obj.style.top = "0px";
        } else { if (!isNaN(yOffset)) { Obj.style.top = (parseInt(Obj.style.top) + parseInt(yOffset)) + "px"; } }
        //alert(document.body.clientWidth + "|" + document.body.clientHeight + "||L" + Obj.style.left + "|" + Obj.style.top);
        Obj.style.visibility = "visible"; Obj.style.display = "";
    }
}

function checkmeStr(str){
	resultval = confirm(str);
	if (resultval == true) {return true;} else {return false;}
}

//MSIE advanced editor
function ToggleEditor() {
	if (ConfirmAction('you are about to switch to the format of HTML-based text editing') == true) {
		if (gid('AEToolbar').style.visibility == 'hidden') {
		    var chunkStr = gid('form1').txtcontent.value;
			var linesStr = chunkStr.split('\n');
			gid('form1').txtcontent.value = linesStr.join('\n<BR>');

			gid('AEToolbar').style.visibility = 'visible';
			gid('AEToolbar').style.display = '';
			gid('form1').txtcontent.rows = 1; gid('form1').txtcontent.readOnly = true; gid('form1').txtcontent.title = 'This box is now in read-only mode';
			gid('AEHref').innerHTML = 'Simple Editor';
			gid('AEBox').innerHTML = gid('form1').txtcontent.value;
		} else {
			gid('AEToolbar').style.visibility = 'hidden';
			gid('AEToolbar').style.display = 'none';
			gid('form1').txtcontent.rows = 20; gid('form1').txtcontent.readOnly = false; gid('form1').txtcontent.title = 'This box is now editable';
			gid('AEHref').innerHTML = 'Advanced Editor';
			gid('form1').txtcontent.value = gid('AEBox').innerHTML;
		}
	}
}
var toggle = 1;
function spanToggle(spanID) {
	if (toggle == 0) toggle = 1; else toggle = 0;
	if (document.all) {
		if (toggle == 0) {
		    //eval(spanID + '.style.visibility = "visible";'); eval(spanID + '.style.top = AEToolbar.style.top;');
            gid(spanID).style.visibility = "visible"; gid.style.top = AEToolbar.style.top;
			if (spanID == 'spanEI') {gid(spanID).innerHTML = IconValues;}
		} else {gid(spanID).style.visibility = "hidden";}
	} else {
	//alert(toggle);
		if (toggle == 0) {
			gid(spanID).style.visibility = "visible"; gid(spanID).style.display = "";
			if (spanID == 'spanEI') {
			    gid(spanID).innerHTML = IconValues;
			    alert("CODE\t...MEANING\n}:{\t...mad\n:-[\t...angry\n:-]\t...grinning\n:@)\t...kissing\n:))\t...laughing\n:((\t...sad\n:{}\t...scared\n:-)\t...smiling\n:-o\t...surprised\n;-]\t...winking\n[+]\t...in need of help\n[K]\t...being cool\n[?]\t...asking a question\n[x]\t...wishing to stop\n[@]\t...in love");
            }
		} else { gid(spanID).style.visibility = "hidden"; gid(spanID).style.display = "none";}
	}
}

function UpdateContent() {gid('form1').txtcontent.value = gid('AEBox').innerHTML;}

var sInitColor = null;
function EditText(command, option) {
	/*if (command == 'ForeColor') {
		if (sInitColor == null) 
			option = dlgHelper.ChooseColorDlg();
		else
			option = dlgHelper.ChooseColorDlg(sInitColor);
		//change the return value from a decimal value to a hex value and make sure the value has 6
		//digits to represent the RRGGBB schema required by the color table
		option = option.toString(16);
		if (option.length < 6) {
			var sTempString = "000000".substring(0,6-sColor.length);
			option = sTempString.concat(option);
		}
		sInitColor = option;
	}*/
	if (option == 'CustomFont') {
		var theResponse = window.prompt("Set Custom Font Name \n However, your font type may not be installed on the other machines.", "Arial");
		if (theResponse) option = theResponse;
		else return false;
	}
	if (option == 'CustomColor') {
		var theResponse = window.prompt("Set Custom Color Value \n Enter a value in hexadecimal format.", "#FFFFFF");
		if (theResponse) option = theResponse;
		else return false;
	}
	gid('AEBox').focus();
	document.execCommand(command, true, option);
	gid('AEBox').focus();
	gid('form1').txtcontent.value = gid('AEBox').innerHTML;
	if (command == 'ForeColor' || command == 'BackColor') { ShowColors(command); }
}

function UpdateCharCount(Obj) {
    if (Obj) {
        if (document.getElementById("spLabelCharCounter")) {
            var LenLimit = document.getElementById("spLabelCharCounter").getAttribute("LenLimit");
            if (!isNaN(LenLimit)) {
                if (document.getElementById("AEToolbar") && document.getElementById("AEToolbar").style.visibility == "visible") {
                    document.getElementById("spLabelCharCounter").innerHTML = "Characters left: " + (parseInt(LenLimit) - document.getElementById('AEBox').contentWindow.document.body.innerHTML.length)
                } else { if (Obj.value) { document.getElementById("spLabelCharCounter").innerHTML = "Characters left: " + (parseInt(LenLimit) - Obj.value.length); } }
                if (Obj.value.length > LenLimit) {
                    Obj.value = Obj.value.substring(0, LenLimit);alert("The content is too long, it has been truncated to the permissible length.");
                }
            }
        }
    }
}

//Load and initialize colorbox once
function LoadColors (mode) {
	var CStep, clGray;
	CStep = 50; AEColors.innerHTML = '<FONT COLOR=#000000>' + mode + '</FONT> ';
//gray scale
	for (var i=0; i<7; i++) { clGray = ToHex(i*CStep) + ToHex(i*CStep) + ToHex(i*CStep); AddAColor(clGray, mode); }
//red
	for (var i=2; i<6; i++) { clGray = ToHex(i*CStep) + ToHex(0) + ToHex(0); AddAColor(clGray, mode); }
	for (var i=2; i<5; i++) { clGray = ToHex(255) + ToHex(i*CStep) + ToHex(i*CStep); AddAColor(clGray, mode); }
//green
	for (var i=2; i<6; i++) { clGray = ToHex(0) + ToHex(i*CStep) + ToHex(0); AddAColor(clGray, mode); }
	for (var i=2; i<5; i++) { clGray = ToHex(i*CStep) + ToHex(255) + ToHex(i*CStep); AddAColor(clGray, mode); }
//blue
	for (var i=2; i<6; i++) { clGray = ToHex(0) + ToHex(0) + ToHex(i*CStep); AddAColor(clGray, mode); }
	for (var i=2; i<5; i++) { clGray = ToHex(i*CStep) + ToHex(i*CStep) + ToHex(255); AddAColor(clGray, mode); }
//to yellow
	for (var i=1; i<6; i++) { clGray = ToHex(255) + ToHex(i*CStep) + ToHex(0); AddAColor(clGray, mode); }
//to green
	for (var i=3; i<5; i++) { clGray = ToHex(i*CStep) + ToHex(255) + ToHex(0); AddAColor(clGray, mode); }
//to cyan
	for (var i=3; i<6; i++) { clGray = ToHex(0) + ToHex(255) + ToHex(i*CStep); AddAColor(clGray, mode); }
//to pink
	for (var i=2; i<6; i++) { clGray = ToHex(i*CStep) + ToHex(0) + ToHex(255); AddAColor(clGray, mode); }
//to red
	for (var i=3; i<6; i++) { clGray = ToHex(255) + ToHex(0) + ToHex(i*CStep); AddAColor(clGray, mode);}

	AEColors.innerHTML += " <A onclick=EditText('" + mode + "'," + "'CustomColor') STYLE='CURSOR:hand;'>Custom Color...</A> ";
}
function AddAColor (ColorV, mode) {
	AEColors.innerHTML += "<A HREF='' onclick=EditText('" + mode + "'," + "'#" + ColorV + "')><SPAN STYLE='Background-Color:#" + ColorV + "; CURSOR:hand; Color:#" + ColorV + "'>__</SPAN></A> ";
}
//Toggle color box
function ShowColors(mode) {
	if (AEColors.style.visibility == 'hidden') {
		AEColors.style.visibility = 'visible';
		AEColors.style.display = '';
	}
	LoadColors(mode);
}

function createHexArray(n) {
	this.length = n;
	for (var i = 1; i <= n; i++) this[i] = i - 1;
	this[11] = "A"; this[12] = "B"; this[13] = "C"; this[14] = "D"; this[15] = "E"; this[16] = "F";
	return this;
}
hx = new createHexArray(16);
function ToHex(x) {
	if (x < 17) x = 16;
	var high = x / 16; var s = high+"";
	s = s.substring(0, 2);
	high = parseInt(s, 10);
	var left = hx[high + 1]; var low = x - high * 16;
	if (low < 1) low = 1;
	s = low + ""; s = s.substring(0, 2);
	low = parseInt(s, 10);
	var right = hx[low + 1]; var string = left + "" + right;
	return string;
}

function switchtable(tb) {
	if ((eval(tb + '.style.visibility == ' + "'visible'")) || (eval(tb + '.style.visibility == ' + "''"))) {
		eval(tb + '.style.visibility =' + "'hidden';");
		eval(tb + '.style.display =' + "'none';");
	} else {
		eval(tb + '.style.visibility =' + "'visible';");
		eval(tb + '.style.display =' + "'';");}
}

function ConfirmAction(message){
	resultval = confirm('This process can not be undone, ' + message + '.  Are you sure to continue?');
	if (resultval == true) {return true;}  else {return false;}
}

//===========Text selection and insertion
function EnableSelection() {
	if (document.getSelection) {
		if (document.getSelection() != '') document.form1.bGetSelection.value = '+ Quote Selection';
		else document.form1.bGetSelection.value = 'Quote Not Selected';
	} else if (document.selection && document.selection.createRange) {
		if (document.selection.createRange().text != '') document.form1.bGetSelection.value = '+ Quote Selection';
		else document.form1.bGetSelection.value = 'Quote Not Selected';
	} else {
	//Not supported by browser
	}
}

function QuoteSelection () {
	if (document.getSelection) {
		if (document.getSelection() == '') alert('Please select/highlight some text from this same page.');
		else document.form1.txtcontent.value = document.form1.txtcontent.value + '\n[Quote]' + document.getSelection() + '[/Quote]\n';
	} else if (document.selection && document.selection.createRange) {
		if (document.selection.createRange().text == '') alert('Please select/highlight some text from this same page.');
		else document.form1.txtcontent.value = document.form1.txtcontent.value + '\n[Quote]' + document.selection.createRange().text + '[/Quote]\n';
	} else {
	//Not supported by browser
	}
}
if (window.Event) document.captureEvents(Event.MOUSEUP);
//document.onmouseup = EnableSelection;

function ModifyDropTextData() {
	event.dataTransfer.setData('Text', '[QUOTE]' + event.dataTransfer.getData('Text') + '[/QUOTE]');
}
function ModifyDropHTMLData() {
	event.dataTransfer.setData('Text', '[QUOTE]' + event.dataTransfer.getData('Text') + '[/QUOTE]');
}

//=============Slide Show
function WriteSlideShow(url, caption) {
	if (document.all) {
		if (document.SSForm.chkAppend.checked == false || url == '') {
			i = window.frames["SSFrame"];
			i.document.open();
			//SSFrame.location.reload();
		}
	} else {
		if (document.SSForm.chkAppend.checked == false || url == '') {
			//i = SSFrame; i.reload();
			SSFrame.location.reload();
		}
	}

	if (url != '') {
		SSFrame.document.write('<P>' + caption + '<P><IMG SRC=\"' +url + '\"><HR>');
		if (document.SSForm.chkNewWindow.checked == true) {GetWindow(url, caption);}
	} else {
		SSFrame.document.write('Click on a number to view...');
	}
}

//=========== AJAX
var StatusBarFr, StatusBarFill;
var StatusBarFr = document.createElement("span");
StatusBarFr.style.backgroundcolor = "#EEEEEE"; StatusBarFr.style.border = "2px"; StatusBarFr.style.borderStyle = "solid";
StatusBarFr.style.width = "120px"; StatusBarFr.style.height = "20px";
StatusBarFr.style.position = "relative"; StatusBarFr.style.left = 1; StatusBarFr.style.top = 1;

var StatusBarFill = document.createElement("span");
StatusBarFill.style.backgroundcolor = "#111111"; StatusBarFill.style.border = "1px"; StatusBarFill.style.borderStyle = "solid";
StatusBarFill.style.width = "100px"; StatusBarFill.style.height = "20px";
StatusBarFill.style.position = "relative"; StatusBarFill.style.left = 0; StatusBarFill.style.top = 0; StatusBarFill.innerHTML = 'Loading...';

//StatusBarFr.appendChild(StatusBarFill);

function makeRequest(divn, url, CurPage, PageNavigating) {
	if (eval('inTopicn' + divn + '.getElementsByTagName("table").length > 0') && PageNavigating == 0) {
		eval('inTopicn' + divn + '.innerHTML = "";'); eval("inTopicn" + divn + ".style.visibility = 'hidden';"); eval("inTopicn" + divn + ".style.display = 'none';"); return false;
	}

	var httpRq = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		httpRq = new XMLHttpRequest();
		if (httpRq.overrideMimeType) {httpRq.overrideMimeType('text/xml');}
	} else {
		if (window.ActiveXObject) { // IE
			try {httpRq = new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e) {
				try {httpRq = new ActiveXObject("Microsoft.XMLHTTP");}
				catch (e) {}
		}
	}
}

	if (!httpRq) {alert('Cannot create an XMLHTTP instance'); return false;}

	httpRq.onreadystatechange = function() {ReturnData(divn, httpRq);};
	httpRq.open("GET", url, true);
	httpRq.send(null);
}

function ReturnData(divn, httpRq) {
	var tempChunk = ''; var nPC;
	eval("inTopicn" + divn + ".style.visibility = 'visible';"); eval("inTopicn" + divn + ".style.display = '';");
	if (httpRq.readyState > 0) { nPC = ((httpRq.readyState / 4)*100) + "%";}
	eval('inTopicn' + divn + '.innerHTML = "<img src=\'./icons/ajaxloading2.gif\'><span id=StatusFr style=\'font-size:14px; background-color:#fefEfe; border-width:2px; border-style:solid; border-color:#bbbbbb; position:relative; left:1; top:1; width:126px; height:24px;\'>&nbsp;Loading...' + nPC + '</span>";');
	eval('inTopicn' + divn + '.innerHTML += "<span id=StatusFill style=\'font-size:14px; background-color:#aafEaa; border-width:1px; border-style:solid; border-color:#bbbbbb; position:relative; left:-122; top:-2; width:' + (httpRq.readyState * 30) + 'px; height:14px; filter:alpha(opacity=50);-moz-opacity:.5;opacity:.25;\'>&nbsp;</span>";');
	//eval('inTopicn' + divn + '.appendChild(StatusBarFr);');

	if (httpRq.readyState == 4) {
		if (httpRq.status == 200) {
			tempChunk = httpRq.responseText;
			eval('inTopicn' + divn + '.innerHTML = tempChunk;');
		} else { eval("inTopicn" + divn + ".innerHTML = '';"); alert('There was a problem with the request.');}
	}
}
