//   *******************************
//   *                             *
//   *          Industry 3         *
//   *                             *
//   *          1 Mai 2003         *
//   *                             *
//   *******************************
//
//   Toute reproduction, même partielle,
//   est strictement interdite
//   sans autorisation préalable.
//
//	 (c) 1999-2006 - Eurojeu.com
//

function setfocus(elem){
	document.forms[0].elements[elem].focus();
}

function initArray(){
	this.length=initArray.arguments.length;
	for(var i=0;i < this.length;i++)
		this[i]=initArray.arguments[i];
}

function isDigit(num){
	return (parseInt(num,10) >= 0 && parseInt(num,10) <=9 );
}

function isSeparator(sep){
	// return ( sep == "/"  ||  sep == ":" || sep == "-" || sep == ".") ;
	return (sep == "/" );
}

function isDay(day){
	return (day >=1 && day <= 31);
}

function isMonth(month){
	return (month >= 1 && month <= 12) ;
}

function isYear(year){
	for (var i=0; i<year.length;i++)
		if (!isDigit (year.charAt(i)) )
			return false;

	return true;
}

function FirstCheck(dat){
	var count=0;
	for(var i=0;i < dat.length;i++){
		car=dat.charAt(i);
		if (isSeparator(car))
			count++;
		else if (!isDigit(car))
			return false;
	}
	return (count == 2);
}

function FurtherCheck(dat){
	pack1=new initArray(1,3,5,7,8,10,12);
	pack2=new initArray(4,6,9,11);

	day=GetDay(dat);
	month=GetMonth(dat);
	year=GetYear(dat);
	for (var i=0; i<pack1.length;i++){
		if (month == pack1[i] && day >31)
			return false;
		if (i<pack2.length && month==pack2[i] && day >30)
			return false;
	}
	return (month ==2 && (day >29 || (day == 29 && year%4 !=0)))?false:true;
}

function SepIndex(dat,index){
	for (var i=index;i<dat.length;i++){
		if (isSeparator(dat.charAt(i)))
			return i;
	}
	return -1;
}

function CheckDateElement(val,flag){
	return  (((flag == "M" && !isMonth(val)) ||
             (flag == "D" && !isDay(val)) ||
             (flag == "Y" && !isYear(val)))?false:true);
}

function GetDay(dat){
	return parseInt(GetDateElement(dat,"D"),10);
}

function GetMonth(dat){
	return parseInt(GetDateElement(dat,"M"),10);
}

function GetYear(dat){
	tmp=GetDateElement(dat,"Y");
	if (tmp == 0)
		return 2000;
	for (var i=0; i<tmp.length;i++)
		if (tmp.charAt(i) !="0")
			break;
	tmp1=tmp.toString();
	temp = parseInt(tmp1.substring(i,tmp.length),10);
	if ( tmp.length <=2 && temp >=0 && temp <=9 )
		temp="200"+temp;
	else if ( (temp >=10) && (temp <=49) )
		temp="20"+temp;
	else if ( (temp >= 50) && (temp <=99) )
		temp="19"+temp;

	return parseInt(temp,10);
}

function GetDateElement(dat,flag){
	Dformat="DMY";
	first=SepIndex(dat,0);
	second=SepIndex(dat,first+1);
	for(var i=0;i<3;i++){
		if (Dformat.charAt(i) == flag)
			break;
	}
	return (i==0?dat.substring(0,first):(i==1?dat.substring(first+1,second):(i==2?dat.substring(second+1,dat.length):-1)));
}

function isDate(elem){
	dat=document.forms[0].elements[elem].value;
	if (dat.length==0)
		return true;
	return checkDate(dat);
}

function checkDate(dat){
	if (!FirstCheck(dat))
		return false;
	Dformat="DMY";
	if ( (first=SepIndex(dat,0)) == -1)
		return false;
	if (!CheckDateElement(dat.substring(0,first),Dformat.substring(0,1)))
		return false;
	if ( (second=SepIndex(dat,first+1)) == -1)
		return false;
	if (!CheckDateElement(dat.substring(first+1,second),Dformat.substring(1,2)))
		return false;
	if (!CheckDateElement(dat.substring(second+1,dat.length),Dformat.substring(2,3)) )
		return false;

	return FurtherCheck(dat);
}

function compareDates(date1,date2){
	// returns <0, 0, >0 if less,equal or greater respectively
	y = GetYear(date1) -GetYear(date2);
	if (y)
		return y;
	m= GetMonth(date1)-GetMonth(date2);
	if (m)
		return m;
	return (GetDay(date1) - GetDay(date2));
}

function alertDate(elem){
	document.forms[0].elements[elem].value = trim(document.forms[0].elements[elem].value);

	if (!isDate(elem)){
		setfocus(elem);
		alert ("Date incorrecte : "+document.forms[0].elements[elem].value);
		return false;
	}
	return true;
}

function isNumber(elem){
	el =document.forms[0].elements[elem];
	if (el.value.length == 0)
		return true;
	for (var i=0; i<el.value.length; i++){
		if (!(isDigit(el.value.charAt(i)) ) )
			return false;
	}
	return true;
}

function alertNotNull(elem){
	if ((isNumber(elem)) && (document.forms[0].elements[elem].value <= 0)){
		setfocus(elem);
		alert (unescape("La valeur doit être non nulle : ")+ document.forms[0].elements[elem].value);
		return false;
	}
	return true;
}

function alertNumber(elem){
	if (!isNumber(elem)){
		setfocus(elem);
		alert (unescape("Num%E9ro incorrect : ")+ document.forms[0].elements[elem].value);
		return false;
	}
	return true;
}

function alertPresence(elem){
	document.forms[0].elements[elem].value = trim(document.forms[0].elements[elem].value);

	if (document.forms[0].elements[elem].value.length==0){
		setfocus(elem);
		alert(unescape("Champ obligatoire"));
		return false;
	}
	return true;
}

function societe(id) {
 	window.focus();
 	connectewin = window.open('./j_societepopup.php?idsoc='+id,'','width=600,height=450,resizable=yes,scrollbars=yes');
 	connectewin.focus();
}

function joueur(id) {
 	window.focus();
 	connectewin = window.open('./j_joueurpopup.php?id='+id,'','width=600,height=400,resizable=yes,scrollbars=yes');
 	connectewin.focus();
}

function trim(TRIM_VALUE) {
	if(TRIM_VALUE.length < 1)
		return "";

	TRIM_VALUE = rTrim(TRIM_VALUE);
	TRIM_VALUE = lTrim(TRIM_VALUE);

	if(TRIM_VALUE=="")
		return "";
	else
		return TRIM_VALUE;
}

function rTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
		return "";

	var iTemp = v_length - 1;

	while(iTemp > -1) {
		if (VALUE.charAt(iTemp) != w_space) {
			strTemp = VALUE.substring(0, iTemp + 1);
			break;
		}
		iTemp = iTemp-1;
	}

	return strTemp;
}

function lTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";

	if(v_length < 1)
		return "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) != w_space) {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}

	return strTemp;
}

