/*
* Developed by: Azeus Systems Limited 
*
* Copyright (c) 2008 Azeus Systems Limited
* All rights reserved.
*/

bModified = false;
bShouldCheckModified = true;

var ViewPort = new Object();

function getScreenWidth() {
  return window.screen.availWidth - 5;
}
  
function getScreenHeight() {
  return window.screen.availHeight - 30;
}

ViewPort.getScreenWidth = getScreenWidth;
ViewPort.getScreenHeight = getScreenHeight;
ViewPort.getWindowLeft = getScreenLeft;
ViewPort.getWindowTop = getScreenTop;
ViewPort.getChildLeft = getChildLeft;
ViewPort.getChildTop = getChildTop;
ViewPort.getContentHeight = getContentHeight;

function launchExternal(url, name) {
  window.open(url, name, 'top=' + window.screen.availTop + ', left=' + window.screen.availLeft + ', width=' + ViewPort.getScreenWidth() + ',height=' + ViewPort.getScreenHeight());
}

window.onload = function() {
  try {
    document.execCommand('BackgroundImageCache', false, true)
  } catch (e) {
  }

  if (!window.opener) {
      var width = ViewPort.getScreenWidth();
      var height = ViewPort.getScreenHeight();
	  var dimensions = 'width=' + width + ', height=' + height;
	  var position = ',left=' + window.screen.availLeft + ', top=' + window.screen.availTop;
      var newWindow = window.open(window.location.href, 'bookstore', 'toolbars=false, scrollbars=yes, resizable=yes, status=false,' + dimensions + position);
	  window.opener = newWindow;
	  hideProgress()
	  window.close() 
  }
  
  setupButton("exit", null, null, exitApplication);
  setupLinkHovers();
  hideProgress();
}

function setupLinkHovers() {
  var links = document.getElementsByTagName("a");
  for (e in links) {
    links[e].onmouseover = showStatus
  }
}

function showStatus(e) {
  window.status = this.title
  return true
}

function setupButton(aId, aMouseOverFunction, aMouseOutFunction, aClickFunction)  {
  var button = get(aId)
  if (!button) return
  button.onmouseover = aMouseOverFunction
  button.onmouseout = aMouseOutFunction
  button.onclick = aClickFunction
}
  
function toggleDisplay(element, visible) {
  element.style.display = (visible ? "" : "none")
}

function toggleFields(fields, visible) {
  for (var i = 0; i < fields.length; i++) {
    toggleDisplay(document.get(fields[i]), visible)
  }
}

function hideFields(fields) {
  toggleFields(fields, false)
}

function showResults() {
  setCookie("show_results", "Y", "", "/")
  window.location.href = "./SOB0901.html"
}

function getContentHeight() {
  var a = document.body.offsetHeight;
  var b = document.body.scrollHeight;
  if (a < b)
    return a;
  return b;
}

function getScreenLeft() {
  if (window.screen.left == undefined)
    return window.screenLeft;
  return window.screen.left;
}

function getScreenTop() {
  if (window.screen.top == undefined)
    return window.screenTop;
  return window.screen.top;
}

function getChildLeft(width) {
  return getScreenLeft() + ((getViewportWidth() - width) / 2);
}

function getChildTop(height) {
  return getScreenTop() + ((getViewportHeight() - height) / 2);
}

function launchHelp(url, windowId) {
  var childX = ViewPort.getChildLeft(800);
  var childY = ViewPort.getChildTop(650);
  window.open(url, windowId || "help", "toolbars=false,width=800,height=700,resizable,scrollbars=yes,left=" + childX + ",top=" + childY);
}

function filterByEnglish(aFormName) {
  showProgress();
  if (document && document.forms[aFormName]) {
    document.forms[aFormName].elements["filterByEnglish"].value = true;
    document.forms[aFormName].submit();
  }
}

function changeLanguage(aLanguage) {
  var form = document.forms["callbackForm"];
  if (!form) return;

  if (!form.elements["language"]) {
    var element = document.createElement("input");
    element.setAttribute("id", "language")
    element.setAttribute("name", "language")
    element.setAttribute("type", "hidden");
    form.appendChild(element);
  }
  
  if (!form.elements["country"]) {
    var element = document.createElement("input");
    element.setAttribute("id", "country")
    element.setAttribute("name", "country")
    element.setAttribute("type", "hidden");
    form.appendChild(element);
  }
  
  var language = form.elements["language"];
  var country = form.elements["country"];
  
  language.value = aLanguage;
  if (aLanguage == 'zh')
    country.value = 'TW';
  else
    country.value = '';
    
  try {
    if (confirmLoseChanges()) {
      showProgress();
      form.submit();
      return;
    }
  } catch (e) {
  }
  hideProgress();
}

function doBack() {
  window.history.go(-1);
}

function log(aMsg) {
  if (!loggingEnabled)
    return;

  var logBox = document.get("log");
  if (logBox)
    logBox.innerHTML += "<br />[" + aMsg + "]";
}

function toggleDropDowns() {
  var bSearchWithin = document.get("search_within").checked;
  
  for (var i = 0; i < dropDowns.length; i++) {
    var name = dropDowns[i];
    var dropDown = document.get(name);
    
    log(name + "[" + dropDown.selectedIndex + "] = " + dropDown.value)
     
    if (bSearchWithin && dropDown.originalSelectedIndex != dropDown.selectedIndex) {
      dropDown.selectedIndex = dropDown.originalSelectedIndex;
      if (dropDowns[i] == "obPubDetailsCatCd")
        updateSubCategory();
    }
    var bShouldDisable = bSearchWithin && dropDown.value || dropDown.length == 1;
    
    // toggle disabled, create hidden
    if (bShouldDisable) {
      freeze(formName, name, dropDown.value);
    } else {
      unFreeze(name);
    }
  }

  // ugly hack - workaround for IE problem where the added options
  // are not immediately registered so selectedIndex does not work as expected
  // added delay so that by the time the option is selected, we are sure
  // that the browser can already recognize it
  window.setTimeout("for (var i = 0; i < dropDowns.length; i++) { $(dropDowns[i]).selectedIndex = $(dropDowns[i]).originalSelectedIndex; }", 100);  
}

function freeze(aFormName, aName, aValue) {
  var element = document.get(aName);
  element.setAttribute("disabled", "disabled");

  // substitute with hidden field
  var hidden = createHidden("temp_" + aName, aName, aValue);
  log("attaching hidden field for " + aName)
  document.forms[aFormName].appendChild(hidden);
}
  
function unFreeze(aName) {
  var element = document.get(aName);
  if (element)
    element.removeAttribute("disabled");

  var child = document.get("temp_" + aName)
  if (child) {
    var parent = child.parentNode;
    log("removing hidden field for " + aName)
    parent.removeChild(child);
  }
}

function createHidden(aId, aName, aValue) {
  var hidden = document.createElement("input");
  hidden.setAttribute("type", "hidden");
  hidden.setAttribute("value", aValue);
  hidden.setAttribute("id", aId);
  hidden.setAttribute("name", aName);
  return hidden;
}

function signalChange(e) {
  if (
    (this.tagName == "INPUT" || this.tagName == "TEXTAREA") &&
    (this.value != this.defaultValue || this.checked != this.defaultChecked)
  )
    bModified = true;
  else if (this.tagName == "SELECT" && this[this.selectedIndex].selected != this[this.selectedIndex].defaultSelected)
    bModified = true;
  else
    bModified = false;
}

function monitorChanges(aElements) {
  if (!aElements)
    return;

  for (var i = 0; i < aElements.length; i++) {
    var element = $(aElements[i]);
    if (element)
      element.onchange = signalChange;
  }
}

function launchGuidelines(aUrl) {
  window.open(aUrl, 'guidelines', 'scrollbars=yes, addressbar=no, resizable, height=405, width=540, left=' + ViewPort.getChildLeft(540) + ', top=' + ViewPort.getChildTop(365));
}


function doSubmit(aName) {
  bShouldCheckModified = false;

  var form = document.forms[aName];
  showProgress();
  form.submit();

  bShouldCheckModified = true;
  return true;
}

function followLink() {
  if (bModified && bShouldCheckModified) {
    if (confirmLoseChanges()) {
      showProgress();
      return true;
    } else {
      hideProgress();
      return false;
    }
  }
  showProgress();
  return true;
}

        
 function isModified() {
   return bModified;
 }
 
function doLogin() {
  if (!confirmLoseChanges())
    return false;
  var form = document.forms["loginForm"];
  showProgress();
  form.submit();
  return true;
}

function goUrl(url, showProgress) {
	if(showProgress == null || showProgress) {
		if(followLink()) {
			window.location = url;
		}
	} else {
		if (bModified && bShouldCheckModified) {
		    if (confirmLoseChanges()) {
		      window.location = url;
		    } else {
		      window.location = url;
		    }
		}
		window.location = url;
	}
}
