    //-------------------------------------------------------------------------
    // Global Variable Definitions
    //-------------------------------------------------------------------------
        var clkAction       = 0;
        var clkCustCode     = '';




    //-------------------------------------------------------------------------
    // Define Event Handlers
    //-------------------------------------------------------------------------
     $(document).ready(function(){

        // Don't show the List or Add forms.
        $('#customerLookupFilter').css('display', 'none');
        $('#customerLookupCreate').css('display', 'none');

        //-----------------------------------------------------------------
        // If they change the Zip Code or E-Mail Address - reset the forms.
        //-----------------------------------------------------------------
        
            $("#clkBillZip").change(function() {
                clkAction = 0;
                if ($("#customerLookupFilter").css('display') != 'none')
                    $("#customerLookupFilter").hide('slow');
                if ($("#customerLookupCreate").css('display') != 'none')
                    $("#customerLookupCreate").hide('slow');
            });

            $('#clkEmail').change(function(){
                clkAction = 0;
                if ($("#customerLookupFilter").css('display') != 'none')
                    $("#customerLookupFilter").hide('slow');
                if ($("#customerLookupCreate").css('display') != 'none')
                    $("#customerLookupCreate").hide('slow');
            });
            $("#clkBillZip").mouseup(function() {
                clkAction = 0;
                if ($("#customerLookupFilter").css('display') != 'none')
                    $("#customerLookupFilter").hide('slow');
                if ($("#customerLookupCreate").css('display') != 'none')
                    $("#customerLookupCreate").hide('slow');
            });

            $('#clkEmail').mouseup(function() {
                clkAction = 0;
                if ($("#customerLookupFilter").css('display') != 'none')
                    $("#customerLookupFilter").hide('slow');
                if ($("#customerLookupCreate").css('display') != 'none')
                    $("#customerLookupCreate").hide('slow');
            });            
    });      

    
    //-------------------------------------------------------------------------
    // loadCustomerFilter
    //      Displays the list of matching customers in Everest Database
    //      for the given email address and billing zip code.
    //-------------------------------------------------------------------------    
   
        function loadCustomerFilter() {

			semail      = jQuery.trim($("#clkEmail").val());
			sbillto     = jQuery.trim($("#clkBillZip").val());
					
			var re = new RegExp('[^0-9]',"ig");
		    sbillto =  sbillto.replace(re, '');		

			if (semail == '' || sbillto == '' || sbillto.length != 5) {
                $.prompt('To proceed, please enter a valid E-Mail address and 5-digit Zip Code.');				
				return false;
			}


            switch (clkAction) {
                case 0: 
                    // The user has submitted an email address and zip code.  
                    // We haven't done anything with the information yet.
                    // 
                    
                    if (semail != '' && sbillto != '') {
                        url = '/common/ajax_custlookup.php';
                        $.get(url, { email: semail, billto: sbillto },
                          function(data){
                   
                            if (data == "0") {
                                // no record for this email address/zip combo.
                                $("#cLookupFilterList").html("no records - <a href=\"#\" onclick=\"showCustomerCreate(); return false;\">create new</a> / search again?");
                                $("#customerLookupCreate").show('slow');
                                clkAction = 3;
                            } else {
                                
                                $("#customerLookupFilter").show('slow');
      
                                
                                $("#cLookupFilterList").html(data);
                                clkAction = 1;
                            }
                          });
                    } else {
                       $("#cLookupFilterList").html('');
                    }
                    break;
                case 1:
                    // The user has chosen either a customer ID or a new customer
                    
                    clkCustCode = $("input[name=custAddress]:checked").val();
                    if (typeof(clkCustCode) == 'undefined') {
                        $.prompt('Please select a valid user from the list or change your E-Mail or Billing Zip code to Search again.');
                        return;
                    }
                    
                    
                    //alert('Selected : ' + clkCustCode);
                    
                    $('#customerLookupForm').submit(); 
                    
                    
                    break;
                case 3: 
                    
                    // The user is saying that they are new
                    
                    
                    // Hide the selection list (too long).
                    $("#customerLookupFilter").hide('fast', function(){
                        // Show the add a user form.
                        clkShowCustForm(semail, sbillto)    
                     });
                    
                    
                    
                    
                    
                    
                    clkAction = 4;
                    break;
                case 4:
                    // The user is submitting the new user form.
                    $('#customerLookupForm').submit(); 
                   
            }
        }
    
        
        
    //-------------------------------------------------------------------------
    // clkShowNew - Shows the form which adds a new customer to the database.
    //-------------------------------------------------------------------------       
        function clkShowCustForm(clkEmail, clkBillZip) {
            if (clkEmail == null) {
                if (typeof($("#clkEmail")) != 'undefined' ) {
                    clkEmail = $("#clkEmail").val();
                } else {
                    clkEmail = "";
                }
             }
             
             if (clkBillZip == null) {
                if( typeof($("#clkBillZip")) != 'undefined' ) {
                    clkBillZip = $("#clkBillZip").val();
                } else {
                    clkBillZip = "";
                }
             }
            
            // Set the values for the form to add a new contact
            $('#ZipCode').val(clkBillZip);
            $('#E-mail').val(clkEmail);
            
            $('#customerLookupCreate').css('display', 'block');

        }
    
        function clkHideCustForm() {
            if (typeof($("#ZipCode")) != 'undefined') {
                $('ZipCode').val("");
                $('E-mail').val("");  
                
                $('#customerLookupCreate').css('display', 'none');
            }
        }

    //-------------------------------------------------------------------------
    // selectCusomterFilter - Select Customer or New from the list..
    //-------------------------------------------------------------------------            
        function selectCusomterFilter()  {
            clkCustCode = $("input[name=custAddress]:checked").val();
            if (clkCustCode == 'addnew') {
                clkAction = 3;
            } else {
                // The user selected a code from the list.
                clkHideCustForm();
                clkAction = 1;
            }
        }

        function showCustomerCreate() {
            $("#cCreate").show();
            return false;
        }
 