function isRequired(element){
	return element.className.indexOf('required')!=-1 || element.className.indexOf('confirm')!=-1;
}

function typeElement(element){
	return element.className.split(" ");
}

function armarErrors(element, type){
	if (type == 'checkbox' || type == 'radio' || type == 'select'){
		error = 'Debe marcar el campo ' + element.parentNode.parentNode.childNodes[1].innerHTML.replace(/<[^>]*>/gi,'').replace(/[\n\s]/gi,' ') + '\n';
	}
	else{
		error =  'Debe completar el campo ' + element.parentNode.parentNode.childNodes[1].innerHTML.replace(/<[^>]*>/gi,'').replace(/[\n\s]/gi,' ') + '\n';
	}
	return error;
}

function setClassError(element){
	var elemS = getElement('tr_'+element.id);
	if (elemS.className.search("f_error") == -1){
		elemS.className += ' f_error';
	}
}

function setError(element, tipo){
	setClassError(element);
	return armarErrors(element, tipo);
}

function setNoError(element, tipo){
	setClassNoError(element);
}

function setClassNoError(element){
	elem = getElement('tr_'+element.id);
	elem.className = elem.className.replace(" f_error", "");
}

function showErrorMsg(flag, errors){
	if (!flag){
		alert(errors);
	}
}

function getValue(_elemento){
	var obj = getElement(_elemento);
	if(obj.tagName=='select'){
		var n = obj.selectedIndex;
		var val = obj[n].value;
		return val;
	}
	else{
		switch(obj.type){
			case 'checkbox':
				return obj.checked;
			break;
		default:
			return obj.value;
			break;
		}
	}
}

function setValue(_elemento, val){
	var obj = getElement(_elemento);
	if(obj.tagName=='select'){
		if(val!=''){
			for (var i = 0; i < obj.length; ++i){
				if (obj[i].value == val){
					obj.selectedIndex = i;
				}
			}
		}
	}
	else{
		switch(obj.type){
			case 'checkbox':
				if (val=='1'){
					obj.checked = true
				}
				else{
					obj.checked = false;
				}
			break;
			default:
				obj.value=val;
			break;
		}
	}
}

function submitForm(f){
	var formName = getElement(f);
	formName.submit();
}

function isEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (lstr > 0){
		if (str.indexOf(at)==-1){
			return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
		}

		if (str.indexOf(at,(lat+1))!=-1){
			return false
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		}

		if (str.indexOf(dot,(lat+2))==-1){
			return false
		}
	}
	else{
		return false;
	}

	if (str.indexOf(" ")!=-1){
		return false
	}
	return true
}

function IsNumeric(strString){
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	for (u = 0; u < strString.length && blnResult == true; u++){
		strChar = strString.charAt(u);
		if (strValidChars.indexOf(strChar) == -1){
			blnResult = false;
		}
	}
	return blnResult;
}

function isAlphanumeric(alphane){
	if (alphane.length == 0) return false;
	var numaric = alphane;
	var bool = true;
	for(var j=0; j<numaric.length; j++){
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(!((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))){
			bool = bool && false;
		}
	}
	return bool;
}

function isNotEmpty(name){
	return name.length>0;
}

function validateCheckBox(name){
	return getValue(name);
}

function validateText(name){
	texto = getValue(name);
	return isNotEmpty(texto);
}

function validateNIF(name){
	var nif = getValue(id);
/*
	var tomatch = /(^[0-0]{1,1})+([0-9]{7,7})+([a-zA-Z]{1,1})/
	if (tomatch.test(nif)){
		return true;
	}
	else{
		return false;
	}*/

	return nif.length == 8;
}

function validateUsuario(name){
	var user = getValue(name);
	var tomatch = /(^[a-z]{1,1})+([a-z0-9]{3,11})*$/
	if (tomatch.test(user)){
		return true;
	}
	else{
		return false;
	}
}

function validatePassword(name){
	var user = getValue(name);
	/*
	var tomatch = /(^[a-z0-9]{1,1})+([a-z0-9]{3,11})*$/

	*/
	var tomatch = /^([a-z0-9]{3,11})*$/
	if (tomatch.test(user)){
		return true;
	}
	else{
		return false;
	}
}

function validatePasswordConfirm(name){
	pass = getValue(name);
	aux = validatePassowrd(name) && pass == getValue('e_passwordConfirm');
	return aux;
}

function validateEmail(name){
	email = getValue(name);
	aux = isEmail(email);
	return aux;
}

function validateEmailConfirm(name){
	email = getValue(name);
	aux = isEmail(email) && getValue('e_email') == email;
	return aux;
}

function validateUrl(name){
	var theUrl = getValue(name);
	var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
	if (tomatch.test(theUrl)){
		return true;
	}
	else{
		return false;
	}
}


function validateImagen(name){
	var file = getElement(name);
	valor = file.value.toLowerCase();
	var bool1 = valor.search(".jpeg") != -1;
	var bool2 = valor.search(".jpg") != -1;
	var bool3 = valor.search(".gif") != -1
	return bool1 || bool2 || bool3;
}

function validateCpSpain(id){
	valor = getValue(id);
	return IsNumeric(valor) && valor.length ==5;
}

function validateFileDoc(name){
	var file = getElement(name);
	valor = file.value.toLowerCase();
	var bool1 = valor.search(".doc") != -1;
	var bool2 = valor.search(".pdf") != -1;
	return bool1 || bool2;
}
function validateCampos(element,arrClass){
	var bool = true;
	id = element.id;
		switch(arrClass[1]){
			case "text":
				bool = validateText(id);
			break;
			case "select":
				valor = getValue(id);
				bool = valor != '#' && valor != '';
			break;
			case "checkbox":
				bool = validateCheckBox(id);
			break;
			case "email":
				bool = validateEmail(id);
			break;
			case "url":
				bool = validateUrl(id);
			break;
			case "password":
				bool = validatePassword(id);
			break;
			case "username":
				bool = validateUsuario(id);
			break;
			case "imagen":
				bool = validateImagen(id);
			break;
			case "fileDoc":
				bool = validateFileDoc(id);
			break;
			case "cp":
				if (getValue('s_pais') == 34){
						bool = validateCpSpain(id);
				}
				else{
					bool = validateText(id);
				}
			break;
			case "cif":
				cif = getElement('e_nif');
				bool = compruebaCIF(cif) && validateCIF(cif);
			break;
			case "nif":
				bool = validateNIF(id);
			break;
			case "textArea":
				bool = validateText(id) && getValue(id).length >= 100;
			break;
	}
	if (arrClass[0] == 'confirm'){
			bool = bool && getValue(id) == getValue(arrClass[2]);
	}
	return bool;
}

function validateForm( f ) {
	var errors = '';
	var i;
	if ( !f ) return false;
	flag = true;
	for( i=0 ; i<f.length ; i++ ){
		if (isRequired(f.elements[i])){
			element = f.elements[i];
			arrClass = typeElement(f.elements[i]);
			if (!validateCampos(element, arrClass)){
				errors += setError(element, arrClass[1]);
				flag = false;
			}
			else{
				setNoError(element, arrClass[1]);
			}
		}
	}
	if (!flag){
		window.scroll(0,0);
		setDisplay("error_msg", "");
	}
	return flag;
}