function addEvent(func){
// Simon Willison: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  if (!document.getElementById | !document.getElementsByTagName) return
  var oldonload=window.onload
  if (typeof window.onload != 'function') {window.onload=func}
  else {window.onload=function() {oldonload(); func()}}
}


/* JavaScript code highlighting parser from: */
/* http://www.howtocreate.co.uk/tutorials/jsexamples/syntax/ */
/* Very slightly modified */

var tempValOrig="", isEnc=false

function inAr(oStr){
  var opAr = new Array("+", "-", "*", "%", "=", "+=", "-=", "&gt;", "&lt;", "&gt;=", "&lt;=", "++", "--", "==", "!=", "&lt;&lt;", "&gt;&gt;", "&amp;", "|", "^", "~", "!", "&amp;&amp;", "||", "?", ":")
  for (var x=0; x<opAr.length; x++)
    if (oStr.substr(0, opAr[x].length)==opAr[x]) return opAr[x]
  return false
}

function syntaxHighlight(){
  var tempVal=document.getElementById('toParse').value, newVal=""
  tempValOrig=document.getElementById('toParse').value
  var oAmp="a", oRand=new Array("b", "c")
  document.getElementById('parsed').value = "Highlighting. Please wait..."

  while (tempVal.indexOf(oAmp)+1)
    oAmp+=oRand[Math.round(Math.random())]

  var fromAr=new Array("&",oAmp,"<",">","\""), toAr=new Array(oAmp,"&amp;","&lt;","&gt;","&quot;")
  for (var x=0; x<fromAr.length; x++)
      while (tempVal.indexOf(fromAr[x])+1)
        tempVal=tempVal.replace(fromAr[x], toAr[x])

  for (var x=0; x<tempVal.length; x++){
    if (tempVal.substr(x,1).replace(/[\w\+\-\*\/%=&\!\|\^~\'\"\?:]/, ""))
      newVal+=tempVal.substr(x,1)
    else{
      if (!tempVal.substr(x,1).replace(/[a-zA-Z_]/, "")){
        var tempKey=tempVal.substr(x).replace(/\W[\w\W]*/, "")
        x+=tempKey.length-1
        if (tempVal.substr(x).replace(/\w*\s*/, "").substr(0, 1)=="("){
          newVal+=(tempKey=="for" || tempKey=="if" || tempKey=="while" || tempKey=="switch" || tempKey=="with" || tempKey=="typeof" || tempKey=="void" || tempKey=="return" || tempKey=="function")?"<span class=\"oper\">":"<span class=\"func\">"
          newVal+=tempKey+"</span>"
        }
        else{
          if (tempKey=="var" || tempKey=="function" || tempKey=="var" || tempKey=="new" || tempKey=="else" || tempKey=="return" || tempKey=="do" || tempKey=="break" || tempKey=="continue" || tempKey=="case" || tempKey=="default" || tempKey=="in" || tempKey=="delete" || tempKey=="typeof")
            newVal+="<span class=\"oper\">"+tempKey+"</span>"
          else{
            newVal+=(tempKey=="true" || tempKey=="false" || tempKey=="null")?"<span class=\"num\">":"<span class=\"var\">"
            newVal+=tempKey+"</span>"
          }
        }
      }
      else{
        if (!tempVal.substr(x,1).replace(/[\d]/, "")){
          newVal+="<span class=\"num\">"+tempVal.substr(x).replace(/[^0-9.xE][\w\W]*/, "")+"</span>"
          x+=tempVal.substr(x).replace(/[^0-9.xE][\w\W]*/, "").length-1
        }
        else{
          if (tempVal.substr(x,2)!="/*" && tempVal.substr(x, 2)!="//"){
            if (tempVal.substr(x,1)=="'"){
              var backSl=0
              for (var y=1; (backSl % 2 || tempVal.substr(y+x, 1)!="'") && x+y < tempVal.length-1; y++)
                backSl=(tempVal.substr(y+x, 1)=="\\" && (tempVal.substr(y+x+1, 1)=="\\" || tempVal.substr(y+x+1, 1)=="'"))?backSl++:0
              newVal+="'<span class=\"string\">"+tempVal.substr(x + 1, y - 1)+"</span>'"
              x+=y
            }
            else{
              if (tempVal.substr(x, 6)=="&quot;"){
                var backSl=0
                for (var y=1; (backSl % 2 || tempVal.substr(y+x, 6)!="&quot;") && x+y<tempVal.length-1; y++)
                  backSl=(tempVal.substr(y+x,1)=="\\" && (tempVal.substr(y+x+1,1)=="\\" || tempVal.substr(y+x+1,6)=="&quot;"))?backSl++:0
                newVal+="&quot;<span class=\"string\">"+tempVal.substr(x+6, y-6)+"</span>&quot;"
                x+=y+5
              }
              else{
                if (inAr(tempVal.substr(x))){
                  newVal+="<span class=\"oper\">"+inAr(tempVal.substr(x))+"</span>"
                  x+=inAr(tempVal.substr(x)).length-1
                }
                else{
                  for (var y=-1; x+y != 0 && (tempVal.substr(x+y,1)=="  " || tempVal.substr(x+y, 1)==" " || tempVal.substr(x+y, 1)=="\n" || tempVal.substr(x+y, 1)=="\r" || tempVal.substr(x+y, 1)=="\f"); y--) {}
                  if (!tempVal.substr(x+y,1).replace(/[\)\w\]]/,""))
                    newVal+="<span class=\"oper\">/</span>"
                  else{
                    var backSl=0
                    for (var y=1; (backSl % 2 || tempVal.substr(y+x, 1)!="/") && x+y<tempVal.length-1; y++)
                      backSl=(tempVal.substr(y+x, 1)=="\\" && (tempVal.substr(y+x+1, 1)=="\\" || tempVal.substr(y+x+1, 1)=="/"))?backSl++:0
                    newVal+="/<span class=\"reg\">"+tempVal.substr(x+1,y-1)+"</span>/"
                    x+=y
                    y=x
                    if (tempVal.substr(x+1,1)=="g" || tempVal.substr(x+1,1)=="i") x++
                    if (tempVal.substr(x+1,1)=="g" || tempVal.substr(x+1,1)=="i") x++
                    if (x>y) newVal+="<span class=\"reg\">"+tempVal.substr(y+1, x-y)+"</span>"
                  }
                }
              }
            }
          }
          else{
            if (tempVal.substr(x,2)=="/*"){
              newVal+="<span class=\"comment\">"+tempVal.substr(x).replace(/\*\/[\w\W]*/, "")+"*/</span>"
              x+=tempVal.substr(x).replace(/\*\/[\w\W]*/, "").length+1
            }
            else{
              newVal+="<span class=\"comment\">"+tempVal.substr(x).replace(/[\n\r][\w\W]*/, "")+"</span>"
              x+=tempVal.substr(x).replace(/[\n\r][\w\W]*/, "").length-1
            }
          }
        }
      }
    }
  }
  var lookslikeheader="<p class=\"tofollow\">It will look like this (with CSS added):</p>\n"
  var header = "<pre lang=\"javascript\"><code>\n"
  var footer = "\n</code></pre>\n"

  document.getElementById("lookslike").innerHTML = lookslikeheader+header+newVal+footer
  if (document.getElementById('includetags').checked) newVal=header+newVal+footer
  document.getElementById("parsed").value=newVal
}



function initialise(){
  document.getElementById('noscript').style.display="none"
  document.getElementById('javascript_parser').style.display="block"
  document.getElementById('parse').onclick=function(){syntaxHighlight()}
  document.getElementById('toParse').onclick=function(){this.select()}
  document.getElementById('parsed').onclick=function(){this.select()}
}

addEvent(initialise)
