var bulle_tp=0;
var bulle_obj;
var bulle_div;
var drag_isdrag=false;
var drag_x;
var drag_y;
var drag_dobj;

//Firefox
var ie = /MSIE/.test(navigator.userAgent);
//var moz = !ie && navigator.product == "Gecko";
var moz = !ie



//event
window.onload = CaptureOnload;
document.oncontextmenu = CaptureOnContextMenu;
document.onclick=CaptureOnClick;
document.onmousemove=CaptureOnMouseMouve;
document.onmousedown=selectmouse;
document.onmouseup=CaptureOnMouseUp;

//drag
function CaptureOnMouseUp(e){
	drag_isdrag=false;
	document.onmousemove=CaptureOnMouseMouve;
}


function movemouse(e)
{
  if (drag_isdrag)
  {
    dobj.style.left = moz ? tx + e.clientX - drag_x : tx + event.clientX - drag_x;
    dobj.style.top  = moz ? ty + e.clientY - drag_y : ty + event.clientY - drag_y;
    return false;
  }
}

function selectmouse(e)
{
  var reg=new RegExp("dragme","g");
  var reg1=new RegExp("draganchor","g");
  var fobj       = moz ? e.target : event.srcElement;
  var topelement = moz ? "HTML" : "BODY";
  
  if(!fobj.className.match(reg1))return;
  
  while (fobj.tagName != topelement && !fobj.className.match(reg))
  {
    fobj = moz ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className.match(reg))
  {
    drag_isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0,10);
    ty = parseInt(dobj.style.top+0,10);
    drag_x = moz ? e.clientX : event.clientX;
    drag_y = moz ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }
}

function CaptureOnload(e){
	try{
		document.body.scrollTop=scry;
		document.body.scrollLeft=scrx;
	} catch (ex){}
	try{
		if(act=="ajoutpanier")alert("Votre article a bien été ajouté à votre panier.");
	} catch (ex){}
	try{
		if(msgpage!="")alert(msgpage);
	} catch (ex){}

	creerbulle("bbulle");
	bulle_div=document.getElementById("bbulle");
	creerbulle("bpopup");
	creerbulle("bpopup2");
	
}


//Validation de champs
function formulaire_valide(frm){
	for(i=0;i<frm.elements.length;i++){
		var fld=frm.elements[i];
		if(fld.validrule=="")continue;
		switch(fld.validrule){
			case "numerique": 
				if(isNaN(fld.value)){
					alert("Le champs \""+fld.title+"\" doit être un nombre.");
					return false;
				}
			break;
			case "texte": 
				var reg=new RegExp("^[ ]*$","g");
				if(fld.value.match(reg)){
					alert("Le champs \""+fld.title+"\" doit être complété.");
					return false;
				}
			break;
			case "email": 
			 	var reg = new RegExp("^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$","g");
   				if(!fld.value.match(reg)){
					alert("Le champs \""+fld.title+"\" n'est pas un email valide.");
					return false;
				}
			break;
			case "mini6": 
				if(fld.value.length<6){
					alert("Le champs \""+fld.title+"\" doit comporter au moins 6 caractères.");
					return false;
				}
			break;
			case "identique": 
				if(fld.value!=frm.elements[i-1].value){
					alert("Le champs \""+fld.title+"\" doit doit être identique au champs précédent.");
					return false;
				}
			break;	
		}
	}
	return true;
}

//Panier
function ajouter_panier(code){
	var reg=new RegExp("[?&=]","g");
	ar_search=window.location.search.split(reg);
	new_search="?";
	for(i=0;i<ar_search.length;i=i+2){
		if(ar_search[i]!="code"&&ar_search[i]!="scrx"&&ar_search[i]!="scry")new_search+=ar_search[i]+"="+ar_search[i+1]+"&";
	}

	window.location.href=window.location.pathname+new_search+"act=ajoutpanier&code="+code+"&scrx="+document.body.scrollLeft+"&scry="+document.body.scrollTop;
}
function recalculer_panier(){
	tableaudata.action=window.location.pathname+"?act=recalculer_panier";
	tableaudata.submit();	
}

function supprimer_panier(k){
	window.location.href=window.location.pathname+"?act=supprimer_panier&k="+k;
}

//Bulles
function creerbulle(iid){
	var bb=document.createElement("DIV");
	bb.style.position="absolute";
	bb.style.left="0px";
	bb.style.top="0px";
	bb.style.zIndex="2000";
	bb.style.display="none";
	bb.id=iid;
	bb.innerHTML="";
	document.body.appendChild(bb);
}

function CaptureOnMouseMouve(e){
	/*tp: 	1 pointer
			2 popup
	*/
	try{
		if(!bulle_div)return;
		if(moz){
			var obj=e.target;
			//ev.returnValue=false;
		} else {
			var obj=event.srcElement;
			//event.returnValue=false;
		}
	
		if(	bulle_tp==2){		
			if(bulle_obj && bulle_obj!=obj){
				bulle_div.style.display="none";
				bulle_obj="";
				bulle_tp=0;	
			}
		} else if(bulle_tp==0){
			try {
				ib=obj.getAttribute("infobulle");
				ibstyle=obj.getAttribute("infobullestyle");
				if(ib==null){
          ib=obj.parentNode.getAttribute("infobulle");
          ibstyle=obj.parentNode.getAttribute("infobullestyle");
        }	
				if(ib==null){
          ib=obj.parentNode.parentNode.getAttribute("infobulle");
          ibstyle=obj.parentNode.parentNode.getAttribute("infobullestyle");
        }	
        		
				if(ib!=null){
					bulle_tp=2;	
					x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
					y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;

					if(moz){
						ws=window.innerWidth;
						hs=window.innerHeight;
					} else {
						ws=document.body.clientWidth;
						hs=document.body.clientHeight;
					}
					
					bulle_div.style.border="1px solid black";
					bulle_div.style.backgroundColor="#FFFFDD";
					bulle_div.style.Color="black";
					bulle_div.style.fontFamily="tahoma,arial,Geneva,Helvetica,sans-serif";
				  bulle_div.style.fontWeight="100";
				  bulle_div.style.padding="2px";
					bulle_div.style.fontSize="11px";
				  bulle_div.style.width="";
					//bulle_div.style.left=x+15;
					//bulle_div.style.top=y+15;
					bulle_div.innerHTML=ib;
					
					bulle_obj=obj;
					bulle_div.style.display="";
					
					if(x<=ws/2)	bulle_div.style.left=x+10;
					else bulle_div.style.left=x-10-bulle_div.offsetWidth;
				
					if(y<=hs/2)bulle_div.style.top=y+10;
					else bulle_div.style.top=y-10-bulle_div.offsetHeight;

          //application du style specifique de la bulle
          if(ibstyle){
          	var reg=new RegExp("[:;]","g");
            var ar_ibstyle=ibstyle.split(reg);
            var i;
            for(i=0;i<ar_ibstyle.length;i+=2){
              eval("bulle_div.style."+ar_ibstyle[i]+"=\""+ar_ibstyle[i+1]+"\";");
            }
          }

				}
			} catch(ex){}
		}
	} catch(ex){}
}

function CaptureOnContextMenu(e){	
	CaptureOnClick(e);
	bulle_tp=1;
	if(!bulle_div)return;	
	if(moz){
		var obj=e.target;
		e.returnValue=false;
	} else {
		var obj=event.srcElement;
		event.returnValue=false;
	}

	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
	
	if(moz){
		ws=window.innerWidth;
		hs=window.innerHeight;
	} else {
		ws=document.body.clientWidth;
		hs=document.body.clientHeight;
	}
	
	bulle_div.style.border="1px solid #9999FF";
	bulle_div.style.backgroundColor="white"; //"#E2E9EF";
	bulle_div.style.Color="black";
	bulle_div.style.fontSize="11px";
	bulle_div.style.fontFamily="tahoma,arial,Geneva,Helvetica,sans-serif";
	bulle_div.style.zIndex="2000";
	
	bulle_div.style.left=x+10;
	bulle_div.style.top=y+10;

	var txt="";
	//menu objet
	var fctm=obj.getAttribute("mcontexte");
	if(fctm!=null)txt+=eval(fctm+"(e)");

	if(txt!=""){
		bulle_div.innerHTML=txt;		
		bulle_div.style.display="";

		if(x<=ws/2)	bulle_div.style.left=x+10;
		else bulle_div.style.left=x-10-bulle_div.offsetWidth;
	
		if(y<=hs/2)bulle_div.style.top=y+10;
		else bulle_div.style.top=y-10-bulle_div.offsetHeight;

	} else bulle_tp=0;

	return false;
}

function CaptureOnClick(e){
	if(moz){
		var obj=e.target;
	} else {
		var obj=event.srcElement;
	}

	if(bulle_tp==3){
		bulle_tp=1;
		return;
	}

	dansbulle=0;
	try{
		if (obj==bulle_div)dansbulle=1;
		else if(obj.parentNode==bulle_div)dansbulle=1;
		else if(obj.parentNode.parentNode==bulle_div)dansbulle=1;
		else if(obj.parentNode.parentNode.parentNode==bulle_div)dansbulle=1;
	} catch (ex){}

 	if (dansbulle==0&&bulle_div){
		bulle_div.style.display="none";
		bulle_tp=0;	
	}

}

//Général
function showHide( targetName, imgObjName ) {
    var target;
    var imgObj;
    if( imgObjName ) { 
        imgObj = eval("document." + imgObjName); 
    }
    
    if( document.getElementById ) { // NS6+
        target = document.getElementById(targetName);
    } else if( document.all ) { // IE4+
        target = document.all[targetName];
    }
	
     // only attempt the show hide if a target is found, could be looking at user's other cookie name that doesn't fit as a node name
    if( target ) {
        // IE & NS6 like 'none'/'block', a value is needed for the cookie
        if( target.style.display == "none" ) {
            target.style.display = "block";
            //setCookie( targetName, "block" );
        } else {
            target.style.display = "none";
            //setCookie( targetName, "none" );
        }
    }

    if( imgObj ) {
        var imgPath = imgObj.src;
        if( imgPath ) {
            imgPath = imgPath.substring(0,imgPath.lastIndexOf("/")) + "/";
            if( imgObj.src == imgPath + "close.gif" ) {
                imgObj.src = imgPath + "open.gif";
            } else {
                imgObj.src = imgPath + "close.gif";
            }
        }
    }

} // showHide


//dimensions
function afficher_txt(txt){ /* Anti htmlspecialchars+nl2br*/
		txt=txt.replace(RegExp("(\&)","gi"),"&amp;");
		txt=txt.replace(RegExp("(\<)","gi"),"&lt;");
		txt=txt.replace(RegExp("(\>)","gi"),"&gt;");
		txt=txt.replace(RegExp("\n","gi"),"<br/>");
		return txt;
}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
return str;
}

//programme principal
if (moz) {
     HTMLElement.prototype.__defineGetter__("children", function() {
           var arr = new Array(), i = 0, l = this.childNodes.length;
           for ( i = 0; i < l; i++ ) {
               if ( this.childNodes[ i ].nodeType == 1 ) {
                    arr.push( this.childNodes[ i ] );
               }
           }
      return arr;
      });

      HTMLElement.prototype.__defineGetter__("firstChild", function() {
            var node = this.childNodes[ 0 ];
            while (node.nodeType != 1) node = node.nextSibling;
                 return node;
            });

     HTMLElement.prototype.__defineGetter__("lastChild", function() {
     var node = this.childNodes[ this.childNodes.length - 1 ];
     while (node.nodeType != 1) node = node.previousSibling;
           return node;
      });
	  
	// check for XPath implementation
	if( document.implementation.hasFeature("XPath", "3.0") )
	{
	   // prototying the XMLDocument
	   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	   {
		  if( !xNode ) { xNode = this; } 
		  var oNSResolver = this.createNSResolver(this.documentElement)
		  var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
					   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		  var aResult = [];
		  for( var i = 0; i < aItems.snapshotLength; i++)
		  {
			 aResult[i] =  aItems.snapshotItem(i);
		  }
		  return aResult;
	   }
	
	   // prototying the Element
	   Element.prototype.selectNodes = function(cXPathString)
	   {
		  if(this.ownerDocument.selectNodes)
		  {
			 return this.ownerDocument.selectNodes(cXPathString, this);
		  }
		  else{throw "For XML Elements Only";}
	   }
	   
	   // prototying the XMLDocument
	   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	   {
		  if( !xNode ) { xNode = this; } 
		  var xItems = this.selectNodes(cXPathString, xNode);
		  if( xItems.length > 0 )
		  {
			 return xItems[0];
		  }
		  else
		  {
			 return null;
		  }
	   }
	   

	   // prototying the Element
	   Element.prototype.selectSingleNode = function(cXPathString)
	   {	
		  if(this.ownerDocument.selectSingleNode)
		  {
			 return this.ownerDocument.selectSingleNode(cXPathString, this);
		  }
		  else{throw "For XML Elements Only";}
	   }	   
	}

} 
