var dateVStr=''; //a workspace
var VBdate='';
var VEdate='';
var VBdateStr = '';
var VEdateStr = '';
var daysInMonths = new Array(12);
daysInMonths[0]=31; //Jan
daysInMonths[1]=28;
daysInMonths[2]=31;
daysInMonths[3]=30;
daysInMonths[4]=31;
daysInMonths[5]=30;
daysInMonths[6]=31;
daysInMonths[7]=31;
daysInMonths[8]=30;
daysInMonths[9]=31;
daysInMonths[10]=30;
daysInMonths[11]=31; //Dec

var MonthArray = new Array(12);
MonthArray[0] = "Jan";
MonthArray[1] = "Feb";
MonthArray[2] = "Mar";
MonthArray[3] = "Apr";
MonthArray[4] = "May";
MonthArray[5] = "Jun";
MonthArray[6] = "Jul";
MonthArray[7] = "Aug";
MonthArray[8] = "Sep";
MonthArray[9] = "Oct";
MonthArray[10] = "Nov";
MonthArray[11] = "Dec";
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='';
	for (xx=0; xx < document.siteemail_go.elements.length; xx++) {
	if (document.siteemail_go.elements[xx].type != 'checkbox' && document.siteemail_go.elements[xx].type != 'radio' && document.siteemail_go.elements[xx].type != 'button' && document.siteemail_go.elements[xx].type != 'hidden') {
		document.siteemail_go.elements[xx].style.backgroundColor='#FFFFFF';
		}
	}

    document.getElementById('Name').value = TrimSpaces(document.getElementById('Name').value);
	if (!VText('Name',document.getElementById('Name').value,'Y'))
		document.getElementById('Name').style.backgroundColor="#FAEBD7";
	
	try {
		var utitle = document.getElementById('GetTitle').value;
	} catch(err) {
		var utitle = "N";
	}
	if (utitle == "Y") {
	    document.getElementById('Title').value = TrimSpaces(document.getElementById('Title').value);
		if (!VText('Title',document.getElementById('Title').value,document.getElementById('GetTitleRequired').value))
			document.getElementById('Title').style.backgroundColor="#FAEBD7";
	}

	try {
		var ctitle = document.getElementById('GetCompanyInfo').value;
	} catch(err) {
		var ctitle = "N";
	}
	if (ctitle == "Y") {
	    document.getElementById('CompanyName').value = TrimSpaces(document.getElementById('CompanyName').value);
		if (!VText('Company name',document.getElementById('CompanyName').value,document.getElementById('GetCompanyRequired').value))
			document.getElementById('CompanyName').style.backgroundColor="#FAEBD7";
	}


	try {
		var umail = document.getElementById('GetMailingInfo').value;
	} catch(err) {
		var umail = "N";
	}
	if (umail == "Y") {
	    document.getElementById('MailingAddress1').value = TrimSpaces(document.getElementById('MailingAddress1').value);
		if (!VText('Mailing Address 1',document.getElementById('MailingAddress1').value,"Y"))
			document.getElementById('MailingAddress1').style.backgroundColor="#FAEBD7";
	    document.getElementById('MailingAddress2').value = TrimSpaces(document.getElementById('MailingAddress2').value);
		if (!VText('Mailing Address 2',document.getElementById('MailingAddress2').value,"N"))
			document.getElementById('MailingAddress2').style.backgroundColor="#FAEBD7";
	    document.getElementById('City').value = TrimSpaces(document.getElementById('City').value);
		if (!VText('City',document.getElementById('City').value,"Y"))
			document.getElementById('City').style.backgroundColor="#FAEBD7";
	    document.getElementById('Zip').value = TrimSpaces(document.getElementById('Zip').value);
		if (!VText('Zip',document.getElementById('Zip').value,"Y"))
			document.getElementById('Zip').style.backgroundColor="#FAEBD7";
	}


    document.getElementById('EmailAddress').value = TrimSpaces(document.getElementById('EmailAddress').value);
	if (!VEmail('Email Address',document.getElementById('EmailAddress').value,'Y'))
		document.getElementById('EmailAddress').style.backgroundColor="#FAEBD7";

	var uphone = document.getElementById('PhoneRequired').value;

	if (uphone != "X") {
	    document.getElementById('PhoneNumber').value = TrimSpaces(document.getElementById('PhoneNumber').value);
		if (!VPhone('Phone Number',document.getElementById('PhoneNumber').value,document.getElementById('PhoneRequired').value))
			document.getElementById('PhoneNumber').style.backgroundColor="#FAEBD7";
	}

    document.getElementById('Extension').value = TrimSpaces(document.getElementById('Extension').value);
	if (!VText('Extension',document.getElementById('Extension').value,"N"))
		document.getElementById('Extension').style.backgroundColor="#FAEBD7";

    document.getElementById('Purpose').value = TrimSpaces(document.getElementById('Purpose').value);
	if (!VText('Purpose',document.getElementById('Purpose').value,'Y'))
		document.getElementById('Purpose').style.backgroundColor="#FAEBD7";

    document.getElementById('EventDate').value = TrimSpaces(document.getElementById('EventDate').value);
	if (!VDate('Event Date',document.getElementById('EventDate').value,'Y'))
		document.getElementById('EventDate').style.backgroundColor="#FAEBD7";

    document.getElementById('Notes').value = TrimSpaces(document.getElementById('Notes').value);
	if (!VText('Notes',document.getElementById('Notes').value,'Y'))
		document.getElementById('Notes').style.backgroundColor="#FAEBD7";



    if (FullErMsg != '')
    {
        alert(FullErMsg);
        return false;
    } else {
        /*TextArea Update*/
        var tmp=KillSymbols(form.Notes.value);
        form.Notes.value=tmp;
        return true;
    }
	return true;
}
function TrimSpaces(str) {
	str = str.replace(/  /, "");
	var ns = '';
	//leading space trim
	var spcnt = 0;
	for (var i=0;i<str.length;i++) {
		if (str.charAt(i) == ' ') {
			spcnt++;
		} else {
			break;
		}
	}
	str = str.substr(spcnt,str.length - spcnt);
	//Trailing space trim
	spcnt = 0;
	for (var i=str.length - 1;i>0;i--) {
		if (str.charAt(i) == ' ') {
			spcnt++;
		} else {
			break;
		}
	}
	str = str.substr(0,str.length - spcnt);
	//Final runthrough to insure we have only single spaces in the string
	var lc = '';
	var tc = '';
	var ns = '';
	for (i =0;i< str.length;i++) {
		tc = str.substr(i,1);
		if (lc == ' ' && tc == ' ') {
		} else {
			ns+=tc;
		}
		lc = tc;
	}
	
	return ns;
}
/*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*/

/*VText End*/
function VOneDate(date, required) //date=mm/dd/yy or mm/dd/yyyy and required = "R"equired, "N"ot required, if blank "N"ot
{
	var erFlg = 0;
	var parseat = 0;
	var mon = '';
	var day = '';
	var year = '';
	erMsg='';
	if (date.length == 0)
	{
		erMsg="Required";
		return 1;
	}
	for (var i=0;i < date.length;i++)
	{
		if (date.charAt(i) != "/")
		{
			if (parseat == 0)
				mon+=date.charAt(i);
			if (parseat == 1)
				day+=date.charAt(i);
			if (parseat == 2)
				year+=date.charAt(i);
		} else {
			parseat++;
		}
	}
	mon--; //Change from 1-n to 0-(n-1) to use with array	
	var ly=ChkLeapYear(year);
	if (ly)
		daysInMonths[1]++;
	//Check out the values
	if (mon < 0 || mon > 11 || isNaN(mon))
	{
		erMsg+='Invalid month';
		erFlg++;
	}
	if (day < 1 || day > daysInMonths[mon] || isNaN(day))
	{
		erMsg+=' Invalid day';
		erFlg++;
	}
	//Peek at the year
	if (year.length == 2)
		year = '20' + year;
	if (year < 2000)
	{
		erMsg+=' Invalid year';
		erFlg++;
	}
	edateVStr = MonthArray[mon - 1] + ' ' + day + ', ' + year;
	return erFlg;
}
function ChkLeapYear(intYear) {
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0) { return true; }
	} else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}



/*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 false;
		}
	}
	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*/
/*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 false;
		}
	}
	if (required == 'N' || required =='')
	{
		if (email.length == 0)
		{
			return false;
		}
	}
	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*/
/*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 digits\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*/










/*VDate Begin*/
function VDate(field, tdate, required)
{
	erMsg='';
	erCnt=0;
	VOneDate(tdate,required);
	if (erMsg != '')
		FullErMsg+=field + ': Date errors - ' + erMsg + '\n';
	
}
/*VDate End*/
/*VBeginDate Begin*/
function VBeginDate(field, tdate, required)
{
	erFnd=0;
	edateVStr='';
	required = required.toUpperCase();
	if (required == 'N' && tdate.length ==0)
		return true;

	erFnd=VOneDate(tdate, required);
	VBdateStr=edateVStr; //save for reversal test
	VBdate=tdate;
	if (erFnd != 0)
	{
		FullErMsg+=field + ': ' + erMsg + '\n';
	}
}
/*VBeginDate End*/
/*VTextArea Begin*/
function VTextArea (field, text, required)
{
	required = required.toUpperCase();
	if (required == 'Y')
	{
		if (text.value == '' || text.length == 0)
		{
			FullErMsg+=field + ': An entry is required.\n';
			return false;
		}
	}
	if (required == 'N' || required == '')
	{
		if (text.length==0)
			return true;
	}
	var tcnt=0;
	for (var i=0;i<text.length;i++)
	{
		if (text.charAt(i) == ' ')
			tcnt++;
	}
	if (tcnt == text.length && text.length > 0)
	{
		if (required == 'Y')
		{
			FullErMsg+=field + ': A non-space entry is required.\n';
			return false;
		} else {
			FullErMsg+=field + ': All spaces is meaningless; please erase or reenter.\n';
			return false;
		}		
	}
	return true;
}
/*VTextArea 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;
    }
}
