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

function REGISTER_GINFO(parent){
	var JSObject = this;
	this.type = "Register"; 
	this.arr_inputs = ["_inp_Username","_inp_Password","_inp_RetypePassword","_inp_Email",
					   "_inp_Firstname","_inp_Lastname","_inp_Phone","_inp_Avatar",
					   "_inp_City","_inp_Country","_inp_Job","_inp_Description","_inp_CaptchaCode"];
					   
	this.form = document.getElementById("register_form");
	this.sendBtn = this.form["sendBtn"];
	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_Email = new INPUTFIELD(this, document.getElementById('reg_email'));
		this._inp_Firstname = new INPUTFIELD(this, document.getElementById('reg_firstname'));
		this._inp_Lastname = new INPUTFIELD(this, document.getElementById('reg_lastname'));
		this._inp_Phone = new INPUTFIELD(this, document.getElementById('reg_phone'));
		this._inp_City = new INPUTFIELD(this, document.getElementById('reg_city'));
		this._inp_Country = new INPUTFIELD(this, document.getElementById('reg_country'));
		this._inp_Job = new INPUTFIELD(this, document.getElementById('reg_job'));
		this._inp_Description = new INPUTFIELD(this, document.getElementById('reg_description'));
		this._inp_Avatar = new INPUTFIELD(this, document.getElementById('reg_avatar'));
		this._inp_CaptchaCode = new INPUTFIELD(this, document.getElementById('reg_captcha_code'));
		
		this._inp_Birthday = new INPUTFIELD(this, document.getElementById('birth_day'));
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE REGISTER PANEL                                   */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'USERNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Username.input;
		this._inp_Username.setRequired("yes");
		this._inp_Username.setReadySubmit(true);
		this._inp_Username.setValidationType("alphanumeric_extended");
		this._inp_Username.setValidationNetworkType("server");
				
		this._inp_Username.setAJAXFunction(function (data){
													//alert(document.getElementById("checkregisterpath").value+"?"+'username='+data);
													Username.www.post(document.getElementById("checkregisterpath").value,
																 'username='+data, 
																  function(response) {
																	 // alert(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;
																		Username.return_validateF();
																	  }
																	  Username.www = new WWW(true,'xmlhttp'); 
																	}
																 );
															});
		
		var errors = ["Camp obligatoriu.",
					  "Sunt permise doar caracterele alfanumerice, _  si punct.",
					  "Numele de utilizator exista deja in baza de date!"];
		
		var extentedChars = [".","_"];
		this._inp_Username.addExtendedChars(extentedChars);
		this._inp_Username.addErrors(errors);
		this._inp_Username.setErrorsContainer("reg_username_container");
		var Username = this._inp_Username;
		this._inp_Username.www = new WWW(true,'xmlhttp');
		this._inp_Username.validateF = function(){
			//alert("Username:" + JSObject.ajax)
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
		}
		this._inp_Username.return_validateF = function(){
			//alert("Username2:" + JSObject.ajax)
			if (JSObject.ajax == true){
				//alert("aici")
				JSObject._inp_Email.validateF();
			}
		}
		this._inp_Username.initActions();
		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.setValidationType("password");
		var errors = ["Camp obligatoriu.",
			          "Parola trebuie sa contina intre 6 si 32 caractere. Nu folosi adresa de email."];
		this._inp_Password.addErrors(errors);
		this._inp_Password.setErrorsContainer("reg_password_container");
		this._inp_Password.initActions();
		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.setValidationType("retypepassword");
		var errors = ["Camp obligatoriu.",
			      	  "Te rugam sa rescrii parola."];
		this._inp_RetypePassword.setErrorsContainer("reg_retypepassword_container");
		this._inp_RetypePassword.addErrors(errors);
		this._inp_RetypePassword.initActions();
		this._inp_RetypePassword.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      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("checkregisterpath").value+" "+'email='+data);
													Email.www.post(document.getElementById("checkregisterpath").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.",
					  "Adresa de email incorecta! Ex: nume@domeniu.ro",
					  "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();
		this._inp_Email.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'FIRSTNAME' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Firstname.input;
		this._inp_Firstname.setRequired("yes"); 
		this._inp_Firstname.addData(input.value);
		this._inp_Firstname.setReadySubmit(true);
		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();
		this._inp_Firstname.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'LASTNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Lastname.input;
		this._inp_Lastname.setRequired("yes"); 
		this._inp_Lastname.addData(input.value);
		this._inp_Lastname.setReadySubmit(true);
		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();
		this._inp_Lastname.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                       INPUT 'PHONE' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Phone.input;
		this._inp_Phone.setRequired("no"); 
		this._inp_Phone.addData(input.value);
		this._inp_Phone.setReadySubmit(true);
		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();
		this._inp_Phone.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'CITY' ACTIONS                                       */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_City.input;
		this._inp_City.setRequired("no"); 
		this._inp_City.addData(input.value);
		this._inp_City.setReadySubmit(true);
		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();
		this._inp_City.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'COUNTRY' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		
		var input = this._inp_Country.input;
		this._inp_Country.setRequired("no"); 
		this._inp_Country.addData(input.value);
		this._inp_Country.setReadySubmit(true);
		this._inp_Country.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_Country.addExtendedChars(extentedChars);
		var errors = ["",
			          "Sunt permise doar urmatoarele caractere: litere, numere, spatiu, punct, liniuta sau apostrof."];
		this._inp_Country.addErrors(errors);
		this._inp_Country.setErrorsContainer("reg_country_container");
		this._inp_Country.initActions();
		this._inp_Country.input.onfocus = function(){
			JSObject.ajax = false;	
		}
	
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'JOB' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		
		var input = this._inp_Job.input;
		this._inp_Job.setRequired("no"); 
		this._inp_Job.addData(input.value);
		this._inp_Job.setReadySubmit(true);
		this._inp_Job.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'"];
		this._inp_Job.addExtendedChars(extentedChars);
		var errors = ["",
			          "Sunt permise doar urmatoarele caractere: litere, numere, spatiu, punct, liniuta sau apostrof."];
		this._inp_Job.addErrors(errors);
		this._inp_Job.setErrorsContainer("reg_job_container");
		this._inp_Job.initActions();
		this._inp_Job.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'DESCRIPTION' ACTIONS                                  */          
		/*                                                                                                   */
		/*****************************************************************************************************/
			
		var input = this._inp_Description.input;
		this._inp_Description.setRequired("no"); 
		this._inp_Description.addData(input.value);
		this._inp_Description.setReadySubmit(true);
		this._inp_Description.setValidationType("normal");
		var errors = ["",
			          "Descrierea contine caractere interzise!"];
		this._inp_Description.addErrors(errors);
		this._inp_Description.setErrorsContainer("reg_description_container");
		this._inp_Description.input.onkeyup = function(){
			if (this.value.length > 400){
				this.value = this.value.substring(0,400);
			}
			
			document.getElementById("description_letters").innerHTML = (400-this.value.length) + " caractere";
		}
		this._inp_Description.initActions();
		this._inp_Description.input.onfocus = function(){
			JSObject.ajax = false;	
		}	
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'AVATAR' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
			
		var input = this._inp_Avatar.input;
		this._inp_Avatar.setRequired("no"); 
		this._inp_Avatar.addData(input.value);
		this._inp_Avatar.setReadySubmit(true);
		this._inp_Avatar.setValidationType("arhive");
		var docTypes = ["jpg","gif","png"];
		this._inp_Avatar.addDocTypes(docTypes);
		var errors = ["",
			          "Tipul fisierului nu este valid! Formate valide: jpg, gif, png"];
		this._inp_Avatar.addErrors(errors);
		this._inp_Avatar.setErrorsContainer("reg_avatar_container");
		this._inp_Avatar.initActions();
		this._inp_Avatar.input.onchange = function(){
			this.focus();
			
		};
		this._inp_Avatar.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                     SELECTS 'BIRTHDAY' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Birthday.setRequired("no"); 
		this._inp_Birthday.setReadySubmit(true);
		var errors = ["",
			          ""];
		this._inp_Birthday.addErrors(errors);
		this._inp_Birthday.setErrorsContainer("reg_birthday_container");
		this._inp_Birthday.initActions();
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'CODE' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_CaptchaCode.input;
		this._inp_CaptchaCode.setRequired("yes");
		this._inp_CaptchaCode.setReadySubmit(true);
		this._inp_CaptchaCode.setValidationType("alphanumeric");
		this._inp_CaptchaCode.setValidationNetworkType("server");
		this._inp_CaptchaCode.setAJAXFunction(function (data){
													Code.www.post(LOCALPATH+"check_captcha.php",
																 'code='+data+'&public_key='+document.getElementById("public_key").value, 
																  function(response) {
																	 if (parseInt(response)==1){
																		Code.hideError();
																		Code.setReadySubmit(true);
																		Code.ajaxReturn = true;
																		//alert(JSObject.ajax)
																		if (JSObject.ajax == true){
																			Code.return_validateF();
																		}
																	  }
																	  else{
																		Code.displayError(Code.errors[2]);
																		Code.setReadySubmit(false);
																		Code.ajaxReturn = false;
																		JSObject.ajax = false;
																	  }
																	  Code.www = new WWW(true,'xmlhttp'); 
																  }
																 );
															});
		
		var errors = ["Codul din imaginea alaturata este obligatoriu de completat.",
				      "Codul este invalid.",
				      "Codul este invalid."];
		this._inp_CaptchaCode.addErrors(errors);
		this._inp_CaptchaCode.setErrorsContainer("reg_captcha_code_container");
		var Code = this._inp_CaptchaCode;
		this._inp_CaptchaCode.www = new WWW(true,'xmlhttp');
		this._inp_CaptchaCode.validateF = function(){
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
		}
		this._inp_CaptchaCode.return_validateF = function(){
			JSObject.validate();
		}
		this._inp_CaptchaCode.initActions();
		//anulez functia de apasare buton
		this._inp_CaptchaCode.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        LINK 'CAPTCHA' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
			
		document.getElementById('captcha_link').onclick = function(){
			www.post(LOCALPATH+"captcha_public_key.php",
						 'dummy=', 
						  function(response) {
							 document.getElementById('captcha_img').src = LOCALPATH+"captcha.php?public_key="+response;
							 document.getElementById('public_key').value = response;
							 
							 JSObject._inp_CaptchaCode.input.focus();
						  }
						 );			
		}
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                               FUNCTION SERVER VALIDATE(AJAX - Username,Email)                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		this.ajax = true;
		
		if (this._inp_Username.submit_ready == true){
			this._inp_Username.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++;
			}
		}
		
		
				
		
		
		//submit form
		if (countErrors==0){ 
			this.form.submit();
		}
		else{ 
			var img = document.getElementById("block_expandcollapse");
			var _src = img.src;
			if (_src.lastIndexOf("plus") != -1){
				img.src = _src.substring(0,	_src.lastIndexOf("plus")) + "minus" + "_big.gif";
			}
			animatedcollapse.show('edit_profile_container');
			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();
		}
	}
	
}