//	by Aleksandar Dulovic - November 19, 2007


//	Form Validation Components
// -----------------------------------------------------------------------------
function validate_required(field, alerttxt){
	with (field){
		if (value==null || value==""){
			alert(alerttxt);
	        field.focus();
			return false;
		} else {
			return true;
		}
	}
}
function validate_length(field, mini, maxi){
	with (field){
		if (value.length<mini || value.length>maxi ){
			alert("El campo "+ field.name +" debe tener entre "+ mini +" y "+ maxi +" dígitos!");
	        field.focus();
			return false;
		} else {
			return true;
		}
	}
}
function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			alert(alerttxt);
	        field.focus();
			return false;
		} else {
			return true;
		}
	}
}
function validate_date(field){
    var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    if ((field.value.match(RegExPattern)) && (field.value!='')) {
		return true
    } else {
        alert('Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 30-2-2000 would not be accepted.\nFormay dd/mm/yyyy.');
        field.focus();
		return false;
    } 
}
function checkArray(form, arrayName) {
	var retval = new Array();
	for(var i=0; i < form.elements.length; i++){
		var el = form.elements[i];
		if(el.type == "checkbox" && el.name == arrayName && el.checked){
			retval.push(el.value);
		}
	}
	return retval;
}

//	Specific Form Validations
// -----------------------------------------------------------------------------
function ValidateInscriptionForm(thisform){
	with (thisform){
		if (validate_required(usuario,"Falta usuario!")==false)		return false
		if (validate_length(usuario, 4, 20)==false)					return false
		if (validate_required(nombre,"Falta nombre!")==false)		return false
		if (validate_length(nombre, 2, 40)==false)					return false
		if (validate_required(apellido,"Falta apellido!")==false)	return false
		if (validate_length(apellido, 2, 40)==false)				return false
		if (validate_required(peso,"Falta peso!")==false)			return false
		if (validate_required(dob,"Falta fecha de nacimiento!")==false)			return false
//		if (validate_date(dob)==false)											return false
		if (validate_required(email,"Falta correo electronico!")==false)		return false
		if (validate_email(email,"Tu email no parece valido!")==false)			return false
		if (validate_required(telefono,"Falta numero de telefono!")==false)		return false
//		if (validate_required(disponible,"Hay que eligir una de las opciones!")==false)		return false
	}
}

function ValidateSolicitud(thisform){
	with (thisform){
		if (validate_required(mensaje,"Falta tu mensaje!")==false)			return false
		if (validate_required(nombre,"Falta tu nombre!")==false)			return false
		if (validate_length(nombre, 2, 40)==false)							return false
		if (validate_required(telefono,"Falta numero de telefono!")==false)	return false
		if (validate_required(email,"Falta correo electronico!")==false)	return false
		if (validate_email(email,"Tu email no parece valido!")==false)		return false
	}
}


function ValidateListing(form) {
	var itemsChecked = checkArray(form, "donor[]");
	//alert("You selected " + itemsChecked.length + " items");
	if(itemsChecked.length > 0) {
		//alert("The items selected were:\n\t" + itemsChecked);
		return true;
	} else {
		alert("Es necesario que selecciones al menos un posible donante.")
		return false; 
	}
}


// showhide
// -----------------------------------------------------------------------------
function showhide(x) {
	document.getElementById(x).style.display = (document.getElementById(x).style.display=='block') ? 'none' : 'block';
}
function justshow(x) {
	document.getElementById(x).style.display = 'block';
}
function justhide(x) {
	document.getElementById(x).style.display = 'none';
}


// image resizing
// -----------------------------------------------------------------------------
function calculateWidth(w,h,Directory,file){
	var nh = document.getElementById('new_'+file+'_height_'+Directory).value;
	var nw = Math.round(w * nh / h);
	document.getElementById('new_'+file+'_width_'+Directory).value = nw;
}
function calculateHeight(w,h,Directory,file){
	var nw = document.getElementById('new_'+file+'_width_'+Directory).value;
	var nh = Math.round(h * nw / w);
	document.getElementById('new_'+file+'_height_'+Directory).value = nh;
}


// confirm deletions
// -----------------------------------------------------------------------------
function confirmDelete(){
	if (confirm("Are you sure you want to delete this record?"))
		return true ;
	else
		return false ;
}


// table highlighting
// -----------------------------------------------------------------------------
function HILITE() {
	if (document.getElementById && document.createTextNode) {
		var myTABLE = document.getElementsByTagName("table");
		for (var i=0; i<myTABLE.length; i++) {
			var myROW = myTABLE[i].getElementsByTagName("tr");
			for (var j=1; j<myROW.length; j++) {
				var myClass;
				myROW[j].onmouseover = function(){
					myClass = this.className;
					this.className = 'colorme';
					return false;
				}
				myROW[j].onmouseout  = function(){
					this.className = myClass;
					return false;
				}
			}
		}
	}
}


// cant donate
// -----------------------------------------------------------------------------
function nocando(msg) {
	alert(msg);
	window.location = "index.php";
}


//	SelectAll
// -----------------------------------------------------------------------------
function SelectAll() {
	if (document.getElementById && document.createTextNode) {
		var myTABLE = document.getElementsByTagName("table");
		for (var i=0; i<myTABLE.length; i++) {
			var myCHECK = myTABLE[i].getElementsByTagName("input");
			for (var j=1; j<myCHECK.length; j++) {
				myCHECK[j].checked = myCHECK[0].checked;
			}
		}
	}
}

// BookMark Site
// -----------------------------------------------------------------------------
function add2favorites(title,url){
	if (window.sidebar){						// firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){		// opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)						// ie
		window.external.AddFavorite(url, title);
}
