var currentBox
function openImageWindow(curBox,rec_id){
	var tmpStr
	var oEditor=eval("idContentoEdit1");
	var oSel;
	if (window.getSelection)		//Firefox
	{
		oSel = oEditor.getSelection();
		oElement = window.getSelectedElement(oSel);
		currentBox = curBox
		window.open('manage_images.asp?rec_id=' + rec_id, '', 'width=650,height=500,scrollbars=yes,resizable=yes,status=yes');
	}
	else							//IE
	{
		oSel=oEditor.document.selection.createRange();
		if (oSel.parentElement)	oElement=oSel.parentElement();
		else oElement=oSel.item(0);
		
		if(oElement.tagName=="IMG"){
			modelessDialogShow('../scripts/image.htm',440,351);
		}else{
		currentBox = curBox
		window.open('manage_images.asp?rec_id=' + rec_id, '', 'width=650,height=500,scrollbars=yes,resizable=yes,status=yes');
		}
	}
	
	
}
function addImage(addText){
	var oEditor=eval("idContentoEdit1");
	oEditor.focus();
	eval(currentBox + ".insertHTML(addText)");
}

function openDocWindow(curBox,rec_id){
	currentBox = curBox
	window.open('manage_documents.asp?rec_id=' + rec_id, '', 'width=750,height=500,scrollbars=yes,resizable=yes,status=yes');
}
function addDocument(addText){
	eval(currentBox + ".insertHTML(addText)");
}

function GetHtml() {
   return oEdit1.getXHTMLBody();
}
function GetHTMLMult(theObject){
  return theObject.getXHTMLBody();
}

function SetHtml(sVal) {
  if(theGlobalObject != null){
    theGlobalObject.putHTML(sVal);
  }else{
   oEdit1.putHTML(sVal);
   }
}

function addText(tmpStr){
   oEdit1.insertHTML(tmpStr);
}

function cleanHTMLCode(curForm){
	var tmpStr

	if(confirm("You are about to reformat your entire page.  This action cannot be undone.  Continue?")){
		tmpStr = GetHtml();	
		tmpStr = cleanCodeRegExp(tmpStr)
		oEdit1.loadHTML(tmpStr);
	}
	
   	//sel = Composition.document.selection;
	//if ( "Text" == sel.type){
	//	
   	//	range = sel.createRange();
	//	tmpStr = range.htmlText;
	//	alert(tmpStr)
	//	tmpStr = cleanCodeRegExp(tmpStr)		
	//	alert(tmpStr)
	//	range.pasteHTML(tmpStr);	
	//}else{
		//OLD CODE GOES HERE
	//}
}

function cleanCodeRegExp(tmpStr){
	

	//Remove HTML/XML comments
	tmpStr = tmpStr.replace(/<!--.*?-->/gi,""); 
	// Remove XML
	tmpStr = tmpStr.replace(/<!.*?>/gi,"");
	tmpStr = tmpStr.replace(/<\?xml.*?>/gi,"");	
	// Remove Meta elements
	tmpStr = tmpStr.replace(/<meta\s.*?>/gi,"");
	// Remove visual formatting
	tmpStr = tmpStr.replace(/<span\s.*?>/gi,"");
	tmpStr = tmpStr.replace(/<\/span>/gi,"");
	tmpStr = tmpStr.replace(/<font\s.*?>/gi,"");
	tmpStr = tmpStr.replace(/<\/font>/gi,"");

	tmpStr = tmpStr.replace(/<li\s.*?>/gi,"<li>");
//	tmpStr = tmpStr.replace(/<\/li>/gi,"");	
	
	tmpStr = tmpStr.replace(/<div\s.*?>/gi,"");
	tmpStr = tmpStr.replace(/<\/div>/gi,"");	
	
	tmpStr = tmpStr.replace(/<h\d.*?>/gi,"");
	tmpStr = tmpStr.replace(/<\/h\d.*?>/gi,"");	
	
	// Remove some unknown stuff
	tmpStr = tmpStr.replace(/<o:p>/gi,"");
	tmpStr = tmpStr.replace(/<\/o:p>/gi,"");
	// Remove stylesheet stuff
	tmpStr = tmpStr.replace(/<p\s.*?>/gi,"<p>");
	tmpStr = tmpStr.replace(/<link\s.*?>/gi,"")
	tmpStr = tmpStr.replace(/<style\W.*?<\/style>/gi,"");
	// Remove style attributes
	tmpStr = tmpStr.replace(/\sstyle\=\'[^']*?mso-.*?\'/gi,"");
	// Remove some more garbage
	tmpStr = tmpStr.replace(/<html\s.*?>/gi,"<html>");
	tmpStr = tmpStr.replace(/<body\s.*?>/gi,"<body>");
	
	tmpStr = tmpStr.replace(/style=\".*?\"/gi,"");
	tmpStr = tmpStr.replace(/style=\'.*?\'/gi,"");

	//Replace empty links
	tmpStr = tmpStr.replace(/<a href=[^>]*?><\/a>/gi,"");
	tmpStr = tmpStr.replace(/<a href=[^>]*?>(<a href=[^>]*?>)/gi,"$1");

	tmpStr = tmpStr.replace(/<P>&nbsp;<\/P>/gi,"");
	
	//tmpStr = tmpStr.replace(/<\/a><\/a>/gi,"</A>");
	
	//tmpStr = tmpStr.replace(/<p>/gi,"");
	//tmpStr = tmpStr.replace(/<\/p>/gi,"<br>");	

	return tmpStr
}


function removeAllHTML(curForm){
	var tmpStr
	
	if(confirm("You are about to remove all formatting from your entire page.  This action cannot be undone.  Continue?")){
		tmpStr = GetHtml();
		
		tmpStr = tmpStr.replace(/<\/p>/gi,"#br#");
		tmpStr = tmpStr.replace(/<br>/gi,"#br#");	
		tmpStr = tmpStr.replace(/<[\w\/\!\?][^>]*?>/gi,"");
		tmpStr = tmpStr.replace(/#br#/gi,"<br>");	
		
		oEdit1.loadHTML(tmpStr);
	}
}
