/* Estimated Shipping in cart */

function zip_est_shipping() {
  var zip = document.form_zip_est_shipping.dest_zip.value;
  if (isNaN(zip) || zip.length!=5)
     alert ('Please provide a valid 5 digit zip code');
  else {
    url = 'cart?action=zip&zip='+zip;
    //alert(url);
    href(url); 
//    //alert ('Getting quote for '+zip);
//     var myurl = 'api.php?req=zip_est_shipping&dest_zip='+zip;
//     var result_func = 'result_zip_est_shipping';
//     document.getElementById('id_zip_est_shipping').innerHTML ='Calculating Shipping  <img src="images/progress_bar.gif">';
//     //alert('calling api.php?req=zip_est_shipping&dest_zip='+zip);
//     jx.load('api.php?req=zip_est_shipping&dest_zip='+zip,'result_zip_est_shipping',"text","POST");
  }
}

function result_zip_est_shipping(result) {
  //alert('Back: '+result);
  var parts = result.split(',');
  var upd = document.getElementById('id_zip_est_shipping');
  if (parts[0]!='OK') 
    upd.innerHTML ='ERROR! Please Retry';
  else 
    upd.innerHTML ='Estimated Shipping to <i><u>' + parts[1] + '</u></i>   <b>$'+ parts[2] +'</b>';
}

function diaper_gal_set(id,imgurl) {
  var obj = get_object(id);
  obj.src = imgurl;
}

function img_gal_set(imgurl) {
  var obj = get_object('item_img');
  //alert ('current: '+ obj.src +'\n setting to: '+imgurl);
  obj.src = imgurl;
}

function get_object(id) {
 var object = null;
 if (document.layers) {
    object = document.layers[id];
 } else if (document.all) {
    object = document.all[id];
 } else if (document.getElementById) {
    object = document.getElementById(id);
 }
 return object;
}

function href(url) { 
  window.location=url; 
}

function more(id) {
  v = document.itemform.elements[id].value;
  //alert ("add IN: "+v);
  if (isNaN(v)) {
    if (confirm("Quantity is not a number !\nWould you like to reset ?")) {
      document.itemform.elements[id].value = 0;
    }
  }
  else {
    v++;
    document.itemform.elements[id].value = v;
  }
  ///alert ("add OUT: " + document.itemform.elements[id].value);
}

function less(id) {
  v = document.itemform.elements[id].value;
  //alert ("less IN: "+v);
  if (isNaN(v)) {
    if (confirm("Quantity is not a number !\nWould you like to reset ?")) {
      document.itemform.elements[id].value = 0;
    }
  }
  else {
    v--;
    if (v>=0)
       document.itemform.elements[id].value = v;
  }
  //alert ("less OUT: " + document.itemform.elements[id].value);
}

function cartaddoptions() {
  var errs = new Array();
  var c = document.itemform.elements.length;
  var options = new Array();  
  var index = 0;
  var i,e,q;
  for( i=0; i<c; i++ ) {
    e = document.itemform.elements[i];
    if (e.type=='text') {
      q = e.value;
      if (isNaN(q)) 
        errs.push('* Item #' + (i+1) + ' has "'+q+'" as quantity, pleas fix');
      else if (q<0) 
        errs.push('* Item #' + (i+1) + ' has negative quantity "'+q+'", pleas fix');
      else if (q>0)
        options.push(e.name+':'+q);
    }
  }
  if (errs.length>0) 
      alert(errs.join('\n'));
  else if (options.length==0) 
      alert("Please provide quantity before adding to cart");
  else {
      document.cartaction.cat.value = document.itemform.cat.value;
      document.cartaction.item.value = document.itemform.item.value;
      document.cartaction.options.value = options.join(',');
      //alert(document.cartaction.cat.value+'\n'+document.cartaction.item.value+'\n'+document.cartaction.options.value);
      document.cartaction.submit();
  }
}

function cartadditem() {
  var i,e,q;
  var err = '';
  var c = document.itemform.elements.length;
  for( i=0; i<c; i++ ) {
    e = document.itemform.elements[i];
    if (e.type=='text') {
      //alert (e.name+' | '+e.value);
      q = e.value;
      if (isNaN(q)) 
        err = '* Item has "'+q+'" as quantity, pleas fix';
      else if (q<0) 
        err = '* Item has negative quantity "'+q+'", pleas fix';
      else if (q==0) 
        err = 'Please provide quantity before adding to cart';
    }
  }
  if (err!='') alert(err); 
  else {
      document.cartaction.cat.value = document.itemform.cat.value;
      document.cartaction.item.value = document.itemform.item.value;
      document.cartaction.q.value = q;
      //alert(document.cartaction.cat.value+'\n'+document.cartaction.item.value+'\n'+document.cartaction.q.value);
      document.cartaction.submit();
  }
}

function cartadd(val) {
  return;
  var errs = new Array();
  var c = document.itemform.elements.length;
  var items = new Array();  
  var index = 0;
  var i,e,q;
  for( i=0; i<c; i++ ) {
    e = document.itemform.elements[i];
    if (e.type=='text') {
      index++;
      q = e.value;
      if (isNaN(q)) 
        errs.push('* Item #' + index + ' has "'+q+'" as quantity, pleas fix');
      else if (q<0) 
        errs.push('* Item #' + index + ' has negative quantity "'+q+'", pleas fix');
      else if (q>0)
        items.push(document.itemform.elements[i].name+':'+q);
    }
  }
  if (errs.length>0) 
      alert(errs.join('\n'));
  else if (items.length==0) 
      alert("Please provide quantity before adding to cart");
  else {
      document.cartaction.cat.value = document.itemform.cat.value;
      document.cartaction.items.value = items.join(',');
      //alert(document.cartaction.items.value);
      document.cartaction.submit();
  }
}

function cartedit(item) {
  var q = document.cartitems.elements[item].value; 
  if (isNaN(q)) alert("Please use numeric quantity\n'"+q+"' is not a number");
  else if (q<0) alert("Please use positive quantity\n'"+q+"' is negative amount");
  else if (q==0) alert("Please submit quantity to add");
  else if (q>100) alert("Are you sure this is the correct amount ?"); //\n'"+q+"' is a large quantity");
  else {
     //alert('edit\nitem: '+item+'\nq: '+q);
     document.cartaction.action.value = 'edit';
     document.cartaction.item.value = item;
     document.cartaction.q.value = q;
     document.cartaction.submit();
  }  
}

function cartdelete(item,name) {
  if (confirm("Are you sure you want to remove\n"+name+" ???")) {
     //alert('delete\nitem: '+item);
     document.cartaction.action.value = 'delete';
     document.cartaction.item.value = item;
     document.cartaction.submit();
  } 
}

// -----------------------------------


/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.
   
   sil, http://www.kryogenix.org/
   
   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
*/

var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 25;

ss.addEvent(window,"load",ss.fixAllLinks);


