var dir_images          = 'images/';
var server              = 'http://'+document.domain;
var pathtoxml           = server+'/';
var onClassName         = 'product-list-selected';
var offClassName        = 'product-list';
var highlightClassName  = 'product-list-highlight';
var backgroundOdd       = '#ececec';
var backgroundEven      = '#dfdfdf';
var currentDisplay      = null;
var targetCategory      = null;
var lastDisplayTable    = null;
var req;
///////////////////////////////
////// general functions //////
///////////////////////////////
function reload() {location = location;}
function safemail(name,domain,subject,display,css) {
  css       = (css)     ? ' class="' + css + '"' : '';
  subject   = (subject) ? '?subject=' + subject : '';
  displayed = (display) ? display : name + '@' + domain;
  mailto = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + css + '>' + displayed + '</a>');
}
function Popup(title,filename,w,h,scroll,resizable){if (title == ''){ title = 'popup'; }if (scroll == 'scroll'){ scroll = 'yes'; }else{ scroll = 'no'; }if (resizable == 'lock'){ resizable = 'no'; }else{ resizable = 'yes'; }winOptions = eval("'width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");window.open(filename,title,winOptions);return;}
function SetFocus(fieldname){document.getElementById(fieldname).focus();}
function Init(client) {
  if(document.xmlform) {
    document.xmlform.submit();
  } 
  var client_id = client;
  loaded = true;
}
function FormatAsMoney(mnt) {
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
return (mnt == Math.floor(mnt)) ? mnt + '.00' 
: ( (mnt*10 == Math.floor(mnt*10)) ? 
mnt + '0' : mnt);
}

function AddTotal(productId,value,price) {
  errorStatus = null;
  
  // validate input
  if (!isNaN(value)) {
    if (value < 0) {
      alert('only positive numbers allowed');
      errorStatus = true;
    } else {
    
      // format total cost of item
      document.getElementById('case'+productId).innerHTML = FormatAsMoney(value * price); 
      
      // send order back to db
      OrderSwitch(productId); 
    }
  } else {
    alert ('only positive numbers allowed')
      errorStatus = true;
  }
  if (errorStatus) {
    // clear any bad input
    document.getElementById('case_order'+productId).value = '';
  }
}

///////////////////////////////
///////// dom functions ///////
///////////////////////////////
function HideAddresses() {
  var x=document.getElementsByTagName('BLOCKQUOTE');
  for (i=0; i<x.length; i++) {
    x[i].style.display='none';
  }
}
function ClassSwitchOn(id) {
  target           = document.getElementById(id);
  target.className = highlightClassName;
}
function ClassHighlight(id) {
  if (document.getElementById('case_order'+id)) {
    formcheck = document.getElementById('case_order'+id).value;
    hlight    = (formcheck > 0) ? true : false;
  } else {
    formcheck = document.getElementById('f'+id).value;
    hlight    = (formcheck.checked) ? true : false;
  } 
  target    = document.getElementById('p'+id);
  target.className = (hlight) ? onClassName : offClassName;
}
function AlterColors(id) {
  if (this.className == 'product-list') {
    this.backgroundColor = (this.rowIndex%2) ? backgroundEven : backgroundOdd;
  }
  var targetrow
  var tablelist=document.getElementsByTagName('table');
  for (x=0; x<tablelist.length; x++) {
    var target=document.getElementById(tablelist[x].id)
    var c = 0;
    for (i=0; i<target.rows.length; i++) {
      targetrow = target.rows[i]
      if (targetrow.className == 'product-list') {
        targetrow.style.backgroundColor = (targetrow.rowIndex%2) ? backgroundEven : backgroundOdd;
      } 
    }
  }
}
function OnTestFavorite(id) {
  // restyle row
  document.getElementById('f'+id).style.display = 'none';
  document.getElementById('fv'+id).style.display = 'inline';
  document.getElementById('offfavtext'+id).style.display = 'none';
  document.getElementById('onfavtext'+id).style.display = 'inline';
  document.getElementById('onfavtext'+id).innerHTML = 'Added to favorites <em style="text-decoration:underline; cursor:pointer;">click to remove</em>';

  // record favorites in db
  AddFavorite(id, '');
}
function OffTestFavorite(id) {
  // restyle row
  document.getElementById('fv'+id).style.display = 'none';
  document.getElementById('f'+id).style.display = 'inline';
  document.getElementById('f'+id).checked = false;
  document.getElementById('offfavtext'+id).style.display = 'inline';
  document.getElementById('onfavtext'+id).style.display = 'none';
  document.getElementById('offfavtext'+id).innerHTML = 'Removed from Favorites';
  
  // delete favorite from db
  DeleteFavorite(id, '');
  
  // reset user option to default 
  setTimeout("document.getElementById('offfavtext"+id+"').innerHTML = 'Add to Favorites';", 3000);
}

function progressBar() {
  // used for long page loads, use as 'signposts' in long form
  x = document.getElementById('progressbarDiv');
  current = x.style.width.replace(/[^0-9]+/g,'');
  current = parseFloat(current) + 20;
  x.style.width = current+'px';
}
function OrderSwitch(id) {
  formcheck = document.getElementById('case_order'+id);
  target    = document.getElementById('p'+id);
  target.className  = (formcheck.value > 0) ? onClassName : offClassName;
}
function InsertDetails(product) {
  // call details xml and display within tables
  target=document.getElementById('producttable');
  
  // delete any old display
  old=document.getElementById('prrow');
  if (old) {
    target.deleteRow(old.rowIndex);
  }
  if (currentDisplay != product) {
    // create new table row
    insert=document.getElementById('p'+product).rowIndex;
    x=target.insertRow(insert+1);
    y=x.insertCell(0);
    y.id="prdetails";
    x.id="prrow";
    y.colSpan="5";
    y.className="productdetails";
    
    // get product details from db via xml
    
    ShowDetails(product, '');
    // track the current id displayed
    currentDisplay = product;
  } else {
    currentDisplay = null;
  }
}
function InsertDetailsForm(product, table) {
  // modified function for pages with multiple tables
  oldTable         = document.getElementById(lastDisplayTable);
  lastDisplayTable = table;
  target=document.getElementById(table);
  old=document.getElementById('prrow');
  
  // delete old display
  if (old) {
    oldTable.deleteRow(old.rowIndex);
  }
  if (currentDisplay != product) {
  
    // create new table row
    insert=document.getElementById('p'+product).rowIndex;
    x=target.insertRow(insert+1);
    y=x.insertCell(0);
    y.id="prdetails";
    x.id="prrow";
    y.colSpan="5";
    y.className="productdetails";
    
    // get product details from db via xml
    ShowDetails(product, '');
    
    // track current id displayed
    currentDisplay = product;
  } else {
    currentDisplay = null;
  }
}
function CloseDetails() {
  // delete row
  if (lastDisplayTable) {
    document.getElementById(lastDisplayTable).deleteRow(document.getElementById('prrow').rowIndex)
  } else {
    document.getElementById('producttable').deleteRow(document.getElementById('prrow').rowIndex)
  }
  ; currentDisplay=null;
}
function DisableButton(obj) {
 obj.src = 'images/icon_please_wait.gif';
 obj.disabled = true;
 obj.style.border='none';
}
///////////////////////////////
///// order form functions ////
///////////////////////////////
function TotalValue(value) {
  current  = document.getElementById('total');
  discount = document.getElementById('discount_total');
  display  = document.getElementById('display_total');
  newTotal = (value * 1) + (current.value * 1);
  current.value = newTotal;
  if (newTotal < 500) {
    display.innerHTML = FormatAsMoney(current.value);
    discount.innerHTML = '&nbsp;';
  } else {
    discount.innerHTML = FormatAsMoney(current.value);
    display.innerHTML  = FormatAsMoney(current.value - (current.value * .25));    
  }
}
function UpdateOrderForm() { 
  // update order form based on tag and class
  var c = 0;
  temporder = document.getElementsByTagName('INPUT');
  for (x=0; x<temporder.length; x++) {
    if (temporder[x].className == 'orderitem') {
      c++;
      if (temporder[x].value > 0) {
        vdr = temporder[x].value;
        tdr = temporder[x].id;
        tdr = tdr.replace(/[^0-9]+/g,'');
        pdr = document.getElementById('price'+tdr).innerHTML;
        
        // modify style
        ClassSwitchOn('p'+tdr);
        
        // take care of orders in db
        UpdateOrder(tdr,vdr,pdr,'');
        AddTotal(tdr,vdr, pdr);
      }
    }
  }
}
function ProcessInput (thisId, thisValue, productId, productPrice, categoryId, productTable) {
  var sumTotal = 0;
  UpdateOrder(productId,thisValue,productPrice,''); 
  AddTotal(productId, thisValue, productPrice); 
  document.getElementById(thisId).style.backgroundColor='#ffffff'; 
  checkTable = document.getElementById(productTable).getElementsByTagName('input');;
  for (x=0; x<checkTable.length; x++) {
    sumTotal = sumTotal + eval(checkTable[x].value * 1);
  }
  document.getElementById(categoryId).innerHTML = (sumTotal > 0) ? '(Items Selected)' : '';
}
function PlaceOrder(id) {
  document.getElementById(id).style.display='none'; 
  document.getElementById('reset').style.display='none'; 
  document.getElementById('wait1').style.display='inline';
  submit(); 
}
function DisplayProductDetails(status, productId, tableName) {
  if (status == 'true') {
    InsertDetailsForm(productId, tableName);
  } else {
    Popup('popup', 'popup_details.php?product='+productId, '520', '350');
  }
}
////// reuse FAQs functions for order form //////
function InitFAQs() {
  LoadIcons();
}
function LoadIcons() {
  ImagePlus      = new Image();
  ImagePlus.src  = "images/faq_plus.gif";
  ImageMinus     = new Image();
  ImageMinus.src = "images/faq_minus.gif";
}
function ShowAnswer(prefix, id, count_clicks) {
  var answer = document.getElementById(prefix+id);
  if (answer.style.display == "none") {
    answer.style.display = "block";
    document.getElementById("plusminus"+prefix+id).src = ImageMinus.src;
  } else {
    answer.style.display = "none";
    document.getElementById("plusminus"+prefix+id).src = ImagePlus.src;
  }
}
allopened = false;
function OpenAll(id) {
  // open all answers
  divColl = document.getElementsByTagName("DIV");
  for (i=0; i<divColl.length; i++) {
    if (divColl[i].className == "answer") {
      target = (divColl[i].id);
      divColl[i].style.display = (allopened) ? "none" : "block";
    }
  }
  
  // change "plus" to "minus"
  regex = new RegExp("plusminus(\\w){1,3}(\\d){1,2}");
  imgColl = document.getElementsByTagName("IMG");
  for (i=0; i<imgColl.length; i++) {
    if (regex.test(imgColl[i].id)) {
      imgColl[i].src = (allopened) ? ImagePlus.src : ImageMinus.src;
    }
  }
  document.getElementById('openall').style.display = (allopened) ? "block" : "none"; 
  document.getElementById('closeall').style.display = (allopened) ? "none" : "block"; 
  
  // set flag
  allopened = !allopened;
}

///////////////////////////////
///////// xml functions ///////
///////////////////////////////
function LoadXMLDoc(url) 
{
  // function fails silently for IE MAC and Safari < 1.3
  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
      req.onreadystatechange = ProcessReqChange;
      req.open("GET", url, true);
      req.send(null);
    
  // branch for IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
        req.onreadystatechange = ProcessReqChange;
        req.open("GET", url, true);
        req.send();
    }
  }
}
function ProcessReqChange() 
{
  // only if req shows "complete"
  if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200) {
      method   = null;
      
      // processing statements
      response = req.responseXML.documentElement;

      if (response) {
        method   = response.getElementsByTagName('method')[0].firstChild.data;
        result   = response.getElementsByTagName('result')[0].firstChild.data;
        // what to do is based on method returned in xml
        if (method == "login") {
          document.getElementById('xmlcheck').value = true;
        } 
        if (method == 'details') {
         document.getElementById('prdetails').innerHTML = result;
        }
        if (method == 'order') {
          document.getElementById('display_total').innerHTML = result;
        }
        if (method == 'category') {
          document.getElementById(targetCategory).innerHTML = result;
        }
      }
    } else {
      alert("There was a problem retrieving the data:\n" + req.statusText);
    }
  }
}
function UpdateOrder(product, quantity, price, response)
{
  if (response == ''){ 
    // Input mode
    url = pathtoxml+'xmlupdateorder.php?product_id='+ product +'&quantity=' + quantity + '&price=' + price;
    LoadXMLDoc(url);
  }
}
function AddFavorite(product, response)
{
  if (document.getElementById('current['+product+']')) {
    document.getElementById('current['+product+']').value = product;
  }
  if (response == ''){ 
    // Input mode
    url = pathtoxml+'xmlupdatefavorites.php?product_id='+product;
    LoadXMLDoc(url);
  }
}
function DeleteFavorite(product, response)
{
  if (document.getElementById('current['+product+']')) {
    document.getElementById('current['+product+']').value = '';
  }
  if (response == ''){ 
    current = null;
    // Input mode
    url = pathtoxml+'xmldeletefavorites.php?product_id='+product;
    LoadXMLDoc(url);
  }
}
function ShowDetails(product, response)
{
  if (response == ''){ 
    // Input mode        
    url = pathtoxml+'xmlproductdetails.php?product='+ product;
    LoadXMLDoc(url);
  }
}
function CheckStatus(response)
{
  if (response == ''){ 
    // Input mode
    url = pathtoxml+'xmllogin.php';
    LoadXMLDoc(url);
  }
}
function GetCategoryList(id, category, response)
{
  if (response == ''){ 
    // Input mode
    if (document.getElementById(id).innerHTML.length < 500) {
      url = pathtoxml+'xmlcategorylist.php?category='+category;
      targetCategory = id;
      LoadXMLDoc(url);
    }
  }
}




































