/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// google.load("jquery", "1.3.2");

function PageMethod(fn, paramArray, successFn, errorFn) {

    var pagePath = window.location.pathname;

    // In case the page is referenced by the directory name and
    // Default.aspx page is automatically loaded
    
    if (pagePath.match(/\/$/)) {
        pagePath += "Default.aspx";
    }
    
    //alert(pagePath);    
    //Create list of parameters in the form:   
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}   
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method   
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}

function AjaxSucceeded(result) {
    //alert('success');
    $('#suggestions').fadeIn(); // Show the suggestions box
    $('#suggestions').html(result.d); // Fill the suggestions box
    // alert(result.d);
}

function AjaxFailed(result) {
    // alert(result.d);
}

google.setOnLoadCallback(function() {
    // Safely inject CSS3 and give the search results a shadow
    var cssObj = { 'box-shadow': '#888 5px 10px 10px', // Added when CSS3 is standard
        '-webkit-box-shadow': '#888 5px 10px 10px', // Safari
        '-moz-box-shadow': '#888 5px 10px 10px'
    }; // Firefox 3.5+
    $("#suggestions").css(cssObj);

    // Fade out the suggestions box when not active
    $("input").blur(function() {
        $('#suggestions').fadeOut();
    });
});

function lookup(tbQuery) {
    if (tbQuery.length == 0) {
        $('#suggestions').fadeOut(); // Hide the suggestions box
    } else {

        //var vid = $("[id$=lblVendorID]").html();

        PageMethod("loadACData", ["keyword", tbQuery], AjaxSucceeded, AjaxFailed);

        /*
        $.post("rpc.php", { queryString: "" + inputString + "" }, function(data) { // Do an AJAX call
        $('#suggestions').fadeIn(); // Show the suggestions box
        $('#suggestions').html(data); // Fill the suggestions box
        });
        */
    }
}

