/*****************************************************************************************************/
/*                                                                                                   */
/*                                     'CONTACT PANEL' CLASS                                         */          
/*                                                                                                   */
/*****************************************************************************************************/

function CONTACT(){
	var JSObject = this;
	this.type = "contact"; 
	this.arr_inputs = ["_inp_Email",
					   "_inp_Subject",
					   "_inp_Message"];
	
	this.form;
	this.DOMDoc; //document object from thickbox window
	
	this.send_btn;
	this.reset_btn;
	this.close_link;
	
	this.tb_identifier;
	
	this.sendValues = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Email = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_email'));
		this._inp_Subject = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_subject'));
		this._inp_Message = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_message'));
		
		this.send_btn = $('input:button#contact_send_btn',this.DOMDoc).get(0);
		this.reset_btn = $('input:button#contact_reset_btn',this.DOMDoc).get(0);
		this.close_link = $('a[id="contact_close_btn"]',this.DOMDoc).get(0);
		this.form = this.send_btn.form;
		$("#"+this.form.id,this.DOMDoc).bind("submit",function(){return false;});
		
		//apply css effects
		//JSInterface.StyleSheet.init(this.DOMDoc);
		
		this.initCreate();
		
		//add thickbox identifier
		JSInterface.setThickboxID(this.tb_identifier);
		JSInterface.checkThickBoxHeight = null;
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PANEL                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'EMAIL' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Email.input;
		input.value = "";
		//$(input).data("error_row",this.type+"_email_row");
		this._inp_Email.setRequired("yes");
		this._inp_Email.setReadySubmit(false);
		this._inp_Email.setValidationType("email");
		var err_1 = "Email : This field is required"
		var err_2 = "Email : Only letters, numbers, underscores(_), dots(.) and one @ are allowed. Ex: name@domain.com";
		var errors = [err_1,err_2];
		this._inp_Email.addErrors(errors);
		this._inp_Email.setErrorsContainer(this.type+"_email_container");
		this._inp_Email.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Email.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Email.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'SUBJECT' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Subject.input;
		input.value = "";
		//$(input).data("error_row",this.type+"_subject_row");
		this._inp_Subject.setRequired("yes"); 
		this._inp_Subject.setReadySubmit(false);
		this._inp_Subject.setValidationType("alphanumeric_extended");
		var extentedChars = String(" .-,/:'Ã€Ã?Ã‚Ã„Ã†Ã‡Ã‰ÃˆÃŠÃ‹ÃŒÃ?ÃŽÃ Ã¡Ã¢Ã£Ã¤Ã¦Ã§Ã¨Ã©ÃªÃ«Ã¬Ã­Ã®Ã¯Ã²Ã³Ã´ÃµÃ¶Ã¹Ã»Ã¼ÃºÃ½").split("");
		this._inp_Subject.addExtendedChars(extentedChars);
		var err_1 = "Subject : This field is required";
		var err_2 = "Subject : Only letters, numbers, spaces, hyphens(-), dots(.), commas(,), slashes(/) and apostrophes(') are allowed";
		var errors = [err_1,err_2];
		this._inp_Subject.addErrors(errors);
		this._inp_Subject.setErrorsContainer(this.type+"_subject_container");
		this._inp_Subject.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Subject.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Subject.initActions();
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    TEXTAREA 'MESSAGE' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Message.input;
		input.value = "";
		//$(input).data("error_row",this.type+"_message_row");
		this._inp_Message.setRequired("yes"); 
		this._inp_Message.setReadySubmit(false);
		this._inp_Message.setValidationType("normal");
		var err_1 = "Message : This field is required";
		var errors = [err_1,""];
		this._inp_Message.addErrors(errors);
		this._inp_Message.setErrorsContainer(this.type+"_message_container");
		this._inp_Message.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Message.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Message.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT BUTTON 'SEND' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var button_mouseDown = false;
		$(this.send_btn).mousedown(function(){
										
										if (JSObject.sendValues == true){ return;}
										
										button_mouseDown = true;
										$(JSObject.DOMDoc.body).mouseup(function(){
																		 
																		 if (button_mouseDown == true){
																			button_mouseDown = false;
																			$(JSObject.send_btn).unbind("mouseleave");
																			$(JSObject.DOMDoc.body).unbind("mouseup");
																			JSObject.sendValues = true;
																			JSObject.validate();
																		 }
																		   })
										
									});
		
		
		
		$(this.send_btn).mouseup(function(){
										if (JSObject.sendValues == true) return;
										
										if (button_mouseDown == true){
											button_mouseDown = false;
											$(this).unbind("mouseleave");
											$(JSObject.DOMDoc.body).unbind("mouseup");
											JSObject.sendValues = true;
											JSObject.validate();
										}
									});
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT BUTTON 'RESET' ACTIONS                                   */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		$(this.reset_btn).mousedown(function(){
										
										if (JSObject.sendValues == true){ return;}
										
										button_mouseDown = true;
										$(JSObject.DOMDoc.body).mouseup(function(){
																		 
																		 if (button_mouseDown == true){
																			button_mouseDown = false;
																			$(JSObject.reset_btn).unbind("mouseleave");
																			$(JSObject.DOMDoc.body).unbind("mouseup");
																			JSObject.sendValues = true;
																			JSObject.reset();
																		 }
																		   })
										
									});
		
		
		
		$(this.reset_btn).mouseup(function(){
										if (JSObject.sendValues == true) return;
										
										if (button_mouseDown == true){
											button_mouseDown = false;
											$(this).unbind("mouseleave");
											$(JSObject.DOMDoc.body).unbind("mouseup");
											JSObject.sendValues = true;
											JSObject.reset();
										}
									});
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      LINK 'CLOSE' ACTIONS                                         */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		$(this.close_link).bind("click", function(){
													JSInterface.setThickboxID(JSObject.tb_identifier);
													JSInterface.removeThickboxID();
													JSInterface.closeThickbox();
												});
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                          FUNCTION RUN BEFORE BLUR  -  INPUTS                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.runBeforeBlur = function(obj){
		if (obj.validation_type == "integer" || obj.validation_type == "float"){
			//
		}
		else{
			//
		}
	}
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                              FUNCTION RUN AFTER BLUR   -  INPUTS                                  */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.runAfterBlur = function(obj){
		if (obj.validation_type == "integer" || obj.validation_type == "float"){
			if (isNaN(obj.input.value) || $.trim(obj.input.value) == "") obj.input.value = obj.minval;
			
			if (parseFloat(obj.input.value) > obj.maxval) obj.input.value = obj.maxval;
			if (parseFloat(obj.input.value) < obj.minval) obj.input.value = obj.minval;
		}
		else{
			
		}
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 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]];
			//alert(this.arr_inputs[i])
			if (obj.data.length == 0 && obj.required=="yes"){
				obj.displayError(obj.errors[0]);
				obj.setReadySubmit(false);
				countErrors++;
				//alert("1 "+this.arr_inputs[i]);
			}
			else if (obj.submit_ready == false){ 
				countErrors++;
				//alert("3 "+this.arr_inputs[i]);
			}
			
		}
		
		
		//alert(countErrors);
		if (countErrors == 0){
			//server validation
			JSObject.sendData();
		}
		else{
			JSObject.sendValues = false;
		}
	}
	
	
	this.reset = function(){
		this.DOMDoc.location.reload();	
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SUBMIT FORM                                         */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.submitForm = function(){
		
		return JSInterface.AjaxUpload.dosubmit(JSObject.form, {'onStart' : JSObject.startUploadingData, 'onComplete' : JSObject.completeUploadingData});
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SEND DATA                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.sendData = function(){
		
		$("#"+this.form.id,this.DOMDoc).unbind("submit");
		$("#"+this.form.id,this.DOMDoc).bind("submit",function(){JSObject.submitForm();});
		$("#"+this.form.id,this.DOMDoc).submit();
		JSObject.sendValues = false;
				
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                FUNCTION START UPLOADING DATA                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.startUploadingData = function(){
		// make something useful before submit (onStart)
		
		$('#loading_container',JSObject.DOMDoc).remove();
		$($('body',JSObject.DOMDoc).get(0)).append('<div id="loading_container" style="position:absolute"><table width="100%" height="100%" cellpading="0" cellspacing="0"><tr><td align="center" class="loaderOverlay">Sending data<br><br><img src="'+JSInterface.localpath+'includes/images/loading_animation.gif" /></td></tr></table></div>');
		$('#loading_container',JSObject.DOMDoc).height($('#container',JSObject.DOMDoc).height());
		$('#loading_container',JSObject.DOMDoc).width($('#container',JSObject.DOMDoc).width());
		
		var top = 0;
		var left = 0;
		var td = $('#container',JSObject.DOMDoc).get(0);
		while (td.offsetParent){
			top += td.offsetTop;
			left += td.offsetLeft;
			td = td.offsetParent;
		}
		
		$('#loading_container',JSObject.DOMDoc).css('top',top);
		$('#loading_container',JSObject.DOMDoc).css('left',left);
		
		
		return true;
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                              FUNCTION COMPLETE UPLOADING DATA                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.completeUploadingData = function(response){
		
		$("#"+JSObject.form.id,JSObject.DOMDoc).unbind("submit");
		$("#"+JSObject.form.id,JSObject.DOMDoc).bind("submit",function(){return false;});
		alert(response);
		var response = Boolean(Number(String(response)));
		
		if (response == true){
			var message = "Thank you for your message. You will get an answer as soon as posible. In <font style='color:#FF0000' id='page_timer'>5</font> seconds this page will close";
		}
		else{
			var message = "There was an error. Please try again in <font style='color:#FF0000' id='page_timer'>5</font> seconds";
		}
		
		
		var html = '<table width="100%" height="100%" cellpading="0" cellspacing="0">'
			html +='	<tr>'
			html +='		<td align="center" class="loaderOverlay">'
			html +='			<table cellpading="0" cellspacing="0">'
			html +='				<tr>'
			html +='					<td style="background-image:none; background-color:#FFFFFF; border:1px solid #CCCCCC; padding:30px"><span class="formDarkGreyText">'+message+'</span></td>'
			html +='				</tr>'
			html +='			</table>'
			html +='		</td>'
			html +='	</tr>'
			html +='</table>'
		
		//show message
		$('#loading_container',JSObject.DOMDoc).html(html);
		
		
		
		$('#page_timer',JSObject.DOMDoc).unbind("countTimer");
		$('#page_timer',JSObject.DOMDoc).bind("countTimer", function(){
																		var seconds = $(this).data("seconds")
																		
																		if (seconds - 1 >= 1){
																			$(this).html(seconds-1)
																			$(this).data("seconds",seconds-1)
																		}
																		else{
																			$(this).unbind("countTimer")
																			clearTimeout($(this).data("timerInterval"))
																			$(this).removeData("timerInterval")
																			
																			//redirect to homepage
																			if (response == true){
																				//window.document.location = JSInterface.localpath;
																				$(JSObject.close_link).trigger("click");
																			}
																			else{
																				//remove loader
																				$('#loading_container',JSObject.DOMDoc).remove();		
																			}
																		}
																	 })
		
		$('#page_timer',JSObject.DOMDoc).data("timerInterval", setInterval(function(){$('#page_timer',JSObject.DOMDoc).trigger("countTimer")},1000));
		$('#page_timer',JSObject.DOMDoc).data("seconds",5);
		
		
	}
}