﻿var webServiceProxy;
var queryText;      //the value entered by the user in the search
var resultContent = "";       //the HTML we'll create with results

// Initializes global and proxy default variables.
function pageLoad()
{
    // Instantiate the service proxy.
    webServiceProxy = new Samples.Aspnet.CapSupWebservice();

    // Set the default call back functions.
    webServiceProxy.set_defaultSucceededCallback(SucceededCallback);
    webServiceProxy.set_defaultFailedCallback(FailedCallback);
    
    //set the default test of the search input
    //***NOTE THIS IS A TEMPORARY MEASURE AS ASP NET ID MAY CHANGE***
    if(document.getElementById('ctl00_SearchBox1_ctl00')!== "undefined"){
        document.getElementById('ctl00_SearchBox1_ctl00').value = 'Search...';
    }
    
}

//Process the click function when user is clicks in the search field
function query_click(textBox){

    //clear the input field if a user clicks it
    //***NOTE THIS IS A TEMPORARY MEASURE AS ASP NET ID MAY CHANGE***
    if(document.getElementById('ctl00_SearchBox1_ctl00')!== "undefined"){
        document.getElementById('ctl00_SearchBox1_ctl00').value = '';
    }
}
//Process the keyup function when user is typing a search
function query_keyUp(textBox) {

    //* this is a wild card. Searches will be made on words containing the value.
    queryText = textBox.value + '*';
    if(queryText.length>=4)
        runQuery(queryText);
    else
    //Show "no results statement" if less than three characters are entered.
        showNoResult('nocallback');
    //document.getElementById('resultsPanel').style.display = 'none';
}

function runQuery(query) {        
    var searchQuery = webServiceProxy.LoadResults(query);
}

// Callback function that
// processes the service return value.
function SucceededCallback(result) {

    var resultContent = "";       //the HTML we'll create with results
    var resultStr = "";
    //show/hide the panel depending on if we have results
    if (result.length > 0){
        document.getElementById('resultsPanel').style.display = 'block';
        //generate html using result data
        resultContent += "<div class='head'><span>Search Results</span></div>";
        for (var i = 0; i < result.length; i++) {
            
            //Limit the character number to 100
            resultStr = result[i].Summary.substring(0,100);
            
            //Remove unwanted white space and character returns
            resultStr = resultStr.replace('\r','');
            resultStr = resultStr.replace('\n','');
            resultStr = resultStr.replace(/^\s+|\s+$/g, '');
            
            //Write out the result
            resultContent += "<div class='resultsWrap'>";
            resultContent += "<a href='" + result[i].UriString + "' class='resultWrap'>";
            resultContent += "<span class='resultTitle'><span class='title'>" + result[i].Title + "</span><span class='arrow'></span></span>";
            
            //restrict the result character length so that it fits into our result display
            resultContent += "<span class='resultSummary'>.." + resultStr + "..</span>";
            resultContent += "</a>";
            resultContent += "</div>";


        }

        //View all results link              
        resultContent += "<a class='foot' href='http://www.capitalsupport.com/searchresults.aspx?QueryExpr=" + queryText + "&ResultsPage=1'>";
        resultContent += "<span class='l'></span>";
        resultContent += "<span class='c'>View all search Results</span>";
        resultContent += "<span class='arrow'></span>";
        resultContent += "<span class='r'></span>";
        resultContent += "</a>";
        //insert the results into the results panel
        $get('resultsPanel').innerHTML = resultContent;
    }
    
    //If no results are returned show the 'no result' alert
    else showNoResult('callback')



}

// Callback function invoked when a call to 
// the  service methods fails.
function FailedCallback(error, userContext, methodName)  {
    if (error !== null) {
        //handle the error? alert("An error occurred: " + error.get_message());
    }
}

//Populate the results drop down with 'No result' statement
function showNoResult(source){
    var resultContent = "";       //the HTML we'll create with results
    document.getElementById('resultsPanel').style.display = 'block';
    resultContent += "<div class='head'><span>Search Results</span></div>";
    resultContent += "<div class='resultsWrap'>";
    resultContent += "<a class='resultWrap'>";
    resultContent += "<span class='noResultTitle'><span>No results</span></span>";

    //If a query has been run, then there must be no results.
    if (source == 'callback')
        resultContent += "<span class='resultSummary'>Your search did not match any results.</span>";
    else
        resultContent += "<span class='resultSummary'>Search term must be a minimum of three characters long.</span>";
        
    resultContent += "</a>";
    resultContent += "</div>";
    
    $get('resultsPanel').innerHTML = resultContent;
}
document.onclick=check;
function check(e){
    var target = (e && e.target) || (event && event.srcElement);
    var obj = document.getElementById('resultsPanel');
    var obj2 = document.getElementById('form1');
    checkParent(target)?obj.style.display='none':null;
    target==obj2?obj.style.display='block':null;
}
function checkParent(t){
    while(t.parentNode){
        if(t==document.getElementById('resultsPanel')){
            return false
        }
    t=t.parentNode
    }
    return true
}  


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();








