/*****************************************************************************************************/
/*                                                                                                   */
/*                                      'NEW COMMENT PANEL' CLASS                                    */          
/*                                                                                                   */
/*****************************************************************************************************/

function NEWCOMMENT_GINFO(parent){
	var JSObject = this;
	this.type = "New comment"; 
	this.arr_inputs = ["_inp_Message","_inp_Code"];
					   
	this.form = document.getElementById("newcomment_form");
	this.sendBtn = this.form["sendBtn"];
	this.ajax = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT INPUTS PANEL                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Message = new INPUTFIELD(this, document.getElementById('comment_message'));
		this._inp_Code = new INPUTFIELD(this, document.getElementById('comment_code'));
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE NEW COMMENT PANEL                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                        INPUT 'MESSAGE' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
			
		var input = this._inp_Message.input;
		this._inp_Message.setRequired("yes"); 
		this._inp_Message.setReadySubmit(true);
		this._inp_Message.setValidationType("normal");
		var errors = ["Camp obligatoriu.",
			          "Comentariul contine caractere interzise!"];
		this._inp_Message.addErrors(errors);
		this._inp_Message.setErrorsContainer("comment_message_container");
		this._inp_Message.initActions();
		this._inp_Message.input.onfocus = function(){
			JSObject.ajax = false;	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'CODE' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Code.input;
		this._inp_Code.setRequired("yes");
		this._inp_Code.setReadySubmit(true);
		this._inp_Code.setValidationType("alphanumeric");
		this._inp_Code.setValidationNetworkType("server");
		this._inp_Code.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_Code.addErrors(errors);
		this._inp_Code.setErrorsContainer("comment_code_container");
		var Code = this._inp_Code;
		this._inp_Code.www = new WWW(true,'xmlhttp');
		this._inp_Code.validateF = function(){
			if (this.submit_ready == true){
				this.ajaxF(this.data);
				this.ajaxReturn = false;
			}
			else{
				JSObject.ajax = false;	
			}
		}
		this._inp_Code.return_validateF = function(){
			JSObject.validate();
		}
		this._inp_Code.initActions();
		//anulez functia de apasare buton
		this._inp_Code.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_Code.input.focus();
						  }
						 );			
		}
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                             FUNCTION SERVER VALIDATE(AJAX - CODE)                                 */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		this.ajax = true;
		
		//daca s-a facut validarea de continut, se face validarea AJAX
		if (this._inp_Code.submit_ready == true){
			this._inp_Code.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){ 
			//alert(JSObject.form.action+'?message='+JSObject._inp_Message.data+'&tutorial_id='+document.getElementById("tutorial_id").value+'&comment_alert='+document.getElementById("comment_alert").checked)
			www.post(JSObject.form.action,
							 'message='+JSObject._inp_Message.data+'&tutorial_id='+document.getElementById("tutorial_id").value+'&comment_alert='+document.getElementById("comment_alert").checked, 
							  function(response) {
								// alert(response)
								 if (parseInt(response)==1){
									var container = document.getElementById("send_comment_container");
									var string = "<div align='center' style='border-top:1px solid #d5d5d5; padding-top:10px'><span class='tab_info'><b>Comentariul tau pentru acest tutorial a fost trimis cu succes</b>.<br><br>In <b><font color='#FF0000' id='seconds'>5</font></b> secunde comentariul tau va fi afisat!</span></div>";
									container.innerHTML = string;
									changeURL();
								  }
								  else{
									
								  }
								  
							  }
							 );
			
		}
		else{ 
			return false;
		}
		
	}
	
}