/*
NAME: PitTip Training Registration Form Validation
DATE: 02/05/2007
AUTHOR: Kenneth Pitt (guru@pittip.net)
WEBSITE: www.pittip.net
DESCRIPTION: Validates form elements dependent on their "title" element values.
HOW IT WORKS: It steps through the input elements when the submit button is pressed, checking for title element values. If the title value equals one of the validator values (below), it checks the appropriate field. If there is an error, it creates a "div" element with the class of "error". It then reads the field's label text to add to the text of the error div. 
VALUES: (title = )
"req" - marks a field as required; anything is allowed, as long as it isn't blank
"email" - validates the field for proper email formatting; foo@bar.com
"card" - validated the field as a credit card number; no less than 12-digits, numbers only
"expiration" - validates the field as a credit card expiration date; mm/yy format
*/

var err_count = 0;		// Set global error count

/* -- DOM whitespace checker, for W3C standards -- */
var wspace = {
	test : function(elem){
		var wspace = /[^\t\n\r ]/
		if(!wspace.test(elem.data)){ return elem.nextSibling; }else{ return elem; }
	}
}

/* -- VALIDATION CHECKERS -- */
var check_req = /[a-z\dA-Z\d]/, check_email = /^.+@.+\..{2,3}$/, check_card = /[0-9]/, check_expire = /[0-9]+\/+[0-9]/;

var valid = {
	req : function(elem){				// Check for empty field
		if( (!check_req.test(elem.value)) || (elem.value == '') ){
			var first = wspace.test(elem.parentNode.firstChild);
			var error = first.innerHTML + " is required!";	return error;
		}
	},
	email : function(elem){				// Check for valid email formatting
		if(!check_email.test(elem.value)){
			var error = "This must be a valid email address!";	return error;
		}
	},
	card : function(elem){				// Check for valid credit card number formatting
		if( (parseInt(elem.value) != elem.value) || (elem.value.length < 12) ){
			var error = "This must be a valid credit card number! Numbers only.";	return error;
		}
	},
	expire : function(elem){			// Check for valid expiration date formatting
		if(!check_expire.test(elem.value)){
			var error = "Please format like mm/yy";	return error;
		}else{
			var parts = elem.value.split("/");
			if( parts.length != 2 || parts[0].length != 2 || parts[1].length != 2){
				var error = "Please format like mm/yy";	return error;
			}
		}
	}
}

/* -- TOGGLE ERROR MESSAGES -- */
var display_error = {
	show : function(elem,error){
		err = document.createElement('div');		// Create error layer
		err.className = "error";					// Assign error layer classname
		//Add error layer to the document
		elem.parentNode.lastChild.className != 'error' ? elem.parentNode.appendChild(err) : null;
		err.innerHTML = error;						// Fill in error layer text
		elem.className = "focus";					// Change field class
		err_count++;								// Add error count
		return err_count;
	},
	hide : function(elem,error){
		//Remove error layer from the document
		elem.parentNode.lastChild.className == 'error' ? elem.parentNode.removeChild(elem.parentNode.lastChild) : null;
		elem.className = "";						// Remove field class
		err_count > 0 ? err_count = 1 : null;		// There are errors
		return err_count;
	}
}

/* -- PERFORM VALIDATION CHECKS -- */
var check = {
	verify : function(elem){
		if( (elem.getAttribute('title')) && (elem.parentNode.style.display != "none") ){
			var error, required = elem.getAttribute('title');
			switch(required){
				case "req":		error = valid.req(elem);	break;		// Check if empty
				case "email":	error = valid.email(elem);	break;		// Check if email formatting
				case "card":	error = valid.card(elem);	break;		// Check numbers only and length
				case "expire":	error = valid.expire(elem);	break;		// Check expiration formatting
			}
			// Toggle error display
			error == null ? err_count = display_error.hide(elem,error) : err_count = display_error.show(elem,error);
		}
		return err_count;
	}
}

/* -- CHANGE SUBMIT BUTTON -- */
var submit_button = {
	on : function(elem,submit_value){
		elem.disabled = false;	elem.value = submit_value;
		return false;
	},
	off :  function(elem){
		elem.disabled = true;	elem.value = "Please Wait...";
	}
}

var error_output = {
	show : function() {
		var to_output = document.getElementById('error_output');
		if( to_output ) {
			to_output.innerHTML = "There seems to be some errors, please scroll up and correct.";
			to_output.style.display = "block";
			to_output.style.color = "#dd0000";
		}
	},
	hide : function() {
		var to_output = document.getElementById('error_output');
		if( to_output ) {
			to_output.style.display = "none";
			to_output.innerHTML = "";
		}
	}
}

/* -- FIND REQUIRED ELEMENTS AND VALIDATE -- */
function submit_check(elem){
	var form = elem.form, inputs = document.getElementsByTagName('input'), selects = document.getElementsByTagName('select');
	
	var submit_value = elem.value;
	button = submit_button.off(elem);		// Turn off submit button

	for( var i = 0, input; input = inputs[i]; i++ ) {	err_count = check.verify(input);	}
	for( var i = 0, selec; selec = selects[i]; i++ ) {	err_count = check.verify(selec);	}
	
	if( err_count == 0 ) {	// Count errors, if 0, submit form
		error_output.hide();
		form.submit();
	} else {
		error_output.show();
		submit_button.on(elem,submit_value);
	}
	err_count = 0;				// Reset error count
}