// Global Variables

var cms_affiliate_ajax = '/lib/ajax/cms_affiliate_ajax.asp';

// OBJECTS

objCMSAffiliateSignUp = {

    render : function() {
        $.get(cms_affiliate_ajax,
        
            { 
                callmethod : 'renderSignUp'
            },
            
            function(str_response) {
                arr_response = str_response.split('||');
                if (arr_response[0] == '1') {
                    $('#divCMSAffiliateArea').html(arr_response[1]);
                    $('#frmAffiliateSignUp input[name=fldPayableTo]').focus();
                } else {
                    alert(arr_response[1]);
                }
            }
            
        ,'text');
    },
    
    saveAffiliate : function() {
    
        vError = '';
        if ($('#frmAffiliateSignUp input[name=fldPayName]').val() == '') { vError = vError + '-> Payable To\n'; }
        if ($('#frmAffiliateSignUp input[name=fldFirstName]').val() == '') { vError = vError + '-> First Name\n'; }
        if ($('#frmAffiliateSignUp input[name=fldLastName]').val() == '') { vError = vError + '-> Last Name\n'; }
        if ($('#frmAffiliateSignUp input[name=fldEmail]').val() == '') { vError = vError + '-> E-mail Address\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAddress]').val() == '') { vError = vError + '-> Address\n'; }
        if ($('#frmAffiliateSignUp input[name=fldCity]').val() == '') { vError = vError + '-> City\n'; }
        if ($('#frmAffiliateSignUp input[name=fldState]').val() == '') { vError = vError + '-> State\n'; }
        if ($('#frmAffiliateSignUp input[name=fldZip]').val() == '') { vError = vError + '-> Zip/Postal Code\n'; }
        if ($('#frmAffiliateSignUp input[name=fldCountry]').val() == '') { vError = vError + '-> Country\n'; }
        if ($('#frmAffiliateSignUp input[name=fldPhone]').val() == '') { vError = vError + '-> Phone\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAffiliateUsername]').val() == '') { vError = vError + '-> Username\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAffiliatePassword]').val() == '') { vError = vError + '-> Password\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAffiliatePassword2]').val() == '') { vError = vError + '-> Confirm Password\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAffiliatePassword]').val() != $('#frmAffiliateSignUp input[name=fldAffiliatePassword2]').val()) { vError = vError + '-> Passwords Do Not Match\n'; }
        if ($('#frmAffiliateSignUp input[name=fldAgree]').attr('checked') == false) { vError = vError + '-> Confirm that you agree to the Affiliate Agreement\n'; }
        
        if (vError == '') {
        
            $.post(cms_affiliate_ajax,$('#frmAffiliateSignUp').serialize(),
                
                function(str_response) {
                    arr_response = str_response.split('||');
                    if (arr_response[0] == '1') {
                        location.href = '/web/content/AffiliateDashboard/page.asp'
                    } else {
                        alert(arr_response[2]);
                    }
                }
                
            ,'text');
        } else {
            alert('The following items are missing from your Affiliate Sign Up form:\n\n' + vError);
        }
    }
    
}

objCMSAffiliateSignIn = {

    render: function () {
        $.get(cms_affiliate_ajax,

            {
                callmethod: 'renderSignIn'
            },

            function (str_response) {
                arr_response = str_response.split('||');
                if (arr_response[0] == '1') {
                    $('#divCMSAffiliateArea').html(arr_response[1]);
                    $('#frmAffiliateSignIn input[name=fldAffiliateUsername]').focus();
                } else {
                    alert(arr_response[1]);
                }
            }

        , 'text');
    },

    signIn: function () {
        $.get(cms_affiliate_ajax,

            {
                callmethod: 'processSignIn',
                affiliateusername: $('#frmAffiliateSignIn input[name=fldAffiliateUsername]').val(),
                affiliatepassword: $('#frmAffiliateSignIn input[name=fldAffiliatePassword]').val()
            },

            function (str_response) {
                arr_response = str_response.split('||');
                if (arr_response[0] == '1') {
                    location.href = '/web/content/AffiliateDashboard/page.asp'
                } else {
                    alert(arr_response[1]);
                }
            }

        , 'text');
    },

    sendPasswordReminder: function () {
        $('#frmAffiliateSignIn input[name=fldReminderFlag]').val('true');
        $.post(cms_affiliate_ajax, $('#frmAffiliateSignIn').serialize(), function (vResponse) {
            arrResponse = vResponse.split('||');
            alert(arrResponse[1]);
        });
    }
}

objCMSAffiliateDashboard = {

    render : function(vView,vReportStartDate,vReportEndDate) {
        $.get(cms_affiliate_ajax,
        
            { 
                callmethod : 'renderDashboard',
                view : vView,
                reportstartdate : vReportStartDate,
                reportenddate : vReportEndDate
            },
            
            function(str_response) {
                arr_response = str_response.split('||');
                if (arr_response[0] == '1') {
                    $('#divCMSAffiliateArea').html(arr_response[1]);
                } else {
                    location.href = '/web/content/AffiliateLogIn/page.asp';
                }
            }
            
        ,'text');
        
    },
    
    logOff : function() {
        $.get(cms_affiliate_ajax,
        
            { 
                callmethod : 'logOff'
            },
            
            function(str_response) {
                location.href = '/web/content/AffiliateLogIn/page.asp';
            }
            
        ,'text');
    },
    
    saveAffiliate : function() {

        vError = '';
        if ($('#frmAffiliateUpdate input[name=fldPayName]').val() == '') { vError = vError + '-> Payable To\n'; }
        if ($('#frmAffiliateUpdate input[name=fldCompanyName]').val() == '') { vError = vError + '-> Company Name\n'; }
        if ($('#frmAffiliateUpdate input[name=fldFirstName]').val() == '') { vError = vError + '-> First Name\n'; }
        if ($('#frmAffiliateUpdate input[name=fldLastName]').val() == '') { vError = vError + '-> Last Name\n'; }
        if ($('#frmAffiliateUpdate input[name=fldEmail]').val() == '') { vError = vError + '-> E-mail Address\n'; }
        if ($('#frmAffiliateUpdate input[name=fldAddress]').val() == '') { vError = vError + '-> Address\n'; }
        if ($('#frmAffiliateUpdate input[name=fldCity]').val() == '') { vError = vError + '-> City\n'; }
        if ($('#frmAffiliateUpdate input[name=fldState]').val() == '') { vError = vError + '-> State\n'; }
        if ($('#frmAffiliateUpdate input[name=fldZip]').val() == '') { vError = vError + '-> Zip/Postal Code\n'; }
        if ($('#frmAffiliateUpdate input[name=fldCountry]').val() == '') { vError = vError + '-> Country\n'; }
        if ($('#frmAffiliateUpdate input[name=fldPhone]').val() == '') { vError = vError + '-> Phone\n'; }
        if ($('#frmAffiliateUpdate input[name=fldAffiliateUsername]').val() == '') { vError = vError + '-> Username\n'; }
        if ($('#frmAffiliateUpdate input[name=fldAffiliatePassword]').val() == '') { vError = vError + '-> Password\n'; }
        if ($('#frmAffiliateUpdate input[name=fldAffiliatePassword2]').val() == '') { vError = vError + '-> Confirm Password\n'; }
        if ($('#frmAffiliateUpdate input[name=fldAffiliatePassword]').val() != $('#frmAffiliateUpdate input[name=fldAffiliatePassword2]').val()) { vError = vError + '-> Passwords Do Not Match\n'; }
        
        if (vError == '') {

            $.post(cms_affiliate_ajax,$('#frmAffiliateUpdate').serialize(),
                
                function(str_response) {
                    arr_response = str_response.split('||');
                    if (arr_response[0] == '1') {
                        if (arr_response[1] == 'UpdateSuccess') {
                            $('#divUpdateStatus').slideUp('1000',function() {  $('#divUpdateStatus').slideDown('2000'); });
                            $('#divUpdateStatus').html(arr_response[2]);
                        } else {
                            location.href = '/web/content/AffiliateDashboard/page.asp'
                        }
                    } else {
                        alert(arr_response[2]);
                    }
                }
                
            ,'text');
        } else {
            alert('The following items are missing from your Affiliate Info form:\n\n' + vError);
        }
    },
    
    submitReferral : function() {

        vError = '';
        if ($('#frmAffiliateReferral input[name=fldReferralCompany]').val() == '') { vError = vError + '-> Company Name\n'; }
        if ($('#frmAffiliateReferral input[name=fldReferralContact]').val() == '') { vError = vError + '-> Contact Name\n'; }
        if ($('#frmAffiliateReferral input[name=fldReferralPhone]').val() == '') { vError = vError + '-> Phone Number\n'; }
        if ($('#frmAffiliateReferral input[name=fldReferralEmail]').val() == '') { vError = vError + '-> E-mail Address\n'; }
        
        if (vError == '') {

            $.post(cms_affiliate_ajax,$('#frmAffiliateReferral').serialize(), 
                
                function(str_response) {
                    arr_response = str_response.split('||');
                    if (arr_response[0] == '1') {
                        $('#divReferralStatus').slideUp('1000',function() {  $('#divReferralStatus').slideDown('2000'); });
                        $('#divReferralStatus').html(arr_response[1]);
                        $('#frmAffiliateReferral')[0].reset();
                    } else {
                        alert(arr_response[1]);
                    }
                }
                
            ,'text');
        } else {
            alert('The following items are missing from your Affiliate Referral form:\n\n' + vError);
        }
    }
    
}

$(document).bind("ajaxError", function(vEvent, request, settings, thrownError){
    
    // Change false to true (below) to skip friendly error
    if(false || confirm("We apologize but your request could not be handled at this time.\n\nWould you like to see more information?")){
        var vPrms = settings.data.split("&");
        
        for(var p = 0; p<vPrms.length; p++){
            if(vPrms[p].length > 50) vPrms[p] = "[LONG TEXT CONTENT]"
        }
        
        var dbg = "Please provide the information below with any support requests:\n\n" + 
              "INFO:" + decodeHTMLEntities(request.responseText.replace(/(<([^>]+)>)/ig,"").replace(/(\-([\-]+)\^)/ig,"")) + "\n\n" +
              "DATA:\n" + (vPrms.length < 15 ? vPrms.join("\n") : vPrms.join("&"))
    
        alert(dbg);
    }
    
    function decodeHTMLEntities(strVal) {
        var ta=document.createElement('textarea');
        ta.innerHTML = strVal;
        return ta.value;
    }
});

