/**
* @package   Javascript
* @version   $Id: fw.js
* @author
* @copyright ZFcms
* @filesource
*/

String.prototype.trim = function()
{
	var reg1 = new RegExp("^\\s*");
	var reg2 = new RegExp("\\s*$");
	return this.replace(reg1,"").replace(reg2,"");
};

//------------------------------------------------------------------------------

var G_objFramework;

/**
 * Fonction d'initialisation
 * @return void
 * @access public
 */
function framework()
{
	G_objFramework = new ClasseFramework();
}

/**
 * Classe Framework
 */
function ClasseFramework()
{
 /**
  * @var array : tableau des menus
  */
	this.TabMenu = new Array();

	/**
	 * Fonction d'ouverture de popup
	 * @param string url     : adresse URL
	 * @param integer width  : largeur
	 * @param integer height : hauteur
	 * @param string nom     : nom du popup
	 * @return void
	 * @access public
	 */
	this.popup = function(url,width,height,nom)
	{
	    var n6 = (document.getElementById && ! document.all) ? 1 : 0;
	    var plusN6 = 10;
	    if ( width < 1 )
	    {
	        var windowWidth = Math.round(screen.width * width);
	    }
		else
	    {
	        var windowWidth = parseInt(width);
	    }
	    if ( height < 1 )
	    {
	        var windowHeight = Math.round(screen.height * height);
	    }
	    else
	    {
	        var windowHeight = parseInt(height);
	    }
		if ( n6 ) windowHeight = windowHeight + plusN6;
		var windowLeft = (screen.width-windowWidth)/2;
		var windowTop  = (screen.height-windowHeight)/2;
		var w = window.open(url,nom,"menubar=yes,status=yes,resizable=yes,scrollbars=yes,fullscreen=no,width="+windowWidth+",height="+windowHeight+",top="+windowTop+",left="+windowLeft);
	}

	/**
	 * Fonction de roll-over
	 * @param string src : source de l'image rollOver
	 * @param string img : nom de l'image rollOver
	 * @return void
	 * @access public
	 */
	this.OverImage = function(src,rollOverImage)
	{
	    document.getElementById(rollOverImage).src = src;
	    document.getElementById(rollOverImage).style.display = 'block';
	}

	/**
	 * Fonction de fin de roll-over
	 * @return void
	 * @access public
	 */
	this.OutImage = function(rollOverImage)
	{
		document.getElementById(rollOverImage).style.display = 'none';
	}

	/**
	 * Fonction de soumission du panier
	 * @param string form : nom du formulaire
	 * @return void
	 * @access public
	 */
	this.checkPanier = function(action,form)
	{
	    if ( validePanier(form) )
	    {
	    	form.action = action;
		    form.submit();
	    }
	}

	function validePanier(form)
	{
		var article = "article[";
		for (var i=0;i<form.elements.length;i++)
		{
			var champ = form.elements[i];
			if ( champ.name.substring(0,article.length) == article )
			{
				var msg = '';
				var RE  = /^\-?[0-9]+$/;
				if ( champ.value == '' || ! RE.test(champ.value) ) msg = 'Quantité non valide !';
				if ( msg != '' )
				{
					alert(msg);
					champ.focus();
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Fonction modifiant une quantité du panier
	 * @param string champ : nom du champ
	 * @return void
	 * @access public
	 */
	this.changeQuantite = function(form)
	{
		if ( validePanier(form) ) form.submit();
	}

	/**
	 * Fonction affichant un message de confirmation en suppression d'un élément du panier
	 * @param string msg : message d'alerte
	 * @param string url : URL de redirection
	 * @return void
	 * @access public
	 */
	this.deletePanier = function(msg,url)
	{
		if ( confirm(msg) ) document.location.href = url;
	}

	/**
	 * Fonction de redirection
	 * @param string url : adresse url
	 * @return void
	 * @access public
	 */
	this.redirection = function(url)
	{
		document.location.href = url;
	}

	/**
	 * Fonction de validation du transport
	 * @param string form : nom du formulaire
	 * @param string msg  : message d'alerte
	 * @return boolean : au moins un transport sélectionné (ou non)
	 * @access public
	 */
	this.validateTransport = function(form,msg)
	{
	    for (var i=0;i<form.elements.length;i++)
	    {
	        if ( form.elements[i].name == "typetransport" && form.elements[i].checked )
	        {
	        	form.submit();
	        	return true;
	        }
	    }
	    alert(msg);
	    return false;
	}

	/**
	 * Fonction de validation de commande
	 * @param string form : nom du formulaire
	 * @param string msg  : message si CGV non validées
	 * @return boolean : CGV validée (ou non)
	 * @access public
	 */
	this.validateCommande = function(form,msg)
	{
	    if ( ! form.cgv.checked )
	    {
	    	alert(msg);
	        document.getElementById('labelcgv').className += ' warning';
	        document.getElementById('WARNCGV').style.visibility = "visible";
	        form.cgv.focus();
	        return false;
	    }
	    form.submit();
	    return true;
	}

	/**
	 * Fonction de validation de commande
	 * @param string form : nom du formulaire
	 * @param string msg1 : message de confirmation
	 * @param string msg2 : message de confirmation (suite)
	 * @param string msg3 : message d'alerte
 	 * @return boolean : au moins un type de paiement sélectionné (ou non)
	 * @access public
	 */
	this.validatePaiement = function(form,msg1,msg2,msg3)
	{
	    for (var i=0;i<form.elements.length;i++)
	    {
	        if ( form.elements[i].name == "typepaiement" && form.elements[i].checked )
	        {
				if ( confirm(msg1+"\n"+msg2) )
				{
		            form.submit();
		            return true;
				}
				else
				{
				    return false;
				}
	        }
	    }
	    alert(msg3);
	    return false;
	}

	/**
	 * Fonction de création d'un favoris
	 * @param string uu : URL
	 * @param string tt : titre
	 * @return boolean : succès ou échec de la création
	 * @access public
	 */
	this.CreateBookmarkLink = function(uu,tt)
	{
	    if ( tt == '' )
	    {
	        var title = document.title;
	    }
	    else
	    {
	        var title = tt;
	    }
	    if ( uu == '' )
	    {
	        var url = document.location.href;
	    }
	    else
	    {
	        var url = uu;
	    }
	    if ( window.sidebar && (typeof window.sidebar.addPanel == 'function') )
	    {
	        // Mozilla Firefox Bookmark
	        return window.sidebar.addPanel(title,url,'');
	    }
	    else if( window.external )
	    {
	        // IE Favorite
	        return window.external.AddFavorite(url,title);
	    }
	    else
	    {
	        return true;
	    }
	}

	/**
	 * Fonction de confirmation
	 * @param string msg : message d'alerte
	 * @param string url : URL de redirection
	 * @return void
	 * @access public
	 */
	this.confirmLink = function(msg,url)
	{
	    if ( confirm(msg) ) document.location.href = url;
	}

	/**
	 * Fonction de confirmation via formulaire
	 * @param object form  : objet formulaire
	 * @param string texte : texte de confirmation
	 * @return void
	 * @access public
	 */
	this.confirmSubmit = function(msg,form)
	{
	    if ( confirm(msg) ) form.submit();
	}

	/**
	 * Passage sur un menu
	 * @param object obj : objet à traiter
	 * @return void
	 * @access public
	 */
	this.overMenu = function(obj)
	{
 		this.TabMenu[obj] = 0;
		document.getElementById(obj).style.display = 'block';
	}

	/**
	 * Sortie du menu
	 * @param object obj : objet à traiter
	 * @return void
	 * @access public
	 */
	this.outMenu = function(obj)
	{
		var curObj = obj;
		var thisObj = this;
		setTimeout(function(){ if ( thisObj.TabMenu[curObj] == 0 ) {document.getElementById(curObj).style.display = 'none'; thisObj.TabMenu[curObj] = -1;} },100);
	}

	/**
	 * Passage sur le menu item
	 * @param object obj : objet à traiter
	 * @param int indice : indice du sous-menu
	 * @return void
	 * @access public
	 */
	this.overMenuItem = function(obj,indice)
	{
		this.TabMenu[obj] = indice;
	}

	/**
	 * Sortie du menu item
	 * @param object obj : objet à traiter
	 * @param int indice : indice du sous-menu
	 * @return void
	 * @access public
	 */
	this.outMenuItem = function(obj,indice)
	{
		var curObj = obj;
		var curInd = indice;
		var thisObj = this;
		setTimeout(function(){ if ( thisObj.TabMenu[curObj] == curInd ) {document.getElementById(curObj).style.display = 'none'; thisObj.TabMenu[curObj] = -1;} },100);
	}

	/**
	 * Relaod de la page parente
	 * @return void
	 * @access public
	 */
	this.reloadParent = function()
	{
		opener.location.href = opener.location.href;
	}

	/**
	 * Fonction qui masque des objets
	 * @param object form     : objet formulaire
	 * @param string champ    : nom du champ
	 * @param boolean chkflag : true ou false
 	 * @return void
	 * @access public
	 */
	this.checkall = function(form,champ,chkflag)
	{
		for (var i=0;i<form.elements.length;i++)
		{
	    	if ( form.elements[i].name.substring(0,champ.length) == champ ) form.elements[i].checked = chkflag;
		}
	}

	this.apparaitre = function(obj1,obj2)
	{
 		if ( document.getElementById(obj2) ) document.getElementById(obj2).style.display = 'none';
 		document.getElementById(obj1).style.display = 'block';
	}

	this.disparaitre = function(obj)
	{
 		document.getElementById(obj).style.display = 'none';
	}

	this.calendrier = function(url,id)
	{
		date = this.replaceAll(document.getElementById(id).value,'/','-');
		this.popup(url+date,250,250,'CALENDRIER');
	}

	this.replaceAll = function(text, strA, strB)
	{
	    while ( text.indexOf(strA) != -1)
	    {
	        text = text.replace(strA,strB);
	    }
	    return text;
	}

	this.selDate = function(champ,date)
	{
		opener.document.getElementById(champ).value = date;
		self.close();
	}
}