/*

  "The Source" JavaScript Functions:

  AJAX is handled by mootools.js (rev 86) - www.mootools.net

  canAjax()           - Returns boolean value of whether the user's browser can handle XMLHTTP Requests

  addtoCart(pageName) - Adds the item on the current page to the shopping cart. pageName is the page to handle the request
  reduceQty(id)       - Basket handler to take 1 off the current [id] item's quantity (detects AJAX)
  increaseQty(id)     - Basket handler to add 1 to the current [id] item's quantity (detects AJAX)
  deleteItem(id)      - Basket handler to completely remove [id] from the basket (detects AJAX)
  checkAvailabililty  - Sends the username on the registration form to register.php via AJAX to see if the username is free
  upperCase(what)     - Form field formatter. Changes input text to UPPERCASE alphanumeric only (plus spaces).

*/

function canAjax(){
  var ro;
  var browser = navigator.appName;
  if(browser == 'Microsoft Internet Explorer'){
    ro = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    ro = new XMLHttpRequest();
  }
  return ro;
}

// BASKET FUNCTIONS

function addToCart(pageName){
  if(document.getElementById('txtQuantity').value && !isNaN(document.getElementById('txtQuantity').value)){
    if(canAjax()){
      document.getElementById('btnAddBasket').src='images/icon_wait.gif';
      var myAjax = new Ajax(pageName, {method: 'post', postBody: 'btnAddBasket=Add&ajax=1&txtQuantity='+document.getElementById('txtQuantity').value, update: $('bigOutcome'), onComplete: changeBack}).request();
      document.getElementById('txtQuantity').value = '';
      //btnAddBasket
      return false;
    }else{
      return true;
    }
  }else{
    document.getElementById('txtQuantity').value='';
    document.getElementById('txtQuantity').focus();
    return false;
  }
}

function changeBack(){
  document.getElementById('btnAddBasket').src='images/btn_add_basket.gif';
  document.getElementById('bigOutcome').style.display='block';
}

function reduceQty(id){
  if(canAjax()){
    if(document.getElementById('basket'+id)){
      aElements = new Array('basket'+id, 'subtotal'+id);
      var updateStr = '<img vspace="1" src="images/icon_wait.gif" alt="Working" width="14" height="14" align="texttop" />';
      for(i=0;i<aElements.length;i++) {
        document.getElementById(aElements[i]).innerHTML = updateStr;
      }
    }
    var myAjax = new Ajax('./?action=reduce&ajax=1&id='+id, {method: 'get', update: $('customerBasket'), onComplete: updateMyBasket}).request();
  }else{
    top.location='./?action=reduce&id='+id;
  }
}

function increaseQty(id){
  if(canAjax()){

    if(document.getElementById('basket'+id)){
      aElements = new Array('basket'+id, 'subtotal'+id);
      var updateStr = '<img vspace="1" src="images/icon_wait.gif" alt="Working" width="14" height="14" align="texttop" />';
      for(i=0;i<aElements.length;i++) {
        document.getElementById(aElements[i]).innerHTML = updateStr;
      }
    }

    var myAjax = new Ajax('./?action=increase&ajax=1&id='+id, {method: 'get', update: $('customerBasket'), onComplete: updateMyBasket}).request();
  }else{
    top.location='./?action=increase&id='+id;
  }

}

function updateMyBasket(){
  if(canAjax()){
    var myAjax = new Ajax('./?updatebasket=1', {method: 'get', update: $('basket')}).request();
  }
}

function productBrowse(prodUrl){
  if(canAjax()){
    var myAjax = new Ajax(prodUrl+'&ajax=1', {method: 'get', update: $('productsList')}).request();
  }else{
    top.location = prodUrl;
  }
}

function deleteItem(id){
  if(confirm('Are you sure you want to delete\nthis item from your basket?')){
    if(canAjax()){
      var myAjax = new Ajax('./?action=delete&ajax=1&id='+id, {method: 'get', update: $('customerBasket'), onComplete: updateMyBasket}).request();
    }else{
      top.location='./?action=delete&id='+id;
    }
  }
}

function changeVerify(){
  if(canAjax()){
      var myAjax = new Ajax('./?change=1&ajax=1', {method: 'get', onComplete: updateVerify}).request();
  }else{
    document.getElementById("frmContact").action="./?change=1";
    document.getElementById('frmContact').submit();
  }
}

function updateVerify(){
  img = document.getElementById('imgVerify');
  if(img){
    img.src = "./?image=1&r="+Math.floor(Math.random()*99999);
  }
}

function upperCase(what){
  what.value = what.value.toUpperCase().replace(/([^0-9A-Z ])/g,"");
}


function viewImage(imgpath,wwidth,wheight){
  imageWindow = window.open('image.php?image='+imgpath,'imageWindow','width='+wwidth+',height='+wheight+',toolbar=no,addressbar=no,statusbar=no,menubar=no,scrollbars=yes,resizable=yes');
}