function now() 
{ 
	var stDate; 
	var now; 
	now_date= new Date(); 
	stDate=(now_date.getMonth()+1)+"/"+now_date.getDate()+"/"+now_date.getYear(); 
	return(stDate); 
} 

function IsEmail(sFieldValue)
{
	var checkEmail = "@.";
	var checkStr = sFieldValue;
	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;
		}
	}

	return(EmailValid);
}

function IsNumeric(sFieldValue)
{
	var checkOK = "0123456789";
	var checkStr = sFieldValue;
	var allValid = true;
	var allNum = "";

	if (checkStr=="")
	{
		return(false);
	}
	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)
		{
			allValid =false;
		}
	}

	return(allValid);
}

function FieldValidation(sFieldName, sFieldValue, sRule, bRequired, maxLength)
{
	var sErrorMessage;
	
	sErrorMessage="";

	switch(sRule)
	{
		case "Numeric":
			if (IsNumeric(sFieldValue)==false)
			{
				sErrorMessage+=sFieldName + " is not numeric \n";
			}
			break;
		case "Email":
			if (IsEmail(sFieldValue)==false)
			{
				sErrorMessage+=sFieldName + " is not an email \n";
			}
			break;
	}

	/*------------------------------------------check if field is required---------------------------*/
	if ( (bRequired==true) && (sFieldValue!=""))
	{
		if (sFieldValue.length==0)
		{
			sErrorMessage+=sFieldName + " is required \n";
		}
	}	
	else if ( (bRequired==true) && (sFieldValue==""))
	{
		sErrorMessage+=sFieldName + " is required \n";
	}
	/*------------------------------------check for maxlength--------------------------------------*/
	if ((sFieldValue!="") && (maxLength>0))
	{
		if (sFieldValue.length>maxLength)
		{
			sErrorMessage+=sFieldName + " exceeded the maximum length of " + maxLength + " characters \n";
		}
	}

	return(sErrorMessage);
}
function displayErrorMessage(sErrorMsg) 
{
	//win = window.open(",", 'popup', 'height = 200 width=200 toolbar = yes  titlebar=yes status = no resizeable=yes scrollbars=yes');
	//win.document.write("Data Validation Errors<br>");
	//win.document.write("" + sErrorMsg + "");
	sErrorMsg = "Please fill/correct this information:\r\n\r\n" + sErrorMsg;
	alert(""+ sErrorMsg + "");
}
