function xmlhttpPost(strURL, updatePage, updateArea) {
  xmlhttpRequest(strURL, updatePage, updateArea, 'POST');
}

function xmlhttpGet(strURL, updatePage, updateArea) {
  xmlhttpRequest(strURL, updatePage, updateArea, 'GET');
}

/* xmlhttpPost methos will execute a url and update
 * a portion of the html page specified by the id 
 * 
 */
function xmlhttpRequest(strURL, updatePage, elementID, method) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open(method, strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
      if (self.xmlHttpReq.readyState == 4 && updatePage) {
		  if (self.xmlHttpReq.status == 200) {
			  // will update a specified area if updatePage value is true
			  //alert(self.xmlHttpReq.responseText);
			  changeContent(elementID, self.xmlHttpReq.responseText);
		  } 
		/*else {
            alert("There was a problem retrieving the XML data:\n" +
                self.xmlHttpReq.statusText);
        }*/
      }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    //var form     = document.forms['f1'];
    //var word = form.word.value;
    //qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return "";
}

/*
function updatepage(elementID, str){
    document.getElementById(elementID).innerHTML = str;
} 
*/

function changeContent(elementID, newContent){
  if(document.getElementById){
    document.getElementById(elementID).innerHTML = newContent
  }
  else if(document.layers){ // only supported for loading contents of a file into a layer syntax: load(pathNfilename, width)
    document.layers[elementID].load(newContent, 300)
  }
  else if(document.all){
    document.all[elementID].innerHTML = newContent
  }
  else {
	  alert('inside final else '+elementID+' => '+newContent);
  }
}

/* addToShoppingList method will add an item
 * to a users' shopping list without refereshing
 * the page (using AJAX method) - Henok
 */
function addToShoppingList(assetId, imageId, lang) {
  var langParam = "";
  if(lang == "fr") {
     langParam = "&lang=fr";
  }
  var stamp = new Date();
  var DTstamp = stamp.getHours();
  DTstamp = DTstamp+stamp.getMinutes();
  DTstamp = DTstamp+stamp.getSeconds();

  var strURL = "/wpscontroller?command=storelocator.ServletaddToShopList"+
     "&assetId="+assetId+"&imageId="+imageId + langParam+"&stamp="+DTstamp;

  // making GET HTTP request using AJAX method 
  xmlhttpGet(
     strURL, // url to execute
     true,   // true/false value. Weather to update an HTML area
     "shoppingListCount"); // id of HTML area to update
  if(lang == 'fr') {
	  alert("L'article a été ajouté ŕ votre liste.");
  }
  else {
	  alert('Item successfully added to your shopping list');
  }
}

function addToShoppingListNoAlert(assetId, imageId, lang, noAlert)
{
	  var langParam = "";
	  if (lang == "fr")
	  {
	     langParam = "&lang=fr";
	  }
	  var stamp = new Date();
	  var DTstamp = stamp.getHours();
	  DTstamp = DTstamp+stamp.getMinutes();
	  DTstamp = DTstamp+stamp.getSeconds();
	  
	  var strURL = "/wpscontroller?command=storelocator.ServletaddToShopList"+
	     "&assetId="+assetId+"&imageId="+imageId + langParam+"&stamp="+DTstamp;

	  // making GET HTTP request using AJAX method 
	  xmlhttpGet(
	     strURL, // url to execute
	     false,   // true/false value. Whether to update an HTML area
	     ""); // id of HTML area to update
	  if (noAlert)
		  return;
	  if (lang == 'fr')
	  {
		  alert("L'article a été ajouté ŕ votre liste.");
	  }
	  else
	  {
		  alert('Items successfully added to your shopping list');
	  }
}

function addToOrderFormList(assetId, imageId, lang) {
  var langParam = "";
  if(lang == "fr") {
     langParam = "&lang=fr";
  }
  var stamp = new Date();
  var DTstamp = stamp.getHours();
  DTstamp = DTstamp+stamp.getMinutes();
  DTstamp = DTstamp+stamp.getSeconds();
  
  var strURL = "../wpscontroller?command=storelocator.specialorder.ServletaddToOrderFormList"+
     "&assetId="+assetId+"&imageId="+imageId + langParam+"&stamp="+DTstamp;

  // making GET HTTP request using AJAX method 
  xmlhttpGet(
     strURL, // url to execute
     true,   // true/false value. Weather to update an HTML area
     "orderFormListCount"); // id of HTML area to update
  
  if(lang == 'fr') {
	  alert('L\'article a été ajouté ŕ votre commande');
  }
  else {
	  alert('Item successfully added to your Order Form');
  }

}

