
/* resend confirmation */
function resend_confirmation(email_id)
{
    jQuery.post('/resend_confirmation/',
        {'email_id':email_id},
        function (data, textStatus)
        {
            jQuery(".confirm_result").html(data);
        }
    );
    return false
}


function prefix_from_drop_down(select_box_id) {
    return select_box_id.replace(/^id_/, "" ).replace(/content_type$/, "");
}


/* connect drop down to search field */
function update_search_box(obj_select, obj_id_input) {

    var select_box_id = obj_select.attr('id');
    var prefix = prefix_from_drop_down(select_box_id);
    var select_dropdown_id = "id_" + prefix + "content_type";
    var object_id_input_id = "id_" + prefix + "object_id";
    var search_box_id = "search_id_" + prefix + "object_id";

    jQuery("#" + search_box_id).attr('content_type',obj_select.val());
    /* put in correct value for content type */
    if (obj_id_input != "blank" && obj_id_input.val()!="") {
        jQuery.get("/item_lookup/" + jQuery("#"+select_box_id).val() + "/" + obj_id_input.val() + "/", function(data) {
              jQuery("#"+search_box_id).val(data.split('|')[0]);
        } );
    } else {
        /* blank out search box */
        jQuery("#"+search_box_id).val("");
        jQuery("#"+object_id_input_id).removeAttr('value');
    }
}

/* attach search autocomplete to search box */
function attach_search(obj) {
        jQuery("#"+obj.attr('id')).unautocomplete();

        jQuery("#"+obj.attr('id')).autocomplete("/item_lookup/" + obj.attr('content_type') + "/", {
                    delay:10,
                    minChars:1,
                    matchSubset:1,
                    matchContains:1,
                    cacheLength:10,
                    autoFill:false,
                    extraParams:{ item: obj.attr('index')  }
         }
         );
}



/* attach search to box realted to dropdown */
function attach_search_dropdown(obj) {
    select_box_id = obj.attr('id');
    prefix = prefix_from_drop_down(select_box_id);
    attach_search(jQuery("#search_id_" + prefix + "object_id"));
}

function attach_search_results() {
    /* set result of auto completes */
    jQuery('.search_autocomplete').result(function(event, data, formatted) {
      jQuery('#' + data[2]).val(data[1]);
    });
}

function ToggleDiv(divName,opt)
{
  if(document.getElementById(divName))
  {
    if(document.getElementById(divName).style.display=='none')
    {
      if(typeof opt!='undefined'&&opt!='')
        Show(divName,opt);
      else
        $("#"+divName).show("fast");
    }
    else
    {
      $("#"+divName).hide("fast");
    }
  }
}

/* attach autocomplete */
/* needs attr: content_type = '<content_type>' */
/* needs attr: index = '<id-of-hidden-input>'
   makes round trip back to the js via
*/

jQuery(document).ready(function() {

    jQuery(".variable_autocomplete").each(function() {
        // class variable_autocomplete - search box
        // class variable_autocomplete_select - select input of content type
        // get prefix - search_id_quote_object_id
        search_box_id = jQuery(this).attr('id');
        prefix = search_box_id.replace(/^search_id_/, "" ).replace(/object_id$/, "");
        select_dropdown_id = "id_" + prefix + "content_type";
        object_id_input_id = "id_" + prefix + "object_id";

        update_search_box(jQuery("#" + select_dropdown_id), jQuery("#" + object_id_input_id));

        jQuery("#"+select_dropdown_id).change( function() { update_search_box(jQuery(this), "blank");
                                                            attach_search_dropdown(jQuery(this));
                                                            attach_search_results();
        });
    });

    jQuery(".search_autocomplete").each(function() {
                attach_search(jQuery(this));
    });

    jQuery(".tag_search_autocomplete").each(function() {
        jQuery("#"+this.id).autocomplete("/tag_lookup/", {
                    delay:10,
                    minChars:1,
                    matchSubset:1,
                    matchContains:1,
                    cacheLength:10,
                    autoFill:false
         }
         );
    });

    attach_search_results();


});