/**
 * Action produite lors d'un clic sur un bouton
 * envoie le formulaire
 */
function submitFormulaire(form) {
	//var f = document.getElementBy Id(form);
	var f = document.forms[form];
	if (f != null) {
		f.submit();
	}
	return false;
}

/**
 * action produite lorsque une touche du clavier
 * est appuyé. Si touche 'entree' on soumet le formulaire
 */
function keypress(e,form) {

	e = (e) ? e : event ;

	if (e.keyCode) {
		key = e.keyCode;
	}
	else {
		key = e.which;
	}

	if (key == 13) {
		//Enter
		submitFormulaire(form);
	}
}

/**
 * affiche ou cache une div
 */
function show(id,display) {
	var myDiv = document.getElementById(id);
	if (myDiv) {
		if (myDiv.style.display == "none") {
			if(display) {
				myDiv.style.display = display;
			} else {
				myDiv.style.display = "inline";
			}
		} else {
			myDiv.style.display = "none";
		}
	} else {
		alert(id + ' not found');
	}
}


/**
 * action appelée lorsque on selectionne une
 * partie dans la liste des parties disponnibles
 */
function selctPartie(id){
	location.href="mygames.php?pid="+id;
}

/**
 *
 * @access public
 * @return void
 **/
function mel(adr1,adr2,dom,sub){
	window.location.href='m'+'ail'+'to:'+adr1+'@'+dom+';'+adr2+'@'+dom+'?sub'+'ject='+sub;
}

function show_stat(value,path,newWin) {

	var url = path+'stat_perso.php?val='+value;

	//on n'ouvre une nouvelle fenetre que si c'est demandé
	if (newWin == true) {
		openCenterWin(url,420,250);
	} else {
		window.location = url;
	}
}

function show_stat_machine(value,path,newWin) {

	var url = path+'stat_perso_machine.php?val='+value;

	//on n'ouvre une nouvelle fenetre que si c'est demandé
	if (newWin == true) {
		openCenterWin(url,620,300);
	} else {
		window.location = url;
	}
}

function getStats(targetId,parentId) {
	var parentList = window.opener.document.getElementById(parentId);
	var targetList = document.getElementById(targetId);
	if (parentList && targetList) {
	
		copy(parentList, targetList);
	} else {
		alert('ids not found');
	}
}

function copy(parentNode,targetNode) {
	for(i=0; i<parentNode.childNodes.length; i++){
		if(parentNode.childNodes[i].nodeName == "OPTION") {
			oOption = document.createElement('OPTION');
			targetNode.appendChild(oOption);
			oOption.text = parentNode.childNodes[i].text;
			oOption.value = parentNode.childNodes[i].value;
		} else if(parentNode.childNodes[i].nodeName == "OPTGROUP") {
			oOptgroup = document.createElement('OPTGROUP');
			oOptgroup.label = parentNode.childNodes[i].label;
			copy(parentNode.childNodes[i],oOptgroup);
			targetNode.appendChild(oOptgroup);
		}
	}
}

function addOptgroup(oSelect, oData)
{
  var returnValue = null;
  var i = 0;
  if(oSelect && oData)
  {
    oOptgroup = document.createElement('OPTGROUP');
    oOptgroup.label = oData[0];
    for(i = 0; i < oData[1].length; i++)
    {
      oOption = document.createElement('OPTION');
      oOptgroup.appendChild(oOption);
      oOption.text = oData[1][i][0];
      oOption.value = oData[1][i][1];
    }
    oSelect.appendChild(oOptgroup);
    returnValue = oOptgroup;
  }
  return returnValue;
}


/*
 * Ouvre la fenetre pour consulter ses messages privés
 */
function openWin(url, width, height){
	window.open(url,'','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,width=420,height=350,left=20,top=30');
}

function openCenterWin(url, width, height) {     

	var top = Math.round((screen.height-height)/2);
	var left = Math.round((screen.width-width)/2);

    var configuration="'toolbar=no, menubar=no, location=no, directories=no, status=no, resizeable=no, width="+width+", height="+height+", left=" + left + ", top=" + top; 
    window.open(url,'',configuration); 
}

/***********************************
 * calcul perso
 ***********************************/

function confirmSupprCalcPerso() {
	return confirm('Etes-vous sûr de vouloir supprimer ce calcul personnalisé ?');
}

/***********************************
 * boutons plus / moins
 ***********************************/
var isMouseDown = false;
var params = {};

function more(id,min,max,pas) {
	var input = document.getElementById(id);

	if (input != null) {
		var value = parseInt(input.value);
		if (isNaN(value)) {
			//ce n'est PAS un nombre !!
			input.value = min;
		} else {
			//c'est un nombre

			value += pas;
			if (value > max) {
				//on annule
				value = max;
			} else if (value < min) {
				//on annule
				value = min;
			}
			
			//on sauvegarde
			input.value = value;
		}
	}
	
	isMouseDown = true;
	params = {'id': id, 'min': min, 'max': max, 'pas': pas};
	
	setTimeout("mouseDownHandler()",200);
}
function less(id,min,max,pas) {
	more(id,min,max, - pas);
}

function mouseDownHandler() {
	if (isMouseDown) {
		more(params.id,params.min,params.max,params.pas);
	}
}


function mouseUp() {
	isMouseDown = false;
	params = [];
}


function displayAnchor(name) {
	//var anchor = document.anchors[name];
	//if (anchor != null) {
		window.location.hash=name;
	//}
}

function css(elem,className) {
	elem.className = className;
}

function go(elem) {
	elem.form.button.value = elem.name;
	return true;
}

function toggleSelect() {
	var selects = document.getElementsByTagName('SELECT');
	for(i=0; i<selects.length; i++) {
		selects[i].style.display = (selects[i].style.display == 'none') ? '' : 'none';
	}
}

/**
  * Inserts multiple fields.
  */
function insertValue(form) {
	var myCalc = form.calc;
	var myVars = form.vars;

	if(myVars.options.length > 0) {
		var chaineAj = "";
		var and = "";
		for(var i=0; i<myVars.options.length; i++) {
			if (myVars.options[i].selected){
				chaineAj += and + "{" + myVars.options[i].text + "}";
				and = " + ";
			}
		}

		//IE support
		if (document.selection) {
			myCalc.focus();
			sel = document.selection.createRange();
			sel.text = chaineAj;
			form.insert.focus();
		}
		//MOZILLA/NETSCAPE support
		else if (myCalc.selectionStart || myCalc.selectionStart == "0") {
			var startPos = myCalc.selectionStart;
			var endPos = myCalc.selectionEnd;
			var chaineSql = myCalc.value;
			myCalc.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
		} else {
			myCalc.value += chaineAj;
		}
    }
}

function showMenu(mid, smid) {
	var m = $(mid);
	var s = $(smid);
	
	var offsets = Position.positionedOffset(m);
    var top     = offsets[1];
    var left    = offsets[0];

	s.addClassName("over");
	s.style.left= (left-16)+"px";
	s.style.top= (top+25)+"px";
}
function hideMenu(mid, smid) {
	var m = document.getElementById(mid);
	var s = document.getElementById(smid);
	s.removeClassName("over");
}


var oldtab = null;
function changeTab_old(elt,divid) {
	Effect.divSwap(divid,'tabs');
	Element.addClassName(elt, 'here');
	Element.removeClassName(oldtab, 'here');
	oldtab = elt;
}
Effect.divSwap = function(element,container){
	var div = document.getElementById(container);
	var nodeList = div.childNodes;
	var queue = Effect.Queues.get('menuScope');
	if(queue.toArray().length<1){
		if(Element.visible(element)==false){
			for(i=0;i<nodeList.length;i++){
				if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
					if(Element.visible(nodeList.item(i))==true){
						Effect.SlideUp(nodeList.item(i),{duration: 0.5,queue:{position:'end',scope:'menuScope',limit:2}})
					}
				}
			}
			Effect.SlideDown(element,{duration: 0.5,queue:{position:'end',scope:'menuScope',limit:2}})
		}
	}
}

function flash(obj) {
	document.write(obj);
}

function displayFlash(swf, largeur, hauteur, alternatif, bgColor)
{
	document.write('<object type="application/x-shockwave-flash" data="'+swf+'" width="'+largeur+'" height="'+hauteur+'">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+swf+'" />');
	document.write('<param name="bgcolor" value="'+ bgColor +'">');
	document.write(alternatif);
	document.write('</object>');
}

function getGame(tr,pid,s) {

	var ajparent = jQuery('#ajparent');

	jQuery('.lstparties tr').hover(function(){
			jQuery(this).addClass("tabover");
		},
		function(){
			jQuery(this).removeClass("tabover");
		})
		.removeClass('tabover');
		
	jQuery(tr).hover(function(){},
		function(){
			jQuery(this).addClass("tabover");
		})
		.addClass('tabover');
		
	ajparent.height(ajparent.height()).addClass('hoverBtn').css('vertical-align','top');

	jQuery('#ajax').html('<div id="contentLoading" class="c" style=""><img src="../static/images/loading.gif" alt="Loading data, please wait..."/></div>')
		.load('mygames.game.php','zid='+pid, function (responseText, textStatus, XMLHttpRequest) {
			ajparent.removeClass('hoverBtn')
					.height('auto');
		});
	
	jQuery('.form_warn li').hide();
	jQuery('#'+s).show();

}
function postComment(form) {
	
	jQuery('#ajax').html('<div id="contentLoading" class="c" style=""><img src="../static/images/loading.gif" alt="Loading data, please wait..."/></div>');
	jQuery.ajax({
			type: form.method,
			url:  form.action,
			data: jQuery(form).serialize(),
			dataType: "html",
			success: function(msg){
				jQuery('#ajax').html(msg);
				return false;
			},
			error: function(request, msg, ex) {
				if (ex) {
					alert("Une erreur est survenue: " + ex.message);
				} else {
					alert("Une erreur est survenue: " + msg);
				}
				jQuery.event.trigger( "ajaxStop" );
				return false;
			}
		});
}

function mask(v) {
	/*var val = v.value;
	var len = val.length;
	var res = "";
	for (i = len; i>0; i-=3) {
		pos = (i - 3 > 0) ? i - 3 : 0;
		res = val.substring(pos,i) + " " + res;
	}
	v.value = res;*/
}
function unmask(v) {
	/*var val = v.value;
	var res = val.split(" ").join("");
	v.value = res;*/
}

function hideEvenement(msg) {
	if (confirm(msg)) {
		jQuery.ajax({
			type: "get",
			url:  "myaccount.php",
			data: "hideconseil=1",
			dataType: "html",
			error: function(request, msg, ex) {
				if (ex) {
					alert("Une erreur est survenue: " + ex.message);
				} else {
					alert("Une erreur est survenue: " + msg);
				}
				jQuery.event.trigger( "ajaxStop" );
				return false;
			}
		});
		jQuery(".evenement").hide();
	}
}