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

function CONTACT_GINFO(parent){
	var JSObject = this;
	this.type = "Newsletter"; 
	this.arr_inputs = ["_inp_Lastname","_inp_Firstname","_inp_Email","_inp_Company","_inp_Address",
					   "_inp_Postcode","_inp_City","_inp_Country","_inp_Phone","_inp_Domain","_inp_Function",
					   "_inp_Remarks"];
	this.form = document.getElementById("contact_form");
	this.ajax = false;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                            FUNCTION INIT INPUTS CONTACT PANEL                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		this._inp_Lastname = new INPUTFIELD(this, document.getElementById('contact_lastname'));
		this._inp_Firstname = new INPUTFIELD(this, document.getElementById('contact_firstname'));
		this._inp_Gender = new INPUTFIELD(this, document.getElementById('contact_gender'));
		this._inp_Email = new INPUTFIELD(this, document.getElementById('contact_email'));
		this._inp_Company = new INPUTFIELD(this, document.getElementById('contact_company'));
		this._inp_Phone = new INPUTFIELD(this, document.getElementById('contact_phone'));
		this._inp_Function = new INPUTFIELD(this, document.getElementById('contact_function'));
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  vérification des champs                          */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){		
				//var reg = new RegExp('^[a-zA-Z0-9]+([_\.-]{1}[a-zA-Z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
				var reg = new RegExp('^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*@[a-zA-Z0-9]+([_|\.|-]{1}[a-zA-Z0-9]+)*[\.]{1}[a-zA-Z]{2,6}$', 'i');
                var nomChamps = Array('type','gender','lastname','firstname','phone','email','text');
                this.error = false;
                for(var i=0;i<nomChamps.length;i++)
                {
                    var input = $("#contact_"+nomChamps[i]);
                    if (!input.val())
                    {
                        $("#contact_"+nomChamps[i]).addClass('error');
                        $("#contact_img_"+nomChamps[i]).attr("src","../includes/images/form_require_error.png");
                        this.error = true;
                    }
                    else
                    {
                    	if(nomChamps[i]=="email") {
                    		if(reg.test($('#contact_email').val())){                			
                    			$("#contact_email").removeClass('error');
     	                        $("#contact_img_email").attr("src","../includes/images/form_require.png");                			
                    		} else {
                				$("#contact_email").addClass('error');
                                $("#contact_img_email").attr("src","../includes/images/form_require_error.png");
                                this.error = true;	                       
                    		} 
                    	}else{
                    		  $("#contact_"+nomChamps[i]).removeClass('error');
                              $("#contact_img_"+nomChamps[i]).attr("src","../includes/images/form_require.png");
                    	}
                    	
                    }
                }
		
		
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                             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++;
			}
		}
		*/
		
		/*var valid = false;
		for (var i=0; i<document.getElementsByName('contact_comment').length; i++){
			var checkbox = document.getElementsByName('contact_comment')[i];
			if (checkbox.checked == true) valid = true;
		}
		if (valid == false){
			countErrors++;
			document.getElementById('contact_comment_container').style.display = 'block';
			document.getElementById('contact_comment_container').innerHTML = "<p class='error_text'>Champ obligatoire.</p>";
		}*/
		
		
		
		if (this.error==false){ 
			
			var comment_connu_str = "";
			for (var i=0; i<document.getElementsByName('contact_comment').length; i++){
				var checkbox = document.getElementsByName('contact_comment')[i];
				if (checkbox.checked == true) comment_connu_str += checkbox.value+",";
			}
			comment_connu_str = comment_connu_str.substring(0,comment_connu_str.length-2);
	
			jQuery.ajax({ url :
                        LOCALPATH+"contact_action.php", data :
                         'type='+$('#contact_type').val()+
                         '&lastname='+$('#contact_lastname').val()+
			 '&firstname='+$('#contact_firstname').val()+
			 '&gender='+$('#contact_gender').val()+
			 '&email='+$('#contact_email').val()+
                         '&text='+$('#contact_text').val()+
			 '&company='+$('#contact_company').val()+
			 '&phone='+$('#contact_phone').val()+
			 '&function='+$('#contact_function').val(),
			 
			 success: function(response) {				
				 if (response == "ok"){
					 var cell = document.getElementById('responce');
					 cell.innerHTML = "";
					 cell.vAlign = "middle";
					 cell.align = "center";
					 cell.innerHTML = 'Merci pour votre message, nous vous r&eacute;pondrons dans les meilleurs d&eacute;lais';
					 $('#contact_form').css('display', 'none');
					 $('#contact_location').css('display', 'none');
					 $('.content strong').css('display', 'none');	
				 } else {
					 var cell = document.getElementById('responce');
					 cell.innerHTML = "toto";
					 cell.vAlign = "middle";
					 cell.align = "center";					 
					 cell.innerHTML = 'une erreur est survenu';
				 }
				// alert('resp:'+response);
			 }
             });	
		}
		else{ 
			return false;
		}
		
	}
	
}
