//-----------------------------------------------------------------------------------------------------
function zPopWindow(zURL,zName,xSize,ySize,zOptLevel) {
	// a zOptLevel value of 1 yields a window with no ornamentation, bars, menus etc
	// a zOptLevel value of 2 yields the same window as 1, but with scroll bars
	// a zOptLevel value of 3 (or nothing, or anything but 1 or 2) yields a window with full menus, etc
	W = screen.width;
	H = screen.height;
    if (navigator.appVersion.lastIndexOf('Safari') != -1) {
		xSize += 0;
		ySize += 1;
    }
	if (!xSize) { xSize = W / 1.25; }
	if (!ySize) { ySize = H / 1.25; }
	if (!zName) { zName = "NewWindow"; }
	xPos = (W - xSize) / 2;
	yPos = (H - ySize) / 2;

	if (zOptLevel == 1) {
		var zWinOpts = 'height=' + ySize + ',width=' + xSize + ',left=' + xPos + ',top=' + yPos + 'screenX=' + xPos + ',screenY=' + yPos;
	} else if  (zOptLevel == 2) {
		var zWinOpts = 'height=' + ySize + ',width=' + xSize + ',resizable=yes,scrollbars=yes,left=' + xPos + ',top=' + yPos + 'screenX=' + xPos + ',screenY=' + yPos;
    } else {
		var zWinOpts = 'height=' + ySize + ',width=' + xSize + ',resizable=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,left=' + xPos + ',top=' + yPos + 'screenX=' + xPos + ',screenY=' + yPos;
	}

	var PopWin = window.open(zURL,zName,zWinOpts);

}
//-----------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------
// This is the implementation of SimpleSwap
// by Jehiah Czebotar
// Version 1.1 - June 10, 2005
//
// Include this script on your page
// then make image rollovers simple like:
// <img src="/images/ss_img.gif" oversrc="/images/ss_img_over.gif">
//
// http://jehiah.com/archive/simple-swap
// 


function SimpleSwap(el,which){
	el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
	var x = document.getElementsByTagName("img");
	for (var i=0;i<x.length;i++){
		var oversrc = x[i].getAttribute("oversrc");
		if (!oversrc) continue;
		// preload image
		// comment the next two lines to disable image pre-loading
		x[i].oversrc_img = new Image();
		x[i].oversrc_img.src=oversrc;
		// set event handlers
		x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
		x[i].onmouseout = new Function("SimpleSwap(this);");
		// save original src
		x[i].setAttribute("origsrc",x[i].src);
	}
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}
//-----------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------
// Form Validation Functions
// 

function zConfirmRedirect(prompt,okURL,cancelURL) {
	var confirmResult;
	// ask the user to choose either OK or Cancel
	confirmResult = confirm(prompt);
	// switch to a new URL based on the user's choice
	if (confirmResult) {
		window.location.href = okURL;
	} else {
		window.location.href = cancelURL;
	}
}

function AddUserFormValidate(theForm) {
	var why = "";
	why += isEmpty(theForm.firstname.value,"the user's first name");
	why += isEmpty(theForm.lastname.value,"the user's last name");
	why += checkEmail(theForm.email.value);
	why += isEmpty(theForm.username.value,"a username");
	why += isEmpty(theForm.password.value,"a password");
	if (why != "") {
		alert(why);
		return false;
	}
	return true;
}

// e-mail

function checkEmail(strng) {
	var error = "";
	if (strng == "") {
		error = "You must enter an e-mail address.\n";
	}
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		error = "You must enter a valid e-mail address.\n";
	} else {
		//test email for illegal characters
		var illegalChars = /[\(\)\,\;\:\\\"\[\]]/;
		if (strng.match(illegalChars)) {
			error = "The e-mail address contains illegal characters.\n";
		}
	}
	return error;    
}

// required textbox

function isEmpty(strng,messg) {
	var error = "";
	if (strng.length == 0) {
		error = "You must enter "+messg+".\n"
	}
	return error;     
}

//-----------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------
// Tooltip Functions
// 



// Extended Tooltip Javascript
// copyright 9th August 2002, 3rd July 2005
// by Stephen Chapman, Felgall Pty Ltd

// permission is granted to use this javascript provided that the below code is not altered
var DH = 0;
var an = 0;
var al = 0;
var ai = 0;
if (document.getElementById) {
	ai = 1;
	DH = 1;
} else {
	if (document.all) {
		al = 1;
		DH = 1;
	} else {
		browserVersion = parseInt(navigator.appVersion);
		if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {
			an = 1;
			DH = 1;
		}
	}
}
function fd(oi, wS) {
	if (ai) return wS ? document.getElementById(oi).style:document.getElementById(oi);
	if (al) return wS ? document.all[oi].style: document.all[oi];
	if (an) return document.layers[oi];
}
function pw() {
	return window.innerWidth != null? window.innerWidth: document.body.clientWidth != null? document.body.clientWidth:null;
}
function mouseX(evt) {
	if (evt.pageX) {
		return evt.pageX;
	} else if (evt.clientX) {
		return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	} else {
		return null;
	}
}
function mouseY(evt) {
	if (evt.pageY) {
		return evt.pageY;
	} else if (evt.clientY) {
		return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else {
		return null;
	}
}
function popUp(evt,oi) {
	if (DH) {
		var wp = pw();
		ds = fd(oi,1);
		dm = fd(oi,0);
		st = ds.visibility;
		if (dm.offsetWidth) {
			ew = dm.offsetWidth;
		} else if (dm.clip.width) {
			ew = dm.clip.width;
		}
		if (st == "visible" || st == "show") {
			ds.visibility = "hidden";
 		} else {
 			tv = mouseY(evt) + 20;
			lv = mouseX(evt) - (ew/4);
			if (lv < 2) {
				lv = 2;
			} else if (lv + ew > wp) {
				lv -= ew/2;
			}
			if (!an) {
				lv += 'px';
				tv += 'px';
			}
			ds.left = lv;
			ds.top = tv;
			ds.visibility = "visible";
		}
	}
}

function utf8_decode ( str_data ) {
    // Converts a UTF-8 encoded string to ISO-8859-1  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/utf8_decode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
    str_data += '';
    
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

function showOridinanceList(str) {
	console.log("Begin Oridinance List for \"" + str + "\"");
	for (var i=0; i < str.length; i++) {
		console.log(str.charCodeAt(i), str.charAt(i));
	};
}

/*
	Strip any invalid characters from the supplied string
*/
function filterInvalidCharacters(str) {
	var reg = /[^a-zA-Z0-9 `~!@#\$%\^&\*\(\)_\+=\-\{\}\[\]\|\\"':;,\.<>\?\/\n\r\u00a0\u00a1\u00a2\u00a3\u00a4\u00a5\u00a6\u00a7\u00a8\u00a9\u00aa\u00ab\u00ac\u00ad\u00ae\u00af\u00b0\u00b1\u00b2\u00b3\u00b4\u00b5\u00b6\u00b7\u00b8\u00b9\u00ba\u00bb\u00bc\u00bd\u00be\u00bf\u2022\u2026\u2122\u2190\u2191\u2192\u2193\u2194\u0022\u0026\u003c\u003e\u02c6\u02dc\u2013\u2014\u2018\u2019\u201a\u201c\u201d\u201e\u2030\u2039\u203a\u20ac]/g;
	var returnStr = str.replace(reg, "");
	// if (returnStr.length < str.length) {
	// 	// console.log(str);
	// 	console.log(showOridinanceList(str));
	// 	console.log(returnStr);
	// };
	return returnStr;
}

