var ie = (document.all) ? true:false;
var nn4 = (document.layers) ? true:false;
var nn6 = (document.getElementById && !document.all) ? true:false;
var f = 0;
function alignTop(el,lay) {
	if (nn4 && !document.layers[lay]) {
		return;
	}
	if (ie && !document.all(lay)) {
		return;
	}
	if (nn6 && !document.getElementById(lay)) {
		return;
	}
	var vContentLayer;
	var vEl;
	if (nn4) {
		vContentLayer = document.layers[lay];
		vEl=document.images[el];
		xPos = vEl.left;
		yPos = vEl.top;
		tempEl = vEl.offsetParent;
		while (tempEl != null) {
        	xPos += tempEl.left;
			yPos += tempEl.top;
        	tempEl = tempEl.parent.document.offsetLeft;
    	}
	}
	if (ie) {
		vContentLayer = document.all(lay);
		vEl=document.getElementById(el);
		xPos = vEl.offsetLeft;
		yPos = vEl.offsetTop;
		tempEl = vEl.offsetParent;
		while (tempEl != null) {
        	xPos += tempEl.offsetLeft;
			yPos += tempEl.offsetTop;
        	tempEl = tempEl.offsetParent;
    	}
	} else if (nn6) {
		vContentLayer = document.getElementById(lay);
		vEl=document.getElementById(el);
		xPos = vEl.offsetLeft;
		yPos = vEl.offsetTop;
		tempEl = vEl.offsetParent;
		while (tempEl != null) {
        	xPos += tempEl.offsetLeft;
			yPos += tempEl.offsetTop;
        	tempEl = tempEl.offsetParent;
    	}
	}
	layswap(lay,'1', eval(xPos), eval(yPos+22));
}
function layswap(id,trigger,xPos,yPos) {
    if (document.layers && document.layers[id] != null) {
		//var x=parseInt(xPos)-0;
		//var y=parseInt(yPos)-0;
		//var x=eval(xPos);
		//var y=eval(yPos);
		alert(xPos);
		alert(yPos);
       	document.layers[id].left = xPos;
        document.layers[id].top = yPos;
    }
    else if (ie) {
        document.all[id].style.posLeft = xPos;
        document.all[id].style.posTop = yPos;
    }
	else if (nn6) {
		document.getElementById(id).style.left = xPos;
		document.getElementById(id).style.top = yPos;
	}

	if (trigger=="1"){
		if (nn4) {
			document.layers[''+id+''].visibility = "show";
		} else if (ie) {
			document.all[''+id+''].style.visibility = "visible";
		} else if (nn6) {
			document.getElementById(''+id+'').style.visibility = "visible";
		}
	}
	else if (trigger=="0"){
		if (nn4) {
			document.layers[''+id+''].visibility = "hide";
		} else if (ie) {
			document.all[''+id+''].style.visibility = "hidden";
		} else if (nn6) {
			document.getElementById(''+id+'').style.visibility = "hidden";
		}
	}
}
function jump(target) {
	window.location.href=target;
}
function initToolBar(ed) {
			var eb = document.getElementById('editbar');
			if (ed!=null) {
				eb._editor = window.frames['myEditor'];
			}
		}
		function doFormat(what) {
			var eb = document.getElementById('editbar');
			if(what == "FontName"){
				if(arguments[1] != 1){
					eb._editor.execCommand(what, arguments[1]);
					document.getElementById('font').selectedIndex = 0;
				} 
			} else if(what == "FontSize"){
    			if(arguments[1] != 1){
     			 eb._editor.execCommand(what, arguments[1]);
      			document.getElementById('size').selectedIndex = 0;
    		} 
		} else {
	   		eb._editor.execCommand(what, arguments[1]);
		}
	}

function swapMode() {
	var eb = document.getElementById('editbar')._editor;
  	eb.swapModes();
}
function create() {
    var eb = document.getElementById('editbar');
    eb._editor.newDocument();
}
function newFile(){
	create();
}

function makeUrl(){
	sUrl = document.getElementById('what').value + document.getElementById('url').value;
	if(document.getElementById('what').value=="#t") {
		doFormat('CreateBookMark',document.getElementById('url').value);
	} else {
		doFormat('CreateLink',sUrl);
	}
}

function getImage() {
	doFormmat('InsertImage');
}

function copyValue() {
	var theHtml = "" + document.frames("myEditor").document.frames("textEdit").document.body.innerHTML + "";
	document.getElementById('EditorValue').value = theHtml;
}

function SwapView_OnClick(){
  if(document.getElementById('btnSwapView').value == "View Html"){
		var sMes = "View Wysiwyg";
    	var sStatusBarMes = "Current View Html";
	} else {
		var sMes = "View Html"
    	var sStatusBarMes = "Current View Wysiwyg";
  	}
	document.getElementById('btnSwapView').value = sMes;
  	window.status  = sStatusBarMes;
	swapMode();
}

function Help_OnClick(){
  window.open("editor_images/help_document.htm","wHelp", "toolbar=0, scrollbars=yes, width=640, height=480");
}

function OnFormSubmit(){
  if(confirm("Your content is being submitted for creation -- \nAre you sure you have finished editing?")){
    copyValue();
    //document.fHtmlEditor.submit();
  }
}
function confirmDelete() {
	if(confirm("You are about to delete this content page. This action is permananent and irreversible. \tAre you sure you wish to continue?")) {
		document.forms["delform"].submit();
	}
}
function getSel(theform,flag) {
	if (document.getSelection) txt = document.frames("myEditor").document.frames("textEdit").document.getSelection();
	else if (document.selection) txt = document.frames("myEditor").document.frames("textEdit").document.selection.createRange().text;
	else return;
	//if (txt.outerHTML.nodeName=="SPAN") {
		alert(document.frames("myEditor").document.frames("textEdit").document.selection.createRange().parentElement().tagName);
	//}
	if(flag) {
		if(flag=="br") {
			var newTag1 = '<br>';
			var wholeText = document.frames("myEditor").document.frames("textEdit").document.body.innerHTML;//document.forms(strForm).EditorValue.value;
			newText = wholeText.replace(strText,'<span class="' + styleName + '">' + strText + '</span>');
			document.frames("myEditor").document.frames("textEdit").document.body.innerHTML = newText;
		} else if (flag=="div") {
			var newTag2 = '';
			var wholeText = document.frames("myEditor").document.frames("textEdit").document.body.innerHTML;//document.forms(strForm).EditorValue.value;
			newText = wholeText.replace(strText,'<span class="' + styleName + '">' + strText + '</span>');
			document.frames("myEditor").document.frames("textEdit").document.body.innerHTML = newText;
		}
	} else {
		styleVal = document.forms(theform).elements.styles.options[document.forms(theform).elements.styles.selectedIndex].value;
		applyStyle(theform,txt,styleVal);
	}
}
function setCursorPos() {
  globalCursorPos = getCursorPos(page.EditorValue);
}

//This function returns the index of the cursor location in
//the value of the input text element
//It is important to make sure that the sWeirdString variable contains
//a set of characters that will not be encountered normally in your
//text
function getCursorPos(textElement) {
  //save off the current value to restore it later,
  var sOldText = textElement.value;
	//create a range object and save off it's text
  var objRange = document.selection.createRange();
  var sOldRange = objRange.text;
	//set this string to a small string that will not normally be encountered
  var sWeirdString = '#%~';
	//insert the weirdstring where the cursor is at
  objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

	//save off the new string with the weirdstring in it
  var sNewText = textElement.value;

//set the actual text value back to how it was
  objRange.text = sOldRange;

//look through the new string we saved off and find the location of the weirdstring that was inserted and return that value
  for (i=0; i <= sNewText.length; i++) {
    var sTemp = sNewText.substring(i, i + sWeirdString.length);
    if (sTemp == sWeirdString) {
      var cursorPos = (i - sOldRange.length);
      return cursorPos;
    }
  }
}
//this function inserts the input string into the textarea where the cursor was at
function insertString(stringToInsert) {
  var firstPart = document.frames("myEditor").document.frames("textEdit").document.body.innerHTML.substring(0, globalCursorPos);
  var secondPart = document.frames("myEditor").document.frames("textEdit").document.body.innerHTML.substring(globalCursorPos,page.EditorValue.value.length);
  document.frames("myEditor").document.frames("textEdit").document.body.innerHTML = firstPart + stringToInsert + secondPart;
}
function applyStyle(strForm,strText,styleName) {
	document.frames("myEditor").document.frames("textEdit").document.selection.createRange().parentElement().className=styleName;
	//var newTag = '<span class="' + styleName + '">' + strText + '</span>';
	//var wholeText = document.frames("myEditor").document.frames("textEdit").document.body.innerHTML;//document.forms(strForm).EditorValue.value;
	//newText = wholeText.replace(strText,'<span class="' + styleName + '">' + strText + '</span>');
	//document.frames("myEditor").document.frames("textEdit").document.body.innerHTML = newText;
}
function compareOptionValues(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.value, 36 );  
  var sB = parseInt( b.value, 36 );  
  return sA - sB;
}

// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.text, 36 );  
  var sB = parseInt( b.text, 36 );  
  return sA - sB;
}

// Dual list move function
function moveDualList(srcList, destList, moveAll) 
{
  // Do nothing if nothing is selected
  srcList = document.getElementById(srcList);
  destList = document.getElementById(destList);

  newDestList = new Array(destList.options.length);
  var len = 0;
  for(len=0;len < destList.options.length; len++) {
    if (destList.options[len] != null) {
      newDestList[len]=new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
    }
  }
  for(var i = 0; i < srcList.options.length; i++) { 
    if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
       // Statements to perform if option is selected
       // Incorporate into new list
       newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
       len++;
    }
  }
  // Sort out the new destination list
  newDestList.sort( compareOptionValues );   // BY VALUES
  //newDestList.sort( compareOptionText );   // BY TEXT
  // Populate the destination with the items from the new array
  for ( var j = 0; j < newDestList.length; j++ ) {
    if (newDestList[j] != null ) {
      destList.options[j] = newDestList[j];
    }
  }
  // Erase source list selected elements
  for(var i = srcList.options.length - 1; i >= 0; i--) { 
    if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
       // Erase Source
       //srcList.options[i].value = "";
       //srcList.options[i].text  = "";
       srcList.options[i] = null;
    }
  }
} 
	// Automatically sort items within lists? (1 or 0)
sortitems = 1; 
 
function moveIt(fbox, tbox) { 
	for(var i=0; i < fbox.options.length; i++) { 
		if(fbox.options[i].selected && fbox.options[i].value != "") { 
			var no = new Option(); 
			no.value = fbox.options[i].value; 
			no.text = fbox.options[i].text; 
			tbox.options[tbox.options.length] = no; 
			fbox.options[i].value = ""; 
			fbox.options[i].text = ""; 
		} 
	} 
	bumpUp(fbox); 
	if (sortitems) { 
		sortIt(tbox); 
	} 
} 
 
function bumpUp(box) {
	for(var i=0; i < box.options.length; i++) { 
		if(box.options[i].value == "") { 
			for(var j=i; j < box.options.length-1; j++) { 
				box.options[j].value = box.options[j+1].value; 
				box.options[j].text = box.options[j+1].text; 
			} 
			var ln = i; 
			break; 
		} 
	} 
	if(ln < box.options.length) { 
		box.options.length -= 1; 
		bumpUp(box); 
	} 
} 
 
function sortIt(box)  { 
	var temp_opts = new Array(); 
	var temp = new Object(); 
	for(var i=0; i < box.options.length; i++) { 
		temp_opts[i] = box.options[i]; 
	} 
	for(var x=0; x < temp_opts.length-1; x++)  { 
		for(var y=(x+1); y < temp_opts.length; y++)  { 
			if(temp_opts[x].text > temp_opts[y].text)  { 
				temp = temp_opts[x].text; 
				temp_opts[x].text = temp_opts[y].text; 
				temp_opts[y].text = temp; 
			} 
		} 
	} 
	for(var i=0; i < box.options.length; i++) { 
		box.options[i].value = temp_opts[i].value; 
		box.options[i].text = temp_opts[i].text; 
	} 
}
function move(direction,listname) {
		
		if (listname == 'selected_mods') {
			dfr = document.page.selected_mods;
		}
		
		boxLen = dfr.length;
		currentItem = dfr.selectedIndex;
		
		if ((direction == 'up') && (currentItem > 0)) {
		
			selText = dfr.options[currentItem].text;
			selVal = dfr.options[currentItem].value;
			swpText = dfr.options[currentItem - 1].text;
			swpVal = dfr.options[currentItem - 1].value;
			
			dfr.options[currentItem - 1].text = selText;
			dfr.options[currentItem - 1].value = selVal;
			dfr.options[currentItem].text     = swpText;
			dfr.options[currentItem].value     = swpVal;
			dfr.selectedIndex = currentItem - 1;
			
		} else if ((direction == 'down') && (currentItem < boxLen - 1) && (currentItem != -1)) {
			
			selText = dfr.options[currentItem].text;
			selVal = dfr.options[currentItem].value;
			swpText = dfr.options[currentItem + 1].text;
			swpVal = dfr.options[currentItem + 1].value;
			
			dfr.options[currentItem + 1].text = selText;
			dfr.options[currentItem + 1].value = selVal;
			dfr.options[currentItem].text     = swpText;
			dfr.options[currentItem].value     = swpVal;
			
			dfr.selectedIndex = currentItem + 1;
			
		} else if (currentItem == -1) {
			
			alert("You must select an item before you can move it.");
			
		} else {}
		
	}
	function selectAll(FormName, SelectBox){
  temp = "document." + FormName + "." + SelectBox;
  Source = eval(temp);

  for(x=0; x<(Source.length); x++){
    Source.options[x].selected = "true";
    }
}
function addInput (inputtype,inputname) {
  if (document.all || document.getElementById) {
    var table = document.all ? document.all.formElems :
      document.getElementById('formElems');
    var row = table.insertRow(++f);
    if (document.all) {
	  var firstcell = row.insertCell(0);
	  firstcell.align = "right"
	  firstcell.innerHTML = "Option " + (f+1);
	  var cell = row.insertCell(1);
      cell.innerHTML =
        '<INPUT TYPE="' + inputtype + '" NAME="' + inputname + '" class="smallData" style="width:85%">';
    }
    else {
      var cell = row.insertCell(0);
      var input = document.createElement('INPUT');
      input.setAttribute('type', 'button');
      input.id = 'addButton' + f;
      input.value = 'select another file';
      input.onclick = function () { addInput(); };
      cell.appendChild(input);
      cell = row.insertCell(1);
      input = document.createElement('INPUT');
      input.setAttribute('type', 'file');
      input.name = 'fileName' + f;
      input.onchange = function () { addInput(); };
      cell.appendChild(input);
      var oldButton = document.getElementById('addButton' + (f - 1));
      oldButton.parentNode.removeChild(oldButton);
    }
  }
}