function state_dd_change(dd, brand)
{

	var box_naming_convention = "messaging_box_";

	//Hide all messaging
	for (var x=1; x<=dd.options.length; x++)
	{
		var my_id = box_naming_convention + x;
		var target = document.getElementById(my_id);
		target.style.display = "none";
	}

	//Get the value of the item selected
	var category_id = dd.selectedIndex;


	//show selected messaging box div
	document.getElementById(box_naming_convention + category_id).style.display = "block";
	
	
	//show select dropdown 
	var target = document.getElementById("secondary_dropdown_container");
	
	//Clear its contents
	target.innerHTML = "";
	
	//Get the value of the dropdown in case we have to populate the hidden input
	var primary_dd_value = dd.options[dd.selectedIndex].value;
	
	//populate the dropdown with stuff
	switch(primary_dd_value)
	{
		case "General - Other No Selection":
			//--Select--
			create_and_add_hidden_input(target, primary_dd_value);
			break;
			
		case "Immediate Need":
			//Death has just occurred or is near
			create_and_add_hidden_input(target, primary_dd_value);
			break;
	

		case "Feedback - Other":
			//Concerns &amp; Feedback
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Compliment",						"Feedback - Send Compliment");
			dd_add_option(new_dd, "Cemetery Policies and Procedures",			"Feedback - Cemetery policies and procedures-upkeep");
			dd_add_option(new_dd, "Marker, Vault, Mausoleum Concerns",			"Feedback - Cemetery marker vault mausoleum bench");
			dd_add_option(new_dd, "Staff / Employee Concerns",				"Feedback - Communication/Staff Concerns");
			dd_add_option(new_dd, "Cremation Issues",					"Feedback - Cremation Issues");
			dd_add_option(new_dd, "Sold Location / Non-SCI Firm",				"Feedback - Sold Location / Non-SCI Firm");
			dd_add_option(new_dd, "Funeral Home Concerns",					"Feedback - Funeral Home Concerns");
			dd_add_option(new_dd, "Website Issues / Questions",				"Feedback - Web Issue");
			dd_add_option(new_dd, "Request No Contact",					"Feedback - Discontinue Contact");
			dd_add_option(new_dd, "Complaint",						"Feedback - Complaint");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;


		case "Funeral Cemetery Planning":
			//Funeral/Cemetery Planning
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Make an appointment",					"Appointment Request");
			if (brand != 'dmc')
			{
				dd_add_option(new_dd, "Request a Veteran's Planning Guide",		"Veterans Planning Guide Request");
			}
			dd_add_option(new_dd, "Request a Personal Planning Guide",			"Planning Guide Request");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;
			
			
		case "Obituary":
			//Obituaries/Genealogy
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",          					"");
			dd_add_option(new_dd, "Obituary",						"Obituary");
			dd_add_option(new_dd, "Genealogy",						"Genealogy");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;

			
			
		case "B2B - Other":
			//Business To Business Inquiry
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Vendor Proposal - Supplies",				"Vendor Proposal - Supplies");
			dd_add_option(new_dd, "Vendor Proposal - Travel",				"Vendor Proposal - Travel");
			dd_add_option(new_dd, "Marketing/Advertising/Media",				"Vendor Proposal - Marketing/Advertising/Media");
			dd_add_option(new_dd, "Interested in becoming an SCI Firm",			"B2B - Become SCI Firm");
			dd_add_option(new_dd, "Interested in becoming an SCI Affiliate",		"B2B - Become SCI Affiliate");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;			
			
		case "Meeting Request":
			//Request a Meeting
			create_and_add_hidden_input(target, primary_dd_value);
			break;
			
			
		case "Community - Other":
			//Community Programs
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Five Wishes",						"Community - Five Wishes");
			dd_add_option(new_dd, "Dignity Memorial Escape School",				"Community - Escape School");
			dd_add_option(new_dd, "Public Servants Program",				"Community - Public Servants Program");
			dd_add_option(new_dd, "Hospice",						"Community - Hospice");
			dd_add_option(new_dd, "Dignity Memorial Guidance Series",			"Community - Dignity Memorial Guidance Series");
			dd_add_option(new_dd, "Dignity Memorial LIFT",					"Community - LIFT");
			dd_add_option(new_dd, "Dignity Memorial Smart & Safe Living",			"Community - Smart and Safe Living");
			dd_add_option(new_dd, "Dignity Memorial Vietnam Wall",				"Community - Vietnam Wall");
			dd_add_option(new_dd, "American Diabetes Association",				"Community - American Diabetes Association");
			dd_add_option(new_dd, "Planning Ahead Seminars",				"Community - Planning Ahead Seminars");			
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;			
			
		case "Benefits - Other":
			//Dignity Memorial Services & Benefits
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Bereavement Travel",					"Benefits - Bereavement Travel");
			dd_add_option(new_dd, "Compassion Helpline",					"Benefits - Compassion Helpline");
			dd_add_option(new_dd, "100% Service Guarantee",					"Benefits - 100% Service Guarantee");
			dd_add_option(new_dd, "Child/Grandchild Protection",				"Benefits - Child/Grandchild Protection");
			dd_add_option(new_dd, "Aftercare Planner",					"Benefits - Aftercare Planner");
			dd_add_option(new_dd, "Aftercare Planner Technical Help",			"Benefits - Aftercare Planner Technical Assistance");
			dd_add_option(new_dd, "Omnilaw Legal Plans",					"Benefits - Omnilaw Legal Plans");
			dd_add_option(new_dd, "Everlasting Memorial",					"Benefits - Everlasting Memorial");
			dd_add_option(new_dd, "Away From Home Travel Protection",			"Benefits - Away From Home Protection / Travel Protection Plan");				
			dd_add_option(new_dd, "National Transferability",				"Benefits - National Transferability");				
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;			
			
			
		case "Veterans - Other":
			//Veteran's Resources
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Homeless Veterans Burial Program",			"Veterans - Homeless Veterans Burial Program");
			dd_add_option(new_dd, "VFW Seminar",						"Veterans - VFW Seminar");
			dd_add_option(new_dd, "Dignity Memorial Vietnam Wall",				"Veterans - Dignity Memorial Vietnam Wall");
			dd_add_option(new_dd, "American Legion Seminar",				"Veterans - American Legion Seminar");
			dd_add_option(new_dd, "Veterans Benefit Information",				"Veterans Planning Guide Request");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;			
			
			
		case "Products - Other":
			//Products
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Caskets",						"Caskets");
			dd_add_option(new_dd, "Purchase / Sell Cemetery Plots",				"CemeteryPlots");
			dd_add_option(new_dd, "Cremation Containers",					"Products - Cremation Containers");
			dd_add_option(new_dd, "Urns",							"Products - Urns");
			dd_add_option(new_dd, "Grave Markers",						"Products - Grave Markers");			
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);
			break;			
			
			
		case "Contract - Other":
			//Contract Billing / Payment Information
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "General Contract Questions",				"General - Contract Questions");
			dd_add_option(new_dd, "Payment/Billing Question",				"Contract - Payment Question");
			dd_add_option(new_dd, "Payment Arrangements",					"Contract - Payment Arrangements");
			dd_add_option(new_dd, "Change of Address",					"Contract - Change of Address");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);	
			break;			
			
			
		case "General - Insurance Questions":
			//Insurance Questions
			create_and_add_hidden_input(target, primary_dd_value);
			break;			
			
		case "Employment":
			//Employment Inquiries
			create_and_add_hidden_input(target, primary_dd_value);
			break;			
			
			
		case "Death Certificates - Other":
			//Death Certificates
			var new_dd = create_dropdown();
			dd_add_option(new_dd, "--Select--",						"");
			dd_add_option(new_dd, "Get Additional Copies",					"Death Certificates - Get Additional Copies");
			dd_add_option(new_dd, "Request Corrections",					"Death Certificates - Request Corrections");
			target.innerHTML = "<p>Choose a sub-category:</p>";
			target.appendChild(new_dd);	
			break;				
	
			
			
		case "General - Other":
			//Other	
			create_and_add_hidden_input(target, primary_dd_value);
			break;			
			
			
	}

}

function create_dropdown()
{
	//Create a new dropdown
	var new_dd = document.createElement('select');
	new_dd.setAttribute("class", "select_category");
	new_dd.setAttribute("id", "inquiry");
	new_dd.setAttribute("name", "00N700000025HUU");
	return new_dd;
}


function create_and_add_hidden_input(target, value)
{
	var new_input = document.createElement('input');
	new_input.setAttribute("type", "hidden");
	new_input.setAttribute("id", "inquiry");
	new_input.setAttribute("name", "00N700000025HUU");
	new_input.setAttribute("value", value);
	target.appendChild(new_input);
}


function dd_add_option(dd, text, value)
{
	var new_option = document.createElement('option');
	new_option.setAttribute('value', value);
	new_option.appendChild(document.createTextNode(text));
	dd.appendChild(new_option);
	
}










function submit_form()
{

	var my_primary_dropdown = document.getElementById("primary_dropdown");
	var my_first_name = document.getElementById("first_name");
	var my_last_name = document.getElementById("last_name");
	var my_address = document.getElementById("address");
	var my_city = document.getElementById("city");
	var my_state = document.getElementById("state");
	var my_zip = document.getElementById("zip");
	var my_phone = document.getElementById("phone");
	var my_email = document.getElementById("email");
	var my_message = document.getElementById("message");
	
	if (my_primary_dropdown.selectedIndex == 0)
	{
		alert("Please select a type of inquiry.");
	}
	else if (is_second_dropdown_bad())
	{
		alert("Please select an inquiry sub-type.");
	}
	else if (dm20_trim(my_first_name.value) == "")
	{
		alert("Please enter your First Name");
	}
	else if (dm20_trim(my_last_name.value) == "")
	{
		alert("Please enter your Last Name");
	}
	else if (dm20_trim(my_address.value) == "")
	{
		alert("Please enter your Address");
	}
	else if (dm20_trim(my_city.value) == "")
	{
		alert("Please enter your City");
	}
	else if (dm20_trim(my_state.value) == "")
	{
		alert("Please enter your State or Province");
	}
	else if (!is_valid_zip(my_zip.value))
	{
		alert("Please enter a valid Zip/Postal code.");
	}
	else if (dm20_trim(my_phone.value) == "")
	{
		alert("Please enter your Phone Number.");
	}
	else if (!is_valid_email(my_email.value))
	{
		alert("Please enter a valid Email Address.");
	}
	else if (my_message.value.length > 10000)
	{
		alert("Your message is " + my_message.value.length + " characters long. Please limit your message to 10,000 characters.");
	}		
	else
	{
		var tar = document.getElementById("universal_contact");
		tar.submit();
	}
}


function is_second_dropdown_bad()
{
	var my_inquiry = document.getElementById("inquiry");
	
	//If the secondary dropdown doesn't exist, it's not bad
	if (my_inquiry == null)
		return false;
	
	if (my_inquiry.tagName.toLowerCase() == "select" && my_inquiry.selectedIndex==0)
	{
		return true;
	}
	
	return false;
}



function is_valid_zip(str) 
{
	str = str.replace(/\s+/g,'');
	
	if (str.length == 5)
	{
		//american postal code
		
		if (isNumeric(str))
		{
			return true;
		}		
	}
	else if (str.length == 6 || str.length == 7)
	{
		//Canadian Postal code
		if (isAlphaNumeric(str))
			return true;
	}
	
	return false;
}



function isAlphaNumeric(str)
{
  var re = /[^a-zA-Z0-9]/g
  if (re.test(str)) return false;
  return true;
}

function isNumeric(str)
{
  var re = /[^0-9]/g
  if (re.test(str)) return false;
  return true;
}







function is_valid_email(str) 
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}



//we now have a list that loses then regains focus everytime a key is pressed (while it has focus), i.e. using the keyboard to move up or down through the options now triggers a split-second loss and regaining of focus, which is enough to trigger the 'onchange()' in firefox. I love a good easy fix!
function force_onchange_when_using_keyboard(t)
{
	t.blur();
	t.focus();
}



function pre_populate_dds(primary_index, secondary_index)
{
	var primary = document.getElementById("primary_dropdown");
	primary.selectedIndex = primary_index;
	state_dd_change(primary);
	
	if (secondary_index == 0)
		return;
	
	var secondary = document.getElementById("inquiry");
	secondary.selectedIndex = secondary_index;
}



//Run the dd_state_change
//Set Up
var url_vars = dm20_get_url_vars();
var source = url_vars['source'];
if (source && source.length > 0)
{
	switch (source)
	{
		case "veterans_planning_guide":
		pre_populate_dds(3,2);
		break;
		
		case "personal_planning_guide":
		pre_populate_dds(3,3);
		break;
		
		default:
		break;
	}
}
else
{
	//Reset the primary dropdown (FF retains DD values on page refresh)
	var t = document.getElementById("primary_dropdown");
	t.selectedIndex = 0;
}

//MediaPlex ROI tag muid value
document.getElementById('muid').value = dm20_read_cookie('JSESSIONID');
