function IsEmpty(val) {
	for (i=0; i<val.length; i++) { 
		if (val.charAt(i) != ' ') return false
	}
	return true
}

function IsValidEmail(val) {
	var iLen = val.length;
	if 	((iLen < 6) || (val.indexOf('@') < 1) || ((val.charAt(iLen - 3) != '.') && (val.charAt(iLen - 4) != '.')) ) return false
	return true
}

function formVal(){
	var msg = "";
	var typeList = "text,password,select-one,textarea,checkbox";
	curForm = document.getElementById("form");
	for (var i=0; i<curForm.elements.length; i++){
		tag = curForm.elements[i]; 
		if (typeList.indexOf(tag.type) >= 0 && tag.getAttribute('required') == 1 
			&& ((tag.type != "checkbox" && tag.value == "") || (tag.type == "checkbox" && tag.checked == false))) {
			msg = msg + "<li>" + tag.title + " </li>";
		};
		if (tag.name.indexOf("email") >= 0  && tag.value.length != 0) {
			flag = IsValidEmail(tag.value);
			if (!flag) {
				msg = msg + "<li>" +  tag.title + " is invalid</li>";
			}
		};
		if (tag.type == "password" && tag.value.length > 0  && tag.value.length < 4) {
			msg = msg + "<li>" + "Password is too short</li>";
		}
		if (tag.name == "password2" && tag.value != "" && tag.value != curForm.password.value)
			msg = msg + "<li>" + "Password confirmation</li>";
	};
	
	document.getElementById('err').innerHTML = "";
	if (msg != "") {
		document.getElementById('err').innerHTML = "The following fields are required: <ul>" + msg + "</ul>";
		window.scrollTo(0,10);
		return false;
	} else {
		return true;
	}
}

function showPhoto(img) { 
	photoWin = window.open('','', 'status=yes,toolbar=no,menubar=no,location=no,resizable=yes');
	photoWin.document.write("<html><title>Soy Catequista</title><body style=\"margin:0;text-align:center\">");
	photoWin.document.write("<img src=\"" + img + "\" id=\"photo\" /><br />[ <a href=\"javascript:window.close()\">close window</a> ]");
	photoWin.document.write("<script>function reload() { curPhoto = document.getElementById('photo'); ");
	photoWin.document.write("x = curPhoto.width;");
	photoWin.document.write("y = curPhoto.height + 80; ");
	photoWin.document.write("window.resizeTo(x, y);");
	photoWin.document.write("} reload(); </script>");
	photoWin.document.write("</body></html>");  
}
 
