/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'REGISTER PANEL' CLASS                                        */          
/*                                                                                                   */
/*****************************************************************************************************/

function REGISTER_GINFO(parent){
	var JSObject = this;
	this.type = "Register"; 
	this.arr_inputs = ["_inp_Email","_inp_Password","_inp_RetypePassword","_inp_Firstname","_inp_Lastname",
					   "_inp_ContactAddress","_inp_City","_inp_State","_inp_Phone"];
	this.arr_inputs_2 = ["_inp_CompanyName","_inp_CUI","_inp_NrRegComert","_inp_IBAN","_inp_Bank"];
	this.form = document.getElementById("register_form");
	this.sendBtn = this.form["sendBtn"];
	this.resetBtn = this.form["resetBtn"];
	this.ajax = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS REGISTER PANEL                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Email = new INPUTFIELD(this, document.getElementById('reg_email'));
		this._inp_Password = new INPUTFIELD(this, document.getElementById('reg_password'));
		this._inp_RetypePassword = new INPUTFIELD(this, document.getElementById('reg_retypepassword'));
		this._inp_Firstname = new INPUTFIELD(this, document.getElementById('reg_firstname'));
		this._inp_Lastname = new INPUTFIELD(this, document.getElementById('reg_lastname'));
		this._inp_ContactAddress = new INPUTFIELD(this, document.getElementById('reg_contact_address'));
		this._inp_City = new INPUTFIELD(this, document.getElementById('reg_city'));
		this._inp_State = new INPUTFIELD(this, document.getElementById('reg_provincestate'));
		this._inp_Phone = new INPUTFIELD(this, document.getElementById('reg_phone'));
		this._inp_Birthday = new INPUTFIELD(this, document.getElementById('birth_day'));
		
		this._inp_CompanyName = new INPUTFIELD(this, document.getElementById('reg_companyname'));
		this._inp_CUI = new INPUTFIELD(this, document.getElementById('reg_cui'));
		this._inp_NrRegComert = new INPUTFIELD(this, document.getElementById('reg_nrregcomert'));
		this._inp_IBAN = new INPUTFIELD(this, document.getElementById('reg_iban'));
		this._inp_Bank = new INPUTFIELD(this, document.getElementById('reg_bank'));
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE REGISTER PANEL                                   */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      RADIO 'ACCOUNT TYPE' ACTIONS                                 */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this.form.elements["reg_type"][0].onclick = function(){
			var divObj = document.getElementById("account_type");
			divObj.style.display = "none";
		}
		this.form.elements["reg_type"][1].onclick = function(){
			var divObj = document.getElementById("account_type");
			divObj.style.display = "block";
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'EMAIL' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Email.input;
		this._inp_Email.setRequired("yes");
		this._inp_Email.setReadySubmit(true);
		this._inp_Email.setValidationType("email");
		this._inp_Email.setValidationNetworkType("server");
				
		this._inp_Email.setAJAXFunction(function (data){
													// alert(document.getElementById("checkregisteremailpath").value+" "+'email='+data);
													Email.www.post(document.getElementById("checkregisteremailpath").value,
																 'email='+data, 
																  function(response) {
																	  //alert(response)
																	  if (parseInt(response)==1){
																		Email.displayError(Email.errors[2]);
																		Email.setReadySubmit(false);
																		Email.ajaxReturn = false;
																		JSObject.ajax = false;
																	  }
																	  else{
																		Email.hideError();
																		Email.setReadySubmit(true);
																		Email.ajaxReturn = true;
																		Email.return_validateF();
																	  }
																	  Email.www = new WWW(true,'xmlhttp'); 
																	}
																 );
															});
		
		var errors = ["Camp obligatoriu.",
					  "Sunt permise doar caracterele alfanumerice, _ , punct si un @.",
					  "Adresa de email exista deja in baza de date!"];
		
		this._inp_Email.addErrors(errors);
		this._inp_Email.setErrorsContainer("reg_email_container");
		var Email = this._inp_Email;
		this._inp_Email.www = new WWW(true,'xmlhttp');
		this._inp_Email.validateF = function(){
			//alert("Email1:" + JSObject.ajax)
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
		}
		this._inp_Email.return_validateF = function(){
			//alert("Email2:" + JSObject.ajax)
			if (JSObject.ajax == true){
				//alert("aici")
				JSObject.validate();
			}
		}
		this._inp_Email.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'PASSWORD' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Password.input;
		this._inp_Password.setRequired("yes");
		this._inp_Password.addData(input.value);
		if (this._inp_Password.data.length > 0){
			this._inp_Password.setReadySubmit(true);
		}
		else{
			this._inp_Password.setReadySubmit(false);
		}
		this._inp_Password.setValidationType("password");
		var errors = ["Camp obligatoriu.",
			          "Introduceti intre 6 si 32 caractere. Nu folositi adresa de email."];
		this._inp_Password.addErrors(errors);
		this._inp_Password.setErrorsContainer("reg_password_container");
		this._inp_Password.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'RETYPEPASSWORD' ACTIONS                                 */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_RetypePassword.input;
		input.value = "";
		this._inp_RetypePassword.setRequired("yes");
		this._inp_RetypePassword.addData(input.value);
		this._inp_RetypePassword.setReadySubmit(false);
		this._inp_RetypePassword.setValidationType("retypepassword");
		var errors = ["Camp obligatoriu.",
			      	  "Va rugam rescrieti parola."];
		this._inp_RetypePassword.setErrorsContainer("reg_retypepassword_container");
		this._inp_RetypePassword.addErrors(errors);
		this._inp_RetypePassword.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'FIRSTNAME' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Firstname.input;
		this._inp_Firstname.setRequired("yes"); 
		this._inp_Firstname.addData(input.value);
		if (this._inp_Firstname.data.length > 0){
			this._inp_Firstname.setReadySubmit(true);
		}
		else{
			this._inp_Firstname.setReadySubmit(false);
		}
		this._inp_Firstname.setValidationType("alpha_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_Firstname.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, spatiu, punct, liniuta sau apostrof."];
		this._inp_Firstname.addErrors(errors);
		this._inp_Firstname.setErrorsContainer("reg_firstname_container");
		this._inp_Firstname.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'LASTNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Lastname.input;
		this._inp_Lastname.setRequired("yes"); 
		this._inp_Lastname.addData(input.value);
		if (this._inp_Lastname.data.length > 0){
			this._inp_Lastname.setReadySubmit(true);
		}
		else{
			this._inp_Lastname.setReadySubmit(false);
		}
		this._inp_Lastname.setValidationType("alpha_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_Lastname.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, spatiu, punct, liniuta sau apostrof."];
		this._inp_Lastname.addErrors(errors);
		this._inp_Lastname.setErrorsContainer("reg_lastname_container");
		this._inp_Lastname.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'CONTACT ADDRESS' ACTIONS                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_ContactAddress.input;
		this._inp_ContactAddress.setRequired("no"); 
		this._inp_ContactAddress.addData(input.value);
		this._inp_ContactAddress.setReadySubmit(true);
	
		this._inp_ContactAddress.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".",",","-","'","/","+"];
		this._inp_ContactAddress.addExtendedChars(extentedChars);
		var errors = ["",
			          "Sunt permise doar urmatoarele caractere: litere, numere, spatiu, punct, virgula, liniuta, apostrof, slash sau plus."];
		this._inp_ContactAddress.addErrors(errors);
		this._inp_ContactAddress.setErrorsContainer("reg_contact_address_container");
		this._inp_ContactAddress.initActions();
		
			
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'CITY' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_City.input;
		this._inp_City.setRequired("yes"); 
		this._inp_City.addData(input.value);
		if (this._inp_City.data.length > 0){
			this._inp_City.setReadySubmit(true);
		}
		else{
			this._inp_City.setReadySubmit(false);
		}
		this._inp_City.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_City.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, numere, spatiu, punct, liniuta sau apostrof."];
		this._inp_City.addErrors(errors);
		this._inp_City.setErrorsContainer("reg_city_container");
		this._inp_City.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'STATE' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_State.input;
		this._inp_State.setRequired("no"); 
		this._inp_State.addData(input.value);
		this._inp_State.setReadySubmit(true);
		this._inp_State.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_State.addExtendedChars(extentedChars);
		var errors = ["",
			          "Sunt permise doar urmatoarele caractere: litere, numere, spatiu, punct, liniuta sau apostrof."];
		this._inp_State.addErrors(errors);
		this._inp_State.setErrorsContainer("reg_provincestate_container");
		this._inp_State.initActions();
		
				
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                       INPUT 'PHONE' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Phone.input;
		this._inp_Phone.setRequired("yes"); 
		this._inp_Phone.addData(input.value);
		if (this._inp_Phone.data.length > 0){
			this._inp_Phone.setReadySubmit(true);
		}
		else{
			this._inp_Phone.setReadySubmit(false);
		}
		this._inp_Phone.setValidationType("numeric");
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar caractere numerice."];
		this._inp_Phone.addErrors(errors);
		this._inp_Phone.setErrorsContainer("reg_phone_container");
		this._inp_Phone.initActions();
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                     SELECTS 'BIRTHDAY' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Birthday.setRequired("no"); 
		this._inp_Birthday.setReadySubmit(true);
		var errors = ["Camp obligatoriu. Va rugam alegeti data nasterii.",
			          ""];
		this._inp_Birthday.addErrors(errors);
		this._inp_Birthday.setErrorsContainer("reg_birthday_container");
		this._inp_Birthday.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'COMPANY NAME' ACTIONS                                 */          
		/*                                                                                                   */
		/*****************************************************************************************************/
				
		var input = this._inp_CompanyName.input;
		this._inp_CompanyName.setRequired("yes"); 
		this._inp_CompanyName.addData(input.value);
		if (this._inp_CompanyName.data.length > 0){
			this._inp_CompanyName.setReadySubmit(true);
		}
		else{
			this._inp_CompanyName.setReadySubmit(false);
		}
		this._inp_CompanyName.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_CompanyName.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, cifre, spatiu, punct, liniuta sau apostrof."];
		this._inp_CompanyName.addErrors(errors);
		this._inp_CompanyName.setErrorsContainer("reg_companyname_container");
		this._inp_CompanyName.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'CUI' ACTIONS                                          */          
		/*                                                                                                   */
		/*****************************************************************************************************/
				
		var input = this._inp_CUI.input;
		this._inp_CUI.setRequired("yes"); 
		this._inp_CUI.addData(input.value);
		if (this._inp_CUI.data.length > 0){
			this._inp_CUI.setReadySubmit(true);
		}
		else{
			this._inp_CUI.setReadySubmit(false);
		}
		this._inp_CUI.setValidationType("alphanumeric_extended");
		var extentedChars = [" "];
		this._inp_CUI.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, cifre si spatiu."];
		this._inp_CUI.addErrors(errors);
		this._inp_CUI.setErrorsContainer("reg_cui_container");
		this._inp_CUI.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'NR.REG.COMERT' ACTIONS                                */          
		/*                                                                                                   */
		/*****************************************************************************************************/
				
		var input = this._inp_NrRegComert.input;
		this._inp_NrRegComert.setRequired("yes"); 
		this._inp_NrRegComert.addData(input.value);
		if (this._inp_NrRegComert.data.length > 0){
			this._inp_NrRegComert.setReadySubmit(true);
		}
		else{
			this._inp_NrRegComert.setReadySubmit(false);
		}
		this._inp_NrRegComert.setValidationType("alphanumeric_extended");
		var extentedChars = ["/"];
		this._inp_NrRegComert.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, cifre si slash(/)"];
		this._inp_NrRegComert.addErrors(errors);
		this._inp_NrRegComert.setErrorsContainer("reg_nrregcomert_container");
		this._inp_NrRegComert.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'IBAN' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
				
		var input = this._inp_IBAN.input;
		this._inp_IBAN.setRequired("yes"); 
		this._inp_IBAN.addData(input.value);
		if (this._inp_IBAN.data.length > 0){
			this._inp_IBAN.setReadySubmit(true);
		}
		else{
			this._inp_IBAN.setReadySubmit(false);
		}
		this._inp_IBAN.setValidationType("alphanumeric");
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere si cifre."];
		this._inp_IBAN.addErrors(errors);
		this._inp_IBAN.setErrorsContainer("reg_iban_container");
		this._inp_IBAN.initActions();
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'BANK' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
				
		var input = this._inp_Bank.input;
		this._inp_Bank.setRequired("yes"); 
		this._inp_Bank.addData(input.value);
		if (this._inp_Bank.data.length > 0){
			this._inp_Bank.setReadySubmit(true);
		}
		else{
			this._inp_Bank.setReadySubmit(false);
		}
		this._inp_Bank.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".",",","-","'"];
		this._inp_Bank.addExtendedChars(extentedChars);
		var errors = ["Camp obligatoriu.",
			          "Sunt permise doar urmatoarele caractere: litere, cifre, spatiu, punct, virgula, liniuta sau apostrof."];
		this._inp_Bank.addErrors(errors);
		this._inp_Bank.setErrorsContainer("reg_bank_container");
		this._inp_Bank.initActions();
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                               FUNCTION SERVER VALIDATE(AJAX - Email)                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		this.ajax = true;
		
		if (this._inp_Email.submit_ready == true){
			this._inp_Email.validateF();	
		}
		else{
			this.validate();
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION VALIDATE INFORMATION                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.validate = function(){
		//this.ajax = false;
		
		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++;
			}
		}
		
		
		//birthday validation
		/*var birthday = this.form["birth_month"].value + "/"+this.form["birth_day"].value + "/"+this.form["birth_year"].value;
		var isDate = birthday.isDate(birthday.split("/")[0], birthday.split("/")[1], birthday.split("/")[2]);
		if (isDate == false){
			countErrors++;
			this._inp_Birthday.displayError(this._inp_Birthday.errors[0]);
			this._inp_Birthday.setReadySubmit(false);
		}
		else{
			this._inp_Birthday.hideError();
			this._inp_Birthday.setReadySubmit(true);	
		}*/
		
		
		//aflam tipul de cont selectat: persoana fizica sau juridica
		
		if (this.form.elements["reg_type"][1].checked){
			for (var i=0; i<this.arr_inputs_2.length; i++){
				var obj = this[this.arr_inputs_2[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++;
				}
			}	
		}
		
		
		
		//submit form
		if (countErrors==0){ 
			if (this.form.elements["reg_type"][0].checked){
				document.getElementById("account_type").innerHTML = "";	
			}
			this.form.submit();
		}
		else return false;
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                   FUNCTION RESET INFORMATION                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.resetForm = function(){
		this.ajax = false;
		for (var i=0; i<this.arr_inputs.length; i++){
			var obj = this[this.arr_inputs[i]];
			obj.resetData();
		}
	}
	
}