var FullErMsg = ''; // a final delivery
var erFnd = 0;
var erMsg = ''; //a workspace

var textSymbols = new Array(5);
textSymbols[0]='"';
textSymbols[1]='|';
textSymbols[2]='~';
textSymbols[3]='`';
textSymbols[4]='\'';
var htmlSymbols = new Array(5);
htmlSymbols[0]='&quot;';
htmlSymbols[1]='&#124;';
htmlSymbols[2]='&#126;';
htmlSymbols[3]='&#96;';
htmlSymbols[4]='&#39;';

function ValidateForm(form, required)
{
	FullErMsg='';
	VText('Name',form.Name.value,'Y');
	VText('Address',form.Address.value,'Y');
	VText('City State Zip',form.CityStateZip.value,'Y');
	VEmail('Email Address',form.EmailAddress.value,'Y');
	VPhone('Daytime Phone Number',form.DaytimePhoneNumber.value,'Y');
	VNumber('Nbr Of Books',form.NbrOfBooks.value,'','$%@!.,',2,'Y');
	VNumber('Total Pages',form.TotalPages.value,'','$%@!.,',2,'Y');
	VNumber('B Wpages',form.BWpages.value,'','$%@!.,',2,'Y');
	VText('Color Pages',form.ColorPages.value,'Y');
	VText('Size',form.Size.value,'Y');


    if (FullErMsg != '')
    {
        alert(FullErMsg);
        return false;
    } else {
        /*TextArea Update*/
        return true;
    }
	return true;
}
/*VText Begin*/
function VText(field, text, required)
{
	required = required.toUpperCase();
	if (required == 'Y')
	{
		if (text == '' || text.length == 0)
		{
			FullErMsg+=field + ': An entry is required.\n';
			return false;
		}
	}
	if (required == 'N' && text.length == 0)
		return true;
	//Are we all spaces?
	var tcnt=0;
	for (var i=0; i < text.length; i++)
	{
		if (text.charAt(i) == ' ')
		{
			tcnt++;
		}
	}
	if (tcnt == text.length)
	{
		if (required == 'Y')
		{
			FullErMsg+=field + ': A non-space entry is required.\n';
			return false;
		} else {
			FullErMsg+=field + ': An all-space entry is meaningless; erase or reenter.\n';
			return false;
		}
	}
	return true;
}
/*VText End*/
/*VEmail Begin*/
function VEmail(field,email,required)
{
	required = required.toUpperCase();
	if (required == 'Y')
	{
		if (email.value == '' || email.length == 0)
		{
			FullErMsg+= field + ': Entry required\n';
			return true;
		}
	}
	if (required == 'N' || required =='')
	{
		if (email.length == 0)
		{
			return true;
		}
	}
	var atCnt=0;
	var dotCnt=0;
	for (var i = 0;i < email.length; i++)
	{
		if (email.charAt(i) == '@')
			atCnt++;
		if (email.charAt(i) == '.')
			dotCnt++;
	}
	if (atCnt == 0 || dotCnt ==0)
	{
		FullErMsg+= field + ': Address missing \"\@\" or \"\.\" or both\n';
		return false;
	}
	if (email.length == 5 || email.length < 5)
	{
		FullErMsg+= field + ': Email address too short to be valid\n';
		return false;
	}
	return true;
}
/*VEmail End*/
/*VPhone Begin*/
function VPhone(field, phone, required)
{
	required = required.toUpperCase();
	if (required == 'Y')
	{
		if (phone.value == '' || phone.length == 0)
		{
			FullErMsg+= field + ': Entry required\n';
			return true;
		}
	}
	if (required == 'N' || required =='')
	{
		if (phone.length == 0)
		{
			return true;
		}
	}
	var i=0;
	if (phone.length < 10)
	{
		FullErMsg+= field + ': Phone Number too short to be valid: must include Area Code.\n';
		return false;
	}
	//See if someone entered a 1-800/1-900 number, which would make the entry a special format
	var nNbr = '';
	if (phone.charAt(0) == '1' && (phone.charAt(1) == '-' || phone.charAt(1) == '('))
	{
		//strip the 1- prefix
		for (i = 2; i < phone.length;i++)
		{
			nNbr+=phone.charAt(i);
		}
		phone = nNbr;
		nNbr='';
	}
		
	//make up a new number with known formatting
	var pSep=0; //counter
	var noNbr=0; //signal flag
	var fChr=''; //special test
	nNbr=''; //new number; reused
	mCnt=0;
	for (i=0;i<phone.length;i++)
	{
		if (phone.charAt(i) != '\.' && phone.charAt(i) != '-' && phone.charAt(i)!='(' && phone.charAt(i)!=')' && phone.charAt(i)!=' ')
		{
			if (isNaN(phone.charAt(i)))
			{
				noNbr=1;
			} else {
				nNbr+=phone.charAt(i);
				pSep++;
				if (pSep == 3 || pSep == 6)
					nNbr+='-';
				if (fChr == '')
				{
					fChr = phone.charAt(i); //save first digit
				} else {
					if (fChr == phone.charAt(i))
						mCnt++;
				}
			}
		}
	}
	if (noNbr == 1)
	{
		FullErMsg+=field + ': Invalid character or symbol in number\n';
		return false
	}
	mCnt+=3; //adjust for formatted size (2x "-" and 1x oversize)
	if (mCnt== nNbr.length)
	{
		FullErMsg+=field + ': Unlikely; all numbers are the same\n';
		return false;
	}
	if (nNbr.length < 12)
	{
		FullErMsg+=field + ': Number too short (missing Area Code?)\n';
		return false
	}
	if (nNbr.length > 12)
	{
		FullErMsg+=field + ': Number too long (extra digits seen)\n';
		return false;
	}
	return true;
				
}
/*VPhone End*/
/*VNumber Begin*/
function VNumber(field,text,ChrsAllowed,ChrsDisallowed,dps,required)
{
	//initially, numbers are treated as text
	required = required.toUpperCase();
	if (required == 'Y')
	{
		if (text.value == '' || text.length == 0)
		{
			FullErMsg+=field + ': An entry is required.\n';
			return false;
		}
	}
	if (required == 'N' && text.length ==0)
		return true;
	//Are we all spaces?
	var spcCnt=0;
	var noNbr=0; //When charcter is not a number and not an allowed special character...
	var dpflg=0; //flag to track number of decimal points
	var dpval=''; //the digits after a "."
	var disac=0; //disallowed character count
	for (var i=0; i < text.length; i++)
	{
		if (text.charAt(i) == ' ')
		{
			spcCnt++;
		}
		//Characters that CAN be in the entry (optional ones)
		var pf = 0;
		for (var k=0; k<ChrsAllowed.length;k++) //scan for allowed characters
		{
			if (text.charAt(i) == ChrsAllowed.charAt(k))
				pf=1; //flag it
		}
		if (pf != 1 && isNaN(text.charAt(i)))
			noNbr++;
			
		//Characters that shouldn't be in the entry		
		
		for (var k=0; k<ChrsDisallowed.length;k++)
		{
			if (text.charAt(i) == ChrsDisallowed.charAt(k))
				disac++; //flag it
		}
		//save dp digits
		if (text.charAt(i) == '.')
		{
			dpflg = 1;
		} else {
			if (dpflg == 1)
			{
				dpval+=text.charAt(i);
			}
		}
	}
	if (spcCnt == text.length)
	{
		if (required == 'Y')
		{
			FullErMsg+=field + ': A non-space entry is required.\n';
			return false;
		} else {
			FullErMsg+=field + ': Entry cannot be all spaces (" ").\n';
		}
	}
	if (disac !=0)
	{
		//format the characters
		var fmtDA = '';
		for (i = 0;i<ChrsDisallowed.length - 1;i++)
		{
			fmtDA+=ChrsDisallowed.charAt(i) + ' ';
		}
		fmtDA+=ChrsDisallowed.charAt(i);
		FullErMsg+=field + ': Number cannot contain the characters: ' + fmtDA + '\n';
		return false;
	}
	if (noNbr !=0)
	{
		FullErMsg+=field + ': Number contains characters other than: ' + ChrsAllowed + '\n';;
		return false;
	}

	if (dpval.length != dps && dpflg == 1)
	{
		FullErMsg+=field + ': Too many digits after decimal point (only ' + dps + ' allowed)';
		return false;
	}

	return true;
}
/*VNumber End*/

function KillSymbols(value)
{
	wrk = '';
	for (var i=0;i<value.length;i++)
	{
		if (value.charAt(i) == textSymbols[0])
			wrk+=htmlSymbols[0]
		else if (value.charAt(i) == textSymbols[1])
			wrk+=htmlSymbols[1]
		else if (value.charAt(i) == textSymbols[2])
			wrk+=htmlSymbols[2]
		else if (value.charAt(i) == textSymbols[3])
			wrk+=htmlSymbols[3]
		else if (value.charAt(i) == textSymbols[4])
			wrk+=htmlSymbols[4]
		else if (value.charAt(i) == textSymbols[5])
			wrk+=htmlSymbols[5]
		else 
			wrk+=value.charAt(i);
	}
	return wrk;
}
function ChkDatesRev(from, to) {
	if (Date.parse(from) <= Date.parse(to))
	{
		return true;
	} else {
		return false;
    }
}
var tglFlag=0;
function ToggleBkg(cell) {
   box=document.getElementById(cell);
   if (tglFlag == 0)
     {
     tglFlag=1;
     box.style.backgroundColor="AliceBlue";
 } else {
     tglFlag=0;
     box.style.backgroundColor="";
 }
}
