/**
 ** @project Epitech: techweb
 ** @authors Julien AMAR <amar_u@epitech.net>
 ** @authors Salah AZZOUG <azzoug@gmail.com>
 ** @authors Johanna BERDUGO <berdug_j@epitech.net>
 ** @authors Benoit CALVEZ <calvez_b@epitech.net>
 ** @authors Djamila MEHANNI <mehann_d@epitech.net>
 ** @authors John THIRIET <thirie_j@epitech.net>
 **/
/*********************************************************
** @ Lib validation formulaires
*********************************************************/
function clear(obj)
{
	document.getElementById(obj).className = "input_good";
}

function clear_list(obj)
{
	document.getElementById(obj).className = "input_good_list";
}

function IsValidDate(year, month, day)
{
	var	begin;

	begin = document.getElementById(year).value;
	if (document.getElementById(month).value < 10)
		begin = begin+"-0"+document.getElementById(month).value;
	else
		begin = begin+"-"+document.getElementById(month).value;

	if (document.getElementById(day).value < 10)
		begin = begin+"-0"+document.getElementById(day).value;
	else
		begin = begin+"-"+document.getElementById(day).value;
	if (IsDate(begin))
	{
		document.getElementById(day).className= "input_bad";
		document.getElementById(month).className = "input_bad";
		document.getElementById(year).className= "input_bad";
		return (1);
	}
	document.getElementById(day).className= "input_good";
	document.getElementById(month).className = "input_good";
	document.getElementById(year).className= "input_good";
	return (0);
}

/*
	Check date
*/
 function check_date(d) {
   e = new RegExp("^([0-9]{4}\-[0-9]{2}\-[0-9]{2})$");
   if (!e.test(d))
       return 0;
   a = parseInt(d.split("-")[0], 10);
   m = parseInt(d.split("-")[1], 10);
   j = parseInt(d.split("-")[2], 10);
   if (a < 1000)
   {
		if (a < 89)
			a += 2000;
		else
			a += 1900;
   }
   if (!(a % 4) && (a % 100) || !(a % 400))
     fev = 29;
   else
	fev = 28;
   nbJours = new Array(31, fev, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   return ((m >= 1 && m <=12 && j >= 1 && j <= nbJours[m - 1]));
 }

function IsDate(str)
{
	if (!check_date(str))
		return (1);
	return (0);
}

/*
	Check Nbr
*/
 function IsPositive(nombre) {
   if (nombre < '0' || nombre != /^[0-9]+$/)
       return (1);
   return (0);
}

function IsNbr(obj)
{
	var nombre = document.getElementById(obj).value;
	
    if(isNaN(nombre))
	{
		document.getElementById(obj).className = "input_bad";
		return (1);
	}
	return (0);
}

/*
	Check Email
*/
function IsEmail(obj) 
{ 
	var	email = document.getElementById(obj).value;
	var verif = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;

	if (verif.exec(email) == null)
	{
		document.getElementById(obj).className = "input_bad";
		return (1);
	}
	return (0);
}


/*
	Check Same
*/
function IsSame(obj1, obj2)
{
	var	pass1 = document.getElementById(obj1).value;
	var	pass2 = document.getElementById(obj2).value;

	if (pass1 != pass2)
	{
		document.getElementById(obj1).className = "input_bad";
		document.getElementById(obj2).className = "input_bad";
		return (1);
	}
	return (0);
}

/*
	Check Empty String
*/
function IsEmpty(obj)
{
	if (document.getElementById(obj).value == '')
	{
		document.getElementById(obj).className = "input_bad";
		return (1);
	}
	return (0);
}

function IsEmptyList(obj, lst)
{
	if (obj == '')
	{
		document.getElementById(lst).className = "input_bad_list";
		return (1);
	}
	return (0);
}

function add_line_to_text(obj)
{
	txt = document.getElementById(obj);
	txt.className = "module_text_memo_on";
	txt.rows = txt.rows + 2;
	txt.focus();
}
