//////////////////////////////////////////////////////////////////////////////////////////////////
// Valid Input Images
// Creator: Christian Snodgrass
// This group of functions allows you to easily validate forms with Javascript, and depending
//  on whether it is valid (meets the regex) it will display either a valid or invalid
//  image.
/////////////////////////////////////////////////////////////////////////////////////////////////
var BASE_URL = "http://www.arwebdesign.net/client_sites/resnet/";

function ValidInputImages() {
	this.validImage = document.createElement("img");
	this.validImage.src = BASE_URL + "images/valid.png";
	
	this.invalidImage = document.createElement("img");
	this.invalidImage.src = BASE_URL + "images/invalid.png";
}

ValidInputImages.prototype.elements = Array();
ValidInputImages.prototype.element_types = Array();
ValidInputImages.prototype.radio_ids = Array();
ValidInputImages.prototype.regex = Array();
ValidInputImages.prototype.forms = Array();
ValidInputImages.prototype.validImage = 0;
ValidInputImages.prototype.invalidImage = 0;

ValidInputImages.prototype.addInput = function(type, id, regex) {
	// For radio buttons, we use the name instead of id.	
	this.elements.push(id);
	this.element_types.push(type);
	this.regex.push(regex);
	
	if(type != "radio")
		document.getElementById(id).onchange = this.checkInput;
	else {
		var elements = document.getElementsByName(id);
		for(var i=0; i < elements.length; i++) {
			elements[i].onkeydown = this.checkInput;
			this.radio_ids.push(elements[i].id);
		}
	}
}

ValidInputImages.prototype.addImage = function(valid, id) {
	var img = document.getElementById(id + "img");

	if(img == null) {
		img = document.createElement("img");
		img.id = id + "img";
		var append = true;
	}

	if(valid) {
		img.src = BASE_URL + "images/valid.png";
		img.alt = "Valid";
	} else {
		img.src = BASE_URL + "images/invalid.png";
		img.alt = "Invalid";
	}
	
	if(append)
		document.getElementById(id).parentNode.appendChild(img);
		
	return valid;
}

ValidInputImages.prototype.addForm = function(id) {
	this.forms.push(id);
	
	document.getElementById(id).onsubmit = this.validateForm;
}

ValidInputImages.prototype.checkText = function(id, regex) {
	if(!regex)
		regex = /^.+$/;
		
	return this.check(id, regex);
}

ValidInputImages.prototype.checkRadio = function(name, regex) {
	if(!regex)
		regex = /^.+$/;
		
	var elements = document.getElementsByName(name);
	var string = "";
	var id = "";
	
	for(var i=0; i < elements.length; i++) {
		if(elements[i].checked)
			string += elements[i].id;

		id = elements[i].id;
	}
	
	if(regex == 'u' || string.match(regex))
		var valid = true;
	else
		var valid = false;
		
	return this.addImage(valid, id);
}

ValidInputImages.prototype.checkSelect = function(name, regex) {
	if(!regex)
		regex = /^.+$/;
		
	return this.check(id, regex);
}

ValidInputImages.prototype.checkCheckbox = function(id, regex) {
	if(!regex)
		regex = /^(true)$/;
	
	var element = document.getElementById(id);
	
	if(element.checked)
		var string = "true";
	else
		var string = "false";
	
	if(regex == 'u' || string.match(regex))
		var valid = true;
	else
		var valid = false;
	
	return this.addImage(valid, id);
}

ValidInputImages.prototype.checkSelect = function(id, regex) {
	if(!regex)
		regex = /^.+$/;
		
	return this.check(id, regex);
}

ValidInputImages.prototype.checkTextarea = function(id, regex) {
	if(!regex)
		regex = /^.+$/;
	
	return this.check(id, regex);
}

ValidInputImages.prototype.checkPassword = function(id, regex) {
	if(!regex)
		regex = /^.+$/;
		
	return this.check(id, regex);
}

ValidInputImages.prototype.check = function(id, regex) {
	var element = document.getElementById(id);

	var value = document.getElementById(id).value.replace(new RegExp( "\\n", "g" ),'');
	
	if(regex == 'u' || value.match(regex))
		var valid = true;
	else
		var valid = false;
	
	return this.addImage(valid, id);
}

////////////////////////////////////////////////////////////////
// The following functions are accessed outside of this class.
////////////////////////////////////////////////////////////////
ValidInputImages.prototype.checkInput = function() {
	for(var i=0; i < validInputImages.elements.length; i++)
		if(this.id == validInputImages.elements[i]) {
			switch(validInputImages.element_types[i]) {
				case 'text':
					validInputImages.checkText(this.id, validInputImages.regex[i]);
					break;
				case 'checkbox':
					validInputImages.checkCheckbox(this.id, validInputImages.regex[i]);
					break;
				case 'password':
					validInputImages.checkPassword(this.id, validInputImages.regex[i]);
					break;
				case 'select':
					validInputImages.checkSelect(this.id, validInputImages.regex[i]);
				case 'textarea':
					validInputImages.checkTextarea(this.id, validInputImages.regex[i]);
			}
			
			return;
		}
	
	for(var i=0; i < validInputImages.radio_ids.length; i++)
		if(this.id == validInputImages.radio_ids[i]) {
			validInputImages.checkRadio(this.name, validInputImages.regex[i]);
			return;
		}
}

ValidInputImages.prototype.validateForm = function(submitting) {
	if(submitting == undefined)
		submitting = true;
	var submit = true;
	for(var i=0; i < validInputImages.elements.length; i++)
		switch(validInputImages.element_types[i]) {
			case 'text':
				if(!validInputImages.checkText(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			case 'checkbox':
				if(!validInputImages.checkCheckbox(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			case 'password':
				if(!validInputImages.checkPassword(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			case 'radio':
				if(!validInputImages.checkRadio(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			case 'select':
				if(!validInputImages.checkSelect(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			case 'textarea':
				if(!validInputImages.checkTextarea(validInputImages.elements[i], validInputImages.regex[i]))
					submit = false;
				break;
			}
	
	if(!submitting)
		return false;
		
	if(!submit)
		alert("Please fill out those marked with an X and try again.");
	
	return submit;
}

var validInputImages = new ValidInputImages();