function AskDelete(form) {
	if (confirm("Estas seguro que quieres borrar esta ficha?")) {
		form.submit();
		return (true);
	} else {
		return (false);
	}
}


//validate the whole form to be submitted
function ValidateForm( theForm )
{
	var theAction = "";
	
	//loop thought all form elements, except last two
	for( i = 0; i < (theForm.length-2); i++ )	{//-2 to ignore last submit and reset buttons		
		if (theForm.elements[i].type == "text" || theForm.elements[i].type == "password") { //we only check on input fields
			theAction = theForm.elements[i].onblur; //get the manually set handler
			if (theAction) { //ignore if not action set
				theAction = String(theAction); //force conversion to string
				
				//find command between the two { }
				offset = theAction.indexOf("{") + 2; //skip the found "{" and return char
				theAction = theAction.substring(offset,theAction.length);
				offset = theAction.lastIndexOf("}") -1; //skip the found "}" 
				theAction = theAction.substring(0,offset);
		
				//replace this for refecence to element
				theAction = theAction.replace("this", "theForm.elements[" + i + "]");
								
				theResult = eval (theAction);
				
				if (theResult == false) return false; //validation failed, exit without submitting
			}
		}
	}
	return true;
} // ValidateForm()


//validate strings and integers

function StringOfDigits( theString )
{
	var	validDigits	=	"0123456789";
	var	i			=	0;

	if ( theString.length == 0 )
		return false;	// blank entry is not a string of digits
	
	for( i = 0; i < theString.length; i++ )	{
		var	thisChar	=	theString.charAt(i);	// this line necessary because of buug (?) (Mac only?)
		if ( validDigits.indexOf( thisChar ) == -1 )	{
			return false;
		} // end if
	} // end for

	return true;
} // StringOfDigits()

function ValidateBlank( theElement )	{
	if ( theElement.value == "" )
		return true;
	else
		return false;
} // ValidateBlank()

function ValidateLength( theElement, lowerBound, upperBound )	{
	if ( (theElement.value.length >= lowerBound) && (theElement.value.length <= upperBound))
		return true;
	else
		return false;
} // ValidateLength()

function ValidateInteger( theElement )	{
	if ( theElement.value.length == 0 )
		return false;	// blank entry is not a valid integer
	
	if ( theElement.value.charAt(0) == '-' )	{
		return StringOfDigits( theElement.value.substring( 1, theElement.value.length ) );
	} else	{
		return StringOfDigits( theElement.value );
	} // end if
} // ValidateInteger()

function ValidateIntegerBetween( theElement, lowerBound, upperBound )	{
	if ( ValidateInteger( theElement ) )	{
		var theValue = parseInt( theElement.value, 10 );
		if ( (theValue <= upperBound) && (theValue >= lowerBound))	{
			return true;
		} else	{
			return false;
		} // end if
	} else	{
		return false;
	} // end if
} // ValidateIntegerBetween()

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

function CheckNotBlank( theElement )	{
	if ( ValidateBlank( theElement ) )	{
		alert( "Campo '" + theElement.name + "' esta en blanco." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckNotBlank()

function CheckLength( theElement, lowerBound, upperBound )	{
	if ( !ValidateLength( theElement, lowerBound, upperBound ) )	{
		alert( "Campo '" + theElement.name + "' no tiene entre " + lowerBound + " y " + upperBound + " caracteres." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckLength()

function CheckInteger( theElement )	{
	if ( !ValidateInteger( theElement ) )	{
		alert( "Campo '" + theElement.name + "' no es un numero entero." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckInteger()

function CheckIntegerBetween( theElement, lowerBound, upperBound )	{
	if ( !ValidateIntegerBetween( theElement, lowerBound, upperBound ) )	{
		alert( "Campo '" + theElement.name + "' no es un numero entero entre " + lowerBound + " y " + upperBound + "." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckIntegerLess()


// Date Validation
function ValidateDate( theElement, dateFormat, MustExist )	{
	var	month	=	0;
	var	day		=	0;
	var	year	=	0;
	var	offset	=	0;
	var	thePart	=	"";
	var	testStr	=	"";
	var	monthNames;
	
	if ( theElement.value.length == 0 ) {
		if (MustExist) {
			return false;	// blank entry is not a date
		} else {
			return true;	// blank entry is allowed
		}
	}		
	if ( dateFormat == "" )	{
		dateFormat = "YYYY-MM-DD";	// default date format
	} // end if
	
		
	thePart = theElement.value;
	
	if (thePart == "0000-00-00") return true; //0000-00-00 is valid date!
	
	offset = thePart.indexOf("-");
	if ( offset < 1 )
		return false;
		
	testStr = thePart.substring(0,offset);
	if ( !StringOfDigits( testStr ) )
		return false;
	year = parseInt( testStr, 10 );
	if (year < 1000 || year > 9999) return false; // we dont want ancient years


	thePart = thePart.substring( offset+1, thePart.length );
	offset = thePart.indexOf("-");
	if ( offset < 1 )
		return false;
	testStr = thePart.substring(0,offset);
	if ( !StringOfDigits( testStr ) )
		return false;
	month = parseInt( testStr, 10 );
	if ( (month < 1) || (month > 12 ) )
		return false;

		
	testStr = thePart.substring( offset+1, thePart.length );
	if ( !StringOfDigits( testStr ) )
		return false;
	day = parseInt( testStr, 10 );
	if ( (day < 1) || (day > 31 ) )
		return false;
	
		

	if ( month == 2 )	{
		if ( (4 * Math.floor( year / 4 )) == year )	{
			if ( day > 29 )
				return false;
		} else	{
			if ( day > 28 )
				return false;
		} // end if
	} else if ( (month == 4) || (month == 6) || (month == 9) || (month == 11) )	{
		if ( day > 30 )
			return false;
	} // end if
	
	return true;
} // ValidateDate()

function CheckDate( theElement, MustExist )	{
	if ( !ValidateDate( theElement, "" , MustExist) )	{
		alert( "Campo '" + theElement.name + "' no es una fecha valida 'YYYY-MM-DD'." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckDate()


//Float validation
function ValidateFloat( theElement )	{
	var	i			=	0;
	var	j			=	0;
	var	k			=	0;
	var	l			=	0;
	var	thePart	=	"";
	
	if ( theElement.value.length == 0 )
		return false;	// blank entry is not a number
	
	if ( theElement.value.charAt(i) == '-' )	{
		i = 1;
	} // end if

	if ( (j = theElement.value.indexOf(".")) == -1 )	{
		j = theElement.value.length;
	} // end if 
	
	thePart = theElement.value.toUpperCase();
	if ( (k = thePart.indexOf("E")) == -1 )	{
		k = theElement.value.length;
	} // end if
	
	l = ( j < k ) ? j : k;
	
	if ( l > i )	{
		thePart = theElement.value.substring( i, l );
		
		if ( !StringOfDigits( thePart ) )	return false;
	} // end if
	
	if ( j < theElement.value.length )	{
		thePart = theElement.value.substring( j+1, k );

		if ( !StringOfDigits( thePart ) )	return false;
	} // end if
	
	if ( k < theElement.value.length )	{
		thePart = theElement.value.substring( k+1, k+2 );
		
		if ( (thePart == "+") || (thePart == "-") )	{
			k++;
		} // end if
	
		thePart = theElement.value.substring( k+1, theElement.value.length );

		if ( !StringOfDigits( thePart ) )	return false;
	} // end if
	
	return true;
} // ValidateFloat()

function ValidateFloatBetween( theElement, lowerBound , upperBound )	{
	if ( ValidateFloat( theElement ) )	{
		if ( (parseFloat(theElement.value) >= lowerBound) && (parseFloat(theElement.value) <= upperBound))	{
			return true;
		} else	{
			return false;
		} // end if
	} else	{
		return false;
	} // end if
} // ValidateFloatGreater()

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

function CheckFloat( theElement )	{
	if ( !ValidateFloat( theElement ) )	{
		alert( "Campo '" + theElement.name + "' no es un numero valido." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckFloat()

function CheckFloatBetween( theElement, lowerBound,upperBound )	{
	if ( !ValidateFloatBetween( theElement, lowerBound,upperBound ) )	{
		alert( "Campo '" + theElement.name + "' no es un numero entre " + lowerBound + " y " + upperBound + "." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckFloatGreater()
