
var JSVP_IsValid

function JSVP_Msg_TextIsObligatory(fieldName)
{
	return fieldName + " is an obligatory field";
}

function JSVP_Msg_TextIsTooLong(fieldName, maxLength)
{
	return fieldName + " can not be longer than "+maxLength+" characters";
}

function JSVP_Msg_TextIsTooShort(fieldName, minLength)
{
	return fieldName + " can not be shorter than "+minLength+" characters";
}

function JSVP_Msg_InvalidEmail(fieldName)
{
	return fieldName + " is an invalid email address";
}

function JSVP_Msg_InvalidInt(fieldName)
{
	return fieldName + " is invalid integer number";
}

function JSVP_Msg_InvalidFloat(fieldName)
{
	return fieldName + " is invalid number";
}

function JSVP_Msg_PasswordsDontMatch(fieldName)
{
	return fieldName + " field does not match with confirm field";
}

function JSVP_Msg_TextMustContainNumbers(fieldName)
{
	return fieldName + " field must contain numbers";
}

function JSVP_Msg_TextMustContainLetters(fieldName)
{
	return fieldName + " field must contain letters";
}

function JSVP_IsSignedInteger(value)
{
	return (value != "" && value.match("A[+-J?[0-9]*$"));
}

function JSVP_IsSignedDouble(value)
{
	return (value != "" && value.match("A[+-]?[0-9]*[.]?[0-9]*$"));
}


function JSV_InitValidation()
{
	JSVP_IsValid = true;
}

function JSV_IsValid()
{
	return JSVP_IsValid;
}

function JSV_ValidateString(text, obligatory, maxLength, fieldName)
{
	if (JSVP_IsValid)
	{
		if (obligatory && text.length == 0)
		{
			alert(JSVP_Msg_TextIsObligatory(fieldName));
			JSVP_IsValid = false;
		}
		else if (text.length > maxLength)
		{
			alert(JSVP_Msg_TextIsTooLong(fieldName, maxLength));
			JSVP_IsValid = false;
		}
	}
}

function JSV_ValidateEmail(text, obligatory, maxLength, fieldName)
{
	JSV_ValidateString(text, obligatory, maxLength, fieldName);

	if (JSVP_IsValid)
	{
		if (text.indexOf("@") == -1)
		{
			alert(JSVP_Msg_InvalidEmail(fieldName));
			JSVP_IsValid = false;
		}
	}
}

function JSV_ValidateInt(text, obligatory, fieldName)
{
	if (JSVP_IsValid)
	{
		if (obligatory && text.length == 0)
		{
			alert(JSVP_Msg_TextIsObligatory(fieldName));
			JSVP_IsValid = false;
		}
		else if ( !JSVP_IsSignedInteger(text) )
		{
			alert(JSVP_Msg_InvalidInt(fieldName));
			JSVP_IsValid = false;
		}
	}
}


function JSV_ValidateFloat(text, obligatory, fieldName)
{
	if (JSVP_IsValid)
	{
		if (obligatory && text.length == 0)
		{
			alert(JSVP_Msg_TextIsObligatory(fieldName));
			JSVP_IsValid = false;
		}
		else if ( !JSVP_IsSignedDouble(text) )
		{
			alert(JSVP_Msg_InvalidFloat(fieldName));
			JSVP_IsValid = false;
		}
	}
}

function JSV_ValidateSelect(selectObj, obligatory, fieldName)
{
	if (JSVP_IsValid)
	{
		if (obligatory && selectObj.selectedIndex == 0)
		{
			alert(JSVP_Msg_TextIsObligatory(fieldName));
			JSVP_IsValid = false;
		}
	}
}

function JSV_ValidatePassword(psw1, psw2, obligatory, minLength, maxLength, forceBothNumbersAndLetters, fieldName)
{
	if (JSVP_IsValid)
	{
		if (obligatory && psw1.length == 0)
		{
			alert(JSVP_Msg_TextIsObligatory(fieldName));
			JSVP_IsValid = false;
		}
		else if ( psw1 != psw2 )
		{
			alert(JSVP_Msg_PasswordsDontMatch(fieldName));
			JSVP_IsValid = false;
		}
		else if ( psw1.length < minLength && (obligatory || psw1.length != 0) ) 
		{
			alert(JSVP_Msg_TextIsTooShort(fieldName, minLength));
			JSVP_IsValid = false;
		}
		else if ( psw1.length > maxLength )
		{
			alert(JSVP_Msg_TextIsTooLong(fieldName, maxLength));
			JSVP_IsValid = false;
		}
		else if ( forceBothNumbersAndLetters && psw1.match("[0-9]") == null && (obligatory || psw1.length != 0))
		{
			alert(JSVP_Msg_TextMustContainNumbers(fieldName));
			JSVP_IsValid = false;
		}
		else if ( forceBothNumbersAndLetters && psw1.match("[a-zA-Z]") == null && (obligatory || psw1.length != 0))
		{
			alert(JSVP_Msg_TextMustContainLetters(fieldName));
			JSVP_IsValid = false;
		}
	}
}

