﻿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;}
}

//Advanced editor
function ToggleEditor() {
	if (document.getElementById('AEBox')) document.getElementById('AEBox').contentWindow.document.designMode = "on";
	if (ConfirmAction('You are about to switch to a different method of text editing, continue?') == true) {
		if (document.getElementById('AEToolbar').style.visibility == 'hidden') {
			var chunkStr = document.getElementById('txtcontent').value;
			var linesStr = chunkStr.split('\n');
			document.getElementById('txtcontent').value = linesStr.join('\n<BR>');

			document.getElementById('AEToolbar').style.visibility = 'visible'; document.getElementById('AEToolbar').style.display = '';
			//document.getElementById('txtcontent').rows = 1; document.getElementById('txtcontent').readOnly = true; document.getElementById('txtcontent').title = 'This box is now in read-only mode';
			document.getElementById('txtcontent').style.visibility = 'hidden'; document.getElementById('txtcontent').style.display = 'none';
			document.getElementById('AEHref').innerHTML = 'Simple Editor';
			document.getElementById('AEBox').contentWindow.document.body.innerHTML = document.getElementById('txtcontent').value;
		} else {
			document.getElementById('AEToolbar').style.visibility = 'hidden'; document.getElementById('AEToolbar').style.display = 'none';
			//document.getElementById('txtcontent').rows = 20; document.getElementById('txtcontent').readOnly = false; document.getElementById('txtcontent').title = 'This box is now editable';
			document.getElementById('txtcontent').style.visibility = 'visible'; document.getElementById('txtcontent').style.display = '';
			document.getElementById('AEHref').innerHTML = 'Advanced Editor';
			document.getElementById('txtcontent').value = document.getElementById('AEBox').contentWindow.document.body.innerHTML;
		}
	}
}
var toggle = 1;
function spanToggle(spanID) {
	if (toggle == 0) toggle = 1; else toggle = 0;
	if (document.getElementById(spanID)) {
		if (toggle == 0) {
			document.getElementById(spanID).style.visibility = "visible"; document.getElementById(spanID).style.display = "";
			document.getElementById(spanID).style.top = document.getElementById('AEToolbar').style.top;
			if (spanID == 'spanEI') {document.getElementById(spanID).innerHTML = IconValues;}
			window.location = '#' + spanID;
		} else {document.getElementById(spanID).style.visibility = "hidden"; document.getElementById(spanID).style.display = "none";}
	}
}

var sInitColor = null;
function EditText(command, option) {
	var theResponse = "";
	if (option == 'CustomFont') { theResponse = window.prompt("Setting 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') { theResponse = window.prompt("Setting Custom Color Value \n Enter a value in hexadecimal format.", "#FFFFFF");
		if (theResponse) option = theResponse;
		else return false;
	}
	//where IE and the others differ...mozilla won't take true argument
	if (document.all) { AgentBoolean = true;
	} else {
		AgentBoolean = false;
		if (command == "CreateLink") {
			theResponse = prompt("Setting a Hyperlink \n Please enter the URL you want to connect to...", "http://");
			if (theResponse !== "") {option = theResponse;} else {return false;}
		}
		if (command == "InsertImage") {
			theResponse = prompt("Setting an Image \n Please enter the URL of the image...", "http://");
			alert(theResponse);
			if (theResponse !== "") {option = theResponse;} else {return false;}
		}
	}
	document.getElementById('AEBox').focus();
	document.getElementById('AEBox').contentWindow.document.execCommand(command, AgentBoolean, option);
	document.getElementById('AEBox').focus();

	document.getElementById('txtcontent').value = document.getElementById('AEBox').contentWindow.document.body.innerHTML;
	if (command == 'ForeColor' || command == 'BackColor') {
		ShowColors(command);
	}
}

//Load and initialize colorbox once
function LoadColors (mode) {
	var CStep, clGray;
	CStep = 50; document.getElementById('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);}

	document.getElementById('AEColors').innerHTML += " <A href=\"javascript:void(0)\" onclick=EditText('" + mode + "'," + "'CustomColor') STYLE='CURSOR:pointer;'>Custom Color...</A> ";
}
function AddAColor (ColorV, mode) {
	document.getElementById('AEColors').innerHTML += "<A href=\"javascript:void(0)\" onclick=EditText('" + mode + "'," + "'#" + ColorV + "')><SPAN STYLE='Background-Color:#" + ColorV + "; CURSOR:pointer; Color:#" + ColorV + "'>__</SPAN></A> ";
}
//Toggle color box
function ShowColors(mode) {
	if (document.getElementById('AEColors').style.visibility == 'hidden') {
		document.getElementById('AEColors').style.visibility = 'visible';
		document.getElementById('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;
}

//document.onkeypress = UpdateCharCount(this);
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 { document.getElementById("spLabelCharCounter").innerHTML = "Characters left: " + (parseInt(LenLimit) - Obj.value.length); }
            }
        }
    }
}