if(this.SAMEH == undefined)
{
	SAMEH = {};

}

if(window.console == undefined)
{
	window.console = {log: function(s){}};	
}

SAMEH.UINumField = new  Class.create();

SAMEH.UINumField.prototype = {
		
		current_value: "",
		initialize: function(id, typeTest)
		{
			if($(id)==undefined) return;
			$(id).gestionaire = this;
			this.current_value = $(id).value;
			
				
			$(id).observe('keyup', function()
			{
				if(this.current_value != $(id).value)
				{
					try
					{
						if($(id).value == "") 
						{
							this.current_value = "";
							return;
						}
						var v = parseInt($(id).value);

						if((v + '') == 'NaN')
						{
							
							$(id).value = this.current_value ;
							
						}
						else
						{
							if(typeTest != undefined)
							{
								switch(typeTest)
								{
								case 'j':
									if(v >=1 && v <=31)
									{
										this.current_value = $(id).value;
									}
									else
									{
										$(id).value = this.current_value ;
									}
									break;
								case 'm':
									if(v >=1 && v <=12)
									{
										this.current_value = $(id).value;
									}
									else
									{
										$(id).value = this.current_value ;
									}
									break;
								case 'y':
									
									if($(id).value.length>3)
									{
										var d = new Date();
										if(v >=d.getFullYear() )
										{
											this.current_value = $(id).value;
										}
										else
										{
											$(id).value = this.current_value ;
										}
									}
									else
									{
										this.current_value = $(id).value;
									}
									
									break;
								}
							}
							else
								this.current_value = $(id).value; 
						}
					}
					catch(e)
					{
						alert(e);
					}
					//
				}
				
			}.bindAsEventListener(this));
		},
		
		initCalandar: function(id, bt, idj, idm, idy)
		{
			
		}
}

SAMEH.UIClass = new  Class.create();
SAMEH.UIClass.prototype = {
	
	
		
	
	initialize: function()
	{
	},
	addNewClient: function()
	{
		//alert($("messageForm").action);
		//if($("messageForm").action.indexOf('#')) $("messageForm").action = $("messageForm").action + "#contact"
		$("messageForm").innerHTML += "<input type='hidden' name='addcontact' value='1'><input type='hidden' name='Submit' value='ok'>";
		
		$("messageForm").submit();
	},
	
	submit: function(id, name, val)
	{
		alert(id + ", " + name + ", " + val)
		$(id).innerHTML += "<input type='hidden' name='"+name+"' value='"+val+"'>";
		$(id).submit();
	},
	
	passGen: function(id)
	{
		var pass="";
		var t=["a","b","c","d","e","f","g","h","i","j","l","k","m","n","q","p","o","r","s","t","y","w","z"]
		for(var i = 0; i < 10; i ++)
		{
			if(Math.random() < 0.25)
			{
				pass += "" + Math.floor(10*Math.random())
				
			}
			else
			{
				var n = Math.floor(t.length*Math.random())
				if(Math.random() < 0.75)
				{
					pass += t[n];
				}
				else
				{
					pass += t[n].toUpperCase();
				}
				
			}
		}
		
		$(id).value=pass;
		
	},
	
	updateRegisterDepartement: function()
	{
		if($('pays_reference').value == "") return;
		if($("golfs_reference_error") != undefined) $("golfs_reference_error").update("");
		new Ajax.Request("index.php?",
		{
			method: 'post',
			parameters: {   action: "achat",
						  	list: "departement",
						  	pays: $('pays_reference').value
						 },
						  	
			onSuccess: function(val, json){ 				
				var json = null;	
				try
				{
					$("ref_departement").update(val.responseText);
					//$("golfs_ref").setStyle({height: ($("golfs_ref").getHeight() + $("region_ref").getHeight())+"px"});
					
				}
				catch(exp)
				{
					window.console.log(exp);
				}
         	}.bind(this)
         });
			
	},
	
	updateRegisterGolfs: function()
	{
		if($('pays_reference').value == "") return;
		if($("golfs_reference_error") != undefined)$("golfs_reference_error").update("");
		new Ajax.Request("index.php?",
		{
			method: 'post',
			parameters: {   action: "achat",
						  	list: "golfs",
						  	departement: $("departement_reference").value,
						  	pays: $('pays_reference').value
						 },
						  	
			onSuccess: function(val, json){ 				
				var json = null;	
				try
				{
					$("ref_golf").update(val.responseText);
					//$("golfs_ref").setStyle({height: ($("golfs_ref").getHeight() + $("region_ref").getHeight())+"px"});
					
				}
				catch(exp)
				{
					window.console.log(exp);
				}
         	}.bind(this)
         });
			
	},
	
	getProductInfo: function(id, type)
	{
		new Ajax.Request("index.php?",
		{
			method: 'post',
			parameters: {   action: type,
						  	info: id
						  	
						 },
						 
						 
			onSuccess: function(val, json){ 				
				var json = null;	
				try
				{
					this.current_produit = id;
					$("prestations2").update(val.responseText);
					/*$("offres").setStyle({height: ($("produit_text").getHeight() + 50)+"px"});
					$("offre_choix2").setStyle({height: ($("produit_text").getHeight() + 40)+"px"});
					$("prestations2").setStyle({height: ($("produit_text").getHeight() + 45)+"px"});*/
					//alert($("produit_text").getHeight());
					//$("golfs_ref").setStyle({height: ($("golfs_ref").getHeight() + $("region_ref").getHeight())+"px"});
					
					
					if($("produit_text").getHeight() > ($("prestations2").getHeight() - 20))
					{
						$("prestations2").setStyle({height: ($("produit_text").getHeight() + 20)+"px"});
					}
					
					$("produit_text").setStyle({height: ($("produit_text").getHeight() + 20)+"px"});
					
					$("obligatoire").update("");
					if(id=="2")
					{
						$("obligatoire").update("* &nbsp;");
					}
					
					$("prestations2").setStyle({height: "auto"});
					$("code_promo").setStyle({display: "block"});
					if($("current_code_promo"))
					{
						try
						{
							if( $("current_code_promo").value != undefined)
							{
								
								var v = $("current_code_promo").value;
								this.updateCodePromo(v, type, "");
									
							}
						}
						catch(exppp)
						{
							alert(exppp);
						}
					}
					
					/*var self =this;
					$("valide-promo").observe("click", function()
					{
						self.updateCodePromo($('code_promotion').value, 'achat')
					}.bindAsEventListener());*/
					
					
				}
				catch(exp)
				{
					window.console.log(exp);
				}
         	}.bind(this)
         });
	},
	
	updateCodePromo: function(id, type, mode)
	{
		var p = this.current_produit;
		new Ajax.Request("index.php?",
		{
			method: 'post',
			parameters: {   action: type,
						  	promo: id,
						  	product: p,
						  	mode : mode
						 },
						 
			onSuccess: function(val, json){ 				
				var json = null;	
				try
				{
					$("code_promo").setStyle({display: "block"});
					this.current_promo = id;
					$("code_promo").update(val.responseText);
				}
				catch(exp)
				{
					window.console.log(exp);
				}
         	}.bind(this)
         });
	},
	
	updateTwitter: function(p)
	{
			
		new Ajax.Request("index.php?",
		{
			method: 'post',
			parameters: {   action: "twitter",
						  	pos: p
						 },
						  	
			onSuccess: function(val, json){ 				
				var json = null;	
				
				try
				{
					$("actus").innerHTML =val.responseText; 
				}
				catch(exp)
				{
					window.console.log(exp);
				}
         	}.bind(this)
         });
		
	},
	
	initCalandar: function(id, bt, idj, idm, idy, calcDate)
	{
		//alert($(id) + ", " + $(bt))
		
		
		var dateDay = new Date().toString();
		
		$(idj).observe("focus", function(){$(idj).select();})
		$(idm).observe("focus", function(){$(idm).select();})
		$(idy).observe("focus", function(){$(idy).select();})
		
		
		if(calcDate)
		{
			var j = -1;
			var m = -1;
			var y = -1;
			if($(idj).value != "")
			{	
				if($(idj).value[0] == "0")
					j = parseInt($(idj).value[1]);
				else
					j = parseInt($(idj).value);
				
			}
			
			if($(idm).value != "")
			{	
				if($(idm).value[0] == "0")
					m = parseInt($(idm).value[1]);
				else
					m = parseInt($(idm).value);
				
				
				
			}
			if($(idy).value != "")
			{	
				y = parseInt($(idy).value); 			
			}
			
			if(j != -1 && m != -1 && y != -1)
			{
				dateDay = new Date(y, m, j).toString();
				var v = m +1;
				if(v < 10)
					$(idm).value = "0" + v;
				else 
					$(idm).value = v;
			}
			
			if($(idm).value == "") $(idm).value = "MM";
			if($(idm).value == "NaN") $(idm).value = "MM";
			if($(idj).value == "") $(idj).value = " JJ";
			if($(idj).value == "00") $(idj).value = " JJ";
			if($(idy).value == "") $(idy).value = "AAAA";
			
			//alert(dateDay + ", " + j + "/" + "/" + m + "/" + y)
		}
		
		/*Calendar.setup(
			    {
			      inputField	: id,
			      ifFormat		: "%d/%m/%Y",
			      button		: bt,
			      align			: "Bl",
			      date			: dateDay,
			      //date			: new Date().toString(),
			      
			      
			      onUpdate		: function(calendar)
			      {
			    	$(idy).value = calendar.date.getFullYear();
			    	$(idm).value = (calendar.date.getMonth() + 1);
			    	$(idj).value = calendar.date.getDate();			    	
			      }.bind(this)
			    }
			  );*/
						
	},
	
	getCodePromo: function(id)
	{
		var a = "azertyuiopqsdfghjklmwxcvbnAZERTYUIOPMLKJHGFDSQWXCVBN1234567890";
		var code = "";
		var s = a.length;
		for(var i=0; i < 10; i++) 
		{
			var n = Math.floor(Math.random()*s)
			code += a[n];
		}
		if($(id) != undefined) $(id).value = code;
	}
	
	
	
}
//
SAMEH.UI = new SAMEH.UIClass (); 
