//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

function document.onreadystatechange(){
  	document.designMode="On"
	}
	
function cmdExec(cmd,opt) {
  	if (isHTMLMode)
	{
		alert("Please uncheck 'Edit HTML'");
		return;
	}
  	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(arguments) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	if (!arguments["ImgUrl"])
		{
		el = document.selection.createRange();
		//alert(el.HTML) 
		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);
		el.pasteHTML('<img src="' + thisurl + '" border=' + thisborder + ' align=' + thisalign + '>');
		}
	}
	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 editimage(obj)
{
	//alert(obj.src);
	//showfilechoose('images','0px','70px');
	window.oldimg = document.selimg;
	window.showModelessDialog('imagebrowser.asp',window,'dialogWidth=270px;dialogHeight=313px;unadorned:yes;status:no;scroll:no;');
}

function setimage(url)
{
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	myobj = dialogArguments.document.selimg;
	if (myobj != null)
	{
		myobj.src = 'assets/images/' + url;
	}
	else
	{
		//alert(dialogArguments.document.myobj);
		el = dialogArguments.document.myobj;
		//alert(myobj);
		dialogArguments.window.status = 'Inserting new image ' + url;
		//div = dialogArguments.document.eselect;
		
		//el = dialogArguments.document.createRange();
		el.pasteHTML('<img src="assets/images/' + url + '">');
		dialogArguments.document.selimg = null;
	}
}

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;
	document.selimg = null;
	document.myobj = null;
  	arr=null;
  	args=new Array();
  	if (isHTMLMode){return;}
	el = document.selection.createRange();
	el2 = document.selection.type;
	el3 = document.selection.type.style;
	//alert(el3)
	status = 'No Selected HTML Object, ' + el2;
	if(el2 == "Text"){
		document.myobj = el;
		//el.pasteHTML('Hello');
	}
	if(el2 == "Control"){
     var oImg = document.selection.createRange().item(0);
	 if (oImg.tagName == "IMG")
	 {
	 document.isel = oImg;		
     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)
	 }
	 status = "Selected HTML Object Type = " + oImg.tagName;
	 document.selimg = oImg;
  	}
	
	window.status = status;
	}
	
	
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
	//session("html")
	if (ename != 0)
	{
	//url = 'savedata.asp?saveme=1&id=' + did + '&table=' + tname + '&field_a_name=' + ename + '&field_a_data=' + edata + '&field_b_name=' + ename2 + '&field_b_data=' + edata2 + '&html=' + thiscopy;	
	}
	else
	{
	//url = 'savedata.asp?saveme=1&id=' + did + '&table=' + tname + '&html=' + thiscopy;
	}
	//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 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 = 2;
			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 delcontact(id)
{
	if (confirm("Really delete this contact?"))
	{
		window.open('delcontact.asp?id=' + id,'delwin','width=100,height=100');
	}
}

function MWJ_findObj( oName, oFrame, oDoc ) {
	if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else { oDoc = window.document; } }
	if( oDoc[oName] ) { return oDoc[oName]; } if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }
	if( oDoc.getElementById && oDoc.getElementById(oName) ) { return oDoc.getElementById(oName); }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; x < oDoc.anchors.length; x++ ) { if( oDoc.anchors[x].name == oName ) { return oDoc.anchors[x]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
		var theOb = MWJ_findObj( oName, null, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	if( !oFrame && window[oName] ) { return window[oName]; } if( oFrame && oFrame[oName] ) { return oFrame[oName]; }
	for( var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++ ) {
		var theOb = MWJ_findObj( oName, oFrame.frames[x], oFrame.frames[x].document ); if( theOb ) { return theOb; } }
	return null;
}

		function savedata(table,did,dtitle,dcopy,lastpage)
		{
			//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
			MWJ_findObj('fid').value=did;
			MWJ_findObj('ftable').value=table;
			MWJ_findObj('ftitle').value=dtitle;
			MWJ_findObj('fcopy').value=dcopy;
			MWJ_findObj('lastpid').value=lastpage;
			MWJ_findObj('secret').submit();
		}

function savendata(table,did,dtitle,dcopy,ddate)
		{
			//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
			//MWJ_findObj('fid').value=did;
			MWJ_findObj('fid').value=did;
			MWJ_findObj('ftable').value=table;
			MWJ_findObj('ftitle').value=dtitle;
			MWJ_findObj('fcopy').value=dcopy;
			MWJ_findObj('fdate').value=ddate;
			MWJ_findObj('secret').submit();
		}

function selecta(did)
		{
			//window.toolbar.innerText = 'Editing: ' + did;
			document.eselect = did;
			window.status.value = did; 
			getprop();
			//alert(document.activeElement());
			//window.toolbar.document.etitle.innerText = did;
			//eval(document.eselect).innerText = 'Boo';
			//alert(document.eselect);
		}
		
document.editon = true;
document.eselect = 'dummydiv';
document.isel = null;
document.selimg = null;
document.myobj = null;

function cleanup(myelement)
		{
		//alert(myelement);
		myhtml = MWJ_findObj(myelement).innerHTML;
		MWJ_findObj('detag').value=myhtml;
		MWJ_findObj('requester').value = myelement;
		MWJ_findObj('eaterform').submit();
		}
		
		
// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has 
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position 
        // reflects the position from the top/left of the screen the 
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no 
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard 
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}


	function showfilechoose(foldername,leftpos,toppos)
	{
		myname = "filechooser" + foldername;
		if (leftpos == 'xm')
		{
			leftpos = (xMousePos + 10) + 'px';
		}
		myobj = MWJ_findObj(myname);
		alert(myname);
		myobj.style.visibility = 'visible';
		myobj.style.display = 'block';
		myobj.style.left = leftpos;
		myobj.style.top = toppos;
	}

