var phone = /^(([(]?([2-9]\d{2})[)]?|([2-9]\d{2}))|)(-?| ?)[2-9]\d{2}[- ]?\d{4}/i;

$(function() {
	$("form").submit(function() {
		return validate();
	});
	
	$("input[type=\"text\"], select, textarea").attr("disabled","true").css("background-color","#666666");
	$("input[name=\"account\"]").removeAttr("disabled").css("background-color","#FFFFFF").keyup(function() {
    	if(this.value != "") {
			$("input[type=\"text\"], select, textarea").removeAttr("disabled").css("background-color","#FFFFFF");
		} else {
			$("input[type=\"text\"], select, textarea").attr("disabled","true").css("background-color","#666666");
			$("input[name=\"account\"]").removeAttr("disabled").css("background-color","#FFFFFF");
		}
    });
});

function validate() {
	var invalidFields = [];
	// check to see if any of the fields were filled in
	var filledIn = 0;
	$(":input").each(function() {
		if($.trim(this.value) != "") {
			filledIn++;	
		}
	});
	if(filledIn < 4) {
		$(".error").text("Please fill in at least one field below.").css("display","block");
		return false;	
	} else {
		$(".error").html('Please fill out the following fields:<span id="errorFields"></span>');
	}

	// if one field in a field group is filled in, all of them have to be
	$("div.fieldGroup").each(function() {
		var $fields = $(":text",$(this));
		var filledOut = 0;
		var emptyFields = [];
		$fields.each(function() {
			if($.trim(this.value) != "") {
				filledOut++;
				fieldUnError(this.name);	
			} else {
				emptyFields.push(this.name);
			}
		});
		if(filledOut != 0 && filledOut != $fields.size()) {
			for(var i in emptyFields) {
				invalidFields.push(getLabelForInput(emptyFields[i]));
				fieldError(emptyFields[i]);
			}
		}
	});
	
	// check for empty fields
	/*$("input[type=\"text\"],select").each(function(x) {
		if(this.value == "") {
			invalidFields.push(getLabelForInput(this.name));
			fieldError(this.name);
		} else {
			fieldUnError(this.name);		
		}
	});*/
	
	if($("input[name=\"account\"]").val() == "") {
		fieldError("account");
		invalidFields.push("Account Number");
	} else {
		fieldUnError("account");
	}
	
	// check for incorrectly specified fields	
	$("input[name=\"phone\"],input[name=\"businessPhone\"]").each(function(x) {
		// Phone number fields should be valid phone numbers
		if($("input[name=\""+this.name+"\"]").val() != "" && !this.value.match(phone)) {
			invalidFields.push(getLabelForInput(this.name));
			fieldError(this.name);
		} else  if($.inArray(getLabelForInput(this.name),invalidFields) == -1) {
			fieldUnError(this.name);
		}
	});
	
	// Zip code field should be a valid zip
	if($("input[name=\"zip\"]").val() != "" && !$("input[name=\"zip\"]").val().match(/^\d{5}([\-]\d{4})?$/)) {
		invalidFields.push(getLabelForInput("zip"));
		fieldError("zip");
	} else if($.inArray(getLabelForInput("zip"),invalidFields) == -1) {
		fieldUnError("zip");
	}
		
	if(invalidFields.length) {
		showError(invalidFields);
		return false;
	} else {
		return true;
	}
}

function showError(fields) {
	var list = "<ul><li>"+fields.join("</li><li>")+"</li></ul>";
	$("div.error > span").html(list);
	$("div.error").css("display","block");
}

function fieldError(name) {
		$("input[name=\""+name+"\"],select[name=\""+name+"\"]").css("border-color","red")
													 .css("background-color","#FFAA5F");
}

function fieldUnError(name) {
		if($("input[name=\""+name+"\"],select[name=\""+name+"\"]").attr("disabled"))
		 $("input[name=\""+name+"\"],select[name=\""+name+"\"]").css("border-color","black")
													 .css("background-color","#666666");	
		else
		$("input[name=\""+name+"\"],select[name=\""+name+"\"]").css("border-color","black")
													 .css("background-color","white");	
}

function getLabelForInput(name) {
	return $("label[for=\""+name+"\"]").text();
}

