//<!--
//////////////////////////////////////////////////////////////////////////////////////////////////////
//																									//
//	Global function for Client side validation.														//
//	Function Updated on	: March 20,2002.															//
//	Credits				: Yaasir Kulay.																//
//	Company				: Emovez Web Technologies.													//
//																									//
//////////////////////////////////////////////////////////////////////////////////////////////////////
//																									//
//	Function isValid which handles the client side FORM validations.								//
//	Example call : isValid(document.forms[0].email.value,"e-mail","Email ID",true,3,50);			//
//	1st Parameter = Name of the Form field.															//
//	2nd Parameter = Type of the field.																//
//					(Valid options are "text", "dropdown", "login", "password", "e-mail", "digit")	//
//	3rd Parameter = Description of the field. (This name will be displayed in case of an error)		//
//	4th Parameter = True if it is a required field, false if it is an optional field.				//
//	5th Parameter = Minimum Length of the field.													//
//	6th Parameter=	Maximum Length of the field.													//
//	Retruns false if the argument passed is not valid.												//
//																									//
//////////////////////////////////////////////////////////////////////////////////////////////////////

function isValid(vName,vType,vDescription,vRequired,vMinLength,vMaxLength)
{
	//Username Validation
	if(vType == "login")
	{
		var str = vName;
		if (vRequired == true && str == ""){
			errMsg = "Please enter " + vDescription + "."
			return false;
		}
		if (vRequired == true)
		{
			if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z")){
				errMsg = vDescription + " should begin with an alphabetic character.";
				return false;
			}
		}
		for (var i = 1; i < str.length; i++) {
			var ch = str.substring(i, i + 1);
			if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')) {
				errMsg = vDescription + " accepts letters,numbers & underscore.\n\nPlease re-enter your " + vDescription + ".";
				return false;
			}
		}
		if(str != "" && str.length < vMinLength)
		{
			errMsg = vDescription + " should not be less than " + vMinLength + " characters."
			return false
		}
		if(str.length > vMaxLength)
		{
			errMsg = vDescription + " should not exceed " + vMaxLength + " characters."
			return false
		}
	}
	
	// Password Validation
	if(vType == "pass")
	{
		var str = vName;
		if (str == "") {
			errMsg = "Please enter " + vDescription + "."
			return false;
		}
		//if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z")){
		//	errMsg = vDescription + " should begin with an alphabetic character.";
		//	return false;
		//}
		for (var i = 1; i < str.length; i++) {
			var ch = str.substring(i, i + 1);
			if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')) {
				errMsg = vDescription + " accepts letters,numbers & underscore.\n\nPlease re-enter your " + vDescription + ".";
				return false;
			}
		}
		if(str.length < vMinLength)
		{
			errMsg = vDescription + " should not be less than " + vMinLength + " characters."
			return false
		}
		if(str.length > vMaxLength)
		{
			errMsg = vDescription + " should not exceed " + vMaxLength + " characters."
			return false
		}
	}	
	
	// Password/Confirm Password Validation
	if(vType == "password")
	{
		var str = document.forms[0].secretcode.value;
		if (str == "")
		{
			errMsg = vDescription + " cannot be blank.";
			return false;
		}
		if (str.length < vMinLength)
		{
			errMsg = vDescription + " cannot be less than " + vMinLength + " characters."
			return false;
		}
		if (str.length > vMaxLength)
		{
			errMsg = vDescription + " cannot exceed " + vMaxLength + " characters."
			return false;
		}
		var str2 = document.forms[0].consecretcode.value;
		if (str != str2)
		{
			errMsg = "The 2 " + vDescription + "s you entered do not match.";
			return false;
		}
	}
	
	// Text Box Validation 
	if(vType == "text" && vRequired == true && vName == "")
	{
		errMsg = "Please enter " + vDescription + ".";
		return false;
	}
	if(vType == "text" && vName != "" && vName.length < vMinLength)
	{
		errMsg = vDescription + " should be atleast " + vMinLength + " characters.";
		return false;
	}
	if(vType == "text" && vName != "" && vName.length > vMaxLength)
	{
		errMsg = vDescription + " should not exceed " + vMaxLength + " characters.";
		return false;
	}

	// Numeric Field validation
	if(vType == "digit" && vRequired == true && vName == "")
	{
		errMsg = "Please enter " + vDescription + ".";
		return false;
	}
	if(vType == "digit" && vName != "")
	{
		//Allow only numbers to be entered in the Contact No. field
		var checkOK = "0123456789.";
		var checkStr = vName;
		var allValid = true;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)	{
			ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)	{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
		}
		if (!allValid)  {
			errMsg = "Please enter a valid " + vDescription;
			return (false);
		}
	}
	if(vType == "digit" && vName != "" && vName.length < vMinLength)
	{
		errMsg = vDescription + " should be atleast " + vMinLength + " characters.";
		return false;
	}
	if(vType == "digit" && vName != "" && vName.length > vMaxLength)
	{
		errMsg = vDescription + " should not exceed " + vMaxLength + " characters.";
		return false;
	}
	
	// Email Validation
	if(vType == "e-mail" && vRequired == true && vName == "")
	{
		errMsg = "Please enter " + vDescription + ".";
		return false;
	}
	if(vType == "e-mail" && vName != "")
	{
		//Check if email entered is a valid email address
		var checkEmail = "@.";
		var checkStr = vName;
		var EmailValid = false;
		var EmailAt = false;
		var EmailPeriod = false;
		for (i = 0;  i < checkStr.length;  i++)	{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkEmail.length;  j++)	{
				if (ch == checkEmail.charAt(j) && ch == "@")
					EmailAt = true;
				if (ch == checkEmail.charAt(j) && ch == ".")
					EmailPeriod = true;
				if (EmailAt && EmailPeriod)
					break;
				if (j == checkEmail.length)
					break;
				}
		// if both the @ and . were in the string
			if (EmailAt && EmailPeriod)	{
				EmailValid = true
				break;
			}
		}
		if (!EmailValid)	{
			errMsg = "Please enter a valid " + vDescription + ".";
			return (false);
		}
	}
	if(vType == "e-mail" && vName != "" && vName.length < vMinLength)
	{
		errMsg = "Please enter a valid " + vDescription + ".";
		return false;
	}
	if(vType == "e-mail" && vName != "" && vName.length > vMaxLength)
	{
		errMsg = vDescription + " should not exceed " + vMaxLength + " characters.";
		return false;
	}

	// Drop down Validation
	if(vType == "dropdown" && vRequired == true && vName == "")
	{
		errMsg = "Please select your " + vDescription + ".";
		return false
	}

	return true;
}
//-->

