var validators = new Array();
var ifValidators = new Array();
var _form = null;
var _childMode = false;
var _validTimeout = null;
var _validatingPage = false;

var moz = true;
if (typeof document.all == 'object')
{
	moz = false;
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true; }
	else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r; }
	else { elm['on' + evType] = fn; }
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function _reload()
{
	var obj = this;
	while (obj = obj.base)
	{
		if (typeof obj.reload != "undefined")
		{
			obj.reload();
			break;
		}
	}
}

function _parent_div(obj)
{
	var p = obj;
	while (p = p.parentNode)
	{
		if (p.tagName.toLowerCase() == "div" && p.style.display == "none")
		{
			return p.id;
		}
	}
	return null;
}

function _Other(obj)
{
	var r = obj.form.elements[obj.name];
	
	for (var i = 0; i < r.length; i++)
	{
		var m = /(\w+)\[(\d+)\]/.exec(r[i].id);
		if (m)
		{
			var o = document.getElementById(m[1] + "-other[" + m[2] + "]-span");
			if (o)
			{
				if (r[i].value && r[i].checked || (r[i].value == obj.value && obj.checked))
				{
					o.style.display = "inline";
				}
				else
				{
					o.style.display = "none";
				}
			}
		}
	}
	
	return true;
}

function _ClearOptions(p)
{
	var _o = p.options[0];
	
	var o;

	while (o = p.firstChild)
	{
		p.removeChild(o);
	}
	
	p.options.add(_o);
}

function _Country(obj)
{
	var _country = obj.options[obj.selectedIndex].text;
	var p = document.getElementById('province');
	
	if (p)
	{
		if (provState[_country])
		{
			var _value = p.options[p.selectedIndex].value;
			
			_ClearOptions(p);
			
			for (var i in provState[_country])
			{
				var opt = document.createElement("option");
				
				opt.text = provState[_country][i];
				opt.value = i;
				
				if (i == _value)
				{
					opt.selected = true;
				}
				
				p.options.add(opt);
			}

			if (document.getElementById("member-province"))
			{
				document.getElementById("member-province").style.display = "";
			}
		}
		else
		{
			_ClearOptions(p);
			
			if (document.getElementById("member-province"))
			{
				document.getElementById("member-province").style.display = "none";
			}
		}
	}
}

function _DOM_backwards(el)
{
	if (el.previousSibling && el.parentNode.tagName.toLowerCase() != "fieldset")
	{
		if (el.previousSibling.hasChildNodes())
		{
			el = el.previousSibling;
			while (true)
			{
				if (el.lastChild)
				{
					el = el.lastChild;
				}
				else
				{
					break;
				}
			}
			return el;
		}
		else
		{
			return el.previousSibling;
		}
	}
	else
	{
		if (moz)
		{
			return _DOM_backwards(el.parentNode);
		}
		else
		{
			return _DOM_backwards(el.parentElement);
		}
	}
}

function _test_DOM_backwards(el)
{
	while (el = _DOM_backwards(el))
	{
		if (el.nodeType == 1)
		{
			el.style.backgroundColor = "yellow";
			if (el.focus)
			{
				el.focus();
			}
			if (!confirm(el.tagName)) break;
			el.style.backgroundColor = "";
		}
		else if (el.nodeType == 3)
		{
			if (!confirm(el.textContent)) break;
		}
	}
}

function _field_has_value(_fld)
{
	var _fld2;
	
	if (typeof _fld.length == "undefined" || (typeof _fld.tagName != "undefined" && _fld.tagName.toLowerCase() == "select"))
	{
		_fld2 = _fld;
	}
	else
	{
		_fld2 = _fld[0];
	}
	
	if (_fld2.tagName.toLowerCase() == "input")
	{
		if (_fld2.type == "text" || _fld2.type == "password")
		{
			return _has_value(_fld.value);
		}
		else if (_fld2.type == "radio")
		{
			return _has_checked(_fld);
		}
		else if (_fld2.type == "checkbox")
		{
			if (typeof _fld.length == "undefined")
			{
				return _fld.checked;
			}
			else
			{
				return _has_checked(_fld);
			}
		}
	}	
	else if (_fld2.tagName.toLowerCase() == "select")
	{
		return _select_has_value(_fld);
	}
	else if (_fld2.tagName.toLowerCase() == "textarea")
	{
		return _has_value(_fld.value);
	}
}

function _has_value(_value)
{
	if (typeof _value == "undefined")
	{
		return null;
	}
	else if (_value.replace(/\s/g, "") == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function _has_checked(_grpcheck)
{
	for (var i = 0; i < _grpcheck.length; i++)
	{
		if (_grpcheck[i].checked == true)
		{
			return true;
		}
	}
	return false;
}

function _num_checked(_grpcheck)
{
	var n = 0;
	
	for (var i = 0; i < _grpcheck.length; i++)
	{
		if (_grpcheck[i].checked == true)
		{
			n++;
		}
	}
	
	return n;
}

function _select_has_value(_sel)
{
	if (_sel.multiple)
	{
		for (var i = 0; i < _sel.options.length; i++)
		{
			if (_sel.options[i].selected)
			{
				return true;
			}
		}
		
		return false;
	}
	else
	{
		if (_sel.options[_sel.selectedIndex].value)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function _is_value(obj, _value)
{
	if (typeof obj.tagName != "undefined" && obj.tagName.toLowerCase() == "select")
	{
		for (var i = 0; i < obj.options.length; i++)
		{
			if (typeof _value == "object")
			{
				for (var j = 0; j < _value.length; j++)
				{
					if (obj.options[i].value == _value[j] && obj.options[i].selected) return true;
				}
			}
			else
			{
				if (obj.options[i].value == _value && obj.options[i].selected) return true;
			}
		}
		return false;
	}
	if (typeof obj.length != "undefined")
	{
		for (var i = 0; i < obj.length; i++)
		{
			if (typeof _value == "object")
			{
				for (var j = 0; j < _value.length; j++)
				{
					if (obj[i].value == _value[j] && obj[i].checked) return true;
				}
			}
			else
			{
				if (obj[i].value == _value && obj[i].checked) return true;
			}
		}
		return false;
	}
	else if (typeof obj.value != "undefined")
	{
		if (typeof _value == "object")
		{
			for (var j = 0; j < _value.length; j++)
			{
				if (obj.value == _value[j]) return true;
			}
		}
		else
		{
			return (obj.value == _value);
		}
	}
	else
	{
		alert("What?");
		return null;
	}
}

function _get_id()
{
	var _href = location.href.split('?');
	if (_href.length == 2)
	{
		var _qs = _href[1].split('&');
		for (var i = 0; i < _qs.length; i++)
		{
			var _parts = _qs[i].split('=');
			if (_parts[0] == 'id')
			{
				return _parts[1];
			}
		}
	}

	if (menu.editDBID) return menu.editDBID;

	return null;
}

function setObjectContent(obj, content)
{
	if (moz)
	{
		obj.textContent = content;
	}
	else
	{
		obj.innerText = content;
	}
}

function ValidateForm(frm)
{
	_validatingPage = true;
	
	_form = frm.id;
	var valid = true;
	var firstValidator;
	for (i in validators)
	{
		if (typeof validators[i].Validate != "undefined")
		{
			validators[i].Validate();
			_v = validators[i];
			while (typeof _v.valid == 'undefined')
			{
				_v = _v.base;
			}
			if (_v.valid == false)
			{
				if (typeof firstValidator == "undefined" && (typeof validators[i].field.focus == "function" || typeof validators[i].field.focus == "object" || (typeof validators[i].field[0] != "undefined" && (typeof validators[i].field[0].focus == "function" || typeof validators[i].field[0].focus == "object"))))
				{
					firstValidator = validators[i];
				}

				valid = false;
			}
		}
	}
	for (i in validators)
	{
		if (typeof validators[i].Validate != "undefined")
		{
			_v = validators[i];
			while (typeof _v.valid == 'undefined')
			{
				_v = _v.base;
			}
			if (_v.valid == null)
			{
				if (_validTimeout) window.clearTimeout(_validTimeout);
				_validTimeout = window.setTimeout("Validate_SubmitTimer()", 250);
				return false;
			}
		}
	}
	if (!valid)
	{
		var _name;

		if (typeof firstValidator.field.length == "undefined")
		{
			_name = firstValidator.field.name;
		}
		else
		{
			_name = firstValidator.field[0].name;
		}

		if (typeof firstValidator.field.focus == "function" || typeof firstValidator.field.focus == "object")
		{
			firstValidator.field.focus();
		}
		else
		{
			if (typeof validators[i].field[0] != "undefined" && typeof firstValidator.field[0].focus == "function")
			{
				firstValidator.field[0].focus();
			}

			var _validator = document.getElementById(_name + "-validator");
			
			var parentDiv = _parent_div(_validator);
			if (parentDiv && document.getElementById(parentDiv) && document.getElementById(parentDiv).style.display == "none")
			{
				Tabs._showPanel("click", document.getElementById("tabs"), parentDiv);
			}

			if (!_validatingPage)
			{
				var vldPos = findPos(_validator);
				window.scrollTo(0, vldPos[1]);
			}
		}
	}
	
	_validatingPage = false;

	return valid;
}

function Validate_SubmitTimer()
{
	_validatingPage = true;
	
	var valid = true;
	for (i in validators)
	{
		if (typeof validators[i].Validate != "undefined")
		{
			_v = validators[i];
			while (typeof _v.valid == 'undefined')
			{
				_v = _v.base;
			}
			if (_v.valid == null)
			{
				if (_validTimeout) window.clearTimeout(_validTimeout);
				_validTimeout = window.setTimeout("Validate_SubmitTimer()", 250);
				return false;
			}
			
			if (_v.valid == false)
			{
				valid = false;
			}
		}
	}
	
	_validatingPage = false;

	if (valid == true)
	{
		SimulateSubmit();
	}
	_validTimeout = null;
}

function SimulateSubmit()
{
	if (moz)
	{
		var evt = document.createEvent("MouseEvent");
		evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		document.forms['content-form'].submit.dispatchEvent(evt);
	}
	else
	{
		document.forms['content-form'].submit.click();
	}
}

function Validator_OnChange(event)
{
	if (event.srcElement)
	{
		_field = event.srcElement;
	}
	else
	{
		_field = this;
	}
	
	var fieldname = _field.name;
	
	validators[fieldname].Validate();
	if (typeof ifValidators[fieldname] != "undefined")
	{
		_childMode = true;
		for (var i = 0; i < ifValidators[fieldname].length; i++)
		{
			validators[ifValidators[fieldname][i]].Validate();
		}
		_childMode = false;
	}
}

/*
function Validator_OnChange(fieldname)
{
	validators[fieldname].Validate();
	if (typeof ifValidators[fieldname] != "undefined")
	{
		_childMode = true;
		for (var i = 0; i < ifValidators[fieldname].length; i++)
		{
			validators[ifValidators[fieldname][i]].Validate();
		}
		_childMode = false;
	}
}
*/

function BaseValidator(field, fieldName, required, inline, ifField, ifValue)
{
	/*
	if (typeof validators[field.name] != 'undefined')
	{
		alert(fieldName + ": you cannot assign two validators to this field.");
	}
	*/

	this.field = field;
	//this.fieldName = fieldName;
	this.fieldName = "This";
	this.required = required;
	this.inline = inline;
	this.valid = null;
	
	if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
	{
		_name = this.field.name;
	}
	else
	{
		_name = this.field[0].name;
	}

	if (typeof ifField != "undefined")
	{
		if (typeof ifField.length == "undefined" || (typeof ifField.tagName != "undefined" && ifField.tagName.toLowerCase() == "select"))
		{
			_iName = ifField.name;
		}
		else
		{
			_iName = ifField[0].name;
		}
		
		this.ifField = ifField;
		if (typeof ifValidators[_iName] == "undefined")
		{
			ifValidators[_iName] = new Array();
		}
		ifValidators[_iName][ifValidators[_iName].length] = _name;
	}
	else
	{
		this.ifField = null;
	}
	if (typeof ifValue != "undefined")
	{
		this.ifValue = ifValue;
	}
	else
	{
		this.ifValue = null;
	}
	
	validators[_name] = this;
	
	this.draw();
}

BaseValidator.prototype.draw = function() {
	if (this.inline == true)
	{
		var _name;

		if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
		{
			_name = this.field.name;
		}
		else
		{
			_name = this.field[0].name;
		}

		var span = document.createElement("span");
		span.id = _name + "-validator";
		span.className = "validator-error";
		if (this.required == false)
		{
			span.innerHTML = "&nbsp;";
		}
		else if (this.ifField == null && this.ifValue == null)
		{
			span.innerHTML = "&nbsp;*";
		}
		else if ((this.ifValue == null && _has_value(this.ifField)) || _is_value(this.ifField, this.ifValue))
		{
			span.innerHTML = "&nbsp;*";
		}

		if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
		{
			if (moz)
			{
				this.field.parentNode.insertBefore(span, this.field.nextSibling);
			}
			else
			{
				this.field.parentElement.insertBefore(span, this.field.nextSibling);
			}
		}
		else
		{
			var tmp = this.field[0];
			var nodeContent;
			
			while (true)
			{
				tmp = _DOM_backwards(tmp);
				if (tmp.nodeType == 3 && tmp.parentNode.tagName.toLowerCase() != "noscript")
				{
					nodeContent = moz ? tmp.textContent : tmp.nodeValue;
					if (nodeContent.replace(/\s+/g, "").length > 0)
					{
						break;
					}
				}
			}

			if (moz)
			{
				if (tmp.nextSibling)
				{
					tmp.parentNode.insertBefore(span, tmp.nextSibling);
				}
				else
				{
					tmp.parentNode.appendChild(span);
				}
				
			}
			else
			{
				if (tmp.nextSibling)
				{
					tmp.parentNode.insertBefore(span, tmp.nextSibling);
				}
				else
				{
					tmp.parentNode.appendChild(span);
				}
			}
		}

/*
		if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
		{
			var f;
			eval("f = function() { Validator_OnChange('" + this.field.name + "'); }");
			addEvent(this.field, 'change', f, false);
		}
		else
		{
			for (var i = 0; i < this.field.length; i++)
			{
				var f;
				eval("f = function() { Validator_OnChange('" + this.field[i].name + "'); }");
				addEvent(this.field[i], 'change', f, false);
			}
		}
*/
		if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
		{
			addEvent(this.field, 'change', Validator_OnChange, false);
		}
		else
		{
			for (var i = 0; i < this.field.length; i++)
			{
				if (this.field[i].type == "checkbox" || this.field[i].type == "radio")
				{
					addEvent(this.field[i], 'click', Validator_OnChange, false);
				}
				else
				{
					addEvent(this.field[i], 'change', Validator_OnChange, false);
				}
			}
		}
	}
}

BaseValidator.prototype.reload = function() {
	this.field = document.getElementById(this.field.id);
	this.draw();
}

BaseValidator.prototype.Validate = function() {
	if (this.CheckRequired())
	{
		this.setValid();
	}
	else
	{
		this.setInvalid(this.fieldName + " is a required field.");
	}	
}

BaseValidator.prototype.CheckRequired = function() {
	if (this.required == false || _field_has_value(this.field))
	{
		return true;
	}
	else
	{
		if (this.ifField != null)
		{
			if (this.ifValue != null)
			{
				if (_is_value(this.ifField, this.ifValue))
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			else
			{
				if (_has_value(this.ifField))
				{
					return false;
				}
				else
				{
					return true;
				}
			}
		}
		else
		{
			return false;
		}
	}	
}

BaseValidator.prototype.setValid = function() {
	var _name;

	if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
	{
		_name = this.field.name;
	}
	else
	{
		_name = this.field[0].name;
	}

	validators[_name].valid = true;

	var div = document.getElementById(_name + '-validator');
	if (div)
	{
		if (this.required == false)
		{
			div.innerHTML = "&nbsp;";
		}
		else if (this.ifField == null && this.ifValue == null)
		{
			div.innerHTML = "&nbsp;*";
		}
		else if ((this.ifValue == null && _has_value(this.ifField)) || _is_value(this.ifField, this.ifValue))
		{
			div.innerHTML = "&nbsp;*";
		}
		else
		{
			div.innerHTML = "";
		}
	}
}

BaseValidator.prototype.setInvalid = function(invalidString) {
	var _name;

	if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
	{
		_field = this.field;
		_name = this.field.name;
	}
	else
	{
		_field = this.field[0];
		_name = this.field[0].name;
	}

	validators[_name].valid = false;
	
	if (!_childMode)
	{
		var div = document.getElementById(_name + '-validator');
		if (div)
		{
			if (this.required == false)
			{
				div.innerHTML = "&nbsp;<br /><small>" + invalidString + "</small>";
			}
			else
			{
				div.innerHTML = "&nbsp;*<br /><small>" + invalidString + "</small>";
			}
		}
		
		if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
		{
			_name = this.field.name;
		}
		else
		{
			_name = this.field[0].name;
		}

		var _validator = document.getElementById(_name + "-validator");
		var vldPos = findPos(_validator);
		
		if (vldPos[1] < window.pageYOffset || vldPos[1] > (window.pageYOffset + window.innerHeight))
		{
			window.scrollTo(0, vldPos[1]);
		}

		//if (typeof _field.focus == "function") _field.focus();
	}
	else
	{
		this.setValid();
	}
}

BaseValidator.prototype.setValidAlert = function(invalidString) {
	var _name;

	if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
	{
		_name = this.field.name;
	}
	else
	{
		_name = this.field[0].name;
	}

	validators[_name].valid = true;
	
	var div = document.getElementById(_name + '-validator');
	if (div) div.innerHTML = invalidString;
}

function CompareValidator(field, fieldName, required, inline, field2, msg, ifField, ifValue)
{
	this.base = new BaseValidator(field, fieldName, required, inline, ifField, ifValue);
	this.field = this.base.field;
	this.field2 = field2;
	this.msg = msg;
	validators[field.name] = this;
}

CompareValidator.prototype.Validate = function()
{
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else if (this.base.field.value != this.field2.value)
	{
		this.base.setInvalid(this.msg);
	}
	else
	{
		this.base.setValid();
	}
}

function LengthValidator(field, fieldName, required, inline, maxLength)
{
	this.base = new BaseValidator(field, fieldName, required, inline);
	this.field = this.base.field;
	this.maxLength = maxLength;
	validators[field.name] = this;
}

LengthValidator.prototype.Validate = function() {
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else if (this.base.required == false && !_has_value(this.base.field.value))
	{
		this.base.setValid();
	}
	else if (this.base.field.value.length <= this.maxLength)
	{
		this.base.setValid();
	}
	else
	{
		this.base.setInvalid(this.base.fieldName + " must be no longer than " + this.maxLength + " characters.");
	}
}

LengthValidator.prototype.reload = _reload;

function CheckboxGroupValidator(field, fieldName, required, inline, max, ifField, ifValue)
{
	this.base = new BaseValidator(field, fieldName, required, inline, ifField, ifValue);
	this.field = this.base.field;

	if (typeof max != "undefined")
	{
		this.max = max;
	}
	else
	{
		this.max = null;
	}
	
	var _name;

	if (typeof this.field.length == "undefined" || (typeof this.field.tagName != "undefined" && this.field.tagName.toLowerCase() == "select"))
	{
		_name = this.field.name;
	}
	else
	{
		_name = this.field[0].name;
	}

	validators[_name] = this;
}

CheckboxGroupValidator.prototype.reload = _reload;

CheckboxGroupValidator.prototype.Validate = function()
{
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else if (this.max && _num_checked(this.field) > this.max)
	{
		this.base.setInvalid(this.base.fieldName + " must have no more than " + this.max + " options selected.");
	}
	else
	{
		this.base.setValid();
	}
}

function PasswordValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new BaseValidator(field, fieldName, required, inline, ifField, ifValue);
	this.field = this.base.field;
	validators[field.name] = this;
}

PasswordValidator.prototype.reload = _reload;

PasswordValidator.prototype.Validate = function()
{
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else if (this.base.field.value.length < 5)
	{
		this.base.setInvalid(this.base.fieldName + " must be at least 5 characters long.");
	}
	else if (this.base.field.value.length > 10)
	{
		this.base.setInvalid(this.base.fieldName + " must be no more than 10 characters long.");
	}
	else if (!this.base.field.value.match(/(\d+[a-z]+)|([a-z]+\d+)/i))
	{
		this.base.setInvalid(this.base.fieldName + " must contain both letters and numbers.");
	}
	else
	{
		this.base.setValid();
	}
}

function RegexValidator(field, fieldName, required, inline, rex, formatExample, ifField, ifValue)
{
	this.base = new BaseValidator(field, fieldName, required, inline, ifField, ifValue);
	this.field = this.base.field;
	this.regex = rex;
	this.formatExample = formatExample;
	this.valid = null;
	validators[field.name] = this;
}

RegexValidator.prototype.Validate = function() {
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else if (this.base.required == false && !_has_value(this.base.field.value))
	{
		this.base.setValid();
	}
	else if (this.regex.test(this.base.field.value))
	{
		this.base.setValid();
	}
	else
	{
		this.base.setInvalid(this.base.fieldName + " must have the format: " + this.formatExample);
	}
}

function NumberValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, /^[0-9]*$/i, "1234567890", ifField, ifValue);
	this.field = this.base.base.field;
	validators[field.name] = this;
}

NumberValidator.prototype.reload = _reload;

NumberValidator.prototype.Validate = function() {
	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else if (this.base.regex.test(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else
	{
		this.base.base.setInvalid(this.base.base.fieldName + " must be a number.");
	}
}

function OrgNumberValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, /^[0-9]{9,10}$/i, "1234567890", ifField, ifValue);
	this.field = this.base.base.field;
	validators[field.name] = this;
}

OrgNumberValidator.prototype.reload = _reload;

OrgNumberValidator.prototype.Validate = function() {
	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else if (this.base.regex.test(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else
	{
		this.base.base.setInvalid(this.base.base.fieldName + " must be a 9 or 10 digit number.");
	}
}

function PostalValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, /^\s*([a-ceghj-npr-tvxy]\d[a-z])\s?(\d[a-z]\d)\s*|(\d{5}(-\d{4})?)$/i, "T1A 1A1", ifField, ifValue);
	this.field = this.base.base.field;
	validators[field.name] = this;
}

PostalValidator.prototype.reload = _reload;

PostalValidator.prototype.Validate = function() {
	var x = document.getElementById("country");
	
	if (x && x.options[x.selectedIndex].value == "US")
	{
		var _rex = /^(\d{5}(-\d{4})?)$/;
		var _fmt = "12345";
	}
	else if (x && x.options[x.selectedIndex].value == "CA")
	{
		var _rex = /^\s*([a-ceghj-npr-tvxy]\d[a-z])\s?(\d[a-z]\d)\s*$/i;
		var _fmt = "T1A 1A1";
	}
	else
	{
		var _rex = /.*/;
		var _fmt = this.base.formatExample;
	}

	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else if (_rex.test(this.base.base.field.value))
	{
		if (x.options[x.selectedIndex].value == "CA")
		{
			var m = _rex.exec(this.base.base.field.value);
			if (typeof m[1] != "undefined" && typeof m[2] != "undefined")
			{
				this.base.base.field.value = m[1].toUpperCase() + " " + m[2].toUpperCase();
			}
		}

		this.base.base.setValid();
	}
	else
	{
		this.base.base.setInvalid(this.base.base.fieldName + " must have the format: " + _fmt);
	}
}

function PhoneValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, new RegExp("^\\(?(\\d{3})\\)?[-\\s.]?(\\d{3})[-.]?(\\d{4})[ a-zA-Z.]*(\\d+)?$"), "(123) 123-1234", ifField, ifValue);
	this.field = this.base.base.field;
	validators[field.name] = this;
}

PhoneValidator.prototype.reload = _reload;

PhoneValidator.prototype.Validate = function() {
	var x = document.getElementById("country");
	if (x && x.options[x.selectedIndex].value == "US" || x.options[x.selectedIndex].value == "CA" || x.options[x.selectedIndex].value == "")
	{
		this.international = false;
		var _rex = new RegExp("^\\(?(\\d{3})\\)?[-\\s.]?(\\d{3})[-.]?(\\d{4})[ a-zA-Z.]*(\\d+)?$");
		var _fmt = "(123) 123-1234";
	}
	else
	{
		this.international = true;
		var _rex = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)[. ]+(\d{1,5})){0,1}$/;
		var _fmt = "(123) 123-1234, +61 (0) 2 1234 5678";
	}

	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else if (_rex.test(this.base.base.field.value))
	{
		var m = _rex.exec(this.base.base.field.value);
		if (!this.international)
		{
			this.base.base.field.value = "(" + m[1] + ") " + m[2] + "-" + m[3];
			if (m[4])
			{
				this.base.base.field.value += " ext. " + m[4];
			}
		}
		this.base.base.setValid();
		
	}
	else
	{
		this.base.base.setInvalid(this.base.base.fieldName + " must have the format: " + _fmt);
	}
}

function EmailValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, new RegExp("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"), "user@host.com", ifField, ifValue);
	this.field = this.base.base.field;
	validators[field.name] = this;
}

EmailValidator.prototype.reload = _reload;

EmailValidator.prototype.Validate = function() {
	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else if (this.base.regex.test(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else
	{
		this.base.base.setInvalid(this.base.base.fieldName + " must have the format: user@host.com");
	}
}


function DateValidator(field, fieldName, required, inline, ifField, ifValue)
{
	this.base = new RegexValidator(field, fieldName, required, inline, new RegExp("^(\\d{2})-(\\d{2})-(\\d{4})$"), "mm-dd-yyyy", ifField, ifValue);
	this.field = this.base.base.field;
	this.valid = null;
	validators[field.name] = this;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

DateValidator.prototype.reload = _reload;

DateValidator.prototype.Validate = function() {
	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else if (this.base.base.required == false && !_has_value(this.base.base.field.value))
	{
		this.base.base.setValid();
	}
	else
	{
		var arr = this.base.regex.exec(this.base.base.field.value);
		if (arr == null)
		{
			this.base.base.setInvalid(this.base.base.fieldName + " must have the format: " + this.base.formatExample);
		}
		else
		{
			var daysInMonth = DaysArray(12);
			if (RegExp.$1 > 12)
			{
				this.base.base.setInvalid(this.base.base.fieldName + " must specify a valid month.");
			}
			else if ((RegExp.$2 > daysInMonth[parseInt(RegExp.$1)]) || (parseInt(RegExp.$1) == '2' && RegExp.$2 > daysInFebruary(parseInt(RegExp.$3))))
			{
				this.base.base.setInvalid(this.base.base.fieldName + " must specify a valid day of the month.");
			}
			else
			{
				this.base.base.setValid();
			}
		}
	}
}

function AjaxValidator(field, fieldName, required, inline, url, keys, onComplete, ifField, ifValue)
{
	this.base = new BaseValidator(field, fieldName, required, inline, ifField, ifValue);
	this.field = this.base.field;
	this.url = url;
	this.keys = keys;
	this.onComplete = onComplete;
	validators[field.name] = this;
}

AjaxValidator.prototype.reload = _reload;

AjaxValidator.prototype.Validate = function() {
	if (!this.base.CheckRequired())
	{
		this.base.setInvalid(this.base.fieldName + " is a required field.");
	}
	else
	{
		this.base.valid = null;
		
		if (_validTimeout == null)
		{
			var _parameters = this.base.field.name + '=' + this.base.field.value;
			
			if (_get_id())
			{
				_parameters += "&id=" + _get_id();
			}
			
			for (var i = 0; i < this.keys.length; i++)
			{
				_parameters += "&" + this.keys[i] + "=" + this.base.field.form.elements[this.keys[i]].value;
			}
			
			var myAjax = new Ajax.Request(
				this.url,
				{
					method: 'get',
					parameters: _parameters,
					onComplete: this.onComplete
				}
			);
		}
	}
}

function ShortNameValidator(field, fieldName, required, inline)
{
	eval("var _onComplete = function(xmlRequest) { ShortName_Validate(xmlRequest, '" + field.name + "'); }");
	this.base = new AjaxValidator(field, fieldName, required, inline, '/admin/ajax/check-shortname.php', new Array("parent_page"), _onComplete);
	if (field.form.elements['parent_page']) field.form.elements['parent_page'].onchange = function() { PageChange(); if (document.getElementById("name").value) { Validator_OnChange("name"); } };
	PageChange();
	validators[field.name] = this;
}

ShortNameValidator.prototype.Validate = function() {
	var tmp = this.base.base.field.value.replace(/[^a-z0-9\-_]+/gi, "-");

	if (tmp.substr(0, 1) == "-")
	{
		tmp = tmp.substr(1);
	}
	if (tmp.substr(tmp.length - 1, 1) == "-")
	{
		tmp = tmp.substr(0, tmp.length - 1);
	}
	
	this.base.base.field.value = tmp;
	
	if (!this.base.base.CheckRequired())
	{
		this.base.base.setInvalid(this.base.base.fieldName + " is a required field.");
	}
	else
	{
		this.base.base.valid = null;
		
		if (_validTimeout == null)
		{
			var _parameters = this.base.base.field.name + '=' + this.base.base.field.value;
			
			if (_get_id())
			{
				_parameters += "&id=" + _get_id();
			}
			
			for (var i = 0; i < this.base.keys.length; i++)
			{
				_parameters += "&" + this.base.keys[i] + "=" + this.base.base.field.form.elements[this.base.keys[i]].value;
			}
			
			var myAjax = new Ajax.Request(
				this.base.url,
				{
					method: 'get',
					parameters: _parameters,
					onComplete: this.base.onComplete
				}
			);
		}
	}
}


function ShortName_Validate(xmlRequest, field)
{
	var rsp = xmlRequest.responseText;
	if (rsp == "true")
	{
		validators[field].base.base.setValid();
	}
	else
	{
		validators[field].base.base.setInvalid("This page name is already in use - please choose another.");
	}
}

function PageChange()
{
	var _parentPage = document.getElementById("parent_page");
	if (_parentPage.tagName.toLowerCase() == "select")
	{
		var _tmp = _parentPage.options[_parentPage.selectedIndex].text;
		var _url = /.+\[(.+)\]/.exec(_tmp)[1];
		
		document.getElementById("url-prefix").innerHTML = _url + "/";
	}
}