String.prototype.trim = function() { return this.replace(/(?:^\s+|\s+$)/g, ""); }

window.onresize = moveLoadingScreen;
window.onscroll = moveLoadingScreen;

function fetchLowestPrice(bookID) {
// Remove string token from the beginning of the variable
  bookID = bookID.substr(1, bookID.length - 1);

  if(bookID != undefined) {
    var divToUpdate = document.getElementById('results-' + bookID);
	var comparisonType = 1;
	if(arguments.length > 2 && arguments[2]) comparisonType = 2;

    if(divToUpdate.innerHTML == '' || divToUpdate.innerHTML == '<img src="images/ajax-loading.gif">') ajax_loadContent('results-' + bookID, 'compare.php?id=' + bookID + '&t=' + comparisonType, '<img src="images/ajax-loading.gif">');
    else {
	  if(divToUpdate.style.display == 'block' || divToUpdate.style.display == '') divToUpdate.style.display = 'none';
      else divToUpdate.style.display = 'block';
    }
  }
}

function fetchPriceForSale(bookID) {
// Remove string token from the beginning of the variable
  bookID = bookID.substr(1, bookID.length - 1);

  if(bookID != undefined) {
    var divToUpdate = document.getElementById('results-' + bookID);
	var comparisonType = 1;
	if(arguments.length > 2 && arguments[2]) comparisonType = 2;

    if(divToUpdate.innerHTML == '' || divToUpdate.innerHTML == '<img src="images/ajax-loading.gif">') ajax_loadContent('results-' + bookID, 'compare_sell.php?id=' + bookID + '&t=' + comparisonType, '<img src="images/ajax-loading.gif">');
    else {
	  if(divToUpdate.style.display == 'block' || divToUpdate.style.display == '') divToUpdate.style.display = 'none';
      else divToUpdate.style.display = 'block';
    }
  }
}


function showLoadingScreen(textToShow) {
// Replace the text so that it shows the proper text
  if(textToShow == undefined) textToShow = 'Search';

  var textToSearch = document.getElementById('loading').innerHTML;
  textToSearch = textToSearch.replace(/SEARCH_WORD/g, textToShow);
  textToSearch = textToSearch.replace(/Search/g, textToShow);
  textToSearch = textToSearch.replace(/Comparison/g, textToShow);
  document.getElementById('loading').innerHTML = textToSearch;

  var loadingScreen = document.getElementById('loading');
  var sizeArray = getPageSize();
  var scrollArray = getPageScroll();
  
// The default size of the loading screen is 200 x 100 pixels
  var top = (scrollArray[1] + ((sizeArray[3] - 35 - 80) / 2) + 'px');;
  var left = (((sizeArray[0] - 20 - 200) / 2) + 'px');
  
  if(left < 0) left = 0;
  if(top < 0) top = 0;
  
// Position the loading screen at the center of the screen
  loadingScreen.style.top = top;
  loadingScreen.style.left = left;

// Show the loading screen
  loadingScreen.style.display = 'block';
  loadingScreen.style.visibility = 'visible';
}

function hideLoadingScreen()
{
  var loadingScreen = document.getElementById('loading');
  loadingScreen.style.visibility = 'hidden';
  loadingScreen.style.display = 'none';
}

function moveLoadingScreen()
{
	var loadingScreen = document.getElementById('loading');
	if(loadingScreen != undefined && loadingScreen.style.visibility == 'visible')
	{
	  var sizeArray = getPageSize();
	  var scrollArray = getPageScroll();
	  
	// The default size of the loading screen is 200 x 100 pixels
	  var top = (scrollArray[1] + ((sizeArray[3] - 35 - 80) / 2) + 'px');;
	  var left = (((sizeArray[0] - 20 - 200) / 2) + 'px');
	  
	  if(left < 0) left = 0;
	  if(top < 0) top = 0;
	  
	// Position the loading screen at the center of the screen
	  loadingScreen.style.top = top;
	  loadingScreen.style.left = left;
	}
}

function clearPreloadPage()
{
  if(document.getElementById) // DOM
  {
    document.getElementById('loading').style.visibility = 'hidden';
  }

  else
  {
    if(document.layers) //NS4
    {
      document.loading.visibility = 'hidden';
    }

    else //IE4
    {
      document.all.loading.style.visibility = 'hidden';
    }
  }
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Taken from Lokesh Dhakar - http://www.huddletogether.com (Lightbox project)
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Taken from Lokesh Dhakar - http://www.huddletogether.com (Lightbox project)
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function checkAdvancedSearchForm()
{
// If checking an ID is impossible, return true and validate server side.
	if(!document.getElementById('page')) return true;
	if(document.getElementById('BookTitle').value.trim() != '') return true;
	if(document.getElementById('BookAuthor').value.trim() != '') return true;
	if(document.getElementById('BookSubject').value.trim() != '') return true;
	if(document.getElementById('BookKeyword').value.trim() != '') return true;
	
	return false;
}