function cleanInput(obj, valueorigin, type) {
  if ( obj.value == valueorigin) {
    obj.value = ''
    if (type) {
      var pw = document.createElement ("input");
      pw.setAttribute("type", "password" );
      pw.setAttribute("value", obj.value);
      pw.setAttribute("name", obj.name);
      pw.setAttribute("id", obj.name);
      pw.className=obj.className;
      
      pw.setAttribute("onclick",function(){cleanInput(this,""+valueorigin+"", this.type)})
      pw.onclick=function(){cleanInput(this,""+valueorigin+"", this.type)}
      
      pw.setAttribute("onblur",function(){restoreInput(this,""+valueorigin+"", this.type)})
      pw.onblur=function(){restoreInput(this,""+valueorigin+"", this.type)}

      obj.parentNode.replaceChild (pw, obj);
      pw.focus()
      pw.focus()
    }
  }
}

function restoreInput(obj, valueorigin, type) {
  if (obj.value == '') { 
    obj.value = valueorigin
    if (type) {
      var pw = document.createElement ("input");
      pw.setAttribute("type", "text" );
      pw.setAttribute("value", obj.value);
      pw.setAttribute("name", obj.name);
      pw.setAttribute("id", obj.name);
      pw.className=obj.className;
      
      pw.setAttribute("onclick",function(){cleanInput(this,""+valueorigin+"", this.type)})
      pw.onclick=function(){cleanInput(this,""+valueorigin+"", this.type)}
      
      pw.setAttribute("onblur",function(){restoreInput(this,""+valueorigin+"", this.type)})
      pw.onblur=function(){restoreInput(this,""+valueorigin+"", this.type)}
      
      obj.parentNode.replaceChild (pw, obj);
    }
  }
}


// Function for form submit
var http_request = false;
var http_request2 = false;

function makePOSTRequest(url, parameters) {
  if (arguments[2])
     dst = arguments[2];
  else 
     dst = 'myspan'
  
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
      // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
       if (dst == "myspan")
        windowCenter('myspan');
        //alert(http_request.responseText);
        result = http_request.responseText;
        document.getElementById(dst).style.display = '';
        setInnerHTML(document.getElementById(dst), '')
        setInnerHTML(document.getElementById(dst), result)
        //document.getElementById(dst).innerHTML = '';
        //document.getElementById(dst).innerHTML = result;
     } else {
        alert('There was a problem with the request.');
     }
     http_request = false;
  }
}


function get(obj, dst) {
  opt = "&"
  
  if (arguments[2]) 
    pId=arguments[2];
  else 
    pId="";

  
  if (pId)
    if (document.getElementById('select'+pId))
      var opt = "&" + document.getElementById('select'+pId).name + "=" + document.getElementById('select'+pId).value + "&";
  else {
    var boxes = document.getElementsByTagName('select'+pId);
    for (var i = 0, tEl; tEl = boxes[i]; i++) {
      opt = opt + tEl.name + "=" + encodeURI( tEl.value ) + "&"
    }
  }
  

  var poststr = "products_id=" + encodeURI( document.getElementById("products_id"+pId).value ) + opt;
  //alert(poststr);
  makePOSTRequest(dst, poststr);
}

function getFromList(dst, id) {
  //alert(id);
  var poststr = "products_id=" + id;
  makePOSTRequest(dst, poststr);
}



function getFromCoffValid(url, id, dst) {
  poststr = id;
  makePOSTRequest(url, poststr, dst);
}


function getFromCoffProd(url, id, dst) {
  poststr = id;
  if (arguments[3])
    eval('makePOSTRequest_prod_'+arguments[3]+'(url, poststr, dst)');
  //makePOSTRequest(url, poststr, dst);
  
}
function windowCenter(myId) {
	result=returnSize();
	var myWidth=result[0];
	var myHeight=result[1];
	var elementWidth=document.getElementById(myId).offsetWidth;
	var elementHeight=document.getElementById(myId).offsetHeight;
  var myScrollLeft=document.documentElement.scrollLeft;
	var myScrollTop=document.documentElement.scrollTop;
	var posX=myScrollLeft+myWidth-myWidth/2-elementWidth/2
	var posY=myScrollTop+myHeight-myHeight/2-elementHeight/2;
	document.getElementById(myId).style.left=posX+"px";
	document.getElementById(myId).style.top=posY+"px";
}

function returnSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var result = new Array(myWidth,myHeight);
  return result;
}

function toggleAll() {

        var toggler = document.getElementById('toggler');

        var boxes = document.getElementsByTagName('input');
        for (var i = 0, tEl; tEl = boxes[i]; i++) {
                if ( tEl.type == "checkbox") {
                        tEl.checked = toggler.checked;
                }
        }

}

var cm=null;
document.onclick = new Function("show(null)")
function getPos(el,sProp) {
	var iPos = 0
	while (el!=null) {
		iPos+=el["offset" + sProp]
		el = el.offsetParent
	}
	return iPos

}

function show(el,m) {
	if (m) {
		
    if (m.style.left == '0px') {
		m.style.left = getPos(el,"Left")+"px"
    }
    if (m.style.top  == '0px') {
		m.style.top = getPos(el,"Top") + el.offsetHeight +"px"
    }
    m.style.display='';
	}
	//if ((m!=cm) && (cm)) cm.style.display='none'
	cm=m
}



  // SetInnerHTML Sécurisé
    function setInnerHTML(divContent, HTML) {
      divContent.innerHTML=HTML; 
      var All=divContent.getElementsByTagName("*");
      for (var i=0; i<All.length; i++) {
        All[i].id=All[i].getAttribute("id")
        All[i].name=All[i].getAttribute("name")
        //All[i].className=All[i].getAttribute("class")
      }
      var AllScripts=divContent.getElementsByTagName("script")
      for (var i=0; i<AllScripts.length; i++) {
         var s=AllScripts[i];
         if (s.src && s.src!="") {
            // Précédement asynchrone, mis en synchrone pour éviter des problèmes de dépendances de scripts
            eval(getFileContent(s.src))
         }
         else {
            eval(s.innerHTML)
         }
      }
    }
 
    // Renvoie le texte de l'objet ActiveXObject le plus récent depuis une liste
    var pickRecentProgID = function (idList){
	    // found progID flag
        var bFound = false;
        for(var i=0; i < idList.length && !bFound; i++){
            try{
                var oDoc = new ActiveXObject(idList[i]);
                o2Store = idList[i];
                bFound = true;
            }catch (objException){
                // trap; try next progID
            };
        };
        if (!bFound)
		    throw ("Aucun ActiveXObject n'est valide sur votre ordinateur, pensez à mettre à jour votre navigateur");
        idList = null;
        return o2Store;
    }
 
    // Retourne un nouvel objet XmlHttpRequest
    var GetXmlHttpRequest_AXO=null
    var GetXmlHttpRequest=function () {
	    if (window.XMLHttpRequest) {
		    return new XMLHttpRequest()
	    }
	    else if (window.ActiveXObject) {
		    if (!GetXmlHttpRequest_AXO) {
			    GetXmlHttpRequest_AXO=pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
		    }
		    return new ActiveXObject(GetXmlHttpRequest_AXO)
	    }
	    return false;
    }
 
    function getFileContent(url) {
       var Xhr=GetXmlHttpRequest();
       Xhr.open("GET",url,false);
       Xhr.send(null);
       return Xhr.responseText;
    }
    
    function updatePrice(){
      document.getElementById('total').value = 0;
        for (var i = 1; i <= 6 ; i++) {
          pID = document.getElementById('product_'+i).value;
          //alert(document.getElementById('total').value);
          if (pID)
            document.getElementById('total').value = parseFloat(document.getElementById('price_'+i).value) + parseFloat(document.getElementById('total').value);
        }
        
        document.getElementById('totalPrint').innerHTML = document.getElementById('total').value+"&euro;";
        //alert(document.getElementById('totalPrint').innerHTML);
    }
    
    
  function select_innerHTML(objeto,innerHTML){
  objeto.innerHTML = ""
  var selTemp = document.createElement("micoxselect")
  var opt;
  selTemp.id="micoxselect1"
  document.body.appendChild(selTemp)
  selTemp = document.getElementById("micoxselect1")
  selTemp.style.display="none"
  //if(innerHTML.toLowerCase().indexOf("<option")<0){//se não é option eu converto
  if(innerHTML.indexOf("<OPTION")<0){//se não é option eu converto
    innerHTML = "<OPTION>" + innerHTML + "</option>"
  }
  //innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span")
  innerHTML = innerHTML.replace(/<OPTION/g,"<span").replace(/<\/OPTION/g,"</span")
  selTemp.innerHTML = innerHTML
  for(var i=0;i<selTemp.childNodes.length;i++){
    if (selTemp.childNodes[i].nodeName != "SPAN") {
      if (selTemp.childNodes[i].nodeName == "SCRIPT") {
        eval(selTemp.childNodes[i].innerHTML);
      }
      continue;
    }
    
    var spantemp = selTemp.childNodes[i];
    
    if(spantemp.tagName){     
    opt = document.createElement("OPTION")
    
    if(document.all){ //IE
    objeto.add(opt)
    }else{
    objeto.appendChild(opt)
    }       
    
    //getting attributes
    for(var j=0; j<spantemp.attributes.length ; j++){
      var attrName = spantemp.attributes[j].nodeName;
      var attrVal = spantemp.attributes[j].nodeValue;
      if(attrVal){
        try{
          opt.setAttribute(attrName,attrVal);
          opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
        }catch(e){}
      }
    }
    //getting styles
    if(spantemp.style){
      for(var y in spantemp.style){
        try{opt.style[y] = spantemp.style[y];}catch(e){}
        }
      }
      //value and text
      opt.value = spantemp.getAttribute("value")
      opt.text = spantemp.innerHTML
      //IE
      if (spantemp.getAttribute('selected') == '')
        opt.selected = true;
      //opt.className = spantemp.className;
    } 
  }
  document.body.removeChild(selTemp)
  selTemp = null
  }
