
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var receiveReqInfoBox = getXmlHttpRequestObject();
var receiveReqProduct = getXmlHttpRequestObject();
var file = 'getCart.php';
var loc = 'span_cart';
var tmp = '&nbsp;<img src=images/loading_sc.gif alt=loading>&nbsp;Please wait...<br/>';
var second_call_qty = 0;

//Function for initializating the page.
function startCart(key,file,sid,loc,tmp) {
  //Start Showing Cart.
  getCartText('',file,sid,loc,tmp);
}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
  if (window.XMLHttpRequest) {
  return new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.';
  }
}

//Gets the current cart
function getCartText(key,file,sid,loc,tmp,qty) {
  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
    receiveReq.open("GET", url+'&Cart=1&quantity='+qty, true);
    //receiveReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    receiveReq.onreadystatechange = handleReceiveCart;
    receiveReq.send(null);
  }
}

//Gets the current cart
function getProductInfoText(key,file,sid,loc,tmp) {
  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }
  var url=file+"?"+sid+"&pid="+key;

  if (receiveReqProduct.readyState == 4 || receiveReqProduct.readyState == 0) {
    receiveReqProduct.open("GET", url+'&Cart=1', true);
    receiveReqProduct.onreadystatechange = handleReceiveProduct;
    receiveReqProduct.send(null);
  }
}

//Gets the current cart info box
function getCartInfoBoxText(key,file,sid,loc,tmp) {
  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (receiveReqInfoBox.readyState == 4 || receiveReqInfoBox.readyState == 0) {
    receiveReqInfoBox.open("GET", url+'&Cart=1', true);
    receiveReqInfoBox.onreadystatechange = handleReceiveCartInfoBox;
    receiveReqInfoBox.send(null);
  }
}

//send change of stock to Cart.
function sendCartChangeQty(key,file,sid,loc,tmp,iElementId,product_id,qty) {
  if (typeof iElementId == "string" && iElementId.length > 0) {
    var element = document.getElementById(iElementId);
    if (element) {
      //alert("name=" + element.name + " - id=" + element.id);
      //return;
    } else {
      document.getElementById('p_status').innerHTML = 'Status: Could not find the requested element.';
      return;
    }
  }

  if (qty.length > 0) {
    qty_float = parseFloat(qty);
    if (isNaN(qty_float)) {
      document.getElementById('p_status').innerHTML = 'Status: You must enter a number for Quantity.';
      return;
    }
  } else {
    qty_float = 1;
  }

  // check if Quantity drops below 0
  if (Number(element.value) + Number(qty) <= 0) {
    // Are you sure you want to remove this item
    //var fRet;
    //fRet = confirm('Are you sure you want to remove this item from your shopping cart?');
    //if (fRet == false) { return; }
  }

  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    sendReq.open("POST", url+'&Cart=1', true);
    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    sendReq.onreadystatechange = handleSendCart;

    var param = 'quantity=' + (Number(element.value) + Number(qty));
    second_call_qty = (Number(element.value) + Number(qty));
    param += '&products_id=' + product_id;
    sendReq.send(param);
  }
}

//send change of stock to Cart.
function sendAddToCart(key,file,sid,loc,tmp,iElementId,product_id,qty) {
  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    sendReq.open("POST", url+'&Cart=1', true);
    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    sendReq.onreadystatechange = handleSendCartBox;

    var param = 'quantity=1';
    param += '&products_id=' + product_id;
    sendReq.send(param);
  }
}

//send change of stock to Cart.
function sendCartRemoveItem(key,file,sid,loc,tmp,iElementId,product_id) {
  if (typeof iElementId == "string" && iElementId.length > 0) {
    var element = document.getElementById(iElementId);
    if (element) {
      //alert("name=" + element.name + " - id=" + element.id);
      //return;
    } else {
      document.getElementById('p_status').innerHTML = 'Status: Could not find the requested element.';
      return;
    }
  }

  // check user wants to remove item
  if (element.value != '') {
    // Are you sure you want to remove this item
    var fRet;
    fRet = confirm('Are you sure you want to remove this item from your shopping cart?');
    if (fRet == false) { return; }
  }

  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
  url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    sendReq.open("POST", url+'&Cart=1', true);
    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    sendReq.onreadystatechange = handleSendCartRemoveItem;

    var param = 'products_id=' + product_id;
    param += '&cart_delete=Yes';
    sendReq.send(param);
  }
}

//send change of stock to Cart.
function sendChangeProduct(key,file,sid,loc,tmp,iElementId,product_id,qty) {
  var url=file+"?"+sid+"&"+key;
  if (sid && key) {
    url=file+"?"+sid+"&"+key;
  } else {
    if (sid) {
      url=file+"?"+sid;
    } else {
      if (key) {
        url=file+"?"+key;
      } else {
        url=file;
      }
    }
  }

  if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    sendReq.open("POST", url+'&Cart=1', true);
    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    sendReq.onreadystatechange = function() {eval("getProductInfoText(product_id,'getProduct.php','<?php echo tep_session_name().'='.tep_session_id(); ?>','span_product_info',tmp)");};

    var param = 'quantity=1';
    param += '&products_id=' + product_id;
    sendReq.send(param);
  }
}

//When our stock change has been sent, update our page.
function handleSendCart() {
  getCartText('','getCart.php','osCsid='+osCsid,'span_cart',tmp,second_call_qty);
  getCartInfoBoxText('','getCartBox.php','osCsid='+osCsid,'',tmp);
}

//When our stock change has been sent, update our page.
function handleSendCartBox() {
  getCartText('','getCart.php','osCsid='+osCsid,'span_cart',tmp);
  getCartInfoBoxText('','getCartBox.php','osCsid='+osCsid,'',tmp);
}

//When our stock change has been sent, update our page.
function handleSendProduct(pid) {
  getProductInfoText(pid,'getProduct.php','osCsid='+osCsid,'',tmp);
}

//Function for handling the return of Cart text
function handleReceiveCart() {
  //Check to see if the XmlHttpRequests state is finished.
  if (receiveReq.readyState == 4) {
    //Set the contents of our span element to the result of the asyncronous call.
    document.getElementById('span_cart').innerHTML = receiveReq.responseText;
  }
}

//Function for handling the return of Cart Info Box text
function handleReceiveCartInfoBox() {
  //Check to see if the XmlHttpRequests state is finished.
  if (receiveReqInfoBox.readyState == 4) {
    //Set the contents of our span element to the result of the asyncronous call.
   if (document.getElementById('boxstyle31')!=null) {document.getElementById('boxstyle31').innerHTML = receiveReqInfoBox.responseText;};
  }
  if (receiveReq.readyState == 4) {
    //Set the contents of our span element to the result of the asyncronous call.
    document.getElementById('span_cart').innerHTML = receiveReq.responseText;
  }
}

//Function for handling the return of Cart text
function handleReceiveProduct() {
  //Check to see if the XmlHttpRequests state is finished.
  if (receiveReqProduct.readyState == 4) {
    //Set the contents of our span element to the result of the asyncronous call.
    document.getElementById('span_product_info').innerHTML = receiveReqProduct.responseText;
  }
}

//When our stock change has been sent, update our page.
function handleSendCartRemoveItem() {
  getCartText('','getCart.php','osCsid='+osCsid,loc,tmp);
  getCartInfoBoxText('','getCartBox.php','osCsid='+osCsid,'',tmp);
}

function getObject(name) {
  var ns4 = (document.layers) ? true : false;
  var w3c = (document.getElementById) ? true : false;
  var ie4 = (document.all) ? true : false;

  if (ns4) return eval('document.' + name);
  if (w3c) return document.getElementById(name);
  if (ie4) return eval('document.all.' + name);
  return false;
}

on = "<?php echo TEXT_SHOPPINGCART_UPDATE; ?>";
off = " ";

function advisecustomer(advise_status) {
  document.cart_quantity.advise.value = advise_status;
}
