$.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);
        }
    }
});

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objTopPanel = {

    open: function() {
        $('#divTopPanel').toggle('fast', function() { $('#frmSignIn input[name=fldLoginEmail]').focus(); });
    },

    validate: function() {

        $.get('/lib/ajax/authenticate_ajax.asp',
            {
                callMethod: 'validate'

            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                $('#divTopBar').html(arrResponse[1]);
                $('#divTopPanel').html(arrResponse[2]);
            }
        );

        $.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);
                }
            }
        });

    },

    authenticate: function() {

        var vError = '';
        if ($('#frmSignIn input[name=fldLoginEmail]').val() == '') { vError = vError + '- E-mail Address Missing\n'; }
        if ($('#frmSignIn input[name=fldLoginPassword]').val() == '') { vError = vError + '- Password Missing\n'; }

        if (vError == '') {

            $.get('/lib/ajax/authenticate_ajax.asp', $('#frmSignIn').serialize(),
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        location.href = 'https://www.dayshout.com/dashboard.asp';
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );

        } else {
            alert('The following errors occurred while signing you in:\n\n' + vError);
        }

    },

    signOut: function() {

        $.get('/lib/ajax/authenticate_ajax.asp',
            {
                callMethod: 'signOut'
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    location.href = 'http://www.dayshout.com';
                } else {
                    alert(arrResponse[1]);
                }
            }
        );

    },
    
    autoSignIn: function(vToken) {

        $.get('/lib/ajax/authenticate_ajax.asp',
            {
                callMethod: 'autoSignIn',
                token: vToken
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    location.href = 'https://www.dayshout.com/dashboard.asp';
                } else {
                    objTopPanel.open();
                    alert(arrResponse[1]);
                }
            }
        );

    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objSignUp = {

    render: function() {

        $.get('/lib/ajax/signUp_ajax.asp',
            {
                callMethod: 'renderSignUp'
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                $('#divSignUpForm').html(arrResponse[1]);
                $('#frmSignUp input[name=fldFirstName]').focus();
            }
        );

    },

    process: function() {

        vError = '';
        if ($('#frmSignUp input[name=fldFirstName]').val() == '') { vError = vError + '- First Name Missing\n'; }
        if ($('#frmSignUp input[name=fldLastName]').val() == '') { vError = vError + '- Last Name Missing\n'; }
        if ($('#frmSignUp input[name=fldEmail]').val() == '') { vError = vError + '- E-mail Address Missing\n'; }
        if ($('#frmSignUp input[name=fldPassword]').val() == '') { vError = vError + '- Password Missing\n'; }
        if ($('#frmSignUp input[name=fldPassword2]').val() == '') { vError = vError + '- Retyped Password Missing\n'; }
        if ($('#frmSignUp input[name=fldPassword2]').val() != $('#frmSignUp input[name=fldPassword]').val()) { vError = vError + '- Password Do Not Match\n'; }

        if (vError == '') {
            $.post('/lib/ajax/signup_ajax.asp', $('#frmSignUp').serialize(),
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divSecondaryMain').html(arrResponse[1]);
                } else {
                    alert(arrResponse[1]);
                }
            }
            );
        } else {
            alert('Please correct the following information:\n\n' + vError);
        }

    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objActivation = {

    process: function(vConfirmation) {

        $.get('/lib/ajax/signUp_ajax.asp',
                {
                    callMethod: 'activate',
                    confirmation: vConfirmation
                },
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        $('#divSecondaryMain').html(arrResponse[1]);
                        //objTopPanel.open();
                        setTimeout('location.href = "/dashboard.asp"',5000)
                    } else {
                        $('#divSecondaryMain').html(arrResponse[1]);
                    }
                }
        );

    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objInfoForm = {

    update: function() {
        vError = '';
        if ($('#frmInfo input[name=fldFirstName]').val() == '') { vError = vError + '- Missing First Name\n'; }
        if ($('#frmInfo input[name=fldLastName]').val() == '') { vError = vError + '- Missing Last Name\n'; }
        if ($('#frmInfo input[name=fldEmail]').val() == '') { vError = vError + '- Missing E-mail Address\n'; }
        if ($('#frmInfo input[name=fldPassword]').val() == '') { vError = vError + '- Missing Password\n'; }
        if ($('#frmInfo input[name=fldPassword2]').val() == '') { vError = vError + '- Missing Retyped Password\n'; }
        if ($('#frmInfo input[name=fldPassword]').val() != $('#frmInfo input[name=fldPassword2]').val()) { vError = vError + '- Passwords Do Not Match\n'; }

        if (vError == '') {

            $.post('/lib/ajax/signup_ajax.asp', $('#frmInfo').serialize(),
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        $('#divSuccessMessage').html('Your information has been successfully updated. Back to <a href="/dashboard.asp">Dashboard</a>?');
                        $('#divSuccessMessage').slideUp('fast', function() { $('#divSuccessMessage').slideDown('fast') });
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        } else {
            alert('The following errors were found when trying to update your account:\n\n' + vError);
        }
    },
    
    submitDowngrade: function() {
        if (confirm('Are you sure you want to downgrade to our free service? This will limit your ability to monitor more than two friends and family members.')) {
            $.get('/lib/ajax/signup_ajax.asp','callMethod=submitDowngrade',
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        location.reload();
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            )
        }
    },
    
    openUpgrade: function(vPassToken,vUserID) {
        $('#divServiceLevel').css('text-align','center');
        $('#divServiceLevel').html('Opening Window for Upgrade. <a href=#noref style=color:#880000;>Click Here to Refresh</a>');
        window.open('https://apps.gomezinnovations.com/apps/Service%20Upgrades/?t='+vPassToken+'&from=DS001&to=DS002&userid='+vUserID+'','appServiceUpgrade','width=600px,height=700');    
        $('#divServiceLevel').click(function() {window.location.reload(); });
    },

    openPaymentUpdate: function(vPassToken, vUserID, vBillingID) {
        $('#divServiceLevelOptions').toggle('fast');
        $('#divServiceLevel').css('text-align', 'center');
        //$('#divServiceLevel').html('Opening Window for Upgrade. <a href=#noref style=color:#880000;>Click Here to Refresh</a>');
        window.open('https://apps.gomezinnovations.com/apps/Credit%20Card%20Update/?t=' + vPassToken + '&methodid=' + vUserID + '&billingid='+vBillingID, 'appServiceUpgrade', 'width=600px,height=700');
        //$('#divServiceLevel').click(function() { window.location.reload(); });
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objDayShout = {

    showToday: function (vMode) {
        $.get('/lib/ajax/dayshout_ajax.asp',
            {
                callMethod: 'showToday',
                mode: vMode
            },
            function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divDayShout').fadeOut('fast', function () { $('#divDayShout').html(arrResponse[1]); $('#divDayShout').fadeIn('fast') });
                } else {
                    $('#divDayShout').html(arrResponse[1]);
                }
            }
        );
    },

    submitToday: function (vMode) {
        if ((vMode == 'Web' && $('#frmDayShout input[name=fldPhysical]:checked').length == 1 && $('#frmDayShout input[name=fldEmotional]:checked').length == 1 && $('#frmDayShout input[name=fldSpiritual]:checked').length == 1) || (vMode == 'Mobile' && $('#frmDayShout input[name=fldPhysical]').val() != '0' && $('#frmDayShout input[name=fldEmotional]').val() != '0' && $('#frmDayShout input[name=fldSpiritual]').val() != '0')) {
            $.post('/lib/ajax/dayshout_ajax.asp', $('#frmDayShout').serialize(),
                function (vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        if (arrResponse[2] == 'mobile') {
                            location.href = '/dashboard.asp';
                        } else {
                            objDayShout.showToday();
                        }
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        } else {
                alert('Before submitting your DayShout, please indicate your physical, emotional, and spiritual status.' + vMode + $('#frmDayShout input[name=fldPhysical]').val() + $('#frmDayShout input[name=fldEmotional]').val() + $('#frmDayShout input[name=fldSpiritual').val());
        }
    },

    showFriends: function () {
        $.get('/lib/ajax/dayshout_ajax.asp',
            {
                callMethod: 'showFriends'
            },
            function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divFriends').html(arrResponse[1]);
                } else {
                    $('#divFriends').html(arrResponse[1]);
                }
            }
        );
    },

    showTrends: function (vUserID, vPeriod) {
        $.get('/lib/ajax/dayshout_ajax.asp',
            {
                callMethod: 'showTrends',
                userID: vUserID,
                period: vPeriod
            },
            function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divTrends').html(arrResponse[1]);
                } else {
                    $('#divTrends').html(arrResponse[1]);
                }
            }
        );
    },

    toggleCommentField: function (vDayShoutID) {
        if ($('#divComment' + vDayShoutID).css('display') == 'none') {
            $('#divCommentField' + vDayShoutID).slideUp('fast', function () { $('#divComment' + vDayShoutID).slideDown('fast') });
        } else {
            $('#divComment' + vDayShoutID).slideUp('fast', function () { $('#divCommentField' + vDayShoutID).slideDown('fast') });
        }
    },

    addComment: function (vDayShoutID) {
        $.post('/lib/ajax/dayshout_ajax.asp', $('#frmDayShoutComment' + vDayShoutID).serialize(),
            function (vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divCommentField' + vDayShoutID).slideUp('fast', function () { $('#divCommentField' + vDayShoutID).html('Comment Sent!'); $('#divCommentField' + vDayShoutID).slideDown('fast') });
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    deleteAccount: function () {
        if (confirm('Are you ABSOLUTELY sure you want to delete your account, all history, referral commissions, and future emails from DayShout.Com?')) {
            $.get('/lib/ajax/dayshout_ajax.asp',
                {
                    callMethod: 'deleteAccount'
                },
                function (vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        location.href = '/';
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        }
    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objFriends = {

    showAddFriend: function() {
        $.get('/lib/ajax/dayshout_ajax.asp',
            {
                callMethod: 'showAddFriend'
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divAddFriend').html(arrResponse[1]);
                    $('#frmAddFriend input[name=fldFirstName]').focus();
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    showFriendList: function() {
        $.get('/lib/ajax/dayshout_ajax.asp',
            {
                callMethod: 'showFriendList'
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divFriends').html(arrResponse[1]);
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    processAddFriend: function() {
        vError = '';
        if ($('#frmAddFriend input[name=fldFirstName]').val() == '') { vError = vError + '- Missing Friend First Name\n'; }
        if ($('#frmAddFriend input[name=fldLastName]').val() == '') { vError = vError + '- Missing Friend Last Name\n'; }
        if ($('#frmAddFriend input[name=fldEmail]').val() == '') { vError = vError + '- Missing Friend E-mail Address\n'; }

        if (vError == '') {
            if (confirm('Are you sure you want to add this friend? If so, an e-mail notification will automatically be sent to them.')) {
                $.post('/lib/ajax/dayshout_ajax.asp', $('#frmAddFriend').serialize(),
                    function(vResponse) {
                        arrResponse = vResponse.split('||');
                        if (arrResponse[0] == '1') {
                            $('#divAddFriend').html(arrResponse[1]);
                            $('#divAddFriend').slideUp('fast', function() { $('#divAddFriend').slideDown('fast') });
                            objFriends.showFriendList();
                            $('#divFriends').slideUp('fast', function() { $('#divFriends').slideDown('fast') });
                        } else {
                            alert(arrResponse[1]);
                        }
                    }
                );
            }
        } else {
            alert('The following errors were found when trying to invite your friend:\n\n' + vError);
        }
    },
    
    processFriendRequest: function(vAction,vUserID) {
        if (confirm('Are you sure you want to '+vAction+' this friend?')) {
            $.get('/lib/ajax/dayshout_ajax.asp',
                {
                    callMethod: 'processFriendRequest',
                    action: vAction,
                    userid: vUserID
                },
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        if (arrResponse[2] != 'remove') {
                            objDayShout.showFriends();
                        } else {
                            objFriends.showFriendList();
                        }
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        }
    }

}

var objUtils = {
    
    renderEM : function(vAcct) {
        document.write('<a href="mailto:'+vAcct+'@'+'day'+'shout.'+'com'+'">'+vAcct+'@'+'day'+'shout.'+'com'+'</a>');
    },
    
	calculateFee : function() {
		$('#fldFee').html( ($('#fldLevel1').val() * 2) + ($('#fldLevel1').val() * $('#fldLevel2').val() * 1) + ($('#fldLevel1').val() * $('#fldLevel2').val() * $('#fldLevel3').val() * 1) + ($('#fldLevel1').val() * $('#fldLevel2').val() * $('#fldLevel3').val() * $('#fldLevel4').val() * 1) );
	},
	
	restrictNumeric : function(objField) {
	    var numeric = /^[0-9]*$/; 
	    if (!numeric.test(objField.value)) {
	        objField.value = objField.value.substring(0,objField.value.length-1);
	    }
	}
    
}
