
/* ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* 

ORDER SERVICE - CLASS TO CALL THE REMOTE ORDERINTERFACE 
THIS IS SEPARATE TO THE DESIGN STUDIO CODE BECAUSE...

1. IT'S NOT THE DESIGN STUDIO!
2. IT IS LOADED DIRECTLY IN THE CLIENT CODE AND SO NEEDS TO BE LIGHTWEIGHT AND SIMPLE TO CALL

*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------ */

// namespace - nore syntax between functions and properties
var EFR_OrderService = {


		// change this according to the environment in which it is running

		//SOURCE:"http://localhost/services",
		//SOURCE:"https://services.pro-framer.co.uk/html/services",
		SOURCE:"http://servicestest.pro-framer.co.uk/html/services",

	// ORDER CONSTANTS
	COMMAND_GENERATEIMAGE:3,
	COMMAND_COMMITORDER:15,
	COMMAND_PREPAREORDER:16,
	COMMAND_ADDORDERLINE:17,
	COMMAND_ADDDELIVERYADDRESS:18,
	COMMAND_GETDELIVERYCHARGEFROMPOSTCODE:19,
	COMMAND_GETDELIVERYCHARGEFROMZONE:20,
	COMMAND_ADDINVOICEADDRESS:29,
	COMMAND_GETMOULDINGDATA:35,
	COMMAND_GETASSEMBLYPRICES:37,
	COMMAND_GETVOLUMEDISCOUNT:38,
	COMMAND_GENERATE:39, // unused


	/*
	*/
    generate:function(
			pCallback, 
			urlImage, 
			nHeight, 
			nWidth, 
			DEFAULT_SCALE_VALUE, 
			bHasFrame, 
			bHasMount, 
			nTopBorder, 
			nBottomBorder, 
			nLeftBorder, 
			nRightBorder, 
			nOffsetTop,
			nMountCount,
			bHasVGrooves,
			nBevelTop,
			nBevelMid,
			nBevelBot,
			strTopColour,
			strBotColour,
			nTopCore,
			nMidCore,
			nBotCore,
			strIdMoulding,
			nFaceWidth,
			strMountStyle,
			strTopMountId,
			strBotMountId,
			nTradePrice,
			bIsCropped) 
    {

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/mouldingservice.asp?callback=?", 
			{ /* params */
			 "LIC":encodeURIComponent("pfdemo"),
			 "INVOKE":EFR_OrderService.COMMAND_GENERATE,
			 "urlimg":urlImage,
			 "heightmm":nHeight,
			 "widthmm":nWidth,
			 "scaleto":DEFAULT_SCALE_VALUE,
			 "hasframe":bHasFrame,
			 "hasmount":bHasMount,
			 "topborder":nTopBorder,
			 "bottomborder":nBottomBorder,
			 "leftborder":nLeftBorder,
			 "rightborder":nRightBorder,
			 "offsettop":nOffsetTop,
			 "mountcount":nMountCount,
			 "hasvgrooves":bHasVGrooves,
			 "beveltop":nBevelTop,
			 "bevelmid":nBevelMid,
			 "bevelbot":nBevelBot,
			 "topcolour":strTopColour,
			 "botcolour":strBotColour,
			 "topcore":nTopCore,
			 "midcore":nMidCore,
			 "botcore":nBotCore,
			 "moulding":strIdMoulding,
			 "facewidth":nFaceWidth,
			 "mountstyle":strMountStyle,
			 "topmountid":strTopMountId,
			 "botmountid":strBotMountId,
			 "tradeprice":nTradePrice,
			 "FITTED_BY":"C",
			 "iscropped":bIsCropped
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the generateImage() service to work, a callback must have been passed into the getAssemblyPrices() function");
			 }
		);
	},
	
	fetchAndScaleImage:function(callback, strSourceImage, h,w, nScaleTo)
	{

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/mouldingservice.asp?callback=?", 
			{ /* params */
			 "LIC":encodeURIComponent("pfdemo"),
			 "INVOKE":EFR_OrderService.COMMAND_SCALEIMAGE,
			 "urlimg":urlImage,
			 "heightmm":nHeight,
			 "widthmm":nWidth,
			 "scaleto":DEFAULT_SCALE_VALUE
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the fetchAndScaleImage() service to work, a callback must have been passed into the getAssemblyPrices() function");
			 }
		);
	},

	load(COMMAND_SCALEIMAGE, strURLToServices + "/services/v1/facades/mouldingservice.asp?lic=" + app.EFR_LICENCE_KEY + "&callback=" + strCallback  + "&invoke= " + COMMAND_SCALEIMAGE + "&urlimg=" + strSourceImage + "&scaleto=" + nScaleTo+ "&heightmm=" + h + "&widthmm=" + w);



   /*
	to keep this whole thing simple we will be calculating delivery charges
	based on delivery zone only. No attempt is made to replicate eframe
	delivery calcs which require detailed analysis of size and weight of the items
	in being ordered
	*/
    getDeliveryChargeFromPostCode:function(strPostCode, pCallback) 
    {

		EFR_OrderService.postCodeCallback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "INVOKE":EFR_OrderService.COMMAND_GETDELIVERYCHARGEFROMPOSTCODE,
			 "POSTCODE":strPostCode
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.postCodeCallback) != "undefined")
				{
					EFR_OrderService.postCodeCallback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the post code lookup service to work, a callback must have been passed into the getDeliveryChargeFromPostCode() function");
			 }
		);
	},

	/*
	
	*/
    getMouldingData:function(strMldRefType, strIdMoulding, pCallback) 
    {

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/mouldingservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "INVOKE":EFR_OrderService.COMMAND_GETMOULDINGDATA,
			 "MOULDINGREFTYPE":strMldRefType,
			 "IDMOULDING":strIdMoulding
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the getMouldingData() service to work, a callback must have been passed into the getMouldingData() function");
			 }
		);
	},
	
	/*
	*/
    getVolumeDiscount:function(pCallback, idProduct, unitPrice, unitCostPrice, productType, fittingBy, basePrice, allowVolumeDiscounting, newqty) 
    {

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/configservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "INVOKE":EFR_OrderService.COMMAND_GETVOLUMEDISCOUNT,
			 "IDPRODUCT":idProduct,
			 "UP":unitPrice,
			 "UCP":unitCostPrice,
			 "PT":productType,
			 "FBY":fittingBy,
			 "NEWQTY":newqty,
			 "BP":basePrice,
			 "AVD":allowVolumeDiscounting
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the getVolumeDiscount() service to work, a callback must have been passed into the getAssemblyPrices() function");
			 }
		);
	},
	
	
	/*
	
	*/
    getAssemblyPrices:function(h,w,pCallback) // Not yet Implemented
    {

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/configservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "INVOKE":EFR_OrderService.COMMAND_GETASSEMBLYPRICES,
			 "HEIGHT":h,
			 "WIDTH":w
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the getAssemblyPrices() service to work, a callback must have been passed into the getAssemblyPrices() function");
			 }
		);
	},
	
	/*
	to be used when client doesn't yet know their customer's post code but does know the delivery zone
	*/
    getDeliveryChargeFromZone:function(strZone, pCallback) 
    {

		EFR_OrderService.callback = pCallback;

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "INVOKE":EFR_OrderService.COMMAND_GETDELIVERYCHARGEFROMZONE,
			 "ZONE":strZone
			 }, 
			 function(pJSON)
			 {
				// call back to host to tell them the tx has completed
				if (typeof(EFR_OrderService.callback) != "undefined")
				{
					EFR_OrderService.callback(pJSON);
				}
				else
					alert(COMPANY_NAME, "For the delivery charge lookup service to work, a callback must have been passed into the getDeliveryChargeFromZone() function");
			 }
		);
	},

placeOrder:function(strLicKey, idExternal, pCartObject, strDelCharge, pLoginObject, pInvAddressObject, pDelAddressObject, pPmtObject, strPmtType, strDelZone, strDelInstr, strMoreInfo, pCallback, strMode) 
    {


    	/* 
    	
    	We now call the COMMAND_PREPAREORDER functionality on the server 

    	Because of the possible issue of a lost internet connection before all items have been transmitted
    	we need to employ a robust mechanism as follows:
    	
    	1. startTransaction (aka prepareOrder) - sets up a remote cache of cart items and returns a txid to the client 
    	2. Transfer all cart items - adds to that cache - must transmit the tx id back to the server
    	3. CommitTransaction - saves the cache to the DB using DB transaction mechanism - must transmit the txid
    	
    	In the event that the connection is lost mid-save, then the remote cache will clear after a TIMEOUT 
    	period. If an attempt is made to add to the cache after re-connection and before a TIMEOUT using the
    	same txid then the cache will be updated - any item in the cache with the same id will be overwritten
    	
    	*/
   	
		if (pInvAddressObject == null)
			pInvAddressObject = new Object();
		
		if (pLoginObject == null)
			pLoginObject = new Object();

		if (pPmtObject == null)
			pPmtObject = new Object();
		
		
		// save the params for when we are in the anonymous fn below (which does not have a context of EFR_OrderService)
		EFR_OrderService.clientCallback = pCallback;
		EFR_OrderService.clientCart = pCartObject;
		EFR_OrderService.delAddressObject = pDelAddressObject;
		EFR_OrderService.invAddressObject = pInvAddressObject;
		EFR_OrderService.strLicKey = strLicKey;
		EFR_OrderService.strPaymentType = strPmtType;
		EFR_OrderService.delCharge = strDelCharge;
		EFR_OrderService.strDelZone = strDelZone;
		EFR_OrderService.strDelInstr = strDelInstr;
		EFR_OrderService.strMoreInfo = strMoreInfo;
		EFR_OrderService.UID = pCartObject.UID;
		EFR_OrderService.strMode = strMode;
		EFR_OrderService.idExternal = idExternal;

		/* need to get the total order value and VAT figures to give to server */		
		nNet = parseFloat(0);
		nNetCost = parseFloat(0);
		nVAT= parseFloat(0);
		
		for (var i = 0; i < EFR_OrderService.clientCart.cartitems.length; i++)
		{
			nNet += parseFloat(EFR_OrderService.clientCart.cartitems[i].NET);
			nNetCost += parseFloat(EFR_OrderService.clientCart.cartitems[i].NET_COST);
			//console.log(EFR_OrderService.clientCart.cartitems[i].IDPRODUCT + ":" + EFR_OrderService.clientCart.cartitems[i].NET)
		}
		
		var nCartLen = pCartObject.cartitems.length;

		// before we intitate the sequence of remote calls we set a timer to timeout after 'n' seconds
		EFR_OrderService.timeoutTimer = setTimeout(function () 
		{

			/* and empty the fn so that if the callback does occur, it doesn't re-call */
			EFR_OrderService.committed = function () 
			{
				EFR_OrderService.committed = EFR_OrderService.reset_committed
			};					
				
	  		// show timeout to client
			if (EFR_OrderService.timeoutTimer) 
				clearTimeout(EFR_OrderService.timeoutTimer);
	
			// call back to host to tell them the tx has completed
			if (typeof(EFR_OrderService.clientCallback) != "undefined")
			{
				var pJSON = new Object();
				pJSON.TIMEOUT = true;
				
				EFR_OrderService.clientCallback(pJSON);
				
			}
			else
				alert(COMPANY_NAME, "For the order service service to work, a callback must have been passed into the placeOrder() function");

		}, 20000);

		// if != I then we have the ecomm version
		if (EFR_OrderService.strMode != "I")
			fn = EFR_OrderService.prepareOrderComplete2;
		else
			fn = EFR_OrderService.prepareOrderComplete;		

		// make remote call
		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "UID":EFR_OrderService.UID,
			 "INVOKE" : EFR_OrderService.COMMAND_PREPAREORDER,
			 "TOTALITEMSINORDER" : nCartLen,
			 "PAYMENTTYPE" : EFR_OrderService.strPaymentType,
			 "DELIVERYZONE" : EFR_OrderService.strDelZone,
			 "DELIVERYINSTRUCTIONS" : EFR_OrderService.strDelInstr,
			 "MOREINFO" : EFR_OrderService.strMoreInfo,
			 "DELCHARGE" : EFR_OrderService.delCharge,
			 "ORDERNETCOST" : nNetCost.toFixed(2),
			 "ORDERNET" : nNet.toFixed(2),
			 "MODE": EFR_OrderService.strMode,
			 "TITLE": pLoginObject.TITLE,
			 "FIRSTNAME": pLoginObject.FIRST_NAME,
			 "LASTNAME": pLoginObject.LAST_NAME,
			 "COMPANY": pLoginObject.COMPANY,
			 "EMAIL": pLoginObject.EMAIL,
			 "PASSWORD": pLoginObject.PASSWORD,
			 "CUSTOMERMODE": pLoginObject.CUSTOMER_MODE,
			 "PAYMENTTYPE": pPmtObject.PAYMENTTYPE,
			 "CCNUMBER": pPmtObject.CCNUMBER,
			 "PAYMENTTYPE": pPmtObject.PAYMENTTYPE,
			 "EXPIRYMONTH": pPmtObject.EXPIRYMONTH,
			 "EXPIRYYEAR": pPmtObject.EXPIRYYEAR,
			 "ISSUENUMBER": pPmtObject.ISSUENUMBER,
			 "STARTMONTH": pPmtObject.STARTMONTH,
			 "STARTYEAR": pPmtObject.STARTYEAR,
			 "CSVNUMBER": pPmtObject.CSVNUMBER,
			 "IDEXTERNALON":EFR_OrderService.idExternal
			 }, 
			 fn
		);
    },

	/*
	ecomm version
	*/
	prepareOrderComplete2:function(pJSON) 
	{
		EFR_OrderService.addInvoiceAddress(pJSON);
	},

	/*
	*/
    addInvoiceAddress:function(pJSON) 
    {

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "UID":EFR_OrderService.UID,
			 "INVOKE":EFR_OrderService.COMMAND_ADDINVOICEADDRESS,
			 "TXID":pJSON.ORDER.TXID, /* given to us in return fr om prepareOrder call */
			 "IDEXTERNAL": EFR_OrderService.invAddressObject.IDEXTERNAL, 
			 "FULLNAME": EFR_OrderService.invAddressObject.FULLNAME, 
			 "COMPANY": EFR_OrderService.invAddressObject.COMPANY, 
			 "ADDRESSLINE1": EFR_OrderService.invAddressObject.ADDRESSLINE1, 
			 "ADDRESSLINE2": EFR_OrderService.invAddressObject.ADDRESSLINE2, 
			 "TOWN": EFR_OrderService.invAddressObject.TOWN, 
			 "COUNTY": EFR_OrderService.invAddressObject.COUNTY, 
			 "COUNTRY": EFR_OrderService.invAddressObject.COUNTRY, 
			 "POSTCODE": EFR_OrderService.invAddressObject.POSTCODE, 
			 "TEL": EFR_OrderService.invAddressObject.TEL, 
			 "FAX": EFR_OrderService.invAddressObject.FAX, 
			 "EMAIL": EFR_OrderService.invAddressObject.EMAIL
			 }, 
			 function(p)
			 {
				// now we've added the delivery address, we need to add the order details
				// remember this won't be added until EFR_OrderService.commitOrder has been called
				EFR_OrderService.addDeliveryAddress(pJSON);
			 }
		);
	},


	/*
	called when prepare order call has completed
	json will contain a txid
	*/
	prepareOrderComplete:function(pJSON) 
	{
		EFR_OrderService.addDeliveryAddress(pJSON);
	},
	
	/*
	*/
    addDeliveryAddress:function(pJSON) 
    {

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "UID":EFR_OrderService.UID,
			 "INVOKE":EFR_OrderService.COMMAND_ADDDELIVERYADDRESS,
			 "TXID":pJSON.ORDER.TXID, /* given to us in return fr om prepareOrder call */
			 "IDEXTERNAL": EFR_OrderService.delAddressObject.IDEXTERNAL, 
			 "FULLNAME": EFR_OrderService.delAddressObject.FULLNAME, 
			 "COMPANY": EFR_OrderService.delAddressObject.COMPANY, 
			 "ADDRESSLINE1": EFR_OrderService.delAddressObject.ADDRESSLINE1, 
			 "ADDRESSLINE2": EFR_OrderService.delAddressObject.ADDRESSLINE2, 
			 "TOWN": EFR_OrderService.delAddressObject.TOWN, 
			 "COUNTY": EFR_OrderService.delAddressObject.COUNTY, 
			 "COUNTRY": EFR_OrderService.delAddressObject.COUNTRY, 
			 "POSTCODE": EFR_OrderService.delAddressObject.POSTCODE, 
			 "TEL": EFR_OrderService.delAddressObject.TEL, 
			 "FAX": EFR_OrderService.delAddressObject.FAX, 
			 "EMAIL": EFR_OrderService.delAddressObject.EMAIL
			 }, 
			 function(p)
			 {
				// now we've added the delivery address, we need to add the order details
				// remember this won't be added until EFR_OrderService.commitOrder has been called
				EFR_OrderService.addAllOrderLines(pJSON);
			 }
		);
	},
	
	/*
	*/
    addAllOrderLines:function(pJSON) 
    {
		/* now, for each order line, make a remote call to update the cache in readiness for tx commit*/
		for (var i = 0; i < EFR_OrderService.clientCart.cartitems.length; i++)
		{
			EFR_OrderService.addOrderLine(pJSON, EFR_OrderService.clientCart.cartitems[i], i);
		}
	},
	
	/*
	*/
    addOrderLine:function(pJSON, pCartItem, nLineCount) 
    {

		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
				"LK":EFR_OrderService.strLicKey,
				"UID":EFR_OrderService.UID,
				"INVOKE":EFR_OrderService.COMMAND_ADDORDERLINE,
				"TXID":pJSON.ORDER.TXID, /* given to us in return from prepareOrder call */
				"LINECOUNT": nLineCount,
				"IDPRODUCT":pCartItem.IDPRODUCT,
				"DESCRIPTION":pCartItem.DESCRIPTION,
				"QTY":pCartItem.QTY,
				"UNIT_PRICE":pCartItem.UNIT_PRICE,
				"NET":pCartItem.NET,
				"VAT":pCartItem.VAT,
				"UNIT_PRICE_COST":pCartItem.UNIT_PRICE_COST,
				"NET_COST":pCartItem.NET_COST,
				"URL_TO_IMAGE":pCartItem.URL_TO_IMAGE,
				"UOI":pCartItem.UOI,
				"EXACT_MOUNT_OPENING_SIZE_IN_MM":pCartItem.EXACT_MOUNT_OPENING_SIZE_IN_MM,
				"EXACT_MOUNT_OPENING_HEIGHT_IN_MM":pCartItem.EXACT_MOUNT_OPENING_HEIGHT_IN_MM,
				"EXACT_MOUNT_OPENING_WIDTH_IN_MM":pCartItem.EXACT_MOUNT_OPENING_WIDTH_IN_MM,
				"INTERNAL_FRAME_SIZE":pCartItem.INTERNAL_FRAME_SIZE,
				"EXTERNAL_MOUNT_SIZE":pCartItem.EXTERNAL_MOUNT_SIZE,
				"APPROX_EXTERNAL_FRAME_SIZE":pCartItem.APPROX_EXTERNAL_FRAME_SIZE,
				"PRODUCT_TYPE":pCartItem.PRODUCT_TYPE,
				"IDGLAZING":pCartItem.IDGLAZING,
				"TOP_BORDER_IN_MM":pCartItem.TOP_BORDER_IN_MM,
				"BOTTOM_BORDER_IN_MM":pCartItem.BOTTOM_BORDER_IN_MM,
				"LEFT_BORDER_IN_MM":pCartItem.LEFT_BORDER_IN_MM,
				"RIGHT_BORDER_IN_MM":pCartItem.RIGHT_BORDER_IN_MM,
				"TOP_MOUNT_OFFSET":pCartItem.TOP_MOUNT_OFFSET,
				"HEIGHT_IN_MM":pCartItem.HEIGHT_IN_MM,
				"WIDTH_IN_MM":pCartItem.WIDTH_IN_MM,
				"IDTOPMOUNT":pCartItem.IDTOPMOUNT,
				"IDBOTTOMMOUNT":pCartItem.IDBOTTOMMOUNT,
				"IDLAYOUT":pCartItem.IDLAYOUT,
				"IDMOULDING":pCartItem.IDMOULDING,
				"IDSTYLE":pCartItem.IDSTYLE,
				"UPREFERENCE":pCartItem.UPREFERENCE,
				"UPNAME":pCartItem.UPNAME,
				"UPIMAGESIZEHEIGHT":pCartItem.UPIMAGESIZEHEIGHT,
				"UPIMAGESIZEWIDTH":pCartItem.UPIMAGESIZEWIDTH,
				"UPPAPERSIZEHEIGHT":pCartItem.UPPAPERSIZEHEIGHT,
				"UPPAPERSIZEWIDTH":pCartItem.UPPAPERSIZEWIDTH,
				"UPSELLINGPRICE":pCartItem.UPSELLINGPRICE,
				"UPUNITOFMEASURE":pCartItem.UPUNITOFMEASURE,
				"FITTED_BY":pCartItem.FITTED_BY,
				"URLIMAGEBEINGFRAMED":pCartItem.URLIMAGEBEINGFRAMED,
				"ALLOW_USER_SIZING":pCartItem.ALLOW_USER_SIZING,
				"ALLOW_USER_SIZING_OVERRIDE":pCartItem.ALLOW_USER_SIZING_OVERRIDE
			 }, 
			 function(p)
			 {
			 	
				if (p.EXCEPTIONS)
				{
					if (EFR_OrderService.timeoutTimer) 
						clearTimeout(EFR_OrderService.timeoutTimer);
			
					// call back to host to tell them the tx has completed
					if (typeof(EFR_OrderService.clientCallback) != "undefined")
					{
						EFR_OrderService.clientCallback(p);
					}
					else
						alert(COMPANY_NAME, "For the order service service to work, a callback must have been passed into the placeOrder() function");
				}
			 	else 
			 	{
			 		//debugger;
			 		
// 			 		alert("p.ORDERITEM.HEADER.ISLASTITEM: " + p.ORDERITEM.HEADER.ISLASTITEM);
				 	if (p.ORDERITEM.HEADER.ISLASTITEM)
					{
						EFR_OrderService.commitOrder(p);
					}
				}
			 }
		);
	},

	/*
	called when prepare order call has completed
	json will contain a txid
	*/
	  commitOrder:function(pJSON) 
	  {
		  
		//alert("About to commit order"); 
		  
		$.getJSON(EFR_OrderService.SOURCE + "/v1/facades/orderservice.asp?callback=?", 
			{ /* params */
			 "LK":EFR_OrderService.strLicKey,
			 "UID":EFR_OrderService.UID,
			 "INVOKE" : EFR_OrderService.COMMAND_COMMITORDER,
			 "TXID":pJSON.ORDERITEM.HEADER.TXID /* given to us in return from prepareOrder call */
			 }, 
			 EFR_OrderService.committed
		);
	  },
	
	committed:function(pJSON)
	{
		if (EFR_OrderService.timeoutTimer) 
			clearTimeout(EFR_OrderService.timeoutTimer);

		// call back to host to tell them the tx has completed
		if (typeof(EFR_OrderService.clientCallback) != "undefined")
		{
			/* must include the delivery charges */
			EFR_OrderService.clientCallback(pJSON);
		}
		else
			alert(COMPANY_NAME, "For the order service service to work, a callback must have been passed into the placeOrder() function");
	},
	
	reset_committed:function(pJSON)
	{
		
		if (EFR_OrderService.timeoutTimer) 
			clearTimeout(EFR_OrderService.timeoutTimer);

		// call back to host to tell them the tx has completed
		if (typeof(EFR_OrderService.clientCallback) != "undefined")
		{
			EFR_OrderService.clientCallback(pJSON);
		}
		else
			alert(COMPANY_NAME, "For the order service service to work, a callback must have been passed into the placeOrder() function");
	},
	
	cookie:function(name, value, options) 
	{
	    if (typeof value != 'undefined') { // name and value given, set cookie
	        options = options || {};
	        if (value === null) {
	            value = '';
	            options.expires = -1;
	        }
	        var expires = '';
	        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
	            var date;
	            if (typeof options.expires == 'number') {
	                date = new Date();
	                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	            } else {
	                date = options.expires;
	            }
	            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	        }

	        // CAUTION: Needed to parenthesize options.path and options.domain
	        // in the following expressions, otherwise they evaluate to undefined
	        // in the packed version for some reason...
	        var path = options.path ? '; path=' + (options.path) : '';
	        var domain = options.domain ? '; domain=' + (options.domain) : '';
	        var secure = options.secure ? '; secure' : '';
	        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	    } else { // only name given, get cookie
	        var cookieValue = null;
	        if (document.cookie && document.cookie != '') {
	            var cookies = document.cookie.split(';');
	            for (var i = 0; i < cookies.length; i++) {
	                var cookie = jQuery.trim(cookies[i]);
	                // Does this cookie string begin with the name we want?
	                if (cookie.substring(0, name.length + 1) == (name + '=')) {
	                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                    break;
	                }
	            }
	        }
	        return cookieValue;
	    }	
	}
}
