/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'REGISTER PANEL' CLASS                                        */          
/*                                                                                                   */
/*****************************************************************************************************/

function REGISTER_GINFO(parent){
	var JSObject = this;
	this.type = "Register"; 
	this.arr_inputs = ["_inp_Username","_inp_Email","_inp_Password","_inp_RetypePassword","_inp_FirstName","_inp_MiddleName","_inp_LastName",
					   "_inp_Birthday","_inp_Address","_inp_City","_inp_County","_inp_Postalcode","_inp_Mobile","_inp_Telephone"];
	this.form = document.getElementById("register_form");
	this.ajax = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS REGISTER PANEL                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Username = new INPUTFIELD(this, document.getElementById('reg_username'));
		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_MiddleName = new INPUTFIELD(this, document.getElementById('reg_middlename'));
		this._inp_LastName = new INPUTFIELD(this, document.getElementById('reg_lastname'));
		this._inp_Birthday = new INPUTFIELD(this, document.getElementById('reg_birthday'));
		this._inp_Address = new INPUTFIELD(this, document.getElementById('reg_address'));
		this._inp_City = new INPUTFIELD(this, document.getElementById('reg_city'));
		this._inp_County = new INPUTFIELD(this, document.getElementById('reg_county'));
		this._inp_Postalcode = new INPUTFIELD(this, document.getElementById('reg_postalcode'));
		this._inp_Mobile = new INPUTFIELD(this, document.getElementById('reg_mobiletelephone'));
		this._inp_Telephone = new INPUTFIELD(this, document.getElementById('reg_hometelephone'));
		this._inp_Email = new INPUTFIELD(this, document.getElementById('reg_email'));
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE REGISTER PANEL                                   */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT 'USERNAME' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Username.input;
		this._inp_Username.setRequired("yes");
		this._inp_Username.addData(input.value);
		if (this._inp_Username.data.length > 0){
			this._inp_Username.setReadySubmit(true);
		}
		else{
			this._inp_Username.setReadySubmit(false);
		}
		this._inp_Username.setForm(this.form); 
		this._inp_Username.setValidationType("alphanumeric_extended");
		var extentedChars = [".","-","_"];
		this._inp_Username.addExtendedChars(extentedChars);
		var errors = ["This information is required.",
				  	  "Only letters, numbers, underscores, hyphens and dots(.) are allowed.",
				      "Username already registered! In case you forgot your password click <a class='link2' href='recover_password.php'>here</a>"];
		this._inp_Username.addErrors(errors);
		this._inp_Username.setErrorsContainer("reg_username_container");
		this._inp_Username.setValidationNetworkType("server");
		this._inp_Username.setAJAXFunction(function (data){
														Username.www.post(document.getElementById("checkregisteruseremailpath").value,
																	 'username='+data, 
														 			  function(response) {
																		 if (parseInt(response)==1){
																	  		Username.displayError(Username.errors[2]);
																		  	Username.setReadySubmit(false);
																			Username.ajaxReturn = false;
																			JSObject.ajax = false;
																		  }
																		  else{
																		  	Username.hideError();
																			Username.setReadySubmit(true);
																			Username.ajaxReturn = true;
																			if (JSObject.ajax == true){
																				Username.return_validateF();
																			}
																		  }
																		  Username.www = new WWW(true,'xmlhttp'); 
																		}
																	 );
														});
		var Username = this._inp_Username;
		this._inp_Username.www = new WWW(true,'xmlhttp');
		this._inp_Username.validateF = function(){
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
			
		}
		this._inp_Username.return_validateF = function(){
			JSObject._inp_Email.validateF();
		}
		this._inp_Username.initActions();
		//anulez functia de apasare buton
		this._inp_Username.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    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.setForm(this.form);
		this._inp_Password.setValidationType("password");
		var errors = ["This information is required.",
				      "Capitalization matters. Use 6 to 32 characters, and don't use your username."];
		this._inp_Password.addErrors(errors);
		this._inp_Password.setErrorsContainer("reg_password_container");
		this._inp_Password.initActions();
		//anulez functia de apasare buton
		this._inp_Password.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    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.setForm(this.form);
		this._inp_RetypePassword.setValidationType("retypepassword");
		var errors = ["This information is required.",
				      "Please verify your password again."];
		this._inp_RetypePassword.setErrorsContainer("reg_retypepassword_container");
		this._inp_RetypePassword.addErrors(errors);
		this._inp_RetypePassword.initActions();
		//anulez functia de apasare buton
		this._inp_RetypePassword.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'FIRSTNAME' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_FirstName.input;
		this._inp_FirstName.setForm(this.form); 
		this._inp_FirstName.setValidationType("alpha_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_FirstName.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only letters, spaces, hyphens, dots(.) and apostrophes are allowed."];
		this._inp_FirstName.addErrors(errors);
		this._inp_FirstName.setErrorsContainer("reg_firstname_container");
		this._inp_FirstName.initActions();
		//anulez functia de apasare buton
		this._inp_FirstName.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'MIDDLENAME' ACTIONS                                   */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_MiddleName.input;
		this._inp_MiddleName.setForm(this.form); 
		this._inp_MiddleName.setValidationType("alpha_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_MiddleName.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only letters, spaces, hyphens, dots(.) and apostrophes are allowed."];
		this._inp_MiddleName.addErrors(errors);
		this._inp_MiddleName.setErrorsContainer("reg_middlename_container");
		this._inp_MiddleName.initActions();
		//anulez functia de apasare buton
		this._inp_MiddleName.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'LASTNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_LastName.input;
		this._inp_LastName.setForm(this.form); 
		this._inp_LastName.setValidationType("alpha_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_LastName.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only letters, spaces, hyphens, dots(.) and apostrophes are allowed."];
		this._inp_LastName.addErrors(errors);
		this._inp_LastName.setErrorsContainer("reg_lastname_container");
		this._inp_LastName.initActions();
		//anulez functia de apasare buton
		this._inp_LastName.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'BIRTHDAY' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Birthday.input;
		this._inp_Birthday.setForm(this.form); 
		this._inp_Birthday.setValidationType("date");
		var errors = ["",
				      "Invalid date.<bR> Birthday format: DD / MM / YYYY"];
		this._inp_Birthday.addErrors(errors);
		this._inp_Birthday.setErrorsContainer("reg_birthday_container");
		this._inp_Birthday.initActions();
		this._inp_Birthday.input.onfocus = function(){
			if (this.value.stripBlanks() == "DD/MM/YYYY"){
				this.className = "input_field2";
				this.value = "";
			}
			JSObject.ajax = false;
		}
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'ADDRESS' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Address.input;
		//this._inp_Address.setForm(this.form); 
		this._inp_Address.setValidationType("normal");
		//var extentedChars = [" ",".","-","'",",","/"];
		//this._inp_Address.addExtendedChars(extentedChars);
		/*var errors = ["",
				      "Only letters, numbers, spaces, hyphens, dots(.) and apostrophes are allowed."];
		this._inp_Address.addErrors(errors);
		this._inp_Address.setErrorsContainer("reg_address_container");*/
		//this._inp_Address.initActions();
		//anulez functia de apasare buton
		this._inp_Address.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                         INPUT 'CITY' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_City.input;
		this._inp_City.setForm(this.form); 
		this._inp_City.setValidationType("alphanumeric_extended");
		var extentedChars = ["-",".","/"," ","'"];
		this._inp_City.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only letters, numbers, slashes, hyphens, apostrophes(') and dots(.) are allowed."];
		this._inp_City.addErrors(errors);
		this._inp_City.setErrorsContainer("myaccount_city_container");
		this._inp_City.initActions();
		//anulez functia de apasare buton
		this._inp_City.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'COUNTRY' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_County.input;
		this._inp_County.setForm(this.form); 
		this._inp_County.setValidationType("alphanumeric_extended");
		var extentedChars = ["-",".","/"," ","'"];
		this._inp_County.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only letters, numbers, slashes, hyphens, apostrophes(') and dots(.) are allowed."];
		this._inp_County.addErrors(errors);
		this._inp_County.setErrorsContainer("myaccount_county_container");
		this._inp_County.initActions();
		//anulez functia de apasare buton
		this._inp_County.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'POSTAL CODE' ACTIONS                                  */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Postalcode.input;
		this._inp_Postalcode.setForm(this.form); 
		this._inp_Postalcode.setValidationType("numeric_extended");
		var extentedChars = ["+","-",".","/"];
		this._inp_Postalcode.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only numbers, pluses, slashes, hyphens and dots(.) are allowed."];
		this._inp_Postalcode.addErrors(errors);
		this._inp_Postalcode.setErrorsContainer("myaccount_postalcode_container");
		this._inp_Postalcode.initActions();
		//anulez functia de apasare buton
		this._inp_Postalcode.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'MOBILE PHONE' ACTIONS                                 */          
		/*                                                                                                   */

		/*****************************************************************************************************/
		var input = this._inp_Mobile.input;
		this._inp_Mobile.setForm(this.form); 
		this._inp_Mobile.setValidationType("numeric_extended");
		var extentedChars = ["+","-",".","/"];
		this._inp_Mobile.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only numbers, pluses, slashes, hyphens and dots(.) are allowed."];
		this._inp_Mobile.addErrors(errors);
		this._inp_Mobile.setErrorsContainer("myaccount_mobiletelephone_container");
		this._inp_Mobile.initActions();
		//anulez functia de apasare buton
		this._inp_Mobile.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'HOME PHONE' ACTIONS                                   */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Telephone.input;
		this._inp_Telephone.setForm(this.form); 
		this._inp_Telephone.setValidationType("numeric_extended");
		var extentedChars = ["+","-",".","/"];
		this._inp_Telephone.addExtendedChars(extentedChars);
		var errors = ["",
				      "Only numbers, pluses, slashes, hyphens and dots(.) are allowed."];
		this._inp_Telephone.addErrors(errors);
		this._inp_Telephone.setErrorsContainer("myaccount_hometelephone_container");
		this._inp_Telephone.initActions();
		//anulez functia de apasare buton
		this._inp_Telephone.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'EMAIL' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Email.input;
		this._inp_Email.setRequired("yes");
		this._inp_Email.setReadySubmit(false);
		this._inp_Email.setValidationType("email");
		this._inp_Email.setValidationNetworkType("server");
		this._inp_Email.setForm(this.form);
		
		this._inp_Email.setAJAXFunction(function (data){
													Email.www.post(document.getElementById("checkregisteruseremailpath").value,
																 'email='+data, 
																  function(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;
																		if (JSObject.ajax == true){
																			Email.return_validateF();
																		}
																	  }
																	  Email.www = new WWW(true,'xmlhttp'); 
																	}
																 );
															});
		var errors = ["This information is required.",
				      "Only letters, numbers, underscores, dots(.) and one @ are allowed.",
				      "Email already registered! In case you forgot your password click <a class='link2' href='recover_password.php'>here</a>"];
		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(){
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
		}
		this._inp_Email.return_validateF = function(){
			JSObject.validate();
		}
		this._inp_Email.initActions();
		//anulez functia de apasare buton
		this._inp_Email.input.onfocus = function(){
			JSObject.ajax = false;	
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                         FUNCTION SERVER VALIDATE(AJAX - Username and Email)                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		this.ajax = true;
		
		
		//daca s-a facut validarea de continut, se face validarea AJAX
		if (this._inp_Username.submit_ready == true){
			this._inp_Username.validateF();
			
		}
		else{
			if (this._inp_Email.submit_ready == true){
				this._inp_Email.validateF();
			}
			else{
				this.validate();		
			}	
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 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){ 
			www.post(this.form.action,
			 'username='+JSObject._inp_Username.data+
			 '&email='+JSObject._inp_Email.data+
			 '&password='+JSObject._inp_Password.data+
			 '&firstname='+JSObject._inp_FirstName.data+
			 '&middlename='+JSObject._inp_MiddleName.data+
			 '&lastname='+JSObject._inp_LastName.data+
			 '&birthday='+JSObject._inp_Birthday.data+
			 '&address='+JSObject._inp_Address.data+
			 '&city='+JSObject._inp_City.data+
			 '&county='+JSObject._inp_County.data+
			 '&postalcode='+JSObject._inp_Postalcode.data+
			 '&mobiletelephone='+JSObject._inp_Mobile.data+
			 '&hometelephone='+JSObject._inp_Telephone.data+
			 '&newsletter='+Number(document.getElementById('newsletter_alert').checked), 
			 function(response) {
			 	 if (response.length == 0) return;
				 if (!isNaN(response)){
					 var cell = document.getElementById('autentification_container');
					 cell.vAlign = "middle";
					 cell.align = "center";
					 cell.height = "150px"
					 cell.innerHTML = "";
					 cell.innerHTML = '<table width="100%" cellpadding="0" cellspacing="0">'+
											'<tr><td align="center"><span class="text2"><b>Thank you for registering on EquityTracker.com</b>.<bR> You\'ll  be redirected to home page in <span id="seconds" style="color:#FF0000">5</span> seconds.</span></td></tr>'+
										'</table>';
					 sec = 5;
					 setTimeout(redirectRegister,1000);
				 }
			 }
			 );	
		}
		else return false;
		
	}
	
}