			/* This JQuery code is attached to all forms. It initiates the form validation and runs
			some small functions that make sure the user is served the correct state/province pick-lists
			based on the country that they selected. By Ian and Steve 6/24/08 */
			
				var starFlag = false;				
			
				$(document).ready(function(){
			
							
					/* focus the cursor on the first text field on the page */			
					/* Had to disable b/c the new homepage feature pages have an iframe where the focus jerks the page down */
					$(":text:first").focus();
			
					cookiePrecheck();
									
					/* Validate the form using the JQuery validation plugin in /js/jquery.validate.js */
					$("#registration-form").validate({
							onkeyup: false,
							errorElement: "span",
							rules: {
								FirstName: "required",
								LastName: "required",
								Email: {
									required: true,
									email: true
								},
								Phone: "required",
								Company: "required",
								'country-select': "required",
								Title_Group__c: "required", 
								Industry: "required",
								NumberOfEmployees: "required"
							},
							messages: {
								FirstName: "",
								LastName: "",
								Email: "",
								Phone: "",
								Company: "",
								'country-select': "",
								'state-select': "",
								Title_Group__c: "", /* title */
								Industry: "",
								NumberOfEmployees: "",
								Country: "",
								tosAgreement: ""
							}
					});
			
			
					$("#country-select").change( function(){ countrySelect(); });
			
					$("#registration-form").submit( function() { onFormSubmit(); });
				
					/* removes the form error message when the form actually does validate */
					$("#registration-form input").add("#registration-form select").change( function() {
						if(starFlag == true){
							if ($("#registration-form").valid() != false){
									$(".form-error-message").css("display","none");	
							}		
						}
					});
					/* Passing variables for Download Button on Resource Landing Pages */
					if ($.cookie("registered") == ("yes")) {
							cookieResource()
							var downloadtype = $.cookie('download-type');
							var downloadtitle = $.cookie('download-title');
							var downloadlink = $.cookie('download-link');
							var downloadsize = $.cookie('download-size');
							if (downloadtype=="whitepaper"){
								$("#buttonDL").html("<a href='"+downloadlink+"'>Download whitepaper</a>");
							}
							else if (downloadtype=="datasheet"){
								$("#buttonDL").html("<a href='"+downloadlink+"'>Download datasheet</a>");
							}
							else if (downloadtype=="casestudy"){
								$("#buttonDL").html("<a href='"+downloadlink+"'>Download case study</a>");
							}
							else if (downloadtype=="analyst"){
								$("#buttonDL").html("<a href='"+downloadlink+"'>Download analyst report</a>");
							}
							else if (downloadtype=="survey"){
								$("#buttonDL").html("<a href='"+downloadlink+"'>Download survey results</a>");
							}
							else if (downloadtype=="webinar"){
								$("#buttonDL").html("<a href='"+downloadlink+"' target='_blank'>Launch webinar recording</a>");
							}
							else if (downloadtype=="other"){
								$("#buttonDL").html("<a href='"+downloadlink+"' target='_blank'>Download whitepaper</a>");
							}
							else if (downloadtype=="demo"){
								$("#buttonDL").html("<a href='"+downloadlink+"' target='_blank'>Launch demo</a>");
							}
					}
				/* For optional form fields. In July 2010 Jen needed an optional question added to forms to gather research data.
				   The question is added to "marketo_main.html", which is used on many forms. This Javascript makes the question only show up 
				   on specific pages based on the URL */
				var pathArray = window.location.pathname.split('/'); /* Put the URL segments into an array */
				if (((pathArray[pathArray.length-3]) == "whitepapers") || ((pathArray[pathArray.length-3]) == "datasheets") || ((pathArray[pathArray.length-3]) == "casestudies") || ((pathArray[pathArray.length-3]) == "analyst") || ((pathArray[pathArray.length-3]) == "studies") || ((pathArray[pathArray.length-2]) == "google") || ((pathArray[pathArray.length-3]) == "google") || ((pathArray[pathArray.length-2]) == "website")){
					$("form #fieldsetcontact .optional").css("display","block");
				}
			
				}); /* end document.ready */
					
			
				/* Country/State Dropdown Functions 
				---------------------------------------------------------------------------------------------------------------------
				What is does: displays the approptiate state field (US or Canada) and makes it d based on what the user picks. 
				To Deactivate: Simply change the ID names of the state dropdowns in the form you're using */
			
				function countrySelect() {
					if ($("#country-select").attr("value") == ("United States")){		   	/* If country "US" is selected */
						$("#us-state").attr("class","required");		   		/* make us-state a required field */
						$("#us-state").attr("value","");				   		/* make it empty */
						$("#us-form-list-element").fadeIn("slow");		   		/* fade it in */
						
						/* make canada-state not required and remove it */
						$("#canada-state").attr("class","");	
						$("#canada-state").attr("value","");
						$("#canada-form-list-element").fadeOut("fast"); 
					} 
					else if ($("#country-select").attr("value") == ("Canada")){  	/* If country "CA" is selected */
						$("#canada-state").attr("class","required");	   		/* make canada-state a required field */
						$("#canada-state").attr("value","");			   		/* make it empty */
						$("#canada-form-list-element").fadeIn("slow");	   		/* fade it in */
					
						/* make us-state not required and remove it */
						$("#us-state").attr("class","");
						$("#us-state").attr("value","");
						$("#us-form-list-element").fadeOut("fast");
					}
					/* Otherwise the user has selected another country. Assign the state value as generic "INTL" */
					else {
						$("#canada-state").attr("class","");
						$("#us-state").attr("class","");
						
						$("#us-form-list-element").fadeOut("fast");
						$("#canada-form-list-element").fadeOut("fast");
						$("#canada-state").attr("value","");				
						$("#State").attr("value","INTL");
					}
				}
				
				/* Cookie Precheck Function 
				---------------------------------------------------------------------------------------------------------------------
				What is does: if cookie-precheck is set to 'on' in the form, check if user already has Arena's cookie. If so, then send to the conversion page based on 'retURL' in the hidden fields for Eloqua
				How to activate: Insert this into the form: <input type="hidden" id="cookie-precheck" value="on" /> 
				Warning: This only works if there is a 'retURL' set as a hidden field in the form since that's the URL used to send the user to. This is done so the retURL is not as apparent to the user and for easy implementation */
				
				function cookiePrecheck() {
					/* If cookie-precheck is on */
					if ($("#cookie-precheck").attr("value") == ("on")){ 
						/* check if user is cookied */
						if ($.cookie("registered") == ("yes")) {
							/* if so, redirect their browser to the page with the offer on it */
							/*var returnURL = $("[name=retURL]").attr("value");
							window.location.href= ( $("[name=retURL]").attr("value") );*/
							$('#download').css("display","block");
							$('#form').css("display","none");
						}
						else {
							$('#form').css("display","block");
							$('#download').css("display","none");
						}			
					}
				}

				/* Things to do when form is submitted
				---------------------------------------------------------------------------------------------------------------------
				What is does:
				1) if cookie-onsubmit is set to 'on' in the form, upon clicking 'submit', check if the form validates. If so, cookie user. If not, do nothing.
				   How to activate: Insert this into the form: <input type="hidden" id="cookie-onsubmit" value="on" /> 
				2) if purchase-id-gen is set to 'on' in the form, upon clicking 'submit', check if the form validates. If so, add a random omniture purchase ID onto the 'retURL'
				   How to activate: Insert this into the form: <input type="hidden" id="purchase-id-gen" value="on" />
				3) The country on the form is a drop down but we need to send it as a  */
			
				function onFormSubmit(){
					/*if ($("#form-validation").attr("value") == ("off")){  
						$("#registration-form").valid() = true;
					}*/	
			
						/* check if the form is validated */	
						if ($("#registration-form").valid() == true){

							$("form button").after("<div class='loadingButton'>Please wait...</div>").css("display","none");

							/* if cookie-onsubmit is 'on' give the user the 'registered' cookie good for 90 days */
							if ($("#cookie-onsubmit").attr("value") == ("on")){
								$.cookie('registered', 'yes', { path: '/', expires: 90 });
								cookieResource()
							}
							/* if purchase-id-gen is 'on' add a random purchase ID to the retURL  */
							if ($("#purchase-id-gen").attr("value") == ("on")){ 
								var retURL = $("[name=retURL]").attr("value");
								var newURL = retURL + "?orderid=" + Math.ceil(100000000000*Math.random());
								$("[name=retURL]").attr("value", newURL);
							}
			
							/* Set 'country' (the text field that has to push to SF) to the value of 'country-select' (what the user selected in the drop-down */
							$("#Country").attr("value",($("#country-select").attr("value")));

							/* Sets the State field. Had to implement this when starting to use Marketo. State is populated with 'INTL' using the countrySelect function above if neither US or Canada is selected */
							if($("#Country").attr("value") == ("United States")){
								$("#State").attr("value",($("#us-state").attr("value")));
							}else if($("#Country").attr("value") == ("Canada")){
								$("#State").attr("value",($("#canada-state").attr("value")));
							}
							
			
							/*
if($("#Title_Group__c").attr("value") == ("Manufacturing - VP")){
									$.cookie("Manufacturing", "yes", { path: '/', expires: 90 });
							}
							if($("#Title_Group__c").attr("value") == ("Manufacturing - Manager")){
									$.cookie("Manufacturing", "yes", { path: '/', expires: 90 });
							}
							if($("#Title_Group__c").attr("value") == ("Manufacturing Engineer")){
									$.cookie("Manufacturing", "yes", { path: '/', expires: 90 });
							}
							if($("#Title_Group__c").attr("value") == ("Quality Assurance - Manager")){
									$.cookie("Quality Assurance", "yes", { path: '/', expires: 90 });
							}
							if($("#Title_Group__c").attr("value") == ("CEO/President/Owner")){
									$.cookie("CEO", "yes", { path: '/', expires: 90 });
							}
*/
						}
						else{
							$(".form-error-message").fadeIn("slow");
							if ($.browser.msie && $.browser.version <= 6 ){
								$(".error").css("border", "1px solid red");
								$("select.error").siblings().css("color","red");
							}
							starFlag = true;
						}
			
					if ($("#form-validation").attr("value") == ("off")){  
						$("#registration-form").valid() = true;
					}	
			
				}
				/* Setting Resource Page Cookies based on the Hidden Input Fields */
				function cookieResource(){
					if($("#download-type").attr("value")) {		
						var downloadtype = $("#download-type").attr("value");
						var downloadtitle = $("#download-title").attr("value");
						var downloadlink = $("#download-link").attr("value");
						var downloadsize = $("#download-size").attr("value");
						var opagename = $("#omniture-pagename").attr("value");
						var ochannel = $("#omniture-channel").attr("value");
						var ocampaign = $("#omniture-campaign").attr("value");
						var oproducts = $("#omniture-products").attr("value");
						$.cookie('download-type', downloadtype, { path: '/', expires: 90 });
						$.cookie('download-title', downloadtitle, { path: '/', expires: 90 });
						$.cookie('download-link', downloadlink, { path: '/', expires: 90 });
						$.cookie('download-size', downloadsize, { path: '/', expires: 90 });
						$.cookie('omniture-pagename', opagename, { path: '/', expires: 90 });
						$.cookie('omniture-channel', ochannel, { path: '/', expires: 90 });
						$.cookie('omniture-campaign', ocampaign, { path: '/', expires: 90 });
						$.cookie('omniture-products', oproducts, { path: '/', expires: 90 });
					}
				}
