/*
 *    AUTHOR:      Andrew M King {andrewking@WEtap.com}
 *
 *    DESCRIPTION: This JS file has the responsibility of fetching 
 *                 the search criteria entered, validating it and 
 *                 redirecting the browser to the search screen.
 *
 *    ASSUMPTIONS: It is assumed that this js file will be used on
 *                 a web page with a primary form named "form". It
 *                 is further assumed that the search handler url 
 *                 is "EntryPoint"...
 */

/*
    This is the entry search function. Calling it will verify we have
    a form to process. Upon successful verification, reference to form
    will be passed to the search_form function.
*/
function search(){
    var form = null;
    
    //get a handle to the page's form
    if(document.SearchForm){
        search_form(document.SearchForm);
    }else{
        alert("There was an error processing your request. Please try again later.");
    }
}

/*
    This is the entry search function. Calling it will verify we have
    a form to process. Upon successful verification, reference to form
    will be passed to the second_search_form function. It's for the search
    in the search result page.
*/
function second_search(){
    var form = null;
    
    //get a handle to the page's form
    if(document.SecondSearchForm){
        search_form(document.SecondSearchForm);
    }else{
        alert("There was an error processing your request. Please try again later.");
    }
}

/*
    This function performs the bulk of client side processing for searches.
    Upon successful validation of search field, it posts data to the
    search handler.
*/
function search_form(form){
    if(search_validate(form)){
        search_submit(form);
    }
}

/*
    This function posts the data to the search handler.
    We add "&ch=" to switch to the Home channel when displaying the search results
*/
function search_submit(form){
    var goURL;
    goURL = "EntryPoint?viewtype=search&action=get_search&page=1&sort=relevance&searchtext=" + escape(form.searchText.value) + "&advflag=0&ch=";
    window.location = goURL;
}

/*
    This function performs search validation to make sure that 
    keywords are present.
*/
function search_validate(form){
    var returnVar = false;
    var searchText = form.searchText.value;
    if(searchText.length > 0){
        returnVar = true;
    }else{
        alert("Please enter your search keywords to continue.");
        form.searchText.focus();
    }
    return returnVar;
}

function advancedsearch(){
    var form = null;
    
    //get a handle to the page's form
    if(document.AdvancedSearchForm){
        advanced_search_form(document.AdvancedSearchForm);
    }else{
        alert("There was an error processing your request. Please try again later.");
    }
}

/*
    This function performs the bulk of client side processing for searches.
    Upon successful validation of search field, it posts data to the
    search handler.
*/
function advanced_search_form(form){
    if(search_validate(form)){
        advanced_search_submit(form);
    }
}

/*
    This function posts the data to the search handler.
    We add "&ch=" to switch to the Home channel when displaying the search results
*/
function advanced_search_submit(form){
    var goURL;
    goURL = "EntryPoint?viewtype=search&action=get_search&page=1&sort=relevance&searchtext=" + escape(form.searchText.value) +
            "&advflag=1" + 
            "&query=" + form.query.options[form.query.selectedIndex].value +
            "&daterange=" + form.daterange.options[form.daterange.selectedIndex].value +
            "&contenttype=" + form.contenttype.options[form.contenttype.selectedIndex].value +
            "&channel=" + form.channel.options[form.channel.selectedIndex].value +
            "&ch=";
    window.location = goURL;
}

/*
    This function saves the advanced search prefernece
*/
function savepreference(){
    var goURL;
    goURL = "EntryPoint?action=set_pagesize&pagesize=" +
            document.PreferenceForm.pagesize.options[document.PreferenceForm.pagesize.selectedIndex].value;
    alert("Your preference has been saved.");
    window.location = goURL;
}

