var
	FORM_DEFAULT_ERROR =
		"There was a problem processing the submitted form. "+
		"Please review the following items and try again:"
;
function getFormControls(form, name)
{
	var i, a = form.getElementsByTagName("*"), o = Array();
	for (i=0; a[i]; i++){
		if (name && (a[i].name != name))
			continue;

		switch (getTag(a[i])){
			case "input":
				{
					switch (a[i].type){
						case "text":
						case "password":
						case "checkbox":
						case "radio":
						case "file":
							{
								o.push(a[i]);
							}
						break;
					}
				}
			break;

			case "select":
			case "textarea":
				{
					o.push(a[i]);
				}
			break;
		}
	}
	return o;
}
function formControlValidates(control)
{
	if (control.getAttribute("pattern") && control.value != ""){
		var value = String(control.value), r;
		if (control.getAttribute("ignore")){
			r = RegExp(control.getAttribute("ignore"), "g");
			value = value.replace(r, "");
		}
		r = RegExp("^" + control.getAttribute("pattern") + "$", "");
		return r.test(value);
	}
	if (
			control.getAttribute("required") == "required"
			||
			control.getAttribute("required") == "required-if-selected"
	){
		if (control.getAttribute("required") == "required-if-selected"){
			var top = getAncestor(control, ".option");
			while (top && hasClass(top.parentNode, "option")){
				top = top.parentNode;
			}
			if (top && !hasClass(top, "option-selected")){
				return true;
			}
		}
		switch (getTag(control)){
			case "textarea":
			case "input":
				{
					switch (control.type){
						case "radio":
							{
								var ret = false;
								forEachItem(
									getFormControls(control.form, control.name),
									function(radio){
										if (radio.checked){
											ret = true;
											return -1;
										}
									}
								);
								return ret;
							}
						break;
						case "checkbox":
							return control.checked;
						break;
					}
				}
			break;

			case "select":
				{
					return (control.selectedIndex > 0);
				}
			break;
		}
		return (control.value != "");
	}
	return true;
}
function getFormErrorPrefix(form)
{
	return String(form.getAttribute("error") || FORM_DEFAULT_ERROR).replace(/\\n/g, "\n") + "\n\n";
}
function getFormControlError(control)
{
	return control.getAttribute("error") || control.getAttribute("title") || "";
}
function validateForm(form)
{
	var e, invalid = false, errors = Array();

	/** Check for event fire */
	if (form.type == "submit"){
		e = form;
		form = getEventTarget(form);
	}

	forEachItem(
		getFormControls(form),
		function(control){
			if (!formControlValidates(control)){
				var s = getFormControlError(control);
				if (s != ""){
					errors.push(s);
				}
				invalid = true;
			}
		}
	);

	if (e && invalid){
		cancelEvent(e);

		if (!errors.length){
			errors.push("Please ensure that all required fields have been filled in.");
		}
		alert(getFormErrorPrefix(form) + "- " + errors.join("\n- "));
	}
	return (!invalid);
}

function enhanceForms(root)
{
	if (!(root && root.tagName)){
		root = document;
	}
	forEachItem(
		getElements(root, "form"),
		function(form){
			hookEvent(form, "submit", validateForm);
		}
	);
}

function _mobileSuccessChange(e)
{
	var s = getElement("#mobile-success"), f = getAncestor(s, "fieldset");
	if (
			(s.value == "null")
			||
			(s.value == 1 && !document.getElementById("mobile-success-both"))
	){
		setClass(f, "simple");
		unsetClass(f, "halfsimple");
	}
	else if (s.value == 1){
		setClass(f, "halfsimple");
		unsetClass(f, "simple");
	}
	else{
		unsetClass(f, "halfsimple");
		unsetClass(f, "simple");
	}

	if (browser.isGecko){
		setStyle(f, "display", "none");
		setStyle(f, "display", "block");
	}
}
function enhanceMobileForms()
{
	var s;
	if (s = getElement("#mobile-success")){
		unsetClass(getAncestor(s, "fieldset"), "unscripted");
		hookEvent(s, "change", _mobileSuccessChange);
		_mobileSuccessChange();
	}
}

if (window.deviantART){
	hookEvent(deviantART, "applicationready", enhanceForms);
}
hookEvent(deviantART, "applicationready", enhanceMobileForms);