//Place ppjot.js in the same folder as HTML files.
//Put this line: <SCRIPT Language = "JavaScript" src="ppjot.js"></SCRIPT>
//at the top of any HTML file to make the file editable.
//Specify editable sections of the file by wrapping 
//them in span tags with ppjot() as ondblclick event, e.g.
//<span id=myspan ondblclick="ppjot(id)">This is editable text.</span>

//Public variables - can be changed here
//or at the top of an HTML file, e.g. put <script>saveprogram='mysaver.php'</script>
//AFTER <SCRIPT Language = "JavaScript" src="ppjot.js"></SCRIPT> in the html file
var saveprogram='/ppsave.mv';
var correctpassword='had2gQ3E.cc3k';
var correctuploadpassword='ha3DPfvsnkcKU';
var helpfile='/ppmenu.mv';
var helpfilewindow='pphwindow';
var email='alerts@iafrica.com';
var checkeditstatus='y';
var author='Martin Terre Blanche';
var title='';
var byline='';
var permalink='';
var blogspanid='blogspan';
var blogdivid='blogdiv';
var channelfiles= 'collab.rss';
var channeltitles='Collaborative Learning';
var channeldescriptions='Collaborative Learning news and updates by Martin Terre Blanche';
var channellinks='http://www.criticalmethods.org/collab/news.htm';
var channelimages='http://www.criticalmethods.org/images/martin.jpg';
var footer='<hr size=1>';
var currentmonth;
var currentyear;
var nowid;
var maxrssitems=15;
var maxrssitemlength=32000;


//Global but private variables - don't fiddle with them.
monthArray = ['Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec'];
b = 'bold';i = 'italic';u = 'underline';c = 'cut';cp = 'copy';pst = 'paste';j = 'justifyleft';jc = 'justifycenter';jr = 'justifyright';io ='insertorderedlist';iu ='insertunorderedlist';od='outdent';idt='indent';cl='CreateLink';
fn='fontname';fs='fontsize';fc='ForeColor';

var ppsetupdone;
var currentlyEditing;
var currenteditMode;
var oldInnerHTML='';
var foldername;
var filename;
var fileextension;
var header;
var IFrameObj;
var archivefilepath='';
var datestring='';
var commentsheader='<table><tr><td width=80% valign=top><font face=verdana size=2>';
var commentsfooter='</font></td><td valign=top><font face=verdana size=1><form action=/ppcom.mv method=post><input type=hidden value=comments_blog_pppost_><input type=hidden name=archivefilepath value=><input type=hidden name=filepath value=><input type=hidden name=commentsid value=><input type=submit value="submit" style="font-size:8;"> comments<br /><textarea rows=2 wrap=virtual class=entercomments name=comments></textarea><br /><span class=showcomments name=storedcomments ondblclick=ppjot(id) id=></span></form></font></td></tr></table><hr>';

ppeditstring ='<div id="ppeditdiv" style="background-color:#CCCCFF;bborder-style:solid; bborder-width:1; bborder-color:red;position:absolute;top:0;left:0;visibility:hidden;">  <img align="top" id="pptoolbar" usemap="#tool" border="0" src="/images/tool.gif">'
 +' <select style="background-color:aqua;font-size:8pt;" onchange="doCommand(fn,this[this.selectedIndex].value);this.selectedIndex=0"> <option selected value="">FONT</option><option value="Arial">Arial</option><option value="Comic Sans MS">Comic</option><option value="Courier New">Courier</option><option value="System">System</option><option value="Times New Roman">Times</option><option value="Verdana">Verdana</option> </select>'
 +'<select style="background-color:aqua;font-size:8pt;" onchange="doCommand(fs,this[this.selectedIndex].value);this.selectedIndex=0"> <option selected value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option></select>'
 +'<select style="background-color:aqua;font-size:8pt;"onchange="doCommand(fc,this[this.selectedIndex].value);this.selectedIndex=0"> <option selected value="">COLOR</option><option value="Black">Black</option><option value="Red">Red</option><option value="Green">Green</option><option value="Blue">Blue</option><option value="Navy">Navy</option><option value="Yellow">Yellow</option><option value="Gray">Gray</option><option value="White">White</option><option value="Silver">Silver</option><option value="Purple">Purple</option><option value="Pink">Pink</option><option value="Orange">Orange</option><option value="Brown">Brown</option><option value="Aqua">Aqua</option><option value="Cyan">Cyan</option><option value="Magenta">Magenta</option></select>'
 +' <img id="pptoolbar2" src="/images/save.gif" alt="Save" align="top" onclick="ppsave()";>&nbsp; </div> <map name="tool"><area shape="rect" coords= "1,1,20,20" alt='+b+' onClick="doCommand(b)"><area shape="rect" coords="21,1,40,20" alt='+i+' onClick="doCommand(i)"><area shape="rect" coords="41,1,60,20" alt='+u+' onClick="doCommand(u)"><area shape="rect" coords="61,1,80,20" alt='+c+' onClick="doCommand(c)"><area shape="rect" coords="81,1,100,20" alt='+cp+' onClick="doCommand(cp)"><area shape="rect" coords="101,1,120,20" alt='+pst+' onClick="doCommand(pst)"><area shape="rect" coords="121,1,140,20" alt='+j+' onClick="doCommand(j)"><area shape="rect" coords="141,1,160,20" alt='+jc+' onClick="doCommand(jc)"><area shape="rect" coords="161,1,180,20" alt='+jr+' onClick="doCommand(jr)"><area shape="rect" coords="181,1,200,20" alt='+io+' onClick="doCommand(io)"><area shape="rect" coords="201,1,220,20" alt='+iu+' onClick="doCommand(iu)"><area shape="rect" coords="221,1,240,20" alt='+od+' onClick="doCommand(od)"><area shape="rect" coords="241,1,260,20" alt='+idt+' onClick="doCommand(idt)"><area shape="rect" coords="261,1,280,20" alt="options" onClick="doHelp()"><area shape="rect" coords="281,1,300,20" alt='+cl+' onClick="doCommand(cl)"><area shape="rect" coords="301,1,320,20" alt="Insert an image" onClick="getImage()">';



function doCommand(comd,optns) {
  var ypos=document.body.scrollTop;
  document.getElementById(currentlyEditing).focus();
  document.execCommand(comd,"",optns);
  document.body.scrollTop=ypos;
}

function doHelp () {
  window.open('', helpfilewindow,'toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,copyhistory=0,width=620,height=420 left=0 top=0');
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').target=helpfilewindow;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').action=helpfile;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').filename.value=filename;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').fileextension.value=fileextension;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').foldername.value=foldername;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').includefilename.value=document.getElementById(currentlyEditing).includefile;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').content.value=document.getElementById(currentlyEditing).innerHTML;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').contentid.value=currentlyEditing;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').header.value=header;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').email.value=email;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').correctpassword.value=correctpassword;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').correctuploadpassword.value=correctuploadpassword;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submit();
}

function doHTML() {
  if (currentlyEditing) {
  var tempinnerHTML;
  if (currenteditMode!='html') {
    document.getElementById('ppeditdiv').style.visibility='hidden';
    document.getElementById(currentlyEditing).contentEditable='false';
    tempinnerHTML=document.getElementById(currentlyEditing).innerHTML;
    document.getElementById(currentlyEditing).innerHTML='<textarea id=gurgleop style="font-size:8pt;font-family:verdana;width:400;height:150">'
        +tempinnerHTML+'</textarea>';
    document.getElementById('gurgleop').focus();
    currenteditMode='html';
  } else {
          tempinnerHTML=document.getElementById('gurgleop').value;
          document.getElementById(currentlyEditing).innerHTML=tempinnerHTML;
          document.getElementById(currentlyEditing).contentEditable='true';
          currenteditMode='rich';
          document.getElementById('ppeditdiv').style.visibility='visible';
         }
  }
}

function keyTrap() {
  key = window.event.keyCode;
  switch (key) {
    case 8   : doHTML(); break; //shift-ctrl-h
    case 16  : ppblog(); break; //shift-ctrl-p
    case 17  : ppquit('true'); break; //ctrl-q
    case 19  : ppsave(); break; //shift-ctrl-s
  }
}



function setFilename() {
	foldername=window.location.pathname;
	if (foldername.indexOf('.htm',0) < 0) {foldername=foldername+'index.htm'}
	if (foldername.indexOf('.',0) < 0) {foldername=foldername+'index.htm'}
	filename=foldername;
	while (filename.indexOf('/',0) > -1) {filename=filename.substring(filename.indexOf('/',0)+1,1000)}
	foldername=foldername.substring      (0,foldername.indexOf(filename,0));
	fileextension=filename.substring(filename.indexOf('.',0)+1,255);
	filename=filename.substring(0,filename.indexOf('.',0));
}

function setPassword() {
	document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').correctpassword.value=correctpassword;
      document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').correctuploadpassword.value=correctuploadpassword;

      var g='x';
      var tempvar='';
      g=g+window.location;
      if (g.indexOf('http://127.0.0.1')!=1) {
      	tempvar=prompt('Password:','');
      } else {
          tempvar='http://127.0.0.1';
        }
	if (tempvar==null) {tempvar=''};
	document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submittedpassword.value=tempvar;
}

function doscroll() {
  document.getElementById('ppeditdiv').style.top=document.body.scrollTop;
}

function setForm() {
	document.body.innerHTML = document.body.innerHTML+ppeditstring;
      window.onscroll=doscroll;
}

function ppblog() {
    var goahead=true;
    if (currentlyEditing) {goahead=ppquit()}
    if (goahead) {     
      currentlyEditing=blogspanid;
      today = new Date();
      currentmonth = today.getMonth()+1;
      currentyear = today.getYear();
      nowid = today.getTime();
      postid='pppost_'+nowid;
      title=prompt('Title',''); if (title==null) {return;}

      author=prompt('Posted by ',author); if (author==null) {return;}
      permalink=foldername+currentyear+'/'+currentmonth+'/'+filename+'.'+fileextension+'#'+nowid;
      datestring=today.getDate()+' '+monthArray[currentmonth-1]+' '+currentyear;
      byline='Posted by '+author+' on '+datestring+' ['+'<a href='+permalink+'>url</a>'+']';

      document.getElementById(blogdivid).innerHTML=
        header+'<b><p>'+title+'</p></b><span id='+blogspanid+'><p>.</p></span>'+'<br /><br /><font size=1>'+byline+'</font>'+footer
        +document.getElementById(blogdivid).innerHTML;
      ppjot(currentlyEditing);
    }
}


function ppsetup() {
  if (document.all) {
    if (ppsetupdone != 'y') {
      document.onkeypress = keyTrap;
      setFilename();
      setForm();
      ppsetupdone = 'y';
    }
  }
}

function doeditstatuscheck() {
  var date_now = new Date (); 
  var time_now = date_now.getTime (); 
  window.status='Checking who else may be editing this.';
  callToServer('/pplock.mv?f='+window.location+'&t='+time_now, 'saveframe');
}

function ppjot(id) {
  if (id > '') {
  var ypos=document.body.scrollTop;
  if (ppsetupdone != 'y') {ppsetup();}
  var goahead='true';
  if (currentlyEditing > '') {
  if (currentlyEditing != id) {
    if (document.getElementById(currentlyEditing).innerHTML != oldInnerHTML) { 
      goahead=ppquit();
    }
    if (goahead) {
      document.getElementById(currentlyEditing).innerHTML=oldInnerHTML;
      document.getElementById('ppeditdiv').style.visibility='hidden';
      document.getElementById(currentlyEditing).contentEditable='false';
    } else {
        document.getElementById(currentlyEditing).focus();
      }
  }
  }

  if (goahead) {

     currentlyEditing=id;
     if (checkeditstatus == 'y') {doeditstatuscheck();}
     oldInnerHTML=document.getElementById(id).innerHTML;
     var incl=document.getElementById(id).includefile;
         document.getElementById('ppeditdiv').style.visibility='visible';
         document.getElementById(id).focus();
         document.getElementById(id).contentEditable='true';
         document.getElementById(id).focus();
         document.getElementById('ppeditdiv').style.top=ypos;
         document.body.scrollTop=ypos;
  }
  }

     if (incl) {
        alert('THIS IS AN AUTO-INCLUDE SECTION. When you save it, your changes will be ignored and an updated version of the section will be fetched from another file ('+incl+') and inserted here.');
     } 

}

// function buildQueryString(theFormName) {
//   theForm = document.forms[theFormName];
//   var qs = ''
//   for (e=0;e<theForm.elements.length;e++) {
//     if (theForm.elements[e].name!='') {
//       qs+=(qs=='')?'?':'&'
//       qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
//       }
//     }
//   return qs
// }


function callToServer(theaddress, theID) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  var URL = theaddress;
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called

      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id', theID);
	tempIFrame.setAttribute('scrolling','no');
	tempIFrame.setAttribute('border','0px');
	tempIFrame.setAttribute('frameBorder','0px');
	tempIFrame.setAttribute('marginHeight','0px');
	tempIFrame.setAttribute('marginWidth','0px');
      tempIFrame.style.position='absolute';
      tempIFrame.style.top='0';
      tempIFrame.style.left='0';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);     
  }
  IFrameDoc = IFrameObj.contentWindow.document;
  IFrameDoc.location.replace(URL);
  return false;
}

function ppsave() {
  if ((currentlyEditing > '') ) {
    document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').action=saveprogram+'?f='+window.location;
      document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').filename.value=filename;
      document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').fileextension.value=fileextension;
      document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').foldername.value=foldername;
      document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').includefilename.value=document.getElementById(currentlyEditing).includefile;

  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').header.value=header;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').footer.value=footer;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').blogspanid.value=blogspanid;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').title.value=title;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').author.value=author;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').permalink.value=permalink;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').channelfiles.value=channelfiles;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').channeltitles.value=channeltitles;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').channeldescriptions.value=channeldescriptions;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').channellinks.value=channellinks;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').channelimages.value=channelimages;

  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').currentmonth.value=currentmonth;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').currentyear.value=currentyear;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').nowid.value=nowid;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').byline.value=byline;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').blogdivid.value=blogdivid;

  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').maxrssitems.value=maxrssitems;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').maxrssitemlength.value=maxrssitemlength;
  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').datestring.value=datestring;

  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').archivefilepath.value=
      archivefilepath=foldername+currentyear+'/'+currentmonth+'/'+filename+'.'+fileextension;


 	if (document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submittedpassword.value=='') {
		setPassword();
	}

	if (document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submittedpassword.value!='') {
        document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').content.value=document.getElementById(currentlyEditing).innerHTML;
        document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').contentid.value=currentlyEditing;

	  window.status='Saving';
        document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submit();
	  oldInnerHTML=document.getElementById(currentlyEditing).innerHTML;

     	document.getElementById(currentlyEditing).contentEditable='false';
      document.getElementById('ppeditdiv').style.visibility='hidden';

      }
  }
}

function ppquit(resetpassword) {
  if (confirm('Undo all changes since last save?')) {
	document.getElementById(currentlyEditing).innerHTML=oldInnerHTML;
   	document.getElementById(currentlyEditing).contentEditable='false';
	window.status='';
      if (resetpassword) {
	  document.getElementById('saveframe').contentWindow.document.getElementById('ppsaveform').submittedpassword.value='';
      }
      document.getElementById('ppeditdiv').style.visibility='hidden';
      return('true');
  }
}



function getImage() {
  var imagename = prompt('Address of image to insert:','');
  if (imagename != null) {
    document.getElementById(currentlyEditing).innerHTML=
      '<img src="'+imagename+'">'+
      document.getElementById(currentlyEditing).innerHTML;
  }
}
