//Make a list with the fields we don't want to check, by id
var nocheck = new Array(
	//"commentos"
);

//Specify the id of the form we're working with.
//If working with separate forms in a single page is needed,
//then we can ignore this variable and rewrite the validate();
//function to either allow it to be passed the id of the
//form directly, or have it get the form by itself with the
//object "this", whichever works.
//var formtoget = "contacto";

//Regular expressions for e-mail, phone and number

var email_re = /^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
var phone_re = /^(\(\d{1,3}\)\s*)?(\d{2,3}(-?|\s?))?\d{3,4}(-?|\s?)\d{4}$/;
var num_re = /^\d+$/;
var name_re = /^([a-z'\x7f-\xff-]+\.?\s?)*$/i;

var extra_re = /(bautizo|xv)/i;

//Set the error messages to display. Translate as necessary
var email_error = ": E-mail inválido.";
var name_error = ": Sólo letras en este campo.";
var phone_error = ": Número telefónico inválido.";
var num_error = ": Sólo números en este campo.";
var blank_error = ": Campo vacío.";

//The main function
function validate(formtoget)
{

	//First, reset all other counters
	allGood = 1;
	elements = 0;
	errorelement = 0;
	errorlog = 0;
	errorfound = 0;
	errorcount = 0;
	firstError = 0;

	//Variables to be used with error-reporting
	errorlog = new Array();
	errorelement = new Array();
	errorcount = 0;

	//Get our list of elements
	elements = getElements(formtoget);

	//First check that each field is filled out
	/*allGood = check_empty(elements) ? 1 : 0;
	if(allGood == 0)
	{
		alert(blank_error);
		//firstError.focus();
	}*/

	//Check each field that requires a certain type of data,
	//and assign the result to a variable

	//Uncomment to enable checking. First parameter is
	//the index of the element in the array we generated.
	//In other words, ignore anything other than a text field
	//when determining the index of the element.

	//NOTE: Disabling name check because there is no regex that
	//matches characters with diacritics, and restricting the
	//allowed characters to [a-zA-Z] is just too much hassle.

	allGood = (check_name(0) && allGood) ? 1 : 0;
	allGood = (check_email(1) && allGood) ? 1 : 0;
	allGood = (check_phone(2) && allGood) ? 1 : 0;

//	var optionlist = document.getElementById("model_list");
	var optionlist = document.getElementById("modelo");
	var checked = optionlist.value.search(extra_re);

	if(checked == -1){
		allGood = (check_name(3) && allGood) ? 1 : 0;
		allGood = (check_email(4) && allGood) ? 1 : 0;
		allGood = (check_phone(5) && allGood) ? 1 : 0;
	}

	allGood = (check_num(6) && allGood) ? 1 : 0;


	//If all is in order, submit the form
	if(allGood == 1)
	{
		theform.submit();
	}
	else
	{
		//If something's wrong, alert the user with each invalid value
		if(errorlog.length > 0) alert(errorlog.join("\n"));
		//Then loop through all the elements that caused errors
		for(i=0;i<errorelement.length;i++)
		{
			//And highlight them
			errorelement[i].style["border"] = "1px solid #f00";
		}
		return null;
	}
	return true;
}

function getElements(form)
{
	//Get the form element to work with
	theform = document.getElementById(form);
	theelements = theform.elements;

	fields = new Array();

	//Populate the fields array with the form's data in text fields only
	//This array holds each text field in the order they appear in the page,
	//storing it as the element itself, its id, its value and its name
	for(i=0,j=0;i<theelements.length;i++)
	{
		current = theelements[i];
		tagname = current.tagName.toLowerCase();
		if((tagname != "input") && (tagname != "textarea")) continue;
		fields[j] = new Array(
			current,
			current.getAttribute("id"),
			current.value,
			current.getAttribute("name")
		);
		j++;
	}
	return fields;
}

//Auxiliary function for checking if an array contains a certain element
//Returns the index of the element if found, or -1 if not found
//Stupid JavaScript for not having an isin(); function already.
function inArray(val,arr)
{
	for(i=0;i<arr.length;i++)
	{
		if(arr[i] == val)
		{
			return i;
		}
	}
	return -1;
}

function check_empty(i)
{
	
	var current = elements[i];
	alert(current+"in "+i);
//	if(i==0) return 1;
	current[0].style["border"] = "1px solid #000";
	if(current[2]=="")
	{
		errorlog[errorcount] = current[3] + blank_error;
		errorelement[errorcount] = current[0];
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		errorcount++;
		return 0;
	}
	return 1;
}

//Function that checks whether the value of fields[i][2] is an e-mail
//Arguments are the index of the element in the textfield array,
//and the array itself. Further testing will determine if
//passing the array is really necessary. Ideally, it shouldn't be.
function check_email(i,empty)
{
	empty = (empty == null) ? 1 : empty;
	var current = elements[i];
	current[0].style["border"] = "1px solid #000";
	if(empty==1 && current[2]=="")
	{
		errorlog[errorcount] = current[3] + blank_error;
		errorelement[errorcount] = current[0];
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		errorcount++;
		return 0;
	}
	else if(current[2].search(email_re) == -1)
	{
		errorlog[errorcount] = current[3] + email_error;
		errorelement[errorcount] = current[0];
		errorcount++;
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		return 0;
	}
	return 1;
}

function check_name(i,empty)
{
	empty = (empty == null) ? 1 : empty;
	var current = elements[i];
	current[0].style["border"] = "1px solid #000";
	//Same as before, only with names

	if(empty==1 && current[2]=="")
	{
		errorlog[errorcount] = current[3] + blank_error;
		errorelement[errorcount] = current[0];
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		errorcount++;
		return 0;
	}
	else if(current[2].search(name_re) == -1)
	{
		errorlog[errorcount] = current[3] + name_error;
		errorelement[errorcount] = current[0];
		errorcount++;
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		return 0;
	}
	return 1;
}

function check_phone(i,empty)
{
	empty = (empty == null) ? 1 : empty;
	var current = elements[i];
	current[0].style["border"] = "1px solid #000";
	//Same as before, only with phone numbers

	if(empty==1 && current[2]=="")
	{
		errorlog[errorcount] = current[3] + blank_error;
		errorelement[errorcount] = current[0];
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		errorcount++;
		return 0;
	}
	else if(current[2].search(phone_re) == -1)
	{
		errorlog[errorcount] = current[3] + phone_error;
		errorelement[errorcount] = current[0];
		errorcount++;
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		return 0;
	}
	return 1;
}

function check_num(i,empty)
{
	empty = (empty == null) ? 1 : empty;
	var current = elements[i];
	current[0].style["border"] = "1px solid #000";
	//Same as before, only with plain numbers

	if(empty==1 && current[2]=="")
	{
		errorlog[errorcount] = current[3] + blank_error;
		errorelement[errorcount] = current[0];
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		errorcount++;
		return 0;
	}
	else if(current[2].search(num_re) == -1)
	{
		errorlog[errorcount] = current[3] + num_error;
		errorelement[errorcount] = current[0];
		errorcount++;
		if((typeof errorfound == null) || (errorfound == 0))
		{
			errorfound = 1;
			current[0].focus();
		}
		return 0;
	}
	return 1;
}
