/** 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. Try a newer/better browser, this application will not work!");   
	}
}
//General Search Facility
//etAjaxFDBudget
var storeAjaxSearchText = function (mystr) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/ajax/ajax_store_search.asp?st=' + mystr;

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', storeAjaxSearchTextresp);
}
var storeAjaxSearchTextresp = function (oXML) { 
    // get the response text, into a variable
    var response = oXML.responseText;
};

// Ajax for fancy dress home search facility

//etAjaxFDBudget
var getAjaxSearchBudgetOptions = function (gg) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/fancy_dress/fd_search_get_budget.asp?gg=' + gg;

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showFDBudgetResponse);
}
// The function for handling the response from the server
var showFDBudgetResponse = 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("fdhome_search_innner_costtheme").innerHTML = response;
};

var getAjaxSearchSizeOptions = function (gg) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/fancy_dress/fd_search_get_sizes.asp?gg=' + gg;

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showFDSizeResponse);
}
// The function for handling the response from the server
var showFDSizeResponse = 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("fdhome_search_innner_costtheme").innerHTML = response;
};

//Get themese based on Size
var getAjaxSearchThemeSizeOptions = function (gg, sf, st) {
	// build up the post string when passing variables to the server side page
	var PostStr = '/fancy_dress/fd_search_get_themes_size_range.asp?gg=' + gg + "&sf=" + sf + "&st=" + st;

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showFDThemeSizeOptionsResponse);
}
// The function for handling the response from the server
var showFDThemeSizeOptionsResponse = 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("fdhome_search_innner_theme").innerHTML = response;
};

//Get themese based on budget
var getAjaxSearchThemeOptions = function (gb) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/fancy_dress/fd_search_get_themes.asp?gb=' + gb;

	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showFDThemeOptionsResponse);
}
// The function for handling the response from the server
var showFDThemeOptionsResponse = 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("fdhome_search_innner_theme").innerHTML = response;
};

//Get themese based on budget
var getAjaxSearchResults = function (gb) {
	
	// build up the post string when passing variables to the server side page
	//var PostStr = '/fancy_dress/fd_search_get_themes.asp?gb=' + gb;
	var PostStr = gb //; '/fancy_dress/ajax_search_results.asp?gg=ch&mo=2&mi=0&ma=15'
	document.getElementById("ajax_search_result").innerHTML = '<div id="finding_costumes"><div id="fd_ajax-loader"></div></div>';
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showFDSearchResults);
}
// The function for handling the response from the server
var showFDSearchResults = 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("ajax_search_result").innerHTML = response;
};

//ajax_search_result

// END OF Ajax fancy dress search 
// *****************************************

// **********************************
// Ajax for CostumeThemeListings page
var getAjaxCostumeThemeListings = function (filterstr) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/fancy_dress/test.asp?' + filterstr;
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showGACTLResponse);
}
// The function for handling the response from the server
var showGACTLResponse = 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("fd_results").innerHTML = response;
};
// END OF Ajax for CostumeThemeListings page
// *****************************************



// launched from button click 
var getAjaxBasket = function () {
	
	// build up the post string when passing variables to the server side page
	var PostStr = "";
	
	// use the generic function to make the request
	doAJAXCall('/ajax/ajax_basket_list.asp', 'POST', '', showMessageResponse);
}



// Record Low Stock Customer Details
var AddNotifyLowStock = function (SiteCode, CustName, CustEmail, StockCode) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = "/includes/notifylowstock.asp?SiteCode=" + SiteCode + "&CustName=" + CustName + "&CustEmail=" + CustEmail + "&StockCode=" + StockCode;
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', rsp_AddNotifyLowStock);
}

	// NDispplay the response
	var rsp_AddNotifyLowStock = 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("NotifyAdd").innerHTML = response;
	};


var AddAjaxBasket = function (StockCode, PostQtyFld) {
	//Tell users we are doing something..
	//Build the divname

	var divpanelid = StockCode + "_status";

	document.getElementById(divpanelid).innerHTML = '<img src="/images/indicator.gif" width="16" height="16" />';
	
	//Get the Qty we are applying
	//var PostQtyFld = StockCode + "Qty"
	var PostQtyVal = document.getElementById(PostQtyFld).value;
		
	// build up the post string when passing variables to the server side page
	var PostStr = "/ajax/ajax_add_to_basket.asp?ProductID=" + StockCode + "&Qty=" + PostQtyVal;
	
	//now rest the qty field to 0
	//document.getElementById(PostQtyFld).value = "0"

	//Now tell them it was added (even if it wasn't as we cant tell at them moment)
	document.getElementById(divpanelid).innerHTML = '<img src="/images/fancy_dress/conf_add.png" />';
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', ProcessMessageResponse);

}

//Used for complete The Look Items facebox and now all the add to baskets...
var AddAjaxBasketCompLook = function () {
	//Tell users we are doing something..
	//Get the selected product
	//alert('Im running')
	var user_input = '';
	var PostQtyVal = document.getElementById('CompLookQTY').value;
	
	for (i=0;i<document.forms['frmAdd'].ProductID.length;i++) {
		if (document.forms['frmAdd'].ProductID[i].checked) {
			user_input = document.forms['frmAdd'].ProductID[i].value;
		}

	}
	if (user_input === '') {
			user_input = document.forms['frmAdd'].ProductID.value;
	}

	//alert('ui:' + user_input + ': ' + PostQtyVal);
	
//	var divpanelid = StockCode + "_status";

	// tell user you are adding the item
	document.getElementById('ctl_addbtnwrap').innerHTML = '<img src="/images/indicator.gif" width="16" height="16" />';

	//now add the item to basket
	// build up the post string when passing variables to the server side page
	var PostStr = "/ajax/ajax_add_to_basket.asp?ProductID=" + user_input + "&Qty=" + PostQtyVal;
	doAJAXCall(PostStr, 'POST', '', ProcessMessageResponse);

	//Now tell them it was added (even if it wasn't as we cant tell at them moment)
	document.getElementById('ctl_addbtnwrap').innerHTML = '<img src="/images/fancy_dress/conf_add_smile.png" />';

}

//Used for calling add to basket from flash
var AddAjaxAddToBasketFromFlash = function (ProductID, QTY) {
	//Tell users we are doing something..
	//Get the selected product

	//var user_input = '';
	//var PostQtyVal = document.getElementById('CompLookQTY').value;
	
	//alert('ui:' + user_input + ': ' + PostQtyVal);

	// tell user you are adding the item
	//document.getElementById('ctl_addbtnwrap').innerHTML = '<img src="/images/indicator.gif" width="16" height="16" />';

	//now add the item to basket
	// build up the post string when passing variables to the server side page
	var PostStr = "/ajax/ajax_add_to_basket.asp?ProductID=" + ProductID + "&Qty=" + QTY;
	doAJAXCall(PostStr, 'POST', '', ProcessMessageResponse);

	//Now tell them it was added (even if it wasn't as we cant tell at them moment)
	//document.getElementById('ctl_addbtnwrap').innerHTML = '<img src="/images/fancy_dress/conf_add_smile.png" />';

}

// New function for handling the response from the server (PDS 11/08/2009)
var ProcessMessageResponse = 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("cart_items").innerHTML = response;
};

// 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;
};

//Used for the sweets section
//PDS 22/02/2010

var AddAjaxBasketSweet = function () {
	//Tell users we are doing something..
	//Get the selected product
	//alert('Im running')
	var user_input = document.forms['frmAdd'].ProductID.value;
	var PostQtyVal = document.getElementById('CompLookQTY').value;

	//alert('ui:' + user_input + ': ' + PostQtyVal);

	// tell user you are adding the item
	document.getElementById('sm_added').innerHTML = '<img src="/images/indicator.gif" width="16" height="16" />';

	//now add the item to basket
	// build up the post string when passing variables to the server side page
	var PostStr = "/ajax/ajax_add_to_basket.asp?ProductID=" + user_input + "&Qty=" + PostQtyVal;
	doAJAXCall(PostStr, 'POST', '', ProcessMessageResponse);

	//Now tell them it was added (even if it wasn't as we cant tell at them moment)
	document.getElementById('sm_added').innerHTML = '<img src="/images/fancy_dress/conf_add_smile.png" />';

}

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\" style=\"top: 150px; height: " + GetNewBasketHeight() +"px; width: 260px; overflow:scroll; z-index: 5;\"><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;
}

// **********************************
// Ajax for Sweet Ajax Search Collapsible
var getAjaxSweetSearchOptionsMain = function (flid) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/sweets/includes/SweetDietarySearchOptions.asp?fl=' + flid;
	
	//start indicator
	document.getElementById('CollapsiblePanelContent').innerHTML = '<div align="center" style="height:auto; padding-top: 10px;"><img src="/images/indicator.gif" width="16" height="16" /></div>';
	
	//alert(flid);
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showSweetSearchOptionsMainResponse);
}
// The function for handling the response from the server
var showSweetSearchOptionsMainResponse = function (oXML) { 
    
    // get the response text, into a variable
    var response = oXML.responseText;
    //alert(response);
    // update the Div to show the result from the server
	document.getElementById("CollapsiblePanelContent").innerHTML = response;
};
// End of Ajax for Sweet Ajax Search Collapsible
// *****************************************

// **********************************
// Ajax for Sweet Ajax Search Results
var getAjaxSweetSearchResults = function (sweettype, flid, dietary) {
	
	// build up the post string when passing variables to the server side page
	var PostStr = '/sweets/includes/sweet_listing_flavour_dietary_ajax.asp?st=' + sweettype + '&fl=' + flid + '&di=' + dietary;
	
	//start indicator
	document.getElementById('main').innerHTML = '<div align="center" style="height:auto; padding-top: 10px;"><img src="/images/indicator.gif" width="16" height="16" /></div>';
	
	//alert(flid);
	
	// use the generic function to make the request
	doAJAXCall(PostStr, 'POST', '', showSweetSearchResultsResponse);
}
// The function for handling the response from the server
var showSweetSearchResultsResponse = function (oXML) { 
    
    // get the response text, into a variable
    var response = oXML.responseText;
    //alert(response);
    // update the Div to show the result from the server
	document.getElementById("main").innerHTML = response;
};
// End of Ajax for Sweet Ajax Search Results
// *****************************************
