//

//----------------------------------------------------------------------------
// GENERAL LOADING
//----------------------------------------------------------------------------
function mailtoGroupListLoad () {
    $.ajax({
        url: jarvis_base + 'group_list', dataType: 'json', type: 'GET', error: getError,        
        success: function (result){
            $.each (result.data, function (i, item) {
                var group_name = item.GroupDesc;
                $('#mailto_group').append('<option value="' + item.GroupID + '">' + group_name + '</option>');
            });             
            $('#mailto_group').removeAttr('disabled');
        }
    });
}

function loginInfoLoad () {
    $.ajax({
        url: jarvis_base + 'my_account', dataType: 'json', type: 'GET', error: getError,        
        contentType: "application/json; charset=utf-8",
        success: function (result){
            if (result.data.length >= 1) {
                account = result.data[0];
                $('#mailto_first_name').val (account.FirstName);
                $('#mailto_last_name').val (account.LastName);
                $('#mailto_email').val (account.Email);
                $('#mailto_phone_day').val (account.PhoneNumDay);
                $('#mailto_phone_evening').val (account.PhoneNumEvening);
                $('#mailto_phone_other').val (account.PhoneNumOther);
                $('#mailto_group').val (account.GroupID);
            }
        }
    });
}

function messageLoad (options) {
    var educatorID = (options && options.educator) ? options.educator : 0;
    var familyID = (options && options.family) ? options.family : 0;
    if (educatorID != 0) {
        $.ajax({
            url: jarvis_base + 'educator_details?id=' + educatorID, dataType: 'json', type: 'GET', error: getError,
            success: function (result){
                if (result.data.length >= 1) {
                    var educator = result.data[0];
                    $('#mailto_body').val ("Hi,\n\nI would like to enquire regarding the looking-for-work advertisement by educator " + educator.Name + " (PorseID " + educator.PorseID + ").\n\n...");
                    $('#mailto_group').val (educator.FranchiseGroupID);
                }
                else {
                    alert("Error: Educator not found!");
                }
            }            
        });
    } else if (familyID != 0) {
        $.ajax({
            url: jarvis_base + 'family_details?id=' + familyID, dataType: 'json', type: 'GET', error: getError,
            success: function (result){
                if (result.data.length >= 1) {
                    var family = result.data[0];
                    $('#mailto_body').val ("Hi,\n\nI would like to enquire regarding the looking-for-childcare advertisement by family " + family.Name + " (Ref " + family.PorseID + ").\n\n...");
                    $('#mailto_group').val (family.FranchiseGroupID);
                }
                else {
                    alert("Error: Family not found!");
                }
            }            
        });
    } else
        $('#mailto_body').val('');
}

//----------------------------------------------------------------------------
// INTERACTION
//----------------------------------------------------------------------------
function doMailTo () {
    
    var first_name = $('#mailto_first_name').val() || '';
    first_name = first_name.replace (/^\s+/, '').replace (/\s+$/, '');
    if (first_name.length == 0) {
        alert ('First Name must be specified.');
        return;
    }
    var last_name = $('#mailto_last_name').val() || '';
    last_name = last_name.replace (/^\s+/, '').replace (/\s+$/, '');
    if (last_name.length == 0) {
        alert ('Last Name must be specified.');
        return;
    }
    var email = $('#mailto_email').val() || '';
    email = email.replace (/^\s+/, '').replace (/\s+$/, '');
    if (email.length == 0) {
        alert ('eMail must be specified.');
        return;
    }
    if (! email_valid_re.test(email)) {
        alert ('eMail address does not have valid format.');
        return;
    }
    
    var group_id = $('#mailto_group').val();
    if ((group_id == '') || (group_id == undefined)) {
        alert ('Please specify a PORSE Area to contact.');
        return;
    }

    var day_phone_num = $('#mailto_phonenum_day').val();
    var evening_phone_num = $('#mailto_phonenum_evening').val();
    var other_phone_num = $('#mailto_phonenum_other').val();   

    var body = $('#mailto_body').val() || '';
    body = body.replace (/^\s+/, '').replace (/\s+$/, '');
    if (body.length == 0) {
        alert ('You forgot to write the body of your message!');
        return;
    }
    
    var params = {
        first_name: first_name,
        last_name: last_name,
        email: email,
        group_id: group_id,
        day_phone_num: day_phone_num,
        evening_phone_num: evening_phone_num,
        other_phone_num: other_phone_num,
        body: body
    }
    $.ajax({
        url: jarvis_base + "MailTo", dataType: "json", type: "POST",  error: postError,
        contentType: "application/json; charset=utf-8", data: JSON.stringify (params),
        success: function (result){
            if (! postSuccess (this, result)) { return };
            if (result.sent) {
                alert ('Your message has been sent.  Somebody will be in touch soon.');

            } else {
                alert ('Mail Sending Failed: ' + result.reason);
            }
        }
    });
}

/**
 * Standard modal dialog configuration for porse-public
 */
var defaultModalConfiguration = {
      opacity:80
    , overlayCss: {
        backgroundColor:"#000"
    }
    , containerCss:{
        backgroundColor: "#fff",
        border: "solid 4px #bf6a30",
        'border-radius': '15px',
        padding: 10,
        width:500
    }
    , onOpen: function (dialog) {
        dialog.overlay.fadeIn('fast');
        dialog.container.show();
        dialog.data.show();
        // This forces the container to resize when we add info div's to the popup.
        $('#simplemodal-container').css("height", "auto");
    }
}

function openMail(options) {
    $('#mailto_div').modal(defaultModalConfiguration);
    messageLoad(options);
}

//----------------------------------------------------------------------------
// PAGE INITIALISATION PROCESS
//----------------------------------------------------------------------------
$(document).ready(function() {
    loginInfoLoad ();
    mailtoGroupListLoad ();
});
