// SCRIPTS FOR GOWILKES.COM  

var webhomeURL = "http://www.web-wilkes.com";
var xx;

// Prevent Right Clicks 
document.onmousedown=right
document.oncontextmenu=context

function right(e) { 
	if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)) { return false }
	return true
}

function context() { return false }

// Show Arrow 
function showArrow(loc, flip, secure) {
	if (secure == "true") { homeURL = "https://server1.web-wilkes.com/~gowilkes" }

	if (flip == "on") { var arrowPath = homeURL + "/images/arrow.gif" }
	if (flip == "off") { var arrowPath = homeURL + "/images/spacer.gif" }

	if (document.getElementById) { document.getElementById(loc).innerHTML = "<img src=" + arrowPath + " width=13 height=8 border=0>" }
	else if (document.all) { document.all[loc].innerHTML = "<img src=" + arrowPath + " width=13 height=8 border=0>" }
	else if (document.layers) { 
		document.layers[loc].document.write("<img src=" + arrowPath + " width=13 height=8 border=0>");
		document.layers[loc].document.close();
	}
}

// Find the Screen Size 
function screenSize() {
	if (screen.width >= 1024) { document.write("<link rel='stylesheet' type='text/css' href='" + homeURL + "/includes/styles_big.css'>"); return }
	else { document.write("<link rel='stylesheet' type='text/css' href='" + homeURL + "/includes/styles.css'>"); return }
}

/////////////////////// GAMES  ///////////////////////
//
// Get the Games from the Menu 
	function goGames() {
		if (document.game.dropdown.value == "0") { return false }
		else if (document.game.dropdown.value == "pong") { openPong() }
		else if (document.game.dropdown.value == "tetris") { openTetris() }
		else  {
			url = homeURL + "/cgi-bin/games.cgi?game=" + document.game.dropdown.value;

	    var l = (screen.availWidth - 675) / 2;
	    var t = (screen.availHeight - 400) / 2;
	    var cfg  = "width=675,height=400";
				cfg += ",top=" + t;
				cfg += ",left=" + l;
				cfg += ",resizable,titlebar=1";

			window.open(url, 'Games', cfg)
		}
	}

// Pong, from Games.cgi page
	function openPong() {
    var l = (screen.availWidth - 450) / 2;
    var t = (screen.availHeight - 350) / 2;
    var cfg  = "width=450,height=350";
			cfg += ",top=" + t;
			cfg += ",left=" + l;
			cfg += ",resizable,titlebar=1";

		window.open(webhomeURL + '/games/pong.html', 'Games', cfg)
	}

// Tetris, from Games.cgi page
	function openTetris() {
		var arrImage=new Array(
			webhomeURL + "/games/block0.gif",
			webhomeURL + "/games/block1.gif",
			webhomeURL + "/games/block2.gif",
			webhomeURL + "/games/block3.gif",
			webhomeURL + "/games/block4.gif",
			webhomeURL + "/games/block5.gif",
			webhomeURL + "/games/block6.gif",
			webhomeURL + "/games/blockb.gif"
		)

		for (i=0; i < arrImage.length; i++) {
			arrImage[i] = new Image();
			arrImage[i].src = arrImage[i];
		}

    var l = (screen.availWidth - 360) / 2;
    var t = (screen.availHeight - 400) / 2;
    var cfg  = "width=360,height=400";
			cfg += ",top=" + t;
			cfg += ",left=" + l;
			cfg += ",resizable,titlebar=1";

		if (document.layers) { window.open(webhomeURL + '/games/tetrisnn.htm', 'Games', cfg) }
		else if (document.all) { window.open(webhomeURL + '/games/tetrisie.htm', 'Games', cfg) }
		else { alert("Sorry, but your browser does not support this game. Please try one of the other games.") }
	}


/////////////////  COMMUNITY VOICE  //////////////////
//
	function changeCVSpan(v, x) {
		// Insert Image via pasteHTML 
		if (v == "InsertImage") {
			var sel = document.selection;
			if (sel != null) {
				var rng = sel.createRange();
				if (rng != null) rng.pasteHTML(" &nbsp;<img src=" + x + " align=middle border=0 unselectable=on> ");
			}
		}

		// Change font color 
		 else if (v == "ForeColor" && x == "") {
			var oldcolor = DecToRgb(comment.document.queryCommandValue(v));
			var newcolor = showModalDialog(homeURL + "/select_color.html", oldcolor, "dialogWidth: 239px; dialogHeight: 214px; resizable: off; help: off; status: off; scroll: off");
			if (newcolor != null) document.execCommand(v, false, "#"+newcolor);
		}

		// Just send the command 
		else document.execCommand(v, false, x);

		return;
	}


	// OLDER BROWSERS USE THESE FUNCTIONS INSTEAD
	// 
	// Add bold or italics 
	function changeCVText(v) {
		if (document.selection) {
			var str = document.selection.createRange().text;

			if (str == "") {
				alert("Please highlight something to be enhanced.");
				document.reply.comment.focus();
				return false;
			}

			else {
				var sel = document.selection.createRange();
				sel.text = "<" + v + ">" + str + "</" + v + ">";

				return;
			}
		}

		else { alert("This browser does not support this function."); return false; }
	}

	// Change the Font Color 
	function changeCVColor(x) {
		if (document.selection) {
			var str = document.selection.createRange().text;

			if (str == "") {
				alert("Please highlight something to be enhanced.");
				document.reply.comment.focus();
				return false;
			}

			else {
				document.reply.comment.focus();
				var sel = document.selection.createRange();
				sel.text = "<font color='" + x + "'>" + str + "</font>";

				return;
			}
		}

		else { alert("This browser does not support this function."); return false; }
	}

	// Add Emoticons 
	function CVemoticon(z) {
		var txtarea = document.getElementById('comment').innerHTML;
		z = ' ' + z + ' ';

		if (txtarea.createTextRange && txtarea.caretPos) {
			var caretPos = txtarea.caretPos;
			caretPos.z = caretPos.z.charAt(caretPos.z.length - 1) == ' ' ? caretPos.z + z + ' ' : caretPos.z + z;
			txtarea.focus();
		}

		else {
			txtarea.value += z;
			txtarea.focus();
		}
	}

	// ERROR FUNCTIONS 
	//
	// Strip tabs from comment textarea 
	function changeCVFlags() {
		if (!(oDiv.isContentEditable)) {
			var comm = document.reply.comment.value;

			comm = comm.replace(/	/g, ' ');
			document.reply.comment.value = comm;
		}
	}

	// If ContentEditable, set up the Comment value 
	function setCVComment() {
		if (oDiv.isContentEditable) document.reply.commentVal.value = document.getElementById('comment').innerHTML;
	}

	// Error Check 
	function checkCVForm() {
		var message;
		var pinpoint;

		if (document.reply.name.value == "") { message = "The USERNAME field is required."; pinpoint = "name"; }
		else if (document.reply.subject.value == "") { message = "The SUBJECT field is required."; pinpoint = "subject"; }
		else if (document.reply.password.value == "") { message = "The PASSWORD field is required."; pinpoint = "password"; }

		else if (!(oDiv.isContentEditable) && document.reply.comment.value == "") { message = "The MESSAGE field is required."; pinpoint = "comment"; }
		else if (oDiv.isContentEditable && document.getElementById('comment').innerHTML == "<DIV></DIV>") { message = "The MESSAGE field is required."; }

		if (message) {
			alert(message);

			if (pinpoint) {
				document.reply[pinpoint].style.border = "2px solid #FF0000";
				document.reply[pinpoint].focus();
			}

			return false;
		}

		else {
			document.reply.submit.style.color = "#C0C0C0";
			document.reply.submit.value= 'Processing';
			document.reply.submit.disabled= true;

			return true;
		}
	}


//////////////////////  SHARED  //////////////////////
//
// Get Cookie 
function getCookies(tag) {
	var value = null;
	var myCookie = document.cookie + ";";
	var findTag = tag + "=";
	var endPos;

	if (myCookie.length > 0) {
		var beginPos = myCookie.indexOf(findTag);
		if (beginPos != -1) {
			beginPos = beginPos + findTag.length;
			endPos = myCookie.indexOf(";", beginPos);
			if (endPos == -1) endPos = myCookie.length;
			value = unescape(myCookie.substring(beginPos, endPos));
		}
	}

	return value;
}

// Convert Dec to RGB 
function DecToRgb(value) {
	var hex_string = "";
	for (var hexpair = 0; hexpair < 3; hexpair++) { var myByte = value & 0xFF; value >>= 8; var nybble2 = myByte & 0x0F; var nybble1 = (myByte >> 4) & 0x0F; hex_string += nybble1.toString(16); hex_string += nybble2.toString(16); }
	return hex_string.toUpperCase();
}


// Parse 
function getUrl(name) {
	var value = null;
	urlvar = unescape(document.location);
	var urlarray = urlvar.split("&");

	for (i in urlarray) {
		broken_url = urlarray[i].split("=");
		if (broken_url[0] == name) value = broken_url[1];
	}

	return value;
}
