/**
 *  Obviously has jQuery dependencies
 *
 *  The code below based on jquery-1.3.2
 */

var tafForm = new Object();

tafForm.reset = function () {
    jQuery("#taf_to").val("");
    jQuery("#taf_from_name").val("");
    jQuery("#taf_from_email").val("");
    jQuery("#taf_personal_message").val("");
    jQuery("#taf_to_tips").html("");
    jQuery("#taf_from_name_tips").html("");
    jQuery("#taf_email_tips").html("");
    jQuery("#taf_send_to_sender").attr('checked', true);

    delete this.regFirst;
    delete this.coRegFirst;
    delete this.registries;

    this.regFirst = "";
    this.coRegFirst = "";
    this.registries = "";

    jQuery("#taf_pre_post").show();
    jQuery("#taf_post_post").hide();
};

tafForm.validateIncomingRegistries = function(data) {
    return data.exception != "undefined";
};

tafForm.display = function (uid, listOfRegistries) {
    this.reset();

    this.regFirst = jQuery("#" + uid + "_regFirst").val();
    this.coRegFirst = jQuery("#" + uid + "_coRegFirst").val();

    var sampleMessage = "Check out " + this.regFirst + " and "+ this.coRegFirst + "'s wedding registries at:";
    var registriesString = "";

    // stich retailer names together for display
    for(i = 0; i < listOfRegistries.length; i++) {
        sampleMessage += ("<br/><strong><u><a target='_blank' href='" + listOfRegistries[i].registry.urls.guest + "'>" + listOfRegistries[i].registry.retailer + "</a></u></strong>");
        if(i > 0) {
            registriesString += "|";
        }
        // stich retailer and registries together for email content
        registriesString += listOfRegistries[i].registry.retailer + "$" + listOfRegistries[i].registry.urls.guest;
    }

    if(registriesString != "") {
        this.registries = escape(registriesString);
        jQuery("#taf_sample_message").html(sampleMessage);
        jQuery("#tell_a_friend_box").dialog("open");
        jQuery('.taf-dialog-class').css('left', (jQuery(window).width()-jQuery('.taf-dialog-class').width())/2);
    } else {    
        alert("This couple has no registries!");
    }
};

tafForm.validateForm = function() {
    var val = true;
    var toEmailRegEx = /^(\s*(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)(\s*[,;]?\s*))+$/;
    var fromEmailRegEx = /^\s*(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*$/;

    if(jQuery.trim(jQuery("#taf_to").val()) == "") {
        jQuery("#taf_to_tips").html("To email is required.");
        val = false;
    } else {
        if(toEmailRegEx.test(jQuery("#taf_to").val()) == false) {
            jQuery("#taf_to_tips").html("Please enter a valid email address.");
            val = false;
        } else {
            jQuery("#taf_to_tips").html("");
        }
    }

    if(jQuery.trim(jQuery("#taf_from_name").val()) == "") {
        jQuery("#taf_from_name_tips").html("Your name is required.");
        val = false;
    } else {
        jQuery("#taf_from_name_tips").html("");
    }

    if(jQuery.trim(jQuery("#taf_from_email").val()) == "") {
        jQuery("#taf_email_tips").html("Your email is required.");
        val = false;
    } else {
        if(fromEmailRegEx.test(jQuery("#taf_from_email").val()) == false) {
            jQuery("#taf_email_tips").html("Valid email is required.");
            val = false;
        } else {
            jQuery("#taf_email_tips").html("");
        }
    }

    return val;
};

tafForm.submit = function() {
    if(!this.validateForm()) {
        return false;
    }

    var paramMap = {};
    paramMap.toEmailAddresses = jQuery("#taf_to").val();
    paramMap.fromName = jQuery("#taf_from_name").val();
    paramMap.fromEmailAddress = jQuery("#taf_from_email").val();
    paramMap.registries = this.registries;
    paramMap.personalMessage = jQuery("#taf_personal_message").val();
    paramMap.sendToSender = jQuery("#taf_send_to_sender").attr('checked');
    paramMap.regName = this.regFirst;
    paramMap.coRegName = this.coRegFirst;

    jQuery.post( "/staf/coupleSearchResultStaf.action", paramMap);

    jQuery("#taf_pre_post").hide();
    jQuery("#taf_post_post").show();
};

var displayTaf = function(uid, multiReg) {
    var restfulUrl;
    var callBackFunction;

    if(multiReg) {
        restfulUrl = "http://api.weddingchannel.com/1.0/registries.json?o="+ uid + "&callback=?";
        callBackFunction = function(data) {
            if(tafForm.validateIncomingRegistries(data)) {
                tafForm.display(uid, data.registries);
            } else {
                alert("Oops! Error processing registries!");
            }
        };
    } else {
        restfulUrl = "http://api.weddingchannel.com/1.0/registries/"+ uid + ".json?callback=?";
        callBackFunction = function(data) {
            if(tafForm.validateIncomingRegistries(data)) {
                var _data = new Array();
                _data[0] = data;
                tafForm.display(uid, _data);
            } else {
                alert("Oops! Error processing registries!");
            }
        };
    }

    jQuery.getJSON(restfulUrl, callBackFunction);
};

function validateSearchRevision() {
    if(jQuery("#revise_first").val() == null || jQuery.trim(jQuery("#revise_first").val()) == "" || (jQuery("#revise_first").val().length < 1)
            || jQuery("#revise_last").val() == null || jQuery.trim(jQuery("#revise_last").val()) == "" || (jQuery("#revise_last").val().length < 2) ) {
        jQuery("#revise_search_error").css("display","block");
        return false;
    }
    return true;
}

function initializePopupElements(hasSearchResults) {

    jQuery(".revise_search_link").click(function() {
        jQuery("#revise_search_box").dialog("open");        
        jQuery('.revise-search-dialog-class').css('left', (jQuery(window).width()-jQuery('.revise-search-dialog-class').width())/2);
    });

    if(hasSearchResults) {
        jQuery("#revise_search_box").dialog({"bgiframe": true, "autoOpen": false, "width": 460, dialogClass: 'revise-search-dialog-class', position: [-500,190] });
        jQuery("#tell_a_friend_box").dialog({"bgiframe": true, "autoOpen": false, "width": 460, dialogClass: 'taf-dialog-class', position: [-500,190]});
        jQuery("#cpl_srch_right_banner").load("/cs/couponPlacement.action");
    }

    jQuery("#taf_submit").click(function() {
        tafForm.submit();
    });

    jQuery("#cards_go_here").load("/cs/giftCardsPlacement.action");
}
