//--------------------------------------------------------------------
// THE DEBUGGER
function debug(x){
	document.getElementById('debug').innerHTML = x+"<br>"+document.getElementById('debug').innerHTML;
}
//--------------------------------------------------------------------
// use to have a cursor change on href
function func(){}
//--------------------------------------------------------------------
// simply reload the page.
function reload_page(){
	document.location = document.location.href;
}
//--------------------------------------------------------------------
// simply go to the desired page.
function go2page(page){
	document.location = page;
	
}
//--------------------------------------------------------------------
// simply go to the desired page.
function openBlankPage(page){
	window.open(page);
	
}
//--------------------------------------------------------------------
// simply go to the desired page.
function go2pageget(page, var_name, var_value){
	document.location = page+"?"+var_name+"="+var_value;
}
//--------------------------------------------------------------------
// simply return true or false if the var (send as STRING) is set or not.
function isset(varname){
	if(typeof( window[ varname ] ) != 'undefined'){
		return true;
	}else{
		return false;
	}
}
//--------------------------------------------------------------------
// cursor = pointer
function cursor_in(o){
	o.style.cursor = 'pointer';
}
//--------------------------------------------------------------------
// cursor = auto
function cursor_out(o){
	o.style.cursor = 'auto';
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// for the table tr list
function tr_over(tr){
	var l = tr.childNodes;
	for(x in l){
		td = l[x];
		if (td.nodeName=="TD"){
			td.style.backgroundImage="url('images/main/hi50.png')";
		}
	}
}
//--------------------------------------------------------------------
function tr_out(tr){
	var l = tr.childNodes;
	for(x in l){
		td = l[x];
		if (td.nodeName=="TD"){
			td.style.backgroundImage="none";
		}
	}
}
//--------------------------------------------------------------------
// emule the php trim function
function trim(str){
	return str.replace(/^\s+|\s+$/g, '') ;
}
//--------------------------------------------------------------------
// display a hidden onject
function f_display_obj(id){
	var o = document.getElementById(id);
	o.style.display = 'block';
}
//--------------------------------------------------------------------
// display a hidden onject
function f_hide_obj(id){
	var o = document.getElementById(id);
	o.style.display = 'none';
}
//--------------------------------------------------------------------
/*
display_popup()
PURPOSE 		:	open the popup above everything
CALLER			:	
VAR					:	openclose = 'open' / 'close'

HOW TO			:	
	the div = <div id="popup"></div>
						<div id="popup_mask"></div>
						In "popup" we  will put what will be displayed.
						"popup_mask" is the dark area.
	the css = 
	#popup{
	height:0px;
	width:100%;
	position:absolute;
	z-index:101;
	}
	#popup_mask{
	height:0%;
	width:100%;
	position:absolute;
	z-index:100;
	background:#000;
	opacity:0.8;
	filter:alpha(opacity=80);
	}
*/
function display_popup(openclose){
	
	if (openclose=="open"){
		var h = document.body.scrollHeight+"px";
	}else{
		var h = "0%";
	}
	document.getElementById('popup_mask').style.height = h;
	

	if (openclose=="close"){
		document.getElementById('popup').innerHTML = "";
	}
		
}


function scrollTop (){
	body=document.body
		d=document.documentElement
		if (body && body.scrollTop) return body.scrollTop
		if (d && d.scrollTop) return d.scrollTop
		if (window.pageYOffset) return window.pageYOffset
		return 0
}


function wait_ajax(){
	// nothing for the moment
	
	
	/*
	display_popup('open');
	var popup = document.getElementById('popup');
	popup.innerHTML = "<div style='width:500px; margin:auto; margin-top:300px;'><h1>Veuillez patienter</h1></div>";
	*/
}

// function from rico.
// return a list of all the elements with the desire tag name and class name
document.getElementsByTagAndClassName = function(tagName, className) {
  if ( tagName == null )
     tagName = '*';

  var children = document.getElementsByTagName(tagName) || document.all;
  var elements = new Array();

  if ( className == null )
    return children;

  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }

  return elements;
}


// change the display css of desired DOM objects
function f_display_list_tab(name, display){
//	alert(display);
	var lt = document.getElementsByTagAndClassName('td',  'list_tab_'+name);
	var display_value;
	if (display){ display_value = 'table-cell'; }else{ display_value = 'none'; }
	for(var x in lt){
		lt[x].style.display=display_value;
	}
	
}

// info on : http://www.quirksmode.org/dom/getElementsByTagNames.html
function getElementsByTagNames(list,obj) {
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}


//-------------------------------------------------------------------
//-------------------------------------------------------------------



// Verify the mail validity

function verif_mail(email){
		var arobase=email.indexOf("@");
		var point= email.lastIndexOf(".");
		if((arobase < 3)||(point + 2 > email.length) ||(point < arobase+3)){
			return false;
		}else{
			return true;
		}
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------


function isDateValid(saisie, year_on_2_numbers) {
	if (saisie == "") return false;
	saisie = (saisie).split("/");
	if ((saisie.length != 3) || isNaN(parseInt(saisie[0])) || isNaN(parseInt(saisie[1])) || isNaN(parseInt(saisie[2]))) return false;
	var laDate = new Date(eval(saisie[2]),eval(saisie[1])-1,eval(saisie[0]));
	if (year_on_2_numbers){
		var annee = saisie[2];
		
		return ((laDate.getDate() == eval(saisie[0])) && (laDate.getMonth() == eval(saisie[1])-1) && (annee.length == 2));
	}else{
		var annee = laDate.getYear();
		if ((Math.abs(annee)+"").length < 4) annee = annee + 1900;
		return ((laDate.getDate() == eval(saisie[0])) && (laDate.getMonth() == eval(saisie[1])-1) && (annee == eval(saisie[2])));
	}
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------
// secure some caracters
//-------------------------------------------------------------------
//-------------------------------------------------------------------
function secure_string_for_ajax(str){
	
	var reg=new RegExp("(&)", "g");
	str = str.replace(reg,'|#and#|');
	
	var reg=new RegExp("(\\+)", "g");
	str = str.replace(reg,'|#plus#|');

	// we deal with the special Word chars.
	var reg=new RegExp("(’)", "g");
	str = str.replace(reg,"'");
	
	var reg=new RegExp("(«)", "g");
	str = str.replace(reg,'"');
	
	var reg=new RegExp("(»)", "g");
	str = str.replace(reg,'"');
	
	return str;
}



//-------------------------------------------------------------------
//-------------------------------------------------------------------
// VP_SCAN :  how to act on DOM obj according to their class name
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//--------------------------------
function vp_button_hi(o){o.style.cursor="pointer"; o.className="button_hover";}
function vp_button_norm(o){o.style.cursor="auto"; o.className="button";}
//--------------------------------
function vp_button_reactions(o){
	o.onmouseover = function(){ vp_button_hi(o); }
	o.onmouseout = function(){ vp_button_norm(o); }
}
//--------------------------------
// Scan the whole body searching objects with class 'vp_button'
// and apply the function button_reaction to them
function vp_scan_page(o){
	var atmp;
	var c;
	var l = o.childNodes;
	var x;
	var y;
	for(x in l){
		if (l[x] && l[x].className != undefined){
			//debug( l[x].className+" / "+l[x].id);
			c = l[x].className;
			atmp = c.split(' ');
			for (y in atmp){
				if(atmp[y] == "button"){
					vp_button_reactions(l[x]);
				}
			}
			
			if (l[x].childNodes.length > 0) vp_scan_page(l[x]);
		}
	}
}
window.onload=function(){ vp_scan_page(document.body); }
