/*** 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 validate(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 '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. ***/			
												}
										}
									}
						}
						/*** If it is a radio it looks if any option is checked ***/
						if(form.elements[i].type == "radio" && form.elements[i].disabled == false)
						{
										fail = true;
										if(radios == null || radios[0] != document.getElementsByName(form.elements[i].name)[0])
										{
											var radios = document.getElementsByName(form.elements[i].name); 
											for (k = 0; k < radios.length; k++)
											{
												if (radios[k].checked)
												{		
														fail=false;
														k=radios.length;
												}
											}
											if(fail) error += "<li>" + form.elements[i].id + "</li>";
										}
						}
						/*** If it is a checkbox it looks if it is checked ***/
						if(form.elements[i].type == "checkbox" && form.elements[i].disabled == false && !form.elements[i].checked)
						{
								error += "<li>" + form.elements[i].id + "</li>";
						}
						
						if(form.elements[i].type == "select-one" && form.elements[i].disabled == false && form.elements[i].value =="-1")
						{		
								error += "<li>" + form.elements[i].id + "</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();
	
}

function payChange(change){
		document.getElementById("cctit").disabled = change;
		document.getElementById("ccnum").disabled = change;
		document.getElementById("ccmes").disabled = change;
		document.getElementById("ccanyo").disabled = change;
		document.getElementById("cctipo").disabled = change;
}