﻿function Basket()
{
	//Public properties
	this.IdProduct			= null;
	this.ProductName		= null;
	this.IdCustomer			= null;
	this.Quantity   		= null;
	this.Price   		        = null;
	this.ProductCode   	        = null;
	
	//Private objects
	var oXmlHttp = null;
	var mIdProduct = null;
	var mProductName = null;
	var mQuantity = null;
	var mPrice = null;
        var mProductCode = null;
        
	
	function Initialize()
	{
		if (window.XMLHttpRequest)
		{
			oXmlHttp = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject)
		{
			oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
		} 
		else 
		{
			alert('Your browser does not support asynchronous(AJAX) technology!');
		}
	}
		
	// Posts desired data
	this.Set = function()
	{       		
		
		mProductName = this.ProductName;
		mQuantity = this.Quantity;
		mPrice = this.Price;
		mIdProduct = this.IdProduct;
		mProductCode = this.ProductCode;

		Initialize();
				
		oXmlHttp.open("POST", "/WebService/Basket.asmx", true);
		oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

		oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/Insert");
        
		sendText = new String();
        
		sendText = sendText.concat('<?xml version="1.0" encoding="utf-8"?>');
		sendText = sendText.concat('<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">');
		sendText = sendText.concat('<soap:Body>');		
		sendText = sendText.concat('<Insert xmlns="http://tempuri.org/">');
		
		sendText = sendText.concat('<IdProduct>' + this.IdProduct + '</IdProduct>');
		sendText = sendText.concat('<IdCustomer>' + this.IdCustomer + '</IdCustomer>');
		sendText = sendText.concat('<Quantity>' + this.Quantity + '</Quantity>');
		
		sendText = sendText.concat('</Insert>');

		
		sendText = sendText.concat('</soap:Body>');
		sendText = sendText.concat('</soap:Envelope>');
        
		oXmlHttp.send(sendText);

		oXmlHttp.onreadystatechange = doReadyStateChange;
	}
	
	// Posts desired data
	this.Update = function()
	{       		
	    
	    mIdProduct = this.IdProduct;
		mProductName = this.ProductName;
		mQuantity = this.Quantity;
		mPrice = this.Price;
		mProductCode = this.ProductCode;


		Initialize();
				
		oXmlHttp.open("POST", "/WebService/Basket.asmx", true);
		oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

		oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/Update");
        
		sendText = new String();
        
		sendText = sendText.concat('<?xml version="1.0" encoding="utf-8"?>');
		sendText = sendText.concat('<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">');
		sendText = sendText.concat('<soap:Body>');		
		sendText = sendText.concat('<Update xmlns="http://tempuri.org/">');
		
		sendText = sendText.concat('<IdProduct>' + this.IdProduct + '</IdProduct>');
		sendText = sendText.concat('<IdCustomer>' + this.IdCustomer + '</IdCustomer>');
		sendText = sendText.concat('<Quantity>' + this.Quantity + '</Quantity>');
		
		sendText = sendText.concat('</Update>');

		
		sendText = sendText.concat('</soap:Body>');
		sendText = sendText.concat('</soap:Envelope>');
        
		oXmlHttp.send(sendText);

		oXmlHttp.onreadystatechange = doReadyStateChangeUpdate;
	}
	
	function doReadyStateChangeUpdate()
	{		
		if(oXmlHttp.readyState==4)
		{
			if(oXmlHttp.status == 200)
			{
				try
				{	                    
                    if(oXmlHttp.responseText.indexOf('YES') != -1)
                    {
					    
					    var outTextArray = oXmlHttp.responseText.split('#');
					    
					    var objOl = document.getElementById('basketItems');
					    var objLi = document.createElement("li");
					    var objTxtMiniQuantity = document.getElementById('txtMiniQuantity' + mIdProduct);
                        
                        objTxtMiniQuantity.value = mQuantity;
                        
                        var objTotalSpan = document.getElementById('totalSpan');
                        objTotalSpan.innerHTML = parseFloat(outTextArray[1]).toFixed(2);

				    }
					else if(oXmlHttp.responseText.indexOf('ZERO') != -1)
					{
					    alert('Product quantity can not be less than one!');
					}
					else
					{
					    alert('We are sorry but the item you have selected is currently unavailable or out of stock. Please contact us at parts@demilecusa.com, or contact us in the office at 1-877-DEMILEC (336-4532) and ask for the parts department. One of our Customer Service Representatives will be more than happy to assist you. We are available from 8:00 a.m. – 5:00 p.m. Monday-Friday (Central Time). We appreciate your business.');
					}
					
				}
				catch(e){}
			}
			else
			{
				alert('There is a error trying to retreive data! Try again later.');
			}
		}
	}
		
	function doReadyStateChange()
	{		
		if(oXmlHttp.readyState==4)
		{
			if(oXmlHttp.status == 200)
			{
				try
				{	                    
                    if(oXmlHttp.responseText.indexOf('YES') != -1)
                    {
					    var objOl = document.getElementById('basketItems');
					    var objLi = document.createElement("li");

                        objLi.innerHTML = mProductName + " #" + mProductCode + " ~ $ " + mPrice + "&nbsp;<a href=\"javascript:Delete('Are you sure you want to delete product from cart?', '?idProduct=" + mIdProduct + "&deleteProduct=true');\"><img alt=\"Delete\" title=\"Remove\" src=\"Repository/ShopImages/html/ShopButtons/trashCan.gif\" width=\"15\" height=\"17\" border=\"0\" align=\"absmiddle\" /></a>&nbsp;&nbsp;&nbsp;<div align=\"right\" style=\"margin:0 5px 3px 0;\"><b>Quant. " + mQuantity + "</b>&nbsp;<input type=\"text\" readonly=\"true\" class=\"miniQuantity\" value=\"1\" id=\"txtMiniQuantity" + mIdProduct + "\" /><input type=\"button\" onclick=\"SetQuantity('txtMiniQuantity" + mIdProduct + "', 'minus', " + mIdProduct + ");\" class=\"miniButton\" id=\"btnMinus\" value=\"-\" /><input type=\"button\" onclick=\"SetQuantity('txtMiniQuantity" + mIdProduct + "', 'plus', " + mIdProduct + ");\" class=\"miniButton\" id=\"btnPlus\" value=\"+\" /></div>";

                        objOl.appendChild(objLi);
                        
                        var objTotalSpan = document.getElementById('totalSpan');
                        var totalSuma = parseFloat(objTotalSpan.innerHTML) + parseFloat(mPrice);
                        objTotalSpan.innerHTML = parseFloat(totalSuma).toFixed(2);

					    alert('Product successfully added to basket!');
				    }
					else if(oXmlHttp.responseText.indexOf('NO') != -1)
					{
					    alert('Product already in basket!');
					}
					else if(oXmlHttp.responseText.indexOf('ZERO') != -1)
					{
					    alert('Product quantity can not be less than one!');
					}
					else
					{
					    alert('We are sorry but the item you have selected is currently unavailable or out of stock. Please contact us at parts@demilecusa.com, or contact us in the office at 1-877-DEMILEC (336-4532) and ask for the parts department. One of our Customer Service Representatives will be more than happy to assist you. We are available from 8:00 a.m. – 5:00 p.m. Monday-Friday (Central Time). We appreciate your business.');
					}
					
				}
				catch(e){}
			}
			else
			{
				alert('There is a error trying to retreive data! Try again later.');
			}
		}
	}
}