/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'RECOVER PASSWORD PANEL' CLASS                                */          
/*                                                                                                   */
/*****************************************************************************************************/

function RECOVERPASSWORD_GINFO(parent){
	var JSObject = this;
	this.type = "Recover password"; 
	this.arr_inputs = ["_inp_Username","_inp_Email"];
					   
	this.form = document.getElementById("password_recovery_form");
	this.sendBtn = this.form["sendBtn"];
	this.ajax = false;
	
	this.seconds = 6;
	this.interval;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS PANEL                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Username = new INPUTFIELD(this, document.getElementById('recover_username'));
		this._inp_Email = new INPUTFIELD(this, document.getElementById('recover_email'));
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE 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");
		var errors = ["Camp obligatoriu.",
					  "Sunt permise doar caracterele alfanumerice, _  si punct."];
		
		var extentedChars = [".","_","@"];
		this._inp_Username.addExtendedChars(extentedChars);
		this._inp_Username.addErrors(errors);
		this._inp_Username.setErrorsContainer("recover_username_container");
		this._inp_Username.initActions();
		this._inp_Username.input.onfocus = function(){
			document.getElementById('error_container').style.display = "none";
			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");
		var errors = ["Camp obligatoriu.",
					  "Adresa de email incorecta! Ex: nume@domeniu.ro"];
		
		this._inp_Email.addErrors(errors);
		this._inp_Email.setErrorsContainer("recover_email_container");
		this._inp_Email.initActions();
		this._inp_Email.input.onfocus = function(){
			document.getElementById('error_container').style.display = "none";
			JSObject.ajax = false;	
		}
		
		
		
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                               FUNCTION SERVER VALIDATE(AJAX - Username,Email)                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		this.ajax = true;
		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){ 
			www.post(JSObject.form.action,
							 'username='+JSObject._inp_Username.data+
							 '&email='+JSObject._inp_Email.data, 
							  function(response) {
								// alert(response)
								 if (parseInt(response)==1){
									 var cell = document.getElementById('recover_container');
									 cell.innerHTML = "";
									 cell.vAlign = "middle";
									 cell.align = "center";
									 
									 cell.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0">'+
														'<tr><td>'+
														'<table width="100%" cellpadding="0" cellspacing="0">'+
															'<tr><td align="center" style="padding-top:30px"><span class="tab_info"><b>'+JSObject._inp_Username.data+'</b>, vei primi un email cu datele de login pe adresa: <a href="mailto:'+JSObject._inp_Email.data+'" class="keyword_link">'+JSObject._inp_Email.data+'</a> <br><br> In <b><font color="#FF0000" id="seconds">5</font></b> secunde vei fi redirectionat catre prima pagina.</span></td></tr>'+
														'</table>'+
														'</td></tr></table>';
									
									 this.interval = setTimeout("window.RecoverPasswordObject.info.changeURL()",1000);
								  }
								  else{
									document.getElementById('error_container').style.display = "block";
								  }
								  
							  }
							 );
		}
		else{ 
			
			return false;
		}
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION CHANGE URL                                               */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.changeURL = function(){
		this.seconds--;
		this.interval = setTimeout("window.RecoverPasswordObject.info.changeURL()",1000);
		document.getElementById("seconds").innerHTML = this.seconds;
		
		if (this.seconds == 0){ 
			clearTimeout(this.interval);
			parent.location.href = parent.LOCALPATH;
		}
	}
	
}