// forms //

function check_string_len_simple (string, min, max) {

  if (min != "") {
    if (string.value.length < min) return false;
  }

  if (max != "") {
    if (string.value.length > max) return false;
  }

  return true;
}

function check_string_len (string, min, max, msg) {
  if (!check_string_len_simple(string, min, max)) {
	alert(msg);
    return false;
  } else return true;
}

function check_num (string, min, max, msg) {
  var sValue = check_string_len (string, min, max, msg);
  if (sValue == true) {
    var filter = /^[0-9]+$/;
    if (filter.test(string.value)) {
	  sValue = true;
	} else {
	  alert(msg);
	  sValue = false;
	}
  }
  return sValue;
}

function check_pass (string1, string2, min, max, msg) {
  var sValue = check_string_len (string1, min, max, msg);
  if (sValue == true) {
    if (string1.value == string2.value) sValue = true; else {
	  alert(msg);
	  sValue = false;
	}
  }
  return sValue;
}

function check_select (string, msg) {
  if (string.options[string.selectedIndex].value == "") {
    alert(msg);
	return false;
  } else return true;
}

function check_email (string, msg) {
  var filter = /^.+@.+\..{2,4}$/;
  if (filter.test(string.value)) sValue = true;
  else {
	alert(msg);
    sValue = false;
  }
  return sValue;
}

function check_euro (string, msg) {
  var filter = /^[0-9]+(\.[0-9]{1,2})?$/;
  if (filter.test(string)) sValue = true;
  else {
	alert(msg);
    sValue = false;
  }
  return sValue;
}

function check_date (string, hms, msg) {

/*
  var filter = /^[0-9]{4}+\-[0-9]{2}+\-[0-9]{2})?$/;
  if (filter.test(string.value)) sValue = true;
  else {
	alert(msg);
    sValue = false;
  }
  return sValue;
*/

  // FONCTION A AMELIORER !!!

  if (hms == "Y")
	  num_char = 19;
  else
	  num_char = 10;


  if (!check_string_len (string, num_char, num_char, msg)) return false;
  else return true;

}

function check_radio(string, msg) {
  var sValue = false;
  for (var a=0; a < string.length; a++) {
    if (string[a].checked == true) sValue = true;
  }
  if (sValue == false) {
    alert(msg);
  }
  return sValue; 
}

function check_one_cbox (string, msg) {
  if (string == true) sValue = true;
  else {
    sValue = false;
	alert(msg);
  }
  return sValue;
}

function check_cbox (tabCbox, min, max, msg) {
  var cpt2 = 0;
  for (var cpt = 0; cpt < tabCbox.length; cpt++) {
    if (tabCbox[cpt] == true) cpt2++;
  }
  if ( (cpt2 < min) || (cpt2 > max) ) {
	alert(msg);
    sValue = false;
  } else sValue = true;
  return sValue;
}


function check_num_char(element_id, div_control_element_id, max_char, element_type)  {

// ----------------------------------------------------------------------------------
// -- this function print the number of characters in text or textarea in the form --
// ----------------------------------------------------------------------------------

  switch(element_type) {

// this part of this fonction perform and print the length of the form element remaining autorized
    case "TEXT" :

      StrLen = max_char - document.getElementById(element_id).value.length;
      if (StrLen < 0) {
        document.getElementById(element_id).value = document.getElementById(element_id).value.substring(0, max_char);
        CharsLeft = 0;
      } else {
        CharsLeft = StrLen;
      }
      break;

// this part of this fonction print the length of the form element

    case "TEXTAREA" :

      max_char; // don't used in this part of this fonction but avoid a definition error
      StrLen = document.getElementById(element_id).value.length;
      CharsLeft = StrLen;
      break;
  }

  document.getElementById(div_control_element_id).innerHTML = CharsLeft;

}


function resetAllText() {
  for (var cpt = 0; cpt < document.forms.length; cpt++) {
    for (var cpt2 = 0; cpt2 < document.forms[cpt].elements.length; cpt2++) {
      if (document.forms[cpt].elements[cpt2].type == "text")     document.forms[cpt].elements[cpt2].value = "";
      if (document.forms[cpt].elements[cpt2].type == "textarea") document.forms[cpt].elements[cpt2].value = "";
      // if (document.forms[cpt].elements[cpt2].type == "file")     document.forms[cpt].elements[cpt2].value = "";
    }
  }
}


// popup //

function popen (fileUrl, width, height, leftPos, topPos, redim, scroll) {
  window.open(fileUrl, "pop", "resizable=" + redim + ",left=" + leftPos + ",top=" + topPos + ",height=" + height + ",width=" + width + ",scrollbars=" + scroll);
}

function changeImg (imgName, imgFileName) {
  document.images[imgName].src = imgFileName;
}

function resizeImg(imgName, width, height) {
  document.images[imgName].width = width;
  document.images[imgName].height = height;
}



function fillElemForm(form1, form2, elemName, elemNum) {

//alert(document.forms[form1].elements[elemName+'1'].value);

  for (cpt = 1; cpt <= elemNum; cpt++) {
    document.forms[form2].elements[elemName+cpt].value = document.forms[form1].elements[elemName+cpt].value;
  }
  document.forms[form2].submit();

}

function confirm_and_go(url, msg) {
  
  if (window.confirm(msg) == true) {
    window.location = url;
  } 
}


function go(url) {
	top.location.href = url;
}





function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;
    var marked_row = new Array;
    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function





////////////////////


function init_imageOver() {
if(document.getElementById) {
  var j=0;
  var k=0;
  var l=0;
  for(var i=0;i<document.images.length;i++) {
   var image=document.images[i];
   if(!image.getAttribute("overSrc","false") && !image.getAttribute("clickSrc","false")) continue;
   eval("outImage"+j+"=new Image();");
   eval("outImage"+j+".src=image.src;");
   if(image.getAttribute("overSrc","false")) {
    eval("overImage"+k+"=new Image();");
    eval("overImage"+k+".src=image.getAttribute('overSrc',false);");
    image.onmouseover=new Function("this.src=overImage"+k+".src");
    k++;
   }
   if(image.getAttribute("clickSrc","false")) {
    eval("clickImage"+l+"=new Image();");
    eval("clickImage"+l+".src=image.getAttribute('clickSrc',false);");
    image.onmousedown=new Function("this.src=clickImage"+l+".src;");
    image.onmouseup=new Function("this.src=outImage"+j+".src;");
    l++;
   }
   image.onmouseout=new Function("this.src=outImage"+j+".src");
   j++;  
  }
}
}
onload=init_imageOver;





function chrono(div_id, today_year, today_month, today_day, today_hour, today_minute, today_second, year, month, day, hour, minute, second, end_text, style) {

	today_month = today_month - 1;

	if (document.getElementById && document.getElementById(div_id)) {

		//var now = new Date(today_year, today_month, today_day, today_hour, today_minute, today_second);
		var now = new Date();

		var goal = new Date(year,month-1,day,hour,minute,second);
		var ts = (goal.getTime() - now.getTime())/1000;

		if (ts > 0) {
			var j = Math.floor(ts/(3600*24));
			var h = Math.floor((ts-j*24*3600)/3600);
			var m = Math.floor((ts-j*24*3600-h*3600)/60);
			var s = Math.floor(ts-j*24*3600-h*3600-m*60);
			var html = ((j>1)?j+" jours ":(j>0)?j+" jour ":"")
			+ ((h<10)?" 0":" ")+h+"h "
			+ ((m<10)?"0":"")+m+"mn "
			+ ((s<10)?"0":"")+s+"s "

			document.getElementById(div_id).innerHTML = "<span class='"+style+"'>"+html+"</span>";
		} else document.getElementById(div_id).innerHTML = "<span class='"+style+"'>"+end_text+"</span>";
	}
}

/*
function ajax_get_datas(file_url) {

	var xhr=null;

    if (window.XMLHttpRequest)
        xhr = new XMLHttpRequest();
    else if (window.ActiveXObject) 
        xhr = new ActiveXObject("Microsoft.XMLHTTP");

    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() {

					if(xhr.readyState == 4 && xhr.status == 200){
						//alert(xhr.responseText);
					}


	};
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", file_url, true);

    xhr.send(null);

	return xhr;
}
*/




function ajax_get_xhr() {

	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) { // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}

		catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 

	return xhr;
}

function ajax_go(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function ajax_clean(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			ajax_go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			ajax_go(b);
	}
	return d;
} 


function ajax_display_datas(file_url, div_id, tag_name, tag_line, tag_column) {

	var xhr = ajax_get_xhr();

    xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {

			reponse = ajax_clean(xhr.responseXML.documentElement);

			document.getElementById(div_id).innerHTML = reponse.getElementsByTagName(tag_column)[tag_line].firstChild.nodeValue;

		}
	};

    //on appelle le fichier reponse.txt
    xhr.open("GET", file_url, true);

    xhr.send(null);

	var docXML= xhr.responseXML;

}



/*	rafraichissement de la page aprés x secondes (s)
	rafraichissement de la page aprés x minutes (m)
	rafraichissement de la page aprés x heures (h)*/
function refresh_page(refresh_delta_time,refresh_delta_type)
{
	var refresh_indice = refresh_delta_time;
	var refresh_type = refresh_delta_type;
	var indice;
	var dt = new Date();
    var hour =	dt.getHours();
	var minute = dt.getMinutes();
	var seconde = dt.getSeconds();
	//window.status=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds();
		
	if(refresh_type == "S" || refresh_type == "s")
	{  indice = seconde; }
	else
	{	if(refresh_type == "M" || refresh_type == "m")
		{  indice = minute; }
		else
		{  indice = hour; }	
	}
			
	if( refresh_indice == seconde)
	{ document.location.reload(); }
	
	window.setTimeout('refresh_page('+refresh_indice+')',1000);
}



//function displayDiv
//Fait apparaitre une div
function displayDiv(div)
{
	var nameDiv = div;
	var myDiv = document.getElementById(nameDiv);
	
	if(myDiv.style.display == 'none')
	{
		myDiv.style.display = 'block';
	}
	else
	{
		if(myDiv.style.display == 'block')
		{
			myDiv.style.display = 'none';
		}
	}
}

function show_hide_account_infos() {
	displayDiv("info_compte");
}

function show_login_subscribe_element() {
	show_mask_div();
	document.getElementById('div_login_subscribe').style.display='block';
	center_div_in_window('div_login_subscribe');
}

function hide_login_subscribe_element() {
	document.getElementById('div_login_subscribe').style.display='none';
	hide_mask_div();
}

function show_mask_div() {
	document.body.style.overflow='hidden';
	document.getElementById('mask').style.display='block';
}

function hide_mask_div() {
	document.body.style.overflow='';
	document.getElementById('mask').style.display='none';
}


function hide_special_info() {
	document.getElementById('div_special_info').style.display='none';
	hide_mask_div();
}

function show_validation(img) {
	show_mask_div();
	if (img != '')
		document.images["validation_img"].src = img;
	document.getElementById('div_global_validation').style.display = "block";
}


function get_body_width() {
	return document.body.clientWidth;
}

function get_body_height() {
	return document.body.clientHeight;
}




function center_div_in_window(div_id) {
	if (document.getElementById(div_id).style.width == '')
		div_width = document.getElementById(div_id).offsetWidth;
	else
		div_width = document.getElementById(div_id).style.width;

	x_pos = (get_body_width() - div_width) / 2;
	document.getElementById(div_id).style.left = x_pos+'px';
}
