
/**
*	There is collections functions that use for checking fields form before submit.
*
*   @author Denis Golovtsov <golden@daxx.ru>
*   @copyright Daxx Web Factory SPb 2003
*   @package Checking JS-functions
*/

/**
*	Collections of often used regular expression.
*/

var r_chars_digits = /^[a-zA-Z0-9]+$/;
var r_digits = /^[0-9]+$/;
var r_postcode = /^[0-9]{4} ?[a-z]{2}$/i;
var r_email = /^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$/i;
var r_website = /^http:\/\/.+/;
var r_float = /^([0-9]+|([0-9]*\.[0-9]+))$/;
var r_one_word = /^[^\s]+$/;

/**
*	Time interval JS-API functions.
*/

// There is error messages.
var msg_error = new Array();
msg_error[0] = "Sorry, your date is incorrect";
msg_error[1] = "Please, select Start date earlier then End date";

function _check_date(day, month, year)
{
	var date_check = new Date(year, month-1, day);

	if (day != date_check.getDate() || month != (date_check.getMonth()+1) 
			|| year != date_check.getFullYear())
	{
		return msg_error[0];
	}
	return true;
}

function _check_interval(st_day, st_month, st_year, fn_day, fn_month, fn_year)
{
	if ((ret_res = _check_date(st_day, st_month, st_year)) != true)
	{
		return ret_res;
	}
	if ((ret_res = _check_date(fn_day, fn_month, fn_year)) != true)
	{
		return ret_res;
	}
	var date_st = new Date(st_year, st_month-1, st_day);
	var date_fn = new Date(fn_year, fn_month-1, fn_day);
	if (date_st.getTime() >= date_fn.getTime())
	{
		return msg_error[1];
	}
	return true;
}

/**
*	Other checking functions.
*/

function _check_radio_group(form, group_name)
{
	if (!form && document.forms[0])
	{
		form = document.forms[0];
	}

	var flg_checked = false;
	for ( var i=0; i < form.elements[group_name].length; i++ )
	{
		if ( form.elements[group_name][i].checked == true )
		{
			flg_checked = true;
			break;
		}
	}
	if ( !flg_checked )
	{
		return false;
	}
	return true;
}

function _value_radio_group(form, group_name)
{
	if (!form && document.forms[0])
	{
		form = document.forms[0];
	}

	for ( var i=0; i < form.elements[group_name].length; i++ )
	{
		if ( form.elements[group_name][i].checked == true )
		{
			return form.elements[group_name][i].value;
		}
	}
	return false;
}

function _enable_disable_elements(form, action)
{
	if (!form && document.forms[0])
	{
		form = document.forms[0];
	}

	if (arguments.length < 3)
	{
		return false;
	}

	for (var i=2; i<arguments.length; i++)
	{
		form.elements[arguments[i]].disabled = action;
	}
}

function _enable_disable_radio_group(form, group_name, action)
{
	if (!form && document.forms[0])
	{
		form = document.forms[0];
	}

	for (var i=0; i < form.elements[group_name].length; i++)
	{
		form.elements[group_name][i].disabled = action;
	}
}

function _enable_disable_group(form, group, action)
{
	if (!form && document.forms[0])
	{
		form = document.forms[0];
	}
	if (typeof(group) == "string")
	{
		var r_name = new RegExp("^"+group+"_.+$","i");

		for (var i=0; i<form.elements.length; i++)
		{
			if (r_name.exec(form.elements[i].name) != null)
			{
				form.elements[i].disabled = action;
			}
		}
	}
	else if (typeof(group) == 'object')
	{
		for (var i=0; i<group.length; i++)
		{
			form.elements[i].disabled = action;
		}
	}
}

function htmlspecialchars(str)
{
	str = str.replace('&','&amp;');
	str = str.replace('"','&quot;');
	str = str.replace('<','&lt;');
	str = str.replace('>','&gt;');
	return str;
}

function in_array(arr, el)
{
	if (typeof(arr) != 'object' || arr.length < 1)
	{
		return false;
	}
	var arr_str = "," + arr.toString() + ",";
	return (arr_str.search(","+el+",") != -1?true:false);
}

function array_remove(arr, el)
{
	var arr_str = "," + arr.toString() + ",";
	arr_str = arr_str.replace(","+el+",",",");
	arr_str = arr_str.replace(/^,/,"");
	arr_str = arr_str.replace(/,$/,"");
	var res = arr_str.split(",");
	var empty_arr = new Array();
	return (res.length == 1 && res[0] == ''?empty_arr:res);
}

/**
*	There is analog of PHP's trim() function.
*	This function removed spaces symbols start & end string
*/

function trim (str)
{
	var s_spaces = /^\s*(.*)$/;
	var e_spaces = /(.*[^\s])(\s*)$/;

	str = str.replace(s_spaces,"$1");
	str = str.replace(e_spaces,"$1");
	return str;
}