

function popUp(URL, width, height) {
  window.open(URL, '', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height+'');
}



//BEGIN scripts for the sponsor-preview functionality

function checkForFiveDigits(control) {
    if (control.value.length == 5 && (!isNaN(control.value)))
        GetSponsorName(control.value);
    else {
        //welcome message is stored in fulleval.htm
        var welcomeMessage = $('#welcomeMessage').get(0).innerHTML;
        $('#sponsor-preview').get(0).innerHTML = welcomeMessage;
    }
}
function GetSponsorName(zip) {
    var webMethod = '/TbkWebService.asmx/GetSponsorName';
    //var webMethod = '/EvalForm.aspx/GetSponsorName';
    var networkId = 1;

    //messages are stored in fulleval.htm
    var dudMessage = $('#dudMessage').get(0).innerHTML;
    var errorMessage = dudMessage;
    var successMessage = $('#successMessage').get(0).innerHTML;

    $.ajax({
        type: "POST",
        url: webMethod,
        data: "{'zipcode':'" + parseFloat(zip) + "','networkId':'" + parseFloat(networkId) + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg.d.length > 0) {
                var response = JSON.parse(msg.d);
                var sponsorName = response.SponsorName;
                $('#sponsor-preview').get(0).innerHTML = successMessage.replace("**SponsorName**", sponsorName);
            }
            else {
                $('#sponsor-preview').get(0).innerHTML = dudMessage;
            }
        },
        error: function(e) {
            $('#sponsor-preview').get(0).innerHTML = errorMessage;
        }
    });
}

//END scripts for the sponsor-preview functionality
//BEGIN functions for New Jersey confirmation pop

//accepted makes sure they have accepted the popup, we don't re-pop if they enter a diff jersey zip
var accepted = false;

//zipcodeid is the DOM id of the zipcode text box that we clear out if they decline
var zipcodeid = '';

//sent is a hack - they keyup function was being called twice on the last digit of zipcode. this prevents the popup from loading twice
var sent = false;


//firedOnKeyup makes sure the onchange event doesn't re-fire the popup
var firedOnKeyup = false;


function CheckForNewJerseyZip(control) {
    var zip;
    if (control.value.length == 5 && (!isNaN(control.value)) && !sent) {
        sent = true;
        zip = control.value;
        zipcodeid = control.id;
        var webMethod = njUrl;
        $.getJSON(webMethod + '?zipcode=' + zip + '&callback=?', function(msg) {
            var isNewJerseyZip = msg.IsNewJerseyZipcode;
            if (isNewJerseyZip) {
                DisplayNewJerseyModal(msg.FaceBoxHtml);
            }
            else
                sent = false;
        });

    }
    else {
        sent = false;
    }
}

function ClickContinue() {
    accepted = true;
    $('.next').removeAttr('disabled');
    try {
        parent.CloseNJ();
    }
    catch (err) {
        parent.postMessage('CloseNJ', '*');
    }
    sent = false;
}
function DisplayNewJerseyModal(FaceBoxHtml) {
    if (accepted != true) {
        try {
            parent.show(FaceBoxHtml);
        }
        catch (err) {
            parent.postMessage(JSON.stringify({ action: 'ShowNJ', html: FaceBoxHtml }), '*');
        }
        $('.next').attr('disabled', 'disabled');
    }
}
function Decline() {
    accepted = false;
    $('#' + zipcodeid).val('');
    try {
        parent.CloseNJ();
    }
    catch (err) {
        parent.postMessage('CloseNJ', '*');
    }

    $('.next').removeAttr('disabled');

    sent = false;
    return false;
}


$(function() {
    if (window.location.toString().indexOf('affevalform.aspx') == -1 && window.location.toString().indexOf('affdbsevalform.aspx') == -1) {
        $('input[id*="Zipcode"]').bind('keyup', function(e) {
            if (!e)
                e = window.event;
            if ((e.keyCode >= 47 && e.keyCode <= 58) || (e.keyCode >= 96 && e.keyCode <= 105) || (e.keyCode == 8)) {
                firedOnKeyup = true;
                CheckForNewJerseyZip(this);
            }
        });

        $('input[id*="Zipcode"]').each(function() {
            CheckForNewJerseyZip(this);
        });

        $('input[id*="Zipcode"]').bind('change', function() {            
            if (!firedOnKeyup)
                CheckForNewJerseyZip(this);

            firedOnKeyup = false;
        });
    }
});


//END functions for New Jersey confirmation pop
