/*
** Boulder Eyes additional JavaScript functionality by Chase Higgins
** chasehx@gmail.com, http://twitter.com/tzDev
*/

function verifyForm(form_name, data_types) {
	// form verification is derived from the Simple Form Validation Framework By Chase Higgins
	parent_form = document.getElementsByName(form_name)[0];
	// now we have our parent form, let us get a count of the input fields
	inputs = parent_form.getElementsByTagName('input');
	text_fields = new Array();
	// iterate them and only save the text type inputs
	ctrl = 0;
	for (i = 0; i < inputs.length; i++) {
		type = inputs[i].getAttribute('type');
		classval = inputs[i].getAttribute('class');
		if (type == 'text' && classval != 'noreq') {
			// this is a text field
			text_fields[ctrl] = inputs[i];
			ctrl++;
		}
	}
	// we should have a good list of fields now, make sure they match the types count
	if (text_fields.length != data_types.length) {
		// this would typically mean the data types array passed here was misaligned
		alert('Potentially misaligned types array passed, can not parse and verify fields');
		return 0;
	}
	// if that checks out we should be good to go
	// define how to parse various fields
	var regexParse = function(regex, field) {
		m = regex.exec(field);
		if (m != null) {
			return true;
		}
		else {
			return false;
		}
	}
	// regexParse will work as a base for creating other parsing methods
	this.verifyAlpha = function(field) {
		regex = /[A-Za-z]+/;
		return regexParse(regex, field);
	}
	this.verifyAlphaNum = function(field) {
		regex = /[\w]+/;
		return regexParse(regex, field);
	}
	this.verifyNum = function(field) {
		regex = /[\d]+/;
		return regexParse(regex, field);
	}
	// those are our generic parsing methods, now let us define special ones for phone numbers etc
	this.verifyPhone = function(field) {
		if (field.length < 7) {
			// well we can rule that out right away can't we?
			return false;
		}
		regex = /1?\(?([\d]{3})\)?.?([\d]{3}).?([\d]{4})/; // ha phone numbers..
		return regexParse(regex, field);
	}
	this.verifyEmail = function(field) {
		regex = /([\w.-]+)@([\w.-]+)\.([\w]{2,3})/;
		return regexParse(regex, field);	
	}
	// I believe that should cover the needed parsing methods for the data types our form verification lib supports
	// now we need to make a method that will put it all together
	this.autoVerify = function() {
		for (i = 0; i < text_fields.length; i++) {
			// first we determine the data type of this field
			type = data_types[i];
			switch(type) {
				case 'alpha':
					clean = this.verifyAlpha(text_fields[i].value);
					break;
				case 'alphanum':
					clean = this.verifyAlphaNum(text_fields[i].value);
					break;
				case 'numeric':
					clean = this.verifyNum(text_fields[i].value);
					break;
				case 'phone':
					clean = this.verifyPhone(text_fields[i].value);
					break;
				case 'email':
					clean = this.verifyEmail(text_fields[i].value);		
					break;
				default:
					return("Unsupported type passed to data_types array");
			}
			// now we check to make sure this item verified
			if (clean == false) {
				// oh no, the data did not clear, handle the problem
				if (text_fields[i].getAttribute('error') == '1') {
					// we have already highlighted this field etc, for some reason the user doesn't get it :)i
					return false;
				}
				name = text_fields[i].getAttribute('name');
				// set the border of failed box to red, let the user know it is required
				text_fields[i].setAttribute('style', 'border: 1px solid red;');
				span = document.createElement('span');
				span.innerHTML = "<b>Required</b>";
				span.setAttribute('style', 'color: red; padding-left: 5px;');
				//text_fields[i].getParent('td').appendChild(span);
				text_fields[i].parentNode.appendChild(span);
				// set the action for when the user clicks on the box to correct the issues
				text_fields[i].onclick = function() {
					text_fields[i].setAttribute('style', 'border: 1px inset;');
					//text_fields[i].getParent('td').removeChild(span);
					text_fields[i].parentNode.removeChild(span);
					text_fields[i].setAttribute('error', 0);
				}
				// finally mark the field as error flagged
				text_fields[i].setAttribute('error', 1);
				return false;
			}
			else {
				// nothing to do 
			}
		}
	}
}

function verifySelects() { 
	selects = document.getElementsByTagName('select');
	for (i = 0; i < selects.length; i++) {
		if (selects[i].getAttribute('error') == 1) {
			continue;
		}
		if (selects[i].value == "--- Please Choose ---") {
			selects[i].setAttribute('error', 1);
			span = document.createElement('span');
                        span.innerHTML = "<b>Required</b>";
                        span.setAttribute('style', 'color: red; padding-left: 5px;');
			selects[i].parentNode.appendChild(span);
			selects[i].onfocus = function() {
				selects[i].getElementsByTagName('span')[0].innerHTMl = "";
				selects[i].setAttribute('error', 0);
			}
		}
	}
}

function setLocalStore() {
	// set some of our user info from the self eval form into local storage for the contact form
	// first we collect the information from the form
	name = document.getElementsByName('text_18')[0].value;
	phone = document.getElementsByName('text_16')[0].value;
	email = document.getElementsByName('text_15')[0].value;
	// now we check for HTML5 local storage support and if it is there we save this data
	if (typeof(localStorage) == 'undefined') {
		// unfortunately for this user we can not help them fill out the next form
		return false;
	}	
	localStorage.setItem('name', name);
	localStorage.setItem('phone', phone);
	localStorage.setItem('email', email);
}

function setLocalStore2() {
	// set some of our user info in the local storage for use on forms
	fields = document.getElementsByClassName('form_item');
	if (typeof(localStorage) == 'undefined') {
		// unfortunately for this user they will have to fill out the form in it's entirety
		return false;
	}
	// now we need to collect the labels and values of each div
	labels = new Array();
	values = new Array();
	for (i = 0; i < fields.length; i++) {
		try {
			labels[i] = fields[i].getElementsByTagName('label')[0].innerHTML;
			values[i] = fields[i].getElementsByTagName('input')[0].value;
		}
		catch(err) {
			// this means we were not able to make this one happen, just skip it
			continue;
		}
	}
	// now they we have built a label-value relation, let us add these to local storage
	for (i = 0; i < fields.length; i++) {
		localStorage.setItem(labels[i], values[i]);
	}
}

function readLocalStore() {
	// we will check and see if there is some local data available to help fill out a form
	try {
		name = localStorage.getItem('name');
		phone = localStorage.getItem('phone');
		email = localStorage.getItem('email');
	}
	catch (err) {
		// looks like we either do not support local storage or it has not been set
		return false;	
	}
	// we must split the name up for the contact form
	if (name == 'undefined' || name == null) {
		return 0;
	}
	else if (phone == 'undefined' || phone == null) {
		return 0;
	}
	else if (email == 'undefined' || email == null) {
		return 0;
	}
	try {
		sname = name.split(" ");
		fname = sname[0];
		lname = sname[1];	
	}
	catch(err) {
		fname = name;
		lname = "";
	}
	// now we can begin setting the fields
	document.getElementsByName('fname')[0].value = fname;
	document.getElementsByName('lname')[0].value = lname;
	document.getElementsByName('phone1')[0].value = phone;
	document.getElementsByName('email')[0].value = email;
}

function readLocalStore2() {
	// read items from HTML5 storage
	





}
















