// JavaScript Document
/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'PORTFOLIO SELL SHARES' CLASS                                 */          
/*                                                                                                   */
/*****************************************************************************************************/

function SHARES_SELL(parent){
	var JSObject = this;
	this.type = "Sell shares"; 
	this.arr_inputs = ["_inp_Date","_inp_Quantity","_inp_Price","_inp_BCharge"];
	
	this.id;//id-ul companiei
	this.broker_commission = 0;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS ADD                                         */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initAdd = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Date = new INPUTFIELD(this, document.getElementById('shares_date'));
		this._inp_Quantity = new INPUTFIELD(this, document.getElementById('shares_quantity'));
		this._inp_Price = new INPUTFIELD(this, document.getElementById('shares_price'));
		this._inp_BCharge = new INPUTFIELD(this, document.getElementById('shares_bcharge'));
		
		this._inp_Portfolio =  new INPUTFIELD(this, document.getElementById('portfolio'));
		this._inp_Shares =  new INPUTFIELD(this, document.getElementById('shares'));
		
		this.form = document.getElementById("addsellshares_form");
		
		this.arr_inputs.push("_inp_Portfolio");
		this.arr_inputs.push("_inp_Shares");
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'DATE' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Date.input;
		this._inp_Date.setRequired("yes");
		this._inp_Date.setReadySubmit(false);
		this._inp_Date.setForm(this.form); 
		this._inp_Date.setValidationType("date");
		var errors = ["This information is required.",
				      "Invalid date.<bR> Date format: DD / MM / YYYY"];
		this._inp_Date.addErrors(errors);
		this._inp_Date.setErrorsContainer("sellshares_date_container");
		this._inp_Date.initActions();
		this._inp_Date.input.onfocus = function(){
			if (this.value.stripBlanks() == "DD/MM/YYYY"){
				this.className = "input_field2";
				this.value = "";
			}
			JSObject.ajax = false;
		}
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'QUANTITY' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Quantity.input;
		this._inp_Quantity.addData(input.value);
		this._inp_Quantity.setRequired("yes");
		this._inp_Quantity.setValidationType("integer");
		this._inp_Quantity.setOnKeyUpFunction(function(){
													var Quantity = this;	   
													this.addData(this.input.value);
													
													var Price = JSObject._inp_Price;
													var BCharge = JSObject._inp_BCharge;
													var Commission = JSObject.broker_commission;
													
													//calculate BCharge 
													BCharge.input.value = parseFloat( ((Commission/100) * Quantity.data * Price.data).toFixed(BCharge.decimals) );
													BCharge.addData(BCharge.input.value);
													if (isNaN(BCharge.input.value)) BCharge.input.value = 0;
											 		});
		this._inp_Quantity.setOnBlurFunction(function(){
													this.input.value = parseFloat( Number(this.input.value).toFixed(this.decimals) );
													this.onkeyupF();	
													});
		this._inp_Quantity.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'PRICE' ACTIONS                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Price.input;
		this._inp_Price.addData(input.value);
		this._inp_Price.setRequired("yes");
		this._inp_Price.setValidationType("float");
		this._inp_Price.decimals = 2;
		this._inp_Price.setOnKeyUpFunction(function(){
													var Price = this;	   
													this.addData(this.input.value);
													
													var Quantity = JSObject._inp_Quantity;
													var BCharge = JSObject._inp_BCharge;
													var Commission = JSObject.broker_commission;
													
													//calculate BCharge 
													BCharge.input.value = parseFloat( ((Commission/100) * Quantity.data * Price.data).toFixed(BCharge.decimals) );
													BCharge.addData(BCharge.input.value);
													if (isNaN(BCharge.input.value)) BCharge.input.value = 0;
											 		});
		this._inp_Price.setOnBlurFunction(function(){
													this.input.value = parseFloat( Number(this.input.value).toFixed(this.decimals) );
													this.onkeyupF();	
													});
		this._inp_Price.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'BCharge' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_BCharge.input;
		this._inp_BCharge.addData(input.value);
		this._inp_BCharge.setRequired("yes");
		this._inp_BCharge.setValidationType("float");
		this._inp_BCharge.decimals = 2;
		this._inp_BCharge.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      SELECT 'PORTFOLIO' ACTIONS                                   */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Portfolio.setRequired("yes");
		this._inp_Portfolio.setValidationType("normal");
		this._inp_Portfolio.setReadySubmit(false);
		var errors = ["This information is required."];
		this._inp_Portfolio.addErrors(errors);
		this._inp_Portfolio.setErrorsContainer("sellshares_portfolio_container");
		this._inp_Portfolio.initActions();
		this._inp_Portfolio.input.onchange = function(){
			if (this.selectedIndex == 0){
				JSObject._inp_Portfolio.displayError(JSObject._inp_Portfolio.errors[0]);
				JSObject._inp_Portfolio.setReadySubmit(false);	
			}
			else{
				JSObject._inp_Portfolio.hideError();
				JSObject._inp_Portfolio.setReadySubmit(true);
				
				www.post("pop_shares.php",
						 'id='+JSObject._inp_Portfolio.input.value, 
						 function(reponse) {
							//alert(reponse) JSON
							
							JSObject.broker_commission = 0;
							JSObject.calculateBCharge();
							
							
							//delete all the options unless the first
							while(JSObject._inp_Shares.input.options.length > 1){
								var length = JSObject._inp_Shares.input.options.length;
								JSObject._inp_Shares.input.options.remove(length-1);
							}
							
							
							//add new options
							var json = eval(reponse);
							for (var i=0; i<json.length; i++){
								var oOption = document.createElement("OPTION");
								JSObject._inp_Shares.input.options.add(oOption, 1);
								oOption.innerHTML = json[i].name;
								oOption.value = json[i].id;
							}
							
						 }
						 );	
			}
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      SELECT 'SHARES' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Shares.setRequired("yes");
		this._inp_Shares.setValidationType("normal");
		this._inp_Shares.setReadySubmit(false);
		var errors = ["This information is required."];
		this._inp_Shares.addErrors(errors);
		this._inp_Shares.setErrorsContainer("sellshares_symbol_container");
		this._inp_Shares.initActions();
		this._inp_Shares.input.onchange = function(){
			if (this.selectedIndex == 0){
				JSObject._inp_Shares.displayError(JSObject._inp_Shares.errors[0]);
				JSObject._inp_Shares.setReadySubmit(false);	
			}
			else{
				JSObject._inp_Shares.hideError();
				JSObject._inp_Shares.setReadySubmit(true);
				
				www.post("pop_broker.php",
						 'id='+JSObject._inp_Shares.input.value, 
						 function(reponse) {
							
							commission = parseFloat(reponse);
							if (!(isNaN(commission))){
								JSObject.broker_commission = commission;
								JSObject.calculateBCharge();
							}
							
						 }
						 );	
			}
		}
		
		
	}
	
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS EDIT                                        */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initEdit = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Date = new INPUTFIELD(this, document.getElementById('shares_date'));
		this._inp_Quantity = new INPUTFIELD(this, document.getElementById('shares_quantity'));
		this._inp_Price = new INPUTFIELD(this, document.getElementById('shares_price'));
		this._inp_BCharge = new INPUTFIELD(this, document.getElementById('shares_bcharge'));
		
		this._inp_Portfolio =  new INPUTFIELD(this, document.getElementById('portfolio'));
		this._inp_Portfolio.input.readOnly = true;
		this._inp_Shares =  new INPUTFIELD(this, document.getElementById('shares'));
		this._inp_Shares.input.readOnly = true;
		
		this.form = document.getElementById("editsellshares_form");
		
		this.broker_commission = parseFloat(document.getElementById("broker_commission").value);
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'DATE' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Date.input;
		this._inp_Date.addData(input.value);
		this._inp_Date.setRequired("yes");
		this._inp_Date.setForm(this.form); 
		this._inp_Date.setValidationType("date");
		var errors = ["This information is required.",
				      "Invalid date.<bR> Date format: DD / MM / YYYY"];
		this._inp_Date.addErrors(errors);
		this._inp_Date.setErrorsContainer("sellshares_date_container");
		this._inp_Date.initActions();
		this._inp_Date.input.onfocus = function(){
			if (this.value.stripBlanks() == "DD/MM/YYYY"){
				this.className = "input_field2";
				this.value = "";
			}
			JSObject.ajax = false;
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'QUANTITY' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Quantity.input;
		this._inp_Quantity.addData(input.value);
		this._inp_Quantity.setRequired("yes");
		this._inp_Quantity.setValidationType("integer");
		this._inp_Quantity.setOnKeyUpFunction(function(){
													var Quantity = this;	   
													this.addData(this.input.value);
													
													var Price = JSObject._inp_Price;
													var BCharge = JSObject._inp_BCharge;
													var Commission = JSObject.broker_commission;
													
													//calculate BCharge 
													BCharge.input.value = parseFloat( ((Commission/100) * Quantity.data * Price.data).toFixed(BCharge.decimals) );
													BCharge.addData(BCharge.input.value);
													if (isNaN(BCharge.input.value)) BCharge.input.value = 0;
											 		});
		this._inp_Quantity.setOnBlurFunction(function(){
													this.input.value = parseFloat( Number(this.input.value).toFixed(this.decimals) );
													this.onkeyupF();	
													});
		this._inp_Quantity.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'PRICE' ACTIONS                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Price.input;
		this._inp_Price.addData(input.value);
		this._inp_Price.setRequired("yes");
		this._inp_Price.setValidationType("float");
		this._inp_Price.decimals = 2;
		this._inp_Price.setOnKeyUpFunction(function(){
													var Price = this;	   
													this.addData(this.input.value);
													
													var Quantity = JSObject._inp_Quantity;
													var BCharge = JSObject._inp_BCharge;
													var Commission = JSObject.broker_commission;
													
													//calculate BCharge 
													BCharge.input.value = parseFloat( ((Commission/100) * Quantity.data * Price.data).toFixed(BCharge.decimals) );
													BCharge.addData(BCharge.input.value);
													if (isNaN(BCharge.input.value)) BCharge.input.value = 0;
											 		});
		this._inp_Price.setOnBlurFunction(function(){
													this.input.value = parseFloat( Number(this.input.value).toFixed(this.decimals) );
													this.onkeyupF();	
													});
		this._inp_Price.initActions();
		
		
				
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'BCharge' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_BCharge.input;
		this._inp_BCharge.addData(input.value);
		this._inp_BCharge.setRequired("yes");
		this._inp_BCharge.setValidationType("float");
		this._inp_BCharge.decimals = 2;
		this._inp_BCharge.initActions();
		
		
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                     FUNCTION CALCULATE BCHARGE                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.calculateBCharge = function(){
		JSObject._inp_BCharge.input.value = parseFloat( ((JSObject.broker_commission/100) * JSObject._inp_Quantity.data * JSObject._inp_Price.data).toFixed(JSObject._inp_BCharge.decimals) );
		
		JSObject._inp_BCharge.addData(JSObject._inp_BCharge.input.value);
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION DELETE SHARES                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.deleteShares = function(){
		conf = confirm('Are you sure you want to delete this shares?');
		if (conf == false){
			return;
		}
		
		document.location.href='delete_portfolio.php?id='+this.id;
	}
	
	
		
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION VALIDATE INFORMATION                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.validate = function(){
		var countErrors = 0;
		// aflam cate erori sunt in formular
		for (var i=0; i<this.arr_inputs.length; i++){
			var obj = this[this.arr_inputs[i]];
			if (obj.submit_ready == false && obj.data.length == 0 && obj.required=="yes"){
				obj.displayError(obj.errors[0]);
				obj.setReadySubmit(false);
				countErrors++;
			}
			else if (obj.submit_ready == true && obj.data.length == 0 && obj.required=="yes"){
				obj.displayError(obj.errors[0]);
				obj.setReadySubmit(false);
				countErrors++;
			}
			else if (obj.submit_ready == false){ 
				countErrors++;
			}
		}
		
		if (countErrors==0){ 
			this.form.submit();
		}
		else return false;
		
	}
	
	
	
}