function isCheckedRadio(field) {
	var checked=0;
	for(i=0;i<field.length;i++) {
		if(field[i].checked) { return field[i].value; }
	}
	return checked;
}



function isCheckedCheckbox(f,name,no) {
	for (i=no; i>0; i--) {
		if (f[name+"_"+i] != undefined) {
			if (f[name+"_"+i].checked==true) {
				return true;
			}
		}
	}
	
	return false;
}



function positiveNotNull(e) {
	var str = e.value;
	if(isNaN(str) == true || str<=0) {
        return false;
    }
    return true;
}


function isBlank(s)

{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}



function isEmpty(e) {
	if ((e.value == null) || (e.value == "") || isBlank(e.value)) { 
		return true;
    }
	return false;
}

function isEmail(e) {

	var str = e.value;

    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

    if (!str.match(re)) {

        return false;

    } else {

        return true;

    }

}



function isNumeric(e) {
	var str = e.value;
	if(isNaN(str) == true) {
        return false;
    }
    return true;
}



function isPostcode(e) {
	var str = e.value;
    var re = /\d{5}/g;
    str = str.toString(  );
    if (!str.match(re) || str.length > 5) {
        return false;
    }
    return true;
}



function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";
	var fieldname = "";
	
	for(var i = 0; i < f.length; i++) {
		// Get elements of the form
		var e = f.elements[i];
		
		// check textfields
		if((e.type == "text") || (e.type =="textarea") ) {
			if(isEmpty(e) && !e.optional) {
				empty_fields += "\n          " + e.label;
			}
			

			if(typeof(e.check)!="undefined" && !isEmpty(e)) {
				if(e.check=="numeric" && !isNumeric(e)) {
					errors += (''==errors? '': "\n")+"- El Campo \"" + e.label + "\" tiene que ser numerico";
				}

				if(e.check=="postcode" && !isPostcode(e)) {
					errors += (''==errors? '': "\n")+"- El campo \"" + e.label + "\" tiene que ser un número de cinco cifras";
				}



				if(e.check=="email" && !isEmail(e)) {
					errors += (''==errors? '': "\n")+"- El formato de \"" + e.label + "\" está incorrecto";
				}



				if(e.check=="positiveNotNull" && !positiveNotNull(e)) {
					errors += (''==errors? '': "\n")+"- El formato de \"" + e.label + "\" está incorrecto. Los decimales se deben separar con un punto.";
				}
			}
		}


		else {
			// Check a dropbox
			if (!e.optional && e.type.substring(0,6)=="select") {
				if (e.name=='country'){
					if (e.selectedIndex==1) {
						empty_fields += "\n          " + e.label;
						continue;
					}
				}

				else{
					if (e.selectedIndex==0) {
						//alert('selectedIndex: '+e.selectedIndex+' Name:'+e.name +' Label:'+e.label +' Opt:'+e.optional);
						empty_fields += "\n          " + e.label;
						continue;
					}

				}
			}
			// Check checkboxes
			if(!e.optional && e.type=="checkbox") {
				if(fieldname != e.name.split("_")[0]) {
					fieldname = e.name.split("_")[0];
					no = e.name.split("_")[1];
					if(!isCheckedCheckbox(f,fieldname,no)) {
						empty_fields += "\n          " + e.label;
						//errors += "- Hay que seleccionar una opcion de \"" + e.label + "\" \n";
					}
				}
			}
			/*
			if(!e.optional && e.type=="radio" && !isCheckedRadio(e)) {
				if(fieldname != e.name) {
					fieldname = e.name;
					//alert(e.label);
					if(!isCheckedRadio(e)) {
						empty_fields += "\n          " + e[fieldname + "_"].label;
					}
				}
			}
			*/
		}
		//alert("e.name: "+e.name+" e.label: "+e.label);
    }

    if (!empty_fields && !errors) return true;
 	  msg="";
    //msg  = "______________________________________________________\n\n"
    //msg += "The form was not submitted because of the following error(s).\n";
    //msg += "Please correct these error(s) and re-submit.\n";
    //msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- Por favor complete los siguientes datos:" + empty_fields;
        if (errors) {
			msg += "\n\n";
		}
    }
    msg += errors;
    alert(msg);
    return false;
}


