
    var m_selectedText;         // holds the textarea's selected text (as an object)
    var m_sender;
    var m_showBorders = true;
    var tbID = 0;
    var detect = navigator.userAgent.toLowerCase();
    var OS,browser,version,total,thestring;
    var currObj;
    var controlBarDelta = 0;
    var http_request = false;
    var box_http_request = false;

    //document.body.onload=init_sp();

window.onscroll=updateScrollArrow;

function init_sp() {
  if (document.body.scrollHeight > document.body.clientHeight) {
    updateScrollArrow();
  }
}


function updateScrollArrow() {
  var sObj=document.getElementById('scrollArrow');
  var flag=false;
  
  if (document.body.scrollHeight > (document.body.scrollTop+document.body.clientHeight)) {
    flag=true;
  }

  if (flag) {
    sObj.style.display="block";
  } else {
    sObj.style.display="none";
  }
  sObj.style.top=(parseInt(document.body.scrollTop)+parseInt(document.body.clientHeight)-30)+"px";
  sObj.style.left=(parseInt(document.body.clientWidth)-100)+"px";
  
  //sObj.innerHTML=document.body.scrollHeight+" - "+(document.body.scrollTop+document.body.clientHeight);
  
}


function scrollFunc() {

    try {

        var obj = document.getElementById('controlBar');

        /*document.getElementById('dump').innerHTML="["+Math.abs(parseInt(obj.style.top) - parseInt(document.body.scrollTop))+"]";

        if (Math.abs(parseInt(obj.style.top) - parseInt(document.body.scrollTop)) >= 8) {

            if (parseInt(obj.style.top) > parseInt(document.body.scrollTop)) {
                controlBarDelta=-6;
            } else {
                controlBarDelta=6;
            }


            obj.style.top = parseInt(obj.style.top) + controlBarDelta;

        } else {*/
           controlBarDelta=0;
           obj.style.top = parseInt(document.body.scrollTop);
        //}

    } catch(e) {
     //alert("Error "+e.description);
    }
}


   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

      //alert("URL:["+url+"] PARAMS:["+parameters+"]");

      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
             var editObj = document.getElementById('editPane');
             //alert('Data was saved successfully');
             //editObj.innerHTML = http_request.responseText;
             //alert(http_request.responseText.substr(0,5));
             if (http_request.responseText.substr(0,5) == "Error") {
                alert(http_request.responseText.substr(6));
             } else {
                alert(http_request.responseText);
             }
             m_showBorders=true;
             toggleTableBorders();

         } else {
            alert('There was a problem saving, please try again. '+http_request.status);
         }
      } else {
            //alert('Argh!');

            //document.forms['SitePULSE'].submit();
      }
   }

        function save(pageID,pageTitle,saveTo){
            //alert(pageID+' '+sid);
            var editObj = document.getElementById('editPane');

            //m_showBorders=false;
            //toggleTableBorders();
            //LimitTableWidth();
            var poststr="saveto="+saveTo+"&data="+escape(encodeURIComponent(document.getElementById("editPane").innerHTML))+"&page_id="+pageID+"&page_title="+pageTitle;
            makePOSTRequest('save.php', poststr);
            return true;
        }

        function checkChanges() {

            try {
                var editObj = document.getElementById('editPane');
                var htmlObj = document.getElementById('htmlPane');

                var oldContent = htmlObj.value;

                htmlObj.value = editObj.innerHTML;

                //alert("["+htmlObj.value+"] ["+oldContent+"]");

                if (htmlObj.value != oldContent) {
                    return true;
                } else {
                    return false;
                }
            } catch (error) {

            }


        }

        function redirect(url,saveIt) {
            if (saveIt) {

                contentChanged=checkChanges();

                if (contentChanged) {
                    confirmSave(url);
                } else {
                    document.location=url;
                }

            } else {
                document.location=url;
            }
        }

        function confirmSave(url) {
            var saveIt=false;

            saveIt=confirm("Do you want to save changes?");

            if (saveIt) {
                save(url);
            } else {
                document.location=url;
            }

        }

        function closeEditor(returnURL) {
            document.location=returnURL;
        }
        
        function publishPage(pageID,pageTitle) {
            save(pageID,pageTitle);
            document.location="page_management.php?action=PUBLISH&pageid="+pageID;
        }

        function imageUpload()
        {
            //var complete = showModalDialog("/imageupload.php", window, 'dialogWidth:480px;dialogHeight:420px;help:0;status:0;resizeable:1;');
            window.open('imageupload.php','newWin','width=580px,height=550px,directories=no,fullscreen=no,location=no,menubar=no,scrollbars=yes');

        }
        function soundUpload()
        {
            //var complete = showModalDialog("/imageupload.php", window, 'dialogWidth:480px;dialogHeight:420px;help:0;status:0;resizeable:1;');
            window.open('soundupload.php','newWin','width=700px,height=550px,directories=no,fullscreen=no,location=no,menubar=no,scrollbars=yes');

        }


        function IsPopupBlocker() {
            try {
                var oWin = window.open("","testpopupblocker","width=1,height=1,top=10,left=-100");
                if (oWin==null || typeof(oWin)=="undefined") {
                    alert("You are using a popup blocker, please allow popups from this site for SitePULSE to work correctly.");
                } else {
                    oWin.close();
                }
            } catch (error) {

            }
        }


        function load_event()
        {
            //alert('loading');
            try {
/*                if (checkIt('konqueror'))
                {
                    browser = "Konqueror";
                    OS = "Linux";
                }
                    else if (checkIt('safari')) browser = "Safari"
                    else if (checkIt('omniweb')) browser = "OmniWeb"
                    else if (checkIt('opera')) browser = "Opera"
                    else if (checkIt('webtv')) browser = "WebTV";
                    else if (checkIt('icab')) browser = "iCab"
                    else if (checkIt('msie')) browser = "Internet Explorer"
                    else if (!checkIt('compatible'))
                    {
                        browser = "Netscape Navigator"
                        version = detect.charAt(8);
                    }
                    else browser = "An unknown browser";

                if (!version) version = detect.charAt(place + thestring.length);
*/
//                IsPopupBlocker();

                var editObj = document.getElementById('editPane');
                if (checkIt('firefox')) {
                //  alert("firefox!");
                  editObj.contentDocument.designMode="on";
                  editObj.contentEditable="true";                
                //  editObj.contentDocument.designMode="on";                
                }
//                var htmlObj = document.getElementById('htmlPane');


/*                if (browser == "Internet Explorer") {
                    editObj.contentEditable='true';
                } else {
                    alert("SitePULSE does not currently support browsers other than MS Internet Explorer");
                    document.location="/";
                }*/
//                document.execCommand("2D-Position", true, true);
                //alert("set 2D-Position to TRUE!");
//                document.execCommand("MultipleSelection", true, true);
//                document.execCommand("LiveResize", true, true);



                //editObj.body.ondrop    = function() { sp_event(objname, 100); }
                //editObj.body.oncut     = function() { sp_event(objname, 100); }
                //editObj.body.onpaste   = function() { sp_event(objname, 100); }
                //editObj.body.onblur    = function() { sp_event(objname, -1); }

                m_showBorders=true;
                toggleTableBorders();

                //alert(editObj.id);
                editObj.onkeydown      = function() { update_event(); }
                editObj.onkeypress     = function() { update_event(); }
                editObj.onkeyup        = function() { update_event(); }
                editObj.onmouseup      = function() { update_event(); }


            } catch(error) {
                alert("Loading Error: "+error);
                //if (checkIt('firefox')) {
                //  editObj.contentDocument.designMode="on";                
                // editObj.contentDocument.designMode="on";                
                //}
            }


        }


        function showCombos() {
            try {
                var e=document.getElementsByTagName("select");

                for(var i=0;i<e.length;i++)
                {
                    e[i].style.visibility="visible";

                }
            } catch(error) {

            }

        }


        function checkIt(string)
        {
            place = detect.indexOf(string) + 1;
            thestring = string;
            return place;
        }




        function LimitTableWidth()
        {
            try {
                // Function that prevents the user from creating tables that are wider than the 'editPane' container
                var e=document.getElementById('editPane').getElementsByTagName("table");
                for (var i=0; i<e.length; i++)
                {
                    if (e[i].width > 630)
                    {
                        e[i].width = 630;
                        e[i].style.width="630px";
                    }
                }
            } catch(error) {

            }
        }

        function removeTableBorder() {

        }

        function toggleTableBorders()
        {
            // function that toggles the shaded borders of tables with a border.width of 0
            var e=document.getElementById('editPane').getElementsByTagName("table");

            if (m_showBorders)
            {
                for(var i=0;i<e.length;i++)
                {
                    //alert(e[i].id+" "+e[i].border);
                    if (e[i].border==0)
                    {
                        //alert("setting border");
                        e[i].runtimeStyle.border="solid 2px #cccccc";
                        e[i].runtimeStyle.borderCollapse="collapse";

                        var f=e[i].getElementsByTagName("td");

                        for(var j=0;j<f.length;j++)
                        {
                            f[j].runtimeStyle.border="solid 2px #cccccc";
                        }
                    }
                }
            }
            else {
                for(var i=0;i<e.length;i++)
                {
                    //alert(e[i].id+" "+e[i].border);
                    if (e[i].border==0)
                    {
                        //alert("setting border");
                        e[i].runtimeStyle.border="none";
                        e[i].runtimeStyle.borderCollapse="collapse";

                        var f=e[i].getElementsByTagName("td");

                        for(var j=0;j<f.length;j++)
                        {
                            f[j].runtimeStyle.border="none";
                        }
                    }
                }
            }

            m_showBorders = !(m_showBorders);
        }

        function selFonts(){
            var editObj = document.getElementById('editPane');
            var fontObj = document.getElementById("Font");

            switch(fontObj.selectedIndex){
                case 1:
                    // Times New Roman
                    document.execCommand("FontName", false, "times");
                    break;
                case 2:
                    // Arial
                    document.execCommand("FontName", false, "arial");
                    break;
                case 3:
                    // Cursive
                    document.execCommand("FontName", false, "cursive");
                    break;
                case 4:
                    // Courier
                    document.execCommand("FontName", false, "courier");
                    break;
                case 5:
                    // Tahoma
                    document.execCommand("FontName", false, "tahoma");
                    break;
                case 6:
                    // Verdana
                    document.execCommand("FontName", false, "verdana");
                    break;
                case 7:
                    // Verdana
                    document.execCommand("FontName", false, "corbel");
                    break;
            }
            fontObj.selectedIndex = 0;
        }


        function selFontSize(){
            var editObj = document.getElementById('editPane');
            var fontSizeObj = document.getElementById('FontSize');

            var fontSize = fontSizeObj[fontSizeObj.selectedIndex].value;

            if (fontSize>0){
                document.execCommand("FontSize", false, fontSize);
            }

            fontSizeObj.selectedIndex = 0;          // don't want anything to be selected in the FontSize combobox
        }

        function insertTable(){
            var tableArr = showModalDialog("tabledata.html", window, 'dialogWidth:450px;dialogHeight:220px;help:0;status:0;resizeable:1;');
            var editObj = document.getElementById('editPane');
            var tmpDate = new Date();
            var newTable = editObj.document.createElement('TABLE');
            newTable.id="tbl"+tmpDate.getTime();

            for(y=0; y<tableArr['rows']; y++) {
                var newRow = newTable.insertRow();
                for(x=0; x<tableArr['cols']; x++) {
                    var newCell = newRow.insertCell();
                    newCell.id=newTable.id+y+x;
                    newCell.innerHTML="&nbsp;";
                    //var cellText = editObj.document.createTextNode("&nbsp;");
                    //newCell.appendChild(cellText);

                    if (tableArr['border']==0)
                    {
                        // if the tableBorder is 0 then make the border faded & dashed
                        newCell.runtimeStyle.border="solid 1px #cccccc";
                    }

                    switch (tableArr['hAlignment'])
                    {
                        case "0":       // left
                            newCell.align="left";
                            break;
                        case "1":       // right
                            newCell.align="right";
                            break;
                        case "2":       // center
                            newCell.align="center";
                            break;
                        default:
                            newCell.align="left";
                            break;
                    }

                    switch (tableArr['vAlignment'])
                    {
                        case "0":   // top
                            newCell.valign="top";
                            break;
                        case "1":   // center
                            newCell.valign="center";
                            break;
                        case "2":   // bottom
                            newCell.valign="bottom"
                            break;
                        default:
                            newCell.valign="center"
                            break;
                    }

                    // set cell width
/*                    if (tableArr['widthType'] == 0) {       // percent
                        newCell.style.width = (tableArr['width'] / tableArr['cols']) + "%";
                    }
                    else {                                  // pixels
                        newCell.style.width = tableArr['width'] / tableArr['cols'];
                    }
*/
                }
            }
            if (tableArr['border']==0)
            {
                newTable.runtimeStyle.border="solid 1px #cccccc";
            }
      			if (tableArr['widthType'] == 0) {
      				newTable.style.width = tableArr['width'] + "%";
      			}
      			else {
      				newTable.style.width = tableArr['width'];
      			}

            newTable.border = tableArr['border'];
            newTable.cellspacing = tableArr['cellSpacing'];
            newTable.cellpadding = tableArr['cellPadding'];

        //  alert(newTable.outerHTML);
            editObj.focus();

            sel = editObj.document.selection.createRange();
            sel.pasteHTML(newTable.outerHTML);

            m_showBorders=true;
            toggleTableBorders();

        }

        function undo()
        {
            document.execCommand("Undo", false, null);
        }
        function redo()
        {
            document.execCommand("Redo", false, null);
        }
        function bold()
        {
            document.execCommand("Bold", false, null);
        }
        function italic()
        {
            document.execCommand("Italic", false, null);
        }
        function colour()
        {
            m_sender = "ForeColor";
            document.getElementById('pickerTitle').innerHTML="Foreground Colour";
            m_selectedText = document.selection.createRange();
            showColourPicker();
        }
        function backcolour()
        {
            m_sender="BackColor";
            document.getElementById('pickerTitle').innerHTML="Background Colour";
            m_selectedText = document.selection.createRange();
            showColourPicker();
        }
        function underline()
        {
            document.execCommand("UnderLine", false, null);
        }
        function justifyfull()
        {
            document.execCommand("JustifyFull", false, null);
        }
        function justifycenter()
        {
            document.execCommand("JustifyCenter", false, null);
        }
        function justifyleft()
        {
            document.execCommand("JustifyLeft", false, null);
        }
        function justifyright()
        {
            document.execCommand("JustifyRight", false, null);
        }
        function orderedList()
        {
            document.execCommand("InsertOrderedList", false, null);
        }
        function unorderedList()
        {
            document.execCommand("InsertUnorderedList", false, null);
        }
        function indent()
        {
            document.execCommand("Indent", false, null);
        }
        function outdent()
        {
            document.execCommand("Outdent", false, null);
        }
        function superScript()
        {
            document.execCommand("superscript", false, null);
        }
        function subScript()
        {
            document.execCommand("subscript", false, null);
        }

function addMovie() {
    var editObj = document.getElementById('editPane');
    var tmpDate = new Date();

    var tmpID="movie_"+tmpDate.getTime();
    
    editObj.focus();
    sel = editObj.document.selection.createRange();
    sel.pasteHTML('<div id="'+tmpID+'">Movie</div>');

  	//var s1 = new SWFObject("mediaplayer.swf","mediaplayer","320","240","7");
  	//s1.addVariable("width","320");
  	//s1.addVariable("height","240");
  	//s1.addVariable("file","\/Test002_0003.flv");
    //s1.write(tmpID);

    var movieObject="";
    
    movieObject+="<embed src=\"mediaplayer.swf\" width=\"320\" height=\"240\" allowscriptaccess=\"always\" flashvars=\"file=Test002_0003.flv\"/>";

    document.getElementById(tmpID).innerHTML=movieObject;
    //alert(document.getElementById(tmpID).innerHTML+"\n\n"+movieObject);
  	
}

        function addSound() {
            var soundFile="";
            var editObj = document.getElementById('editPane');

            ModalWin.init();
            editObj.focus();
				    sel = editObj.document.selection.createRange();
				    
            ModalWin.show('soundupload.php','600px','450px', sel, 'Sound Gallery',true,'EDITOR','SND');

        }
        

    /*  function addSound() {
            var soundFile="";
            var editObj = document.getElementById('editPane');

            var soundFile = showModalDialog("selectsound.php", window, 'dialogWidth:400px;dialogHeight:200px;help:0;status:0;resizeable:1;');

            if (soundFile != null) {
                editObj.focus();

                var soundObject="";
                soundObject+="<EMBED type='application/x-mplayer2' pluginspage='http://www.microsoft.com/windows/windowsmedia/download/default.asp' ";
                soundObject+="filename='"+soundFile+"' ";
                soundObject+="Height='70' Width='280' displaysize='0' name='WMPlay' CACHE='TRUE' controller='1' showstatusbar='1'controller='1' showstatusbar='1'";
                soundObject+="bgcolor='black' AUTOPLAY='false'>\n";
                soundObject+="</EMBED>\n";

                //alert(soundObject);

                sel = editObj.document.selection.createRange();
                sel.pasteHTML(soundObject);

            }

        }*/


function addProductWidget(sid) {
            var widgetHTML = "";
            var editObj = document.getElementById('editPane');
            var url="widget.php";

            //widgetHTML = showModalDialog(url, window, 'dialogWidth:400px;dialogHeight:200px;help:0;status:0;resizeable:1;');
            widgetHTML = window.open(url,'newWin','width=580px,height=550px,directories=no,fullscreen=no,location=no,menubar=no,scrollbars=yes');

            /*if (widgetHTML != "") {
                editObj.focus();
                sel = editObj.document.selection.createRange();
                sel.pasteHTML(widgetHTML);
            }
            */

}

        function addPayMateButton() {
            var buttonHTML = "";
            var editObj = document.getElementById('editPane');

            var buttonHTML = showModalDialog("paymatebutton.html", window, 'dialogWidth:400px;dialogHeight:200px;help:0;status:0;resizeable:1;');

            if (buttonHTML != "") {
                editObj.focus();
                sel = editObj.document.selection.createRange();
                sel.pasteHTML(buttonHTML);

                /*try {
                    var e=document.getElementById('editPane').getElementsByTagName("input");
                    for (var i=0; i<e.length; i++)
                    {
                        e[i].style.visibility="hidden";
                        e[i].style.display="none";
                        e[i].style.width="0px";
                        e[i].style.height="0px";
                    }
                    var e=document.getElementById('editPane').getElementsByTagName("form");
                    for (var i=0; i<e.length; i++)
                    {
                        e[i].style.visibility="hidden";
                        e[i].style.display="none";
                        e[i].style.width="0px";
                        e[i].style.height="0px";
                    }
                } catch(error) {
                    alert('Error');
                } */


            }

        }

        function createLink()
        {
            var linkURL="";
            var editObj = document.getElementById('editPane');

            ModalWin.init();
            editObj.focus();
				    sel = editObj.document.selection.createRange();
				    
            ModalWin.show('insertlink.php','600px','450px', sel, 'Insert Link',true,"EDITOR");

            //var linkURL = showModalDialog("insertlink.html", window, 'dialogWidth:400px;dialogHeight:200px;help:0;status:0;resizeable:1;');

            //if (linkURL != null) {
            //editObj.focus();

            //sel = editObj.document.selection.createRange();
            //alert(linkURL);
            //sel.pasteHTML(linkURL);

        }


        function removeLink()
        {
            document.execCommand("UnLink", true);
        }

        function undo()
        {
            document.execCommand("UnDO", false, null);
        }

        function insertImageToList(objName) {
            obj=document.getElementById(objName);
            insertImage(obj,"LIST");

        }

        function insertImageToTextField(objName) {
            var obj=document.getElementById(objName);
            insertImage(obj,"TEXT");
        }
        
        function insertImageToEditor() {
            var editObj = document.getElementById('editPane');
            editObj.focus();
            var sel = editObj.document.selection.createRange();
            //sel.pasteHTML(strHTML);
            insertImage(sel,"EDITOR");
        }

        function insertImage(obj,rType)
        {
            ModalWin.init();

            ModalWin.show('imageupload.php','600px','450px', obj, 'Image View',true,rType,'IMG');

        }
        
        function viewHTML()
        {
            var editObj = document.getElementById('editPane');
            var htmlPane = document.getElementById('htmlPane');

            htmlPane.value = editObj.innerHTML;

            htmlPane.style.visibility="visible";
            htmlPane.style.display="block";
            htmlPane.style.width="700px";
            htmlPane.style.height="500px";

        }


        function showColourPicker() {
            var colourObj = document.getElementById('colourPicker');
            //colourObj.style.visibility="visible";
            colourObj.style.top = parseInt(event.y) + parseInt(document.body.scrollTop);
            colourObj.style.left = event.x;

            window.status = colourObj.style

            if (colourObj.style.visibility == "visible")
            {
                colourObj.style.visibility = "hidden";
            }
            else
            {
                colourObj.style.visibility = "visible";
            }
        }

		function setMyColour(newColour){
			var colourObj = document.getElementById('colourPicker');
			var editObj = document.getElementById('editPane');

      //alert(currObj.tagName+' '+m_sender);
      //alert(currObj.tagName=="TD");

      var tdNode = searchForParent("TD");

      //alert(tdNode+' '+tdNode.tagName+' '+m_sender);

			if (m_sender=="BackColor") {
          //alert(tdNode.style.backgroundColor);
    			tdNode.style.backgroundColor=newColour;
          //alert(tdNode.style.backgroundColor);
			} else {

			    m_selectedText.execCommand(m_sender, false, newColour);			// run backcolor change here instead
			    m_selectedText=null;

		    }

			//editObj.focus();
			//m_selectedText.select();

			colourObj.style.visibility="hidden";
		}



        function insertHorizRule() {

            var editObj = document.getElementById('editPane');
            editObj.focus();
            sel = editObj.document.selection.createRange();
            sel.pasteHTML("<hr>");

            //document.execCommand("InsertHorizontalRule", false, null);

        }

        function insertDIV() {
            var objDIV = document.createElement("DIV");
            var editObj = document.getElementById('editPane');

            objDIV.style.backgroundColor="#286AFE";
            objDIV.style.border="solid 1px #aaaaee";
            objDIV.style.width="200px";
            objDIV.style.top="10px";
            objDIV.style.left="10px";
            objDIV.style.position="absolute";
            objDIV.innerHTML="Drag the borders to move me around the screen.<br><br>Double-click me to edit my contents";

            editObj.appendChild(objDIV);
        }

        function insertBox() {
            var editObj = document.getElementById('editPane');
            var htmlObj = document.getElementById('htmlPane');
            var tmpDate = new Date();

            var strHTML="<div>";

            var tblID="box"+tmpDate.getTime();

            strHTML+="<table id=\"" + tblID + "\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"200\">";
            strHTML+="<tr>";
            strHTML+="<td id=\"" + tblID + "1\" width=\"11\" height=\"24\" style=\"font-size:8px;line-height:8px;\" background=\"images/box_1x1.gif\" border='0'></td>";
            strHTML+="<td id=\"" + tblID + "2\" height=\"24\" background=\"/images/box_1x3.gif\" border='0'></td>";
            strHTML+="<td id=\"" + tblID + "3\" width=\"21\" height=\"24\" style=\"font-size:8px;line-height:8px;\" background=\"images/box_1x5.gif\" border='0'></td>";
            strHTML+="</tr>";
            strHTML+="<tr>";
            strHTML+="<td id=\"" + tblID + "4\" width=\"11\" height=\"100%\"><img width=\"11\" height=\"100%\" src=\"/images/box_3x1.gif\" border='0'></td>";
            strHTML+="<td id=\"" + tblID + "5\" style=\"background-color:#ffffff;\" valign=\"top\">Edit this Cell</td>";
            strHTML+="<td id=\"" + tblID + "6\" width=\"21\" height=\"100%\"><img width=\"21\" height=\"100%\" src=\"/images/box_3x5.gif\" border='0'></td>";
            strHTML+="</tr>";
            strHTML+="<tr>";
            strHTML+="<td id=\"" + tblID + "7\" width=\"11\" height=\"15\"><img src=\"images/box_5x1.gif\" border='0'></td>";
            strHTML+="<td id=\"" + tblID + "8\" height=\"15\"><img width=\"100%\" height=\"15\"  src=\"images/box_5x3.gif\" border='0'></td>";
            strHTML+="<td id=\"" + tblID + "9\" width=\"21\" height=\"15\"><img src=\"images/box_5x5.gif\" border='0'></td>";
            strHTML+="</tr>";
            strHTML+="</table>";


            strHTML+="</div>";

            //alert(strHTML);

            editObj.focus();
            var sel = editObj.document.selection.createRange();
            sel.pasteHTML(strHTML);

            //htmlObj.value = editObj.innerHTML;

            m_showBorders=true;
            toggleTableBorders();

            //sel=null;

        }

function insertRow() {
    //var obj=document.getElementById('tbl01');

    obj=currObj;

    try {
        if (obj.tagName=="TABLE") {

            var rows=obj.rows.length;
            var newRow=obj.insertRow(rows);
            var maxCells = obj.rows[0].cells.length;

            for (var cells=0; cells<maxCells; cells++) {
                if (cells < 100) {
                    var newCell=newRow.insertCell();
                    newCell.id=obj.id+(rows)+(cells);
                } else {
                    cells=obj.cells.length;
                }
            }
            m_showBorders=true;
            toggleTableBorders();

        } else {
            alert("You can only add rows to a table!");
        }

    } catch(e) {
        alert("You must select a table first!");
    }
}
function insertColumn() {
    //var obj=document.getElementById('tbl01');

    obj=currObj;
    var tmpDate = new Date();
    try {
        if (obj.tagName=="TABLE") {

            var rows=obj.rows.length;

            for (var i=0; i<rows; i++) {
                if (i < 100) {
                    var newCell=obj.rows[i].insertCell();
                    newCell.id="td"+tmpDate.getTime()+(i);
                    newCell.innerHTML="&nbsp;&nbsp;";
                } else {
                    i=rows;
                }
                //alert("i:"+i+" rows:"+rows);
            }
            m_showBorders=true;
            toggleTableBorders();

        } else {
            alert("You can only add columns to a table!");
        }

    } catch(e) {
        alert("You must select a table first!");
    }
}


function insertCell() {
    //var obj=document.getElementById('tbl01');

    var tmpDate = new Date();
    //alert(tmpDate.getTime());
    try {

    obj=currObj;

        if (obj.tagName=="TD") {

        //document.getElementById('dump').innerHTML="["+obj.id+"]"+"["+obj.parentNode.tagName+"]"+"";

                var tmpCell=obj.parentNode.insertCell();
                //tmpCell.style.backgroundColor="#ff0000";
                tmpCell.id="td"+tmpDate.getTime()+random(9999);

                tmpCell.innerHTML="&nbsp;&nbsp;";

            m_showBorders=true;
            toggleTableBorders();
        } else {
            alert("You can only add columns to a table!");
        }
    } catch(e) {
        alert("You must select a table first!"+e.toString());
    }
}

function deleteRow() {
    obj=currObj;

    try {

        if (obj.tagName=="TD") {

            //document.getElementById('dump').innerHTML="["+obj.id+"]"+"["+obj.parentNode.tagName+"]"+"";

            obj.parentNode.parentNode.removeChild(obj.parentNode);

            m_showBorders=true;
            toggleTableBorders();
        } else {
            alert("You select a cell in the table first!");
        }
    } catch(e) {
        alert("You select a cell in the table first!");
    }
}

function deleteColumn() {
    obj=currObj;

    //try {

        if (obj.tagName=="TD") {

            //document.getElementById('dump').innerHTML="["+obj.id+"]"+"["+obj.parentNode.tagName+"]"+"";
            var tbl=obj.parentNode.parentNode.parentNode;
            //alert(tbl.tagName);
            var rows=tbl.rows.length;
            var cols=tbl.rows[0].cells.length;

            //alert("rows:"+rows+" cols:"+cols);

            var maxCells=rows*cols;
            var colToDelete=0;


            for (var i=0; i<maxCells; i++) {
                if (tbl.cells[i].id==obj.id) {
                    colToDelete=(i % cols);
                }
            }

            //alert("Deleting Column : ["+(colToDelete-1)+"]");

            for (var i=0; i<rows; i++) {
                    tbl.rows[i].deleteCell(colToDelete);
            }


            //obj.parentNode.parentNode.removeChild(obj.parentNode);

            m_showBorders=true;
            toggleTableBorders();
        } else {
            alert("You select a cell in the table first! [err01]");
        }
    //} catch(e) {
        //alert("You select a cell in the table first! [err02]");
    //}
}

function deleteCell() {
    obj=currObj;

    try {

        if (obj.tagName=="TD") {

            //document.getElementById('dump').innerHTML="["+obj.id+"]"+"["+obj.parentNode.tagName+"]"+"";

            obj.parentNode.removeChild(obj);

            m_showBorders=true;
            toggleTableBorders();
        } else {
            alert("You select a cell in the table first!");
        }
    } catch(e) {
        alert("You select a cell in the table first!");
    }
}


function mMove() {
    //document.getElementById('dump').innerHTML="obj:"+event.srcElement.id;

}

function mDown() {
    try {

    currObj=event.srcElement;


    } catch(e) {
        //alert("Error!!");
    }
}

function searchForParent(s_tagName) {

var node=currObj;

  while (node.tagName!="BODY") {

    if (node.tagName == s_tagName) {
      return node;
    }
    
    node=node.parentNode;

  }

  return false;
}


function doCellEdit() {

    var inURL="celledit.php?cellObj="+currObj.id;

    showModalDialog(inURL,window.self,'dialogWidth:500px;dialogHeight:400px;help:0;status:0;resizeable:0;');
}



function update_event(objname) {
    var editObj = document.getElementById('editPane');
    //var htmlObj = document.getElementById('htmlPane');
    var editEvent = window.event;
try{
    if (editEvent && editEvent.keyCode) {
      var key       = editEvent.keyCode;
      var ctrlKey   = editEvent.ctrlKey;
      var altKey    = editEvent.altKey;
      var shiftKey  = editEvent.shiftKey;

      if (key == 16 || key == 17 || key == 18) { return; }


       // cancel ENTER key and insert <BR> instead
       if (key == 13 && editEvent.type == 'keypress') {
       
            var sel = editObj.document.selection.createRange();
            
            if (sel.queryCommandState('InsertUnorderedList')) {
              return;

            } else if (sel.queryCommantState('InsertOrderedList')) {
              return;

            } else {

              editEvent.returnValue = false;
              editEvent.cancelBubble = true;

              editObj.focus();
              var sel = editObj.document.selection.createRange();
              sel.collapse(false); // move to end of range
              sel.select();
              sel.pasteHTML("<BR>");

            }
      }
      if (ctrlKey && (key == 122 || key == 90)) {     // catch ctrl-z (UNDO)
//      TODO: Add our own undo/redo functionality
//      editEvent.cancelBubble = true;
        return;
      }
      if ((ctrlKey && (key == 121 || key == 89)) ||
          ctrlKey && shiftKey && (key == 122 || key == 90)) {     // catch ctrl-y, ctrl-shift-z (REDO)
//      TODO: Add our own undo/redo functionality
//      editEvent.cancelBubble = true;
        return;
      }
    }


} catch(e) {
    //TODO: sort out the error
}

    //htmlObj.value = editObj.innerHTML;
    updateToolbar();

}


function updateToolbar() {

    try {

        var editObj = document.getElementById('editPane');
        //alert('Getting command state...');

        //editObj.queryCommandValue('Bold');
        //alert("Creating Text Range...");
        var sel = editObj.document.selection.createRange();

        var btnList = Array('Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','JustifyFull','InsertOrderedList','InsertUnorderedList');

        for (i=0; i<btnList.length; i++) {
            var btnObj = document.getElementById(btnList[i]);

            var cmdActive = sel.queryCommandState(btnList[i]);

            if (cmdActive) {
                document.getElementById(btnList[i]).style.borderTop="1px solid #aaaaaa";
                document.getElementById(btnList[i]).style.borderLeft="1px solid #aaaaaa";
                document.getElementById(btnList[i]).style.borderRight="1px solid #555555";
                document.getElementById(btnList[i]).style.borderBottom="1px solid #555555";
                document.getElementById(btnList[i]).style.backgroundColor="#eecc00";
            } else {
                document.getElementById(btnList[i]).style.border="1px solid transparent";
                document.getElementById(btnList[i]).style.backgroundColor="transparent";
            }

        }

        var font = sel.queryCommandValue("FontName");

        //alert(font);

        if (font.toLowerCase() == 'times') { document.getElementById('Font').selectedIndex=1; }
        if (font.toLowerCase() == 'arial') { document.getElementById('Font').selectedIndex=2; }
        if (font.toLowerCase() == 'cursive') { document.getElementById('Font').selectedIndex=3; }
        if (font.toLowerCase() == 'courier') { document.getElementById('Font').selectedIndex=4; }
        if (font.toLowerCase() == 'tahoma') { document.getElementById('Font').selectedIndex=5; }
        if (font.toLowerCase() == 'verdana') { document.getElementById('Font').selectedIndex=6; }
        if (font.toLowerCase() == 'corbel') { document.getElementById('Font').selectedIndex=7; }

        var fontSize = sel.queryCommandValue("FontSize");

        //alert(font);

        if (fontSize == '1') { document.getElementById('FontSize').selectedIndex=1; }
        if (fontSize == '2') { document.getElementById('FontSize').selectedIndex=2; }
        if (fontSize == '3') { document.getElementById('FontSize').selectedIndex=3; }
        if (fontSize == '4') { document.getElementById('FontSize').selectedIndex=4; }
        if (fontSize == '5') { document.getElementById('FontSize').selectedIndex=5; }
        if (fontSize == '6') { document.getElementById('FontSize').selectedIndex=6; }
        if (fontSize == '7') { document.getElementById('FontSize').selectedIndex=7; }


    } catch (e) {
        // ignore any errors at this stage
    }

}


function getDate(obj,month,year) {
ModalWin.init();

if (month && year) {
  ModalWin.show('cal3.php?month='+month+'&year='+year,'300px','250px', obj, 'Date Picker',true,'TEXT');
} else {
  ModalWin.show('cal3.php','300px','250px', obj, 'Date Picker',true,'TEXT');
}


}

function showImg(obj) {

ModalWin.init();

ModalWin.show(obj.src,'550px','400px', null, 'Image View',true,'');

return true;

}

function showImgGal(productID) {

ModalWin.init();

ModalWin.show('image_view.php?pid='+productID,'550px','400px', null, 'Image View',true,'');

return true;

}

function swapImage(imgObj,newSource) {
  imgObj.src="/images/"+newSource;
}

function deleteEvent(eventID,month,year) {
  document.location="events_management.php?action=DELETE&event_id="+eventID+"&month="+month+"&year="+year;
}

function editEvent(eventID,month,year) {
  document.location="events_management.php?action=EDIT&event_id="+eventID+"&month="+month+"&year="+year;
}

function copyEvent(eventID,month,year) {
  document.location="events_management.php?action=COPY&event_id="+eventID+"&month="+month+"&year="+year;
}

function viewEvent(eventID) {
ModalWin.init();

ModalWin.show('view_event.php?event_id='+eventID,'450px','300px', null, 'View Event',true,'');

return true;
}

function viewEvents(eventList) {
ModalWin.init();

ModalWin.show('view_events.php?event_list='+eventList,'450px','300px', null, 'View Event',true,'');

return true;
}

function swapImg(obj,imgPath) {
  obj.src=imgPath;
}

/*function editObj_onPaste() {

    for (var intLoop = 0; intLoop < workspace.all.length; intLoop++) {
        el = workspace.all[intLoop];
        el.removeAttribute("className","",0);
        el.removeAttribute("style","",0);
    }
    // remove all xml prefixes and smarttags
    var html = workspace.innerHTML;

    html = html.replace(/ <\/o:[pP]>/g, ""); // Remove all instances of
    html = html.replace(/o:/g, ""); // delete all o: prefixes
    html = html.replace(//g, ""); // delete all smarttags
    html = html.replace(/<[pP]><\/[pP]>/g, ""); // delete all empty paragraph tags
    html = html.replace(/<[pP]> <\/[pP]>/g, ""); // delete all empty paragraph tags

}
*/

   function insertUserBox() {
      box_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         box_http_request = new XMLHttpRequest();
         if (box_http_request.overrideMimeType) {
            box_http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            box_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               box_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!box_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

			var parameters='action=GETBOX';

      box_http_request.onreadystatechange = getBox;
      box_http_request.open('POST', 'user_box.php' , true);
      box_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      box_http_request.setRequestHeader("Content-length", parameters.length);
      box_http_request.setRequestHeader("Connection", "close");
      box_http_request.send(parameters);
   }

   function getBox() {
      if (box_http_request.readyState == 4) {
         if (box_http_request.status == 200) {
         
             if (box_http_request.responseText.substr(0,5) == "Error") {
                alert(box_http_request.responseText.substr(6));
             } else {
             
		            var editObj = document.getElementById('editPane');
		            editObj.focus();
		            var sel = editObj.document.selection.createRange();
		            sel.pasteHTML(box_http_request.responseText);
                
             }
             m_showBorders=true;
             toggleTableBorders();

         } else {
            alert('There was a problem saving, please try again. '+http_request.status);
         }
      } else {
            //alert('Argh!');

            //document.forms['SitePULSE'].submit();
      }
   }


