/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'BASKET PANEL' CLASS                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function BASKET_GINFO(parent){
	var JSObject = this;
	this.products = [];
	this.voucher;
	
	count = 0;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION INIT                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(no_products){
		
		count = no_products;
		
		for (var i=0; i<count; i++){
			var product = new PRODUCT_GINFO(JSObject);
			product.init(i);
			
			this.products.push(product);
		}
		
		this.voucher = new VOUCHER_GINFO(JSObject);
		this.voucher.init(myVoucher_JSON);
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CALCULATE TOTAL PRICE                                   */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.calculate = function(){
		var suma = 0;
		for (var i=0; i<this.products.length; i++){
			if (this.products[i] != null){
				suma += this.products[i].total;	
			}
		}
		
		if (document.getElementById("totalPrice") != null){
			//daca voucherul nu este valid
			if (!this.voucher.valid){
				document.getElementById("totalPrice").innerHTML = "<strong>"+JSObject.valueTransform(suma,2)+" lei</strong>";
			}
			//daca voucherul este valid
			else{
				document.getElementById("totalPrice_old").innerHTML = "<span class='price_line_through'>"+JSObject.valueTransform(suma,2)+" lei</span>";
				
				newValue = (suma - this.voucher.getValue() > 0) ? (suma - this.voucher.getValue()) : 0;
				
				document.getElementById("totalPrice").innerHTML = "<strong>"+JSObject.valueTransform(newValue,2)+" lei</strong>";
				
				document.getElementById("voucher_total_container").innerHTML = '<b>&ndash; '+JSObject.valueTransform(this.voucher.getValue(),2)+' lei</b>';
			}
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                     FUNCTION REMOVE PRODUCT                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.removeProduct = function(index){
		
		this.products[index] = null;
		
		var count = 0;
		for (var i=0; i<this.products.length; i++){
			if (this.products[i] != null){ 
				count++;
			}
		}
		
		if (count == 0){
			if (document.getElementById("basket_footer") != null){
				document.getElementById("basket_footer").align = "center";
				document.getElementById("basket_footer").vAlign = "middle";
				document.getElementById("basket_footer").innerHTML = "<span class='light_grey_text'>Nu exista produse in cos.</span>";
				
				//stergem containerul voucherului
				this.voucher.remove();
			}
		}
		else{
			//this.verifyVoucher();	
		}
		
		this.verifyVoucher();
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                     FUNCTION UPDATE PRODUCTS                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.updateProducts = function(){
			
		var count = 0;
		var ids = [];
		var freeassemblies = [];
		var warranties = [];
		var quantities = [];
		
		var todelete = [];
		
		for (var i=0; i<this.products.length; i++){
			if (this.products[i] != null){ 
				count++;
				ids.push(this.products[i].getID());
				freeassemblies.push(this.products[i].freeassembly);
				quantities.push(this.products[i].quantity);
				warranties.push(this.products[i].getWarrantyID());
				
				if (this.products[i].quantity == 0) todelete.push(i);
			}
			else{
				ids.push(null);
				quantities.push(null);
				freeassemblies.push(null);
				warranties.push(null);	
			}
		}
		
		
		//daca exista produse cu cantitatea =0, trebuie sterse
		if (todelete.length > 0){
			for (var i=0; i<todelete.length; i++){
				this.products[todelete[i]].removeHTML();
				this.removeProduct(this.products[todelete[i]].index);
			}
		}
		
		//alert(LOCALPATH+"basket_modify.php?"+'products_ids='+ids+'&'+'products_quantities='+quantities+'&'+'products_warranties='+warranties);
		www.post(LOCALPATH+"basket_modify.php",
				 'products_ids='+ids+'&'+
				 'products_quantities='+quantities+'&'+
				 'products_freeassemblies='+freeassemblies+'&'+
				 'products_warranties='+warranties, 
				 function(response) {
					if (isNaN(response)) return;
					
					JSObject.verifyVoucher();
					
					alert("Cosul de cumparaturi a fost actualizat cu succes!");
					
				 }
				 );	
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                     FUNCTION VERIFY VOUCHER                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.verifyVoucher = function(){
		
		var ids = [];
		
		for (var i=0; i<this.products.length; i++){
			if (this.products[i] != null){ 
				count++;
				ids.push(this.products[i].getID());
			}
		}
		
		//retinem codul introdus
		this.voucher.registerCode();
		
		//codul nu a fost completat, este gol
		if (this.voucher.code.length <=4){
			document.getElementById("voucher_msg_container").style.display = "none";
			JSObject.calculate();
			return;
		}
		
		//alert(LOCALPATH+"voucher_validate.php?"+'products_ids='+ids+'&'+'code='+this.voucher.getCode());
		www.post(LOCALPATH+"voucher_validate.php",
				 'products_ids='+ids+'&'+
				 'code='+JSObject.voucher.getCode(), 
				 function(response) {
					
					//alert(response);
					if (String(response).indexOf('Fatal error') != -1) return;
					
					var myObject = eval('(' + response + ')');
					
					JSObject.voucher.init(myObject);	
					
					//daca voucherul nu este valid
					if (!JSObject.voucher.valid){
						if (document.getElementById("voucher_container") == null) return;
						
						document.getElementById("voucher_container").innerHTML = JSObject.voucher.voucher_HTML_invalid;
						document.getElementById("voucher_msg_container").getElementsByTagName("span")[0].innerHTML = "Codul introdus nu este valid!";
						document.getElementById("voucher_msg_container").style.display = "block";
						JSObject.voucher.completeCode();
						
						document.getElementById("totalPrice_old").style.display = "none";
					}
					//daca voucherul este valid
					else{
						
						if (JSObject.voucher.products_ids.length > 0){
							document.getElementById("voucher_container").innerHTML = JSObject.voucher.voucher_HTML_valid;
							//alert(document.getElementById("voucher_container").innerHTML)
							code = JSObject.voucher.set_1 +" - "+ JSObject.voucher.set_2 +" - "+ JSObject.voucher.set_3 +" - "+ JSObject.voucher.set_4;
							document.getElementById("voucher_code").innerHTML = code;
							document.getElementById("voucher_msg").innerHTML = JSObject.voucher.message;
							
							document.getElementById("totalPrice_old").style.display = "block";
						}
						//daca voucherul este valid dar nu pentru produsele din cos
						/*else{
							document.getElementById("voucher_container").innerHTML = this.voucher.voucher_HTML_invalid;
							this.voucher.completeCode();
							document.getElementById("voucher_msg_container").getElementsByTagName("span")[0].innerHTML = "Codul introdus nu este valid pentru produsele din cos!";
							document.getElementById("voucher_msg_container").style.display = "block";
						}*/
					}
					
					JSObject.calculate();
				 }
				 );	
	}
	
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                         FUNCTION TRANSFORM NUMBER TO STRING  EX: 2.347,00 lei                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.valueTransform = function(val,decimals){
		var decimalValue = Math.round((val - Math.floor(val))*Math.pow(10,decimals));
		
		var letters = String(Math.floor(val)).split("");
		letters.reverse();
		
		var stringValue = "";
		
		for (var i=0; i<letters.length; i++){
			stringValue += letters[i];
			if ((i+1) % 3 == 0 && (i+1)<letters.length){
				stringValue += ".";	
			}
		}
		
		
		var val = stringValue.split("");
		val.reverse();
		return String(val.join("")+","+decimalValue);
		
	}
	
}