/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'VOUCHER PANEL' CLASS                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function VOUCHER_GINFO(parent){
	var JSObject = this;
	this.basket = parent;
	
	this.set_1 = "";
	this.set_2 = "";
	this.set_3 = "";
	this.set_4 = "";
	this.id = -1;
	this.valid = false;
	this.products_ids = [];
	this.value = 0;
	this.type = ""; //bonus or discount
	
	this.code = "";
	
	this._voucherSet_1;
	this._voucherSet_2;
	this._voucherSet_3;
	this._voucherSet_4;
	
	this.voucher_msg_container;
	//retine codul html pentru cele 4 inputuri ale voucherului
	this.voucher_HTML_invalid = '<table width="100%" height="100%" cellpadding="0" cellspacing="0">';
	this.voucher_HTML_invalid += '<tr>';
	this.voucher_HTML_invalid += '<td width="90" style="padding-left:5px" align="center"><span style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif; font-size:18px; color:#FFFFFF">Voucher:</span><div style="display:none; margin:0px"><a href="#" class="white_link">modifica</a></div></td>';
	this.voucher_HTML_invalid += '<td align="center"><input type="text" class="input_voucher" id="voucher_set_1" name="voucher_set_1" style="width:60px; text-align:center" maxlength="4" /><span class="black_price" style="color:#FFFFFF">&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;</span>';
	this.voucher_HTML_invalid += '<input type="text" class="input_voucher" id="voucher_set_2" name="voucher_set_2" style="width:60px; text-align:center" maxlength="4" /><span class="black_price" style="color:#FFFFFF">&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;</span>';
	this.voucher_HTML_invalid += '<input type="text" class="input_voucher" id="voucher_set_3" name="voucher_set_3" style="width:60px; text-align:center" maxlength="4" /><span class="black_price" style="color:#FFFFFF">&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;</span>';
	this.voucher_HTML_invalid += '<input type="text" class="input_voucher" id="voucher_set_4" name="voucher_set_4" style="width:60px; text-align:center" maxlength="4" /><div style="display:none" id="voucher_msg_container"><span class="black_price" style="color:#FFFFFF">&nbsp;&nbsp;&nbsp;&nbsp;Codul intrudus nu este valid!</span></div></td>';
	this.voucher_HTML_invalid += '</tr>';
	this.voucher_HTML_invalid += '</table>';
	
	//retine codul html pentru voucherul valid
	this.voucher_HTML_valid = '<table width="100%" height="51" cellpadding="0" cellspacing="0">';
	this.voucher_HTML_valid += '<tr>';
	this.voucher_HTML_valid += '<td width="110" style="border-right:1px solid #e2bfbe" align="center"><span style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif; font-size:18px; color:#FFFFFF">Voucher:</span><div style="display:block; margin:0px"><a href="javascript: if (BasketObject.info.voucher) BasketObject.info.voucher.modify();" class="white_link">modifica</a><span class="black_price" style="color:#FFFFFF; margin:0px"> | </span><a href="javascript: if (BasketObject.info.voucher) BasketObject.info.voucher.deleteVoucher();" class="white_link">sterge</a></div></td>';
	this.voucher_HTML_valid += '<td align="center" style="padding-left:10px; padding-right:10px"><span class="black_price" style="color:#FFFFFF; font-size: 14px; font-weight:bold" id="voucher_code"></span><br><span class="black_price" style="color:#FFFFFF; font-size:11px" id="voucher_msg"></span><div style="display:none" id="voucher_msg_container"><span class="black_price" style="color:#FFFFFF"></span></div></td>';
	this.voucher_HTML_valid += '<td width="88" align="right" style="padding-right:10px; border-left:1px solid #e2bfbe"><span class="black_price" style="color:#FFFFFF" id="voucher_total_container"><b></b></span></td>';
	this.voucher_HTML_valid += '</tr>';
	this.voucher_HTML_valid += '</table>';
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION INIT ITEM                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(JSON){
				
		//daca avem un voucher valid
		if (Boolean(JSON.valid)){
			//
			//alert(JSON.code)
			this.code = JSON.code;
			this.set_1 = this.code.split(" ")[0];
			this.set_2 = this.code.split(" ")[1];
			this.set_3 = this.code.split(" ")[2];
			this.set_4 = this.code.split(" ")[3];
		}
		//daca voucherul este invalid
		else{
			this._voucherSet_1 = document.getElementById("voucher_set_1");
			this._voucherSet_2 = document.getElementById("voucher_set_2");
			this._voucherSet_3 = document.getElementById("voucher_set_3");
			this._voucherSet_4 = document.getElementById("voucher_set_4");
			
			this.code = "";
		}
		
		this.voucher_msg_container = document.getElementById("voucher_msg_container");
		
		this.id = JSON.voucherid;
		this.valid = Boolean(JSON.valid);
		this.type = JSON.type;
		this.value = Number(JSON.value);
		this.message = JSON.message;
		this.products_ids = JSON.ids;
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION GET ID ITEM                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.getID = function(){
		return 	this.id;
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION GET CODE                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.getCode = function(){
		var code = this.set_1 +" "+ this.set_2 +" "+ this.set_3 +" "+ this.set_4;
		return code;
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION REGISTER CODE                                  */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.registerCode = function(){
		if (document.getElementById("voucher_set_1") != null){
			this.set_1 = document.getElementById("voucher_set_1").value;
			this.set_2 = document.getElementById("voucher_set_2").value;
			this.set_3 = document.getElementById("voucher_set_3").value;
			this.set_4 = document.getElementById("voucher_set_4").value;
			this.code = this.getCode();
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION COMPLITE CODE                                  */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.completeCode = function(){
		document.getElementById("voucher_set_1").value = this.set_1;
		document.getElementById("voucher_set_2").value = this.set_2;
		document.getElementById("voucher_set_3").value = this.set_3;
		document.getElementById("voucher_set_4").value = this.set_4;
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION GET VALUE                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.getValue = function(){
		
		if (this.type.toLowerCase() == "bonus"){
			return Number(this.value);	
		}
		else if (this.type.toLowerCase() == "discount"){
			var sum = 0;
			
			//cautam produsele pe care se aplica voucherul
			for (var i=0; i<this.products_ids.length; i++){
				var product_id = this.products_ids[i];
				
				for (var j=0; j<JSObject.basket.products.length; j++){
					if (JSObject.basket.products[j] != null){ 
						if (JSObject.basket.products[j].getID() == product_id){
							sum += JSObject.basket.products[j].quantity * JSObject.basket.products[j].price;
							break;
						}
					}
				}
			}
			
			//aplicam voucherul pe suma
			discount = sum * (Number(this.value) / 100);
			
			if (isNaN(discount)) discount = 0;
			
			return parseFloat(discount.toFixed(2));
		}
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION MODIFY VOUCHER                                 */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.modify = function(){
			
		//daca voucherul este valid 
		if (Boolean(this.valid)){
			document.getElementById("voucher_container").innerHTML = this.voucher_HTML_invalid;
			this.completeCode();
			
			document.getElementById("totalPrice_old").style.display = "none";
			document.getElementById("totalPrice").innerHTML = "<strong>"+document.getElementById("totalPrice_old").getElementsByTagName("span")[0].innerHTML+"</strong>";
			
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION DELETE VOUCHER                                 */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.deleteVoucher = function(){
		
		var conf = confirm("Esti sigur ca vrei sa stergi acest voucher?");
		
		if (!conf) return;
		
		www.post(LOCALPATH+"voucher_delete.php",
				 'voucherid='+JSObject.id, 
				 function(response) {
					
					//
					JSObject.set_1 = "";
					JSObject.set_2 = "";
					JSObject.set_3 = "";
					JSObject.set_4 = "";
					
					JSObject.modify();
					JSObject.valid = false;
				 }
				 );	
		
	}
	
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                           FUNCTION REMOVE ITEM                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.remove = function(){
		var container = document.getElementById("voucher_container");
		while (container.parentNode.tagName != "TD"){
			container = container.parentNode;
		}
		
		container = container.parentNode;
		while (container.parentNode.tagName != "TABLE"){
			container = container.parentNode;
		}
		
		while (container.rows.length > 1){
			container.deleteRow(0);	
		}
	}
	
	
}