﻿
/********************* Login by social network *********************/

function SigninWithFacebookAccount() {
    window.location = '/home/FacebookUserLogin';
}

function FacebookUserLogoff() {
    FB.Connect.logout(
        function() {
            window.location = '/home/FacebookUserLogoff';
        });
}

/********************* End login by social network *********************/

function registerProfile() {

    var emailAddress = $('#txtEmailAddress').attr('value');

    if (emailAddress != '') {

        var chkAgree = document.getElementById('chkAgree');
        if (chkAgree.checked) {

            var firstName = $('#hidFirstName').attr('value');
            var lastName = $('#hidLastName').attr('value');
            var phoneNumber = $('#txtPhoneNumber').attr('value');
            var address1 = $('#txtAddress1').attr('value');
            var address2 = $('#txtAddress2').attr('value');
            var postCode = $('#txtPostCode').attr('value');
            var state = $('#txtState').attr('value');
            var country = $('#cmbCountries').attr('value');

            Sys.Net.WebServiceProxy.invoke("/Services/CheekyCheckupServices.asmx", "RegisterProfile", false, { firstName: firstName, lastName: lastName, emailAddress: emailAddress, mobileNumber: phoneNumber, address1: address1, address2: address2, postCode: postCode, state: state, country: country },
                function(result) {
                    if (result) {
                        if (result == 1) {
                            $('#dlgProfile').dialog('close');
                        }
                        else if (result == 2) {
                            alert('Please input valid email address');
                        }
                        else {
                        }
                    }
                }, null, null);

        }
        else {
            alert('You must agree Terms of Conditions');
        }
    }
    else {
        alert('Please input email address');
    }
}

function saveAnswer() {
    var chkSendToFacebook = document.getElementById('chkSendToFacebook');
    var chkSendToFriends = document.getElementById('chkSendToFriends');

    var answerString = '';
    for (var i = 1; i <= 3; i++) {
        var rdo = document.getElementById('rdo' + i);
        if (rdo.checked) {
            answerString = rdo.value;
            break;
        }
    }

    var loginUser = $('#hidLoginUser').attr('value');
    if (loginUser == "2") {
        if (chkSendToFacebook.checked || chkSendToFriends.checked) {
            FB.Connect.showPermissionDialog('publish_stream,offline_access,email',
                                    function(userAllowPerms) {
                                        if (userAllowPerms) { //user has allowed publish access permission                                            
                                            try {
                                                Sys.Net.WebServiceProxy.invoke("/Services/CheekyCheckupServices.asmx", "SaveAnswer", false, { question: 'How often do you want us to send you a Cheeky Check-up nudge?', answerString: answerString, sendFacebook: chkSendToFacebook.checked, sendFriends: chkSendToFriends.checked },
                                                        function(result) {
                                                            if (result) {
                                                                if (result == 1) {
                                                                    alert('Thank you. Your answer is successfully saved.');
                                                                } else if (result == -2) {
                                                                    alert('Fail to save your answer');
                                                                }
                                                                else {
                                                                    alert('Please login before saving your answer');
                                                                    FB.Connect.requireSession(function() { SigninWithFacebookAccount(); });
                                                                }
                                                            }
                                                        }, null, null);
                                            } catch (err) {
                                                alert(err);
                                            }
                                        }
                                    }, false);
        } else {
            try {
                Sys.Net.WebServiceProxy.invoke("/Services/CheekyCheckupServices.asmx", "SaveAnswer", false, { question: 'How often do you want us to send you a Cheeky Check-up nudge?', answerString: answerString, sendFacebook: chkSendToFacebook.checked, sendFriends: chkSendToFriends.checked },
                    function(result) {
                        if (result) {
                            if (result == 1) {
                                alert('Thank you. Your answer is successfully saved.');
                            } else if (result == -2) {
                                alert('Fail to save your answer');
                            }
                            else {
                                alert('Please login before saving your answer');
                                FB.Connect.requireSession(function() { SigninWithFacebookAccount(); });
                            }
                        }
                    }, null, null);
            } catch (err) {
                alert(err);
            }
        }
    } else {
        //alert('Please login before saving your answer');
        FB.Connect.requireSession(function() { SigninWithFacebookAccount(); });
    }
}

function shareWithFriends() {

    var txtSenderName = $('#txtSenderName').attr('value');
    var txtSenderEmail = $('#txtSenderEmail').attr('value');
    var txtFriendEmails = $('#txtFriendEmails').attr('value');
    var txtContent = $('#txtContent').attr('value');

    if (txtSenderEmail == '') {
        alert('Please input sender email');
        return;
    }
    if (txtFriendEmails == '') {
        alert('Please input Friends\' emails');
        return;
    }

    Sys.Net.WebServiceProxy.invoke("/Services/CheekyCheckupServices.asmx", "SendEmailToFriends", false, { yourName: txtSenderName, yourMail: txtSenderEmail, yourFriends: txtFriendEmails, content: txtContent },
        function(result) {
            if (result) {
                if (result == 1) {
                    $('#dlgShareToFriends').dialog('close');
                }
                else if (result == -1) {
                    alert('Sender email is invalid');
                }
                else {
                    alert('Friends\' emails is invalid');
                }
            }
        }, null, null);
}
