/*

The script is created by  Mariyan Mladenov  http://slackrover.com  26 Fub 2006.

Short instructions :

To check for empty field use attribute empty.
The value of the attribute will appear in alert.

Example:
<input type="name" empty="First Name">.
if this field is empty will appear alert: Please check your First Name.

Other options:

attribute : empty - check for empty field.
attribute : email - check for valid email.
attribute : numeric - check for numeric value.
attribute : real - check for real number value.
attribute : checkit = check for checked checkbox.
attribute : length = check for min length of the string

*/



function checkform(form)
{

	var valid = true;

	var form_elements = form.elements.length;

	var warrning = "Моля попълнете:\n";

	for(m=0; m<form_elements; m++)
	{
		element = form.elements.item(m);

		// check for empty fields


		attribute = element.getAttribute('empty');
		if(element.disabled == false)
			if(attribute)
			{
				if(empty_field(element))
				{
					warrning = warrning +  attribute+'\n';
					valid = false;
				}
			}
		// check for wrong email    

		attribute = element.getAttribute('email');
		if(attribute)
			if(wrong_email(element))
			{
				warrning = warrning +  attribute+'\n';
				valid = false;
			}

		//check for numeric value

		attribute = element.getAttribute('numeric');
		if(attribute)
			if(!is_numeric(element,0))
			{
				warrning = warrning +  attribute+'\n';
				valid = false;
			}

		//check for real value

		attribute = element.getAttribute('real');
		if(attribute)
			if(!is_numeric(element,1))
			{
				warrning = warrning +  attribute+'\n';
				valid = false;
			}

		//check for a checked 
		attribute = element.getAttribute('checkit');

		if(attribute)
			if(element.disabled==false)
				if(!element.checked)
				{
					warrning = warrning +  attribute+'\n';
					valid = false;
				}

		//check for minimun length
		attribute = element.getAttribute('length');        
		if(attribute)
			if(!length(element,5))
			{
				warrning = warrning +  attribute+'\n';
				valid = false;
			}
	} // end for loop

	warrning = warrning.substring(0,warrning.length-1);
	warrning = warrning+'';

	if(!valid)  
		alert(warrning);



	return valid;
}

function empty_field(field)
{


	if((field.value == null)||(field.value.length == 0)||(field.value.charAt(0)==" "))
		return true;


	return false;
	
}


function length(field,len)
{

	if(field.value.length<len)
		return false;

	return true; 
	
}

function wrong_email(field)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (!filter.test(field.value)) 
		return true 

	return false;
} 

function is_numeric(field,type)
{
	var filter;
	if(type == 0)
		filter  = "0123456789";
	if(type == 1)
		filter  = "0123456789.-";

	if(empty_field(field))
		return false; 

	var ch;
	var count = 0;

	for(s=0; s<field.value.length; s++)
	{
		ch = field.value.charAt(s);
		if(ch == "-")
			count ++;
		if(filter.indexOf(ch)<0)
			return false;
	} 

	if(field.value.indexOf("-")>0)
		return false;

	if(count>1)
		return false; 

	return true;
	
}

function disable(way, name)
{
	for (i=0;  i<document.form1.length;    i++)
	{
		if (document.form1.elements.item(i).name == name)
		document.form1.elements.item(i).disabled=way;
	}
}

function switch_mode(element, name)
{	
	if(element.checked)
		for (i=0;  i<document.form1.length;    i++)
		{
			if (document.form1.elements.item(i).name == name)
			document.form1.elements.item(i).disabled=false;
		}
	else
		for (i=0;  i<document.form1.length;    i++)
		{
			if (document.form1.elements.item(i).name == name)
			document.form1.elements.item(i).disabled=true;
		}
}