// base64.js v 0.05


function encode64(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4 = "";
	var i = 0;
	do {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) enc3 = enc4 = 64;
		else if (isNaN(chr3)) enc4 = 64;
		output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);
	return output;
}


function decode64(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4 = "";
	var i = 0;
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	do {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output = output + String.fromCharCode(chr1);
		if (enc3 != 64) output = output + String.fromCharCode(chr2);
		if (enc4 != 64) output = output + String.fromCharCode(chr3);
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);
	return output;
}


var debugMode = 0;

function log(txt) {
	var i = 0,
	z;
	if (!(debugMode&1))
		return;
	do {
		if (typeof console != "undefined" && console.log && ! console.emul){
			console.log.apply(console, arguments);
			return;
		}else {
			var dbg = document.getElementById('debug');
			if (!dbg)
				return;
			var div = document.createElement('div');
			if (typeof txt != "string") {
				/*if (typeof txt == "object")
				txt = Object.keys(txt);
				else*/
				//txt = Object.inspect(txt);
			}
			div.appendChild(document.createTextNode(txt));
			z = String((this.___dbg_cnt++) & 1 << 2);
			div.style.backgroundColor = "#" + z + z + z;
			dbg.insertBefore(div, dbg.firstChild);
		}
		txt = arguments[++i];
	}
	while (txt!==undefined);
}

//Permet d'agrandir les textarea
function big(id,that){
  that=that.firstChild;
  var e=document.getElementById(id);
  if(e.style.width!="800px"){
    e.style.width="800px";
    e.style.height="600px";
    e.style.margin="5px";
    that.src="img/-.gif";
  }
  else{
    e.style.width="";
    e.style.height="";
    that.src="img/+.gif";
  }
}
function is_ie(){
  if(navigator.appName.indexOf("Microsoft")!=-1)return true;
  return false;
}
function toggle(id){
  //console.log(id);
  var i=document.getElementById(id);
  if(!i){
    alert(id+"n'existe pas!");
    return false;
  }
  if(i.style.display=="" || i.style.display=="block"){
    hide(id);
  }
  else{
    show(id);
  }
  return true;
}
var locked=false;

function show(id,callback,param){

  if(locked){return false;}
  locked=true;



  var div2show=document.getElementById(id);
  if(!div2show){
    locked=false;
    return false;
  }
  if(is_ie()){
      div2show.style.display="";
      locked=false;
      if(callback!=undefined){
        if(param!=undefined){
          callback.call(this,param);
        }
        else{

          callback.call(this);
        }
      }
      return true;
    }

  if(navigator.platform){
      if(navigator.platform.indexOf("Linux")!=-1){
          if(div2show){
            div2show.style.display="";
            locked=false;
            if(callback!=undefined){
              if(param!=undefined){
                callback.call(this,param);
              }
              else{

                callback.call(this);
              }
            }
          }
          return false;
      }
  }
  //document.title=id;
  if(div2show){
      var func=callback;


      function appear(){
        if(div2show.style.opacity!="1"){
          div2show.style.opacity=(parseInt(String(div2show.style.opacity).replace("0.",""))+1)/10;
          setTimeout(appear,50);
        }
        else{

          if(callback!=undefined){
            if(param!=undefined){
              callback.call(this,param);
            }
            else{
              callback.call(this);
            }
          }
          locked=false;
        }
      }
      if(div2show.style.display=='none'){
        div2show.style.opacity=0;
        div2show.style.display="";
        appear();
      }
      return true;
  }
  return false;
}

function hide(id,callback,param){
  if(locked){return false;}
  locked=true;
  var div2show=document.getElementById(id);
  if(!div2show){
    locked=false;
    return false;
  }

  if(is_ie()){
      div2show.style.display="none";
      locked=false;
      if(callback!=undefined){
        if(param!=undefined){
          callback.call(this,param);
        }
        else{
          callback.call(this);
        }
      }
      return true;
  }
  if(navigator.platform){
      if(navigator.platform.indexOf("Linux")!=-1){
          if(div2show){
            div2show.style.display="none";
            if(callback!=undefined){
              if(param!=undefined){
                callback.call(this,param);
              }
              else{
                callback.call(this);
              }
            }
          }
          locked=false;
          return false;
      }

  }
  function disappear(){


    if(div2show.style.opacity>=0.1){
      div2show.style.opacity=(parseInt(div2show.style.opacity.replace("0.",""))-1)/10;
      setTimeout(disappear,50);
      return false;
    }
    else{
      div2show.style.display='none';
      div2show.style.opacity="";
      locked=false;
      if(callback!=undefined){
        if(param!=undefined){
          callback.call(this,param);
        }
        else{
          callback.call(this);
        }
      }
      return true;
    }
  }

  if(div2show.style.display==''){
    div2show.style.opacity=0.9;
    disappear();
  }
  return true;
}
function load(page, param, target_id,fct) {
  if (window.all && !window.opera){      //Plus de bug ;-)
      var XhrObj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else{
      var XhrObj = new XMLHttpRequest();
  }
  console.log(target_id);
  var target = document.getElementById(target_id);
  if(target!=undefined){
  	target.innerHTML="<div id='waitDiv' style='width:200px;margin:auto;position:absolte;padding:15px;border:1px solid #1B5790;background:#B2D281;color:#1B5790;text-align:center;'><img src='/img/loading2.gif' /><br/>Veuillez patienter!</div>";
  }
  XhrObj.open("POST", page, true);
  XhrObj.onreadystatechange = function() {
    if (XhrObj.readyState == 4 && XhrObj.status == 200){
        if(target!=undefined){
          target.innerHTML = XhrObj.responseText;
        }
        if(fct!=undefined){
          fct.call(XhrObj.responseText);
        }
    }
    if(target!=undefined){
        var scripts = target.getElementsByTagName("script")
        for (var i = 0; i < scripts.length; ++i) {
           var s = scripts[i];
           if (!s.text) {
               dump(s);
               dump("len =" + scripts.length + "   i=" + i);
           }
           if (s.text) {
               eval(s.text);
           }

        }
    }
  }
  XhrObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  XhrObj.send("ajax=1&"+param);
  return false;
}
function verifyForm(){
	//document.title="verifyForm";
	var notEmpty=new Array("name","email","password","url");
	var n=notEmpty.length;

	for(var i=0;i<n;i++){
		var f=document.getElementsByName(notEmpty[i]);
		if(f[0]){
			f=f[0];
			if(f.value=="" || f.value==" " || f.value=="AUCUN"){
				f.style.backgroundColor="#fee";
				f.focus();
				f.onclick=f.onkeypress=function(){
					f.style.backgroundColor="#fff";
				}
				return false;
			}
		}
	}
}

function deleteItem(e){
	var a;
	//a = e.parentNode;
	a=e;
	if(confirm("Etes vous sûr de vouloir supprimer l'élément ?")){//'
		$.post(a.href,{},function(){
			location.reload();
			//hide(a.parentNode.id);
		});
	}
	return false;
}
function loadRespSearch(c){
	document.getElementById("pagination").style.display="none";
	var target=document.getElementsByTagName("table")[0];
	var callback=function(){
		target.removeAttribute("id");
	}
	if(target){
		target.id="target_id";
		load("/"+c+"/search","ajax=1&field="+document.getElementById("field_"+c).value+"&operator="+document.getElementById("operator_"+c).value+"&value="+document.getElementsByName(document.getElementById("field_"+c).value)[0].value+"&order="+document.getElementById("order_"+c).value+"&sens="+document.getElementById("sens_"+c).value,target.id,callback);
	}
	return false;
}

function formsend(){
	var mess = document.getElementById("mymess");

}
//Ligne en surbrillance
function setPointer(theRow, thePointerColor)
{
if(typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined'){return false;}
var row_cells_cnt= theRow.cells.length;
for(var c = 0; c < row_cells_cnt; c++){theRow.cells[c].bgColor = thePointerColor;}
return true;
}
function callBIns(){
		//console.log(this);	
		if(this==0){
			document.getElementById("insAjax").innerHTML="<span class='important'>Le compte n'a pu être créé!</span>";	
		}
		else{
			document.getElementById("insAjax").innerHTML="<span class='important'>"+this+"</span>";	
			//document.location="/index";
		}
}
function callCIns(){
		//console.log(this);	
		if(this==0){
			document.getElementById("passAjax").innerHTML="<span class='important'>L'adresse email n'a pas été reconnue</span>";	
		}
		else{
			document.getElementById("passAjax").innerHTML="<span class='important'>"+this+"</span>";	
			//document.location="/index";
		}
}
function affvisites (nbvisites) {
	document.getElementById("headbox-nbvisites").style.display = "block";
	document.getElementById("headbox-nbvisites").getElementsByTagName("span")[0].firstChild.data = nbvisites;
}
function hidevisites () {
document.getElementById("headbox-nbvisites").style.display = "none";
}

function hidediv(div) {
	if (document.getElementById) { 
	document.getElementById(div).style.display = 'none';
	}
}

function showdiv(div) {
	if (document.getElementById) {
	document.getElementById(div).style.display = 'block';
	}
} 

function limitChars(textarea, limit, infodiv){
var text = textarea.value; 
var textlength = text.length;
var info = document.getElementById(infodiv);

if(textlength > limit){
info.innerHTML = 'You cannot write more then '+limit+' characters!';
textarea.value = text.substr(0,limit);
return false;
}
else{
info.innerHTML = 'You have '+ (limit - textlength) +' characters left.';
return true;
}
}

$().ready(function() {	
	// validate forms
	$("#signup").validate();
	$("#modification").validate();
	$("#promo").validate();
	$("#inscription").validate({
		rules: {
			password: {
				required: true,
				minlength: 5
			}
		}
	});
});
