//validation methods for VSTTextObject
// author v.h.indukumar
// copyright(c) virtual software & training

function validateTextObject(arr)
{
	var i=0;
	for (i=0;i<=arr.length-1;i++)
	{
		switch (arr[i].object.type)
		{
			case "text" :
			{
				if (arr[i].object.value == "" || isOnlySpace(arr[i].object.value))	// empty field
				{
					if (arr[i].required == true)
					{
						alert(arr[i].errorMessage) // should not be empty
						arr[i].object.select();
						arr[i].object.focus();
						return false;
					}
				}
				else							// the field is not empty
				{
					switch(arr[i].content)	//check the content type
					{
						case "alpha" :
						{
							if (!(isAlpha(arr[i].object.value)))
							{
								alert("This field should contain only alphabets.");
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						case "integer" :
						{
							if (!(isInteger(arr[i].object.value)))
							{
								alert("This field should not contain characters other than numeric.")
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						case "float" :
						{
							if (!(isNumberFloat(arr[i].object.value)))
							{
								alert("This field should not contain characters other than decimal values.")
								selectObject(arr[i].object);
								return false;
							}
							break;
						}				
						case "email" :
						{
							if (!(isEmail(arr[i].object.value)))
							{
								alert("Please enter a valid email")
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						case "age" :
						{
							if (!(isAge(arr[i].object.value))||(isFloat(arr[i].object.value)))
							{
								alert ("Please enter the correct Age")
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						case "id" :
						{
							if (!isId(arr[i].object.value))
							{
								alert ("This field should not contain numeric at the beginning.\nAlso Special characters are not allowed.");
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						case "alphanumeric" :
						{
							if (!isAlphaNumeric(arr[i].object.value))
							{
								alert ("This field should not contain special characters.");
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
						default:
						{
							if (arr[i].content.substring(0,3)=="min")
							{
								var minVal =0;
								var val = 0;
								val = arr[i].object.value;
								if (!isNumeric(val))
								{
									alert ("This field should be Integer");
									selectObject(arr[i].object);
									return false;
								}
								minVal = arr[i].content.substring(3,arr[i].content.length);
								if((isLessThan(val,minVal)))
								{
									alert("Minimum value of this field is " + minVal);
									selectObject(arr[i].object);
									return false;
								}
							}
							break;
						}
					}
				}
				break;
			}
			case "password" :
			{
				if (arr[i].object.value == "")	// empty field
				{
					if (arr[i].required == true)
					{
						alert(arr[i].errorMessage) // should not be empty
						arr[i].object.select();
						arr[i].object.focus();
						return false;
					}
				}
				else							// the field is not empty
				{
					switch(arr[i].content)	//check the content type
					{
						case "password" :
						{
							if (!isId(arr[i].object.value))
							{
								alert ("This field should not contain numeric at the beginning.\nAlso Special characters and white spaces are not allowed.");
								selectObject(arr[i].object);
								return false;
							}
							if (arr[i].object.value.length < 8)
							{
								alert ("For Security Reasons , the number of characters should be greater than 8.");
								selectObject(arr[i].object);
								return false;
							}
							break;
						}
					}
				}
			}
		}
	}
	return true;
}


function isSpecial(str)
{
	var j=0;
	specialCharList = new Array("!","@","#","%","&","*","{","}");
	for(j=0;j<=(specialCharList.length)-1;j++)
	{
		if(str.search(specialCharList[j])!= -1)
		{
			alert(specialCharList[j] + " Found");
			return true;
		}
	}
	return false ; // special char does'nt exist
}

function isAlpha(str)
{
	var j=0;
	for (j=0;j<=str.length-1;j++)
	{
		chr = str.substring(j,j+1);
		if (!((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') ||
			(chr == ' ') || (chr=='\'') || (chr=='.')))
			return false;
	}
	return true;
}

function isEmail(str)
{
	var j=0;
	if((str.search('@') == -1) || (str.indexOf('.') == -1))
	{
		return false;
	}
	else
	{
		if (getCountOfOccurance(str,'@')>1 || (isMiddle(str,'@')==false) || (isMiddle(str,'.')==false))
			return false;
	}
	for (j=0;j<=str.length-1;j++)
	{
		chr = str.substring(j,j+1);
		if (!((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') ||
		(chr=='.') || (chr=='@') || (chr=='_') || (chr=='-') || (chr>='0' && chr <= '9')))
			return false;
	}
	return true;
}

function isNumeric(str)
{
	if (!isNaN(str))
	{
		return true;
	}
	return false;
}

function getCountOfOccurance(str,chr)
{
	var j=0;
	var count = 0;
	for (j=0;j<=str.length;j++)
	{
		if(str.charAt(j)==chr)
		{
			count++;
		}
	}
return count;
}

function isMiddle(str,chr)
{
	if(str.indexOf(chr)<1 || str.indexOf(chr)>str.length-2)
		return false;
	else
		return true;
}

function isAge(str)
{
	if(isNumeric(str))
	{
		if (str>0 && str<=100)
			return true;
	}
	return false;
}

function selectObject(obj)
{
	obj.select();
	obj.focus();
}

function isFloat(str)
{
	if (isNumeric(str))
	{
		if(getCountOfOccurance(str,'.')>0)
		{
			return true;
		}
	}
	return false;
}

function isInteger(str)
{
	if (isNumeric(str) && !(isFloat(str)))
		return true;
	return false;
}

//added by ritu on 10-07-2000 for tax calculator
function isNumberFloat(str)
{ 
	if (isNumeric(str) && (isFloat(str))){
		return true;
	}
	return false;
}
//end of ritu added code'


function isLessThan(str,min)
{
	if (isNumeric(str) && isNumeric(min))
	{
		if (parseFloat(str) < parseFloat(min)) return true;
	}
	return false;
}

function isId(str)
{
	if (isNumeric(str.substring(0,1)) || !(isAlphaNumeric(str)))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isAlphaNumeric(str)
{
	var j=0;
	for (j=0;j<=str.length-1;j++)
	{
		chr = str.substring(j,j+1);
		if (!((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') ))
			return false;
	}
	return true;
}

function isOnlySpace(str)
{
	var j=0;
	for (j=0;j<=str.length-1;j++)
	{
		chr = str.substring(j,j+1);
		if (chr!=' ')
			return false;
	}
	return true;
}