/* General validation functions */
function is_empty(field, text, htmlError) {
	if($(field).value == '') {
		if(text) {
			alert("Por favor introduzca su  '" + text + "'");
		}
		if(htmlError) {
			showHtmlError(field, htmlError);
		}
		return true;
	}
	return false;
}


function is_numeric(field, text, htmlError) {
	 var ValidChars = "0123456789.";
	 var IsNumber=true;
	 var Char;
	 
	 sText = $(field).value;
	 
	   for (i = 0; i < sText.length && IsNumber == true; i++)  { 
	      
		   Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) == -1) {
	         IsNumber = false;
	      }
	   }
	   if(!IsNumber) {
		   if(text) {
	        	 alert("El campo '" + text + "' solo puede contener números");
	         }
	         if(htmlError) {
	        	 showHtmlError(field, htmlError);
	 		}
	   }
	   return IsNumber;
}


function has_min_length(field, length, text, htmlError) {
	sText = $(field).value;
	if(sText.length < length) {
		if(text) {
			alert("El campo '" + text + "' debe contener al menos '" + length + "' caracteres");
        }
        if(htmlError) {
       	 	showHtmlError(field, htmlError);
		}
        return false;
	}
	return true;
}

function is_valid_email(field, text, htmlError) {
	email = $(field).value;
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")

	if (AtPos == -1 || StopPos == -1) {
		if(text) {
			alert("El campo '" + text + "' debe contener un email válido");
		}
		if(htmlError) {
       	 	showHtmlError(field, htmlError);
		}
		return false;
	}
	return true;


}

function showHtmlError(field, message) {
	var field = field + 'Error';
	$(field).innerHTML = message;
	$(field).show();
}

function hideHtmlError(field) {
	var field = field + 'Error';
	$(field).hide();
}

/* Validates if the terms & conds have been accepted */
function terms_accepted(warn) {
	if(!$('terms').checked) {;
		if(warn) {
			alert('Debes aceptar los términos y condiciones del sitio para registrarte');
		}
		return false;
	}
	return true;
}

function validateStep1() {
	var errors = false;
	
	//Resets errors
	hideHtmlError('CompanyName');
	hideHtmlError('ClaimTitle');
	hideHtmlError('ClaimText');
	$("fsUploadProgress").innerHTML ='';
	$('ClaimImages').value = '';
	
	if(is_empty('CompanyName', '', 'Escribe o selecciona la empresa a la cual está dirigido tu reclamo.')) {
		errors = true;
	}
	if(is_empty('ClaimTitle', '', 'Introduce una descripción breve para tu reclamo.')) {
		errors = true;
	}
	if(is_empty('ClaimText', '', 'Introduce información adicional sobre tu reclamo.')) {
		errors = true;
	}
	
	//Validates details are added
	if(!errors) {
		swapel('step1', 'step2');
		
	} else {
		return false;
	}
	
}

function validateStep2(formid) {
	
	//Resets errors
	hideHtmlError('ClaimDocument');
	hideHtmlError('ClaimPhone');
	
	var errors = false;
	
	if($('contactYES').checked) {
		if(is_empty('ClaimDocument', '', 'Debes introducir tu cédula')) {
			errors = true;
		}
		if(is_empty('ClaimPhone', '', 'Debes introducir tu teléfono')) {
			errors = true;
		}
		if(!is_numeric('ClaimDocument', '', 'Tu cédula solo puede incluir números')) {
			errors = true;
		}
		if(!is_numeric('ClaimPhone', '', 'Tu teléfono solo puede incluir números')) {
			errors = true;
		}
		if(!has_min_length('ClaimDocument', 5, '', 'Tu número de cédula debe contener al menos 5 dígitos')) {
			errors = true;
		}
		if(!has_min_length('ClaimPhone', 7, '', 'Tu teléfono debe contener al menos 7 dígitos')) {
			errors = true;
		}
		
	}
	
	if(!errors) {
		var stats = this.Upl.getStats();
		if(stats.files_queued > 0) {
			this.Upl.startUpload();
		}
		else {
			new Ajax.Updater( 
					"formcnt", 
					this.baseurl + "add.html",
					{ 	asynchronous: true,
						evalScripts: true, 
						onLoading: function (request) 
										{swapel("formcnt", "MB_loading");}, 
						onLoaded: function (request) 
										{swapel("MB_loading", "formcnt");}, 
						parameters: Form.serialize(formid), 
						requestHeaders: ["X-Update", "formcnt"]}); 
		}
		return true;
	} else {
		return false;
	}
}

function validateComment(formid) {
	
	//Resets errors
	hideHtmlError('CommentText');
	
	var errors = false;
	
	if(is_empty('CommentText', '', 'Debes introducir un comentario')) {
		errors = true;
	}
	if(!errors) {
		return true;
	} else {
		
		return false;
	}
}

function validateEmailFriend(formid) {
	
	//Resets errors
	hideHtmlError('ClaimOname');
	hideHtmlError('ClaimDname');
	hideHtmlError('ClaimDname');
	hideHtmlError('ClaimDmail');
	
	var errors = false;
	
	if(is_empty('ClaimOname', '', 'Debes introducir tu nombre')) {
		errors = true;
	}
	if(!is_valid_email('ClaimOmail', '', 'Debes introducir un e-mail válido')) {
		errors = true;
	}
	if(is_empty('ClaimDname', '', 'Debes introducir el nombre de tu amigo')) {
		errors = true;
	}
	if(!is_valid_email('ClaimDmail', '', 'Debes introducir un e-mail válido')) {
		errors = true;
	}
	if(!errors) {
		return true;
	} else {
		return false;
	}
	
}
                  

