﻿function StartLightBoxAgentReferralForm(visistatdid, visistatPageName) {
    InsertExtraVisistat(visistatdid, visistatPageName);

    //curtains up, start the show!

    $('#agentreferralform-thankyou').hide();
    $('#agentreferralform-info').find('.lightboxtopwrapper').show();
    $('#agentreferralform-info').find('.lightboxform').show();
    $('#agentreferralform-lightbox').show().fadeTo(300, 0.6, function () { startLightBox2extraagentreferral('#agentreferralform-lightbox', '#agentreferralform-info', '#agentreferralform-wrapper'); });
}

//finish loading up the show and setup the close button
function startLightBox2extraagentreferral(divtoclose, divtoshow, wrapper) {
    $(wrapper).show().fadeTo(300, 1.0);

    $(divtoshow).show();
    $(wrapper).find('a.close').click(
	function () {
	    $(wrapper).fadeTo(300, 0.0, function () { closeLightboxextraagentreferral(divtoclose, wrapper); });
	}
		);
}

function closeLightboxextraagentreferral(divtoclose, wrapper) {
    closeJsError_AgentReferral();
    $(divtoclose).fadeTo(300, 0.0, function () { hideLightboxextraagentreferral(divtoclose, wrapper); });
}

function hideLightboxextraagentreferral(divtoclose, wrapper) {
    $(wrapper).hide();
    $(divtoclose).hide();
}

function constructErrorMsg_AgentReferral(errormsg, addme) {
    if (errormsg != '') {
        errormsg = errormsg + ', ';
    }
    errormsg = errormsg + addme;

    return errormsg;
}

function showJsError_AgentReferral(errormsg) {
    if (errormsg != '') {
        $('.formerror').html(errormsg);
    }
    $('#lightbox-messaging').show().animate({ top: "0" }, { duration: 800, easing: 'easeOutExpo' }).animate({ top: "-50px" }, { duration: 300, easing: 'easeInExpo' });

    setTimeout('$("#lightbox-messaging a.closeerror").fadeTo(800, 1.0);', 1200);

    $('#lightbox-messaging a.closeerror').click(
		   function () {
		       closeJsError_AgentReferral();
		   }
	    );
}

function OnAgentReferralAjaxError(arg, msg) {
    showJsError_AgentReferral(msg);
}

function closeJsError_AgentReferral() {
    $("#lightbox-messaging a.closeerror").fadeTo(800, 0.0);
    $('#lightbox-messaging').animate({ top: "-300px" }, { duration: 800, easing: 'easeInExpo', complete: hideErrorMessaging_AgentReferral });
}

function hideErrorMessaging_AgentReferral() {
    $('#lightbox-messaging').hide();
}

function ThankYouAgentReferral(title, msg, thankyoudivid, infodivid) {
    $(thankyoudivid).find('h2').html(title);
    $(thankyoudivid).find('p').html(msg);
    $(infodivid).find('.lightboxtopwrapper').hide();
    $(infodivid).find('.lightboxform').hide();
    $(thankyoudivid).show();
}

function SendAgentReferral(systemid) {

    var form, errorMsg, vFields, pName;

    form = $('#agent-referral-form');
    errorMsg = '';

    //enter in the field id's to be validated
    vFields = new Array("agentreferralform_name", "agentreferralform_site");
    //enter in the display names for each field in the same order as above
    pName = new Array(R.Common.YourName, R.Common.YourSite);

    errorMsg = validate(false, vFields, pName);

    if (errorMsg == 'success') {
        $.post(
            form.attr('action'),
            form.serialize(),
            OnSendAgentReferralComplete
        );
    }
    else {
        showJsError_AgentReferral(errorMsg);
    }
}

function OnSendAgentReferralComplete(d) {
    if (d.Success) {
        closeJsError_AgentReferral();
        ThankYouAgentReferral(R.Common.ThankYou, d.Message, '#agentreferralform-thankyou', '#agentreferralform-info');
        resetAgengReferralFields();
    }
    else {
        showJsError_AgentReferral(d.Message);
    }
}

function resetAgengReferralFields() {
    $('#agentreferralform_name').val('');
    $('#agentreferralform_company').val('');
    $('#agentreferralform_city').val('');
    $('#agentreferralform_state').val('');
    $('#agentreferralform_site').val('');
    $('#agentreferralform_email').val('');
    $('#agentreferralform_phone').val('');
    $('#agentreferralform_linktitle').val('');
    $('#agentreferralform_description').val('');
    $('#agentreferralform_location').val('');
}
