var countiesWith2010HMOPPO = ['Alamance','Alexander','Alleghany','Ashe','Avery','Beaufort','Bertie','Bladen','Brunswick','Cabarrus','Caldwell','Carteret','Caswell','Catawba','Chatham',
						'Chowan','Columbus','Cumberland','Davidson','Davie','Duplin','Durham','Edgecombe','Forsyth','Franklin','Gaston','Gates','Granville','Greene','Guilford','Halifax',
						'Harnett','Haywood','Henderson','Hertford','Hoke','Hyde','Iredell','Johnston','Lee','Lincoln','Martin','Mecklenburg','Nash','New Hanover','Northampton','Onslow','Orange',
						'Pender','Perquimans','Person','Pitt','Polk','Randolph','Richmond','Robeson','Rockingham','Rowan','Sampson','Stanly','Stokes','Surry','Tyrrell','Wake','Warren',
						'Washington','Watauga','Wayne','Wilkes','Yadkin'];

var countiesWith2011HMOPPO = ['Alamance','Alexander','Alleghany','Ashe','Avery','Beaufort','Bertie','Bladen','Brunswick','Cabarrus','Caldwell','Carteret','Caswell','Catawba','Chatham',
						'Chowan','Cleveland','Columbus','Cumberland','Davidson','Davie','Duplin','Durham','Edgecombe','Forsyth','Franklin','Gaston','Gates','Granville','Greene','Guilford','Halifax',
						'Harnett','Haywood','Henderson','Hertford','Hoke','Hyde','Iredell','Johnston','Lee','Lincoln','Martin','Mecklenburg','Montgomery','Nash','New Hanover','Northampton','Onslow','Orange',
						'Pender','Perquimans','Person','Pitt','Polk','Randolph','Richmond','Robeson','Rockingham','Rowan','Sampson','Scotland','Stanly','Stokes','Surry','Tyrrell','Union','Wake','Warren',
						'Washington','Watauga','Wayne','Wilkes','Wilson','Yadkin'];

var countiesWith2012HMOPPO = ['Alamance','Alexander','Alleghany','Ashe','Avery','Beaufort','Bertie','Bladen','Brunswick','Cabarrus','Caldwell','Carteret','Caswell','Catawba','Chatham',
						'Chowan','Cleveland','Columbus','Cumberland','Davidson','Davie','Duplin','Durham','Edgecombe','Forsyth','Franklin','Gaston','Gates','Granville','Greene','Guilford','Halifax',
						'Harnett','Haywood','Henderson','Hertford','Hoke','Hyde','Iredell','Johnston','Lee','Lincoln','Martin','Mecklenburg','Montgomery','Nash','New Hanover','Northampton','Onslow','Orange',
						'Pender','Perquimans','Person','Pitt','Polk','Randolph','Richmond','Robeson','Rockingham','Rowan','Sampson','Scotland','Stanly','Stokes','Surry','Tyrrell','Union','Wake','Warren',
						'Washington','Watauga','Wayne','Wilkes','Wilson','Yadkin'];

//Use this function when specifying year to enroll. Make sure years (countiesWith...) are correct
/*
function getAvailablePlan(countyName, option) 
{
	if(option == 0)
	{
		var filename = "2011/nohmoppo.htm";
	}
	else if(option == 1)
	{
		var filename = "2012/nohmoppo.htm";
	}
	
	if(option == 0)
	{
		$.each(countiesWith2011HMOPPO,(function(i, n)
			{
				if(n == countyName && option == 0)
				{
					filename = "2011/hmoppo.htm";
				}
				else if(n == countyName && option == 1)
				{
					filename = "2012/hmoppo.htm";
				}
			}));
	}
	else if(option == 1)
	{
		$.each(countiesWith2012HMOPPO,(function(i, n)
			{
				if(n == countyName && option == 0)
				{
					filename = "2011/hmoppo.htm";
				}
				else if(n == countyName && option == 1)
				{
					filename = "2012/hmoppo.htm";
				}
			}));
	}
	
	location.href= 'http://' + location.host + "/content/medicare/shopper/" + filename;
}
*/

$(function(){
	$('#zipCode').keyup(function(event) {
		if ($(this).val().length >= 5){
			populateCounties(this, document.getElementById('countyOfResidence'));
			$('#errMsg').addClass('hide')
		}
	});
});

//Use this function when there is no year selection. Make sure to use correct year
function getAvailablePlan(countyName) 
{
	var filename = "2012/nohmoppo.htm";
	
	//make sure to use correct year countiesWith...
	$.each(countiesWith2012HMOPPO,(function(i, n)
			{
				if(n == countyName)
				{
					filename = "2012/hmoppo.htm";
				}
			}));
	
	location.href= 'http://' + location.host + "/content/medicare/shopper/" + filename;
}

function populateCountiesMS(zipField, selectControl) {
		fillCountyWithName(zipField.value, selectControl);
	
		if(selectControl.options.length > 1) {
			selectControl.focus();
			
		}
	}


function populateCounties(zipField,selectControl)
	{
		fillCountyWithName(zipField.value,selectControl);$('#selectwrapper').css('display','');$('#countyhtml').html('');
		if(selectControl.options.length>1)
			{
				
				$('#countycontainer').removeClass('hide');
				$('.countyform').removeClass('hide');/*$('#apply_error_box').show();$('#label-zip').removeClass('labelerror');*/
				$('.pixelArrow').removeClass('hide');
			}
		/*if($('#apply_error_box').html().indexOf("Select the county where you reside.")==-1)
			{
				$('#apply_error_box').append("<p>Select the county where you reside.</p>")}$(selectControl).addClass("required");selectControl.focus()
			}
		*/
			if(selectControl.options.length==1&&selectControl.value!='')
				{
					$('#countycontainer').removeClass('hide');
					$('.countyform').removeClass('hide');/*$('#label-zip').removeClass('labelerror');*/$('#selectwrapper').css('display','none');$('#countyhtml').html(selectControl.value);$(selectControl).removeClass("required");
					$('.pixelArrow').removeClass('hide');
				}
	}

function selectByValue(selControl,selValue)
{for(var i=0;i<selControl.options.length;i++){if(selControl.options[i].value==selValue){selControl.options[i].selected=true}}}


function validateRadio(form) {
		option1 = -1;
		for (i=0; i < form.enrollYear.length; i++) {
				if (form.enrollYear[i].checked) {
				option1 = i;
			}
		}
		if (option1 == -1) {
				$('#errMsg').removeClass('hide').html("<p>Please select the year you want your coverage to start.</p>");/*$(selectControl).addClass("required");selectControl.focus();*/
				return false;
			}
		else {
				getAvailablePlan(document.getElementById('countyOfResidence').value, option1);
				return false;
			}
	} 


function checkCountySelection(selectControl)
	{
		if(selectControl.options.length > 1 && selectControl.value != '') { //More than one county
			//use this function when selecting a year
			//validateRadio(document.GetMedSuppPlan);
			
			//remove this function when selecting a year
			getAvailablePlan(document.getElementById('countyOfResidence').value);
			return false;	
		}
		
		else if(selectControl.options.length > 1) { //More than one county
			selectControl.focus();
			$('#errMsg').removeClass('hide').html("<p>Select a county.</p>");
			return false;	
		}
		
		else if(selectControl.options.length == 0) { //No county
			$('#errMsg').removeClass('hide').html("<p>Enter valid North Carolina zip code.</p>");
			return false;	
		}
		
		else if(selectControl.options.length == 1 && (selectControl.value == '' || selectControl.value == '-Enter ZIP code-')) { //No county
			$('#errMsg').removeClass('hide').html("<p>Enter valid North Carolina zip code.</p>");
			return false;	
		}

		else if(selectControl.options.length == 1) { //One County 
			//use this function when selecting a year
			//validateRadio(document.GetMedSuppPlan);
			
			//remove this function when selecting a year
			getAvailablePlan(document.getElementById('countyOfResidence').value);
			return false;
		}
		
	}
