addEvent(window,'load',onLoadCookie);

var activeGlossary = '';
var activeGlossaryLink = '';
function toggleGlossary(toggleMe,link)
{
	if(activeGlossary == toggleMe)
	{
		setClassName(link,'');
		toggleDisplay(toggleMe);
		activeGlossary = '';
		activeGlossaryLink = '';
	}
	else
	{
		if(activeGlossary != '')
		{
			toggleDisplay(activeGlossary);
			setClassName(activeGlossaryLink,'');
		}
		activeGlossaryLink = link;
		toggleDisplay(toggleMe);
		activeGlossary = toggleMe;
		setClassName(link, 'active_link');
	}
}
var activeFaq = '';
var activeFaqLink = '';
function toggleFaq(toggleMe,link)
{
	if(activeFaq == toggleMe)
	{
		setClassName(link,'');
		toggleDisplay(toggleMe);
		activeFaq = '';
		activeFaqLink = '';
	}
	else
	{
		if(activeFaq != '')
		{
			toggleDisplay(activeFaq);
			setClassName(activeFaqLink,'');
		}
		activeFaqLink = link;
		toggleDisplay(toggleMe);
		activeFaq = toggleMe;
		setClassName(link, 'selected');
	}
}
//
// UTILITY FUNCTIONS
//
function showHide(show,hide)
{
	if(show != '')
	{
		showdiv(show);
	}
	if(hide != '')
	{
		hidediv(hide);
	}
}
function hidediv(id) 	
{
	document.getElementById(id).style.display = 'none';
}    
function showdiv(id) 	
{
	document.getElementById(id).style.display = 'block';
}
function setClassName(div, classname)
{
	if(document.getElementById(div))
	{
		document.getElementById(div).setAttribute("class", classname);
		document.getElementById(div).setAttribute("className", classname);
	}
}

function toggleDisplay(toggleMe) {
  var elem, vis;
  if(document.getElementById) // this is the way the standards work
    elem = document.getElementById(toggleMe);
  else if(document.all) // this is the way old msie versions work
      elem = document.all[toggleMe];
  else if(document.layers) // this is the way nn4 works
    elem = document.layers[toggleMe];
  vis = elem.style;
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
function setFocus(focusMe)
{
	var elem;
  if(document.getElementById) // this is the way the standards work
    elem = document.getElementById(focusMe);
  else if(document.all) // this is the way old msie versions work
      elem = document.all[focusMe];
  else if(document.layers) // this is the way nn4 works
    elem = document.layers[focusMe];
  elem.focus();
}
function toggleElements()
{
	var id = 'main_content';
	var elem;
	if(document.getElementById) // this is the way the standards work
		elem = document.getElementById(id);
    else if(document.all) // this is the way old msie versions work
		elem = document.all[id];
	else if(document.layers) // this is the way nn4 works
		elem = document.layers[id];
	
	var i, a, vis;
	for(i=0; (a = elem.getElementsByTagName("embed")[i]);i++)
	{
		vis = a.style;
		if(vis.display==''&&a.offsetWidth!=undefined&&a.offsetHeight!=undefined)
			vis.display = (a.offsetWidth!=0&&a.offsetHeight!=0)?'block':'none';
		vis.display = (vis.display==''||vis.display=='block')?'none':'block';
	}
}
function showPopup(link,name)
{
	window.open(link,name,'height=400,width=450,alwaysRaised=yes,scrollbars=yes,status=no,toolbar=no');
}
function setEnabled(id,enabled) {
	if(document.getElementById(id))
	{
		if (enabled)
			document.getElementById(id).removeAttribute("disabled");
		else
			document.getElementById(id).setAttribute("disabled", "disabled");
	}
}
//
// Adds an event, e.g. an OnLoad event.
//
function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else 
	{ 
		return false; 
	} 
}
function resetButton()
{		
	var elem = document.getElementById('chk_terms_of_use');
	if(elem != null)
	{
		setEnabled('btn_create_user',elem.checked);
	}
}
//
//Styleswitcher.js - A style script written by Paul Sowden
//
function setActiveStyleSheet(title) 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
    {
      a.disabled = true;
      if(a.getAttribute("title") == title) 
        a.disabled = false;
    }
  }
}
function getActiveStyleSheet() 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) 
        return a.getAttribute("title");
  }
  return null;
}
function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}
function onLoadCookie()
{
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

//
// Cookie scripts
//
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else
		var expires = "";
	
	document.cookie = name + "=" + value + expires + "; path=/";

}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {
		var c = ca[i];

		while (c.charAt(0)== ' ') 
			c = c.substring(1,c.length);

		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length,c.length);
	}
	
	return null;			
}
function displayCookie(name) {
	var value = readCookie(name);

	if (value != null)
		alert(name + ' = ' + value);
}

/*
	ClientSide Validations for Forms2 Module
	Version: 1.0
*/

var validations = [];
function AddRequiredValidation(formId, fieldId, message) {
	var val = GetValidation(formId, fieldId);
	val.required = true;
	val.message = message;
}
function AddRegexValidation(formId, fieldId, regex, message) {
	var val = GetValidation(formId, fieldId);
	val.regex = regex;
	val.message = message;
}
function GetValidation(formId, fieldId) {
	// validation already defined?
	var i;
	for (i=0; i<validations.length; i++) {
		if (validations[i].fieldId == fieldId && validations[i].formId == formId) {
			return validations[i];
		}
	}
	
	// add new validation 
	var newValidation = {
		formId: formId,
		fieldId:  fieldId,
		required: false,
		regex: "",
		message: ""
	};
		
	validations.push(newValidation);
	return newValidation;
}
function ValidateForm(formId, localGeneralMessage) {
	var localMessage = localGeneralMessage || "Please correct the following errors:\n";
	
	var errors = [];
	var firstErrorField;
	
	var i;
	for (i=0; i<validations.length; i++) {
		var validation = validations[i];
		if(validation.formId == formId) {
			var field = document.getElementById(validation.fieldId);
			
			var valid = false;
			switch(field.tagName.toUpperCase()) {
				case "INPUT":
					switch(field.type.toUpperCase()) {
						case "TEXT":
						case "PASSWORD":
							valid = ValidateValue(field.value, validation.required, validation.regex);
							break;
						// TODO: other INPUT types to validate?
					}
					break;
				case "TEXTAREA":
					valid = ValidateValue(field.value, validation.required, validation.regex);
					break;
				case "SELECT":
					valid = ValidateValue(field.value, validation.required, validation.regex);
					break;
			}
			if (valid == false) {
				errors.push(validation.message);
				firstErrorField = firstErrorField || validation.fieldId;
			}		
		}
	}
	if (errors.length > 0) {
		var allErrors = "";
		for (i=0; i<errors.length; i++) {
			allErrors += "\n- " + errors[i];
		}
		alert(localMessage + allErrors);
		setTimeout("FocusFormField('" + firstErrorField + "');", 100);
		return false;
	}
	
	return true;
}
function ValidateValue(value, required, regex) {
	if (required && trim(value) == "") {
		return false;
	}
	if (regex != "") {
		var regExValidation = new RegExp(regex);
		var error = regExValidation.test(value);
		return error;
	}
	return true;
}
function FocusFormField(field) {
	var element = document.getElementById(field);
	if (element) {
		element.focus();
		element.select();
	}
}
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}