// on load add error boxes and validation checks to every form element
addEvent(window,"load", setupForms);

// add an event to the current element
function addEvent(el,evt,fxn) {
	// cross browser handling
	if(el.addEventListener) el.addEventListener(evt, fxn, false);
	else if(el.attachEvent) el.attachEvent("on" + evt, fxn);
}

// add error boxes and validation checks to every form element
function setupForms() {
	// go through every form
	var forms = document.forms;
	for(var i = 0; i < forms.length; i++) {
		// go through every element of the current form
		var elements = forms[i].elements;
		for(var ii = 0; ii < elements.length; ii++) {
			// add validation check
			addEvent(elements[ii],"blur", dfv_validate);
			// create error box
			elements[ii].dfv_error = document.createElement("div");
			elements[ii].dfv_error.className = "dfv_error";
			// add the error box immediately after the form element
			if(elements[ii].nextSibling)
				elements[ii].parentNode.insertBefore(elements[ii].dfv_error, elements[ii].nextSibling);
			else
				elements[ii].parentNode.appendChild(elements[ii].dfv_error);
			elements[ii].blur();
		}
	}
}
// run through each validation function and collect all the errors
function dfv_validate(ref) {
	// if a reference to the element can be established
	if (element = getEventElement(ref)) {
		// initially there are no errors
		var errorsExist=false;
		var errors="";
		// get each class of the element
		var validation_array = element.className.split(/\s+/);
		// for each class of the element
		for (i = 0; i < validation_array.length; i++)
			// check to see if there is a corosponding function
			if (eval("window.validate_"+validation_array[i])) {
				// run the corrosponding function and append any errors if returned
				var curError = eval("validate_"+validation_array[i]+"(element)");
				if (curError[0]==false) {
					if (errorsExist)
						errors += "<hr />"+curError[1];
					else
						errors = curError[1];
					errorsExist=true;
				}
			}
		// if errors display the current errors
		if (errorsExist)
			displayErrors(element,errors);
		// else clear any preexisting errors
		else
			clearErrors(element);

		if (errorsExist)
			return false;
		else
			return true;
	}
}


// displays an error
function displayErrors(ref,error) {
	ref.style.background="#FF6666";
	ref.dfv_error.style.display = "block";
	ref.dfv_error.innerHTML = error;
}

// clears the error
function clearErrors(ref) {
	ref.style.background="#FFFFFF";
	ref.dfv_error.style.display = "none";
}

// get an event handler's element
function getEventElement (e) {
	if(!e) e = window.event;
	return e.currentTarget ? e.currentTarget : (e.srcElement ? e.srcElement : false);
}

// validates a phone number
function validate_dfv_phone(ref) {
	var result = [true,""];
	var validation = /(^$)|(^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Phone numbers should be in the form 123-123-1234";
	}
	return result;
}

// validates a fax number
function validate_dfv_fax(ref) {
	var result = [true,""];
	var validation = /(^$)|(^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Fax numbers should be in the form 123-123-1234";
	}
	return result;
}

// validates a phone number
function validate_dfv_required(ref) {
	var result = [true,""];
	var validation = /.+/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Required";
	}
	return result;
}

// validates a name
function validate_dfv_name(ref) {
	var result = [true,""];
	var validation = /(^$)|([a-zA-z]+)/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Invalid name";
	}
	return result;
}
// validates a ssn
function validate_dfv_ssn(ref) {
	var result = [true,""];
	var validation = /(^$)|(\d{3}-\d{2}-\d{4})/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Social security numbers should be in the form 123-12-1234";
	}
	return result;
}
// validates an email address
function validate_dfv_email(ref) {
	var result = [true,""];
	var validation = /(^$)|(^[^@]+@[^@]+.[^@]{2,4}$)/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Invalid e-mail address";
	}
	return result;
}

// validates a Zip Code
function validate_dfv_zip(ref) {
	var result = [true,""];
	var validation = /^(\s*|([a-zA-Z][0-9][a-zA-Z](\s|\-)*[0-9][a-zA-Z][0-9]|\d{5}(\-+\d*)*))$/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Zip Code must be in format \"12345\" or \"A0A 0A0\"";
	}
	return result;
}



// validates a credit card number
function validate_dfv_cc_num(ref) {
	var result = [true,""];
	var validation = /(^$)|(^\d{15}\d?$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Credit card numbers must be at least 15 digits";
	}
	return result;
}

// validates a bank account number
function validate_dfv_bank_account(ref) {
	var result = [true,""];
	var validation = /(^$)|(^\d{3}\d*$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Bank accounts must be at least three digits";
	}
	return result;
}

// validates a routing number
function validate_dfv_routing_number(ref) {
	var result = [true,""];
	var validation = /(^$)|(^[0123]\d{8}$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Routing numbers must be nine digits and start with 0, 1, 2 or 3";
	}
	return result;
}

// validates a payment amount
function validate_dfv_amount(ref) {
	var result = [true,""];
	var validation = /(^$)|(^\d+\.\d{2}$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Ammounts must be greater than 0 in the format 10.00";
	}
	return result;
}

// validates a payment amount
function validate_dfv_month(ref) {
	var result = [true,""];
	var validation = /(^$)|(^01$)|(^02$)|(^03$)|(^04$)|(^05$)|(^06$)|(^07$)|(^08$)|(^09$)|(^10$)|(^11$)|(^12$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Month must be two digits";
	}
	return result;
}

// validates a payment amount
function validate_dfv_year_2_digit(ref) {
	var result = [true,""];
	var validation = /(^$)|(^\d{2}$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Years must be two digits";
	}
	return result;
}

function validate_dfv_percentage(ref) {
	var result = [true,""];
	var validation = /^\d+(\.\d+)?%?$/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "A valid percentage should consist of only numbers and possibly a decimal point or percent sign. Examples: 7.25%, 5%";
	}
	return result;
}

// validates a payment amount
function validate_dfv_amount_loose(ref) {
	var result = [true,""];
	var validation = /(^$)|(^\d+(\.\d{2})?$)/;
	if (element.value.search(validation)==-1) { //if match failed
		result[0] = false;
		result[1] = "Ammounts must be greater than 0 in the format 10.00";
	}
	return result;
}

function validate_dfv_value_max_700(ref, max) {
	var result = [true,""]
	if (element.value > 700) {
		result[0] = false;
		result[1] = "Amount can't be greater than 700.00";
	}
	return result;
}


// validates an email address
function validate_dfv_url(ref) {
	var result = [true,""];
	var validation = /(^$)|(^https?:\/\/.+$)/;
	if (element.value.search(validation)==-1) {//if match failed
		result[0] = false;
		result[1] = "Invalid URL. URL's should start with http:// or https:// for example http://www.flowershopnetwork.com";
	}
	return result;
}

// validates all fields that need attention
// use on submit, returns true or false\
function validationChecker() {
	// go through every form
	var forms = document.forms;
	for(var i = 0; i < forms.length; i++) {
		// go through every element of the current form
		var elements = forms[i].elements;
		for(var ii = 0; ii < elements.length; ii++) {
			var trueorfalse = true;
			trueorfalse = dfv_validate({currentTarget:elements[ii]});
			
			if (elements[ii].dfv_error.style.display == "block")
			{
				alert("Problem with " + elements[ii].title + ":\n" + elements[ii].dfv_error.innerHTML);
				return false;
			}
		}
	}
	return true;
}
