
/*
	PAVIT Validators (pValidators) v1.0
	2006—2007 © Pavel 'pasha' Vladimiroff
	http://www.pavit.ru
	Supported Browsers: IE 6.0+, Mozilla based, Opera
	NOT FOR COMERCICAL USE

	input params:
	type			- Type of validation
	control			- Control to validate
	control_compare		- Secondary control
	depend_value		-
	regexp_value		- ReqExp for validate
	validator		- Control for error showing
	popup_alert		- Show javascript alert if error
	error			- Error text

	output params:		false	- error
				true	- ok
*/

function CheckField(type, control, control_secondary, depend_value, regexp_value, popup_alert, error_text)
{
	try { var reg = new RegExp(regexp_value, "i"); } catch (e) {}
	var ctrl1 = document.getElementById(control);
	var ctrl2 = document.getElementById(control_secondary);
	var valid = document.getElementById('validator_' + control);
	var tmp = document.getElementById('tmp_' + control);

	switch (type)
	{
		case 'req':
			if (ctrl1.value == '') {
				if (popup_alert) {
					alert(error_text);
					ctrl1.focus();
				}
				else
					valid.innerHTML = error_text;
				tmp.value = 0;
				return false;
			}
			valid.innerHTML = '';
			tmp.value = 1;
			return true;
			break;
		case 'regexp':
			if (ctrl1.value != '' && !reg.test(ctrl1.value)) {
				if (popup_alert) {
					alert(error_text);
					ctrl1.focus();
				}
				else
					valid.innerHTML = error_text;
				tmp.value = 0;
				return false;
			}
			valid.innerHTML = '';
			tmp.value = 1;
			return true;
			break;
		case 'compare':
			if (ctrl1.value != ctrl2.value) {
				if (popup_alert) {
					alert(error_text);
					ctrl1.focus();
				}
				else
					valid.innerHTML = error_text;
				tmp.value = 0;
				return false;
			}
			valid.innerHTML = '';
			tmp.value = 1;
			return true;
			break;
		case 'depend':
			if (ctrl1.value == depend_value) {
				ctrl2.disabled = '';
			}
			else
				ctrl2.disabled = 'disabled';
			return true;
			break;
	}
}


// Auto field validation
//window.onload = InitAutoValidator;
function InitAutoValidator()
{
	var tags = ['INPUT','TEXTAREA', 'SELECT'];
	for(var tagCounter = 0; tagCounter < tags.length; tagCounter++)
	{
		var inputs = document.getElementsByTagName(tags[tagCounter]);
		for(var no = 0; no < inputs.length; no++)
		{
			if (document.getElementById('validator_function_' + inputs[no].id) != null)
				inputs[no].onblur = FeildValidation;
		}
	}
}
function FeildValidation()
{
	var func = document.getElementById('validator_function_' + this.id);
	if (func != null)
	try {
		eval (func.innerHTML);
	} catch (e) {}
}

// Submit form
function SubmitForm(formname, validatorname)
{
	var error = 0;

	var formname = document.getElementById(formname);
	var validatorname = document.getElementById(validatorname);

	if (formname == null || validatorname == null)
		return false;

	if (validatorname.childNodes.length <= 1)
		formname.submit();

	for (var i = 0; i < validatorname.childNodes.length; (i = i + 4))
	{
		var j = i;
		if (!isIE)
			j++;
		var tmpValid = validatorname.childNodes[j];
		if (tmpValid == null)
			continue;
		try {
			eval (tmpValid.innerHTML);
			if (validatorname.childNodes[j+2].value == 0)
				error++;
		}
		catch (e) { }
	}

	if (!error)
		formname.submit();
	return false;
}




/*
	PAVIT Get * functions (pGets) v1.0
	2006 © Pavel 'pasha' Vladimiroff
	http://www.pavit.ru
	Supported Browsers: IE 6.0+, Mozilla based, Opera
	NOT FOR COMERCICAL USE

	getElementsByClassName
	getElementsByAttributeName
	getAttributesByAttributeName

*/

// Get elements by class name
function getElementsByClassName(classname)
{
	var customcollection = new Array();
	var alltags = document.all ? document.all : document.getElementsByTagName("*");
	for (var i = 0; i < alltags.length; i++)
	{
		if (alltags[i].className == classname)
			customcollection.push(alltags[i]);
	}
	return customcollection;
}

// Get elements by attribute name
function getElementsByAttributeName(attributename)
{
	var customcollection = new Array();
	var alltags = document.all ? document.all : document.getElementsByTagName("*");
	for (var i = 0; i < alltags.length; i++)
	{
		if (alltags[i].getAttribute(attributename))
			customcollection.push(alltags[i]);
	}
	return customcollection;
}

// Get elements by attribute name
function getAttributesByAttributeName(attributename)
{
	var customcollection = new Array();
	var alltags = document.all ? document.all : document.getElementsByTagName("*");
	for (var i = 0; i < alltags.length; i++)
	{
		if (alltags[i].getAttribute(attributename))
			customcollection.push(alltags[i].getAttribute(attributename));
	}
	return customcollection;
}
