/*  The names of these functions are "c_x", where _x is the name of the 
	check defined in form_check.php 
*/

function c_is_not_partial_date(form_name, element_name, description) 
{
	if (stop_checking) return;
	
	var D_s = document.getElementById(element_name + '_day').value;
	var M_s = document.getElementById(element_name + '_month').value;
	var Y_s = document.getElementById(element_name + '_year').value;
	
	// If date is a required field, c_is_empty will have already
	// caught an empty date, month or day input box.
	if (D_s == '' && M_s == '' && Y_s == '') return;
	
	// Check if some part has not been filled in, while the other part(s) have
	if (D_s == '' || M_s == '' || Y_s == '') 
	{
		alert(w_date(description));
		stop_checking=true;
		return;
	}

  	var D = parseInt(D_s, 10);
	var M = parseInt(M_s, 10);
	var Y = parseInt(Y_s, 10);
	
    if (!(D > 0 &&
        (D <= [, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M] ||
        D == 29 && M == 2 && Y % 4 == 0 && (Y % 100 > 0 || Y % 400 == 0))))
	{
		alert(w_date(description));
		stop_checking=true;
	}
}


function c_is_not_partial_dt(form_name, element_name, description)
{
	if (stop_checking) return;
	
	var day = eval('document.' + form_name + '.' + element_name + '_day');
	var month = eval('document.' + form_name + '.' + element_name + '_month');
	var year = eval('document.' + form_name + '.' + element_name + '_year');
	var hour = eval('document.' + form_name + '.' + element_name + '_hour');
	var minutes = eval('document.' + form_name + '.' + element_name + '_minutes');

	// If something has been filled in, make sure both time and date
	// have been filled in
	
	all_empty =		trim(day.value) == ''
				&&	trim(month.value) == ''
				&&	trim(year.value) == ''
				&&	trim(hour.value) == ''
				&&	trim(minutes.value) == ''
				;
	
	if (	!all_empty
		&&	(		trim(day.value) == ''
				||	trim(month.value) == ''
				||	trim(year.value) == ''
				||	trim(hour.value) == ''
				||	trim(minutes.value) == ''
			)
	   )
	{
		alert(w_date(description));
		
		if (trim(day.value) == '') day.focus();
		else if (trim(month.value) == '') month.focus();
		else if (trim(year.value) == '') year.focus();
		else if (trim(hour.value) == '') hour.focus();
		else if (trim(minutes.value) == '') minutes.focus();
		
		stop_checking = true;
	}
}


function c_is_hour(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);

	n = parseInt(e.value, 10); // 2nd argument because of leading zero-problems
	
	if (n < 0 || n > 23)
	{
		alert(w_date(description));
		e.focus();
		stop_checking = true;
	}
}

function c_is_minutes(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);

	n = parseInt(e.value, 10); // 2nd argument because of leading zero-problems
	
	if (n < 0 || n > 59)
	{
		alert(w_date(description));
		e.focus();
		stop_checking = true;
	}
}

function c_is_day(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);
	
	n = parseInt(e.value, 10); // 2nd argument because of leading zero-problems
	
	if (n < 1 || n > 31)
	{
		alert(w_date(description));
		e.focus();
		stop_checking = true;
	}
}

function c_is_month(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);

	n = parseInt(e.value, 10); // 2nd argument because of leading zero-problems
	
	if (n < 1 || n > 12)
	{
		alert(w_date(description));
		e.focus();
		stop_checking = true;
	}
}

function c_is_not_partial_time(form_name, element_name, description)
{
	if (stop_checking) return;
	
	hour = eval('document.' + form_name + '.' + element_name + '_hour');
	minutes = eval('document.' + form_name + '.' + element_name + '_minutes');

	// If something has been filled in, make sure both hour and minutes
	// have been filled in
	
	if (!(trim(hour.value) == '' && trim(minutes.value) == '')
		&& (trim(hour.value) == '' || trim(minutes.value) == '')
	   )
	{
		alert(w_date(description));
		
		if (trim(hour.value) == '') hour.focus();
		else if (trim(minutes.value) == '') minutes.focus();
		
		stop_checking = true;
	}
}

function c_is_year(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);

	n = parseInt(e.value);
	
	if (n < 1900 || n > 2100)
	{
		alert(w_date(description));
		e.focus();
		stop_checking = true;
	}
}

function c_is_empty(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);
	
	if (e.value.length == 0)
	{
		alert(w_empty(description));
		e.focus();
		stop_checking = true;
	}
}

function c_number(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);
	
	if (e && isNaN(e.value))
	{
		alert(w_nav(description));
		e.focus();
		stop_checking = true;
	}
}

function c_rightname(form_name, element_name, description)
{
	if (stop_checking) return;
	
	e = eval('document.' + form_name + '.' + element_name);
	
	re = /^[A-Z_]+$/
	if (!re.test(e.value))
	{
		alert(w_rightname(description));
		e.focus();
		stop_checking = true;
	}
}

function c_upl_empty(form_name, element_name, description)
{
	c_is_empty(form_name, element_name, description);
}

function trim(s)
{
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
		s = s.substring(1,s.length);
		
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
		s = s.substring(0,s.length - 1);
	
	return s;
}