/*************************
 * FONCTIONS JAVASCRIPT
*************************/

var selected;
var imgs = new Array();
var nombre_total_images;

function selectRowEffect(object, buttonSelect, mon_form) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
 // if (document.forms[mon_form].address[0]) {
//    document.forms[mon_form].address[buttonSelect].checked=true;
//  } else {
//    document.forms[mon_form].address.checked=true;
//  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function montre(id) {
	var d = document.getElementById(id);
		for (var i = 1; i<=10; i++) {
			if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
		}
	if (d) {d.style.display='block';}
}

function changeUrl( formulaire )
{
	location.href = formulaire.ListeURL.options[formulaire.ListeURL.selectedIndex].value;
}

function popup_news( url, width, height, nom )
{
	var marg = 30;
	var width = ( width ? width + marg : 800 + marg );
	var height = ( height ? height + marg : 600 + marg );
	var nom = ( nom ? nom : 'popup' );
	window.open ( url, nom, config='toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width='+width+', height='+height+', left=50, top=50' );
}

function popup(img, width, height, titre) {
	var marg = 10;
	var width = ( width ? width + marg : 800 + marg );
	var height = ( height ? height + marg : 600 + marg );
		w = open("",'image','width='+width+',height='+height+',toolbar=no,scrollbars=no,resizable=no');	
		w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>");
		/*w.document.write("<SCRIPT language=javascript>function checksize()  { if (document.images[0].complete) {  window.resizeTo(document.images[0].width+12,document.images[0].height+30); window.focus();} else { setTimeout('check()',250) } }</"+"SCRIPT>");*/
		/*onload='checksize()' */
		w.document.write("<BODY onClick='window.close()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><IMG src='"+img+"' width='"+width+"' height='"+height+"' alt='"+titre+"' border=0>");
		w.document.write("");
		w.document.write("</BODY></HTML>");
		w.document.close();
}

function FitPic() {
       iWidth = document.body.clientWidth;
       iHeight = document.body.clientHeight;
       iWidth = document.images[0].width - iWidth;
       iHeight = document.images[0].height - iHeight;
       window.resizeBy(iWidth, iHeight);
       self.focus();
}

function hide_block( admin, val, val1, val2, val3 )
{
	isIE = ( document.all );
	isNN6 = ( !isIE ) && ( document.getElementById );
	
	if (isIE)
	{
		if ( val1 ) hide1 = document.all['hide_' + val1];
		if ( val2 ) hide2 = document.all['hide_' + val2];
		if ( val3 ) hide3 = document.all['hide_' + val2];
	}
	else if (isNN6)
	{
		if ( val1 ) hide1 = document.getElementById( 'hide_' + val1 );
		if ( val2 ) hide2 = document.getElementById( 'hide_' + val2 );
		if ( val3 ) hide3 = document.getElementById( 'hide_' + val3 );
	}
	
	if ( hide1.style.display == "none" )
	{
		if ( val1 ) hide1.style.display = "";
		if ( val2 ) hide2.style.display = "none";
		if ( val3 ) hide3.style.display = "none";
		//if (admin) document.images['puce_' + val].src='img/moins.gif';
	}
	else
	{
		if ( val1 ) hide1.style.display = "none";
		//if (admin) document.images['puce_' + val].src='img/plus.gif';
	}
}

function insert_block( text, formulaire, block )
{
	var caretPos = null;
	var txtarea  = document.forms[formulaire].elements[block];
	text = '' + text + '';
	
	if ( txtarea.selectionEnd >= 0 && txtarea.setSelectionRange )
	{
		caretPos = ( txtarea.selectionEnd + text.length );
		var before = txtarea.value.substring( 0, txtarea.selectionStart );
		var after = txtarea.value.substring( txtarea.selectionEnd, txtarea.textLength );
		txtarea.value = before + text + after;
		txtarea.setSelectionRange( caretPos, caretPos );
	}
	else if ( txtarea.createTextRange && txtarea.caretPos ) {
		caretPos = txtarea.caretPos;
		caretPos.text = ( caretPos.text.charAt(caretPos.text.length - 1) == '' ? caretPos.text + text + '' : caretPos.text + text );
	}
	else
	{
		txtarea.value += text;
	}
	txtarea.focus();
}

function clicMenu( num )
{
	isIE = ( document.all )
	isNN6 = ( !isIE ) && ( document.getElementById )
	if ( isIE ) menu = document.all['menu' + num];
	if ( isNN6 ) menu = document.getElementById( 'menu' + num );
	
	if ( menu.style.display == 'none' )
	{
		menu.style.display = '';
		//document.images['image'+num].src = 'img/moins.gif';
	}
	else
	{
		menu.style.display = 'none';
		//document.images['image'+num].src = 'img/plus.gif';
	}
}

// ajout d'image au block
function add_tag( element_, val )
{
	var element = $(element_);
	
	element.value += val;
}

function insert_menu( menu_, element_ )
{
	var menu = $(menu_);
	var element = $(element_);
	var i = menu.selectedIndex;
	
	element.value += " {" + menu.options[i].value + "} ";
}

function changeVal( element_, mod, calc )
{
	var element = $(element_);
	var val = Number( element.value );
	
	if ( mod == "more" )
	{
		val += 1;
	}
	else if ( mod == "less" )
	{
		val -= 1;
	}
	
	element.value = val;
	
	if ( calc )
	{
		$( 'j_' + calc ).value = ( Number( $F( 'g_' + calc ) ) + Number( $F( 'n_' + calc ) ) + Number( $F( 'p_' + calc ) ) );
		$( 'dif_' + calc ).value = ( Number( $F( 'bp_' + calc ) ) - Number( $F( 'bc_' + calc ) ) );
	}
}

function setSndTrack(mp3_file, div_id) {
	document.getElementById('mediaplayer_'+div_id).innerHTML='<EMBED SRC="./medias/'+mp3_file+'" HIDDEN=false AUTOSTART=true LOOP=false>';
}

function addPhoto(instext, formulaire, champ) {
	//var mess = document.concerts.crt_descr;
	var mess = document.forms[formulaire].elements[champ];
	//IE support
	if (document.selection) {
		mess.focus();
		sel = document.selection.createRange();
		sel.text = instext;
		//document.concerts.focus();
		mess.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (mess.selectionStart || mess.selectionStart == '0') {
		var startPos = mess.selectionStart;
		var endPos = mess.selectionEnd;
		var chaine = mess.value;

		mess.value = chaine.substring(0, startPos) + instext + chaine.substring(endPos, chaine.length);

		mess.selectionStart = startPos + instext.length;
		mess.selectionEnd = endPos + instext.length;
		mess.focus();
	} else {
		mess.value += instext;
		mess.focus();
	}
}

var ns6=document.getElementById&&!document.all;

function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength)
		return false;
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ ;
		if (pressedkey.test(String.fromCharCode(e.which)))
			e.stopPropagation();
	}
}

function countlimit(maxlength,e,placeholder){
	var theform=eval(placeholder);
	var lengthleft=maxlength-theform.value.length;
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder);
	if (window.event||e.target&&e.target==eval(placeholder)){
		if (lengthleft<0)
			theform.value=theform.value.substring(0,maxlength);
		placeholderobj.innerHTML=lengthleft;
	}
}

function displaylimit(thename, theid, thelimit){
	var theform=(theid!=""? document.getElementById(theid) : thename)
	var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> caractères maximum.';
	if (document.all||ns6)
		document.write(limit_text);
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)};
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)};
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

function remplitTabImages(photo, indice)
{
	imgs[indice] = photo;
}
