// Copyright 2004-2010 by Matt Conescu. All rights reserved.

var indx = 0;
var inits = [];
var heads = [];
var areas = [];
var count = [0,0];
var optn = '<OPTION value=';
var sheet = document.styleSheets;
var links = document.URL.split('?'); 
var parm = unescape(location.search.slice(1));
var cmds = parm.match(/(^sort,|^text|^view|doc$|pdf$)/); 

var menus = ['<OPTION>Resume Formats',
		optn + '?sort,0> &raquo; Printer Friendly',
		optn + '?sort,1,2,3> &raquo; Recruiter (Blind)',    
		optn + '?sort,0,3,2,1> &raquo; Curriculum Vitae',
		optn + '?text> &raquo; Plain Text',
		optn + '?.doc> &raquo; Word', 
		optn + '?.pdf> &raquo; PDF']; 

var html = ['<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>',
		'\n<TR>\n<TD NOWRAP COLSPAN=3 CLASS=',
		'col VALIGN=top>',
		'<FONT CLASS=txt>',
		'\n<TR>\n<TD>&nbsp;',
		'\n</TABLE>\n',
		'<TR CLASS=box>',
		'<BR>&nbsp;\n<CENTER>&copy; 2004-' + (new Date()).getFullYear() + ' Matt Conescu. All rights reserved.'];

if (cmds) {	var print = 'print';
		var menu = optn + links[0] + '> &laquo; ' + document.title;
		for (w=1; w<menus.length; w++) {
			if (menus[w].match(parm + '>') || menus[w].match('.' + cmds[0] + '>')) { 
				indx = w;  var work = menus[w].split(' &raquo; ');
				menus[0] = menu; menus[w] = '<OPTION SELECTED>' + work[1];   
				document.title = work[1] + ' Resume'; break;}}
		if (indx == 0) {document.title = 'Customized (' + parm + ') Resume'; menus[0] += menu;}}												
										
else {print = 'resume';
	var cols = ['.ask','.name','center']; 
		if (parm.match(/^find,/)) {
			var col = 255; var click = parm.substr(5); document.title += ' about ' + click;}
		else {col = 155;}}

document.write('<SCRIPT SRC=resume.php?page=' + escape(document.title) + '><\/SCRIPT><LINK REL=stylesheet TYPE=text/css HREF=' + print + '.css>');

function search(text, sort, edits) {

var n = 0;
var ranges = [];
var newHTML = '';
var pass = [1,0,0];
var state = /\b(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)\b\S/;

if (!sort) {sort = '0';}
var sorts = sort.split(',');
var lines = text.split('\n');
if (!lines[lines.length-1]) {lines.splice(lines.length-1,1);}

if (cmds || click && edits) {
	var parms = parm.split(','); 
	for (var i=0; i<parms.length; i++) {
		if (parms[0] == 'view') {newHTML += '<PLAINTEXT><HTML>\n<HEAD>\n';
			if (document.title > '') {newHTML += '<TITLE>' + document.title + '</TITLE>\n';} 
			if (sheet[0].cssText) {
			newHTML += '<STYLE>\n' + sheet[sheet.length-1].cssText + '</STYLE>';}
			else {newHTML += '<LINK REL=stylesheet TYPE=text/css HREF=' + sheet[sheet.length-1].href + '>';}
			newHTML += '\n</HEAD>\n<BODY>\n'; html[7] += '\n</BODY>\n</HTML>';
			if (i == parms.length-1) {break;}
			else {parms.splice(i,1);}}
		if (parms[i] == 'sort') {sorts = parms.slice(i+1); break;}
		if (parms[0] == 'find') {if (i > 0) {if (parm.split(parms[i]).length > 2) {continue;} 
								 finds.push(parms[i],'<SPAN CLASS=fnd>' + parms[i] + '</SPAN>');}
						else {var finds = [];}}}} 

for (i=0; i<lines.length; i++) {

	while (lines[i].match(/\s$/)) {lines[i] = lines[i].slice(0,-1);}

	if (finds && lines[i] == heads[2]) {edits = finds; pass.push(1);}
	
	if (edits) {for (w=0; w<edits.length; w++) { 
				if (lines[i].match(edits[w])) {
					if (edits[w+1].match(/^(mailto|\?)/)) {
					lines[i] = lines[i].replace(edits[w],'<A HREF=' + edits[w+1] + '>' + edits[w] + '</A>');
					edits.splice(w,2); break;}
					else {lines[i] = lines[i].split(edits[w]).join(edits[w+1]);}} w += 1;}}				
	
	else {	if (lines[i].match(/~/)) {works = lines[i].split('~'); lines[i] = works[0]; 
				if (!cmds) { if (works[1].substr(0,1) != ',') {inits.push(works[0],works.join(' '));}
					   	 else {work = works[0].substr(works[0].match(/\w/).index);
							 if (click && click == work) {parm = 'find' + works[1];}
							 else	{inits.push(work,'?find,' + escape(work));}}}}

			else if (!cmds && n > 1) {var mails = text.match(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/gi); 
							  if (mails) {inits.push(mails[0],'mailto:' + mails[0] + '?subject=' + escape(document.title));}
							  inits.push(heads[0],'<BR>Contact Information<BR>',heads[1],' ',heads[2],heads[2].toUpperCase());	
							  return(lines.join('\n'));}}
	
	var words = lines[i].replace(/\s+/,' ').split(' ');
	
	if (words.length > 1 && pass[1] == 0 || words[0].match(/^[A-Z]+[a-z]*$/) && (words.length < 3 && pass[2] == 0 || lines[i] == lines[i].toUpperCase())) { 
			
		if (lines[i] == lines[i].toUpperCase()) {
						for (w=0; w<words.length; w++) { 
							if (words[w].length < 3) {words[w] = words[w].toLowerCase();} 
							else {words[w] = words[w].substr(0,1) + words[w].slice(1).toLowerCase();}}
						heads.push(words.join(' ')); if (parm != 'text') {lines[i] = words.join(' ');} 
						pass[0] = 1;}
		if (pass[0] > 2 ) 	{areas[n] += html[1].substr(5) + html[2] + lines[i];
						pass[1] = 1;}	    
		if (pass[0] == 2) 	{areas[n] += html[4] + html[5] + html[0] + html[1] + html[2] + lines[i];}
		if (pass[0] == 1) 	{if (pass[1] == 0) {areas[n] = html[0] + html[1] + 'top>' + lines[i]; 
							if (!heads[0]) {heads.push(lines[i]);} 
						pass[0] = 0;}
						else {areas[n] += html[5]; n += 1; 
							if (sorts[0] == n) {areas[n] = html[0];} 
							else {areas[n] = html[0] + html[4];} 
							areas[n] += html[1] + 'bar>' + lines[i];}
						pass[1] = 1;}	    
		pass[0] += 1; pass[2] = 0; words = '';}
	
	if (words.length > 2 && ((lines[i].match(/^\w/) && (lines[i].match(state) || lines[i].match(/(19|20)\d\d\s*[-]/))))) {
		
		pass[2] = 0;
		if (pass[3]) 	{years = lines[i].match(/(19|20)\d\d/g);
					if (years && !years[1]) {years[1] = (new Date()).getFullYear();}}
 
		for (w=words.length-1; w>0; w--) { 
						if (pass[2] == 2 && words[w].match(/\W$/)) {words[w] += html[3]; break;}    
						if (pass[2] == 1 && words[w].match(/\W$/)) {pass[2] = 2;} 
						if (pass[2] == 0 && words[w].match(state)) {pass[2] = 1;}} 
		areas[n] += html[4] + html[1] + 'row>' + words.join(' ');
		pass[0] = 1; pass[1] = 1; words = '';}

	if (words.length > 0 && (words[0].match(/^\w/) || words[0].match(/^\W/))) { 
		
		if (pass[1] == 1) 	{if (pass[0] == 2) {areas[n] += html[1] + 'txt>\n<BR>';
						pass[0] = 1;}
						else {if (pass[2] == 0) {areas[n] += html[3];} areas[n] += '\n<BR>';}
						pass[1] += 1;}
		
		if (words[0].match(/^\W+$/) && words[1] != '<A') {words[0] = '\n<LI>'; areas[n] += words.join(' ');}
	 	else if (pass[2] == 2) {areas[n] += '\n<I>' + lines[i] + '</I>\n<BR>';}
			else {if (words[1] == '<A') {words.splice(0,1);} areas[n] += '\n' + words.join(' ')  + '\n<BR>';}
		pass[2] = 0;}

	if (pass[3]) {var counts = lines[i].match(/(\w|\s|^)<S/g); 
			  if (counts) {count[0] += counts.length; 
			  		   if (years[0]) {for (w=years[0]; w<years[1]; w++) {var year = w - 1950;
								if (!ranges[year]) {count[1] += 1; ranges[year] = 1;}} years = [];}}}}

if (parm == 'text') {html[7] = '\n\nCopyright' + html[7].substr(25); return('<PLAINTEXT>' + lines.join('\n'));}

if (areas[n]) {areas[n] += html[5];}

if (sorts.length > 1) { 
	for (w=0; w<sorts.length; w++) { 	
	if (areas[sorts[w]]) {newHTML += areas[sorts[w]]; areas[sorts[w]] = '';}}}
else {if (sorts[0] > 0) {areas[0] = areas[sorts[0]]; areas.splice(sorts[0],1);} newHTML += areas.join('');}

return(newHTML);

}

function resume() { 

if (indx < 5) {
	init = search(document.getElementById('text').innerHTML); 
	if (cmds) {document.getElementById('text').innerHTML = heading() + html[5] + init + html[7];}
	else	{init = search(init,'1,2,3,0',inits);
		if (click) {init = init.replace('<LI> ' + click,html[6] + '<TD NOWRAP CLASS=tab>' + click + '<BR><I>' + count[0] + ' Matches Totaling ' + count[1] + ' Years' + 
	      		'<TD WIDTH=14px CLASS=exit>' + corners('tabc','1') + '<A HREF=' + links[0] + '>X</A>' + corners('tabc') + 
              		'<TD WIDTH=16px>&nbsp;' + html[1] + 'txt>');}
		document.getElementById('text').innerHTML = html[0] + '<TR><TD COLSPAN=2>' + heading() + areas.join('').replace(html[4],'') + '<TD NOWRAP ROWSPAN=2 VALIGN=top WIDTH=20%>' +
		html[0] + html[4] + init.replace(html[0],'') + html[6] + '<TD>' +
		corners('tabe') + html[6] + '<TD COLSPAN=2>&nbsp;' + html[1] + '>' + corners('boxc') + html[5] + html[7];}
	styles('.hide','display:inline'); if (!cmds) {fadeIn();}}
else {location = parm;}

}

function heading() {

var head = html[0] + '<TR><TD><SELECT onchange="location=this.value; selectedIndex=indx;">' + menus.join('</OPTION>').replace(/\?\./g,'?' + escape(heads[0]) + '.') + '</SELECT>';
if (!cmds) {head += '<TD ALIGN=center WIDTH=100% CLASS=name>Resume of ' + heads[0] + '</TABLE><TD NOWRAP CLASS=ask><I>Ask my Resume about&mdash;' + 
		html[1] + '>&nbsp;' + corners('boxc','1') + html[6] + '<TD NOWRAP ROWSPAN=3 WIDTH=18px>&nbsp;<TD NOWRAP VALIGN=top>&nbsp;'  + corners('tabe','1');} 
return(head);

}

function corners(cls,top) {
 
if (top) {work = '<DIV><b CLASS=' + cls + '><b CLASS=' + cls + '1><b></b></b><b CLASS=' + cls + '2><b></b></b><b CLASS=' + cls + '3></b><b CLASS=' + cls + '4></b><b CLASS=' + cls + '5></b></b><DIV CLASS=' + cls + 'fg>';}
else {work = '</DIV><b CLASS=' + cls + '><b CLASS=' + cls + '5></b><b CLASS=' + cls + '4></b><b CLASS=' + cls + '3></b><b CLASS=' + cls + '2><b></b></b><b CLASS=' + cls + '1><b></b></b></b></DIV>';}
return(work);

}

function fadeIn() { 

for (f=0; f<cols.length; f++) {styles(cols[f],'color:rgb(' + col + ',' + col + ',' + col + ')');}
col += 5; if (col < 255) setTimeout('fadeIn()',75); 

}

function styles(css,val) {

if (sheet[0].addRule) {sheet[0].addRule(css,val);}
else if (sheet[0].insertRule) {sheet[0].insertRule(css + '{' + val + '}',sheet[0].cssRules.length);}

}