function getElement(id) {
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

var editorWindow = null;

function SetEditor(editorWindowId) {
	editorWindow = getElement(editorWindowId).contentWindow;
	editorWindow.document.designMode="on";
	editorWindow.focus();
//	editorWindow.document.bgColor = '000000';
//	editorWindow.document.fgColor = 'FFFFFF';
	if (editorWindow.document.body && editorWindow.document.body.style)
		editorWindow.document.body.style.fontFamily = 'Arial';
}
/////////////////////////////////////////////////////////////
function adjustLayer() {

	document.getElementById('idContent').document.designMode = "On";
	document.getElementById('idContent').focus();

	var the_width=document.getElementById('idContent').contentWindow.document.body.scrollWidth;
	var the_height=document.getElementById('idContent').contentWindow.document.body.scrollHeight;
		
	w1 = document.body.offsetWidth;
	h = document.body.offsetHeight;
	
	if(w1 < 700) 
	 document.getElementById('idContent').style.width = 0.9 * w1;
	else
	 document.getElementById('idContent').style.width = 685;
	
	w2 = the_width;	
	val = (w1 - w2 - 15) / 2;
	val2 = (w1 / 2) - 87;

	document.getElementById('idContent').style.left = val;
	document.getElementById('idContent').style.height = 0.7 * h;
	document.getElementById('ColorPalette').style.left = val2;	
}

function displayColorPalette(){  
 var panel = document.getElementById('ColorPalette'); 
 
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  {panel.style.display = 'none'; panel.src=null;}
 		
	//adjustLayer();	
	//if(ColorPalette.style.display == "inline") 
  //  	hideColorPalette();
 	//else
  //     ColorPalette.style.display = "inline";		    
} 
function hideColorPalette(){ 
	ColorPalette.style.display = "none";
	return false;
}

function ToggleImagePanel(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('ImagePanel'); 
 panel.src="SelectImage.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  {panel.style.display = 'none'; panel.src=null;}
}

function ToggleImageProperties(){
	//---> Toggles the 'Image Properties' panel...
 var panel = document.getElementById('ImageProperties'); 
 panel.src="ImageProperties.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  {panel.style.display = 'none'; panel.src=null;}
}

function ToggleImageUpload(){
 //---> Toggles the 'Image Upload' panel...	
 var panel = document.getElementById('UploadImage'); 
 panel.src="ImageUpload.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none'; 
}

function ToggleThemeSelect(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('ThemeSelect'); 
 panel.src="ThemeSelect.htm" ;
 if (panel.style.display == 'none') panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none';
 return false;
}
function ToggleTable(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('InsertTable'); 
 panel.src="InsertTable.php" ;
 if (panel.style.display == 'none') panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none';
}

function TableProps(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('EditTable'); 
 panel.src="EditTable.php" ;
 if (panel.style.display == 'none') panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none';
}
function ToggleDocument(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('DocumentDownload'); 
 panel.src="DocumentDownload.php" ;
 if (panel.style.display == 'none') panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none';
}
function ToggleDocumentProperties(){
 //---> Toggles the 'Image Upload' panel...	
 var panel = parent.document.getElementById('DocumentProperties'); 
 panel.src="DocumentProperties.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none'; 
}
function InsertDocumentDemo(){
 //---> Toggles the 'Image Upload' panel...	
 var panel = parent.document.getElementById('DocumentProperties'); 
 panel.src="InsertDocumentDemo.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none'; 
}
function TogglePhotoProperties(){
 //---> Toggles the 'Image Upload' panel...	
 var panel = parent.document.getElementById('PhotoProperties'); 
 panel.src="PhotoProperties.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  panel.style.display = 'none'; 
}
function InsertImageDemo(){
	//---> Toggles the 'Select Image' panel...
 var panel = document.getElementById('PhotoProperties'); 
 panel.src="InsertImageDemo.php" ;
 if (panel.style.display == 'none')  panel.style.display = 'inline'; 
 else if (panel.style.display == 'inline')  {panel.style.display = 'none'; panel.src=null;}
}
function openWindow(pageName){
	var x = screen.width * 0.4;
	var y = screen.height * 0.5;
	var SB = "scrollbars=yes";
	if(pageName == "ImageUpload.php")
	 SB = "scrollbars=no";
	window.open(pageName,'',SB+', location=no, menu=no, status=no, width=300, height=200, left = '+x+', top = '+y);
}
function showHTML(){
	
     if (navigator.appName.indexOf("Microsoft")==-1){
      alert(document.getElementById("idContent").contentDocument.body.innerHTML); 
     } 	
     else{ alert(idContent.document.body.innerHTML);}
}
function boldText(){
	editorWindow.document.execCommand('bold',false,null);
	editorWindow.focus();
	return false;
    }   
function italicText(){
	editorWindow.document.execCommand('italic',false,null);
	editorWindow.focus();
	return false;
    } 
function underlineText(){
	editorWindow.document.execCommand('underline',false,null);
	editorWindow.focus();
	return false;
    } 
function setFontName(theName){
	editorWindow.document.execCommand('FontName',false,theName);
	editorWindow.focus();
	return false;
    } 
function setFontSize(theSize){
	editorWindow.document.execCommand('FontSize',false,theSize);
	editorWindow.focus();
	return false;
    } 
function setFontColor(theColour){
	
	if(ColourOption !='TXT'){
	editorWindow.document.bgColor = theColour;
	//editorWindow.focus();
	//return false;
  }
  else{
	 editorWindow.document.execCommand("ForeColor", false, theColour);
	//editorWindow.focus();
	//return false;  	
  	}
    	
  }    
function indent(){
	editorWindow.document.execCommand('Indent',false,null);
	editorWindow.focus();
	return false;
    }  
function outdent(){
	editorWindow.document.execCommand('Outdent',false,null);
	editorWindow.focus();
	return false;
    }  
function align(theAlignment){
	if(theAlignment == "left"){
		
     idContent.document.execCommand("JustifyLeft", true, null);
  }
	if(theAlignment == "right")
     idContent.document.execCommand("JustifyRight", true, null);
	if(theAlignment == "center")
     idContent.document.execCommand("JustifyCenter", true, null);	
    }  
function alignLeft(){
	editorWindow.document.execCommand('JustifyLeft',false,null);
	editorWindow.focus();
	return false;
	}
function alignRight(){
	editorWindow.document.execCommand('JustifyRight',false,null);
	editorWindow.focus();
	return false;
}
function alignCenter(){
	editorWindow.document.execCommand('JustifyCenter',false,null);
	editorWindow.focus();
	return false;
}
function insertHyperlink(){
	if (navigator.appName.indexOf("Microsoft")!=-1){//IE 
	  editorWindow.document.execCommand("CreateLink",true,null);
	  editorWindow.focus();
	  return;
	var slink = showModalDialog("insertlink.asp","","dialogHeight: 210px; dialogWidth: 310px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: yes; resizable: Yes; status: yes;" );
  if (slink != null){     
  	var text = editorWindow.document.selection.type;
  
  		var slinkall = slink["all"];
	    var slinkhref = slink["href"];
	    alert (slinkall);
	    alert (slinkhref);
	    alert (slink);
  	if(text == "Text"){
			//var testo=editorWindow.document.selection.createRange().text;  
			//var testohtml=editorWindow.document.selection.createRange();
			//testohtml.pasteHTML ("<a href='"+slink+">"+testo+"</a>");
	  	//alert (editorWindow.document.selection.Text);
	  	var range = editorWindow.document.selection.createRange();
	    var oldtext  = range.text;
	  	//alert ("dgh");
			//editorWindow.document.execCommand("CreateLink",false,src);
			editorWindow.document.selection.clear();	
			//editorWindow.focus();	   

	  	var bla ="_blank";
			var html = "<a href='"+slink+">";
			html = html + oldtext;
			html = html + "</a>";
			var random_string = "insert_html_" + Math.round(Math.random()*100000000);			
			editorWindow.document.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			var current_html = parent.idContent.document.body.innerHTML = parent.idContent.document.body.innerHTML.replace(pat, html); 				
	  }
	  else{
	  	var bla ="_blank";
			var html = "<a href='"+slink+">";
			html = html + slinkhref;
			html = html + "</a>";
			var random_string = "insert_html_" + Math.round(Math.random()*100000000);			
			editorWindow.document.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			var current_html = parent.idContent.document.body.innerHTML = parent.idContent.document.body.innerHTML.replace(pat, html); 				  	  
	  }
	}
	return;  
		var src = prompt("Type the URL below", "http://");
    var text = editorWindow.document.selection.type;  
         
	  if(text == "Text"){
	  	//alert (editorWindow.document.selection.Text);
	  	var range = editorWindow.document.selection.createRange();
	    var oldtext  = range.text;
	  	//alert ("dgh");
			//editorWindow.document.execCommand("CreateLink",false,src);
			editorWindow.document.selection.clear();	
			//editorWindow.focus();	   
	  
	  	var bla ="_blank";
			var html = "<a href=";
			html = html +  src;
			html = html + " target=\"" + bla + "\">";
			html = html + oldtext;
			html = html + "</a>";
			var random_string = "insert_html_" + Math.round(Math.random()*100000000);			
			editorWindow.document.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			var current_html = parent.idContent.document.body.innerHTML = parent.idContent.document.body.innerHTML.replace(pat, html); 	
		}
		else{
	  	var bla ="_blank";
			var html = "<a href=";
			html = html + src;
			html = html + " target=\"" + bla + "\">";
			html = html + src;
			html = html + "</a>";
			var random_string = "insert_html_" + Math.round(Math.random()*100000000);			
			editorWindow.document.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			var current_html = parent.idContent.document.body.innerHTML = parent.idContent.document.body.innerHTML.replace(pat, html); 			
		}
	}
	else{
		var src = prompt("Type the URL below", "http://");
    var text = editorWindow.window.getSelection();      
	  if(text != "" || text != null){
			editorWindow.document.execCommand("CreateLink",false,src);
			editorWindow.window.getSelection().removeAllRanges();	
			editorWindow.focus();	   
	  }
	  else{	  	
	  	var bla ="_blank";
			var html = "<a href=";
			html = html + src;
			html = html + " target=\"" + bla + "\">";
			html = html + src;
			html = html + "</a>";
			var random_string = "insert_html_" + Math.round(Math.random()*100000000);			
			parent.document.getElementById("idContent").contentDocument.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			var current_html = parent.document.getElementById("idContent").contentDocument.body.innerHTML = parent.document.getElementById("idContent").contentDocument.body.innerHTML.replace(pat, html); 	
	  }
 
	}
	return false;
}
function insertHorizontalRule(){
	editorWindow.document.execCommand('InsertHorizontalRule',false,null);
	editorWindow.focus();
	return false;			
	}
function ctrlX(){
	var cmdFailed = false;
	if (navigator.appName.indexOf("Microsoft")==-1){
		eval("try {editorWindow.document.execCommand('Cut',false,null);} catch (e) {cmdFailed = true;}");
		if (cmdFailed) {
	    if(confirm('Copy/Cut/Paste is not available with Mozilla and Firefox.\n Do you want more informaiton about this issue?')){
	      window.open('http://www.mozilla.org/editor/midasdemo/securityprefs.html', '_blank');
	    }
	    else {return false;}
    }
  }
	editorWindow.document.execCommand('Cut',false,null);
	editorWindow.focus();
	return false;
	}
function ctrlC(){
	var cmdFailed = false;
	if (navigator.appName.indexOf("Microsoft")==-1){
		eval("try {editorWindow.document.execCommand('Copy',false,null);} catch (e) {cmdFailed = true;}");
		if (cmdFailed) {
	    if(confirm('Copy/Cut/Paste is not available with Mozilla and Firefox. \n Do you want more informaiton about this issue?')){
	      window.open('http://www.mozilla.org/editor/midasdemo/securityprefs.html', '_blank');
	    }
	    else {return false;}
    }
  }	
	editorWindow.document.execCommand('Copy',false,null);
	editorWindow.focus();
	return false;
	}
function ctrlV(){
	var cmdFailed = false;
	if (navigator.appName.indexOf("Microsoft")==-1){
		eval("try {editorWindow.document.execCommand('Paste',false,null);} catch (e) {cmdFailed = true;}");
		if (cmdFailed) {
	    if(confirm('Copy/Cut/Paste is not available with Mozilla and Firefox. \n Do you want more informaiton about this issue?')){
	      window.open('http://www.mozilla.org/editor/midasdemo/securityprefs.html', '_blank');
	    }
	    else {return false;}
    }
  }	
	editorWindow.document.execCommand('Paste',false,null);
	editorWindow.focus();
	return false;	
	}
function insertImage(){
	  if (navigator.appName.indexOf("Microsoft")==-1){
      document.getElementById("idContent").contentDocument.execCommand("InsertImage");	
    }	
	idContent.document.execCommand("InsertImage");	
	}
function insertOrderedList(){
	editorWindow.document.execCommand('InsertOrderedList',false,null);
	editorWindow.focus();
	return false;	
	
	}
function insertUnorderedList(){
	editorWindow.document.execCommand('InsertUnorderedList',false,null);
	editorWindow.focus();
	return false;	
	
	}
function undo(){
	editorWindow.document.execCommand('Undo',false,null);
	editorWindow.focus();
	return false;	
	}
function redo(){
	editorWindow.document.execCommand('Redo',false,null);
	editorWindow.focus();
	return false;
	}
function pasteHTML(txt){
	document.getElementById("idContent").writeln(txt);	
	}	
function findText(txt){
	var rng = document.getElementById("idContent").document.body.createTextRange();
	if (rng.findText(txt)==true) {
     rng.select();
     rng.scrollIntoView();
	 }
	}