function addValidation() {
	for(var q=0; q < 3; q++) {
		if(q == 0) el = document.getElementsByTagName("input");
		if(q == 1) el = document.getElementsByTagName("textarea");
		if(q == 2) el = document.getElementsByTagName("select");
		
		for(var i = 0; i < el.length; i++) {
			if(el[i].type != "submit" && el[i].type != "button" && el[i].type != "hidden") {
				ar = el[i].name.split(":", 2);
				if(ar[0] != "optional" && ar.length==2) {
					el[i].onvalidateresponse = function(e) { if(e != null && e != "") { document.getElementById(this.id+'_validation').innerHTML = e; document.getElementById(this.id+'_validation').style.visibility = 'visible'; } else { document.getElementById(this.id+'_validation').innerHTML = ''; document.getElementById(this.id+'_validation').style.visibility = 'hidden'; } };
					el[i].validateAs = ar[0];
					if(q==2) {
						if(typeof el[i].onchange == 'function') {
							el[i].onchange = stackFunction(el[i].onchange,function(obj){RPC("validate", obj.validateAs, obj, "onvalidateresponse", Array(obj.value));});
						} else {
							el[i].onchange = function(){RPC("validate", this.validateAs, this, "onvalidateresponse", Array(this.value));};
						}
					} else if(el[i].type == "checkbox") {
						if(typeof el[i].onchange == 'function') {
							el[i].onclick = stackFunction(el[i].onchange,function(obj){RPC("validate", obj.validateAs, obj, "onvalidateresponse", Array(obj.checked));});
						} else {
							el[i].onclick = function(){RPC("validate", this.validateAs, this, "onvalidateresponse", Array(this.checked));};
						}
					} else {
						if(ar[0] == "password" || ar[0] == "opt_password") {
							//this section is for checking passwords match, password fields must be called password1 and password2 and there should be one validation message field which is called 'password_validation'
							el[i].onvalidatepasswordresponse = function(e) { if(document.getElementById('password1').hasHadFocus && document.getElementById('password2').hasHadFocus) { if(e != null && e != "") { document.getElementById('password_validation').innerHTML = e; document.getElementById('password_validation').style.visibility = 'visible'; } else { document.getElementById('password_validation').innerHTML = ''; document.getElementById('password_validation').style.visibility = 'hidden'; } } };
							el[i].onblur = stackFunction(el[i].onblur,function(obj){RPC("validate", obj.validateAs, obj, "onvalidatepasswordresponse", Array(document.getElementById('password1').value, document.getElementById('password2').value));});
						} else if(ar[0] == "unique_sku" || ar[0] == "unique_country_iso" || ar[0] == "unique_under_weight" || ar[0] == "unique_tracking_ref") {
							current_unique_key = document.getElementById('current_unique_key').value;
							if(typeof el[i].onblur == 'function') {
								el[i].onblur = stackFunction(el[i].onblur,function(obj){RPC("validate", obj.validateAs, obj, "onvalidateresponse", Array(obj.value, current_unique_key));});
							} else {
								el[i].onblur = function(){RPC("validate", this.validateAs, this, "onvalidateresponse", Array(this.value, current_unique_key));};
							}
						} else {
							if(typeof el[i].onblur == 'function') {
								el[i].onblur = stackFunction(el[i].onblur,function(obj){RPC("validate", obj.validateAs, obj, "onvalidateresponse", Array(obj.value));});
							} else {
								el[i].onblur = function(){RPC("validate", this.validateAs, this, "onvalidateresponse", Array(this.value));};
							}
						}
					}
				}
			}
		}
	}
}


function associateActions(actions) {
  for(var element in actions) {
	var e;
	e = document.getElementById(element);
	if(e != null) {
	  events = actions[element];
	  for(var event in events) {
		e["on" + events[event][0]] = events[event][1];
	  }
	}
  }
}

function do_focuses() {
	if(document.getElementById('password1')) {
		document.getElementById('password1').hasHadFocus=false;
		document.getElementById('password1').onblur = stackFunction(document.getElementById('password1').onblur,function() { document.getElementById('password1').hasHadFocus=true; });
	}
	if(document.getElementById('password2')) {
		document.getElementById('password2').hasHadFocus=false;
		document.getElementById('password2').onblur = stackFunction(document.getElementById('password2').onblur,function() { document.getElementById('password2').hasHadFocus=true; });
	}
}

function check_for_error_messages() {
	n = document.getElementsByTagName('SPAN');
	for(i=0; i<n.length; i++) {
		if(n[i].id.match("_validation$") && n[i].innerHTML!="") {
			return true;
		}
	}
	return false;
}

function show_main_error_message() {
	if(document.getElementById('main_error_message')) {
		if(check_for_error_messages()) {
			document.getElementById('main_error_message').style.display='block';
		}
	}
}

addLoadEvent(addValidation);
addLoadEvent(do_focuses);
addLoadEvent(show_main_error_message);
