pagination_links = new Array ();
total_num_pages = 0;
current_page = 0;
/*
 Function PerformSearch()

 Main function when submitting the Advanced Search Form.
 This function will retrieve the search results by making a Asynchronous HTTP request to advanced_search_results.html .
 By setting form param 'notemplate' to 1 advanced_search_results.html will only not display the template.
 'Form.serialize()' is being used to create the form parameters from the search for and send it to advanced_search_results.html.

 After the HTTP Request is completed the 'SearchResultsDiv' div will be updated with the results.

 While performing the HTTP Request
 this function also display the 'indicator' animated gif by setting the 'indicator' div tag to visible and
 hide the 'Search' link by making the 'searchLink' Div invisible.
 After the HTTP Request is completed 'indicator' div is set to invisible while the 'searchLink' is made visible again.
*/
function PerformSearch(loadingTxt, page)
{
  if(page) {
    $("pageInput").value = page;
  }
  var data = Form.serialize('SearchForm');
  $('SearchResultsDiv').innerHTML = "<img src=/pics/indicator.gif> <b>" + loadingTxt + "...</b>";
  new Ajax.Updater('SearchResultsDiv', 'advanced_search_results.html', {
		     evalScripts: true,
		     method: 'post',
		     onComplete: function(request) {
		       Effect.expand(this, '100%');
		       setPaginationLinks(current_page, total_num_pages);
                     },
                     parameters: data + '&notemplate=1',
                     asynchronous:true
                   });
  window.location.hash = data;
}


/*
  Function: loadProducts()

  Input:
      divid - The div ID of the link being clicked. By having this ID we can create a 'Pulsating' effect to that link.
              Also after the HTTP request is complete we will modify the style for this ID making the link bold , bigger font and change the color of the text.
      totalNumPages - this is an integer, the total num of pages for this search.  We need this to reset the style for all links IDs.
      list - this is the list of products that we will pass to advanced_search_results.html

  Description:
      This function is used in the pagination links.
      Each Page link will request a Asynchronous HTTP Request to advanced_search_results.html passing a list of products.
      advanced_search_results.html will return the product details for those products only.
      The 'coreResults' div is updated with the new results while the pagination links does not change.

*/
function loadProducts(page, linkId, totalNumPages, list, loadingTxt)
{
  setPaginationLinks(page, totalNumPages);
  $('coreResults').innerHTML = "<div style=" + '"text-align: center">' +  "<img src=/pics/indicator.gif> <b>" + loadingTxt + " ...</b></div>";
  new Ajax.Updater('coreResults', 'advanced_search_results.html',
		   {
                     method: 'post',
                     onLoading: function(request){
                       new Effect.Pulsate(linkId);
                       new Effect.Pulsate(linkId + 'Bottom');
                     },

                     onComplete: function(request){
		       $(linkId).addClassName('searchPageLinkSelected')
		       $(linkId + 'Bottom').addClassName('searchPageLinkSelected')
		       $("pageInput").value = current_page;
		       window.location.hash = Form.serialize('SearchForm');
                     },

                     parameters: 'product_list=' + list + '&notemplate=1',
                     asynchronous:true
                   });
}

function setPaginationLinks(page, totalNumPages)
{
  new Effect.ScrollTo('SearchResultsDiv');
  current_page = page;
  if(totalNumPages <= 1) return;
  // if we are on the first page let's not show the previous link.
  if (page == 1) {
    $('previousPageLink').style.display='none';
    $('previousPageLinkBottom').style.display= 'none';
  } else {
    if ($('previousPageLink').style.display == 'none') {
      $('previousPageLink').style.display='inline';
    }
    if ($('previousPageLinkBottom').style.display == 'none') {
      $('previousPageLinkBottom').style.display='inline';
    }
  }

  // if we are on the last page let's not show the next link.
  if (page == total_num_pages) {
    $('nextPageLink').style.display='none';
    $('nextPageLinkBottom').style.display= 'none';
  } else {
    if ($('nextPageLink').style.display == 'none') {
      $('nextPageLink').style.display='inline';
    }
    if ($('nextPageLinkBottom').style.display == 'none') {
      $('nextPageLinkBottom').style.display='inline';
    }
  }

  // reset the style for each paginationlinks
  for (i=1; i<= totalNumPages; i++) {
    if(i == page) {
      $('page' + i + 'Link').addClassName('searchPageLinkSelected')
      $('page' + i + 'LinkBottom').addClassName('searchPageLinkSelected')
    } else {
      $('page' + i + 'Link').removeClassName('searchPageLinkSelected')
      $('page' + i + 'LinkBottom').removeClassName('searchPageLinkSelected')
    }
  }
}

function nextPage(loadingTxt)
{
  var next_page = new Number(current_page) + 1;
  if (next_page <= total_num_pages) {
    current_page = next_page;
  } else {
    current_page = 1;
    next_page = 1;
  }
  loadProducts(next_page, 'page'+ next_page + 'Link', total_num_pages, pagination_links[next_page-1], loadingTxt);
}

function previousPage(loadingTxt)
{
  var previous_page = new Number(current_page) - 1;
  if (previous_page <= 0) {
    current_page = total_num_pages;
    previous_page = total_num_pages;
  } else {
    current_page = previous_page;
  }
  loadProducts(previous_page, 'page'+ previous_page + 'Link', total_num_pages, pagination_links[previous_page-1], loadingTxt);
}

function resetForm()
{
  $('searchFormKeywords').value = '';
  $('searchFormPriceRange').selectedIndex = 0;
  $('searchFormColor').selectedIndex = 0;
  $('searchFormCategory').selectedIndex = 0;

/*
  for (var input in Form.getElements($(form))) {
    alert(input.name);
    input.value='';
  }
  for (var input in Form.getInputs($(form), 'select')) {
    input.selectedIndex=0;
  }*/
}