/*** Created by: Daniel Ramos (a.k.a. Gonshu) Last edited: 2 - 1 - 2008 ***/
/*** Generic validation function, this should be correct to validate any type of form ***/
/*** Introduction: All field names specified of fields you want to validate in form should start with validation type tag, + and 
then field you want to give to a field. In example, if you want to validate that a field is not empty and it is a email, 
field name should be: re+email (r for required, and e for email). If you want to show errors using innerHtml I suggest you to
id content boxes with same name as fields validated plus '_error' or something similar, so you should only change 'alert()' 
functions for document.getElementById('name_error').innerHtml ***/
function validate2(form)
{
	/*** Inicialize 'error' variable, wich is going to indicate if there is an error on our form ***/
	document.getElementById("error").innerHTML = "";
	var error = "";
	/*** We look in all elements from form and depending on his type and if it is enabled we make diferent controls ***/
	for (i = 0; i < form.elements.length; i++) 
	{		
			if(form.elements[i].name != null){
					toCheck = form.elements[i].name.split("+");
					/*** if there is no error and field has some requeriments, check next field ***/
					if(toCheck.length > 1)
					{	
						/*** Take all requeriments ***/
						reqs = toCheck[0];
						if((form.elements[i].type == "text" || form.elements[i].type == "textarea") && form.elements[i].disabled == false)
						{		
								/*** For each requeriment we look if the validation is ok, if there is an error at anytime validation finishes and error is shown on an alert. ***/
								done = false;
									for(j = 0; j < reqs.length; j++)
									{
										if(!done){
												switch(reqs.substring(j,j+1))
												{
													case 'r': // Field required, looks if a field is empty.
															if(form.elements[i].value == ""){
																	 error += "<li>" + form.elements[i].id + "</li>";
																	 done = true;
															}
													break;
													case 'e': // Email, looks if a field accomplish all emails standards.
															var objRegExp  = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
															if(!objRegExp.test(form.elements[i].value)){
																	 error += "<li>" + form.elements[i].id + "</li>";
																	 done = true;
															}
													break;
													case 't': // Phone, looks if a field accomplish all phone standards.
															var objRegExp  = /(^([0-9]{9,9}))$/;
															if(!objRegExp.test(form.elements[i].value)){
																	 error += "<li>" + form.elements[i].id + "</li>";
																	 done = true;
															}
													break;
													case 'p': // Postal code, looks if a field is a postal code (5 numbers).
															var objRegExp  = /(^[0-9]{5}$)/i;
															if(!objRegExp.test(form.elements[i].value)) {
																	 error += "<li>" + form.elements[i].id + "</li>";
																	 done = true;
															}
													break;
													/*** You can add whatever field validation you want here. ***/			
												}
										}
									}
						}
						
												
				}
		}
	}

	var error1 = 0;
	var error2 = 0;


	var chekerror = 0;
	
	
	for(x=0;x<4;x++){if(document.getElementById("ra0"+x).checked === true){ error1 = 1;}}
	if(error1 == 0) error += "<li>¿Cuántos regalos de navidad a empleados y clientes entrega tu empresa?</li>";
	
	
	for(x=0;x<5;x++){if(document.getElementById("ra1"+x).checked === true){ error2 = 1;}}
	if(error2 == 0) error += "<li>¿Cuál es el presupuesto por regalo que tu empresa destina a los regalos de Navidad?</li>";
	
	
	if(error != "")
	{		
		 document.getElementById("error").innerHTML = "<font color='#FF3300' face='Arial'>Por favor, rellena correctamente los siguientes campos:<ul style='padding-left:20px'>" + error + "</ul><br/><br/></font>";
		 return false;
	}
	else	form.submit();
}


