//server side scripts: PWUpload.php GetScript.php 
nodeList = new Array();         
var gTypeToggle = 0;         
var gHtmlToggle = 0;   
var gScriptTextToggle = 0;         
var iframeDoc=null;         
var BMargins=0;         

// Issue: when editing files that have <object> tags like the shockwave game files.  It changes swStretchStyle param from "stage" to "fill".  Results in smaller shockwave display on Rubick's Cube. 
window.onload=initialize();         

function initialize() {         
 window.status="Window loaded.  Initializing...";         
 loadKeyEvent();         
// gTypeToggle=1; //       12/5 test 
// DoIFrame();         
 window.status="Press F2 to edit.";         
}         

function UnDoIFrame() {         
 //Issues: when converting back the relative links become absolute and the tags are capitalized and other optimizations from the DOm occur.    //Issue2: When writing back a second time it "appends".         
 if(gHtmlToggle==0){         
  window.document.body.innerHTML=iframeDoc.body.innerHTML;         
 } else {         
  if(document.all){//IE         
   var ts = iframeDoc.body.innerText.toString();         
  window.document.open();          
  window.document.write(ts);         
  window.document.close();//prevents "append" problem on subsequent writes.         
  } else { //FF doesn't have innerText (uses textContent) or OuterHTML functions         
   window.document.write("this is a test.");//iframeDoc.body.textContent);         
  }         
 }         
 document.body.style.margin = BMargins;         
}         

function KillMargins(){         
 BMargins=document.body.style.margin;         
 document.body.style.margin=0;//"0px";          
}         

function GetWidth(){         
 alert("Height = " + document.body.clientHeight          
+ "\nWidth = " + document.body.clientWidth);         
}         

function DoIFrame() {         
 var wd="";         
 if(gHtmlToggle==0){         
  wd = window.document.body.innerHTML;         
 } else {         
  if(document.all) {//IE         
   wd = window.document.body.parentNode.outerHTML;         
  } else {//FF         
   wd='<html>'+window.document.body.parentNode.innerHTML+'</html>'         
  }         
 }         
 var helpText="Keyboard Shortcut - Result in Editor\\nCTRL and A  Selects all in the current document.\\nCTRL and B  Bold text.\\nCTRL and C  Copies the item or text to the Clipboard.\\nCTRL and I  Italic text.\\nCTRL and K  Create Hyperlink\\nCTRL and P  Displays the Print dialog box.\\nCTRL and U  Underline text\\nCTRL and V  Pastes the copied item or text from the Clipboard.\\nCTRL and X  Cuts the item or text selected to the Clipboard.\\nCTRL and Y  Redo the last undone action.\\nCTRL and Z  Undoes the last action.\\nCTRL and ENTER  Insert Break.\\n";         
// var checkKeyFunction = 'function checkKey(e){ window.status="processing keystroke..."; if(window.event){   window.status="IE key";  e=window.event;  var key = window.event.keyCode; } else if(e) {   window.status="FF key";  var key=e.which;  if(key==undefined){key=e.keyCode;}  window.status="FF key: "+key; } else {window.status="key error.";} if(key == 113) {   F2Key(); }}function F2Key(){  if(gTypeToggle==0){   gTypeToggle=1;   DoIFrame();   window.status="gTypeToggle ON";   } else {   gTypeToggle=0;   UnDoIFrame();   window.status="gTypeToggle off"  }}';    
 var buttons= "<DIV ID='Toolbar1' STYLE='position:absolute; left: 0px; top: 0px;'>";         
 buttons = buttons + "<input type='button' value='End Editing' onClick='F2Key()'>";         
 buttons = buttons + '<input type="button" value="Help" onClick="alert('+"'"+helpText+"'"+')">';         
 buttons = buttons + '<input type="button" value="HTML" onClick="F8Key()">';//"alert(window.document.body.style.margin)">';         
 buttons = buttons + '<input type="button" value="Save" onClick="F9Key()">';         
 buttons = buttons + "<INPUT id='SFile' value='Enter Filename' name='SFile'><INPUT type='button' value='Load raw text' onClick='LoadScriptTextF2()'>";      
 buttons = buttons + '</DIV>';         
 KillMargins();         
 document.body.innerHTML=buttons;         
 var iframe = createIframe ('iframe0', "100%", "100%");         
 if (iframe) {         
  if (iframe.contentDocument) {         
   iframeDoc = iframe.contentDocument;         
  }         
  else if (iframe.contentWindow) {         
   iframeDoc = iframe.contentWindow.document;         
  }         
  else if (window.frames[iframe.name]) {         
   iframeDoc = window.frames[iframe.name].document;         
  }         
  if (iframeDoc) {         
   iframeDoc.open();         
   if(gHtmlToggle==0){         
    iframeDoc.write("<html><body>"+wd+"</body></html>");         
             
   } else {         
    wd=wd.replace(/\x3C/gi,"\x26lt;");         
    wd=wd.replace(/\n/gi,"<br>\n");         
    wd=wd.replace(/ /gi,"\x26nbsp;");         
    iframeDoc.write("<html><body><code>"+wd+"</code></body></html>");         
   }         
   iframeDoc.close();         
   iframeDoc.designMode='On';         
   iframeDoc.attachEvent('onkeyup',checkKey);         
   window.status='iFrame HTML Setup';         
  }         
  else {         
   window.status='iFrame HTML setup failed.';         
  }         
 }         
}         
function createIframe (iframeName, width, height) {         
 var iframe;         
 if (document.createElement && (iframe = document.createElement('iframe'))) {         
  iframe.name = iframe.id = iframeName;         
  iframe.width = width;         
  iframe.height = height;         
  iframe.height = iframe.height;         
  iframe.src = 'about:blank';         
  iframe.setAttribute('frameBorder','0');         
  iframe.setAttribute('onkeyup','checkKey(event)');         
/*  iframe.marginwidth='0px';         
  iframe.frameborder="0";         
  iframe.border=0;         
  iframe.scrolling="no";         
  iframe.style.border=0;         
  iframe.style.margin=0;         
  iframe.style.outline=0;         
  iframe.style.padding=0;         
*/         
  document.body.appendChild(iframe);         
 }         
 return iframe;         
}         


//p176 of JavaScript DOM book.         
function loadKeyEvent() {         
  document.onkeyup=checkKey;//works on Firefox and IE.         
}         

function endtag_output(str) {         
 var et = document.getElementsByTagName("endtag");         
 window.status="attempting endtag output. et.length=" + et.length;         
 if(!et || et.length==0){         
  window.status="No endtag element.";         
 } else {          
  et[0].nextSibling.nodeValue = str;         
  window.status="endtag = " + str ;         
 }         
}         

function checkKey(e){          
 window.status="processing keystroke...";         
 if(window.event){ //IE         
  window.status="IE key";         
  e=window.event;         
  var key = window.event.keyCode;         
 } else if(e) { //Firefox, IE if in IFRAME?         
  window.status="FF key";         
  var key=e.which;//keyCode;//FF         
  if(key==undefined){key=e.keyCode;}//IE         
  window.status="FF key: "+key;         
 } else {window.status="key error.";}         
 if(key == 113) { //113=F2 key         
  F2Key(e);         
// }          
//else if(gTypeToggle==0) {         
//  window.status="Press F2 key to enable editing functionality.";         
 } else if(key==118) { //F7 key         
  endtag_output(document.body.innerHTML);         
  //endtag_output(iFrm2d.body.innerHTML);         
  return true;         
//F7 key in FF turns carot/cursor browsing on.         
 } else if(key==119) { //F8         
  F8Key(e);         
 } else if(key==120) { //F9 key         
  F9Key();         
 }          
//    alert('Note: gTypeToggle = ' + gTypeToggle);         
 //window.status="processed keystroke.";         
}         

function F2Key(event){         
  if(gTypeToggle==0){         
   gTypeToggle=1;         
   if(event.shiftKey) { //do Load a File   
// fix this by moving button to toolbar.   
    alert("F2 with shift key.");   
   } else     if('php'==document.URL.substr(1+document.URL.lastIndexOf("."))) {   
//if PHP:   
//   var dURL=document.URL;   
    LoadScriptText(document.URL);   
//   } else if(event.ctrlKey) { //do HTML   
//for some reason this is resulting in an error with ctrlKey.   
//    alert("Testing F2 with event.ctrlKey.");         
   } else if(event.altKey) { //do HTML   
    alert("Testing F2 with event.altKey.");         
   } else {   
    DoIFrame();         
   }   
   window.status="gTypeToggle ON";          
  } else {         
   gTypeToggle=0;         
   UnDoIFrame();         
   gHtmlToggle=0;         
   window.status="gTypeToggle off"         
  }   
}   

function LoadScriptTextF2(){   
  gScriptTextToggle = 1;   
  //alert("Loading " + document.getElementById('SFile').value + "\nusing GetScript.php");      
  gTypeToggle=1;         
  gHtmlToggle=1;         
//  DoIFrame();      
 LoadScriptText(document.getElementById('SFile').value);   
}   

function LoadScriptText(dURL){   
    dURL = "/" + dURL;   
    gHtmlToggle=1;   
    iframeDoc.open();   
    iframeDoc.write("Loading " + dURL.substr(1+dURL.lastIndexOf("/")) + "<P><span id='result'>");   
    iframeDoc.close();   
    loadXMLDoc("GetScript.php", "SFile=" +dURL.substr(1+dURL.lastIndexOf("/")));   
}   

function F8Key(event){         
 if(gTypeToggle==0){         
  gTypeToggle=1;         
  gHtmlToggle=1;         
  DoIFrame();         
//if PHP:   
  var dURL=document.URL;   
  if('php'==dURL.substr(1+dURL.lastIndexOf("."))) {   
   LoadScriptText(dURL);   
  }   
 } else {         
  F2Key();         
 }         


/*         
  if(gHtmlToggle==0) {//show HTML         
   var tag = document.URL   
   if('php'==tag.substr(1+tag.lastIndexOf("."))) {   
  var request=XMLHttpRequest();   
   request.open("POST", "http://multiplayer3d.com/PHP/GetScript.php",false);   
   request.send();   
   //alert(request.responseText);   
   }// else {   
//not php      
    gTypeToggle=0;//go back         
    UnDoIFrame();         
    gHtmlToggle=1;         
    gTypeToggle=1;         
    DoIFrame();          

    if(document.all){//IE         
    fullHTML=window.document.body.parentNode.outerHTML;         
//    }   
   }else{ //FF, see http://snipplr.com/view/5460/outerhtml-in-firefox/         
//    fullHTML='<html>'+window.document.body.parentNode.innerHTML+'</html>';         
   }         
   gTypeToggle=1;//return         
   DoIFrame();         
    alert(iframeDoc.body.innerText);         
   if(document.all){//IE         
    iframeDoc.body.innerText=fullHTML;         
   }else{ //FF         
//    iframeDoc.body.textContent=fullHTML;         
   }         

   window.status="HTML editing mode.";         
   } else { //Toggle back to rich text         
   gTypeToggle=0;         
   UnDoIFrame();//complete reboot of javascript         
   gHtmlToggle=0;         
   DoIFrame();         
   gTypeToggle=1;         

   if(document.all){//IE         
    iframeDoc.body.innerHTML=iframeDoc.body.innerText;         
   }else{ //FF         
    iframeDoc.body.innerHTML=iframeDoc.body.textContent;         
   }         
*/         
//   window.status="Rich Text editing mode.";         
//    alert('F8 gTypeToggle = ' + gTypeToggle);         
   //what is probably happening here is that when it switches from          
   //HTML mode to rich text it passes through the main page         
   //which causes the main page to initiate again, setting         
   //gTypeToggle to zero after UnDoIFrame.         
//  }         
// }         
}         

function F9Key(){         
 if(gScriptTextToggle == 1) {    
  var ts = iframeDoc.body.innerText.toString();         
  //alert("1 Saving " + document.getElementById('SFile').value + "\n" + ts);   
  SubmitData(ts, document.getElementById('SFile').value);         
 } else if(gTypeToggle==1){   
  F2Key();   
 } else {   
  SubmitData(window.document.body.parentNode.outerHTML, document.URL);         
 }   
  //one crude way to save.         
/*  wo = window.open("", "newWindow");         
//    alert('F9 gTypeToggle = ' + gTypeToggle);         
  if(gTypeToggle==0){         
alert("document.body.parentNode.outerHTML = " + document.body.parentNode.outerHTML);         
   wo.document.write(document.body.parentNode.outerHTML);         
  } else {         
//  wo.document.write(window.document.body.parentNode.outerHTML);         
   wo.document.write(iframeDoc.body.parentNode.outerHTML);         
  }         
*/         
/*  if(gHtmlToggle==1) {         
   gHtmlToggle=0;         
   if(document.all){//IE         
    alert("iframeDoc.body.innerText = " + iframeDoc.body.innerText);         
    iframeDoc.body.innerHTML=iframeDoc.body.innerText;         
   }else{ //FF         
    iframeDoc.body.innerHTML=iframeDoc.body.textContent;         
   }         
  }         
  if(gTypeToggle==1){         
   gTypeToggle=0;         
   UnDoIFrame();         
  }           
//  SubmitData(iframeDoc.body.parentNode.outerHTML);         
  if(document.all){//IE         
   SubmitData(window.document.body.parentNode.outerHTML);         
  }else{ //FF, see http://snipplr.com/view/5460/outerhtml-in-firefox/         
   SubmitData('<html>'+window.document.body.parentNode.innerHTML+'</html>', document.URL);         
  }         
*/         
}         

function SubmitData(txt,fileName){         
// alert("enter Password");//OK      
// confirm("enter Password");//OK or cancel, returns true or false      
// var pw = prompt("Enter Password");//OK or cancel and text, returns text or false?      
// This is just fucking unbelievable:      
//The window.prompt script method is blocked and the gold Information bar is displayed by default in Internet Zone for Internet Explorer 7. The helps prevent websites from spoofing things such as the logon screens of other websites. This is a new security enhancement for Internet Explorer 7.      
// An alternative is to have a window change to ask for a prompt.      
//  alert("SubmitData: Saving " + fileName);   
 document.body.innerHTML="<P><form method='post' action='PWUpload.php'><input type='text' name='URL' id='URL' value='" + fileName + "'><input type='hidden' name='DataHTML' id='DataHTML' value=''><INPUT id='PWord' value='Enter Password' name='PWord'> <INPUT type=submit value='Submit Query'> </FORM>";      
 ft=document.getElementById('DataHTML');      
 ft.value=txt;      
// ft=document.getElementById('URL');      
// ft.value=fileName;   

/*      
 fe = window.document.createElement('form');         
 fe.method='post';        
 fe.action="http://multiplayer3d.com/PHP/PWUpload.php";         
 ft = window.document.createElement('input');         
 ft.type='hidden';         
 ft.name='URL';         
 ft.value = document.URL;         
 fe.appendChild(ft);         
 ft = window.document.createElement('input');         
 ft.type='hidden';         
 ft.name='DataHTML';         
 ft.value = txt;         
 fe.appendChild(ft);         
 fp = window.document.createElement('input');         
 fp.type='hidden';         
 fp.name='PWord';      
 fp.value = pw;         
 fe.appendChild(fp);         
 window.document.body.appendChild(fe);         
 fe.submit();         
 window.document.body.removeChild(fe);         
*/      
}         

function loadXMLDoc(url,data)    
{   
    // branch for native XMLHttpRequest object   
    if (window.XMLHttpRequest) {   
        req = new XMLHttpRequest();   
        req.onreadystatechange = processReqChange;   
        req.open("POST", url, true);   
        req.send(data);   
    // branch for IE/Windows ActiveX version   
    } else if (window.ActiveXObject) {   
        req = new ActiveXObject("Microsoft.XMLHTTP");   
        if (req) {   
            req.onreadystatechange = processReqChange;   
//            req.open("GET", url, true);//will be POST anyway if use data in req.send   
            req.open("POST", url, true);   
            req.setRequestHeader("Cache-Control", "no-cache");   
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');   
//            req.setRequestHeader("Content-Type", "text/html");   
            req.send(data);   
        }   
    }   
}   

function processReqChange()    
{   
//alert("Wait for it...");   
    if (req.readyState == 4) {   
        if (req.status == 200) {   
//    
//        alert(req.responseText);   
//iframeDoc.getElementById('result').innerHTML=req.responseText;   
    var wd = req.responseText;   
    wd=wd.replace(/\x3C/gi,"\x26lt;");         
    wd=wd.replace(/\n/gi,"<br>\n");         
    wd=wd.replace(/ /gi,"\x26nbsp;");         
    iframeDoc.open();   
    iframeDoc.write("<html><body><code>"+wd+"</code></body></html>");     
    iframeDoc.close();        } else {   
            alert("There was a problem retrieving the XML data:\n" + req.statusText);   
        }   
    }   
}