var isWindows						= (navigator.userAgent.indexOf("Windows") >= 0);
var isLinux							= (navigator.userAgent.indexOf("Linux") >= 0);
var isMac							= (navigator.userAgent.indexOf("Mac") >= 0);
var isIE 							= (document.all && !window.opera ? true : false);
var isFirefox						= (navigator.userAgent.indexOf("Firefox") >= 0);
var isOpera 						= (window.opera ? true : false);
var isSafari						= (navigator.userAgent.indexOf("Safari") >= 0);
var isCamino						= (navigator.userAgent.indexOf("Camino") >= 0);
var isKonquerer						= (navigator.appName == "Konquerer");
var ieVersion						= -1;

if (isIE){
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	
	if (re.exec(navigator.userAgent) != null){
		ieVersion = parseFloat(RegExp.$1);
	}
}


/* *********** Form Functions *********** */
// IfEmpty
function errorIfEmpty(id, errorMsg){
	return errorIfEmptyV(document.getElementById(id).value, errorMsg);
}


function errorIfEmptyV(value, errorMsg){
	if (isEmptyV(value)){
		return errorMsg;
	} else {
		return "";
	}
}


function isEmpty(id){
	return isEmptyV(document.getElementById(id).value);
}


function isEmptyV(value){
	if (value.length == 0){
		return true;
	} else {
		return false;
	}
}


// IfLessThen
function errorIfLessThan(id, length, errorMsg){
	return errorIfLessThanV(document.getElementById(id).value, length, errorMsg);
}


function errorIfLessThanV(value, length, errorMsg){
	if (isLessThanV(value, length)){
		return errorMsg;
	} else {
		return "";
	}
}


function isLessThan(id, length){
	return isLessThanV(document.getElementById(id).value, length);
}


function isLessThanV(value, length){
	if (value.length < length){
		return true;
	} else {
		return false;
	}
}


// IfNotEqualTo
function errorIfNotEqual(id1, id2, errorMsg){
	return errorIfNotEqualV(document.getElementById(id1).value, document.getElementById(id2).value, errorMsg);
}


function errorIfNotEqualV(value1, value2, errorMsg){
	if (value1 == value2){
		return "";
	} else {
		return errorMsg;
	}
}


// IfEqualTo
function errorIfEqual(id1, id2, errorMsg){
	return errorIfEqualV(document.getElementById(id1).value, document.getElementById(id2).value, errorMsg);
}


function errorIfEqualV(value1, value2, errorMsg){
	if (value1 == value2){
		return errorMsg;
	} else {
		return "";
	}
}


// IfEqual
function isEqual(id1, id2){
	return isEqualV(document.getElementById(id1).value, document.getElementById(id2).value);
}


function isEqualV(value1, value2){
	if (value1 == value2){
		return true;
	} else {
		return false;
	}
}


// IfNotValidEmail
function errorIfNotValidEmail(id, errorMsg){
	return errorIfNotValidEmailV(document.getElementById(id).value, errorMsg);
}


function errorIfNotValidEmailV(value, errorMsg){
	if (isValidEmail(value)){
		return "";
	} else {
		return errorMsg;
	}
}


function isChecked(id){
	return document.getElementById(id).checked;
}


// ---------- Misc functions ----------
function isValidEmail(email){
	var re = new RegExp("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$","i");  
	
	return re.test(email);
}


function selectOption(list){
	document.getElementById(list).checked = true;
}


function getSelectValue(element){
	if (element.selectedIndex != -1){
		return element.options[element.selectedIndex].value;
	} else {
		return null;	// Nothing selected
	}
}


// Returns a URL encoded string of the select's selected values
function getSelectValuesEncodedForURL(id){
	var select = document.getElementById(id);
	var encodedName = encodeURIComponent(select.name);
	var querystring = "";
	
	if (select.selectedIndex != -1){
		for (var i = 0; i < select.options.length; i++){
			if (select.options[i].selected){
				querystring += encodedName + "=" + encodeURIComponent(select.options[i].value) + "&";
			}
		}
		
		querystring = querystring.substr(0, querystring.length - 1);
	}
	
	return querystring;
}


// Returns a URL encoded string of the textbox's value
function getTextboxValueEncodedForURL(id){
	var querystring = "";
	var textbox = document.getElementById(id);
	
	querystring = encodeURIComponent(textbox.name) + "=" + encodeURIComponent(textbox.value);
	
	return querystring;
}


function getCheckboxValueEncodedForURL(id){
	var querystring = "";
	var checkbox = document.getElementById(id);
	
	if (checkbox.checked){
		querystring = encodeURIComponent(checkbox.name) + "=" + encodeURIComponent(checkbox.value);
	} else {
		querystring = "";
	}
	
	return querystring;
}


function openCenteredWindow(url, name, options, width, height){
	var left = (screen.width  - width) / 2;
	var top = (screen.height - height) / 2;
	
	return openWindow(url, name, 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + (options.length ? ',' + options : ''));
}


/*
directories no		should the directories bar be shown? (Links bar)
location	no		specifies the presence of the location bar
resizable	no		specifies whether the window can be resized.
menubar		no		specifies the presence of the menu bar
toolbar		no		specifies the presence of the toolbar
scrollbars	no		specifies the presence of the scrollbars
status 		no		specifies the presence of the statusbar
*/

function openWindow(url, name, options){
	var popupWindow  = window.open(url, name, options);
	
	popupWindow.focus();
	
	return popupWindow;
}


// content should be URL encoded and look like a querystring
// callbacks are all optional, the xmlhttprequest object is provided as a parameter when they are called
// onFailureCallback is when a comm/server error has occured, not with the form validation (since it doesn't do any)
function ajaxPost(url, content, onSuccessCallback, onFailureCallback, onReadyStateChange){
	var xmlhttp = new XMLHttpRequest();		// XMLHttpRequest is browser neutralized by Sarissa

	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
									if (typeof(onReadyStateChange) == "function"){
										onReadyStateChange(xmlhttp);
									}

									if (xmlhttp.readyState == 4) {
										if (xmlhttp.status == 200){
											if (typeof(onSuccessCallback) == "function"){
												onSuccessCallback(xmlhttp);
											}
										} else {
											if (typeof(onFailureCallback) == "function"){
												onFailureCallback(xmlhttp);
											}
										}
									}
	}
	
	xmlhttp.send('ajax=1' + (content ? '&' +content : ''));
//	delete xmlhttp;
}