// unimed.js - javascript functions used by the Universal Medical Inc web site

function itemAdded() {
  //alert("item added to cart");
overlib("Item added to the cart",TIMEOUT,1500);
}

//------------------------------ show quantity pricing ---------------------
function showQtyPricing(theText) {
  //alert("Quantity Pricing = " + theText);
  return("<p>" + theText +"</p>");
}
//--------------------------------- ajax stuff -------------------------------

// This function uses DHTML to modify the cart values on a tile.jsp page
function updateCartValues(count,total) {
        var elemCount = document.getElementById("cartItemCount");
        var elemTotal = document.getElementById("cartTotal");       
        elemCount.firstChild.nodeValue = count;   
        elemTotal.firstChild.nodeValue = total;
        if ( count > 0 ) {
            elemTotal.style.color = "red";
        }
        //setTimeout("document.getElementById('cartTotal').style.color = 'black';",2000);

}
function shopCartShipping(theElem) {
  var value = ( theElem == null ? "restore" : theElem.value);  // 2nd Day etc .
// if null the shippingfee servlet uses the last value set in the session object
// or Standard Ground if none set.
  data = null;
  //alert("doing shippingfee with value =" + theElem.value);
  loadXMLDoc("/shippingfee/?shipHow=" + value + "&ajaxCommand=getShipFee" + "&sid=" + Math.random());
  //alert("in shopCartShipping() after loadXMLDoc call data = " + data + " req.responseText=" + req.responseText);
  // data will now contain the ship fee and the total
  if ( data == null ) {
    data = req.responseText;
  }
  var strs = data.split(" ",3);
  //alert("after data.split");
  //alert("in shopCartShipping() shipfee = " + strs[0] + " total= " + strs[1]);
  if ( theElem != null ) {
      var elemShipFee = document.getElementById("shipFee");
      //alert("after getElementById");
      var elemTotal = document.getElementById("total");
      //alert("in shopCartShipping() ShipHow value =" + value);
      elemShipFee.firstChild.nodeValue = strs[0];
      //alert("after elemShipFee setting");
      elemTotal.firstChild.nodeValue = strs[1];
  }
  if ( strs[0] != "error" ) {
      updateCartValues(strs[2],strs[1]);
   }
}


//-----------------------------------------------------------------------------------
function printWindow()
{
  myPrintWindow = open("/printcart", "myPrintWindow");
  myPrintWindow.focus();
}

// The function is invoked by the Back button in our navigation bar.
// from JavaScript book 4th edition Page 217
function go_back()
{
    // First, clear the URL entry field in our form.
   // document.navbar.url.value = "";

    // Then use the History object of the main frame to go back.
    //parent.frames[0].history.back();
     window.history.back();   // for tiled web application

    // Wait a second, and then update the URL entry field in the form
    // from the location.href property of the main frame. The wait seems
    // to be necessary to allow the location.href property to get in sync.
    //setTimeout("document.navbar.url.value = parent.frames[0].location.href;",
    //           1000);
}
//---------------------------------------------------
// AJAX routines used by some tiled fragments 
var req=null;
var console=null;
var data=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
        req.onreadystatechange = processReqChange;
        req.open("GET", url, false);  // false means wait until complete
        req.send(null);  
    }
}

function processReqChange(){
  var ready=req.readyState;
  //var data=null;
  if (ready==READY_STATE_COMPLETE){
    data=req.responseText;
  }else{
    data="saving image...["+ready+"]";
  }
  toConsole(data);
}


function toConsole(data){
  if (console!=null){
    var newline=document.createElement("div");
    console.appendChild(newline);
    var txt=document.createTextNode(data);
    newline.appendChild(txt);
  }
}

//------------------------------------------------------------------
// The init function updates the cart values using AJAX when the
// browser forward and back arrow are used. It is invoked from the body tag in tiles.jsp
function init()
{ 
  try {
       shopCartShipping(null);
    }
    catch ( e ) {
      // alert('a JavaScript exception' + e + ' was caught in the init() method on loading the window');
      // ignore else search engine cached pages display this error due to AJAX cart update
    }
}
