// JavaScript Document
/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'PORTFOLIO ADD SHARES' CLASS                                  */          
/*                                                                                                   */
/*****************************************************************************************************/

function SHARES_ADD(parent){
	var JSObject = this;
	this.type = "Add shares"; 
	this.arr_inputs = ["_inp_Date","_inp_Quantity","_inp_Price","_inp_Bonus","_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('addportfolio_date'));
		this._inp_Quantity = new INPUTFIELD(this, document.getElementById('addportfolio_quantity'));
		this._inp_Price = new INPUTFIELD(this, document.getElementById('addportfolio_price'));
		this._inp_Bonus = new INPUTFIELD(this, document.getElementById('addportfolio_bonus'));
		this._inp_BCharge = new INPUTFIELD(this, document.getElementById('addportfolio_bcharge'));
		
		this._inp_Portfolio =  new INPUTFIELD(this, document.getElementById('portfolio'));
		this._inp_Company =  new INPUTFIELD(this, document.getElementById('company'));
		this._inp_Broker =  new INPUTFIELD(this, document.getElementById('broker'));
		
		this.form = document.getElementById("addshares_form");
		
		this.arr_inputs.push("_inp_Portfolio");
		this.arr_inputs.push("_inp_Company");
		this.arr_inputs.push("_inp_Broker");
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      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("addportfolio_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 'BONUS' ACTIONS                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Bonus.input;
		this._inp_Bonus.addData(input.value);
		this._inp_Bonus.setRequired("yes");
		this._inp_Bonus.setValidationType("float");
		this._inp_Bonus.decimals = 2;
		this._inp_Bonus.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("addportfolio_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);	
			}
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      SELECT 'COMPANY SYMBOL' ACTIONS                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Company.setRequired("yes");
		this._inp_Company.setValidationType("normal");
		this._inp_Company.setReadySubmit(false);
		var errors = ["This information is required."];
		this._inp_Company.addErrors(errors);
		this._inp_Company.setErrorsContainer("addportfolio_symbol_container");
		this._inp_Company.initActions();
		this._inp_Company.input.onchange = function(){
			if (this.selectedIndex == 0){
				JSObject._inp_Company.displayError(JSObject._inp_Company.errors[0]);
				JSObject._inp_Company.setReadySubmit(false);	
			}
			else{
				JSObject._inp_Company.hideError();
				JSObject._inp_Company.setReadySubmit(true);	
			}
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      SELECT 'BROKER' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Broker.setRequired("yes");
		this._inp_Broker.setValidationType("normal");
		this._inp_Broker.setReadySubmit(false);
		var errors = ["This information is required."];
		this._inp_Broker.addErrors(errors);
		this._inp_Broker.setErrorsContainer("addportfolio_broker_container");
		this._inp_Broker.initActions();
		this._inp_Broker.input.onchange = function(){
			if (this.selectedIndex == 0){
				JSObject._inp_Broker.displayError(JSObject._inp_Company.errors[0]);
				JSObject._inp_Broker.setReadySubmit(false);	
			}
			else{
				JSObject._inp_Broker.hideError();
				JSObject._inp_Broker.setReadySubmit(true);
				//alert("broker_commission.php"+" "+'id='+JSObject._inp_Broker.input.value+'&buy='+1);
				www.post("broker_commission.php",
						 'id='+JSObject._inp_Broker.input.value+
						 '&buy='+1, 
						 function(reponse) {
							//alert(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('editportfolio_date'));
		this._inp_Quantity = new INPUTFIELD(this, document.getElementById('editportfolio_quantity'));
		this._inp_Price = new INPUTFIELD(this, document.getElementById('editportfolio_price'));
		this._inp_Bonus = new INPUTFIELD(this, document.getElementById('editportfolio_bonus'));
		this._inp_BCharge = new INPUTFIELD(this, document.getElementById('editportfolio_bcharge'));
		
		this._inp_Portfolio =  new INPUTFIELD(this, document.getElementById('editportfolio_name'));
		this._inp_Portfolio.input.readOnly = true;
		this._inp_Company =  new INPUTFIELD(this, document.getElementById('editportfolio_company'));
		this._inp_Company.input.readOnly = true;
		this._inp_Broker =  new INPUTFIELD(this, document.getElementById('editportfolio_broker'));
		this._inp_Broker.input.readOnly = true;
		
		this.form = document.getElementById("editshares_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("editportfolio_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 'BONUS' ACTIONS                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Bonus.input;
		this._inp_Bonus.addData(input.value);
		this._inp_Bonus.setRequired("yes");
		this._inp_Bonus.setValidationType("float");
		this._inp_Bonus.decimals = 2;
		this._inp_Bonus.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 COMPANY                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.deleteCompany = function(){
		conf = confirm('Are you sure you want to delete this company?');
		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;
		
	}
	
	
	
}