function Validate()
{
	var form = document.getElementById("OptOutForm");
	ClearErrorMessages(form);
	
	var inputAccountNumber = form.elements["accountNumber"];
	var inputMonth = form.elements["month"];
	var inputDay = form.elements["day"];
	var inputYear = form.elements["year"];
	var inputOptOut = form.elements["optOut"];
	var bErrorFocus = false;
	
	for(var i = 0; i < form.elements.length; i++)
	{
		if(form.elements[i].type == "text" || form.elements[i].type == "select-one")
		{
			if(CheckRequired(form.elements[i], bErrorFocus))
				bErrorFocus = true;
		}
	}
	
	if(inputOptOut.checked == false)
	{
		ShowError(inputOptOut, "errorInField");
		document.getElementById("optOutError").style.display = "block";
		if(!bErrorFocus)
		{
			bErrorFocus = true;
			inputOptOut.focus();
		}
	}
	
	var dateReceived = new Date(inputYear.value,inputMonth.value - 1, inputDay.value);
	if(dateReceived == "Invalid Date" || isNaN(dateReceived))
	{
		ShowError(inputYear, "errorInField");
		ShowError(inputMonth, "errorInField");
		ShowError(inputDay, "errorInField");
		document.getElementById("badDateError").style.display = "block";
		if(!bErrorFocus)
		{
			inputMonth.focus();
			bErrorFocus = true;
		}
	}
	var now = new Date()
	
	if(!DateCompare(now, dateReceived))
	{
		ShowError(inputYear, "errorInField");
		ShowError(inputMonth, "errorInField");
		ShowError(inputDay, "errorInField");
		document.getElementById("futureDateError").style.display = "block";
		if(!bErrorFocus)
		{
			inputMonth.focus();
			bErrorFocus = true;
		}
	}
	var goodDstAccnt = CheckDstAccountNumber(inputAccountNumber);
	var goodCsgAccnt = CheckCsgAccountNumber(inputAccountNumber);
	if(!goodDstAccnt && !goodCsgAccnt)
	{
		ShowError(inputAccountNumber, "errorInField");
		document.getElementById("accountNumberError").style.display = "block";
		if(!bErrorFocus)
		{
			inputAccountNumber.focus();
			bErrorFocus = true;
		}
	}
	
	
	if(!bErrorFocus)
	{
		inputOptOut.value = inputOptOut.checked;
		form.submit();
	}
	else
	{
		var errorMessages = document.getElementById("errorMessages");
		errorMessages.style.display = "block";
		errorMessages.scrollIntoView(true);
	}
}

function CheckRequired(Field, ErrorFocused)
{
	if(Field.length && !Field.type)
	{
		for(var i = 0; i <Field.length; i++)
		{
			var Item = Field[i];
			if(Item.checked == true)
			{
				return false;
			}
				
		}
		Field[0].focus();
		ShowError(Field, "errorInField");
		return true;
	}
	if(!Field.value || Field.value.trim() == null || Field.value.trim().length == 0)
	{
		ShowError(Field, "errorInField");
		Field.value = "";
		if(!ErrorFocused)
		{
			document.getElementById("requiredFieldsError").style.display = "block";
			Field.focus();
		}
		return true;
	}
	else
		return false;
}

function GetLabelNodeForField(Field)
{
	var labels = document.getElementsByTagName("label");
	var fieldId = Field.id;
	if(fieldId != null)
	{
		for(var l = 0; l < labels.length; l++)
		{
			var label = labels[l];
			if(label.htmlFor == fieldId)
			{
				return label;
			}
		}
	}
	return null;
}

function ShowError(Field, ErrorClassName)
{
	if(Field.length && !Field.type)
	{
		for(var i = 0; i < Field.length; i++)
		{
			
			var Item = Field[i];
			var Label = GetLabelNodeForField(Item);
			if(Label != null)
			{
				var sClass = new String(Label.className);
				if ( sClass.indexOf(ErrorClassName) == -1 )
				{
					sClass += " " + ErrorClassName;
					UpdateClassAttribute(Label, sClass);
				}	
			}	
		}
	}
	else if (Field.tagName == "SELECT")
	{
		Field = Field.parentNode;
		var sClass = new String(Field.className);
		if ( sClass.indexOf(ErrorClassName) == -1 )
		{
			sClass += " " + ErrorClassName;
			UpdateClassAttribute(Field, sClass);			
		}
	}
	else
	{
		var sClass = new String(Field.className);
		if ( sClass.indexOf(ErrorClassName) == -1 )
		{
			sClass += " " + ErrorClassName;
			UpdateClassAttribute(Field, sClass);
		}
	}
}

function ClearErrorMessages(Form)
{
	var errorMessages = document.getElementById("errorMessages");
	
	ClearMessage(errorMessages);
	
	var mainDiv = document.getElementById("content");
	
	var listFields = mainDiv.getElementsByTagName("input");
	var listLabels = document.getElementsByTagName("label");
	var listSelect = document.getElementsByTagName("div");	
	ClearList(listFields);
	ClearList(listLabels);
	ClearList(listSelect);
}

function ClearMessage(MessageContainer)
{
	var list = MessageContainer.getElementsByTagName("DIV");
	for(var i = 0; i < list.length; i++)
	{
		list[i].style.display = "none";
	}
	MessageContainer.style.display = "none";
}

function UpdateClassAttribute(Field, Value)
{
	Field.className = Value;
}

function ClearList(List)
{
	for(var i in List)
	{
		if(typeof List[i] == 'object')
		{
			var Field = List[i];
		
			var sClass = new String(Field.className);
			if(sClass.indexOf("errorInField") > -1)
			{
				sClass = sClass.replace(new RegExp(" errorInField\\b"), "");
				UpdateClassAttribute(Field, sClass);
			}
		}
	}
}


// returns true if FirstDate >= SecondDate otherwise false
function DateCompare (FirstDate, SecondDate)
{
	var firstYear = FirstDate.getFullYear();
	var firstMonth = FirstDate.getMonth();
	var firstDay = FirstDate.getDate();
	
	var secondYear = SecondDate.getFullYear();
	var secondMonth = SecondDate.getMonth();
	var secondDay = SecondDate.getDate();
	
	if(firstYear > secondYear)
		return true;
		
	if(firstYear >= secondYear && firstMonth > secondMonth)
		return true;
	
	if(firstYear >= secondYear && firstMonth >= secondMonth && firstDay >= secondDay)
		return true;
	
	return false;
}


/*
 * Calendar code
 */
 
 
function toggleCalendar()
{
	var calendar = document.getElementById("calendar");
	if(calendar.style.display == "none")
	{
		calendar.style.display = "block";
	}
	else
	{
		calendar.style.display = "none";
	}
}
 
var todaydate=new Date();
var curmonth=todaydate.getMonth()+1; //get current month (1-12)
var curyear=todaydate.getFullYear(); //get current year
var selectedDay = 0;
var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
			
//headClass defines which color class to used for the month and year header (e.g. 'customers', 'shop')
function Calendar(m, y){
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1);
	oD.od=oD.getDay()+1;
	
	var todaydate=new Date()
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0

	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0)) ? 29 : 28;
	//Because IE sets innerHTML as read-only for most table elements, it was necessary to encapsulate the entire 
	//table here so it could be written back to the page dynamically.
	var t='<table class="cMain" cols="7" cellpadding="0" border="0" cellspacing="0"><colgroup ><col class="ColCalendar"/><col class="ColCalendar"/><col class="ColCalendar"/><col class="ColCalendar"/><col class="ColCalendar"/><col class="ColCalendar"/><col class="ColCalendar"/></colgroup><tbody id="calender"><tr>';
	t+='<td class="cMonth cButton" colspan="2"><a href="#" onclick="updateCalDownMonth();return false;" title="Previous month">&lt;</a></td>';
	t+='<td class="cMonth" align="center" colspan="3" id="cHeader">' + mn[curmonth-1] + ' ' + curyear + '</td>';
	t+='<td class="cMonth cButton" colspan="2"><a style="float:right;" href="#" onclick="updateCalUpMonth();return false;" title="Next month">&gt;</a></td>';
	t += '</tr><tr align="center">';
	for(s=0;s<7;s++)t+='<td class="cwDay">'+"SMTWTFS".substr(s,1)+'</td>';
	t+='</tr><tr align="center">';
	for(i=1;i<=42;i++){
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&#160;';
		if(todaydate.getDate() > x || (todaydate.getMonth()+1 > m || todaydate.getFullYear() > y))
		{
		    
		    if(x != '&#160;')
		    {
				x='<a href="" onclick="return updateCalendarFields('+x+','+m+','+y+');">'+x+'</a>';
		    }
			t +='<td class="cDay">'+x+'</td>'
		    
		}
		else
		{
		    t+='<td class="cDayPast">'+x+'</td>';
		}
		if(((i)%7==0)&&(i<36))
			t+='</tr><tr align="center">';
	}
	return t+='</tr></tbody></table>';
}

function updateCalendarFields(sDay, sMonth, sYear)
{
	var form = document.getElementById("OptOutForm");
	
	var inputMonth = form.elements["month"];
	var inputDay = form.elements["day"];
	var inputYear = form.elements["year"];
	
	inputMonth.value = sMonth;
	inputDay.value = sDay;
	inputYear.value = sYear;
	
	toggleCalendar();
	
	return false;
}
			
function updateCalUpMonth(){
	if(!((todaydate.getMonth()+1 == curmonth) && (todaydate.getFullYear() == curyear)))
	{
		if(curmonth >= 12){
			curmonth = 1;
			curyear++;
		} else {
			curmonth++;
		}
		document.getElementById('calendar').innerHTML = Calendar(curmonth ,curyear);
	}
}

function updateCalUpYear(){
	curyear++;
	document.getElementById('calendar').innerHTML = Calendar(curmonth ,curyear);
}

function updateCalDownMonth(){
	
	if(curmonth <= 1){
		curmonth = 12;
		curyear--;
	} else {
		curmonth--;
	}
	document.getElementById('calendar').innerHTML = Calendar(curmonth ,curyear);
}
		
function updateCalDownYear(){
	curyear--;
	document.getElementById('calendar').innerHTML = Calendar(curmonth ,curyear);
}



function clearOptions(sID){
	var len = document.getElementById(sID).length;
	for(i = len; i > 0; i--){
		document.getElementById(sID).options[i-1] = null;
	}
}