// Common Functions

function aoa_validateEmail(email)
{
	if ((email.length < 3) || (email.length > 50) || 
			(email.charAt(0) == '@') || (email.charAt(email.length-1) == '@') || 
			(email.charAt(0) == '.') || (email.charAt(email.length-1) == '.') || 
			(email.indexOf('.') == -1) || (email.indexOf('@') == -1) ||
			(email.indexOf('@') != email.lastIndexOf('@')) || 
			(email.indexOf(' ') > 0) || (email.indexOf('?') > 0) || (email.indexOf('..') > 0)
			)
	{
		return false;
	}
	else
	{
		return true;
	}
} 


function aoa_aoaValidateEmail(email, strict)
{
	if (email.length == 0) {
		errorMsg += "\nPlease enter your email address.";
		return false;
	}
	valid_chars = "[^ \(\)<>@,;:\\\"\[\]]";
	
	switch (strict) {
		case 1:
			if (!email.match("^.{2,}@.+$")) {
				errorMsg += "\nPlease supply a valid email address.";
				return false;
			} else {
				return true;
			}

			break;
		case 2:
			re = /^[^ \(\)<>@,;:\\\"\[\]]{2,}@[^ \(\)<>@,;:\\\"\[\]]+$/;
			reQuotas = /^\".*\"@[^ \(\)<>@,;:\\\"\[\]]+$/;
			if (reQuotas.test(email)) {
				return true;
			} else if (!re.test(email)) {
				errorMsg += "\nPlease supply a valid email address.";
				return false;
			} else {
				return true;
			}

			break;
		case 3:
			reQuotas = /^\".*\"@[^ \(\)<>@,;:\\\"\[\]]+\.[^ \(\)<>@,;:\\\"\[\]]+$/;
			re = /^[^ \(\)<>@,;:\\\"\[\]]{2,}@[^ \(\)<>@,;:\\\"\[\]]+\.[^ \(\)<>@,;:\\\"\[\]]+$/
			if (reQuotas.test(email)) {
				return true;
			}
			if (!re.test(email)) {
				errorMsg += "\nPlease supply a valid email address.";
				return false;
			} else {
				return true;
			}
			
			break;
	}

}


function aoa_popUp(page, w, h, left, top, resize, scrollbars)
{
	resWin = window.open(page, "", "dependent, height=" + h + ", width=" + w + ", left=" + left + ", top=" + top + ", resizable=" + resize + ", scrollbars=" + scrollbars);
}


function aoa_switchClass(obj,strClassName)
{
	obj.className	= strClassName;
}

function aoa_hasValueInArray(oNeedle, oHaystack)
{
	for (var i = 0; i < oHaystack.length; i++) {
		if (oNeedle == oHaystack[i]) {
			return true;
		}
	}
	return false;
}

function aoa_clearValueFromArray(oNeedle, oHaystack)
{
	var newArray = new Array();
	
	for (var i = 0; i < oHaystack.length; i++) {
		if (oNeedle != oHaystack[i]) {
			newArray[i] = oHaystack[i];
		}
	}
	
	return newArray;
}

function aoa_readCookie (cookieName)
{
	var cookieString = document.cookie;
	var cookieSet = cookieString.split (';');
	var setSize = cookieSet.length;
	var cookiePieces;
	var returnValue = "";
	var x = 0;
	
	for (x = 0; ((x < setSize) && (returnValue == "")); x++) {
		cookiePieces = cookieSet[x].split ('=');
		
		if (cookiePieces[0].substring (0,1) == ' ') {
			cookiePieces[0] = cookiePieces[0].substring (1, cookiePieces[0].length);
		}
		
		if (cookiePieces[0] == cookieName) {
			returnValue = cookiePieces[1];
		}
	}
	
	return returnValue;
	
}

// End Common Functions

function aoa_deleteConfirm(question, idPart)
{
	var inputsDom = document.getElementsByTagName("input");
	var re = new RegExp(idPart, "i")
	
	for (i = 0; i < inputsDom.length; i++) {
		if (re.test(inputsDom[i].id)) {
			// Check if it is checked
			if (inputsDom[i].checked == true) {
				return confirm(question);
			}
		}
	}
	
	return true;
}

function aoa_loadData(nodeId, url, evalOnLoad)
{
	var nodeTo = dojo.byId(nodeId);
	var loadingSplashNode = dojo.byId('loadingSplash');
	nodeTo.innerHTML = loadingSplashNode.innerHTML;
	
	dojo.io.bind(
	{
		url: url,
		load: function(type, data, evnt) {nodeTo.innerHTML = data; if(evalOnLoad) { eval(evalOnLoad+'();'); }},
		error: function(type, error) {if (confirm('Error loading from '+url+'\r\n Try again?')) { loadData(nodeId, url); }},
		mimetype: "text/html"
	});
}

function aoa_clearSearch(formId, findClearId)
{
	document.getElementById(findClearId).value = 'yes';
	document.getElementById(formId).submit();
}

function aoa_showExtendedSearch(elementId)
{
	var elementNode = document.getElementById(elementId);
	elementNode.style.display = 'block';
}

function aoa_hideExtendedSearch(elementId)
{
	var elementNode = document.getElementById(elementId);
	elementNode.style.display = 'none';
}

function aoa_showBlock(blockId)
{
	document.getElementById(blockId).style.display = 'block';
}

function aoa_hideBlock(blockId)
{
	document.getElementById(blockId).style.display = 'none';
}

var AOA = {};

function aoa_toggleBlock(blockId)
{
	if (document.getElementById(blockId).style.display == 'none') {
		aoa_showBlock(blockId);
	} else {
		aoa_hideBlock(blockId);
	}
}

// site specific

function aoa_fixMultipleSelection(select, allValue)
{
	// If value different form the allValue is selected, disselect the allValue option
	var bToDisselect = false;
	
	if (select.options.length > 0) {
		for(var i = 0; i < select.options.length; i++) {
			if (select.options[i].selected && select.options[i].value != allValue) {
				bToDisselect = true;
				break;
			}
		}
		
		if (bToDisselect) {
		for(var i = 0; i < select.options.length; i++) {
				if (select.options[i].value == allValue) {
					select.options[i].selected = false;
					break;
				}
			}
		}
	}
}

function aoa_showLoadingSplash(nodeName)
{
	var nodeTo = document.getElementById(nodeName);
	var loadingSplashNode = document.getElementById('loadingSplash');
	nodeTo.innerHTML = loadingSplashNode.innerHTML;
}

function aoa_updateNode(containerName, url)
{
	aoa_showLoadingSplash(containerName);
	new Ajax.Updater(containerName, url, {'evalScripts': true});
}

function aoa_updateNodePost(containerName, url, formNode)
{
	aoa_showLoadingSplash(containerName);
	new Ajax.Updater(containerName, url, {'evalScripts': true, asynchronous:true, parameters:Form.serialize(formNode)});
}


function aoa_showPrototypeWindow(containerId, properties)
{
	var win = new Window(properties);
	win.getContent().innerHTML = '<div id="'+containerId+'"></div>';
	win.showCenter();   
}

function aoa_postAjaxForm(formId)
{
	$(formId).request({
		method: 'post',
		'evalScripts': true,
		onComplete: aoa_selfUpdate,
		onError: aoa_errorPostAjaxForm
	})
}

function aoa_errorPostAjaxForm(e)
{
	alert('Connection failed!');
}

function aoa_init()
{
	// Can be overwritten in the page source
}
