/*
 *	Content Editor JavaScripts
 *
 *	Updates
 *	Date		Person  Changes
 *
 * 13-07-04	OB			Moved editContent() to here
 * 
 *	08-01-01			Added putHTML function. This will be used to add HTML to the editor window
 *	10-01-01			Rewrote the LoadForm function so that is uses a modal dialogue
 *	10-01-01			Removed image specific functions from this page, and put them in the image page
 *	10-01-01			Changes putHTML function to use pastHTML method, rather than innerHTML property
 *	11-01-01			Made a ton of changes...
 *
 */


	function editContent(contentID) {
		source = "editor.jsp?contentID="+contentID;
		newWindow = window.open(source, 'editWin', 'toolbar=no,location=no,scrollbars=no,width=650,height=700');
	}




	// NewTag function uses built in ie iframe editing features
	// requires command eg MakeTag('formatblock', '<h1>')
	function NewTag(block, command){
		if (format == "HTML"){
			var ST = frames.ContentEditor.document.selection.createRange()
			ST.execCommand(block, false, command)
			ST.select()
			frames.ContentEditor.focus()
		}
	}

	// LoadForm function opens input form in new window
	//
	// requires form name to load eg LoadForm('imageContainer.jsp')
	function LoadForm(source,height,width){
		// Open Modal Dialogue and wait for a return value
		eval("returnHTML = window.showModalDialog(source,'',\"dialogHeight:'"+height+"px';dialogWidth:'"+width+"px';scroll:'no';status:'no';help:'no'\")");
		// Call "putHTML" with the return value - if return value is valid, that is!
		if ((returnHTML != "")&&(returnHTML != null)&&(returnHTML != "undefined")) putHTML(returnHTML);
	}


	// MakeTag function uses built in ie iframe editing features
	// requires command eg MakeTag('Bold')
	function MakeTag(command){
		frames.ContentEditor.focus()
		if (format == "HTML"){
			var ST = frames.ContentEditor.document.selection.createRange()
			ST.execCommand(command)
			ST.select()
			frames.ContentEditor.focus()
		}
	}
	
	function StripBaseURL(u) {
		//var re = new RegExp ('http://localhost:8080/aima.net.au/', 'gi');
		//return u.replace(re,'');
	}
	
	
	// ModeSwap function changes between HTML(WYSIWYG)and Edit Modes
	function ModeSwap(){
		if (format =="HTML"){ // switch to Text
			ContentEditor.document.body.innerText = ContentEditor.document.body.innerHTML
	
			//var t = ContentEditor.document.body.innerHTML;
			//ContentEditor.document.body.innerText = StripBaseURL(t);
	
			ContentEditor.document.body.style.fontFamily = "Courier New"
			ContentEditor.document.body.style.fontSize = "10pt"
			format="Text"
		} 
		else { // switch to HTML
			//var t = ContentEditor.document.body.innerText;
			//ContentEditor.document.body.innerHTML = StripBaseURL(t);
			
			ContentEditor.document.body.innerHTML = ContentEditor.document.body.innerText;
			
			ContentEditor.document.body.style.fontFamily = ""
			ContentEditor.document.body.style.fontSize = ""
			format="HTML"
		}
		ContentEditor.focus()
		var s = ContentEditor.document.body.createTextRange()
		s.collapse(true)
		s.select()
	}

	function ModeSwap2() {
		if (format =="HTML") { // switch to Text
			iHTML = ContentEditor.document.body.innerHTML;
			//alert( 'iHTML = ' + iHTML );
			ContentEditor.document.body.innerText = iHTML;
			ContentEditor.focus();
			format="Text"
		}
		else {	
			txtRange = ContentEditor.document.body.createTextRange() ;
			txtRange.select();
			txtRange.execCommand("Copy");
			txtRange.execCommand("Paste");
			iText = ContentEditor.document.body.innerText;
			//alert( 'iText = ' + iText );
			ContentEditor.document.body.innerHTML = iText;
			ContentEditor.focus();
			format="HTML"
		} 		

	}


	//function to send image Code to Main Editor
//	function SendtoEditor(imagename){
//		var SM = opener.ContentEditor.document.body.innerHTML
//		var ST = "<img src="+imagename+">"
//		opener.ContentEditor.document.write(ST)
//		window.close()
//	}

	// function for inserting links
	function insertLink(){
		returnLink = window.showModalDialog('linkform.html','',"dialogHeight:'150px';dialogWidth:'240px';scroll:'no';status:'no';help:'no'");
		if ((returnLink != null) && (returnLink != "")){
			var linkHTML = returnLink+frames.ContentEditor.document.selection.createRange().text+"</a>"
			putHTML(linkHTML);
		}
	}

  /**********************************************************
   * putHTML(STRING)                                        *
   * ACCEPTS: STRING of HTML                                *
   * RETURNS: nothing                                       *
   * ACTION:  inserts the HTML string at the current cursor *
   *          position, or over the currently selected text *
   *          in the editor iFrame                          *
   **********************************************************/
	function putHTML(html){
	 	frames.ContentEditor.focus();
		var Range = ContentEditor.document.selection.createRange();
		Range.pasteHTML(html);
	}


  /**********************************************************
   * getHTML()                                              *
   * ACCEPTS: nothing                                       *
   * RETURNS: STRING of HTML                                *
   * ACTION:  returns the innerHTML of the editor iFrame as *
   *          a string                                      *
   **********************************************************/
	function getHTML(){
	 	var target = frames.ContentEditor
		htmlString = target.document.body.innerHTML;
		alert(htmlString);
	 	target.focus()
		return htmlString;
	}


  /**********************************************************
   * saveHTML()                                             *
   * ACCEPTS: nothing                                       *
   * RETURNS: STRING of HTML                                *
   * ACTION:  returns the innerHTML of the editor iFrame as *
   *          a string                                      *
   **********************************************************/
	function saveHTML(){
		//alert(frames.ContentEditor.document.body.innerHTML);

        if (format =="HTML"){
			document.forms.editForm.content.value = frames.ContentEditor.document.body.innerHTML;
		    document.forms.editForm.submit();
		} else {
			ContentEditor.document.body.innerHTML = ContentEditor.document.body.innerText
			ContentEditor.document.body.style.fontFamily = ""
			ContentEditor.document.body.style.fontSize = ""
			format="HTML"
			document.forms.editForm.content.value = frames.ContentEditor.document.body.innerHTML;
		    document.forms.editForm.submit();
		}
		//ContentEditor.focus()
		//var s = ContentEditor.document.body.createTextRange()
		//s.collapse(true)
		//s.select()
                		
		//document.forms.editForm.content.value = frames.ContentEditor.document.body.innerHTML;
		//document.forms.editForm.submit();
	}


	function pasteSpecial(){
		frames.ContentEditor.focus()
		var ST = frames.ContentEditor.document.selection.createRange()
		ST.text = window.clipboardData.getData("Text");
	}
