/*****************************************************************************************************/
/*                                                                                                   */
/*                                  'NEWSLETTER PANEL' CLASS                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function NEWSLETTER_GINFO(parent){
	var JSObject = this;
	this.type = "Newsletter"; 
	this.arr_inputs = ["_inp_Name","_inp_Nationality","_inp_Birthday","_inp_Email"];
	this.form = document.getElementById("newsletter_form");
	this.ajax = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                            FUNCTION INIT INPUTS NEWSLETTER PANEL                                  */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Name = new INPUTFIELD(this, document.getElementById('newsletter_name'));
		this._inp_Nationality = new INPUTFIELD(this, document.getElementById('newsletter_nationality'));
		this._inp_Birthday = new INPUTFIELD(this, document.getElementById('newsletter_birthdate'));
		this._inp_Email = new INPUTFIELD(this, document.getElementById('newsletter_email'));
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE RECOVERPASSWORD PANEL                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'NAME' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Name.input;
		this._inp_Name.setRequired("yes"); 
		this._inp_Name.setReadySubmit(false);
		this._inp_Name.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'","_"];
		this._inp_Name.addExtendedChars(extentedChars);
		if (LANGUAGE.toUpperCase() == "RO"){
			var errors = ["Numele este obligatoriu de completat.",
						  "Sunt permise doar litere, spatiu, linie(-), punct(.) si apostrof(')."];
		}
		else if (LANGUAGE.toUpperCase() == "EN"){
			var errors = ["Your name is required.",
				          "Only letters, numbers, spaces, hyphens, dots(.) and apostrophes(') are allowed."];
		}
		this._inp_Name.addErrors(errors);
		this._inp_Name.setErrorsContainer("newsletter_name_container");
		this._inp_Name.runOnBlurF = "after";
		this._inp_Name.setOnBlurFunction(function(){
											if (LANGUAGE.toUpperCase() == "RO"){
												if (this.input.value.toUpperCase() == ""){
													this.input.value = "Numele tau";
													JSObject._inp_Name.addData("");
												}
											}
											else if (LANGUAGE.toUpperCase() == "EN"){
												if (this.input.value.toUpperCase() == ""){
													this.input.value = "Your name";
													JSObject._inp_Name.addData("");
												}
											}	
										});
		this._inp_Name.initActions();
		this._inp_Name.input.onfocus = function(){
			
			this.focus();
			if (LANGUAGE.toUpperCase() == "RO"){
				if (this.value.toUpperCase() == "NUMELE TAU"){
					this.value = "";
					JSObject._inp_Name.addData("");
				}
			}
			else if (LANGUAGE.toUpperCase() == "EN"){
				if (this.value.toUpperCase() == "YOUR NAME"){
					this.value = "";
					JSObject._inp_Name.addData("");
				}
			}	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'NATIONALITY' ACTIONS                                  */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Nationality.input;
		this._inp_Nationality.setRequired("yes"); 
		this._inp_Nationality.setReadySubmit(false);
		this._inp_Nationality.setValidationType("alphanumeric_extended");
		var extentedChars = [" ",".","-","'","_"];
		this._inp_Nationality.addExtendedChars(extentedChars);
		if (LANGUAGE.toUpperCase() == "RO"){
			var errors = ["Nationalitatea este obligatorie de completat.",
						  "Sunt permise doar litere, spatiu, linie(-), punct(.) si apostrof(')."];
		}
		else if (LANGUAGE.toUpperCase() == "EN"){
			var errors = ["Your nationality is required.",
				          "Only letters, numbers, spaces, hyphens, dots(.) and apostrophes(') are allowed."];
		}
		this._inp_Nationality.addErrors(errors);
		this._inp_Nationality.setErrorsContainer("newsletter_nationality_container");
		this._inp_Nationality.runOnBlurF = "after";
		this._inp_Nationality.setOnBlurFunction(function(){
													if (LANGUAGE.toUpperCase() == "RO"){
														if (this.input.value.toUpperCase() == ""){
															this.input.value = "Nationalitatea";
															JSObject._inp_Nationality.addData("");
														}
													}
													else if (LANGUAGE.toUpperCase() == "EN"){
														if (this.input.value.toUpperCase() == ""){
															this.input.value = "Your nationality";
															JSObject._inp_Nationality.addData("");
														}
													}	
												});
		this._inp_Nationality.initActions();
		this._inp_Nationality.input.onfocus = function(){
			
			this.focus();
			if (LANGUAGE.toUpperCase() == "RO"){
				if (this.value.toUpperCase() == "NATIONALITATEA"){
					this.value = "";
					JSObject._inp_Nationality.addData("");
				}
			}
			else if (LANGUAGE.toUpperCase() == "EN"){
				if (this.value.toUpperCase() == "YOUR NATIONALITY"){
					this.value = "";
					JSObject._inp_Nationality.addData("");
				}
			}	
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                       INPUT 'BIRTHDAY' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Birthday.input;
		this._inp_Birthday.setRequired("yes"); 
		this._inp_Birthday.setReadySubmit(false);
		this._inp_Birthday.setValidationType("date");
		var extentedChars = ["/"];
		this._inp_Birthday.addExtendedChars(extentedChars);
		if (LANGUAGE.toUpperCase() == "RO"){
			var errors = ["Data nasterii este obligatorie de completat.",
						  "Data introdusa nu este valida. Format: ZZ/LL/AAAA"];
		}
		else if (LANGUAGE.toUpperCase() == "EN"){
			var errors = ["Your birthdate is required.",
				          "Your birthdate is invalid. Format: DD/MM/YYYY."];
		}
		this._inp_Birthday.addErrors(errors);
		this._inp_Birthday.setErrorsContainer("newsletter_birthdate_container");
		this._inp_Birthday.runOnBlurF = "after";
		this._inp_Birthday.setOnBlurFunction(function(){
													if (LANGUAGE.toUpperCase() == "RO"){
														if (this.input.value.toUpperCase() == ""){
															this.input.value = "Data nasterii (ZZ/LL/AAAA)";
															JSObject._inp_Birthday.addData("");
														}
													}
													else if (LANGUAGE.toUpperCase() == "EN"){
														if (this.input.value.toUpperCase() == ""){
															this.input.value = "Your birthdate (DD/MM/YYYY)";
															JSObject._inp_Birthday.addData("");
														}
													}	
												});
		this._inp_Birthday.initActions();
		this._inp_Birthday.input.onfocus = function(){
			
			this.focus();
			if (LANGUAGE.toUpperCase() == "RO"){
				if (this.value.toUpperCase() == "DATA NASTERII (ZZ/LL/AAAA)"){
					this.value = "";
					JSObject._inp_Birthday.addData("");
				}
			}
			else if (LANGUAGE.toUpperCase() == "EN"){
				if (this.value.toUpperCase() == "YOUR BIRTHDATE (DD/MM/YYYY)"){
					this.value = "";
					JSObject._inp_Birthday.addData("");
				}
			}	
		}
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'EMAIL' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Email.input;
		this._inp_Email.setRequired("yes"); 
		this._inp_Email.setReadySubmit(false);
		this._inp_Email.setValidationType("email");
		
		if (LANGUAGE.toUpperCase() == "RO"){
			var errors = ["Camp obligatoriu de completat.",
						  "Adresa de email invalida! Email de forma: nume@domeniu.com",
						  "Aceasta adresa a mai fost inregistrata!"];
		}
		else if (LANGUAGE.toUpperCase() == "EN"){
			var errors = ["Your email address is required.",
				      	  "Only letters, numbers, underscores, dots(.) and one @ are allowed.",
						  "This email address is already registered!"];
		}
		this._inp_Email.addErrors(errors);
		this._inp_Email.setErrorsContainer("newsletter_email_container");
		this._inp_Email.runOnBlurF = "after";
		this._inp_Email.setOnBlurFunction(function(){
											if (LANGUAGE.toUpperCase() == "RO"){
												if (this.input.value.toUpperCase() == ""){
													JSObject._inp_Email.addData("");
													this.input.value = "Adresa ta de email";
													
												}
											}
											else if (LANGUAGE.toUpperCase() == "EN"){
												if (this.input.value.toUpperCase() == ""){
													this.input.value = "Your email address";
													JSObject._inp_Email.addData("");
												}
											}
										});
		this._inp_Email.initActions();
		this._inp_Email.input.onfocus = function(){
			
			animatedcollapse.show("newsletter_form_container");
			
			this.focus();
			if (LANGUAGE.toUpperCase() == "RO"){
				if (this.value.toUpperCase() == "ADRESA TA DE EMAIL"){
					this.value = "";
					JSObject._inp_Email.addData("");
				}
			}
			else if (LANGUAGE.toUpperCase() == "EN"){
				if (this.value.toUpperCase() == "YOUR EMAIL ADDRESS"){
					this.value = "";
					JSObject._inp_Email.addData("");
				}
			}	
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                             FUNCTION SERVER VALIDATE(AJAX - Email)                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		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"){
				//alert("1")
				obj.displayError(obj.errors[0]);
				obj.setReadySubmit(false);
				countErrors++;
			}
			else if (obj.submit_ready == true && obj.data.length == 0 && obj.required=="yes"){
				//alert("2")
				obj.displayError(obj.errors[0]);
				obj.setReadySubmit(false);
				countErrors++;
			}
			else if (obj.submit_ready == false){ 
				//alert("3")
				countErrors++;
			}
		}
		
		if (countErrors==0){ 
			
			www.post(LOCALPATH+"newsletter_action.php",
			 'name='+JSObject._inp_Name.data+
			 '&nationality='+JSObject._inp_Nationality.data+
			 '&birthdate='+JSObject._inp_Birthday.data+
			 '&email='+JSObject._inp_Email.data+
			 '&format='+'h', 
			 function(response) {
				 alert(response)
				 if (response.lastIndexOf("<body>OK</body>") != -1){
					// alert("aci")
					 var cell = document.getElementById('newsletter_container');
					 cell.innerHTML = "";
					 cell.vAlign = "middle";
					 cell.align = "center";
					 
					if (LANGUAGE.toUpperCase() == "RO"){
						 cell.innerHTML = '<table width="100%" cellpadding="0" cellspacing="0">'+
												'<tr><td align="left" style="padding-top:7px; padding-right:10px"><p class="blackText">Multumim pentru interesul acordat site-ului <b>GrandHotelRex.ro</b>.<br><br> Veti primi un email de confirmare pe andresa: <a href="mailto:'+JSObject._inp_Email.data+'" class="availibility_link" name="'+JSObject._inp_Email.data+'" title="'+JSObject._inp_Email.data+'">'+JSObject._inp_Email.data+'</a></p></td></tr>'+
											'</table>';
					 }
					 else if (LANGUAGE.toUpperCase() == "EN"){
						 cell.innerHTML = '<table width="100%" cellpadding="0" cellspacing="0">'+
												'<tr><td align="left" style="padding-top:7px; padding-right:10px"><p class="blackText">Thank your for your interest in our site <b>GrandHotelRex.ro</b>!<br><br> You will receive a confirmation email at: <a href="mailto:'+JSObject._inp_Email.data+'" class="availibility_link" name="'+JSObject._inp_Email.data+'" title="'+JSObject._inp_Email.data+'">'+JSObject._inp_Email.data+'</a></p></td></tr>'+
											'</table>';
					 }
					 
				 }
				 else{
					 JSObject._inp_Email.displayError(JSObject._inp_Email.errors[2]);
					 JSObject._inp_Email.setReadySubmit(false);
				 }
				 //alert(response)
			 }
			 );	
		}
		else{ 
			animatedcollapse.show("newsletter_form_container");
			return false;
		}
		
	}
	
}
