//--------------------------------------------------------------------
// 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 go2pageget(page, var_name, var_value){
	document.location = page+"?"+var_name+"="+var_value;
}
//--------------------------------------------------------------------
// simply go to the desired page.
function openBlankPage(page){
	window.open(page);
	}
//--------------------------------------------------------------------
// 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";
		}
	}
}
//--------------------------------------------------------------------
// Put a zero before a Number if it's < 10
function put_zero(num){
	if (num<10) num = "0"+num;
	return num;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// 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';
}
//--------------------------------------------------------------------

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); }


//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Open_popup
/*
							- $jumi[0] = the popup src file
							- $jumi[1] = the width of the iframe
							- $jumi[2] = the height of the iframe
							- $jumi[3] = "yes" OR "no" for the scrollbars
*/
//-------------------------------------------------------------------
//-------------------------------------------------------------------
function open_popup(url, title, width, height, toolbar, menubar, scrollbars, resizable){
	window.open (url, title, 'height='+height+', width='+width+', toolbar='+toolbar+', menubar='+menubar+', scrollbars='+scrollbars+', resizable='+resizable+', location=no, directories=no, status=no');
	
}



//-------------------------------------------------------------------
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//-------------------------------------------------------------------
function php_serialize(obj)
{
    var string = '';

    if (typeof(obj) == 'object') {
        if (obj instanceof Array) {
            string = 'a:';
            tmpstring = '';
            count = 0;
            for (var key in obj) {
                tmpstring += php_serialize(key);
                tmpstring += php_serialize(obj[key]);
                count++;
            }
            string += count + ':{';
            string += tmpstring;
            string += '}';
        } else if (obj instanceof Object) {
            classname = obj.toString();

            if (classname == '[object Object]') {
                classname = 'StdClass';
            }

            string = 'O:' + classname.length + ':"' + classname + '":';
            tmpstring = '';
            count = 0;
            for (var key in obj) {
                tmpstring += php_serialize(key);
                if (obj[key]) {
                    tmpstring += php_serialize(obj[key]);
                } else {
                    tmpstring += php_serialize('');
                }
                count++;
            }
            string += count + ':{' + tmpstring + '}';
        }
    } else {
        switch (typeof(obj)) {
            case 'number':
                if (obj - Math.floor(obj) != 0) {
                    string += 'd:' + obj + ';';
                } else {
                    string += 'i:' + obj + ';';
                }
                break;
            case 'string':
                string += 's:' + obj.length + ':"' + obj + '";';
                break;
            case 'boolean':
                if (obj) {
                    string += 'b:1;';
                } else {
                    string += 'b:0;';
                }
                break;
        }
    }

    return string;
}


