var objHomepage = {

    slides: ['homepageSlide1', 'homepageSlide2', 'homepageSlide3', 'homepageSlide4'],
    links: ['/church-website-hosting', '/ourwork', '/church-website-hosting', '/church-website-hosting'],
    currentSlide: -1,

    startSlideshow: function () {
        objHomepage.renderSlide();
        objInterval = setInterval('objHomepage.renderSlide()', 10000);
    },

    renderSlide: function () {
        if (objHomepage.currentSlide == objHomepage.slides.length - 1) { objHomepage.currentSlide = -1 }
        $('#divTopBanner').show();
        objHomepage.currentSlide += 1;
        vSlideCode = '<a href="' + objHomepage.links[objHomepage.currentSlide] + '"><img src="/lib/templates/churchwebsitecms.com/images/' + objHomepage.slides[objHomepage.currentSlide] + '.jpg" height="381" /></a>'
            + '<div style="position:absolute;left:0px;bottom:-78px;"><img src="/lib/templates/churchwebsitecms.com/images/' + objHomepage.slides[objHomepage.currentSlide] + 'Bottom.png" height="78" /></div>';
        $('#divTopBannerArea').html(vSlideCode);
    },
    slideChooser: function (vSliderName, vDirection, vUlimit) {
        vSlide = (eval($('#' + vSliderName + '').css('left').replace('px', '')) / 334);
        if (vDirection == 'left') {
            if (vSlide < 0) {
                $('#' + vSliderName + '').animate({ left: (vSlide + 1) * 334 }, 500);
            }
        } else {
            if (vSlide > (-1*vUlimit)) {
                $('#' + vSliderName + '').animate({ left: (vSlide - 1) * 334 }, 500);
            }
        }
    }

}

var objGallery = {
    galleries: {},
    rate: 800,
    getDesigns: function (vMode, vSearch, vArea) {
        $.get('/lib/services/galleryService.asp', { fldAction: 'getDesigns', fldMode: vMode, fldSearch: vSearch }, function (vResponse) {
            arrResponse = vResponse.split('||');
            $('#' + vArea).html(arrResponse[1]);
            objGallery.galleries[vArea] = arrResponse[0];
        });
    },
    move: function (vDirection, vDIV) {
        vCount = eval(objGallery.galleries[vDIV]);
        vWidth = eval((vCount-1) * 214);
        vLeft = eval($('#' + vDIV).css('left').replace('px', ''));
        if (vDirection == 'right') {
            $('#' + vDIV).animate({ left: '-' + eval(vWidth) + 'px' }, { easing: 'linear', duration: ((vCount * objGallery.rate) * ((vWidth - (vLeft * -1)) / vWidth)) });
        } else {
            $('#' + vDIV).animate({ left: '+0px' }, { easing: 'linear', duration: ((vCount * objGallery.rate) * (vLeft / vWidth)) * -1 });
        }
    },
    stopMove: function (vDIV) {
        $('#' + vDIV).stop(false, false);
    }
}

var objOrder = {
    currentTab: 0,
    getForm: function () {
        $.get('/lib/services/orderService.asp', { fldAction: 'getForm' }, function (vResponse) {
            $('#divOrderWebsite').html(vResponse);
        });
    },
    switchPaymentMethod: function (vPaymentMethod) {
        if (vPaymentMethod == 'Credit Card') {
            $('#divCheckDraft').fadeOut('fast', function () {
                $('#divCheckDraftTerms').slideUp('fast');
                $('#divCreditCard').fadeIn('fast');
                $('#divCreditCardTerms').slideDown('fast');
            });
        } else if (vPaymentMethod == 'Check Draft') {
            $('#divCreditCard').fadeOut('fast', function () {
                $('#divCreditCardTerms').slideUp('fast');
                $('#divCheckDraft').fadeIn('fast');
                $('#divCheckDraftTerms').slideDown('fast');
            });
        }
    },
    changeTabs: function (vTab) {
        for (a = 1; a <= 7; a++) {
            if (vTab == a) {
                $('#divOrderStep' + a).show();
                $('#divOrderTab' + a).css('display', 'block')
            } else {
                $('#divOrderStep' + a).hide();
            }
        }
    },
    renderProfileTab: function () {
        $.get('/lib/services/orderService.asp', { fldAction: 'renderProfileTab' }, function (vResponse) {
            $('#divOrderStep1Content').html(vResponse);
        });
    },
    profileSendReminder: function () {
        if ($('#frmReminder input[name=fldUE]').val() != '') {
            $.get('/lib/services/orderService.asp', $('#frmReminder').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    alert('Your login information has been sent to the e-mail address we have on file for you. Please check your e-mail within the next few minutes. If you do not receive an e-mail, please contact us for support.');
                } else {
                    alert(arrResponse[1]);
                }
            });
        } else {
            alert('Please tell us your USERNAME or E-MAIL ADDRESS before requesting a reminder. It\'s the only way that we can find you in our system.');
        }
    },
    signIn: function () {
        if ($('#frmSignIn input[name=fldUsername]').val() != '' && $('#frmSignIn input[name=fldPassword]').val() != '') {
            $.get('/lib/services/orderService.asp', $('#frmSignIn').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    objOrder.renderProfileTab();
                } else {
                    alert(arrResponse[1]);
                }
            });
        } else {
            alert('Please provide your USERNAME and PASSWORD before trying to sign in. Otherwise, we have no idea who you are really you!');
        }
    },
    registerUser: function () {
        vError = '';
        if ($('#frmRegister input[name=fldFirstName]').val() == '') { vError = vError + '- Missing First Name\n'; }
        if ($('#frmRegister input[name=fldLastName]').val() == '') { vError = vError + '- Missing Last Name\n'; }
        if ($('#frmRegister input[name=fldEmail]').val() == '') { vError = vError + '- Missing E-mail Address\n'; }
        if ($('#frmRegister input[name=fldPhone]').val() == '') { vError = vError + '- Missing Phone Number\n'; }
        if ($('#frmRegister input[name=fldUsername]').val() == '') { vError = vError + '- Missing Username\n'; }
        if ($('#frmRegister input[name=fldPassword]').val() == '') { vError = vError + '- Missing Password\n'; }
        if ($('#frmRegister input[name=fldPasswordConfirm]').val() == '') { vError = vError + '- Missing Password Confirmation\n'; }
        if ($('#frmRegister input[name=fldPasswordConfirm]').val() != $('#frmRegister input[name=fldPassword]').val()) { vError = vError + '- Password and Password Confirmation Do Not Match\n'; }
        if (vError == '') {
            $.post('/lib/services/orderService.asp', $('#frmRegister').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    objOrder.renderProfileTab();
                } else {
                    alert(arrResponse[1]);
                }
            });
        } else {
            alert('We\'re missing a few things before we can process your new account:\n\n' + vError);
        }
    },
    listPossibleClients: function () {
        $.get('/lib/services/orderService.asp', { fldAction: 'listPossibleClients' }, function (vResponse) {
            $('#divPossibleClients').html(vResponse);
        });
    },
    retrieveClient: function (vClientID, vEmail) {
        $('#frmClient input[name=fldClientID]').val(vClientID);
        $('#frmClient input[name=fldEmail]').val(vEmail);
        objOrder.findClient();
    },
    findClient: function () {
        if ($('#frmClient input[name=fldClientID]').val() != '') {
            $.get('/lib/services/orderService.asp', { fldAction: 'findClient', fldClientID: $('#frmClient input[name=fldClientID]').val(), fldEmail: $('#frmClient input[name=fldEmail]').val() }, function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    if ($('#frmClient input[name=fldEmail]').val() == arrResponse[8]) {
                        $('#frmClient input[name=fldName]').val(arrResponse[1]);
                        $('#frmClient input[name=fldAddress]').val(arrResponse[2]);
                        $('#frmClient input[name=fldCity]').val(arrResponse[3]);
                        $('#frmClient input[name=fldState]').val(arrResponse[4]);
                        $('#frmClient input[name=fldZip]').val(arrResponse[5]);
                        $('#frmClient input[name=fldCountry]').val(arrResponse[6]);
                        $('#frmClient input[name=fldContact]').val(arrResponse[7]);
                        $('#frmClient input[name=fldEmail]').val(arrResponse[8]);
                        $('#frmClient input[name=fldPhone]').val(arrResponse[9]);
                        $('#frmClient input[name=fldFax]').val(arrResponse[10]);
                        $('#frmClient input[name=fldWebsite]').val(arrResponse[11]);
                        $('#frmClient input[name=fldFetched]').val('1');
                        objOrder.enableClientRegistration();
                    } else {
                        $('#frmClient input[name=fldEmail]').css('background-color', '#feffcc');
                        alert('In order to find your information in our system, please type your e-mail address exactly as we have it in our system then click FIND ME again.');
                    }
                } else {
                    $('#frmClient input[name=fldEmail]').css('background-color', '#feffcc');
                    $('#frmClient input[name=fldEmail]').focus();
                    alert(arrResponse[1]);
                }
            });
        } else {
            alert('We need a Client ID to find you in our system. If you don\'t have one, don\'t worry. We\'ll create a new one and give it to you before the next step.');
        }
    },
    enableClientRegistration: function () {
        vError = '';
        if ($('#frmClient input[name=fldName]').val() == '') { vError = vError + '- Organization Name\n' }
        if ($('#frmClient input[name=fldAddress]').val() == '') { vError = vError + '- Address\n' }
        if ($('#frmClient input[name=fldCity]').val() == '') { vError = vError + '- City\n' }
        if ($('#frmClient input[name=fldState]').val() == '') { vError = vError + '- State/Province\n' }
        if ($('#frmClient input[name=fldZip]').val() == '') { vError = vError + '- Zip/Postal Code\n' }
        if ($('#frmClient input[name=fldCountry]').val() == '') { vError = vError + '- Country\n' }
        if ($('#frmClient input[name=fldContact]').val() == '') { vError = vError + '- Contact Name\n' }
        if ($('#frmClient input[name=fldEmail]').val() == '') { vError = vError + '- E-mail Address\n' }
        if ($('#frmClient input[name=fldPhone]').val() == '') { vError = vError + '- Phone Number\n' }
        if (vError == '') {
            $('#frmClient input[name=butAddClient]').attr('disabled', false)
        } else {
            $('#frmClient input[name=butAddClient]').attr('disabled', true)
        }
    },
    registerClient: function () {
        vError = '';
        if ($('#frmClient input[name=fldName]').val() == '') { vError = vError + '- Organization Name\n' }
        if ($('#frmClient input[name=fldAddress]').val() == '') { vError = vError + '- Address\n' }
        if ($('#frmClient input[name=fldCity]').val() == '') { vError = vError + '- City\n' }
        if ($('#frmClient input[name=fldState]').val() == '') { vError = vError + '- State/Province\n' }
        if ($('#frmClient input[name=fldZip]').val() == '') { vError = vError + '- Zip/Postal Code\n' }
        if ($('#frmClient input[name=fldCountry]').val() == '') { vError = vError + '- Country\n' }
        if ($('#frmClient input[name=fldContact]').val() == '') { vError = vError + '- Contact Name\n' }
        if ($('#frmClient input[name=fldEmail]').val() == '') { vError = vError + '- E-mail Address\n' }
        if ($('#frmClient input[name=fldPhone]').val() == '') { vError = vError + '- Phone Number\n' }
        if (vError == '') {
            $.post('/lib/services/orderService.asp', $('#frmClient').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#frmClient input[name=fldClientID]').val(arrResponse[2]);
                    $('#frmPaymentMethod input[name=fldClientID]').val(arrResponse[2]);
                    $('#frmClient input[name=fldFetched]').val('1');
                    objOrder.changeTabs('3');
                } else {
                    alert(arrResponse[1]);
                }
            });
        } else {
            alert('We need the following information before we can continue with your website order:\n\n' + vError);
        }
    },
    resetDomain: function () {
        $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('disabled', true);
        $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('checked', false);
        $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('disabled', true);
        $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('checked', false);
        $('#frmDomainName input[name=fldDomainAction][value=DN2]').attr('disabled', true);
        $('#frmDomainName input[name=fldDomainAction][value=DN2]').attr('checked', false);
        $('#frmDomainName input[name=fldDomainAction][value=DN5]').attr('disabled', true);
        $('#frmDomainName input[name=fldDomainAction][value=DN5]').attr('checked', false);
        $('#frmDomainName input[name=fldDomainOwned]').attr('disabled', true);
        $('#frmDomainName input[name=fldDomainOwned]').attr('checked', false);
        $('#frmDomainName input[name=butDomainName]').attr('disabled', true);
        $('#divDomainCheckResults').slideUp('fast');
        $('#divDomainCheckResults').html('');
    },
    checkDomain: function () {
        $.get('/lib/services/orderService.asp', { fldAction: 'checkDomain', fldDomainName: $('#frmDomainName input[name=fldDomainName]').val() }, function (vResponse) {
            arrResponse = vResponse.split('||');
            if (arrResponse[0] == '1') {
                if (arrResponse[1] == 'available') {
                    $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('disabled', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('checked', true);
                    $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('disabled', true);
                    $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('checked', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN2]').attr('disabled', true);
                    $('#frmDomainName input[name=fldDomainAction][value=DN2]').attr('checked', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN5]').attr('disabled', true);
                    $('#frmDomainName input[name=fldDomainAction][value=DN5]').attr('checked', false);
                    $('#divDomainCheckResults').html('This Domain is Available');
                    $('#divDomainCheckResults').css('color', '#009900');
                    $('#divDomainCheckResults').slideDown('fast');
                } else {
                    $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('disabled', true);
                    $('#frmDomainName input[name=fldDomainAction][value=DN1]').attr('checked', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('disabled', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN2]').attr('disabled', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN5]').attr('disabled', false);
                    $('#frmDomainName input[name=fldDomainAction][value=DN3]').attr('checked', true);
                    $('#frmDomainName input[name=fldDomainOwned]').attr('disabled', false);
                    $('#frmDomainName input[name=fldDomainOwned]').attr('checked', true);
                    $('#divDomainCheckResults').html('This Domain is Registered');
                    $('#divDomainCheckResults').css('color', '#990000');
                    $('#divDomainCheckResults').slideDown('fast');
                }
                $('#frmDomainName input[name=butDomainName]').attr('disabled', false);
            } else {
                alert(arrResponse[0] + ' ' + arrResponse[1]);
            }
        });
    },
    setDomain: function () {
        $('#frmPaymentMethod input[name=fldDomainName]').val($('#frmDomainName input[name=fldDomainName]').val());
        $('#frmPaymentMethod input[name=fldDomainAction]').val($('#frmDomainName input[name=fldDomainAction]:checked').val());
        objOrder.displayTotal();
        objOrder.changeTabs(4);
    },
    setPackageID: function (vPackageID) {
        $('#divOrderStep4 input[type=button]').attr('disabled', false)
        $('#divOrderStep4 input[type=button]').attr('value', 'Choose This')
        $('#divOrderStep4 input[type=button][name=but' + vPackageID + ']').attr('disabled', true)
        $('#divOrderStep4 input[type=button][name=but' + vPackageID + ']').attr('value', 'CHOSEN')
        $('#frmPaymentMethod input[name=fldPackageID]').val(vPackageID);
        objOrder.displayTotal();
        objOrder.changeTabs('5');
    },
    setPlanID: function (vPlanID) {
        $('#divOrderStep5 input[type=button]').attr('disabled', false)
        $('#divOrderStep5 input[type=button]').attr('value', 'Choose This')
        $('#divOrderStep5 input[type=button][name=but' + vPlanID + ']').attr('disabled', true)
        $('#divOrderStep5 input[type=button][name=but' + vPlanID + ']').attr('value', 'CHOSEN')
        $('#frmPaymentMethod input[name=fldPlanID]').val(vPlanID);
        objOrder.displayTotal();
        objOrder.changeTabs('6');
    },
    submitOrder: function () {
        vError = '';
        if ($('#frmPaymentMethod input[name=fldPaymentMethod]:checked').val() == 'Credit Card') {
            if ($('#frmPaymentMethod input[name=fldCardNumber]').val() == '') { vError = vError + '- Credit Card Number\n'; }
            if ($('#frmPaymentMethod input[name=fldExpDate]').val() == '') { vError = vError + '- Expiration Date\n'; }
            if ($('#frmPaymentMethod input[name=fldCardCode]').val() == '') { vError = vError + '- CVV Code\n'; }
            if ($('#frmPaymentMethod input[name=fldPaymentCreditCardOK]')[0].checked == false) { vError = vError + '- Please agree to the billing terms\n'; }
        } else {
            if ($('#frmPaymentMethod input[name=fldBankName]').val() == '') { vError = vError + '- Bank Name\n'; }
            if ($('#frmPaymentMethod input[name=fldAccountNumber]').val() == '') { vError = vError + '- Account Number\n'; }
            if ($('#frmPaymentMethod input[name=fldRoutingNumber]').val() == '') { vError = vError + '- Routing Number\n'; }
            if ($('#frmPaymentMethod input[name=fldPaymentCheckDraftOK]')[0].checked == false) { vError = vError + '- Please agree to the billing terms\n'; }
        }
        if ($('#frmPaymentMethod input[name=fldAccountName]').val() == '') { vError = vError + '- Account Holder\s Name\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountAddress]').val() == '') { vError = vError + '- Billing Address\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountCity]').val() == '') { vError = vError + '- Billing City\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountState]').val() == '') { vError = vError + '- Billing State\Province\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountZip]').val() == '') { vError = vError + '- Billing Zip/Postal Code\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountCountry]').val() == '') { vError = vError + '- Billing Country\n'; }
        if ($('#frmPaymentMethod input[name=fldAccountPhone]').val() == '') { vError = vError + '- Billing Phone Number\n'; }
        if ($('#frmPaymentMethod input[name=fldPaymentFinalOK]')[0].checked == false) { vError = vError + '- Please agree to the project and refund terms\n'; }

        if (vError == '') {
            //$('#butCompleteOrder').attr('disabled',true);
            $.post('/lib/services/orderService.asp', $('#frmPaymentMethod').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    for (a = 0; a <= 6; a++) {
                        $('#divOrderTab' + a).slideUp('fast');
                    }
                    objOrder.changeTabs('7');
                    $('html, body').animate({ scrollTop: 0 }, 'fast');
                    $('#divOrderConfirmation').html(arrResponse[1]);
                    $('#butCompleteOrder').attr('disabled', false);
                } else {
                    $('#butCompleteOrder').attr('disabled', false);
                    alert(arrResponse[1]);
                }
            });
        } else {
            $('#butCompleteOrder').attr('disabled', false);
            alert('The follow items must be entered before submitting your order:\n\n' + vError);
        }
    },
    displayTotal: function () {
        $.get('/lib/services/orderService.asp', {
            fldAction: 'displayTotal',
            fldDomainAction: $('#frmPaymentMethod input[name=fldDomainAction]').val(),
            fldPackageID: $('#frmPaymentMethod input[name=fldPackageID]').val(),
            fldPlanID: $('#frmPaymentMethod input[name=fldPlanID]').val()
        }, function (vResponse) {
            $('#divOrderTotalPrice').html(vResponse);
        })
    }

}

var objMarketing = {

    getLeadForm: function (vSource,vBonus) {
        $.get('/lib/services/marketingService.asp', { fldAction: 'getLeadForm', fldSource: vSource, fldBonus: vBonus }, function (vResponse) {
            $('#divLeadFormArea').html(vResponse);
        });
    },

    submitLeadForm: function () {
        var objDate = new Date();
        vError = '';
        if ($('#frmLead input[name=fldContact]').val() == '') { vError = vError + '- Your Name\n'; }
        if ($('#frmLead input[name=fldName]').val() == '') { vError = vError + '- Organization/CompanyName\n'; }
        if ($('#frmLead input[name=fldEmail]').val() == '') { vError = vError + '- E-mail Address\n'; }
        if (objUtils.validateEmail($('#frmLead input[name=fldEmail]').val()) == false) { vError = vError + '- Invalid E-mail Address\n'; }
        if ($('#frmLead input[name=fldPhone]').val() == '') { vError = vError + '- Phone Number\n'; }
        if (objDate.getTimezoneOffset() < 240 || objDate.getTimezoneOffset() > 420) { vError = vError + 'We do not currently accept inquiries outside of North America. However, thank you for showing your interest.\n'; }
        if (vError == '') {
            $('#frmLead input[name=fldTimeZone]').val(objUtils.convertToTimeZone(objDate.getTimezoneOffset()));
            $.post('/lib/services/marketingService.asp', $('#frmLead').serialize(), function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divLeadForm').html(arrResponse[1]);
                } else {
                    alert(arrResponse[1]);
                }
            });
        } else {
                alert('Please complete the form before trying to submit it. The following items were missing:\n\n' + vError);
        }
    }

}

var objUtils = {

    convertToTimeZone: function (vTimeOffset) {
        switch (vTimeOffset) {
            case 240:
                return 'EST';
                break;
            case 300:
                return 'CST';
                break;
            case 360:
                return 'MST';
                break;
            case 420:
                return 'PST';
                break;
            default:
                return ('GMT ' + (0 - vTimeOffset / 60));
                break;
        }

    },

    validateEmail: function (vEmail) {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        return (reg.test(vEmail));
    }

}

$.ajaxSetup({
    error: function (x, e) {
        if (x.status == 0) {
            alert('You are offline!!\n Please Check Your Network.');
        } else if (x.status == 404) {
            alert('Requested URL not found.');
        } else if (x.status == 500) {
            alert('Internal Server Error. ' + x.responseText);
        } else if (e == 'parsererror') {
            alert('Error.\nParsing JSON Request failed.');
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }
});

