/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function show_mt_caste_data(frm, def_selected_caste)
{
    var ele_community       = $("#"+frm+" #frm-community");
    var ele_mothertongue    = $("#"+frm+" #frm-mothertongue");
    var ele_caste           = $("#"+frm+" #frm-caste");
    var ele_gender          = $("#"+frm+" [name=gender]");

    if (typeof def_selected_caste === 'undefined'){
        def_selected_caste = '';
    }	

    $(ele_caste).empty();
    $(ele_caste).append("<option value=\"\">Doesn\'t Matter</option>");

    if( !$.trim( $(ele_community).val() ) ||
        !$.trim( $(ele_gender).val()) )
        {
        $("#loading-caste").html('');
        $(ele_caste).attr("disabled", false);
        return false;
    }

    $(ele_caste).attr("disabled", true);

    // ajax loader
    $("#loading-caste").html("<img src=\"http://img.shaadi.com/imgs/loading.gif\" align=\"absmiddle\">");

    $.ajax({
        url: "/ajax/show-mt-caste?community=" + escape($(ele_community).val()) + "&mothertongue=" + escape($(ele_mothertongue).val())+ "&gender=" + escape($(ele_gender).val()),
        success: function(sourceData){

            if(!sourceData || sourceData.search(/no matches/i) > -1)
            {
                $("#loading-caste").html('');
                return false;

            } // EO if()

            // process data here [start]
            splitString = sourceData.split("@");

            $.each(splitString, function(index, value)
            {
                arr_caste_val = value.split("|");

                if(value.indexOf("|") == -1)
                {
                    value = value.replace(/\|/,'');
                    if (value == 'Spiritual - not religious')
                    {
                        value = 'Spiritual';
                    }

                    caste_display_label = value;
                    if(value.indexOf(":") > 0)
                    {
                        arr_caste_label = Array();
                        arr_caste_label = value.split(':');
                        caste_display_label = arr_caste_label[0];
                    }

                } // EO if(value.indexOf("|") == 0)

                // Splitting the db values n then checking if it matches for preselection..
                check_selected_caste_spilt_string = def_selected_caste.split('|');

                $.each(arr_caste_val, function(index, value)
                {
                    value  = $.trim(value);
                    caste_display_txt = '';

                    //Added  the below code b'coz we want to show Maya and not Hindu:Maya
                    if(value.indexOf(":") > 0)
                    {
                        arr_caste_txt = Array();
                        arr_caste_txt = value.split(':');
                        if(arr_caste_txt[0] == arr_caste_txt[1])
                        {
                            value = arr_caste_txt[0];
                        }

                        caste_display_txt = arr_caste_txt[1];

                    }
                    else
                    {
                        caste_display_txt = value;
                    }

                    text_is_selected = "";
                    if (caste_display_txt && $.inArray( caste_display_txt , check_selected_caste_spilt_string ) >-1 )
                    {
                        text_is_selected = "selected";
                    }

                    if(index)
                        $(ele_caste).append("<option value=\""+value+"\" "+text_is_selected+">"+caste_display_txt+"</option>");

                }); // EO $.each(arr_caste_val, function(index, value)

            }); // EO $.each(splitString, function(index, value)

            $("#loading-caste").html('');
            $(ele_caste).attr("disabled", false);

        }, // success:
        error:function(xhr, ajaxOptions, thrownError){
            $("#loading-caste").html('');
            $(ele_caste).attr("disabled", false);
            alert('Error!\nStatusText='+xhr.status+'\nContents='+thrownError);
        }

    }); // $.ajax


}
