/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

// doAJAXCall : Generic AJAX Handler, used with XHConn
// Author : Bryce Christensen (www.esonica.com)
// PageURL : the server side page we are calling
// ReqType : either POST or GET, typically POST
// PostStr : parameter passed in a query string format 'param1=foośm2=bar'
// FunctionName : the JS function that will handle the response

var doAJAXCall = function (PageURL, ReqType, PostStr, FunctionName) {

	// create the new object for doing the XMLHTTP Request
	var myConn = new XHConn();

	// check if the browser supports it
	if (myConn)	{
	    
	    // XMLHTTPRequest is supported by the browser, continue with the request
	    myConn.connect('' + PageURL + '', '' + ReqType + '', '' + PostStr + '', FunctionName);    
	} 
	else {
	    // Not support by this browser, alert the user
	    alert("XMLHTTP not available. Please upgarde your browser, this application will not work!");   
	}
}


// launched from button click 
//var getAjaxBasket = function () {
	
	// build up the post string when passing variables to the server side page
//	var PostStr = "/pick_n_mix/item_list.asp?CartID=hvlocgy5045don555xolta55";
	
	// use the generic function to make the request
//	doAJAXCall(PostStr, 'POST', '', showMessageResponse);
//}

var AddEditRemoveAjaxBasket = function (CartID, StockCode, QTY, TYP) {
	//This is used in the PickNMix area
	//document.getElementById("ajaxbasketlist").innerHTML = '<div align="center"><span><br>Updating Bag..</span>&nbsp;<br><br><br><img src="/images/indicator.gif" width="16" height="16" /></div>';
		
	// build up the post string when passing variables to the server side page
	//var PostStr = "/pick_n_mix/add_item.asp?ProductID=" + StockCode + "&Qty=" + 1;
	var PostStr = "/pick_n_mix/add_item.asp?ProductID=" + StockCode;

	UpdateBasketDivContent(QTY);
	//var cQ = parseInt(parent.document.getElementById('ctl00_NoOfItemsInCartLBL').innerHTML);
	var cQ = parseFloat(document.getElementById('picmqty').innerHTML) + parseFloat(QTY);

	//var rec_rewrite_const = '<div id="picm_funnel"><OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"	CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" WIDTH="150"    HEIGHT="130"     ID=playfunnel>  <PARAM NAME="MOVIE" VALUE="funnel_red.swf">  <PARAM NAME="PLAY" VALUE="false"> <PARAM NAME="LOOP" VALUE="false">  <PARAM NAME="QUALITY" VALUE="high">   <PARAM NAME="SCALE" VALUE="SHOWALL"> <PARAM NAME="WMODE" VALUE="Transparent"> <EMBED	NAME="playfunnel" SRC="funnel_red.swf" WIDTH="150"	 HEIGHT="130" PLAY="false"  LOOP="false"  QUALITY="high"  SCALE="SHOWALL"   swLiveConnect="true"  wmode="transparent"	 PLUGINSPAGE="http://www.macromedia.com/go/flashplayer/"> </EMBED></OBJECT></div></div>';
	
	//var rec_rewrite_flat = ' <div class="rec_left" style="background-image: url(/images/woolworths/picknmix/bagstate_flat.png);"> ' + rec_rewrite_const;
	//var rec_rewrite_1 = ' <div class="rec_left" style="background-image: url(/images/woolworths/picknmix/bagstate_1.png);"> ' + rec_rewrite_const;
	//var rec_rewrite_2 = ' <div class="rec_left" style="background-image: url(/images/woolworths/picknmix/bagstate_2.png);"> ' + rec_rewrite_const;
	//var rec_rewrite_3 = ' <div class="rec_left" style="background-image: url(/images/woolworths/picknmix/bagstate_3.png);"> ' + rec_rewrite_const;
	
	//if(cQ < 1) {document.getElementById('rec_rewrite').innerHTML = rec_rewrite_flat + rec_rewrite_const;};
	//if(cQ > 0 && cQ < 3) {document.getElementById('rec_rewrite').innerHTML = rec_rewrite_1;};
	//if(cQ > 2 && cQ < 5) {document.getElementById('rec_rewrite').innerHTML = rec_rewrite_2;};
	//if(cQ > 4 ) {document.getElementById('rec_rewrite').innerHTML = rec_rewrite_3;};
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', 'CartID=' + CartID + '&PID=' + StockCode + '&QTY=' + QTY + '&TYP=' + TYP, showMessageResponse);
	
}

var AddEditRemoveAjaxBasketFD = function (CartID, StockCode, QTY, TYP) {
	//This is used in the Fancy Dress for Complete the look area
	
	document.getElementById("ajaxbasketlist").innerHTML = '<div align="center"><span><br>Adding Item..</span>&nbsp;<br><br><br><img src="/images/indicator.gif" width="16" height="16" /></div>';

	// build up the post string when passing variables to the server side page
	//var PostStr = "/pick_n_mix/add_item.asp?ProductID=" + StockCode + "&Qty=" + 1;
	var PostStr = "/fancydress/add_item.asp";
	
	//now rest the qty field to 0
	//document.getElementById(PostQtyFld).value = "0"

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', 'CartID=' + CartID + '&PID=' + StockCode + '&QTY=' + QTY + '&TYP=' + TYP, showMessageResponseInBasketSummary);
	
	//Now tell the user it went perfectly well (even though we have o idea if it did or not!) - could add something to test basketsummary div before and after
	//ctl00_CartItemsUP
	document.getElementById("ajaxbasketlist").innerHTML = '<div align="center"><span><br>Your item was added to the basket</div>';
	
}

var UpdateBasketSummary = function (CartID) {
	//This is used to update the basket summary at the top of the page follwing ajax style calls (totally bespoke) PDS 04/05/2009
		
	// build up the post string when passing variables to the server side page
	//var PostStr = "/pick_n_mix/add_item.asp?ProductID=" + StockCode + "&Qty=" + 1;
	var PostStr = "/asp/includes/basket_summary.asp";
	
	//now rest the qty field to 0
	//document.getElementById(PostQtyFld).value = "0"

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', 'WCI_CartID=' + CartID, WriteBasketSummary);
	
}

var GetBasketContent = function (CartID) {
	//Tell users we are doing something..
	document.getElementById("ajaxbasketlist").innerHTML = '<div align="center"><span><br>Retreiving your Bag Contents..</span>&nbsp;<br><br><br><img src="/images/indicator.gif" width="16" height="16" /></div>';
		
	// build up the post string when passing variables to the server side page
	//var PostStr = "/pick_n_mix/add_item.asp?ProductID=" + StockCode + "&Qty=" + 1;
	var PostStr = "/pick_n_mix/item_list.asp?CartID=" + CartID;
	
	//now rest the qty field to 0
	//document.getElementById(PostQtyFld).value = "0"

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showMessageResponse);

}

// The function for handling the response from the server
var showMessageResponse = function (oXML) { 
    
    // get the response text, into a variable
    var response = oXML.responseText;
    
    // update the Div to show the result from the server
	document.getElementById("ajaxbasketlist").innerHTML = response;
};

// The function for handling the response from the server AND wrties it to the basketsummary section (besoked by PDS 04/05/2009)
var showMessageResponseInBasketSummary = function (oXML) { 
    
    // get the response text, into a variable
    var response = oXML.responseText;
    
    // update the Div to show the result from the server
	document.getElementById("ctl00_CartItemsUP").innerHTML = response;
};


var ResizeBasket = function () {
		var ResizeStarted = document.getElementById("ResizeStarted").value;
		//alert(ResizeStarted)
		if (ResizeStarted=="0") {
			document.getElementById("ResizeStarted").value = "1";
			window.setTimeout('ResizeBasketNow()',1000);
		}
}
var ResizeBasketNow = function () {
		document.getElementById("basket").innerHTML = "<div id=\"ajaxbasketlist\"><div align=\"center\"><br>Updating Basket&nbsp;<br><br><br><img src=\"/images/indicator.gif\" width=\"16\" height=\"16\"</div></div>";
		getAjaxBasket();
		document.getElementById("ResizeStarted").value = "0";
		
}
var GetNewBasketHeight = function () {
	var NewHeight = ((GetInnerHeight()) - 130);
	return NewHeight;
}

var GetInnerHeight = function () {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myHeight;
}