/*
 *    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.");
    }
}

function search_top(){
    var form = null;
    
    //get a handle to the page's form
    if(document.SearchForm1){
        search_form_public(document.SearchForm1);
    }else{
        alert("There was an error processing your request. Please try again later.");
    }
}

function search_bottom(){
    var form = null;
    
    //get a handle to the page's form
    if(document.SearchForm2){
        search_form_public(document.SearchForm2);
    }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.
    we don't validate the search text here for the Public site.
*/
function search_form_public(form){
    var searchText = form.searchText.value;
    if(searchText.length > 0){
        search_submit(form);
    }else{
      form.submit();
      var goURL;
      var ch = form.ch.value;
      var pagesize = form.pagesize.value;
      goURL = "EntryPoint?action=set_pagesize&ch=" + ch + "&pagesize=" + pagesize;
      window.location = goURL;
    }
}

/*
    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;
}
