/*
 * This function is responsible for the article pagination.
 */
function gotoPage(noOfDivs,pageNum) {
	for (var j=1;j<=noOfDivs;j++) {
		document.getElementById('articleBody'+j).style.display = 'none';
		document.getElementById('pageNo'+j).className = 'notSelectedPage';
	}
	
	document.getElementById('articleBody'+pageNum).style.display = 'block';
	document.getElementById('pageNo'+pageNum).className = 'selectedPage';
	
	return;
}

/*
 *  Function:   replaceHtml
 *
 *  Purpose:    Used by ajax pagination so that the contents of the div can be 
 *              replaced by new html returned dynamically. An ajax query is made
 *              to retrieve the html for a single page element and then this html
 *              is rendered on the page without a reload taking place. This method
 *              uses jquery but you dont need to import the library because that
 *              is handled by the PageElmentController.
 *
 *              The method should now handle query strings as part of the URL. In
 *              the original version, all query parameters for a GET request would
 *              be cut of the URL of the Ajax request being sent.
 *
 *  Parameters: div - the id of the div that you are going to replace
 *              siteId - the site id
 *              pageElementTypeId - the type id of the page element that you need new html for
 *              pageElementInstanceId - the instanceid of the page element that you are replacing
 *              pageNumber - the page number that you require
 *              detailId - the detail id to pass on to the server. If null the current page detail id will be used.
 *              extraDetailIds - the extra detail ids to pass on to the server. If null the curent page extra detail ids will be used. 
 *
 *  Returns:    Nothing
 */
function replaceHtml(div, siteId, pageElementTypeId, pageElementInstanceId, pageNumber, detailId, extraDetailIds) {
  
  //
  // If the detailId has not been specified, 
  // use the global ones for the page.
  // 
  if (!detailId && 
       window.GLOBAL_DETAIL_ID && 
       window.GLOBAL_DETAIL_ID > 0) {
    detailId = window.GLOBAL_DETAIL_ID;
  }
  
  //
  // If the extra ids have not been specified,
  // use the global ones for the page.
  //
  if (!extraDetailIds &&
      window.GLOBAL_EXTRA_IDS &&
      window.GLOBAL_EXTRA_IDS.length > 0) {
    extraDetailIds = window.GLOBAL_EXTRA_IDS;
  }
  
  //
  // url for the ajax request to get the new page.
  //
  var url = "/page/elements/html" + (window.GLOBAL_PAGE_BASE_PATH?GLOBAL_PAGE_BASE_PATH:"") + 
            "/0,," + siteId + "~" + pageElementTypeId + "~" + pageElementInstanceId + 
            ((detailId && detailId > 0)?("~" + detailId):((extraDetailIds && extraDetailIds.length>0)?"~0":""));
            
  if (extraDetailIds && extraDetailIds.length > 0) {
    for (var i=0; i<extraDetailIds.length; i++) {
      url = url + "~" + extraDetailIds[i];
    }
  }          
  url = url + "," + pageNumber + ",00.html" + document.location.search; 
  
  //
  // Make ajax call.
  //
  $.ajax({
    url: url,
    cache: false,
    success: function(html){
      $("#" + div).replaceWith(html);
    }
  });
  
  return;
}