
//var savedFields = {};
function jsForm() {
	var box;
	$("form td a").click(function(){
		if (!box)
			box = new $.msgBox(this.innerHTML.replace(":", ""), $("#form .pop").show());
		else
			box.show();
		return false;
	}).not("a[title]").each(function(){
		if (dHover)
			$(this).attr("title", "What is unsecured debt?").Tooltip({ showURL: false, extraClass: "small" });
	});
	$("#fb a").not("a[target]").unbind().click(function(){ return false; }).Tooltip({ showURL: false, extraClass: "small" });
	$("input[defval]").focus(function(){
		var me = $(this);
		if (me.val() == me.attr("defval"))
			me.val("").removeClass("def");
	}).blur(function(){
		var me = $(this);
		if ($.trim(me.val()).length == 0 || me.val() == me.attr("defval"))
			me.val(me.attr("defval")).addClass("def");
	}).blur();
	$("form td.phone input").each(function(){
		var all = $(this).parent().children("input");
		var l = all.length;
		var i = all.index(this) + 1;
		$(this).autotab({format: "numeric", target: (i < l ? all.get(i) : null), previous: (i > 1 ? all.get(i - 2) : null)});
	});
	var mulphones = ($("form td.mul").length > 1);
	$("form :text").change(function(){
		var me = $(this).unbind("mouseenter");
		if ($.trim(me.val()).length == 0 || me.val() == me.attr("defval")) {
			if (mulphones && me.parent().hasClass("phone")) {
				addImage(me).remove();
				me.removeClass("error").removeClass("valid");
			} else
				makeInvalid(me, "this information is required");
			return;
		}
		$.getJSON("../validation.ashx", {field: this.id, value: this.value}, function(data){
			if (data.success) {
				if (me.parent().hasClass("phone")) {
					makeValid(me, true);
					if (me.siblings(".error").length == 0) {
						if (me.siblings(".valid").length == 2)
							makeValid(me);
						else
							addImage(me).remove();
					} else
						makeInvalid(me, "this is not a valid phone number", true);
				} else
					makeValid(me);
			} else
				makeInvalid(me, data.error);
		});
	});
	$("form select").change(function(){
		var me = $(this);
		if (me.val() == "select")
			makeInvalid(me, "this information is required");
		else
			makeValid(me);
	});
	$("form").submit(function(){
		if (!overten) {
			var m = new $.msgBox("Error", "<p>Please select whether you have<br />at least $10,000 in credit card debt.</p>", { remove: true });
			return false;
		}
		var valid = true, changed = false;
		$("form input:text, form select").each(function(){
			var me = $(this);
			if (!me.hasClass("valid")) {
				if (me.hasClass("error")) {
					valid = false;
				} else {
					var v = me.val();
					if (v.length > 0 && v != "select")
						changed = true;
					else if (!me.parent().hasClass("mul"))
						valid = false;
					me.change();
				}
			} //else
			//savedFields[this.id] = me.val();
		});
		if (changed) {
			var acf = function(){
				$().unbind("ajaxStop", acf);
				$("form").submit();
			};
			$().bind("ajaxStop", acf);
			$.get("../get.htm");
			return false;
		}
		if (mulphones) {
			var hasone = false;
			$("form td.phone").each(function(){
				if ($(this).children(".valid").length == 3)
					hasone = true;
			});
			if (!hasone) {
				var m = new $.msgBox("Error", "<p>At least one of the phone numbers needs to be filled in.</p>", { remove: true });
				return false;
			}
		}
		if (!valid) {
			errorsPop();
			return false;
		}
		$("#submit").hide();
		$(this).ajaxSubmit({ dataType: "json", success: function(data){
			if (data.success) {
				nopopup = true;
				PreventExitPop = true;
				if (data.nextForm) {
					$("#form").slideUp(function(){
						var f = $(this);
						f.load(data.nextForm, function(){
							//$.each(savedFields, function(k, v){
							//	if (v.length > 0)
							//		$("#" + k, f).val(v).parent().parent().hide();
							//});
							f.slideDown();
							jsForm();
						});
					});
					$("body").addClass("submitted");
				} else
					location.replace(data.nextPage);
			} else {
				if (data.error)
					var m = new $.msgBox("Error", "<p>" + data.error + "</p>", { remove: true });
				else {
					$.each(data.errors, function(id, err){
						makeInvalid($("#" + id), err);
					});
					errorsPop();
				}
				$("#submit").show();
			}
		}});
		var da2 = $("#debt_amount2").val();
		if (da2 != "select" && (da2 - 0) <= 5 && $.isFunction(pop))
			pop("388");
		return false;
	});
	if (initFocus) {
		var fel = $("form :text:eq(0)");
		if (fel.length > 0)
			fel[0].focus();
	}
}
$(function(){
	jsForm();
	var ot = $("input:radio[name=over_ten]").click(function(){
		overten = true;
		var f = function(first, second){
		};
	});
});