var NumericBox = new NumericBox();

function NumericBox()
{
	this.Verify = function(control,exp,message)
	{
		var Expression = new RegExp(exp);
		if(!Expression.test(control.value))
			alert(message);
	};
};
var TelBox = new TelBox();

function TelBox()
{
	this.Verify = function(control,exp,message)
	{
		var Expression = new RegExp(exp);
		if(!Expression.test(control.value))
			alert(message);
	};
};var DateBox = new DateBox();

function DateBox()
{
	this.Verify = function(control,exp,message)
	{
		var Expression = new RegExp(exp);
		if(!Expression.test(control.value))
			alert(message);
	};
};var EmailBox = new EmailBox();

function EmailBox()
{
	this.Verify = function(control,exp,message)
	{
		var Expression = new RegExp(exp);
		if(!Expression.test(control.value))
			alert(message);
	};
};var TabStrip = new TabStrip();


function TabStrip()
{
	//Affichage d'un onglet
	this.ShowTab = function(onglet,tabId,nbOnglet)
	{
		//reinitialisation de tous les onglets
		for(i=0;i<nbOnglet;i++)
		{
		 tabs=document.getElementById('tab_'+i);
		 tabs.style.height='0px';
		 tabs.style.display = 'none';

		 index=document.getElementById('index_'+i);
		}

		tab=document.getElementById(tabId);
		tab.style.height='100%';
		tab.style.display = 'block';
	}
}var UserActionColumn=new UserActionColumn();

function UserActionColumn()
{
	this.DoAction = function (UserAction, sender, id)
	{
		document.forms[sender].UserAction.value = UserAction;
		document.forms[sender].IdEntity.value = id;
	    document.forms[sender].submit();
	};
}var MenuV = new MenuV();

function MenuV()
{
	this.openSubMenu = function(element)
	{

	   var submenu=document.getElementById(element);

	   if(submenu != null)
		{
		submenu.style.height ="100px";
		submenu.style.overflow ="visible";
		submenu.style.visibility ="visible";
		}

	}

	this.CloseSubMenu= function(element)
	{
		var submenu=document.getElementById(element);

		if(submenu != null)
		{

		submenu.style.height ="0px";
		submenu.style.overflow ="hidden";
		submenu.style.visibility ="hidden";
		}
	}
};var LogBlock=new LogBlock();

function LogBlock()
{
	this.Delete	= function()
	{

		this.data = "Action=Delete";
		this.Send();
	};

	this.Send=function()
	{
	  var ajax=new XMLHttpRequest();
	  ajax.open('POST','Ajax.php',false)
	  ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  ajax.send(this.data);

	  return ajax.responseText;
	};
}function PopUp(propriete, argument,action)
{

	//Deserialisation
	property=new Array();
	property = serialization.Decode(propriete);

	arg=new Array();
	arg = serialization.Decode(argument);

	actions = new Array();
	actions = serialization.Decode(action);

	//Propriete
	this.core="";
	this.page = "Ajax.php";
	this.idEntity=arg["idEntity"];

	this.data +="&Arg="+argument;

	for(ar in arg)
	{
  		 this.data += "&"+ar+"="+arg[ar];
	}

	this.Open=function()
	{

	this.core = document.createElement('div');

	var i=0;
	while(document.getElementById(property['Name']+i) != null)
	{
		i++;
	}

	this.core.id = property['Name']+i;

    this.core.style.backgroundColor=property["BackGroundColor"];
    this.core.style.color=this.colore;
    this.core.style.position='absolute';
    this.core.style.left=property["Left"];
    this.core.style.top=property["Top"];
	  this.core.style.width= property["Width"];
	  this.core.style.height=property["Height"];
    this.core.style.overflow="auto";//property["Overflow"];
    this.core.style.opacity=property["Opacity"];
    this.core.style.border='1px solid black';
   
	this.Classe = property["Class"];
	this.RefreshPage = property["RefreshPage"];

    //Remplacement des caracteres
    if(actions["OnClose"] != null)
    {
     var i=0;var b='"';
      while (i!=-1)
       {
         i=actions["OnClose"].indexOf("*",i);
         if (i>=0) {
            actions["OnClose"]=actions["OnClose"].substring(0,i)+b+actions["OnClose"].substring(i+"*".length);
            i+=b.length;
         }
      }
    this.onClose = actions["OnClose"];
   }
   else
   {
   this.onClose ="";
   }
	tool=document.createElement('div');
    this.core.appendChild(tool);

    tool.innerHTML="<table  class='titre'><tr><td  style='overflow:auto;position: relative; '>"+property["Title"]+"</td><td style='text-align:right;'><img src='Images/maximize.png' alt='' title='Fermer' onclick='Maximize(\""+this.core.id+"\")' ><img src='Images/delete.png' alt='' title='Fermer' onclick='Close(\""+this.core.id+"\",\""+this.Classe+"\",\""+this.RefreshPage+"\");"+this.onClose+"' ></td><td style='width:15px'></td></tr></table>";

	 this.page +="?idEntity="+this.idEntity;

	 this.frame=document.createElement('iframe');

	 this.frame.style.width=property["Width"];
	 this.frame.style.height=property["Height"];

	 if(arg["Url"] =="")
   	 {
   	   	 this.core.innerHTML +="<span id='Fermer' onclick='document.body.removeChild(this.parentNode);' ></span>"+this.Send();
	 }
	 else
	 {
		 if(this.idEntity != undefined)
		 {
		 	if(arg["Url"].indexOf('?')>0)
	 		{
		 		this.frame.src = arg["Url"]+"&idEntity="+this.idEntity;
	 		}
		 	else
		 	{
	 			this.frame.src = arg["Url"]+"?idEntity="+this.idEntity;
			}
		 }
		 else
		 {
		 	this.frame.src = arg["Url"];
		 }

		 	this.core.appendChild(this.frame);
	 }

    document.body.appendChild(this.core);

	}

	this.Close=function()
	{
  		document.body.removeChild(block);
  	}

	this.Send=function()
	{
	  var JAjax = new ajax();
	  JAjax.data = this.data;
	  return JAjax.GetRequest(this.page);
	}
};

function Maximize(popup)
{

   po=document.getElementById(popup);
   fr=po.getElementsByTagName("iframe");

	if(po.style.width == "1000px")
	{
		po.style.width = "300px";
		po.style.height ="40px";
		fr[0].style.width = "300px";
		fr[0].style.height ="20px";
	}
	else
	{
		po.style.width = "1000px";
		po.style.height ="800px";
		fr[0].style.width = "1000px";
		fr[0].style.height ="800px";
	}
}

function Close(popup,classe,url)
{
 	po=document.getElementById(popup);
    document.body.removeChild(po);
}
var JUserAction=new UserAction();

function UserAction()
{
	this.DoAction = function (UserAction, Arg, sender)
	{

		if(sender.form != null)
		{
	 	sender.form.UserAction.value = UserAction;
	    sender.form.Arg.value = Arg;

		if(sender.form.Sender != null)
			{sender.form.Sender.value = sender.value;}

		sender.form.submit();
		}
		else
		{
		//recuperation du formulaire
		var formBlock = sender.parentNode;
		while(formBlock.nodeName != "FORM")
		{
			formBlock = formBlock.parentNode;
		}

		formBlock.UserAction.value = UserAction;
		formBlock.Arg.Value = Arg;
		formBlock.Sender.Value = sender.value;
		formBlock.submit()
		}
	};
}function jax(propriete, argument)
{
  var action = new AjaxAction(propriete, argument);
  action.DoAction();
}



AjaxAction = function (propriete, argument)
{
		//Deserialisation
		property=new Array();
		property = serialization.Decode(propriete);

		arg=new Array();
		arg = serialization.Decode(argument);

		//Propriete
		this.page = "Ajax.php";

		this.data +="&Arg="+argument;

		for(ar in arg)
		{
  		 this.data += "&"+ar+"="+arg[ar];
		}

		this.DoAction = function (propriete, argument)
		{
			var sourceControl = document.getElementById(property["SourceControl"]);
			var ChangedControl= document.getElementById(property["ChangedControl"]);

			this.data +="&sourceControl="+sourceControl.value;

			ChangedControl.innerHTML=this.Send();
		};

	this.Send=function()
	{
	  var JAjax = new ajax();
	  JAjax.data = this.data;
	  return JAjax.GetRequest(this.page);
	}
}

//********************Objet Ajax **********************

ajax=function()
  {
  //propriétes
   var requete='';
   this.method='post';
   this.mode='s';
   this.data="";
   this.RetunData="";

   //creation de la requete
   this.CreateRequete=function()
	  {
	    if (window.XMLHttpRequest)
	          {
	         requete = new XMLHttpRequest();
	          }
	    else if (window.ActiveXObject)
	          {
	          requete = new ActiveXObject("Msxml2.XMLHTTP");
	          }
	    else
	         {
	         requete = new ActiveXObject("Microsoft.XMLHTTP");
	         }
	   }

	//envoi de la requete
	this.GetRequest=function(fichier)
  		{
  		this.CreateRequete();
  		typef=fichier.substring(fichier.indexOf("."));

		//selection du type de methode
    	if(this.method=='get')
		      {
			      methode="GET";
			      donnees=null;
		      }
		      else
		      {
			      methode="POST";
			      donnees=this.datas;
     		 }
		//selection du mode
	    if (this.mode=='s')
	    {
	    	modes=false;
	    }
	    else
	    {
	    	modes=true;
	    }

	//envoi de la requete
  	requete.open(methode,fichier,modes);

	//affichage en fonction du mode
  if (this.mode=='s')
    {
   		requete.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   		requete.send(this.data);
  		return select();
    }
    else
    {
		requete.onreadystatechange=this.actionrequete;
		requete.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		requete.send(this.data);
    }
//action appele en most asychrone
	this.actionrequete=function()
	{
		if(requete.readyState==4)
		      {
		        if(requete.status !=500 && requete.status !=400)
		        {
		        block.innerHTML=select();
		        }
		      }

	    }
 	function select()
 	{
            if (typef=='.xml')
            {
            	type=requete.responseXML;
            }
            else
             {
            	type=requete.responseText;
    		 }
 		return type;
 	}
  }

  this.Refresh = function(url,classe,methode)
  {
  	control =new Array();
  	if(url.indexOf('?')>0)
 	{
		controls = serialization.Decode32(this.GetRequest(url+"&CallType=Ajax&Action="+methode));
	}
	else
	{
		controls = serialization.Decode32(this.GetRequest(url+"?CallType=Ajax&Action="+methode));
	}
	for(control  in controls)
	{
		var ctr = document.getElementById(control);

		if(ctr != null)
		{
			var parent = ctr.parentNode;
			var Ndiv = document.createElement("div" );
            Ndiv.innerHTML = controls[control];
            parent.removeChild(ctr);
            parent.appendChild(Ndiv);
		}
	}
  }
}
var Message=new Message();

function Message()
{

	this.Show = function(message)
	{
		alert(message);
	};

	this.Ask=function(message)
	{
		confirm(message);
	};
};var serialization = new Serialization();

function Serialization()
{
	this.Decode = function (chaine)
	{
	var args = chaine.split(":")
	var property = new Array();

		for(i=0;i<args.length;i++)
		{
			prop = args[i].split("!");
			property[prop[0]]=prop[1];
		}

	return property;
	}

	this.Decode32 = function (chaine)
	{
	var args = chaine.split("%")
	var property = new Array();

		for(i=0;i<args.length;i++)
		{
			prop = args[i].split("|");
			property[prop[0]]=prop[1];
		}

	return property;
	}
}
