
//---------------------------------------------

//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 StringOfDateChars( 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;
} // StringOfDateChars()


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 true;	// blank entry IS 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( "Feld '" + theElement.name + "' ist leer." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckNotBlank()

function CheckLength( theElement, lowerBound, upperBound )	{
	if ( !ValidateLength( theElement, lowerBound, upperBound ) )	{
		alert( "Feld '" + theElement.name + "' muss zwischen " + lowerBound + " und " + upperBound + "  Zeichen haben." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckLength()

function CheckInteger( theElement )	{
	if ( !ValidateInteger( theElement ) )	{
		alert( "Feld '" + theElement.name + "' ist keine ganze Zahl." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckInteger()

function CheckIntegerBetween( theElement, lowerBound, upperBound )	{
	if ( !ValidateIntegerBetween( theElement, lowerBound, upperBound ) )	{
		alert( "Feld '" + theElement.name + "' ist keine ganze Zahl zwischen  " + lowerBound + " und " + 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	testStr1	=	"";
	var	testStr2	=	"";
	var	testStr3	=	"";
	var	MySQLFormat	=	"";
	var	monthNames;
	
	if ( theElement.value.length == 0 ) {
		if (MustExist) {
			return "-1";	// blank entry is not a date
		} else {
			return "";	// blank entry is allowed
		}
	}		

	//extract pieces from string
	thePart = theElement.value;
	
	//get first string of digits
	offset = thePart.indexOf("/");
	if ( offset < 1 ) return "-1";
	testStr1 = thePart.substring(0,offset);
	if ( !StringOfDigits( testStr1 ) ) return "-1";

	//get second string of digits
	thePart = thePart.substring( offset+1, thePart.length );
	offset = thePart.indexOf("/");
	if ( offset < 1 ) return "-1";
	testStr2 = thePart.substring(0,offset);
	if ( !StringOfDigits( testStr2 ) ) return "-1";
	
	//get third string of digits
	testStr3 = thePart.substring( offset+1, thePart.length );
	if ( !StringOfDigits( testStr3 ) ) return "-1";


	switch (dateFormat) {
		case "D":
			dateFormat = "DD-MM-YYYY";	// default date format
			if (thePart == "00-00-0000") return "0000-00-00"; //0000-00-00 is valid date!
			year = parseInt( testStr3, 10 );
			month = parseInt( testStr2, 10 );		
			day = parseInt( testStr1, 10 );
			break;
		case "M":
			dateFormat = "MM-DD-YYYY";	// default date format
			if (thePart == "00-00-0000") return "0000-00-00"; //0000-00-00 is valid date!
			year = parseInt( testStr3, 10 );		
			month = parseInt( testStr1, 10 );		
			day = parseInt( testStr2, 10 );
			break;
		default:
			dateFormat = "YYYY-MM-DD";	// default date format
			if (thePart == "0000-00-00") return "0000-00-00"; //0000-00-00 is valid date!
			year = parseInt( testStr1, 10 );		
			month = parseInt( testStr2, 10 );		
			day = parseInt( testStr3, 10 );
			break;
	}
	
	if (year < 1000 || year > 9999) return "-1"; // we dont want ancient years
	if ( (month < 1) || (month > 12 ) ) return "-1";
	if ( (day < 1) || (day > 31 ) ) return "-1";

	if ( month == 2 )	{
		if ( (4 * Math.floor( year / 4 )) == year )	{
			if ( day > 29 )
				return "-1";
		} else	{
			if ( day > 28 )
				return "-1";
		} // end if
	} else if ( (month == 4) || (month == 6) || (month == 9) || (month == 11) )	{
		if ( day > 30 )
			return "-1";
	} // end if
	
	if(month<10) month = "0" + month;
	if(day<10) day = "0" + day;
	MySQLFormat = year + "-" + month + "-" + day;;
	
	return (MySQLFormat);
} // ValidateDate()

function CheckDateD( theElement, MustExist, OutFieldName )	{
	MySQLFormat = ValidateDate( theElement, "D" , MustExist);
	if (MySQLFormat == "-1")	{
		alert( "Feld '" + theElement.name + "' hat kein gültiges Datum." );		
		return false;
	} else	{
		theStr = "document." + OutFieldName + ".value = \"" + MySQLFormat + "\"";
		eval(theStr);
		return true;
	} // end if
} // CheckDate()

function CheckDateM( theElement, MustExist, OutFieldName )	{
	MySQLFormat = ValidateDate( theElement, "M" , MustExist);
	if (MySQLFormat == "-1")	{
		alert( "Feld '" + theElement.name + "' hat kein gültiges Datum." );		
		return false;
	} else	{
		theStr = "document." + OutFieldName + ".value = \"" + MySQLFormat + "\"";
		eval(theStr);
		return true;
	} // end if
} // CheckDate()
function CheckDateY( theElement, MustExist, OutFieldName )	{
	MySQLFormat = ValidateDate( theElement, "Y" , MustExist);
	if (MySQLFormat == "-1")	{
		alert( "Feld '" + theElement.name + "' hat kein gültiges Datum." );		
		return false;
	} else	{
		theStr = "document." + OutFieldName + ".value = \"" + MySQLFormat + "\"";
		eval(theStr);
		return true;
	} // end if
} // CheckDate()


//routines to change MySQL Dates YYYY-MM-DD to human format
function TranslateDate (inDate, dateFormat, inName, inSize, inMaxlength, inRelatedField, MustExist) {
	var outDate = "";
	var	thePart	=	"";
	var outStr = "";
	var	testStr1	=	"";
	var	testStr2	=	"";
	var	testStr3	=	"";
	
	if (inDate != "" && inDate != "0000-00-00 00:00:00" && inDate != "0000-00-00") {
		//In MySQL it is always YYYY-MM-DD
		//extract pieces from string
		thePart = inDate;
		
		//get first string of digits
		offset = thePart.indexOf("-");
		if ( offset < 1 ) return false;
		testStr1 = thePart.substring(0,offset);
		if ( !StringOfDigits( testStr1 ) ) return false;
		//get second string of digits
		thePart = thePart.substring( offset+1, thePart.length );
		offset = thePart.indexOf("-");
		if ( offset < 1 ) return false;
		testStr2 = thePart.substring(0,offset);
		if ( !StringOfDigits( testStr2 ) ) return false;
		
		//get third string of digits
		thePart = thePart.substring( offset+1, thePart.length );
		offset = thePart.indexOf(" ");
		if ( offset <= 0 ) {// -1 is in case of "2003-01-01"
			testStr3 = thePart;
		} else { 
			testStr3 = thePart.substring(0,offset);
		}
		
		if ( !StringOfDigits( testStr3 ) ) return false;
	
		year = parseInt( testStr1, 10 );		
		month = parseInt( testStr2, 10 );		
		day = parseInt( testStr3, 10 );
		if(month<10) month = "0" + month;
		if(day<10) day = "0" + day;
	}
	
	switch (dateFormat) {
		case "D":
			if (inDate != "" && inDate != "0000-00-00 00:00:00" && inDate != "0000-00-00") outDate = day + "/" + month + "/" + year;
			outStr = "<input type='text' name='" + inName + "' size='" + inSize + "' maxlength='" + inMaxlength + "' value='" + outDate + "' onBlur='CheckDateD(this," + MustExist + ",\"" + inRelatedField + "\");'>";
			break;
		case "M":
			if (inDate != "" && inDate != "0000-00-00 00:00:00" && inDate != "0000-00-00") outDate = month + "/" + day + "/" + year;
			outStr = "<input type='text' name='" + inName + "' size='" + inSize + "' maxlength='" + inMaxlength + "' value='" + outDate + "' onBlur='CheckDateM(this," + MustExist + ",\"" + inRelatedField + "\");'>";
			break;
		default:
			if (inDate != "" && inDate != "0000-00-00 00:00:00" && inDate != "0000-00-00") outDate = year + "/" + month + "/" + day;
			outStr = "<input type='text' name='" + inName + "' size='" + inSize + "' maxlength='" + inMaxlength + "' value='" + outDate + "' onBlur='CheckDateY(this," + MustExist + ",\"" + inRelatedField + "\");'>";
			break;
	}
	document.write (outStr);
	return outStr;
}


//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 true;	// blank entry IS 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( "Feld '" + theElement.name + "' hat keinen gültigen Nummer." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckFloat()

function CheckFloatBetween( theElement, lowerBound,upperBound )	{
	if ( !ValidateFloatBetween( theElement, lowerBound,upperBound ) )	{
		alert( "Feld '" + theElement.name + "' muss eine Zahl zwischen " + lowerBound + " und " + upperBound + " haben." );
		
		//theElement.focus();
		//theElement.select();
		
		return false;
	} else	{
		return true;
	} // end if
} // CheckFloatGreater()

