﻿var cImg = "../images/checkmark.gif";
var xImg = "../images/red_x.gif";
var dHover = true;
var bHover = false;
var bType = "gif";
var initFocus = true;
var noChecks = false;
var exitPop = false;
function $$(el) {
	return $(document.createElement(el));
}
var msgBoxShow = function(box){};
var msgBoxHide = function(box){};
$.msgBox = function(title, message, opts){
	var me = this;
	var s = opts || {};

	var box = $$("div").addClass("msgBox");
	var overlay;
	if (s.overlay !== false) {
		overlay = $$("div").addClass("overlay").css("opacity", 0.5).hide().appendTo("body").click(function(){me.close();});
		if (!s.hidden)
			overlay.fadeIn("fast");
	}
	if ($.browser.msie && parseInt($.browser.version) == 6) {
		$("html, body").css({height: "100%", width: "100%"});
		if (overlay)
			$$("p").css({width: "100%", height: "100%"}).appendTo(overlay).bgiframe();
		//else
		//	box.bgiframe();
	}

	me.self = function(){
		return box;
	};
	me.hide = function(){
		box.fadeOut();
		if (overlay)
			overlay.fadeOut();
		msgBoxHide(me);
	};
	me.show = function(){
		if (overlay)
			overlay.fadeIn();
		box.fadeIn();
		msgBoxShow(me);
	};
	me.remove = function(){
		box.fadeOut("fast", function(){
			$(this).remove();
		});
		if (overlay) {
			overlay.fadeOut("fast", function(){
				$(this).remove();
			});
		}
		$().unbind("keydown", fkey);
		msgBoxHide(me);
	};
	me.close = function(){
		if (s.remove)
			me.remove();
		else
			me.hide();
	};
	var fkey = function(e){
		if (e.which == 27)
			me.close();
	}

	var t = $$("div").addClass("title").html(title).appendTo(box);
	$$("a").attr("href", "#").html("Close").appendTo(t).click(function(){
		me.close();
		return false;
	});
	var c = $$("div").addClass("message").appendTo(box).append(message);
	$().keydown(fkey);
	if (s.width)
		box.css("width", s.width);
	if (s.top)
		box.css("top", s.top);
	if (s.left)
		box.css({"left": s.left, "margin-left": 0});
	else if (s.width)
		box.css("margin-left", "-" + Math.floor(s.width / 2) + "px");
	box.css("opacity", s.opacity || (overlay ? 0.9 : 0.95)).hide().appendTo("body");
	if (!s.hidden) {
		box.fadeIn();
		msgBoxShow(me);
	}
};
function errorsPop() {
	var m = new $.msgBox("Error", "<p>" + (noChecks ? "Please fix all highlighted fields." : "Please fix all fields marked with an X.") + "</p>", {remove: true});
}
function addImage(input) {
	var img = input.siblings("img:last-child").unbind();
	if (img.length == 0)
		img = $$("img").appendTo(input.parent());
	return img;
}
function makeValid(input, inputOnly) {
	input.removeClass("error").addClass("valid").unbind("mouseenter");
	if (!inputOnly && !noChecks)
		addImage(input).attr("src", cImg);
}
function makeInvalid(input, err, imageOnly) {
	if (!imageOnly)
		input.removeClass("valid").addClass("error").unbind("mouseenter").attr("title", err).Tooltip();
	if (!noChecks)
		addImage(input).attr({src: xImg, title: err}).Tooltip({showURL: false});
}
$.ajaxSetup({timeout: 8000});
$().ajaxError(function(e, req, s){
	if (s.url.indexOf("validation") > -1) {
		var field = $("#" + s.url.substring(25).split("&")[0]);
		addImage(field).remove();
		makeValid(field, true);
	} else {
		alert("An error occurred while saving your information. Please check your internet connection and try again.");
		$("#submit").show();
	}
});
//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 input:text, form input:password").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 if (!me.parent().hasClass("noreq"))
				makeInvalid(me, "this information is required");
			return;
		}
		if (!me.parent().hasClass("secret")) {
			$.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);
			});
		} else
			makeValid(me, true);
	});
	$("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, form input:password").each(function(){
			var me = $(this);
			if (!me.hasClass("valid")) {
				if (me.hasClass("error") && !me.parent().hasClass("secret")) {
					valid = false;
				} else {
					var v = me.val();
					if (v.length > 0 && v != "select")
						changed = true;
					else if (!me.parent().hasClass("mul") && !me.parent().hasClass("noreq"))
						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();
							setTimeout(function(){
								PreventExitPop = false;
							}, 1500);
						});
					});
					$("body").addClass("submitted");
				} else if (data.nextEval) {
					window.eval(data.nextEval);
				} 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 ffound = false;
		$("form input").each(function(){
			if (!ffound && $(this).is(":text,:password")) {
				this.focus();
				ffound = true;
			}
		});
	}
}
function changeForm(url, dp) {
	$("#form").slideUp(function(){
		var f = $(this);
		var v = $("input[name=vertical_id]", f).val();
		var l = $("input[name=lead_id]", f).val();
		f.load(url + "?vertical_id=" + v + "&lead_id=" + l + (dp ? "&dp=1" : ""), function(){
			f.slideDown();
			jsForm();
		});
	});
}
function showUpsell(dp) {
	changeForm("../upsell.aspx", (dp ? true : false));
	return false;
}
function nextForm() {
	changeForm("../next.aspx", false);
	return false;
}
function submitForm() {
	$("form").submit();
	return false;
}
function exitForm() {
	location.replace("thanks.aspx");
	return false;
}
var overten = false;
var PreventExitPop = false;
$(function(){
	jsForm();
	var ot = $("input:radio[name=over_ten]").click(function(){
		overten = true;
		var f = function(first, second){
			makeValid(second, true);
			second.parent().parent().hide();
			addImage(first).remove();
			first.val("select").removeClass("error").removeClass("valid").parent().parent().show();
		};
		var y = this.value == "yes";
		f($("#debt_amount" + (y ? "" : "2")), $("#debt_amount" + (y ? "2" : "")));
	});
	if (ot.length == 0)
		overten = true;
	ot.filter(":checked").click();
	if (!noChecks) {
		var i = new Image();
		i.src = cImg;
		var j = new Image();
		j.src = xImg;
	}
	if (bHover) {
		var orig, over;
		$("#submit").each(function(){
			orig = this.src;
			over = orig.replace("n." + bType, "n_over." + bType);
		}).hover(function(){
			this.src = over;
		}, function(){
			this.src = orig;
		});
		var h = new Image();
		h.src = over;
	}
	$("#footer a, .footermimic a").click(function(){
		nopopup = true;
		PreventExitPop = true;
	});
	if (exitPop) {
		window.onbeforeunload = function(){
			if (!PreventExitPop) {
				PreventExitPop = true;
				$.get("../exitTrack.ashx");
				scroll(0, 0);
				return "\n\n Are you sure you don’t want help eliminating debt?  \n\n Click cancel to start eliminating your debt today \n\n\n\n*****************************************************";
			}
		};
	}
	$("a[href$='privacy.aspx']").click(function(){
		var leftPos = 0;
		var topPos = 40;
		if (screen) {
			leftPos = (screen.width / 2) - 300;
			topPos = (screen.height / 2) - 200;
		}
		window.open("../privacy.aspx", "privacy", "status=1,height=400,width=600,resizable=0,scrollbars=1,left="+leftPos+",top="+topPos);
		return false;
	});
});