//3 dimensional button stuff

function button_over(eButton){
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft = "buttonhighlight solid 1px";
	eButton.style.borderRight = "buttonshadow solid 1px";
	eButton.style.borderTop = "buttonhighlight solid 1px";
	}
function button_out(eButton){
	eButton.style.borderColor = "threedface";
	}
function button_down(eButton){
	eButton.style.borderBottom = "buttonhighlight solid 1px";
	eButton.style.borderLeft = "buttonshadow solid 1px";
	eButton.style.borderRight = "buttonhighlight solid 1px";
	eButton.style.borderTop = "buttonshadow solid 1px";
	}
function button_up(eButton){
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft = "buttonhighlight solid 1px";
	eButton.style.borderRight = "buttonshadow solid 1px";
	eButton.style.borderTop = "buttonhighlight solid 1px";
	eButton = null; 
	}

//Rich text editor stuff
	
var isHTMLMode=false;
var contentSelected = "";

function makeSelected (opt) {
		contentSelected = opt;
	}
function document.onreadystatechange(){
  	document.designMode="On"
	}
	
function cmdExec(cmd,opt) {
  	if (isHTMLMode)
	{
		alert("Please uncheck 'Edit HTML'");
		return;
	}

	if (cmd == "paste") {
		if (contentSelected == ""){
			alert ("No selection made to paste into.");
			return;
		}else{		
			MWJ_findObj2(contentSelected).focus();
		}
	}
	
	if (cmd == "CreateLink"){
		el = document.selection.createRange();
		el.expand("word");
		if (el.text == '')
		{
			el = document.body.createRange();
		}
		//el.select(el);
		//alert(el.htmlText);
		pe = el.parentElement();
		//alert(pe.tagName);
	}
	
  	document.execCommand(cmd,"",opt);
	document.focus();
	}

function inslink() {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	//alert(el.HTML) 
	mylink = showModalDialog('dialogs/insertlink.htm','','dialogWidth:430px;dialogHeight:380px;unadorned:1;');
	if (mylink != null)
	{
	var linkarr = new Array();
	linkarr = mylink;
	//alert(mylink);
	thisurl = linkarr["LinkUrl"]
  	thislabel = linkarr["LinkLabel"]
	//alert(lin);
	if ((el.text.length > 0) && (thisurl.length > 0))
	{
	el.pasteHTML('<a href="' + thisurl + '">' + el.text + '</a>');
	document.focus();
	}
	else
	{
		alert('You have not selected any text to make a hyperlink');
	}
	}
	}
	
function insimage(target,arguments) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	if (!arguments["ImgUrl"])
		{
		t = MWJ_findObj(target);
		
		//bl = t.createControlRange();
		
		if (t.document.selection.type != "None")
		{
		el = t.document.selection.createRange();
		
		
		myimg = showModalDialog('dialogs/insertimage.htm',arguments,'dialogWidth:460px;dialogHeight:430px;unadorned:1;');
		if (myimg != null)
		{
		var imgarr = new Array();
		imgarr = myimg;
		thisurl = imgarr["ImgUrl"]
		thisalign = imgarr["ImgAlign"]
		thisborder = imgarr["ImgBorder"]
		thishspace = imgarr["HorSpace"]
		thisvspace = imgarr["VerSpace"]
		thisalt = imgarr["AltText"]
		//window.status = ' Image Insterted : ' +thisurl;
		//thislabel = linkarr["LinkLabel"]
		//alert(lin);
		//var image1=new Image()
		//image1.src=thisurl;
		//el.add(image1);
		el.pasteHTML('<img src="' + thisurl + '" border=' + thisborder + ' align=' + thisalign + '>');
		}
		}
		else
		{
			alert('Please select an area to insert image (at least a blank character space)');
		}
	}
	else
	{
		window.status = 'Editing Image : ' + arguments["ImgUrl"];
		el = document.selection.createRange().item(0);
		//alert(el.HTML) 
		myimg = showModalDialog('dialogs/insertimage.htm',arguments,'dialogWidth:460px;dialogHeight:430px;unadorned:1;');
		if (myimg != null)
		{
		var imgarr = new Array();
		imgarr = myimg;
		el.src = imgarr["ImgUrl"];
		el.align = imgarr["ImgAlign"]
		el.border = imgarr["ImgBorder"]
		el.hspace = imgarr["HorSpace"]
		el.vspace = imgarr["VerSpace"]
		el.alt = imgarr["AltText"]
		window.status = 'Image : ' + arguments["ImgUrl"] + ' updated';
		}
	}
	//document.focus();
}

function insasset() {
		myasset= showModalDialog('dialogs/insertasset.htm','','dialogWidth:460px;dialogHeight:300px;unadorned:1;');
		if (myasset != null)
		{
		var assetarr = new Array();
		assetarr = myasset;
		thisurl = myasset["ImgUrl"]
		assetfile.value = thisurl
		ispdf = thisurl.indexOf(".pdf");
		isxls = thisurl.indexOf(".xls");
		isdoc = thisurl.indexOf(".doc"); 
		isimg1 = thisurl.indexOf(".jpg");
		isimg2 = thisurl.indexOf(".gif"); 
		isppt = thisurl.indexOf(".ppt");
		if (ispdf != -1) { thistype = 1}
		if (isxls != -1) { thistype = 2}
		if (isdoc != -1) { thistype = 3}
		if (isppt != -1) { thistype = 4}
		if (isimg1 != -1) { thistype = 5}
		if (isimg2 != -1) { thistype = 5}
		atype.selectedIndex = thistype -1;
		//is = thisurl.indexOf(".pdf"); 
		//window.status = ' Image Insterted : ' +thisurl;
		//thislabel = linkarr["LinkLabel"]
		//alert(lin);
		}
}


function insdiv(arguments) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	if (!arguments["ID"])
	{
	el = document.selection.createRange();
	//alert(el.HTML) 
	mydiv = showModalDialog('dialogs/insertdiv.htm','','dialogWidth:430px;dialogHeight:290px;unadorned:1;');
	if (mydiv != null)
	{
	var divarr = new Array();
	divarr = mydiv;
	thisid = divarr["ID"]
	thiswidth = divarr["Width"]
	thisheight = divarr["Height"]
	thistop = divarr["Top"]
	thisleft = divarr["Left"]
	thisz = divarr["Z-Index"]
	thistype = divarr["Postype"]
	window.status = ' DIV Insterted : ' +thisid;
  	//thislabel = linkarr["LinkLabel"]
	//alert(lin);
	el.pasteHTML('<div id="' + thisid + '" style="position:' + thistype + '; top:' + thistop + '; left:' + thisleft + '; width:' + thiswidth + '; height:' + thisheight + '; z-index:' + thisz + '">DIV Inserted Here</div');
	}
	else
	{
	alert('Coming Soon...');
	}
	document.focus();
	}
}
	
function changestyle(cmd,opt) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	//alert(el.HTML) 
	el.pasteHTML('<SPAN class='+ opt +'>'+ el.text + '</SPAN>');
	document.focus();
	}

function getprop() {
	var arr, args;
  	arr=null;
  	args=new Array();
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	el2 = document.selection.type;
	el3 = document.selection.type.style;
	//alert(el3)
	
	if(el2 == "Control"){
     var oImg = document.selection.createRange().item(0);
	 if (oImg.tagName == "IMG")
	 {

     args["ImgUrl"] = oImg.src;
     args["AltText"] = oImg.alt;
     args["ImgBorder"] = oImg.border;
     args["HorSpace"] = oImg.hspace;
     args["VerSpace"] = oImg.vspace;
     args["ImgAlign"] = oImg.align;
	 insimage(args)
	 }
	 if (oImg.tagName == "TABLE")
	 {
     args["ImgUrl"] = oImg.src;
     args["AltText"] = oImg.alt;
     args["ImgBorder"] = oImg.border;
     args["HorSpace"] = oImg.hspace;
     args["VerSpace"] = oImg.vspace;
     args["ImgAlign"] = oImg.align;
	 insimage(args)
	 }
	 if (oImg.tagName == "DIV")
	 {
	 alert('Boom')
     args["ID"] = oImg.id;
     args["Width"] = oImg.width;
     args["Height"] = oImg.height;
     args["Top"] = oImg.top;
     //args["Z-Index"] = oImg.z-index;
     args["Left"] = oImg.left;
	 insdiv(args)
	 }
	 window.status = "Selected HTML Object Type = " + oImg.tagName;
  	}
	}
	
function setMode(ditem, bMode){
	var sTmp;
  	isHTMLMode = bMode;
  	if (isHTMLMode){sTmp=eval(ditem).innerHTML;eval(ditem).innerText=sTmp;} 
	else {sTmp=eval(ditem).innerText;eval(ditem).innerHTML=sTmp;}
  	//document.focus();
	}
	
//Form stuff
function fninit()
	{
	for(i=0;i<document.all.length;i++) document.all(i).unselectable = "on";
	document.unselectable = "off";
	}
	
function Save(tname,did,ename,edata,ename2,edata2) 
	{
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	thiscopy = document.body.innerHTML;
	form1.html.value = document.body.innerHTML;
	form1.saveme.value = 1
	form1.table.value = tname
	form1.id.value = did
	form1.field_a_name.value = ename
	form1.field_a_data.value = edata
	form1.field_b_name.value = ename2
	form1.field_b_data.value = edata2

	//window.open(url,'savedata','width=200,height=60')
	document.form1.action = "savedata.asp";
	//document.form_1.save.value = 1;
	document.form1.submit();
	}

function savenews(did,data) 
	{
	window.open('savedatanews.asp?id=' + did + '&data='+ data);
	}

function savedata(did)
	{
		if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
		MWJ_findObj2('fid').value = did;
		
		title = MWJ_findObj2('title_' + did);
		if (title.type == "text"){
			MWJ_findObj2('ftitle').value = title.value;
		}else{
			//Remove all invalid HTML
			title.innerHTML = stripHTML(title.innerHTML);
			if (title.innerHTML.length > 100) {	
				iLength = title.innerHTML.length - 100
				alert("The length of your title is too long.\nPlease reduce it by " + iLength + " character(s).\n\nThank you.");
				return;
			}
			MWJ_findObj2('ftitle').value = title.innerHTML;
		}
		
		subtitle = MWJ_findObj2('subTitle_' + did);
		if (title.type == "text"){
			MWJ_findObj2('fsubtitle').value = subtitle.value;
		}else{
			//Remove all invalid HTML
			subtitle.innerHTML = stripHTML(subtitle.innerHTML);
			if (subtitle.innerHTML.length > 100) {	
				iLength = subtitle.innerHTML.length - 100
				alert("The length of your subtitle/venue is too long.\nPlease reduce it by " + iLength + " character(s).\n\nThank you.");
				return;
			}
			MWJ_findObj2('fsubtitle').value = subtitle.innerHTML;
		}			

		MWJ_findObj2('fdate').value = stripHTML(MWJ_findObj2('dateTime_' + did).value);

		if (MWJ_findObj2('content_' + did) != null){
			MWJ_findObj2('fcopy').value = MWJ_findObj2('content_' + did).innerHTML;
		}

		MWJ_findObj2('secret').submit();
	}

function deletedata(did)
	{
		if (did == "addNew") {
			window.history.go(-1);
			return;
		}else if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
			var agree=confirm("Are you sure you wish to continue?");
			if (agree) {
				MWJ_findObj2('fid').value = did;
				MWJ_findObj2('ftitle').value = 'DELETE';
				MWJ_findObj2('fsubtitle').value = 'DELETE';
				MWJ_findObj2('fdate').value = 'DELETE';
				MWJ_findObj2('fcopy').value = 'DELETE';
				MWJ_findObj2('secret').submit();
			}		
	}	
	
function sendmail()
	{
		//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
		if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}

		title = MWJ_findObj2('title_email' );
		MWJ_findObj2('ftitle').value = stripHTML(title.innerHTML);
		MWJ_findObj2('fcopy').value = MWJ_findObj2('content_email').innerHTML;
		MWJ_findObj2('secret').action = "contact.asp";
		MWJ_findObj2('secret').submit();
	}
	
function savepageblurb(page)
	{
	//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
		MWJ_findObj2('fid').value = 'pageBlurb_' + page;
		MWJ_findObj2('fcopy').value = MWJ_findObj2('pageBlurb_' + page).innerHTML;
		MWJ_findObj2('secret').submit();
	}
	
function deletepageblurb(page)
	{
		var agree=confirm("Are you sure you wish to continue?");
		if (agree) {
			MWJ_findObj2('fid').value = 'pageBlurb_' + page;
			MWJ_findObj2('fcopy').value = '';
			MWJ_findObj2('secret').submit();
			return;
		}	
	}		

function savendata(table,dtitle,dcopy,ddate)
	{
	//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
	//MWJ_findObj('fid').value=did;
	MWJ_findObj('ntable').value=table;
	MWJ_findObj('ntitle').value=dtitle;
	MWJ_findObj('ncopy').value=dcopy;
	MWJ_findObj('ndate').value=ddate;
	MWJ_findObj('nsecret').submit();
	}
	
function addnewasset(aid,ftype,title,file)
{
	url = 'saveasset.asp?addme=1&aid=' + aid + '&ftype=' + ftype + '&title=' + title + '&file=' + file
	window.open(url,'savedata','width=200,height=60')
}

function deleteasset(aid)
{
	url = 'saveasset.asp?deleteme=1&aid=' + aid
	window.open(url,'savedata','width=200,height=60')
}
	
function SearchAndReplace(Content, SearchFor, ReplaceWith) {
   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;
   if (SearchFor.length == 0)
      return;
   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;
      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;
      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);
   }
   return tmpOutput + tmpContent;
}

function findStyleRules() {
  //alert(document.styleSheets.length)
  a = 0;
  for (i = 0; i < document.styleSheets.length; i++) { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) {
	  myselector = 	document.styleSheets(i).rules(j).selectorText;
	  if ((myselector.indexOf(".")!=-1) && (myselector.indexOf(":")==-1))
	  {
	  	a = a + 1
	  	//alert(myselector)
		myselector = myselector.substring(1,myselector.length);
		document.form_1.select1.options[a] = new Option(myselector,myselector)
	  }
      //alert();
    }     
  }
}

function ShowTableBorders()
{
	document.SHOWBORDER = !document.SHOWBORDER;
	tableBorders();
}

function tableBorders()
{
	var aTables = document.getElementsByTagName("TABLE");
	for (i=0;i<aTables.length;i++)
	{
		if(document.SHOWBORDER && (aTables[i].border == 0))
		{            
			aTables[i].runtimeStyle.borderWidth = 1;
			aTables[i].runtimeStyle.borderColor = "#BBBBBB";
			aTables[i].runtimeStyle.borderStyle = "dotted";
			aTables[i].runtimeStyle.borderCollapse = "collapse";
			window.status = "Table Borders ON"
		}
		if(!document.SHOWBORDER && (aTables[i].border == 0))
		{            
			aTables[i].runtimeStyle.cssText = ''; 
			window.status = "Table Borders OFF"
		}
	}
}
function getstyles()
{
	document.form_1.select1.options.length = 0;
	setTimeout("findStyleRules()",1000);
	document.form_1.select1.options[0] = new Option("Style...","")
}


function wordClean (myelement) {
	var D = MWJ_findObj2(myelement).innerHTML;

	if ((D.indexOf('class=Mso') >= 0) || (D.indexOf('style="') >= 0) ||  (D.indexOf('<FONT') >= 0)) {
		// make one line
		D = D.replace(/\r\n/g, ' ').
			replace(/\n/g, ' ').
			replace(/\r/g, ' ').
			replace(/\&nbsp\;/g,' ');

		// keep tags, strip attributes
		D = D.replace(/ class=[^\s|>]*/gi,'').
			//replace(/<p [^>]*TEXT-ALIGN: justify[^>]*>/gi,'<p align="justify">').
			replace(/ style=\"[^>]*\"/gi,'').
			replace(/ align=[^\s|>]*/gi,'');

		//clean up tags
		D = D.replace(/<b [^>]*>/gi,'<b>').
			replace(/<i [^>]*>/gi,'<i>').
			replace(/<p [^>]*>/gi,'<li>').
			replace(/<li [^>]*>/gi,'<li>').
			replace(/<ul [^>]*>/gi,'<ul>');

		// replace outdated tags
		D = D.replace(/<b>/gi,'<strong>').
			replace(/<\/b>/gi,'</strong>');

		// mozilla doesn't like <em> tags
		D = D.replace(/<em>/gi,'<i>').
			replace(/<\/em>/gi,'</i>');

		// kill unwanted tags
		D = D.replace(/<\?xml:[^>]*>/g, '').       // Word xml
			replace(/<\/?st1:[^>]*>/g,'').     // Word SmartTags
			replace(/<\/?[a-z]\:[^>]*>/g,'').  // All other funny Word non-HTML stuff
			replace(/<\/?font[^>]*>/gi,'').    // Disable if you want to keep font formatting
			replace(/<\/?span[^>]*>/gi,' ').
			replace(/<\/?div[^>]*>/gi,' ').
			replace(/<\/?h[1-6][^>]*>/gi,' ').
			replace(/<\/?pre[^>]*>/gi,' ');

		//remove empty tags
		//D = D.replace(/<strong><\/strong>/gi,'').
		//replace(/<i><\/i>/gi,'').
		//replace(/<P[^>]*><\/P>/gi,'');

		// nuke double tags
		oldlen = D.length + 1;
		while(oldlen > D.length) {
			oldlen = D.length;
			// join us now and free the tags, we'll be free hackers, we'll be free... ;-)
			D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
				replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
		}
		D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
			replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');

		// nuke double spaces
		D = D.replace(/  */gi,' ');

		MWJ_findObj2(myelement).innerHTML = D;
	}
	
}

var myTimer = null;

function wordCleaner () {
	  	if (isHTMLMode)
		{
			alert("Please uncheck 'Edit HTML'");
			event.returnValue = false;
		}else{
			window.setTimeout ("wordClean (contentSelected)",10);
		}
	}
	
function stripHTML(html) {
   // Remove all SPAN tags
	html = html.replace(/<\/?\s*SPAN[^>]*>/gi, "" );
    // Remove all DIV tags
	html = html.replace(/<\/?\s*DIV[^>]*>/gi, "" );
    // Remove all FONT tags
	html = html.replace(/<\/?\s*FONT[^>]*>/gi, "" );
    // Remove all UL tags
	html = html.replace(/<\/?\s*UL[^>]*>/gi, "" );
    // Remove all OL tags
	html = html.replace(/<\/?\s*OL[^>]*>/gi, "" );
    // Remove all LI tags
	html = html.replace(/<\/?\s*LI[^>]*>/gi, "" );
    // Remove all TABLE tags
	html = html.replace(/<\/?\s*TABLE[^>]*>/gi, "" );
    // Remove all TH tags
	html = html.replace(/<\/?\s*TH[^>]*>/gi, "" );
    // Remove all TR tags
	html = html.replace(/<\/?\s*TR[^>]*>/gi, "" );
    // Remove all TD tags
	html = html.replace(/<\/?\s*TD[^>]*>/gi, "" );
    // Remove all HR tags
	html = html.replace(/<\/?\s*HR[^>]*>/gi, "" );
    // Remove all STYLE tags & content
	//html = html.replace(/<\/?\s*STYLE[^>]*>(.|[\n\r\t])*<\/\s*STYLE\s*>/gi, "" );
	html = html.replace(/<\s*STYLE[^>]*>(.|[\n\r\t])*<\/\s*STYLE\s*>/gi, "" );
	// Remove Class attributes
	html = html.replace(/<\s*(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	// Remove Style attributes
	html = html.replace(/<\s*(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
	// Remove Lang attributes
	html = html.replace(/<\s*(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	// Remove XML elements and declarations
	html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
	// Remove Tags with XML namespace declarations: <o:p></o:p>
	html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
	// Replace the &nbsp;
	html = html.replace(/&nbsp;/, " " );
	
	// Transform <p><br /></p> to <br>
	//html = html.replace(/<\s*p[^>]*>\s*<\s*br\s*\/>\s*<\/\s*p[^>]*>/gi, "<br>");
	html = html.replace(/<\s*p[^>]*><\s*br\s*\/?>\s*<\/\s*p[^>]*>/gi, "<br>");
	
	// Remove <P> 
	html = html.replace(/<\s*p[^>]*>/gi, "");
	
	// Replace </p> with <br>
	html = html.replace(/<\/\s*p[^>]*>/gi, "<br>");
	
	// Remove any <br> at the end
	html = html.replace(/(\s*<br>\s*)*$/, "");
	
	return html;

}

function showToolTip (myID,type) {
	var x = 30;
	var y= 25;

	while(myID.tagName!='HTML'){ 
       x+=myID.offsetLeft; 
       y+=myID.offsetTop; 
       myID=myID.offsetParent; 
    }

	var tip = MWJ_findObj2('toolTip');
	tip.innerHTML  = "Enter new " + type + " here";
	if(tip.style.display != "block"){ 
		tip.style.top = y;
		tip.style.left = x;
		tip.style.display = "block";
	}
	
	timerID  = setTimeout("hideToolTip()", 1500);

}

function hideToolTip () {
	
	var tip = MWJ_findObj2('toolTip');
	if (tip.style.display != "none"){ 
		tip.style.display = "none";
	}
}

function doHelp () {
	var d = "d";
}

  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
