/*****************************************************************************************************/
/*                                                                                                   */
/*                                        'STEP 1' CLASS                                             */          
/*                                                                                                   */
/*****************************************************************************************************/

function STEP1(){
	var JSObject = this;
	this.type = "step1"; 
	this.arr_inputs = ["_inp_Title",
					   "_inp_Description",
					   "_inp_FullName"];
	
	this.form;
	this.DOMDoc; //document object from thickbox window
	
	this.save_btn;
	
	this.sendValues = false;
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Title = new INPUTFIELD(this, this.DOMDoc.getElementById('Title'));
		this._inp_Description = new INPUTFIELD(this, this.DOMDoc.getElementById('Message'));
		this._inp_FullName = new INPUTFIELD(this, this.DOMDoc.getElementById('Signature'));
		
		
		this.save_btn = $('#btn_next',this.DOMDoc).get(0);
		this.form = this._inp_Title.input.form;
		//$("#"+this.form.id,this.DOMDoc).bind("submit",function(){return false;});
		
		this.initCreate();
		this.initPictures();
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PANEL                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'TITLE' ACTIONS                                        */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Title.input;
		$(input).data("defaultValue",JSInterface.MultiLanguage.reverseHTML("Taper votre titre ici, par exemple : Bonjour mes amis, Ch&egrave;re Cliona ... (40 caract&egrave;res au maximum)"));
		this._inp_Title.setRequired("yes"); 
		this._inp_Title.addData(input.value);
		if (this._inp_Title.data.length > 0){
			this._inp_Title.setReadySubmit(true);
		}
		else{
			this._inp_Title.setReadySubmit(false);
		}
		this._inp_Title.setValidationType("alphanumeric_extended");
		var extentedChars = String(" .-'/,:()Ã€Ã?Ã‚Ã„Ã†Ã‡Ã‰ÃˆÃŠÃ‹ÃŒÃ?ÃŽÃ Ã¡Ã¢Ã£Ã¤Ã¦Ã§Ã¨Ã©ÃªÃ«Ã¬Ã­Ã®Ã¯Ã²Ã³Ã´ÃµÃ¶Ã¹Ã»Ã¼ÃºÃ½").split("");
		this._inp_Title.addExtendedChars(extentedChars);
		
		var err_1 = JSInterface.MultiLanguage.translate("This field is required");
		var err_2 = JSInterface.MultiLanguage.translate("Only letters, numbers, spaces, slashes(/), hyphens(-), commas(,), dots(.), apostrophes(') and round brackets are allowed");
		var errors = [err_1,err_2];
		
		this._inp_Title.addErrors(errors);
		this._inp_Title.setErrorsContainer("title_container");
		this._inp_Title.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Title.setAfterBlurFunction(function(){
													  JSObject.runAfterBlur(this);
														if (JSObject._inp_Title.input.value == ""){
															JSObject._inp_Title.input.value = $(JSObject._inp_Title.input).data("defaultValue");	
															JSObject._inp_Title.addData(JSObject._inp_Title.input.value);
															JSObject._inp_Title.setReadySubmit(true);
														}
											});
		
		this._inp_Title.input.onkeyup = function(){
			if (this.value.length > 40){
				this.value = this.value.substring(0,40);
			}
			
			document.getElementById("title_letters").innerHTML = (40-this.value.length);
		}
		
		this._inp_Title.input.onfocus = function(){
			
			if (this.value.toLowerCase() == $(JSObject._inp_Title.input).data("defaultValue").toLowerCase()){
				this.value = "";
				JSObject._inp_Title.addData(this.value);
			}
			
		}
		
		this._inp_Title.initActions();
		
		if (this._inp_Title.input.value.length > 40){
			if (this._inp_Title.input.value.toLowerCase() != $(this._inp_Title.input).data("defaultValue").toLowerCase()){
				this._inp_Title.input.value = this._inp_Title.input.value.substring(0,40);
			}
		}
		if (this._inp_Title.input.value.toLowerCase() != $(this._inp_Title.input).data("defaultValue").toLowerCase()){
			document.getElementById("title_letters").innerHTML = (40-this._inp_Title.input.value.length);
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      TEXTAREA 'DESCRIPTION' ACTIONS                               */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_Description.input;
		$(input).data("defaultValue",JSInterface.MultiLanguage.reverseHTML("Taper votre texte ici. (400 caract&egrave;res au maximum)"));
		this._inp_Description.setRequired("yes"); 
		this._inp_Description.addData(input.value);
		if (this._inp_Description.data.length > 0){
			this._inp_Description.setReadySubmit(true);
		}
		else{
			this._inp_Description.setReadySubmit(false);
		}
		this._inp_Description.setValidationType("normal");
		
		var err_1 = JSInterface.MultiLanguage.translate("This field is required");
		var errors = [err_1];
		
		this._inp_Description.addErrors(errors);
		this._inp_Description.setErrorsContainer("message_container");
		this._inp_Description.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_Description.setAfterBlurFunction(function(){
															JSObject.runAfterBlur(this);
															if (JSObject._inp_Description.input.value == ""){
																JSObject._inp_Description.input.value = $(JSObject._inp_Description.input).data("defaultValue");
																JSObject._inp_Description.addData(JSObject._inp_Description.input.value);
																JSObject._inp_Description.setReadySubmit(true);
															}
													});
		this._inp_Description.input.onkeyup = function(){
			if (this.value.length > 400){
				this.value = this.value.substring(0,400);
			}
			
			document.getElementById("message_letters").innerHTML = (400-this.value.length);
		}
		
		this._inp_Description.input.onfocus = function(){
			
			if (this.value.toLowerCase() == $(JSObject._inp_Description.input).data("defaultValue").toLowerCase()){
				this.value = "";
				JSObject._inp_Description.addData(this.value);
			}
			
		}
		
		this._inp_Description.initActions();
		
		if (this._inp_Description.input.value.length > 400){
			if (this._inp_Description.input.value.toLowerCase() != $(this._inp_Description.input).data("defaultValue").toLowerCase()){
				this._inp_Description.input.value = this._inp_Description.input.value.substring(0,400);
			}
		}
		if (this._inp_Description.input.value.toLowerCase() != $(this._inp_Description.input).data("defaultValue").toLowerCase()){
			document.getElementById("message_letters").innerHTML = (400-this._inp_Description.input.value.length);
		}
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'FULLNAME' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var input = this._inp_FullName.input;
		this._inp_FullName.setRequired("yes"); 
		this._inp_FullName.addData(input.value);
		if (this._inp_FullName.data.length > 0){
			this._inp_FullName.setReadySubmit(true);
		}
		else{
			this._inp_FullName.setReadySubmit(false);
		}
		this._inp_FullName.setValidationType("alphanumeric_extended");
		var extentedChars = String(" .-'/,()Ã€Ã?Ã‚Ã„Ã†Ã‡Ã‰ÃˆÃŠÃ‹ÃŒÃ?ÃŽÃ Ã¡Ã¢Ã£Ã¤Ã¦Ã§Ã¨Ã©ÃªÃ«Ã¬Ã­Ã®Ã¯Ã²Ã³Ã´ÃµÃ¶Ã¹Ã»Ã¼ÃºÃ½").split("");
		this._inp_FullName.addExtendedChars(extentedChars);
		
		var err_1 = JSInterface.MultiLanguage.translate("This field is required");
		var err_2 = JSInterface.MultiLanguage.translate("Only letters, numbers, spaces, slashes(/), hyphens(-), commas(,), dots(.), apostrophes(') and round brackets are allowed");
		var errors = [err_1,err_2];
		
		this._inp_FullName.addErrors(errors);
		this._inp_FullName.setErrorsContainer("signature_container");
		this._inp_FullName.setBeforeBlurFunction(function(){JSObject.runBeforeBlur(this);});
		this._inp_FullName.setAfterBlurFunction(function(){
														 JSObject.runAfterBlur(this);
															if (JSObject._inp_FullName.input.value == ""){
																JSObject._inp_FullName.input.value = JSInterface.MultiLanguage.reverseHTML("Entrez ci-dessous le pr&eacute;nom, nom ou pseudo qui permettra au(x) destinataire(s) de savoir qui a envoy&eacute; l'e-card.");	
																JSObject._inp_FullName.addData(JSObject._inp_FullName.input.value);
																JSObject._inp_FullName.setReadySubmit(true);
															}
												});
		
		this._inp_FullName.input.onfocus = function(){
			if (this.value.toLowerCase() == JSInterface.MultiLanguage.reverseHTML("entrez ci-dessous le pr&eacute;nom, nom ou pseudo qui permettra au(x) destinataire(s) de savoir qui a envoy&eacute; l'e-card.")){
				this.value = "";
				JSObject._inp_FullName.addData(this.value);
			}
			
		}
		
		this._inp_FullName.initActions();
		
		
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT BUTTON 'SEND' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var save_mouseDown = false;
		$(this.save_btn).mousedown(function(){
										if (JSObject.sendValues == true){ return;}
										
										save_mouseDown = true;
										$(this).unbind("mouseleave");
										$(this).bind("mouseleave",function(){
																		   JSObject.form.focus();
																		   })
										
										
										$(JSObject.DOMDoc.body).mouseup(function(){
																		 
																		 if (save_mouseDown == true){
																			save_mouseDown = false;
																			$(JSObject.save_btn).unbind("mouseleave");
																			$(JSObject.DOMDoc.body).unbind("mouseup");
																			JSObject.sendValues = true;
																			$('form',JSObject.DOMDoc).get(0).focus();
																			JSObject.validate();
																		 }
																		   })
										
									});
		
		
		$(this.save_btn).mouseup(function(){
										
										if (JSObject.sendValues == true) return;
										
										if (save_mouseDown == true){
											save_mouseDown = false;
											$(this).unbind("mouseleave");
											$(JSObject.DOMDoc.body).unbind("mouseup");
											JSObject.sendValues = true;
											JSObject.form.focus();
											JSObject.validate();
										}
									});
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION INIT PICTURES                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initPictures = function(){
		
		for (var i=0; i<$("img[id='appletImage']",this.DOMDoc).get().length; i++){
			
			var img = $("img[id='appletImage']",this.DOMDoc).get(i);
			
			$(img).css('cursor','pointer');
			$(img).data('index',i);
				
			$(img).unbind("mouseover");
			$(img).bind("mouseover",function(){
				
				JSObject.createPictureOverlay(this);
			
			});
		}
		
	}
	
	
	this.createPictureOverlay = function(imgObj){
		
		$('#picture_overlay',JSObject.DOMDoc).remove();
		$($('body',JSObject.DOMDoc).get(0)).append('<div id="picture_overlay" style="position:absolute"><div class="picture_overlay" style="width:100%; height:100%"></div><div style="position:absolute; top:40px; left:75px"><img src="'+JSInterface.localpath+'includes/images/icons/play_icon.gif'+'" border="0" /></div></div>');
		$('#picture_overlay',JSObject.DOMDoc).height($(imgObj).height());
		$('#picture_overlay',JSObject.DOMDoc).width($(imgObj).width());
		
		var overlay = $('#picture_overlay',JSObject.DOMDoc);
		
		overlay.css('cursor','pointer');
		overlay.data("img",imgObj);
		
		overlay.css('top',$(imgObj).offset().top);
		overlay.css('left',$(imgObj).offset().left);
		
		
		//attach actions on overlay
		overlay.mouseover(function(){
			
		});
		
		overlay.mouseleave(function(){
			overlay.css('display','none');
		});
		
		overlay.click(function(){
			var index = $(imgObj).data("index");
			$("a.thickbox[id='ecard_link']:eq("+index+")",JSObject.DOMDoc).trigger("click");
		});
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                          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"){
			
		}
		else{
			
		}
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                               FUNCTION SERVER VALIDATE(AJAX - Email)                              */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initServerValidate = function(){
		
		JSObject.sendData();
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 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
			this.initServerValidate();
		}
		else{
			JSObject.sendValues = false;
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SUBMIT FORM                                         */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.submitForm = function(){
		
		//return JSInterface.AjaxUpload.dosubmit(JSObject.form, {'onStart' : JSObject.startUploadingData, 'onComplete' : JSObject.completeUploadingData});
		return true;
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      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();
				
	}
	
	
}