﻿Type.registerNamespace('ShoppingCartInBasket.Controls');

ShoppingCartInBasket.Controls.ShoppingCart = function(element) {
	ShoppingCartInBasket.Controls.ShoppingCart.initializeBase(this, [element]);
	this._products = [];
	this._productQuantity = [];
	this._sessionID = null;
	this._shipOptionPrice = null;
}

ShoppingCartInBasket.Controls.ShoppingCart.prototype = 
{
	initialize: function() {
		ShoppingCartInBasket.Controls.ShoppingCart.callBaseMethod(this, 'initialize');
		this.render();
	},
	// Page Size
	get_sessionID: function() { return this._sessionID; },
	set_sessionID: function(value) { this._sessionID = value; }, 
	
    // ship Option
    
	get_shipOptionPrice: function() { return this._shipOptionPrice; },
	set_shipOptionPrice: function(value) { this._shipOptionPrice = value; }, 
	
	
	 // Products
	get_products: function() { return this._products; },
	set_products: function(value) { this._products = value; }, 
    
    dataBind: function()
	{
	    this.render();
	},
	
	render: function()
	{
	
	    // Get a reference to the passed in element
		var element = this.get_element();
		
        // Clear content from the element
		element.innerHTML = '';
        
        
        
        
        // Add the wrapper table and header row
        var table = document.createElement('table');
        Sys.UI.DomElement.addCssClass(table, 'cartTableBasket');
       // table.setAttribute("cellpadding", "10px");
        table.setAttribute("cellspacing", "10px");
		element.appendChild(table);	
		
	    var tbody = document.createElement('tbody');
		table.appendChild(tbody);	
		
  
  		var row = document.createElement('tr');
		tbody.appendChild(row);	
		
		// Add the header row cell
		var cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'headercartTable');
		cell.innerHTML = "תמונה";
		row.appendChild(cell);	
		
	    cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'headercartTable');
		cell.innerHTML = "ספר";
		row.appendChild(cell);	
		
        cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'headercartTable');
		cell.innerHTML = "כמות";
		row.appendChild(cell);	
		  cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'headercartTable');
		cell.innerHTML = "מחיר";
		row.appendChild(cell);	
		  cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'headercartTable');
		cell.innerHTML = "מחיקה";
		row.appendChild(cell);	

        // Iterate through the items in the collection
		for (var i = 0; i < this._products.length; i++) 
		{
		    // Add the sub row
		    var subrow = document.createElement('tr');
		    tbody.appendChild(subrow);	
		    
		    // Add the fourth cell
            subcell = document.createElement('td');
            var img = document.createElement('img');
            img.setAttribute("src", "../files/" + this._products[i].Picture);
            img.className="CartImg";
            subcell.appendChild(img);
            subrow.appendChild(subcell);
		    
		
            // Add The Second Cell
		    subcell = document.createElement('td');
		    subrow.appendChild(subcell);	
		    
		    //Add the product name
		    var ctrl = document.createElement('span');
            ctrl.innerHTML = this._products[i].ProductName;
            Sys.UI.DomElement.addCssClass(ctrl, 'bodyTextBlack');
            subcell.appendChild(ctrl);	
            
            // Add the third cell
            subcell = document.createElement('td');
            subrow.appendChild(subcell);	

            // Add the textbox
            var inp = document.createElement('input');
            inp.type = "text";
            inp.size = "2";
            Sys.UI.DomElement.addCssClass(inp, 'checkOutFormsField');
            inp.value = this._products[i].Qty;
            $addHandler(inp, 'change', Function.createCallback(Function.createDelegate(this, this.itemQtyUpdated), i));
            subcell.appendChild(inp);	
            
            
	
         
         
            subcell = document.createElement('td');
        
		     var small = document.createElement('small');
            small.innerHTML = this._products[i].Price + "&nbsp; ש''ח" ;
            subcell.appendChild(small);
            subrow.appendChild(subcell);
            
            
                // Add The Cell
		    var subcell = document.createElement('td');
		    subcell.align='center';
		    subrow.appendChild(subcell);	
		    
		    img = document.createElement('img');
		    img = document.createElement('img');
            img.setAttribute("src", "../App_Themes/superSefer/images/basketDelete.jpg");
            Sys.UI.DomElement.addCssClass(img, 'blImg');
            img.alt = "מחק";
            subcell.appendChild(img);

            $addHandler(img, 'click', Function.createCallback(Function.createDelegate(this, this.itemDeleteClicked), i));
            
	

        
        }
         // Add the header row
		row = document.createElement('tr');
		tbody.appendChild(row);	
     
        //שורת סך הכל
        // Add the header row cell
		var cell = document.createElement('td');
		Sys.UI.DomElement.addCssClass(cell, 'sumTotal');
		cell.innerHTML = "סה''כ";
		row.appendChild(cell);	
		//עמודה ריקה
        cell = document.createElement('td');
		row.appendChild(cell);	

		// Add The Cell
		cell = document.createElement('td');
		row.appendChild(cell);	
         //Show no items in cart, if empty, otherwise, show subtotal
        if (this._products.length == 0)
            this.showEmpty(cell);
        else
            this.showSubTotal(cell);
        
        //הוספת אפשרות המשלוח
        row = document.createElement('tr');
		tbody.appendChild(row);	
        cell = document.createElement('td');
        cell.innerHTML = "משלוח";
        row.appendChild(cell);	
        //עמודה ריקה
        cell = document.createElement('td');
		row.appendChild(cell);
        cell = document.createElement('td');
        Sys.UI.DomElement.addCssClass(cell, 'sumTotal');
        cell.innerHTML = this._shipOptionPrice + " ש''ח";
        row.appendChild(cell);	
        //שורה תחתית
        //סך כללי כולל משלוח
        row = document.createElement('tr');
		tbody.appendChild(row);	
        cell = document.createElement('td');
        cell.innerHTML = "סך כללי";
        row.appendChild(cell);
        //עמודה ריקה
        cell = document.createElement('td');
		row.appendChild(cell);
        cell = document.createElement('td');
        Sys.UI.DomElement.addCssClass(cell, 'sumTotal');
        cell.innerHTML = this.showSubTotalWithShip() + " ש''ח";
        row.appendChild(cell);	
          

	},
	
	itemDeleteClicked: function(sender, index)
	{
	    // Remove the item from the array
	     Shopping.Services.CatalogService.deleteProductFromBasket(this._products[index].ProductId,this._sessionID );
	    this._products.splice(index, 1);
	    this.render();
	},
	
	itemQtyUpdated: function(sender, index)
	{
	    // Get the new qty
	    var qty = parseInt(sender.target.value);
	    if (qty == 0 )
	    {
	    
	        Shopping.Services.CatalogService.deleteProductFromBasket(this._products[index].ProductId,this._sessionID );
	        this._products.splice(index, 1);
        }
	    else if (qty >0)
	    {
	        this._products[index].Qty = qty;
    	    Shopping.Services.CatalogService.updateProductToBasket(this._products[index].ProductId,this._sessionID,  this._products[index].Qty );
    	
    	}
    	    
	    this.render();
	},
	
	showEmpty: function(parentDiv)
	{
        var ctrl = document.createElement('div');
        Sys.UI.DomElement.addCssClass(ctrl, 'readOnlyField');
        ctrl.innerHTML = "(סל הקניות שלך ריק)";
        parentDiv.appendChild(ctrl);	
	},
	
	showSubTotal: function(parentDiv)
	{
    
	    var bld = document.createElement('strong');
	    bld.innerHTML =  this.calculateSubtotal() + " ש''ח";
	    parentDiv.appendChild(bld);
	},
	
	calculateSubtotal: function()
	{
	    var subTotal = 0;
	    
	    for (var i = 0; i < this._products.length; i++) 
		{
		    subTotal += (this._products[i].Qty * this._products[i].Price);
		}
		
		return subTotal;
	},
	showSubTotalWithShip:function()
	{
	 var subTotalWithShip = 0;
	 subTotalWithShip = parseInt(this.calculateSubtotal()) + parseInt(this._shipOptionPrice);
	 return subTotalWithShip;
	}
}
	
ShoppingCartInBasket.Controls.ShoppingCart.registerClass('ShoppingCartInBasket.Controls.ShoppingCart', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();

