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

function REGISTER(){
	var JSObject = this;
	this.type = "register"; 
	this.arr_inputs = ["_inp_FirstName",
					   "_inp_LastName",
					   "_inp_Title",
					   "_inp_Company",
					   "_inp_Phone",
					   "_inp_Email",
					   "_inp_Province",
					   "_inp_Country"];
	
	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_FirstName = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_firstname'));
		this._inp_LastName = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_lastname'));
		this._inp_Title = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_title'));
		this._inp_Company = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_company'));
		this._inp_Phone = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_phone'));
		this._inp_Email = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_email'));
		this._inp_Province = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_province'));
		this._inp_Country = new INPUTFIELD(this, this.DOMDoc.getElementById(this.type+'_country'));
		
		this.send_btn = $('input:button#register_send_btn',this.DOMDoc).get(0);
		this.reset_btn = $('input:button#register_reset_btn',this.DOMDoc).get(0);
		this.close_link = $('a[id="register_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 'FIRSTNAME' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_FirstName.input;
		input.value = "";
		this._inp_FirstName.setRequired("yes"); 
		this._inp_FirstName.setReadySubmit(false);
		this._inp_FirstName.setValidationType("alpha_extended");
		var extentedChars = String(" .-'ÀÁÂÄÆÇÉÈÊËÌÍÎ áâãäæçèéêëìíîïòóôõöùûüúý").split("");
		this._inp_FirstName.addExtendedChars(extentedChars);
		
		var err_1 = 'Firstname : This field required';
		var err_2 = "Firstname : Only letters, spaces, hyphens(-), dots(.) and apostrophes(') are allowed";
		var errors = [err_1,err_2];
		
		this._inp_FirstName.addErrors(errors);
		this._inp_FirstName.setErrorsContainer(this.type+"_firstname_container");
		this._inp_FirstName.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_FirstName.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_FirstName.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'LASTNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_LastName.input;
		input.value = "";
		this._inp_LastName.setRequired("yes"); 
		this._inp_LastName.setReadySubmit(false);
		this._inp_LastName.setValidationType("alpha_extended");
		var extentedChars = String(" .-'ÀÁÂÄÆÇÉÈÊËÌÍÎ áâãäæçèéêëìíîïòóôõöùûüúý").split("");
		this._inp_LastName.addExtendedChars(extentedChars);
		
		var err_1 = 'Lastname : This field required';
		var err_2 = "Lastname : Only letters, spaces, hyphens(-), dots(.) and apostrophes(') are allowed";
		var errors = [err_1,err_2];
		
		this._inp_LastName.addErrors(errors);
		this._inp_LastName.setErrorsContainer(this.type+"_lastname_container");
		this._inp_LastName.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_LastName.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_LastName.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                     SELECT 'TITLE' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Title.input;
		this._inp_Title.setRequired("yes"); 
		this._inp_Title.setReadySubmit(false);
		this._inp_Title.setValidationType("normal");
		var err_1 = "Title : This field is required";
		var errors = [err_1,""];
		this._inp_Title.addErrors(errors);
		this._inp_Title.setErrorsContainer(this.type+"_title_container");
		this._inp_Title.input.onchange = function(){JSObject.runOnChange(JSObject._inp_Title);};
		this._inp_Title.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'COMPANY' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Company.input;
		input.value = "";
		this._inp_Company.setRequired("yes"); 
		this._inp_Company.setReadySubmit(false);
		this._inp_Company.setValidationType("alphanumeric_extended");
		var extentedChars = String(" .-/():,'ÀÁÂÄÆÇÉÈÊËÌÍÎ áâãäæçèéêëìíîïòóôõöùûüúý").split("");
		this._inp_Company.addExtendedChars(extentedChars);
		
		var err_1 = 'Company : This field required';
		var err_2 = "Company : Only letters, numbers, spaces, hyphens(-), dots(.), commmas(,), slashes(/) and apostrophes(') are allowed";
		var errors = [err_1,err_2];
		
		this._inp_Company.addErrors(errors);
		this._inp_Company.setErrorsContainer(this.type+"_company_container");
		this._inp_Company.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Company.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Company.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                   INPUT 'PHONE' ACTIONS                                           */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Phone.input;
		input.value = "";
		this._inp_Phone.setRequired("yes"); 
		this._inp_Phone.setReadySubmit(false);
		this._inp_Phone.setValidationType("numeric_extended");
		var extentedChars = String("/-.").split("");
		this._inp_Phone.addExtendedChars(extentedChars);
		var err_1 = 'Phone : This field is required';
		var err_2 = "Phone : Only numbers, slashes(/), hyphens(-) and dots(.) are allowed";
		var errors = [err_1,err_2];
		this._inp_Phone.addErrors(errors);
		this._inp_Phone.setErrorsContainer(this.type+"_phone_container");
		this._inp_Phone.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Phone.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Phone.initActions();
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      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 'PROVINCE' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Province.input;
		input.value = "";
		this._inp_Province.setRequired("yes"); 
		this._inp_Province.setReadySubmit(false);
		this._inp_Province.setValidationType("alphanumeric_extended");
		var extentedChars = String(" .-,/:()'À?ÂÄÆÇÉÈÊËÌ?Î áâãäæçèéêëìíîïòóôõöùûüúý").split("");
		this._inp_Province.addExtendedChars(extentedChars);
		var err_1 = "Province : This field is required";
		var err_2 = "Province : Only letters, numbers, spaces, hyphens(-), dots(.), commas(,), slashes(/) and apostrophes(') are allowed";
		var errors = [err_1,err_2];
		this._inp_Province.addErrors(errors);
		this._inp_Province.setErrorsContainer(this.type+"_province_container");
		this._inp_Province.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Province.setAfterBlurFunction(function(){JSObject.runAfterBlur(this);});
		this._inp_Province.initActions();
		
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                     SELECT 'COUNTRY' ACTIONS                                      */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Country.input;
		this._inp_Country.setRequired("yes"); 
		this._inp_Country.setReadySubmit(false);
		this._inp_Country.setValidationType("normal");
		var err_1 = "Country : This field is required";
		var errors = [err_1,""];
		this._inp_Country.addErrors(errors);
		this._inp_Country.setErrorsContainer(this.type+"_country_container");
		this._inp_Country.input.onchange = function(){JSObject.runOnChange(JSObject._inp_Country);};
		this._inp_Country.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 RUN ONCHANGE   -  SELECT                               */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.runOnChange = function(obj){
		if (obj.input.selectedIndex > 0){
			obj.setReadySubmit(true);
			obj.hideError();
		}
		else{
			obj.setReadySubmit(false);
		}
		  
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 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;
		
		this.send_btn.disabled = true;
		this.reset_btn.disabled = true;
				
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                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;});
		
		var response = Boolean(Number(String(response)));
		
		
		if (response == true){
			$(this.close_link).unbind("click");
			var message = "Thank you for registering. You can now download the Case Study";
		}
		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);
		
		
		//error
		if (JSObject.DOMDoc.getElementById('page_timer')){
			$('#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")
																				clearInterval($(this).data("timerInterval"))
																				$(this).removeData("timerInterval")
																				
																				//remove loader
																				$('#loading_container',JSObject.DOMDoc).remove();
																				
																				JSObject.send_btn.disabled = false;
																				JSObject.reset_btn.disabled = false;
																			}
																		 })
			
			$('#page_timer',JSObject.DOMDoc).data("timerInterval", setInterval(function(){$('#page_timer',JSObject.DOMDoc).trigger("countTimer")},1000));
			$('#page_timer',JSObject.DOMDoc).data("seconds",5);
		}
		//refresh page for downloading files
		else{
			var interval = setInterval(function(){
												clearInterval(interval) 
												window.document.location.reload();
										},200)
		}
		
		
	}
	
}
	
