Event.observe(window, 'load', function() {	

	//Sets the default format of the form
	formset = 0;

	//Creates the cookie instance to retrieve or set the cookie
	jar = new CookieJar({
		expires:'',   // seconds
		path: '/'
		});

	//Attempts to retrieve a previous cookie
	mystate = jar.get('mystate');

	//Hides the state drop down since javascript is enabled
	//$('stateformfield').hide();


	//Checks to see if a cookie already exists
	if(mystate != null) {
	
	
	//Sets the form format variable to what the cookie specifies
	formset = mystate.form;
	
	//Sets the label for what state is held in the cookie
	$('chosenstate').innerHTML = mystate.label;
	
	$('state').value = mystate.name;
	
	$('selectedstate').value = mystate.name;
	
	//Changes the form based on the chosen state in the cookie
	newvalue = mystate.name
	
		if(newvalue == 'WI') {
			
				
			$('3980A').show(); // Next Stop College: Planning and Preparing for Ninth and Tenth Graders (reg to WI)
			$('3980').hide();
			
			$('3987A').show(); // Next Step College: Applying and Deciding for Eleventh and Twelfth Graders (reg to WI)
			$('3987').hide();
			
		} 
		
		
		if(newvalue == 'SD') {
			
			$('3984').show(); // The Native American Student Guide: Making Your College Plan
			$('3993').show(); // South Dakota Tuition List
			$('2833').show(); // Dakota Corps Scholarship Program Flyer
			$('3991').show(); // South Dakota Opportunity Scholarship Flyer
			
		} 
		
		if(newvalue == 'WY' || newvalue == 'MN' || newvalue == 'ND') {
			
			$('3984').show(); // The Native American Student Guide: Making Your College Plan
			
		} 
		
	
		//Enables the form for editing
		//var form = $('demoinfo');
		//form.enable();
	
		
		//Shows the state label to the user and gives them the option to change the state
		new Effect.Morph('changestatespacer', {
		style: 'height:20px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: 0
		});
		
		new Effect.Morph('changestatecontainer', {
		style: 'left:0px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: .5
		});
		
		$('changestatecontainer').appear({ duration: .5, delay: .5, from: 0, to: 1 });
		
		
		//removes the semi transparent cover over the form
		$('overlay').fade({ duration: .5, delay: 2, from:.4, to: 0 });
	
	} else {
	
		//if no cookie is found or the cookie has expired the following occurs
		
		//disable the form
		//var form = $('demoinfo');
		//form.disable();
	
		$('changestatecontainer').setStyle({
		left: '-40px'
		});
		
		$('overlay').setStyle({
		top: '-34px'
		});
		
		
		//Reveal the state picker
		Effect.SlideDown('statepick', { duration: .8, delay: .5 });
		$('overlay').appear({ duration: .5, delay: 0, from: 0, to: .4 });
		
		new Effect.Morph('overlay', {
		style: 'top:0px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: .5
		});

	}
	
	//Listen on the drop down to see which state is picked
	Event.observe('statelist', 'change', function() {
		
		$('changestatedecoy').focus();
												  
		var newvalue = $('statelist').value;
		var newname;
		
		if(newvalue == 'AL') {
			newname = 'Alabama';	
		}
		
		if(newvalue == 'AK') {
			newname = 'Alaska';	
		}
		
		if(newvalue == 'AZ') {
			newname = 'Arizona';	
		}
		
		if(newvalue == 'AR') {
			newname = 'Arkansas';	
		}
		
		if(newvalue == 'CA') {
			newname = 'California';	
		}
		
		if(newvalue == 'CO') {
			newname = 'Colorado';	
		}
		
		if(newvalue == 'CT') {
			newname = 'Connecticut';	
		}
		
		if(newvalue == 'DE') {
			newname = 'Delaware';	
		}
		
		if(newvalue == 'DC') {
			newname = 'District of Columbia';	
		}
		
		if(newvalue == 'FL') {
			newname = 'Florida';	
		}
		
		if(newvalue == 'GA') {
			newname = 'Georgia';	
		}
		
		if(newvalue == 'HI') {
			newname = 'Hawaii';	
		}
		
		if(newvalue == 'ID') {
			newname = 'Idaho';	
		}
		
		if(newvalue == 'IL') {
			newname = 'Illinois';	
		}
		
		if(newvalue == 'IN') {
			newname = 'Indiana';	
		}
		
		if(newvalue == 'IA') {
			newname = 'Iowa';	
		}
		
		if(newvalue == 'KS') {
			newname = 'Kanasas';	
		}
		
		if(newvalue == 'KY') {
			newname = 'Kentucky';	
		}
		
		if(newvalue == 'LA') {
			newname = 'Louisiana';	
		}
		
		if(newvalue == 'ME') {
			newname = 'Maine';	
		}
		
		if(newvalue == 'MD') {
			newname = 'Maryland';	
		}
		
		if(newvalue == 'MA') {
			newname = 'Massachusetts';	
		}
		
		if(newvalue == 'MI') {
			newname = 'Michigan';	
		}
		
		if(newvalue == 'MN') {
			newname = 'Minnesota';	
		}
		
		if(newvalue == 'MS') {
			newname = 'Mississippi';	
		}
		
		if(newvalue == 'MO') {
			newname = 'Missouri';	
		}
		
		if(newvalue == 'MT') {
			newname = 'Montana';	
		}
		
		if(newvalue == 'NE') {
			newname = 'Nebraska';	
		}
		
		if(newvalue == 'NV') {
			newname = 'Nevada';	
		}
		
		if(newvalue == 'NH') {
			newname = 'New Hampshire';	
		}
		
		if(newvalue == 'NJ') {
			newname = 'New Jersey';	
		}
		
		if(newvalue == 'NM') {
			newname = 'New Mexico';	
		}
		
		if(newvalue == 'NY') {
			newname = 'New York';	
		}
		
		if(newvalue == 'NC') {
			newname = 'North Carolina';	
		}
		
		if(newvalue == 'ND') {
			newname = 'North Dakota';	
		}
		
		if(newvalue == 'OH') {
			newname = 'Ohio';	
		}
		
		if(newvalue == 'OK') {
			newname = 'Oklahoma';	
		}
		
		if(newvalue == 'OR') {
			newname = 'Oregon';	
		}
		
		if(newvalue == 'PA') {
			newname = 'Pennsylvania';	
		}
		
		if(newvalue == 'PR') {
			newname = 'Puerto Rico';
		}
		
		if(newvalue == 'RI') {
			newname = 'Rhode Island';	
		}
		
		if(newvalue == 'SC') {
			newname = 'South Carolina';	
		}
		
		if(newvalue == 'SD') {
			newname = 'South Dakota';	
		}
		
		if(newvalue == 'TN') {
			newname = 'Tennessee';	
		}
		
		if(newvalue == 'TX') {
			newname = 'Texas';	
		}
		
		if(newvalue == 'UT') {
			newname = 'Utah';	
		}
		
		if(newvalue == 'VT') {
			newname = 'Vermont';	
		}
		
		if(newvalue == 'VI') {
			newname = 'Virgin Islands';	
		}
		
		if(newvalue == 'VA') {
			newname = 'Virginia';	
		}
		
		if(newvalue == 'WA') {
			newname = 'Washington';	
		}
		
		if(newvalue == 'WV') {
			newname = 'West Virginia';	
		}
		
		if(newvalue == 'WI') {
			newname = 'Wisconsin';	
		}
		
		if(newvalue == 'WY') {
			newname = 'Wyoming';	
		}
		
		//Checks to see if the item clicked is a state
		if(newvalue != '0') {
		
		//Enables the form
		//var form = $('demoinfo');
		//form.enable();
	
		//Shows the selected state above the form
		new Effect.Morph('changestatespacer', {
		style: 'height:20px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: 0
		});
		
		new Effect.Morph('changestatecontainer', {
		style: 'left:0px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: 0
		});
		
		new Effect.Morph('overlay', {
		style: 'top:-34px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: .5
		});
		
		$('changestatecontainer').appear({ duration: .5, delay: 0, from: 0, to: 1 });
		
		//Hides the state picker
		Effect.SlideUp('statepick', { duration: .5, delay: .5 });
		$('overlay').fade({ duration: .5, delay: 1, from:.4, to: 0 });
		$('chosenstate').innerHTML = newname;
		
		$('state').value = newvalue;
		
		$('selectedstate').value = newvalue;
		
		//Conditions on when to show certain parts of the form
		
		if(newvalue == 'WI') {
			$('3980A').show();  // Next Stop College: Planning and Preparing for Ninth and Tenth Graders (reg to WI)
			$('3980').hide();
			
			$('3987A').show(); // Next Step College: Applying and Deciding for Eleventh and Twelfth Graders (reg to WI)
			$('3987').hide();
			
			$('3984').hide();
			$('3993').hide(); // South Dakota Tuition List
			$('2833').hide(); // Dakota Corps Scholarship Program Flyer
			$('3991').hide();
			
		} else {
			$('3980A').hide(); // Next Stop College: Planning and Preparing for Ninth and Tenth Graders (reg to WI)
			$('3980').show();
			
			$('3987A').hide(); // Next Step College: Applying and Deciding for Eleventh and Twelfth Graders (reg to WI)
			$('3987').show();
		}
		
		if(newvalue == 'SD') {
			$('3984').show(); // The Native American Student Guide: Making Your College Plan
			$('3993').show(); // South Dakota Tuition List
			$('2833').show(); // Dakota Corps Scholarship Program Flyer
			$('3991').show(); // South Dakota Opportunity Scholarship Flyer
		} else {
			$('3984').hide(); // The Native American Student Guide: Making Your College Plan
			$('3993').hide(); // South Dakota Tuition List
			$('2833').hide(); // Dakota Corps Scholarship Program Flyer
			$('3991').hide(); // South Dakota Opportunity Scholarship Flyer
		}
		
		
		if(newvalue == 'WY' || newvalue == 'MN' || newvalue == 'ND') {
			
			$('3984').show(); // The Native American Student Guide: Making Your College Plan
			
		} 
		
		if(newvalue != 'SD' && newvalue != 'MN' && newvalue != 'ND' && newvalue != 'WI' && newvalue != 'WY') {
			$('3984').hide();
			$('3993').hide(); // South Dakota Tuition List
			$('2833').hide(); // Dakota Corps Scholarship Program Flyer
			$('3991').hide();
			$('3987A').hide();
			$('3980A').hide();
		}
		
		
		/*if(newvalue == 'Alaska' || newvalue == 'Arkansas') {
			if(formset != 2) {
			Effect.SlideDown('formfield1', { duration: .5, delay: 1 });
			Effect.SlideDown('formfield3', { duration: .5, delay: 1 });	
			formset = 2;
			}
		}*/
		
		//Sets the cookie based on the state selected
		statecookie = {name: $('statelist').value, form:formset, label: newname};
		jar.put('mystate', statecookie);
		
	}	
	
	
	});											   
	
	//Listens for the Change State link to be clicked
	Event.observe('changestate', 'click', function(event) {
		//var form = $('demoinfo');
		//form.disable();
		
		//$('top').focus();
												   
		Effect.SlideDown('statepick', { duration: .8, delay: .5 });
		$('overlay').appear({ duration: .5, delay: 0, from: 0, to: .4 });
		
		new Effect.Morph('overlay', {
		style: 'top:0px;', // CSS Properties
		duration: .5, // Core Effect properties
		delay: .5
		});
		
		Event.stop(event);
	});
	
	
	//Interval is set on page load to start object listener
	var heightinterval = setInterval("resized()", 10);

});


function resized() {
	
	var offsets = document.viewport.getScrollOffsets();
	var string = new String(offsets);
	string = string.replace(/\,+/g,'');
	
	
	widthset = $('printMargins').offsetWidth;
	widthset = widthset + 40;
	
	$('statepick').setStyle({
  	width: widthset+'px'
	});
	
	$('overlay').setStyle({
  	width: widthset+'px'
	});
	
	string2 = string - 148;
	//$('tempfield1').innerHTML = string;
	//$('tempfield2').innerHTML = string2;
	
	if(string > 114) {
	
	$('statepick').setStyle({
  	top: string2+'px'
	});
	
	
	} else {
		
		$('statepick').setStyle({
  		top: '-36px'
		});
		
	}
	
}



