//---------------------------------------------
// Scripts helper
// Copyright C-Logic 2002
//
// Pascal Gagnebin
// Case postale
// 2720 Tramelan
//
// www.c-logic.ch
// jscripts@c-logic.ch
//
// Reproduction autorisée avec mention
// de la source


//----------------------------
// Change le titre de la page
//
function init()
//-------------
{ 
	top.document.title = document.title;
}

//--------------------------------------------
// Ouvre une fenêtre de popup avec une image
//
function popup(image, width, height, c1, c2)
//------------------------------------------
{
  var url = "../../includes/php/photo.php?image="+image+"&c1="+c1+"&c2="+c2;
	var features = "width= "+(width+40)+" height= "+(height+80)+", left=0,top=0," +
			"scrollbars=no,resizable=yes,location=no,directories=no,status=no,menubar=no";
	win = open(url, 'image', features);
	win.focus();	
}


function testeEMail(adr)
//----------------------
{
	adr = String(adr)					
	
	if (!adr.length)
		return false;
	
	var taille = adr.length ;
	var pos = -1 ;
	var i ;
	
	for (i=0 ; i < taille ; i++)
		if (adr.substr(i, 1) == '@')
			pos = i ;
	
	if (pos == -1)
		return false ;
	
	var pos2 = pos ;
	pos = -1 ;
	
	for (i=pos ; i<taille ; i++)
	{
		if (adr.substr(i, 1) == '.')
			pos = i ;
	}
			
	if (pos == -1)
		return false ;
		
	if (pos == taille -1 )
		return false ;
	
	return true ;
}

function FormatDate(aDate)
//------------------------
{
	ActualDate = new Date();
	ActualYear = new String(ActualDate.getYear());
	ActualYear < 1900 ? ActualYear = "19" + ActualYear : ActualYear;
	Millenium = new String(ActualYear.charAt(0));
	Century   = new String(ActualYear.charAt(1));	

	if (aDate != "")
  {
		Datum = aDate.split(".")
		if(Datum.length == 3)
		{
			if(Datum[0].length == 1) {
				Datum[0] = "0" + Datum[0];
			}
			if(Datum[1].length == 1) {
				Datum[1] = "0" + Datum[1];
			}
			if(!Datum[2]) {
				Datum[2] = ActualYear;
			}
			else if(Datum[2] < 50)
			{
				if(Datum[2].length <= 3)
				{
					Datum[2] = Millenium + Century + "0" + Math.abs(Datum[2]);
				}
			}
			else if(Datum[2] >= 50 && Datum[2] < 100)
			{
				if (Datum[2].length <= 3)
					Datum[2] = "19" + Math.abs(Datum[2]);
			}
			else if(Datum[2] >= 100 && Datum[2] < 1000)
			{
				if(Datum[2].length == 3)
					Datum[2] = "0" + Math.abs(Datum[2]);
			}
			aDate = String(Datum[0]).substring(0, 2)+"."+String(Datum[1]).substring(0, 2)+"."+String(Datum[2]).substring(0, 4);
		}
		else {
			aDate ="";
		}
	}

	return aDate;
}

function ValidateDate(aDate)
//--------------------------
{
	var retval = true;
	var leap;

	if(aDate != "")
	{
		Datum = aDate.split(".")
		if(Datum.length == 3)
		{
			leap = Datum[2] % 4 == 0 && Datum[2] % 100 != 0 || Datum[2] % 400 == 0;

			if(Datum[2] < 1)
			  Datum[2] = 2003;

			// Controle le nombre de mois
			if (Datum[1] < 1)
			  Datum[1] = 1;
			else if (Datum[1] > 12)
			  Datum[1] = 12;
			// Controle la longueur des mois
			if (Datum[0]<1)
			  Datum[0] = 1;
				
			if (Datum[1]==4 || Datum[1]==6 || Datum[1]==9 || Datum[1]==11)
			{
				if (Datum[0]>30)
				  Datum[0] = 30;
			}
			else if (Datum[1]==2)
			{
				if (leap && Datum[0]>29)
				  Datum[0] = 29;
				else if (!leap && Datum[0] > 28)
				  Datum[0] = 28;
			}
			else
			{
				if(Datum[0]>31)
				  Datum[0] = 31;
			}
		}
		aDate = String(Datum[0]).substring(0, 2)+"."+String(Datum[1]).substring(0, 2)+"."+String(Datum[2]).substring(0, 4);		
	}
	
  return aDate;
}

function ControleDate(input_object)
//---------------------------------
{
	var retval = true;

	if (input_object.value != "")
	{
		var tempDate = FormatDate(input_object.value);
		if(tempDate != "")
		{
			input_object.value = ValidateDate(tempDate);
		}
		else
			retval = false;
	}
	return retval;
}

