/**
 * Ampdraw - functions.js
 *
 * Javascript functions to support the site.
 *
 * @author     Shane Archer <futureal@rctech.net>
 * @copyright  2005-2007 RC Tech, LLC
 * @version    svn: $Revision:$ $Date:$
 * @package    AmpdrawPublic
 */

/** @var boolean True if we are currently viewing an image in the Javascript "pop-up" window. */
var imageOpen = false;

/** @var boolean Set to false if and only if there is no current AJAX object. */
var http_request = false;

/**
 * Create the global AJAX calling object.
 */
function newAjaxCall() {

  http_request = false;

  if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
  }

}

/**
 * Initiate an inventory search.
 */
function doInventorySearch() {

  var searchKeywords = encodeURIComponent(document.getElementById("searchTitle").value);
  var searchPartNumber = encodeURIComponent(document.getElementById("searchPartNumber").value);
  var searchCategory = document.getElementById("searchCategory").value;
  var searchManufacturer = document.getElementById("searchManufacturer").value;
  var searchMinPrice = document.getElementById("searchMinPrice").value;
  var searchMaxPrice = document.getElementById("searchMaxPrice").value;

  var urlString = "/browse.php?type=search";

  // check to see if we need to include session in any links
  if (sId != "") {
    var sessionString = "&s=" + sId;
  } else {
    var sessionString = "";
  }

  urlString = urlString + assembleTerm("title",searchKeywords,false);
  urlString = urlString + assembleTerm("partnumber",searchPartNumber,false);
  urlString = urlString + assembleTerm("category",searchCategory,true);
  urlString = urlString + assembleTerm("manufacturer",searchManufacturer,true);
  urlString = urlString + assembleTerm("minprice",searchMinPrice,true);
  urlString = urlString + assembleTerm("maxprice",searchMaxPrice,true);

  pageRedirect(urlString+sessionString);

}

/**
 * Create the request string name/value pair for a variable,
 * but leave it blank if the data is "empty" by all accounts.
 */
function assembleTerm(id,data,numeric) {

  if (numeric) {

    if (data > 0)
      return "&" + id + "=" + data;

  } else {

    if (data != "")
      return "&" + id + "=" + data;

  }

  return "";

}

/**
 * Get the X and Y browser scroll offsets.
 */
function getScrollXY() {

  var scrOfX = 0, scrOfY = 0;

  if( typeof(window.pageYOffset) == "number") {

    // netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;

  } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {

    // DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;

  } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {

    // IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;

  }

  return [ scrOfX, scrOfY ];

}

/**
 * Remove all of an element's children.
 */
function clearElement(element) {

  while (element.hasChildNodes()) {
    element.removeChild(element.firstChild);
  }

}

/**
 * Create an IMG HTML element with the preloader graphic.
 */
function createPreloader(align) {

  var js_preloader = document.createElement("img");
  js_preloader.src = "/graphics/loadingbackground.gif";
  js_preloader.setAttribute("width","20");
  js_preloader.setAttribute("height","20");
  js_preloader.setAttribute("border","0");
  js_preloader.setAttribute("alt","");
  js_preloader.setAttribute("align",align);

  if (align == "center") {
    js_preloader.style.margin = "0 auto 0 auto";
  } else if (align == "right") {
    js_preloader.style.margin = "0 0 0 auto";
  }

  return js_preloader;

}

/**
 * Quick way to validate an integer.
 */
function validateInt(value) {

  // make sure the integer matches the string
  return (("" + parseInt(value)) == value);

}

/**
 * Set the focus to a particular element.
 */
function doFocus(element) {
  document.getElementById(element).focus();
}

/**
 * Do a simple javascript page redirect.
 */
function pageRedirect(page) {
  window.location.href = page;
}

/**
 * Captures the enter key and performs the assigned action.
 */
function captureEnter(field, evt, action) {

  var keyCode = evt.which ? evt.which : evt.keyCode;

  if (keyCode == 13) {
    action();
    return false;
  } else {
    return true;
  }

}

/**
 * Check an input box to make sure it has a value.
 */
function checkBlankInput(idArray,nameArray) {

  var errorList = "";

  for (var i = 0; i < idArray.length; i++) {

    testInput = document.getElementById(idArray[i]);

    if (testInput.tagName != "SELECT") {

      if (testInput.value == "") {

        errorList = errorList + (errorList != "" ? "\n" : "") + "Error: " + nameArray[i] + " is a required field.";

      }

    } else {

      children = testInput.childNodes;
      aSelection = false;

      for (var j = 0; j < children.length; j++) {

        // take zero as an "empty" value
        if (children[j].selected == true && children[j].value != 0) {
          aSelection = true;
        }

      }

      // in this case, nothing in the required select field was selected
      if (aSelection == false) {

        errorList = errorList + (errorList != "" ? "\n" : "") + "Error: You must select at least one option in " + nameArray[i] + ".";

      }

    }

  }

  if (errorList != "") {

    alert(errorList);
    return false;

  } else {

    return true;

  }

}

/**
 * Opens a centered window.
 */
function ampWindow(url,name,width,height) {

  var xPos = (screen.availWidth / 2) - (width / 2);
  var yPos = (screen.availHeight / 2) - (height / 2);

  var options = "width=" + width + ",height=" + height + ",resizeable=0,scrollbars=1,left=" + xPos + ",top=" + yPos;

  window.open(url,name,options);

}
