﻿$(document).bind("keypress",function(e){
	if ((e.keyCode == 13 || e.charCode == 13)) {
		submitQuery();
	}
})

$.modal.defaults.overlayClose = "true";


$(function() {
    // ---------------------------------------
	$('#findSupplier').click(function(e) {
		e.preventDefault();
		$('#fasContainer').modal(); //{position:['20%','20%'] }
	});
	// ---------------------------------------
	$('#showAllCompanies').click(function(e) {
	// do not process click if link is suppossed to be disabled
		if($('#showAllCompanies').attr('disabled')==true) return;
		var strQuery = "?";
		strQuery += "searchBy=company&manufacturer=true&distributor=true&exporter=true";
 		window.location = "/Pages/SupplierResults.aspx" + strQuery;
 	});
 	// ---------------------------------------
 	$('#btnSubmit').click(function(e) {
 		//$(document).unbind("keypress");
		submitQuery();
	});
});
       
       // ---------------------------------------
       function submitQuery(){
            var validQuery = true;
           	//parse search criteria
           	var strQuery = "?";
			var srcType = $("input[name='rbCompanyOrIngredient']:checked").val();
            
			strQuery += "searchBy=" + srcType ;
			
			if(srcType == "company"){
				strQuery += "&name=" + $("#txtCompanyName").attr("value");
				//strQuery += "&state=" + $("Select.stateList").attr("value");
				validQuery = ($("#cbManufacturer").attr("checked")==true)||($("#cbDistributor").attr("checked")==true)||($("#cbExporter").attr("checked")==true);
				if (validQuery){
					strQuery += "&manufacturer=" + $("#cbManufacturer").attr("checked");
					strQuery += "&distributor=" + $("#cbDistributor").attr("checked");
					strQuery += "&exporter=" + $("#cbExporter").attr("checked");
				}
				else {
					alert('Please select at least one type of supplier (Manufacturer,Distributor,Exporter)');
				}
			}
			else {
				// first select all items in the selected list to enable their capture
				$("#ddlIngredientList").selectOptions(/./);
				// output the list items as an array
				var str = "";
				$("#ddlIngredientList option:selected").each(function(){
					str += $(this).val() + ","});
				//remove trailing "," if any items have been selected
				if (str.length>0){
					strQuery += "&ingredients=" + str.substr(0, str.length - 1);
					// deselect all the items
					$("#ddlIngredientList").selectOptions(/~[a-zA-Z0-9]/, true);
					//
					strQuery += "&match=" + $("#ddlMatchCriteria").val();
				} else validQuery = false;
			}
							
			if (validQuery) window.location = "/Pages/SupplierResults.aspx" + strQuery;
       }
       
       function btnCompanyName_onclick() {
            $("#btnMoveRt").attr('disabled', 'disabled');
            $("#btnMoveLeft").attr('disabled', 'disabled');
            $("Select.fullList").attr('disabled', 'disabled');
            $("#ddlIngredientList").attr('disabled', 'disabled');
            $("#ddlMatchCriteria").attr('disabled', 'disabled');
            $(".hideIngredients").attr('disabled', 'disabled');
            //--------------------------------------------------
            $("#txtCompanyName").removeAttr('disabled');
           // $("#ddlCompanyState").removeAttr('disabled');
            $("#cbManufacturer").removeAttr('disabled');
            $("#cbDistributor").removeAttr('disabled');
            $("#cbExporter").removeAttr('disabled');
            $(".hideCompanies").removeAttr('disabled');
        }

        function btnIngredient_onclick() {
            $("#btnMoveRt").removeAttr('disabled');
            $("#btnMoveLeft").removeAttr('disabled');
            $("Select.fullList").removeAttr('disabled');
            $("#ddlIngredientList").removeAttr('disabled');
            $("#ddlMatchCriteria").removeAttr('disabled');
            $(".hideIngredients").removeAttr('disabled');
            //--------------------------------------------------
            $("#txtCompanyName").attr('disabled', 'disabled');
            //$("#ddlCompanyState").attr('disabled', 'disabled');
            $("#cbManufacturer").attr('disabled', 'disabled');
            $("#cbDistributor").attr('disabled', 'disabled');
            $("#cbExporter").attr('disabled', 'disabled');
            $(".hideCompanies").attr('disabled', 'disabled');
        }

        function btnMoveRt_onclick() {
            $("Select.fullList").copyOptions("#ddlIngredientList", "selected");
            $("#ddlIngredientList").selectOptions(/~[a-zA-Z0-9]/, true);
            $("Select.fullList").removeOption(/./, true);
        }

        function btnMoveLeft_onclick() {
            $("#ddlIngredientList").copyOptions("Select.fullList", "selected");
            $("Select.fullList").selectOptions(/~[a-zA-Z0-9]/, true);
            $("#ddlIngredientList").removeOption(/./, true);
        }