var previousStateEditor = "";
var previousStateText = "";

/*------====== insert link ======------*/
function addLinkHere(id) {
	var e = document.getElementById(id);
	var linktitle = prompt("What is the title of the link?", "");
	var linkurl = prompt("What is the URL of the link? \n ex: http://www.google.com", "");
	var ltarget = prompt("1: Different Window/Tab \n2: Same Window/Tab \n Enter 1 or 2", "");
	var lcode = "";
	if (ltarget == 1) {
		lcode = ' target="_blank"';
	} else {
		lcode = "";
	}
	e.value+='<a href="' + linkurl + lcode + '" title="' + linktitle+'">' + linktitle+'</a>';
}

/*------====== insert image ======------*/
function addImageHere(id) {
	var e = document.getElementById(id);
	var imgtitle = prompt("What is the title of the image?", "");
	var imgurl = prompt("What is the URL of the link? \n ex: http://www.google.com/image.jpg", "");
	var imgborder = prompt("Border? \n enter 0, 1, 2, or 3", "");
	var imgheight = prompt("HEIGHT in pixels \nLeave blank if image is already sized, or enter a number 20-200", "");
	var imgwidth = prompt("WIDTH in pixels \nLeave blank if image is already sized, or enter a number 20-200", "");
	e.value+='<img src="' + imgurl + '" border="' + imgborder + '"  height="' + imgheight +  '" width="' + imgwidth + '" alt="' + imgtitle+'" />';
}

function expandtextarea(which, areaAffected, whichEd) {  // start expandtextarea
var areaAffected = areaAffected;
	if(which==0) { // add rows
		document.getElementById(areaAffected).rows+1;
		document.getElementById(areaAffected).style.height=document.getElementById(areaAffected).offsetHeight+250+"px";
	} else if(which==3) { // onfocus make the box bigger
		document.getElementById(areaAffected).cols=50;
		document.getElementById(areaAffected).style.width=576+"px"; // 70 cols
		document.getElementById(areaAffected).rows=3;
		document.getElementById(areaAffected).style.height=659+"px"; // 1 row

	} else if(which==2) { // onblur return to small size
		document.getElementById(areaAffected).cols=50;
		document.getElementById(areaAffected).style.width=576+"px"; // 70 cols
		document.getElementById(areaAffected).rows=3;
		document.getElementById(areaAffected).style.height=66+"px"; // 1 row
		
	} else { // add columns
		document.getElementById(areaAffected).cols+1;
		document.getElementById(areaAffected).style.width=document.getElementById(areaAffected).offsetWidth+50+"px";
	}
// clear the var
areaaffected = "";
} // end expandtextarea

function editorToggle(num, whichEd) {
	if (num == 1) {
	    // turn on buttons
	    document.getElementById(whichEd).style.display = "block";
	} else {
	    // turn off buttons
	    document.getElementById(whichEd).style.display = "none";
	}
} // close editorToggle

// function to open or close the editor

function editorToggle(num, whichEd) {
	if (num == 1) {
	    // turn on buttons
	    document.getElementById(whichEd).style.display = "block";
	} else {
	    // turn off buttons
	    document.getElementById(whichEd).style.display = "none";
	}
}


//  function to insert text at the insertion point
/**
@param textObj could be a text area or a text field
Only required for IE
*/
function setCaret (textObj) {
if (textObj.createTextRange) {
textObj.caretPos = document.selection.createRange().duplicate();
}
}

/**
@param textObj could be a text area or a text field
@param text field value that needs to be inserted in
*/
function insertAtCaret (textObj, textFeildValue) {
if(document.all){
if (textObj.createTextRange && textObj.caretPos) {
var caretPos = textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?textFeildValue + ' ' : textFeildValue;
}else{
textObj.value = textFeildValue;
}
}else{
if(textObj.setSelectionRange){
var rangeStart = textObj.selectionStart;
var rangeEnd = textObj.selectionEnd;
var tempStr1 = textObj.value.substring(0,rangeStart);
var tempStr2 = textObj.value.substring(rangeEnd);
textObj.value = tempStr1 + textFeildValue + tempStr2;
}else{
alert("This version of Mozilla based browser does not support setSelectionRange");
}
}
}

function showPreview(hidethis,showthis,flag) {
	// info previewmode previewtext
	
	if(flag==1){
		var prevtxt = document.getElementById('info').value;
		previousStateEditor = document.getElementById('Ed1').style.display;
		document.getElementById('Ed1').style.display = "none";
		document.getElementById('editorlinks').style.display = "none";
		document.getElementById('previewmode').innerHTML = 	prevtxt;
	}
	if(flag==0){
		document.getElementById('editorlinks').style.display = "block";
	}
	document.getElementById('Ed1').style.display = previousStateEditor;
	document.getElementById(hidethis).style.display = "none";
	document.getElementById(showthis).style.display = "block";
	return false;
}

function switchDiv(first, second) {
	document.getElementById(first).style.display = "none";
	document.getElementById(second).style.display = "block";
}

// ajax page function
	//Create a boolean variable to check for a valid Internet Explorer instance.
	var xmlhttp = false;
	
	//Check if we are using IE.
	try {
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		// alert ("You are using Microsoft Internet Explorer.");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			// alert ("You are using Microsoft Internet Explorder");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		// alert ("You are not using Microsoft Internet Explorer");
	}
	
	function makerequest(serverPage, objID) {
		
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}