/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'PRODUCT PANEL' CLASS                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function PRODUCT_GINFO(parent){
	var JSObject = this;
	this.basket = parent;
	this.index;
	
	var _productContainer;
	var _productId;
	var _productFreeAssemblySelect;
	var _productWarrantySelect;
	var _productWarrantyPrice;
	var _productUnitPrice;
	var _productQuantity;
	var _productDeleteLink;
	var _productTotalPrice;
	var _totalPrice = "totalPrice";
	
	this.quantity = 0;
	this.warranty = 0;
	this.freeassembly = '';
	this.price = 0;
	this.total = 0;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION INIT ITEM                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(_index){
		this.index = _index;
		
		_productContainer = document.getElementById("product_container_"+this.index);
		_productId = document.getElementById("productId_"+this.index);
		
		
		if (document.getElementById("warrantySelect_"+this.index) != null){
			_productWarrantySelect = document.getElementById("warrantySelect_"+this.index);
			_productWarrantyPrice = document.getElementById("warrantyPrice_"+this.index);
			this.warranty = _productWarrantyPrice.value;
		}
		
		if (document.getElementById("freeassemblySelect_"+this.index) != null){
			_productFreeAssemblySelect = document.getElementById("freeassemblySelect_"+this.index);
			this.freeassembly = _productFreeAssemblySelect.value;
		}
		
		_productUnitPrice = document.getElementById("unitPrice_"+this.index);
		this.price = _productUnitPrice.value;
		
		_productQuantity = document.getElementById("quantity_"+this.index);
		this.quantity = _productQuantity.value;
		
		_productDeleteLink = document.getElementById("deleteProduct_"+this.index);
		_productTotalPrice = document.getElementById("totalPrice_"+this.index);
		
		
		this.total = Number(String(this.quantity)) * (Number(String(this.price)) + Number(String(this.warranty)));
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'Quantity' ACTIONS                                   */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Quantity = new INPUTFIELD(this, _productQuantity);
		this._inp_Quantity.setValidationType("integer");
		this._inp_Quantity.negative = false;
		this._inp_Quantity.setOnKeyUpFunction(function(type){
													var Quantity = this;
													Quantity.addData(parseInt(this.input.value));
													
													JSObject.total = parseFloat( ((Quantity.data) * (Number(String(JSObject.price)) + Number(String(JSObject.warranty)))).toFixed(2) );
													if (isNaN(JSObject.total)) JSObject.total = 0;
													JSObject.quantity = Quantity.data;
													
													_productTotalPrice.innerHTML = "<strong>"+JSObject.basket.valueTransform(JSObject.total,2)+" lei</strong>";
													
													//calculeaza totalurile
													JSObject.basket.calculate();
													
													});
		this._inp_Quantity.setOnBlurFunction(function(){
													
													if (this.input.value > this.maxval) this.input.value = this.maxval;
													if (this.input.value < this.minval) this.input.value = this.minval;
													
													this.input.value = Math.abs(parseInt(this.input.value));
													
													this.onkeyupF("blur");
													});
		this._inp_Quantity.maxval = 100;
		this._inp_Quantity.minval = 0;
		this._inp_Quantity.initActions();
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        SELECT 'Warranty' ACTIONS                                  */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		if (document.getElementById("warrantySelect_"+this.index) != null){
				
			_productWarrantySelect.onchange = function(){
				
				//alert(LOCALPATH+"basket_getwarrantyprice.php?"+'product_id='+_productId.value+'&'+'product_warranty='+this.value);
				www.post(LOCALPATH+"basket_getwarrantyprice.php",
					 'product_id='+_productId.value+'&'+
					 'product_warranty='+this.value, 
					 function(response) {
						//alert(response)
						if (isNaN(response)) return;
						
						JSObject.warranty = response;
						
						JSObject.total = parseFloat( ((JSObject.quantity) * (Number(String(JSObject.price)) + Number(String(JSObject.warranty)))).toFixed(2) );
						
						if (isNaN(JSObject.total)) JSObject.total = 0;
						
						_productTotalPrice.innerHTML = "<strong>"+JSObject.basket.valueTransform(JSObject.total,2)+" lei</strong>";
						_productWarrantyPrice.parentNode.childNodes[0].innerHTML = "<strong>"+JSObject.basket.valueTransform(JSObject.warranty,2)+" lei</strong>";
						
						//calculeaza totalurile
						JSObject.basket.calculate();
						
					 }
					 );	
				
			}
			
		}
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        SELECT 'FreeAssembly' ACTIONS                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		if (document.getElementById("freeassemblySelect_"+this.index) != null){
				
			_productFreeAssemblySelect.onchange = function(){
				JSObject.freeassembly = this.value;
			};	
			
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        DELETE  ACTIONS                                            */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		_productDeleteLink.onclick = function(){
			
			var conf = confirm("Sunteti sigur ca doriti sa stergeti acest produs din cosul de cumpataruri?")
			
			if (!(conf)) return;
			
			//alert(LOCALPATH+"basket_delete.php?"+'product_id='+_productId.value+'&'+'order_id='+JSObject.index);
			www.post(LOCALPATH+"basket_delete.php",
				 'product_id='+_productId.value+'&'+
				 'order_id='+JSObject.index, 
				 function(response) {
					//alert(response)
					if (isNaN(response)) return;
					
					JSObject.removeHTML();
					JSObject.basket.removeProduct(JSObject.index);
					
					//calculeaza totalurile
					JSObject.basket.calculate();
					
				 }
				 );	
		}
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION GET ID ITEM                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.getID = function(){
		return 	_productId.value;
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                     FUNCTION GET WARRANTY ORDER ITEM                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.getWarrantyID = function(){
		if (_productWarrantySelect == null) return null;
		
		return 	_productWarrantySelect.value;
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION UPDATE ITEM                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.removeHTML = function(){
		var row = _productContainer.parentNode;
		var table = row.offsetParent;
		while (table.tagName != "TABLE"){
			table = table.offsetParent;
		}
		
		table.deleteRow(row.rowIndex);
	}
	
	
}