var search = {}
$(function() {
    /*$(".accordion").accordion({
        autoHeight: false,
        animated: false,
        change: function(event, ui) {
            var index = $(this).find("h3").index(ui.newHeader[0]);
            $("#ctl00_ContentPlaceHolder1_Search_hdnAccordion").val(index);
        }
    });*/

    $('.multi-accordion').multiAccordion();

    /*$('#ctl00_ContentPlaceHolder1_Search_zipAdv').hide(); */
    $(".multi-accordion h3 a").click(function(event) {
        event.preventDefault();
    });
	
	//Adding up behavior to state radio on cunsumers tab only. Email subject "Would you be able to do this on Monday?"
	$('#ctl00_ContentPlaceHolder1_Search_rblLocation_1').live('change',function(){
		if($('#ctl00_ContentPlaceHolder1_Search_liConsumers').is('.ui-tabs-selected'))
			document.location.href = '/content/counts/CoCountState.aspx';
	});
});

function accordion() { 
    $("#spinImg").remove();
    $('#sicDialog').dialog('close');
    $('#naicDialog').dialog('close');
    $('.multi-accordion').multiAccordion();
    $(".multi-accordion h3 a").click(function (event) {
        event.preventDefault();

    });
  if ($("#ctl00_ContentPlaceHolder1_Search_acc").val() != "1") {
    $(".accordion").accordion({ 
        autoHeight: false, 
        animated: false,
        change: function(event, ui) {
		    var index = $(this).find("h3").index ( ui.newHeader[0] );
		    $("#ctl00_ContentPlaceHolder1_Search_hdnAccordion").val(index)		    
	    }	
    });
    if($("#ctl00_ContentPlaceHolder1_Search_hdnAccordion").val()!=""){       
        $(".accordion").accordion("activate" , parseInt($("#ctl00_ContentPlaceHolder1_Search_hdnAccordion").val(),10));
    }   
	
    
    if($("#ctl00_ContentPlaceHolder1_Search_hdnShowDiv").val()=="1"){      
        $("#ctl00_ContentPlaceHolder1_Search_zipAdv").show();
    }else{
        $("#ctl00_ContentPlaceHolder1_Search_zipAdv").hide();
    }
  }
  $("table.num-employees input[type='checkbox']").click(function(){
	    if($(this).attr("id") == "ctl00_ContentPlaceHolder1_Search_numEmployees_11" && $(this).is(":checked")){
	        $("table.num-employees input[type='checkbox']").removeAttr("checked");
	        $("input#ctl00_ContentPlaceHolder1_Search_numEmployees_11").attr("checked","checked");
	    }
	    else{
	        $("input#ctl00_ContentPlaceHolder1_Search_numEmployees_11").removeAttr("checked")
	    }
	});
	
	$("table.annual-sales input[type='checkbox']").click(function(){
	    if($(this).attr("id") == "ctl00_ContentPlaceHolder1_Search_annualSales_10" && $(this).is(":checked")){
	        $("table.annual-sales input[type='checkbox']").removeAttr("checked");
	        $("input#ctl00_ContentPlaceHolder1_Search_annualSales_10").attr("checked","checked");
	    }
	    else{
	        $("input#ctl00_ContentPlaceHolder1_Search_annualSales_10").removeAttr("checked")
	    }
	});
	
	$("table.annual-sales input[type='checkbox']").click(function(){
	    if($(this).attr("id") == "ctl00_ContentPlaceHolder1_Search_annualSales_10" && $(this).is(":checked")){
	        $("table.annual-sales input[type='checkbox']").removeAttr("checked");
	        $("input#ctl00_ContentPlaceHolder1_Search_annualSales_10").attr("checked","checked");
	    }
	    else{
	        $("input#ctl00_ContentPlaceHolder1_Search_annualSales_10").removeAttr("checked")
	    }
	});
	
	$("table.square-footage input[type='checkbox']").click(function(){
	    if($(this).attr("id") == "ctl00_ContentPlaceHolder1_Search_squareFootage_4" && $(this).is(":checked")){
	        $("table.square-footage input[type='checkbox']").removeAttr("checked");
	        $("input#ctl00_ContentPlaceHolder1_Search_squareFootage_4").attr("checked","checked");
	    }
	    else{
	        $("input#ctl00_ContentPlaceHolder1_Search_squareFootage_4").removeAttr("checked")
	    }
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkExcellent").click(function () {
	    if ($(this).is(":checked")) {	        
	        $("#ctl00_ContentPlaceHolder1_Search_chk95").attr("checked", "checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk90").attr("checked", "checked");
	    }
	    else {
	        $("#ctl00_ContentPlaceHolder1_Search_chk95").removeAttr("checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk90").removeAttr("checked");
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chk85").attr("checked", "checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk80").attr("checked", "checked");
	    }
	    else {
	        $("#ctl00_ContentPlaceHolder1_Search_chk85").removeAttr("checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk80").removeAttr("checked");
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkGood").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chk75").attr("checked", "checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk70").attr("checked", "checked");
	    }
	    else {
	        $("#ctl00_ContentPlaceHolder1_Search_chk75").removeAttr("checked");
	        $("#ctl00_ContentPlaceHolder1_Search_chk70").removeAttr("checked");
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk95").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkExcellent").attr("checked", "checked");	        
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk90").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkExcellent").removeAttr("checked");
	        }	        
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk90").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkExcellent").attr("checked", "checked");
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk95").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkExcellent").removeAttr("checked");
	        }
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk85").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").attr("checked", "checked");
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk80").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").removeAttr("checked");
	        }
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk80").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").attr("checked", "checked");
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk85").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").removeAttr("checked");
	        }
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk75").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkGood").attr("checked", "checked");
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk70").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkGood").removeAttr("checked");
	        }
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chk70").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkGood").attr("checked", "checked");
	    }
	    else {
	        if (!$("#ctl00_ContentPlaceHolder1_Search_chk75").is(":checked")) {
	            $("#ctl00_ContentPlaceHolder1_Search_chkGood").removeAttr("checked");
	        }
	    }
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkUnknown").click(function () {	    
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkInstitution").click(function () {
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkAllCredit").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkExcellent").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk95").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk90").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk85").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk80").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkGood").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk75").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chk70").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkUnknown").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkInstitution").removeAttr("checked")
	    }	    
	    chkOthers()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkAllLocations").click(function () {
	    if ($(this).is(":checked")) {
	        $("#ctl00_ContentPlaceHolder1_Search_chkCorporate").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkHeadquarters").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkBranch").removeAttr("checked")
	        $("#ctl00_ContentPlaceHolder1_Search_chkUnknownlocation").removeAttr("checked")	        
	    }
	    chkOthersLocation()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkCorporate").click(function () {	    
	    chkOthersLocation()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkHeadquarters").click(function () {
	    chkOthersLocation()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkBranch").click(function () {
	    chkOthersLocation()
	});
	$("#ctl00_ContentPlaceHolder1_Search_chkUnknownlocation").click(function () {
	    chkOthersLocation()
	});
	showLocations()
	common();
}
function chkOthers() {
    if($("#ctl00_ContentPlaceHolder1_Search_chkExcellent").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk95").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk90").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkVeryGood").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk85").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk80").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkGood").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk75").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chk70").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkUnknown").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkInstitution").is(":checked"))
        $("#ctl00_ContentPlaceHolder1_Search_chkAllCredit").removeAttr("checked");
    else
        $("#ctl00_ContentPlaceHolder1_Search_chkAllCredit").attr("checked", "checked");
}
function chkOthersLocation() {
    if ($("#ctl00_ContentPlaceHolder1_Search_chkCorporate").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkHeadquarters").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkBranch").is(":checked") || $("#ctl00_ContentPlaceHolder1_Search_chkUnknownlocation").is(":checked"))
        $("#ctl00_ContentPlaceHolder1_Search_chkAllLocations").removeAttr("checked");
    else
        $("#ctl00_ContentPlaceHolder1_Search_chkAllLocations").attr("checked", "checked");
} 
function common() { 
    $("table#ctl00_ContentPlaceHolder1_Search_grdKeyword a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdCompany a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdSic a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdNaic a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdEmployees a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdSales a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
    $("table#ctl00_ContentPlaceHolder1_Search_grdSquare a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
    $("table#ctl00_ContentPlaceHolder1_Search_grdAreas a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdCities a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdCounties a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdStates a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdZip a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdRecords a").click(function(){
	        spin($(this).attr("id"),1,1,'');
	    });
	$("table#ctl00_ContentPlaceHolder1_Search_grdContact a").click(function () {
	    spin($(this).attr("id"), 1, 1, '');
	});
	$("table#ctl00_ContentPlaceHolder1_Search_grdCredit a").click(function () {
	    spin($(this).attr("id"), 1, 1, '');
	});
	$("table#ctl00_ContentPlaceHolder1_Search_grdPostal a").click(function () {
	    spin($(this).attr("id"), 1, 1, '');
	});
	$("table#ctl00_ContentPlaceHolder1_Search_grdLocType a").click(function () {
	    spin($(this).attr("id"), 1, 1, '');
	});
	$("table#ctl00_ContentPlaceHolder1_Search_grdRadius a").click(function () {
	    spin($(this).attr("id"), 1, 1, '');
	});	
	if($("#ctl00_ContentPlaceHolder1_Search_hdnCheckout").val()=="1"){       
        AddCustomItemToCartHP();
    }
    if ($("#ctl00_ContentPlaceHolder1_Search_hdnCheckout").val() == "2") {
        AddCustomEmailListItemToCartHP()
    }	    
}
function move(from,to,type,input) {
    var selectedItems = $(from + " :selected").toArray();
    par = ""
    /*if (input != "")
        par = $(input).val() + ";";*/
    for (jj=0;jj<selectedItems.length;jj++) {
        //selectedItems[jj].value = par + selectedItems[jj].value
        if(type==1){
            var hdn = to.replace("drd", "hdn") 
            $(hdn).val( $(hdn).val()+","+selectedItems[jj].text+";"+selectedItems[jj].value)            
        }else{
            var hdn = from.replace("drd", "hdn") 
            $(hdn).val( $(hdn).val().replace(","+selectedItems[jj].text+";"+selectedItems[jj].value, ""))            
        }
    }    
	$(to).append(selectedItems);
	Sortit(to);
	selectedItems.remove;
}
function ShowHideDiv(div) {
    if ($(div).is(':visible')) {
        $("#ctl00_ContentPlaceHolder1_Search_hdnShowDiv").val("0")
        $(div).hide();
    }else{
        $("#ctl00_ContentPlaceHolder1_Search_hdnShowDiv").val("1")
        $(div).show();
    }
}


$(document).ready(function() {
   	$('#ctl00_ContentPlaceHolder1_Search_lnkNext').click(function() {
		var sidebarHeight = $('#search-left').height();
		$('#search').css("height",sidebarHeight+"px");
	});
});
function Sortit(drp) {
    ops = $(drp+" option");
    ops.sort(function (a,b) {
      return ( $(a).html() > $(b).html() )
    });
    html="";
    for(i=0;i<ops.length;i++)
    {
      html += "<option value='" + $(ops[i]).val() + "'>" + $(ops[i]).html() + "</option>";	
    }
    $(drp).html(html);

}
//-------------------------------------Sic Dialog
var page_sics=1;
var order_sics="";
var order_sics_param="";
var pageSize = 15;
var order_type = true; //true = asc, false = desc
$(document).ready(function() {
   	$("head").append(
         '<script type="text/x-jqote-template" id="tplSicList"><![CDATA[' +
            "<tr id='<*= this.Code *>'>" +
            "<td><input type=\"checkbox\" id=\"<*= this.Code *>\" <* var x= this.Code;  $('#ctl00_ContentPlaceHolder1_Search_drdSics option').each(function(){if($(this).val()==x) {*><*='checked'*> <*='disabled'*><*} });*> onClick=\"addSic('<*= this.Code *>',this)\" value=\"<*= this.Code *>\"></td>" +
            "<td><*= this.Code *></td>" +
            "<td><*= this.Industry *></td>" +
            "</tr>" +
          ']]></script>'
        );
    $('#sicDialog').dialog({
		autoOpen: false,
		width: 600,
		modal: true,
		resizable: false,		
		open: function(type, data) {
            $(this).parent().appendTo("form");
        }	
	});	
});
function listSics(page,opt){    
    if(opt==1){
        $('#ctl00_ContentPlaceHolder1_Search_drdSics').children().remove().end()  
        $('#ctl00_ContentPlaceHolder1_Search_hdnSics').val("")
        var $inputs = $('#ctl00_ContentPlaceHolder1_Search_sicInputs :input');
    $inputs.each(function() {
        if($(this).val()!=""){
            $('#ctl00_ContentPlaceHolder1_Search_drdSics').append(
                $('<option></option>').val($(this).val()).html($(this).val())
            );  
        }
    });
    }
    
    if ($("#sic-table-container").html() == "") $("#sic-table-container").html("Loading...").show();
    search=$("#txtFilter").val();
    values ="";
    if (page == null) { page = 1 };
    if (search == null) search = JSON.parse($.cookie(searchProductCookieName));
    ops = $("#ctl00_ContentPlaceHolder1_Search_drdSics option");    
    for(i=0;i<ops.length;i++)
    {
      values += $(ops[i]).val() + "-@@-";
  }
  typeSearch = $('#hpSearchType').val();
    $.ajax({
        type: "POST",
        url: "../ws/HPSearch.asmx/getSics",
        data: JSON.stringify({ search: search, page: page, orderBy: order_sics_param, pageSize: pageSize, exclude: values, type: typeSearch }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var result;
            if (msg.d != undefined) { result = msg.d } else { result = msg }
            var theResults = eval("(" + result + ")");
            if (theResults.length > 0) {  
                cssCode="";
                cssIndustry="";
                if(order_type)
                    ord = "asc"
                else
                    ord = "desc"
                if(order_sics == "Code")          
                     cssCode="class ='"+ord+"'";
                else
                     if(order_sics == "Industry")
                        cssIndustry="class ='"+ord+"'";
                $("#sic-table-container").html("<table id='sic-table' class='data-list'>" +
                    "<tr><th>&nbsp;</th><th id='headerCode' "+cssCode+">Code</th><th id='headerName' "+cssIndustry+">Name</th></tr>" +
                "</table>");
                $("#sic-table").append($('#tplSicList').jqote($.parseJSON(result), "*"));
                $("#sic-table tr:odd").addClass('odd');
                $('#sicDialog').dialog('open');
                //==========paginator===============//
                var paginator = "<table class='paginator'><tr>";
                if (page > 1) paginator += "<td><span class='link prevLink'>< Previous</span></td>"
                //paginator += "<td align='center'>Page " + page + " &nbsp; | &nbsp; " + theResults.length + " Business</td><td align=right >&nbsp;";
                if ($("#sic-table tr").size() >= pageSize) paginator += "<td><span class='link nextLink'>Next ></span></td>";
                paginator += "</tr></table>";
                $("#sic-table").after(paginator);
                $(".prevLink").bind("click", function() { paginateSic(-1, this) });
                $(".nextLink").bind("click", function() { paginateSic(1, this) });
                $("#headerCode").bind("click", function() { sortSic('Code', this) });
                $("#headerName").bind("click", function() { sortSic('Industry', this) });
                //==========paginator===============//
                $("#spinImg").remove();
            }
        },
        error: function(result) {
            alert(result.status + ' ' + result.statusText);
        }
    }); //ajax
}
function paginateSic(i, ctrl) {
    $(ctrl).append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />");
    page_sics = page_sics + i;
    listSics(page_sics,0);
};
function sortSic(sort, ctrl) {
    $(ctrl).append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />");
    order = "ASC"
    if(sort != order_sics) 
        order_type = true
    else
        order_type = !order_type
    if(order_type)
        order = "ASC"
    else
        order = "DESC"
    order_sics = sort
    order_sics_param = sort + " " + order
    listSics(page_sics,0);
};
function addSic(code, ctrl) {    
    $('#ctl00_ContentPlaceHolder1_Search_drdSics').append(
            $('<option></option>').val(code).html(code)
        );   
        parent_code = $(ctrl).parent()
        $(ctrl).remove();
        parent_code.append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />"); 
    ops = $("#ctl00_ContentPlaceHolder1_Search_drdSics option");
    hdn = $('#ctl00_ContentPlaceHolder1_Search_hdnSics')  
    hdn.val("")    
    for(i=0;i<ops.length;i++)
    {
        hdn.val($(ops[i]).val()+"-@@-"+hdn.val())
    }   
    Sortit("#ctl00_ContentPlaceHolder1_Search_drdSics");
    listSics(page_sics,0);
};
function removeSics() {    
    var selectedItems = $("#ctl00_ContentPlaceHolder1_Search_drdSics :selected").toArray();     
    for (jj=0;jj<selectedItems.length;jj++)
    {        
       $("#ctl00_ContentPlaceHolder1_Search_hdnSics").val( $("#ctl00_ContentPlaceHolder1_Search_hdnSics").val().replace(selectedItems[jj].value+"-@@-", ""))       
    }   
    $("#ctl00_ContentPlaceHolder1_Search_drdSics option:selected").remove();      
    listSics(page_sics,0);
};
function closeSicDialog() {
    $('#sicDialog').dialog('close');
};
//-------------------------------------Sic Dialog
//-------------------------------------Naic Dialog
var page_naics=1;
var order_naics="";
var order_naics_param="";
var pageSize = 15;
var order_type = true; //true = asc, false = desc
$(document).ready(function() {
   	$("head").append(
         '<script type="text/x-jqote-template" id="tplNaicList"><![CDATA[' +
            "<tr id='<*= this.NAICS *>'>" +
            "<td><input type=\"checkbox\" id=\"<*= this.NAICS *>\" <* var x= this.NAICS;  $('#ctl00_ContentPlaceHolder1_Search_drdNaic option').each(function(){if($(this).val()==x) {*><*='checked'*> <*='disabled'*><*} });*> onClick=\"addNaic('<*= this.NAICS *>',this)\" value=\"<*= this.NAICS *>\"></td>" +
            "<td><*= this.NAICS *></td>" +
            "<td><*= this.NAICS_Description *></td>" +
            "</tr>" +
          ']]></script>'
        );
    $('#naicDialog').dialog({
		autoOpen: false,
		width: 600,
		modal: true,
		resizable: false,		
		open: function(type, data) {
            $(this).parent().appendTo("form");
        }	
	});	
});
function listNaics(page,opt){    
    if(opt==1){
        $('#ctl00_ContentPlaceHolder1_Search_drdNaic').children().remove().end()  
        $('#ctl00_ContentPlaceHolder1_Search_hdnNaic').val("")
        var $inputs = $('#ctl00_ContentPlaceHolder1_Search_naicsInputs :input');
        $inputs.each(function() {
            if($(this).val()!=""){
                $('#ctl00_ContentPlaceHolder1_Search_drdNaic').append(
                    $('<option></option>').val($(this).val()).html($(this).val())
                );  
            }
        });
    }
    
    if ($("#naic-table-container").html() == "") $("#naic-table-container").html("Loading...").show();
    search=$("#txtFilterNaic").val();
    values ="";
    if (page == null) { page = 1 };
    if (search == null) search = JSON.parse($.cookie(searchProductCookieName));
    ops = $("#ctl00_ContentPlaceHolder1_Search_drdNaic option");    
    for(i=0;i<ops.length;i++)
    {
      values += $(ops[i]).val() + "-@@-";	
    }      
    $.ajax({
        type: "POST",
        url: "/ws/HPSearch.asmx/getNaics",
        data: JSON.stringify({ search: search, page: page,orderBy: order_naics_param,pageSize: pageSize, exclude: values }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var result;
            if (msg.d != undefined) { result = msg.d } else { result = msg }
            var theResults = eval("(" + result + ")");
            if (theResults.length > 0) {  
                cssCode="";
                cssIndustry="";
                if(order_type)
                    ord = "asc"
                else
                    ord = "desc"
                if(order_naics == "NAICS")          
                     cssCode="class ='"+ord+"'";
                else
                     if(order_naics == "NAICS_Description")
                        cssIndustry="class ='"+ord+"'";
                $("#naic-table-container").html("<table id='naic-table' class='data-list'>" +
                    "<tr><th>&nbsp;</th><th id='headerCodeNaic' "+cssCode+">Code</th><th id='headerNameNaic' "+cssIndustry+">Name</th></tr>" +
                "</table>");
                $("#naic-table").append($('#tplNaicList').jqote($.parseJSON(result), "*"));
                $("#naic-table tr:odd").addClass('odd');
                $('#naicDialog').dialog('open');
                //==========paginator===============//
                var paginator = "<table class='paginator'><tr>";
                if (page > 1) paginator += "<td><span class='link prevLinkNaic'>< Previous</span></td>"
                //paginator += "<td align='center'>Page " + page + " &nbsp; | &nbsp; " + theResults.length + " Business</td><td align=right >&nbsp;";
                if ($("#naic-table tr").size() >= pageSize) paginator += "<td><span class='link nextLinkNaic'>Next ></span></td>";
                paginator += "</tr></table>";
                $("#naic-table").after(paginator);
                $(".prevLinkNaic").bind("click", function() { paginateNaic(-1, this) });
                $(".nextLinkNaic").bind("click", function() { paginateNaic(1, this) });
                $("#headerCodeNaic").bind("click", function() { sortNaic('NAICS', this) });
                $("#headerNameNaic").bind("click", function() { sortNaic('NAICS_Description', this) });
                //==========paginator===============//
                $("#spinImg").remove();
            }
        },
        error: function(result) {
            alert(result.status + ' ' + result.statusText);
        }
    }); //ajax
}
function paginateNaic(i, ctrl) {
    $(ctrl).append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />");
    page_naics = page_naics + i;
    listNaics(page_naics,0);
};
function sortNaic(sort, ctrl) {
    $(ctrl).append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />");
    order = "ASC"
    if(sort != order_naics) 
        order_type = true
    else
        order_type = !order_type
    if(order_type)
        order = "ASC"
    else
        order = "DESC"
    order_naics = sort
    order_naics_param = sort + " " + order
    listNaics(page_naics,0);
};
function addNaic(code, ctrl) {    
    $('#ctl00_ContentPlaceHolder1_Search_drdNaic').append(
            $('<option></option>').val(code).html(code)
        );  
        parent_code = $(ctrl).parent()
        $(ctrl).remove();
        parent_code.append("<img src='../../App_Themes/Images/hpsearch/spinner.gif' style='height:11px;width:11px' />"); 
    ops = $("#ctl00_ContentPlaceHolder1_Search_drdNaic option");
    hdn = $('#ctl00_ContentPlaceHolder1_Search_hdnNaic')  
    hdn.val("")    
    for(i=0;i<ops.length;i++)
    {
        hdn.val($(ops[i]).val()+"-@@-"+hdn.val())
    }   
    Sortit("#ctl00_ContentPlaceHolder1_Search_drdNaic");
    
    listNaics(page_naics,0);
};
function removeNaics() {
    var selectedItems = $("#ctl00_ContentPlaceHolder1_Search_drdNaic :selected").toArray();     
    for (jj=0;jj<selectedItems.length;jj++)
    {        
       $("#ctl00_ContentPlaceHolder1_Search_hdnNaic").val( $("#ctl00_ContentPlaceHolder1_Search_hdnNaic").val().replace(selectedItems[jj].value+"-@@-", ""))       
    }    
    $("#ctl00_ContentPlaceHolder1_Search_drdNaic option:selected").remove();     
    listNaics(page_naics,0);
};
function closeNaicDialog() {
    $('#naicDialog').dialog('close');
};
function spin(input, type, color, css) {
    
    strColor = "";
    if($('#'+input).parent().attr("class")=="ui-tabs-selected")
        color=2;
    if(color==1){//dark orange
        strColor="spinner-sidebar.gif";
    }else{
        if(color==2){//orange
            strColor="spinner-sidebar-selected.gif";
        }else{
            if(color==3){//top
                strColor="spinner-top.gif";
            } else {
                if(color==4){//tab top
                    strColor="loader-top-tab.gif";
                }else{
                    strColor="spinner.gif";
                }  
            }                          
        }
    }
    
    if(css!="") css=" class='"+css+"'";

    if(type==0)
        $('#'+input).after("<img id='spinImg' src='../../App_Themes/Images/hpsearch/" + strColor + "' style='height:11px;width:11px' "+css+" />");
    else if(type==1)
        $('#' + input).append("<img id='spinImg' src='../../App_Themes/Images/hpsearch/" + strColor + "' style='height:11px;width:11px' " + css + " />");
}
function spinAddCart(theControl) {
    $(theControl).after("&nbsp;<img id='spinImgPermanent' src='../../App_Themes/Images/hpsearch/spinner-sidebar.gif' style='height:11px;width:11px' />");
}
//-------------------------------------Naic Dialog
function showLocations() {
    $("#ctl00_ContentPlaceHolder1_Search_pnlnational").attr("class","hide");
    $("#ctl00_ContentPlaceHolder1_Search_pnlcity").attr("class","hide");
    $("#ctl00_ContentPlaceHolder1_Search_pnlstate").attr("class","hide");
    $("#ctl00_ContentPlaceHolder1_Search_pnlzip").attr("class","hide");
    $("#ctl00_ContentPlaceHolder1_Search_pnlcounty").attr("class","hide");
        
    if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_0").is(":checked")) {
        $("#ctl00_ContentPlaceHolder1_Search_pnlcity").attr("class","show");
    } else {
        if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_1").is(":checked")) {
            $("#ctl00_ContentPlaceHolder1_Search_pnlstate").attr("class","show");
        } else {
            if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_2").is(":checked")) {
                $("#ctl00_ContentPlaceHolder1_Search_pnlcounty").attr("class","show");
            } else {
                if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_3").is(":checked")) {
                    $("#ctl00_ContentPlaceHolder1_Search_pnlzip").attr("class","show");
                }
            }
        }
    }
}

function isZipValid() {
    if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_3").is(":checked")) {
        var field = $("#ctl00_ContentPlaceHolder1_Search_txtZip").val();        
        field = trim(field);
        if (field != "")
            return true;
        else
            return false;
    }
    else
        return true;
}

function validateZip(source, args) {
    args.IsValid = isZipValid();
    if (!args.IsValid) {
        $(".accordion").accordion("activate", 0);
        $("#spinImg").remove();
    }
    return;
}

function isMilesValid() {
    if ($("#ctl00_ContentPlaceHolder1_Search_rblLocation_3").is(":checked")) {
        var miles = $("#ctl00_ContentPlaceHolder1_Search_txtMiles").val();   
        miles = trim(miles);
        if (miles != "") {
            var reg = /^\d*$/;
            if (reg.test(miles))
                if (miles >= 0 && miles <= 200)
                    return true;
                else
                    return false;
            else
                return false;
        }
        else
            return false;
    }
    else
        return true;
}

function validateMiles(source, args) {
    args.IsValid = isMilesValid();
    if (!args.IsValid) {
        $(".accordion").accordion("activate", 0);
        $("#spinImg").remove();
    }
    return;
}
function ltrim(s) {
    return s.replace(/^\s*/, "");
}

function rtrim(s) {
    return s.replace(/\s*$/, "");
}

function trim(s) {
    return rtrim(ltrim(s));
}

/* New functions multiple select on email search */

function copySelectItems(objSrc, objDst, hdnField, hdnFieldTxt) {
    var fromcontrol = $(objSrc);
    var tocontrol = $(objDst);

    if ($(fromcontrol).children('option:selected').length == 0) return;

    $(tocontrol).append($(fromcontrol).children('option:selected'));

    var my_options = $(tocontrol).children("option");
    my_options.sort(function(a, b) {
        if (a.text > b.text) return 1;
        else if (a.text < b.text) return -1;
        else return 0
    });

    $(tocontrol).empty().append(my_options);
    $(tocontrol).children("option:selected").removeAttr("selected");

    /*Saving values in hidden field*/
    $(hdnField).val('');
    $(hdnFieldTxt).val('');
    var selectedItems = $(objDst + ' option');
    selectedItems.each(function() {
        if (trim($(this).val()) == "") return;
        $(hdnField).val($(hdnField).val() + $(this).val() + ',');
        if ($(hdnFieldTxt).val() != "") $(hdnFieldTxt).val($(hdnFieldTxt).val() + ',');
        $(hdnFieldTxt).val($(hdnFieldTxt).val() + $(this).html());
    });

    //alert('Worked fields:' + hdnField + ' and ' + hdnFieldTxt);
    //alert($(hdnField).val());
    //alert($(hdnFieldTxt).val());
}

function removeSelectItems(objSrc, objDst, hdnField, hdnFieldTxt) {
    var fromcontrol = $(objSrc);
    var tocontrol = $(objDst);

    if ($(fromcontrol).children('option:selected').length == 0) return;

    $(tocontrol).append($(fromcontrol).children('option:selected'));

    var my_options = $(tocontrol).children("option");
    my_options.sort(function(a, b) {
        if (a.text > b.text) return 1;
        else if (a.text < b.text) return -1;
        else return 0
    });

    $(tocontrol).empty().append(my_options);
    $(tocontrol).children("option:selected").removeAttr("selected");

    /*Saving values in hidden field*/
    $(hdnField).val('');
    $(hdnFieldTxt).val('');
    var selectedItems = $(objSrc + ' option');
    selectedItems.each(function() {
        if (trim($(this).val()) == "") return;
        $(hdnField).val($(hdnField).val() + $(this).val() + ',');
        if ($(hdnFieldTxt).val() != "") $(hdnFieldTxt).val($(hdnFieldTxt).val() + ',');
        $(hdnFieldTxt).val($(hdnFieldTxt).val() + $(this).html());
    });

    //alert('Worked fields:' + hdnField + ' and ' + hdnFieldTxt);
    //alert($(hdnField).val());
    //alert($(hdnFieldTxt).val());    
}


function clearSearchVars() {
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedStates').val('');
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedStatesTxt').val('');
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedCounties').val('');
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedCountiesTxt').val('');
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedCities').val('');
    $('#ctl00_ContentPlaceHolder1_Search_hdnSelectedCitiesTxt').val('');

    $('#ctl00_ContentPlaceHolder1_Search_cmbSelectedCounty option').appendTo('#ctl00_ContentPlaceHolder1_Search_cmbCounty');
    $('#ctl00_ContentPlaceHolder1_Search_cmbSelectedStates option').appendTo('#ctl00_ContentPlaceHolder1_Search_cmbState');
    $('#ctl00_ContentPlaceHolder1_Search_cmbSelectedCity option').appendTo('#ctl00_ContentPlaceHolder1_Search_cmbCity');

    SortSelect('ctl00_ContentPlaceHolder1_Search_cmbCounty');
    SortSelect('ctl00_ContentPlaceHolder1_Search_cmbState');
    SortSelect('ctl00_ContentPlaceHolder1_Search_cmbCity');
}

function SortSelect(controlId) {
    var my_options = $('#' + controlId).children("option");
    my_options.sort(function(a, b) {
        if (a.text > b.text) return 1;
        else if (a.text < b.text) return -1;
        else return 0
    });
    $('#' + controlId).empty().append(my_options);
}
