// Global vars
var LastButtonClicked;
var focusFld;
var lastVal;
var subtotal, subTotalEl;
var subqty;
var total, totalEl;
var shipping, shipEl;
var tax, taxEl;

function recLastBtn(e) {
  LastButtonClicked = e;
}

function filterEnterKey(e) {
  var keynum;

  if(window.event) {
    keynum = e.keyCode; // IE
  } else if(e.which) {
    keynum = e.which; // Netscape/Firefox/Opera
  }
  if( keynum != 13) return true;
  return false;
}

function getURL() {
  return(unescape(document.URL));
}

function getQuery() {
  retstr = "";
  fullURL = document.URL;
  if (fullURL.indexOf('?') > 0) {
    retstr = fullURL.substring(fullURL.indexOf('?')+1, fullURL.length);
    retstr = unescape(retstr);
  }
  return (retstr);
}

function resizeContainer(objid, w, h) {
  el = document.getElementById(objid);
  if( el) {
    el.height = h; el.width = w;
    el.resizedContainer();
  }
}

function menuCatHdrClick(menu_id, open_cat_id) {
  window.location = "/home.php?C"+open_cat_id;
}

function menuSubCatClick(menu_id, open_cat_id) {
  window.location = "/home.php?C"+open_cat_id;
}

/*function initFocusFields() {
  focusFld = ''; lastVal=0;
  subTotalEl = document.getElementById('sub_total');
  if( !subTotalEl) { alert('The subtotal field was not found!'); }
  subQtyEl = document.getElementById('sub_qty');
  if( !subQtyEl) { alert('The total quantity field was not found!'); }
  shipEl = document.getElementById('shipping');
  if( !shipEl) { alert('The shipping field was not found!'); }
  taxEl = document.getElementById('sales_tax');
  if( !taxEl) { alert('The sales tax field was not found!'); }
  totalEl = document.getElementById('total');
  if( !totalEl) { alert('The Total field was not found!'); }
  subtotal = 0; subqty = 0; total=0; tax=0; shipping=0;
  subTotalEl.innerHTML = subtotal;
  subQtyEl.innerHTML = subqty;
  // Set focus to the first field.
  var el = document.getElementById('G1014');
  if( el) el.focus();
}*/

/* function calcShipping() {
  if( subqty < 1) {
    shipping = 0;
  } else {
    if( subqty == 1) {
      shipping = 10;
    } else {
      shipping = 10 + (subqty - 1) * 5;
    }
  }
  shipEl.innerHTML = shipping.toFixed(2);
}*/

/* function calcTax() {
  var el = document.getElementById('BillState');
  var el2 = document.getElementById('ShipState');
  if( el.value == 'CA' || el2.value == 'CA') {
    tax = subtotal * 0.0725;
  } else tax = 0.00;
  taxEl.innerHTML = tax.toFixed(2);
}*/

/* function calcTotal() {
  calcShipping();
  calcTax();
  total = 0 + subtotal + tax + shipping;
  totalEl.innerHTML = total.toFixed(2);
}*/

function recFocus(e) {
  focusFld = e;
  lastVal = e.value;
}

// This allows the php program to process a text link as
// though it were a button.
// form1 must have a hidden field called hcmd for this to work!
//
function backToCameFrom() {
  var cmdfld = document.getElementById('hcmd');
  if( !cmdfld) return(false);
  cmdfld.value = "backtocamefrom";
  var el = document.getElementById('form1');
  if( el) el.submit();
}

// val is the value to assign to hcmd before the post.
function camefrom_jump(val) {
  var cmdfld = document.getElementById('hcmd');
  if( !cmdfld) return(false);
  cmdfld.value = val;
  var el = document.getElementById('form1');
  if( el) el.submit();
  cmdfld.value = '';
}

function validateUSZip( strValue ) {
  var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  return objRegExp.test(strValue);
}

function validateUSPhone(fld) {
  var FmtStr="", index = 0;
  var phonenumber = trim(fld.value);
  var maxchars = phonenumber.length;
  while (index != maxchars) {
    if (isNaN(parseInt(phonenumber.charAt(index))))
      { }
    else
      { FmtStr = FmtStr + phonenumber.charAt(index); }
    index = index + 1;
  }
  if (FmtStr.length == 10) {
    FmtStr = FmtStr.substring(0,3) + "-" + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
    fld.value = FmtStr;;
    return true;
  }
  return false;
}

function validate_email(field) {
  with (field) {
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    if (apos<1 || dotpos-apos<2) {
      alert('Invalid email address.'); return false;
    }
    if( value.length - dotpos < 3) {
      alert('Invalid email address!'); return false;
    }
    if( value.indexOf(",") > 0) {
      alert('Invalid email address,'); return false;
    }
    return true;
  }
}

function intChanged(e) {
  e.value = trim(e.value,'');
  var n = parseInt(e.value);
  if( isNaN(n)) n = 0;
  if( n <= 0) {
    e.value = '0';
  } else e.value = n;
}

function floatChanged(e) {
  e.value = trim(e.value,'');
  var n = parseFloat(e.value);
  n = n.toFixed(2);
  if( isNaN(n)) n = 0;
  if( e.maxLength == 4) {
    if( n > 9.99) n = 9.99;
  } else if( e.maxLength == 5) {
    if( n > 99.99) n = 99.99;
  } else if( e.maxLength == 6) {
    if( n > 999.99) n = 999.99;
  } else if( e.maxLength == 7) {
    if( n > 9999.99) n = 9999.99;
  } else if( e.maxLength == 8) {
    if( n > 99999.99) n = 99999.99;
  } else if( e.maxLength == 9) {
    if( n > 999999.99) n = 999999.99;
  }
  if( n <= 0) {
    e.value = '0.00';
  } else e.value = n;
}

function dateChanged(e) {
  e.value = trim(e.value,'');
  if( e.value == '') return;
  var s = ''; c = '';
  var lstch = '';
  var n = e.value.charCodeAt(0);
  var q = 0;
  if( n >= 48 && n <= 57) {
    // It starts with a number so should be something like 12/01/2008
    for( var j=0; j < e.value.length; j++) {
      n = e.value.charCodeAt(j);
      c = e.value.substr(j,1);
      if( n >= 48 && n <= 57) {
        s = s.concat(c); lstch = e.value[j];
      } else {
        if( lstch != ' ') {
          s = s.concat(" "); q++;
        }
        lstch = ' ';
      }
    }
    s = trim(s,'');
    if( q < 2) {
      var now = new Date();
      s = s + " " + now.getFullYear();
    }
    ax = s.split(" ");
    if( ax.length == 3) {
      if( ax[2].length == 2)  ax[2] = '20' + ax[2];
      s = ax[0]+'/'+ax[1]+'/'+ax[2];
    }
  } else {
    s = e.value; // Might be something like May 23 2008
    ax = s.split(" ");
    if( ax.length == 3) {
      if( ax[2].length == 2) {
        ax[2] = '20' + ax[2];
        s = ax[0]+' '+ax[1]+' '+ax[2];
      }
    }
  }
  n = Date.parse(s);
  if( isNaN(n)) {
    // Might be missing the year
    var now = new Date();
    s = s + " " + now.getFullYear();
    n = Date.parse(s);
  }
  if((n)) {
    var dt = new Date(n);
    day = dt.getDate(); if( day < 10) day = '0' + day;
    mth = dt.getMonth() + 1; if( mth < 10) mth = '0' + mth;
    yr = dt.getFullYear();
    if( yr < 1000) {
      e.value = OrgValue; return;
    }
    e.value = mth+'/'+day+'/'+yr;
  } else {
    e.value = OrgValue;
  }
}

function scrollToTop() {
  window.scrollTo(0,0);
}

function startTimer(millisecs) {
  var el = document.getElementById("btn_submit");
  if( el) setTimeout('enableSubmit()', millisecs);
}

function enableSubmit() {
  var el = document.getElementById("btn_submit");
  if( el) el.disabled = false;
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
 chars = chars || "\\s";
 return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// This function returns true if there are any characters in (inChars)
// that are not legal. Returns false if all chars are OK.
function NotLegalChars(inChars, legalChars) {
  var i;
  if( inChars.length < 1 ) return false;
  for( i=0; i < inChars.length; i++) {
    if( legalChars.indexOf(inChars.charAt(i)) == -1) return true;
  }
  return false;
}
function NotAnyLegal(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%&()_-+=\/,.<>:;");
}
function NotAlphaNumPunct(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789., #&()-/:");
}
function NotAlphaNumeric(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}
function NotAlphaSpace(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
function NotDigits(inChars) {
  return NotLegalChars(inChars, "0123456789");
}

/*
////////////////////////////////////////////
// This function traverses the DOM tree of an element and prints the tree.  
// This function called recursively until the DOM tree is fully traversed.
// 
// Parameters:
// - targetDocument is where the tree will be printed into
// - currentElement is the element that we want to print
// - depth is the depth of the current element 
//   (it should be 1 for the initial element)
////////////////////////////////////////////
function traverseDOMTree(currentElement, depth)
{
  if (currentElement) {
    var j;
    var tagName=currentElement.tagName;
    // Traverse the tree
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild) {
      // Recursively traverse the tree structure of the child node
      traverseDOMTree(currentElementChild, depth+1);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
    if (tagName) {
      alert(tagName+' '+currentElement.height);
    }
  }
}*/


function showPopMsg(hdln, msg) {
  var el = document.getElementById('popfrm');
  if( el) {
    if( hdln != '') {
      var hd = document.getElementById('pophd');
      if( hd) hd.innerHTML = hdln;
    }
    var hd = document.getElementById('popmsg');
    if( hd) hd.innerHTML = msg;
    el.style.visibility = "visible";
  }
}

function showItem(elname) {
  var el = document.getElementById(elname);
  if( el) {
    el.style.visibility = "visible";
    el.style.display = "inline";
  }
}

function hideItem(elname) {
  var el = document.getElementById(elname);
  if( el) {
    el.style.visibility = "hidden";
    el.style.display = "none";
  }
}

function hideMe(el) {
  if( el) el.style.visibility = "hidden";
}

// Returns true if (s) does not contain any of the chars in charSet
function AllLegalChars(s, charSet) {
  if( s.length < 1 || charSet.length < 1) return true;
  for( var i=0; i < charSet.length; i++) {
    if( s.indexOf(charSet.charAt(i)) >= 0) return false;
  }
  return true;
}

function imageSubmit(e) {
  var el = document.getElementById('hcmd');
  if( el) {
    el.value = e.id;
    el = document.getElementById('form1');
    if( el) el.submit();
  }
}

// Set isDelete to 1 to ask the delete message.
function imageConfirm(e, isDelete) {
  msg = 'Are you sure?';
  if( isDelete == 1) {
    msg = 'OK to DELETE this?';
  }
  if( !window.confirm(msg)) return;
  var el = document.getElementById('hcmd');
  if( el) {
    el.value = e.id;
    el = document.getElementById('form1');
    if( el) el.submit();
  }
}

function ConfirmSubmit(e,msg) {
  if( msg == '') msg="Are you sure?";
  if( e) e.style.visibility = "hidden";
  v = window.confirm(msg);
  if( !v ) {
    if( e) e.style.visibility = "visible";
    return false;
  }
  return true;
}

function audioError(objid, msg) {
  el = document.getElementById(objid);
  //alert(objid+" "+msg);
  return 0;
}

function royal_vidFunc(objid, msg) {
  el = document.getElementById(objid);
  alert(objid+" "+msg);
  return 0;
}

function searchKeyPress(e) {
  var keynum;

  if(window.event) {
    keynum = e.keyCode; // IE
  } else if(e.which) {
    keynum = e.which; // Netscape/Firefox/Opera
  }
  if( keynum != 13) return true;
  doSearch();
  return true;
}

function doSearch() {
  var el = document.getElementById('search_str');
  if( el != '') {
    el.value = trim(el.value);
    if( el.value == '') return;
    window.location = "/search.php?"+el.value;
  }
}

function CountSelectedQty(selObj) {
  var count = 0;
  for (var i=0; i < selObj.options.length; i++) {
    if (selObj.options[i].selected) count++;
  }
  return count;
}

function GetSelectedArray(selObj) {
  var selectedArray = new Array();
  var count = 0;
  for (var i=0; i < selObj.options.length; i++) {
    if (selObj.options[i].selected) {
      selectedArray[count] = selObj.options[i].value;
      count++;
    }
  }
  // alert(selectedArray.length);
  return selectedArray;
}


