function showAlert (text) {
	Sexy.alert(text);
	return false;
}

// VARS (:S)
var groupstocheck = new Array();
var groupnames = new Array();
var fieldstocheck = new Array();
var fieldnames = new Array();


/**
* Checks if email is valid
*/
function isEmailValid(email){
	var e = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return e.test(email);
}

/**
* Checks form fields
*/
function checkform() {
	for (i=0;i<fieldstocheck.length;i++) {
		if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
			if (document.subscribeform.elements[fieldstocheck[i]].checked) {
			} else {
				showAlert("<h1>Warning</h1><em>Can't complete registration process</em><br/><p>Please enter your "+fieldnames[i]+"</p>");
				return false;
			}
		}
		else {
			if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
				showAlert("<h1>Warning</h1><em>Can't complete registration process</em><br/><p>Please enter your "+fieldnames[i]+"</p>");
				return false;
			}
		}
	}
	for (i=0;i<groupstocheck.length;i++) {
		if (!checkGroup(groupstocheck[i],groupnames[i])) {
			return false;
		}
	}
	
	if(!isEmailValid($("#email").val())){
			showAlert("<h1>Warning</h1><em>Can't complete registration process</em><br/><p>You must use a correct email. Please try again.</p>");
			return false;
		
		}

	
	return true;
}

function checkform_es() {
	for (i=0;i<fieldstocheck.length;i++) {
		if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
			if (document.subscribeform.elements[fieldstocheck[i]].checked) {
			} else {
				showAlert("<h1>Problema</h1><em>No pudo finalizarse el proceso de registro</em><br/><p>Por favor, introduzca su "+fieldnames[i]+"</p>");
				return false;
			}
		}
		else {
			if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
				showAlert("<h1>Problema</h1><em>No pudo finalizarse el proceso de registro</em><br/><p>Por favor, introduzca su "+fieldnames[i]+"</p>");
				return false;
			}
		}
	}
	for (i=0;i<groupstocheck.length;i++) {
		if (!checkGroup_es(groupstocheck[i],groupnames[i])) {
			return false;
		}
	}
	
	if(!isEmailValid($("#email").val())){
			showAlert("<h1>Problema</h1><em>No pudo finalizarse el proceso de registro</em><br/><p>Por favor, introduzca email correcto. </p>");
			return false;
		
		}

	
	return true;
}

/** 
* Add a field to check for
*/
function addFieldToCheck(value,name) {
	fieldstocheck[fieldstocheck.length] = value;
	fieldnames[fieldnames.length] = name;
}

/** 
* Add a group to check for
*/
function addGroupToCheck(value,name) {
	groupstocheck[groupstocheck.length] = value;
	groupnames[groupnames.length] = name;
}

/**
* Compares email and confirmation
*/
function compareEmail() {
	return (document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);
}

/**
* Checks empty fields
*/
function checkGroup(name,value) {
	option = -1;
	for (i=0;i<document.subscribeform.elements[name].length;i++) {
		if (document.subscribeform.elements[name][i].checked) {
			option = i;
		}
	}
	if (option == -1) {
		alert ("Please enter your "+value);
		showAlert("<h1>Warning</h1><em>Can't complete registration process</em><br/><p>Please enter your "+value+"</p>");
		showAlert("<h1>Problema</h1><em>No pudo finalizarse el proceso de registro</em><br/><p>Por favor, introduzca su "+value+"</p>");
		return false;
	}
	return true;
}
