function autoTab(field1, len, field2) {
    if (document.getElementById(field1).value.length == len) document.getElementById(field2).focus();
}



function isEmail(value) {
    if (value == null || !value.toString().match(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/)) return false;
    return true;
}



function isName(value) {
    if (value == null || !value.toString().match(/^[A-z ]*$/)) return false;
    return true;
}



function error_check(type, stepnum) {
    var pass = true;
    var phone_pass = true;
    cur_pass = true;
    var skip = false;
    var non_required = new Array('pnum2a', 'pnum2b', 'pnum2c', 'alt_zip');
    $("#" + type + "_step_" + stepnum + " input, #" + type + "_step_" + stepnum + " select").each(
    function () {
        skip = false;
        for (x in non_required) {
            if (this.name == non_required[x]) {
                skip = true;
                cur_pass = true;
            }
        }
        if (!skip) {
            if (this.type == 'radio' || this.type == 'checkbox') {
                if ($("#" + type + "_step_" + stepnum + " input[name=" + this.name + "]:checked").length === 0) {
                    pass = false;
                    cur_pass = false;
                }
                else cur_pass = true;
            }

            else if (this.type == 'text') {
                if (this.value == "") {
                    pass = false;
                    cur_pass = false;
                }

                else {
                    var name = this.name;
                    if (name.indexOf('pnum1') >= 0) {
                        var char = name.substr(name.length - 1);
                        var num = 0;
                        if (char == 'a') num = 1;
                        else if (char == 'b') num = 2;
                        else num = 3;

                        if (phone_pass) {
                            if (isNaN(this.value)) {
                                pass = false;
                                phone_pass = false;
                                cur_pass = false;
                            }
                            else {
                                if ((num < 3 && this.value.length != 3) || (num == 3 && this.value.length != 4)) {
                                    pass = false;
                                    phone_pass = false;
                                    cur_pass = false;
                                }
                                else cur_pass = true;
                            }
                        }
                        if (num == 3) phone_pass = true;
                    }

                    if (name.indexOf('email') >= 0) {
                        if (!isEmail(this.value)) {
                            pass = false;
                            cur_pass = false;
                        }
                        else cur_pass = true;
                    }

                    if (name.indexOf('name') >= 0) {
                        if (!isName(this.value)) {
                            pass = false;
                            cur_pass = false;
                        }
                        else cur_pass = true;
                    }

                    if (name.indexOf('zip') >= 0) {
                        if (isNaN(this.value) || this.value.length < 5) {
                            pass = false;
                            cur_pass = false;
                        }
                        else cur_pass = true;
                    }
                }
            }

            else if (this.tagName.toLowerCase() == 'select') {
                if ($("#" + type + "_step_" + stepnum + " select[name=" + this.name + "]").val() == "") {
                    pass = false;
                    cur_pass = false;
                }

                else cur_pass = true;
            }
        }

        if (cur_pass == false) {
            if (stepnum < 3) {
                $(this).parent().parent().children("td:first").addClass("errorInfo");
                $(this).parent().children("kbd:first").addClass("error_text");
                $(this).parent().children("kbd:first").css("display", "inline");
            }
            else {
                $(this).parent().children("kbd:first").addClass("error_text");
                $(this).parent().children("kbd:first").css("display", "inline");
            }
        }

        else {
            if (stepnum < 3) {
                $(this).parent().parent().children("td:first").removeClass("errorInfo");
                $(this).parent().children("kbd:first").removeClass("error_text");
                $(this).parent().children("kbd:first").css("display", "none");
            }
            else {
                $(this).parent().children("kbd:first").removeClass("error_text");
                $(this).parent().children("kbd:first").css("display", "none");
            }
        }
    });
    return pass;
}



function need_zip(type) {
    var state = $("#" + type + "_form :input[name=state]").val();
    if (state == "CO" || state == "NC" || state == "VA") {
        if ($("#need_zip_" + type).val() != "1") {
            $("#need_zip").val("1");
            $("#" + type + "_zip").attr({
                name: "tz_zip"
            });
            $("#alt_" + type + "_zip").attr({
                name: "zip"
            });
            $("#" + type + "_zip_td").css({
                display: "table-cell"
            });
        }
    }
    else {
        if ($("#need_zip_" + type).val() == "1") {
            $("#need_zip").val("0");
            $("#" + type + "_zip").attr({
                name: "zip"
            });
            $("#alt_" + type + "_zip").attr({
                name: "alt_zip"
            });
            $("#" + type + "_zip_td").css({
                display: "none"
            });
        }
    }
}



function next_step(type, stepnum) {
    var prev_stepnum = stepnum - 1;
    //Figure out if anything is not up to par before moving onto the next step
    var pass_fail = error_check(type, prev_stepnum);
    //pass_fail = true;
    if (pass_fail == true) {
        //do some google analytics stuff
        var cur_page = window.location.pathname;
        var final_url = cur_page + "/" + type + "_step_" + stepnum;
        pageTracker._trackPageview(final_url);

        document.getElementById(type + "_step_" + prev_stepnum).style.display = "none";
        document.getElementById(type + "_step_" + stepnum).style.display = "block";
        change_step_num(type, stepnum);
		} else {
			$("#starting_text span").fadeOut(function() {
				$(this).css({color: 'red', paddingLeft: '10px', whiteSpace: 'nowrap'}).html("Please answer all of the questions so we can connect you with a VA Specialist.").fadeIn();
			});
		}
}



function change_step_num(type, stepnum) {
    var text = document.getElementById("step_text").innerHTML;

    $("#step" + (stepnum - 1)).css("color", "#666");
    $("#step" + (stepnum - 1)).css("background-color", "#CCC");
    $("#step" + (stepnum - 1)).css("text-decoration", "none");
    $("#step" + stepnum).css("color", "#090");
    $("#step" + stepnum).css("background-color", "");
    $("#step" + stepnum).css("text-decoration", "underline");

    if (stepnum == 2) {
        document.getElementById("starting_text").style.display = "none";
        need_zip(type);
    }

    if (stepnum == 3) {
        text = "Last Step.  You're Almost There!";
        $("#side_header_2").css("display", "none");
        $("#info_secure").css("display", "block");
        $("#side_header_2_img").attr({
            src: "images/right_secure_header.gif"
        });

        if (type == 'refinance') {
            $("#ul_1").removeClass("blue");
            $("#ul_1").addClass("green");
            $("#side_header_1_img").attr({
                src: "images/refi_right_header.gif"
            });
            $("#ul_1").html("<li>Lock in a Low Fixed Rate</li><li>Lower your Monthly Payments</li><li>No Out of Pocket Costs</li><li>The VA Streamline Makes it Easy</li>");
        }
    }
    $("#step_text").html(text);
}



function load_form(option_select) {
    document.getElementById("refinance").style.display = "none";
    document.getElementById("purchase").style.display = "none";
    var cur_page = window.location.pathname;

    var form_type = document.getElementById("loan_type").options[option_select].value;
    if (form_type == 'R') {
        document.getElementById("refinance").style.display = "block";
        document.forms["refinance_form"].loan_type.options[1].selected = "selected";
        var final_url = cur_page + "/refinance_step_1";
        //pageTracker._trackPageview(final_url);
    }

    else {
        document.getElementById("purchase").style.display = "block";
        document.forms["purchase_form"].loan_type.options[0].selected = "selected";
        var final_url = cur_page + "/purchase_step_1";
        //pageTracker._trackPageview(final_url);
    }
}



function submit_form(type, stepnum) {
    var pass_fail = error_check(type, stepnum);
    if (pass_fail == true) {
        var cur_page = window.location.pathname;
        var final_url = cur_page + "/" + type + "_step_4";
        pageTracker._trackPageview(final_url);
        document.getElementById(type + "_form").submit();
    }
}



function get_timezone() {
    var time = new Date();
    var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000);
    var gmtTime = new Date(gmtMS);
    var visitortime = new Date();
    var offset = (Math.ceil((visitortime.getTime() - gmtTime.getTime())) / 60 / 60 / 1000);
    if (offset > 0) {
        //due to the way we store the time zones in DB, so -6 = 6, but +1 = 23.
        offset = offset + (12 - (Math.abs(offset))) * 2;
    }
    offset = Math.abs(offset);
    var zip = 506; //default is CST
    /*
		502: GMT (0)
		503: "Really East" (GMT - 4)
		505: EST (GMT - 5)
		506: CST (GMT - 6)
		507: MST (GMT - 7)
		508: PST (GMT - 8)
		509: ASKT (GMT - 9)
		510: Hawaii (GMT - 10)
		511: Samoa (GMT - 11)
		512: Fiji (12 or GMT - 12)
		513: Sollomon Islands (14 or GMT + 10)
		514: Papua New Guinea (15 or GMT + 9)
		*/

    switch (offset) {
    case 0:
        zip = 502;
        break;
    case 4:
        zip = 503;
        break;
    case 5:
        zip = 505;
        break;
    case 6:
        zip = 506;
        break;
    case 7:
        zip = 507;
        break;
    case 8:
        zip = 508;
        break;
    case 9:
        zip = 509;
        break;
    case 10:
        zip = 510;
        break;
    case 11:
        zip = 511;
        break;
    case 12:
        zip = 512;
        break;
    case 14:
        zip = 513;
        break;
    case 15:
        zip = 514;
        break;
    default:
        zip = 506;
    }

    document.getElementById("purchase_zip").value = zip;
    document.getElementById("refinance_zip").value = zip;
}