﻿/// <reference path="jquery-1.3.2.js" />
var feedbackUrl;
var clearUrl;

$(function() {
    $("#homePhone").keypress(function(e) {
        if (e.which == 13) {
            $("#LastName").focus();
            return false;
        }
    });
    $("#LastName").keypress(function(e) {
        if (e.which == 13) {
            step5NextClick();
            return false;
        }
    });

    $("#elapsedTime").hide();
    if (preHomePhone.length > 0) $("#homePhone").val(preHomePhone);
    if (preLastName.length > 0) {
        $("#LastName").val(preLastName);
        $(".horizMenuRight a").click(closeDonor);
    }
});

function step5NextClick() {
    var tid = $("#VehicleTicket_Territory_id").val();
    var aid = $("#VehicleTicket_ARC_id").val();
    var phone = $("#homePhone").val();
    var lastname = $("#LastName").val();
    var zip = $("#VehicleTicket_Zip").val();
    
    if (!isValidPhone(phone)) {
        alert("Please enter a valid phone number.");
        $("#homePhone").focus();
    } else if (lastname.length == 0) {
        alert("Please enter a last name.");
        $("#LastName").focus();
    } else {
        phone = unFormatPhone(phone);
        $("#step5Busy").css("display", "inline");
        $.getJSON(donorLookupUrl, { arcId: aid, homePhone: phone, lastName: lastname, zipCode: zip }, loadDonor);
        $("#homePhone").val(formatPhone(phone));
    }
}

function submitTicketReturn(data) {
    if (data == null || data.success == false) {
        $("#busyDialog .busy").hide();
        $("#busyDialog .error").show();
    } else {
        ticketSaved = true;
        window.location = data.ReturnUrl;
    }
}

function getAuditData() {
    var ret = { Arc_id: 0, PageAudit: "Unknown donor" };

    var tmp = $("#VehicleTicket_ARC_id").val();
    if (tmp && tmp.length > 0) {
        ret.Arc_id = tmp;
        ret.PageAudit = "Home Phone: " + $("#VehicleTicket_HomePhone").val() +
          "\r\nFirst Name: " + $("#VehicleTicket_FirstName").val() +
          "\r\nLast Name: " + $("#VehicleTicket_LastName").val() +
          "\r\nZip Code: " + $("#VehicleTicket_Zip").val() +
          "\r\nARC: " + tmp;
    }
    return ret;
}


function closeDonor() {
    if (confirm("Would you like to close this donor and return to the home page? Any progress made on this donation will be lost.")) {
        $("div.horizMenuRight").html("Closing session ...");
        ticketSaved = true;
        $.getJSON(clearUrl, null, function() {
            window.location = rootDir;
        });
    }
} 