///////////////////////////////////////////////////////////////////////
// checkcode version 1.4b, Copyright (C) 2007 Max Kiusso
//
// Autor :		Max Kiusso - kiussoATgmailDOTcom
// Date :		2007 06 06
//
// Features:
// 		This software provide to change the color of the JavaScript,
//		XHTML and XHTML code insert into the html tag <pre></pre> to
//		improve the reading quality
//
// Releases:
// 		v1.4b - 2007 06 13:
//			- fix main regular expression
//			- modify HTML regex for XHTML tag
//			- adding check for HTML comments
//
// 		v1.3 - 2007 06 12:
//			- fix some bugs
//			- add substitute for HTML tag
//
// 		v1.2 - 2007 06 11:
//			- remove external stylesheet
//			- adding DOM stylesheet
//
// 		v1.1 - 2007 06 10:
//			- fix "string" color code
//			- add new color for system function
//			- main regexp modified
//
// 		v1.0 - 2006 06 06:
//			- first release of this software
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// If you want to use this software you don't remove this head.
///////////////////////////////////////////////////////////////////////

// JavaScript code array
var el = new Array();	// JS code
var cl = new Array();	// JS class

el[0] = new Array (  'if'
					,'else'
					,'try'
					,'catch'
					,'switch'
					,'case'
					,'break'
					,'for'
					,'in'
					,'function'
					,'this'
					,'delete'
					,'return'
					,'finally'
					,'firstChild'
					,'nodeValue'
					,'while'
					,'appendChild'
					,'removeChild'
					,'display'
					,'nodeName'
					,'childNodes'
					,'previousSibling'
					,'nextSibling'
					,'parentNode'
				);
cl[0] = 'el';

el[1] = new Array (	 'false'
					,'new'
					,'var'
					,'true'
					,'null'
				);
cl[1] = 'vr';
				
el[2] = new Array (	 'window'
					,'parent'
					,'ActiveXObject'
					,'open'
					,'escape'
					,'alert'
					,'document'
					,'innerHTML'
					,'innerText'
					,'replace'
					,'className'
					,'length'
					,'getElementsByTagName'
					,'getElementById'
					,'readyState'
					,'setInterval'
					,'clearInterval'
					,'statusText'
					,'status'
					,'length'
					,'replace'
					,'find'
					,'match'
					,'split'
					,'test'
					,'exec'
					,'eval'
					,'createElement'
					,'setTimeout'
					,'body'
					,'style'
					,'prototype'
					,'arguments'
					,'XMLHttpRequest'
					,'setRequestHeader'
					,'responseText'
					,'responseXML'
					,'send'
					,'setAttribute'
					,'getAttribute'
					,'src'
					,'value'
					,'focus'
					,'toString'
					,'abs'
					,'parseInt'
					,'parseFloat'
					,'getDate'
					,'setMonth'
					,'setFullYear'
					,'indexOf'
					,'id'
				);
cl[2] = 'js';

el[3] = new Array (	 'Array'
					,'String'
					,'Math'
					,'Event'
					,'RegExp'
					,'HTMLElement'
					,'Date'
					,'Object'
				);
cl[3] = 'oj';

// PHP - no fixed
el[4] = new Array (	 'print'
					,'header'
					,'utf8_encode'
					,'mysql_connect'
					,'mysql_select_db'
					,'mysql_fetch_assoc'
					,'strtoupper'
					,'substr'
					,'preg_match'
				);
cl[4] = 'ph';

// HTML code array
var ht = new Array();	// html
var hl = new Array();	// html class
var pr = new Array();	// params
var pl = new Array();	// params class

ht[0] = new Array (	 'body'
					,'head'
					,'html'
					,'table'
					,'tr'
					,'td'
					,'tbody'
					,'thead'
					,'div'
					,'span'
					,'img'
					,'a'
					,'b'
					,'strong'
					,'i'
					,'form'
					,'input'
					,'button'
					,'select'
					,'textarea'
					,'br'
					,'pre'
					,'embed'
					,'param'
					,'script'
					,'style'
					,'link'
					,'frame'
					,'iframe'
					,'frameset'
				);
hl[0] = 'el';

pr[0] = new Array (	 'cellspacing'
					,'cellpadding'
					,'style'
					,'class'
					,'id'
					,'align'
					,'width'
					,'height'
					,'border'
					,'src'
					,'href'
					,'cols'
					,'rows'
					,'maxlength'
					,'name'
					,'action'
					,'method'
					,'enctype'
					,'type'
					,'value'
					,'onload'
					,'onmouseover'
					,'onmouseout'
					,'onkeydown'
					,'onkeyup'
					,'onkeypress'
					,'onclick'
					,'onfocus'
					,'onblur'
				);
pl[0] = 'js';


// Stylesheet
var css = [], pcs = [];

pcs[0] = "pre.code";
css[0] = "color: #4B597B;" + 
		 "background-color: #ECFBFB;" +
		 "letter-spacing: -.05em;" +
		 "margin: 0 20px 0 20px;" +
		 "border: 1px dashed Navy;" +
		 "padding: 10px 0 10px 10px;" +
		 "font-size: 12px;" +
		 "overflow: hidden;";

pcs[1] = "pre.code span.js";
css[1] = "color: orange;";

pcs[2] = "pre.code span.ph,pre.code span.vr";
css[2] = "color: green;";

pcs[3] = "pre.code span.el";
css[3] = "color: blue;";

pcs[4] = "pre.code span.oj";
css[4] = "color: red;";

pcs[5] = "pre.code span.comment,pre.code span.comment span.ph,pre.code span.comment span.js,pre.code span.comment span.oj,pre.code span.comment span.vr,pre.code span.comment span.el";
css[5] = "color: #C95F3E;";

pcs[6] = "pre.code span.texts,pre.code span.texts span.ph,pre.code span.texts span.js,pre.code span.texts span.oj,pre.code span.texts span.vr,pre.code span.texts span.el";
css[6] = "color: #999;";

pcs[7] = "pre.code span.norm,pre.code span.norm span.ph,pre.code span.norm span.js,pre.code span.norm span.oj,pre.code span.norm span.vr,pre.code span.norm span.el";
css[7] = "color: #4B597B;";

///////////////////////////////////////////////////////////////////////////////
// PROGRAM - No modify after this comment
///////////////////////////////////////////////////////////////////////////////
function checkcode() {
	// Creating program stylesheet
	// Verify if the style tag exists, otherwise I create it 
	if ( ! document.getElementsByTagName('style').length ) {
		var head = document.getElementsByTagName('head')[0];
		var styl = document.createElement('style');
		styl.setAttribute('type', 'text/css');
		head.appendChild(styl);
	}
	
	if ( document.styleSheets ) {
		var styl = document.styleSheets[0];
	} else {
		var styl = document.getElementsByTagName('style')[0].sheet;
	}
	
	for ( var j = 0; j < pcs.length; j++ ) {
		if ( styl.insertRule ) {
			styl.insertRule( pcs[j] + " {" + css[j] + "}", styl.cssRules.length);
		} else {
			if ( pcs[j].indexOf(",") == -1 ) {
				styl.addRule(pcs[j], css[j]);
			} else {
				var tmp = pcs[j].split(",");
				for ( var k = 0; k < tmp.length; k++ ) {
					styl.addRule(tmp[k], css[j]);
				}
			}
		}
	}
	// End Creating program stylesheet
	
	var pre = document.getElementsByTagName('pre');
	for ( var j = 0; j < pre.length; j++ ) {
		if ( pre[j].className == 'code' ) {
			// Check for code
			var html = pre[j].innerHTML;
			html = html.replace(/(&lt;)/g, "¦").replace(/(&gt;)/g, "§");
			for ( i = 0; i < el.length; i++ ) {
				for ( var k = 0; k < el[i].length; k++ ) {
					var pattern = "([^¦/]|^)\\b" + el[i][k] + "\\b";
					var finding = new RegExp(pattern, 'g');
					var substit = "$1<span cls=ø" + cl[i] + "ø>" + el[i][k] + "</span>";
					html = html.replace(finding, substit);
				}
			}
			
			// Check for comments
			html = html.replace(/(\/\/.+)\r?/ig, "<span cls=øcommentø>$1</span>");
			
			// HTML comments
			html = html.replace(/¦!--/g, "¢").replace(/--§/, "¥");
			html = html.replace(/(¢[^¥]{0,}¥)/g, "<span cls=øcommentø>$1</span>");
			
			// Check for text into open / close tag
			html = html.replace(/(§)([^¦"¢¥]+)(¦)/g, "$1<span cls=ønormø>$2</span>$3");
			
			// HTML comments
			html = html.replace(/¢/g, "¦!--").replace(/¥/, "--§");
			
			// Check for HTML code and params
			for ( i = 0; i < ht.length; i++ ) {
				for ( var k = 0; k < ht[i].length; k++ ) {
					var pattern = "¦(/)?\\b" + ht[i][k] + "\\b([^§]+[^§])*(/)?§";
					var finding = new RegExp(pattern, 'g');
					var substit = "<span cls=ø" + hl[i] + "ø>¦$1" + ht[i][k] + "</span>$2<span cls=ø" + hl[i] + "ø>$3§</span>";
					var tmpreg  = finding.exec(html);
					var params  = RegExp.$2;
					
					// Check for params
					if ( params.replace(/^\s/g, "").replace(/\s$/, "") ) {
						var oldparams = params;
						
						for ( w = 0; w < pr.length; w++ ) {
							for ( var r = 0; r < pr[w].length; r++ ) {
								var spattern = "\\b" + pr[w][r] + "\\b";
								var sfinding = new RegExp(spattern, 'g');
								var ssubstit = "<span cls=ø" + pl[w] + "ø>" + pr[w][r] + "</span>";
								params = params.replace(sfinding, ssubstit);
							}
						}
						
						// Check for comments
						html = html.replace(/('[^'"]{0,}')/g, "<span cls=øtextsø>$1</span>");
						
						// Substitute params
						html = html.replace(oldparams, params);
					}
					
					// Substitute tag
					html = html.replace(finding, substit);
				}
			}
			
			// Check text into quot
			html = html.replace(/([^\\">¦])("[^"]{0,}")([^"])/g, "$1<span cls=øtextsø>$2</span>$3");
			
			// Check for xhtml tag
			html = html.replace(/(\s)?\/(<span cls=ø[\w]+ø>)§(<\/span>)/g, "$2$1/§$3");
			
			// Print output
			html = html.replace(/¦/g, "&lt;").replace(/§/g, "&gt;");
			html = html.replace(/(cls)/g, "class");
			html = html.replace(/ø/g, "'");
			pre[j].innerHTML = "<pre>" + html + "</pre>";
		}
	}
}
