YAHOO.util.Event.onDOMReady(function () {		
	function loadDropdown(type, list) {
		switch (type) {
        	case 'Searched for Website Companies Online':
            	list.options[list.options.length] = new Option('Select Source');
            	list.options[list.options.length] = new Option('Google', 'Google');
            	list.options[list.options.length] = new Option('Yahoo', 'Yahoo');
            	list.options[list.options.length] = new Option('MSN', 'MSN');
            	list.options[list.options.length] = new Option('Bing', 'Bing');
            	break;
        	case 'Saw a Magazine Ad':
        		list.options[list.options.length] = new Option('Select Source');
            	list.options[list.options.length] = new Option('Veterinarian Economics', 'Veterinarian Economics');
            	list.options[list.options.length] = new Option("Clinician's Brief", "Clinician's Brief");
            	break;
    	}
	}
	
	function handleReferralTypeChange(e) {		
		handleReferralType(YAHOO.util.Event.getTarget(e).value);				
	}
	
	function handleReferralType(value) {
		var list = YAHOO.util.Dom.get('type');
		
		var text = document.createElement('input');
		text.className = 'source';
		text.id = 'source';
		text.type = 'text';
		text.name = 'ReferralSource';		

		var dropdown = document.createElement('select');
		dropdown.className = 'source';
		dropdown.id = 'source';
		dropdown.name = 'ReferralSource';
		
		if (YAHOO.util.Dom.inDocument(YAHOO.util.Dom.get('source')))
			YAHOO.util.Dom.get('contact_info').removeChild(YAHOO.util.Dom.get('source'));
		
		if (YAHOO.util.Dom.inDocument(YAHOO.util.Dom.get('source_label')))
			YAHOO.util.Dom.get('contact_info').removeChild(YAHOO.util.Dom.get('source_label'));				

		var label = document.createElement('label');
		label.id = 'source_label';
		
		switch (value) {						
			case 'Searched for Website Companies Online':
			case 'Saw a Magazine Ad':
				label.innerHTML = 'Source Name <span class="errstring">*</span>';	
				
				YAHOO.util.Dom.insertAfter(label, list);
				YAHOO.util.Dom.insertAfter(dropdown, label);	
				loadDropdown(value, dropdown, YAHOO.util.Dom.get('rsource').value);						
				break;
			case 'Referred by a VetMatrix Client':				
				label.innerHTML = 'Referee Name <span class="errstring">*</span>';				
				
				YAHOO.util.Dom.insertAfter(label, list);	
				YAHOO.util.Dom.insertAfter(text, label);	
				break;
			case 'Referred by an Association/Organization':
				label.innerHTML = 'Association Name <span class="errstring">*</span>';
				
				YAHOO.util.Dom.insertAfter(label, list);	
				YAHOO.util.Dom.insertAfter(text, label);
				break;
			case 'Other':
				YAHOO.util.Dom.setStyle(text, 'margin-left', '12.5833em');
				
				YAHOO.util.Dom.insertAfter(text, list);			
				break;
			case 'Saw a VetMatrix Website Online':
			default:							
		}
	}
	
	// check to see if the 'How did you...' has an option slected and provide the appropriate subinput
	handleReferralType(YAHOO.util.Dom.get('type').value);

	YAHOO.util.Event.on('type', 'change', handleReferralTypeChange);
});


// This script dynamically populates the "Sub Category" select options based on "Category" select option selection.

function SetOptions() {
	
	var ReferralList = document.getElementById('type');
	
	// Clear out the list of teams
	ClearOptions(document.getElementById('source'));
   
	if (ReferralList[ReferralList.selectedIndex].value == "Searched for website companies online") {
		AddToOptionList(document.getElementById('source'), "None Selected", "Select Source");
		AddToOptionList(document.getElementById('source'), "Google", "Google");
		AddToOptionList(document.getElementById('source'), "Yahoo", "Yahoo");
		AddToOptionList(document.getElementById('source'), "MSN", "MSN");
		AddToOptionList(document.getElementById('source'), "Bing", "Bing");
	}
	
	if (ReferralList[ReferralList.selectedIndex].value == "Saw a Magazine ad") {
		AddToOptionList(document.getElementById('source'), "None Selected", "Select Source");
		AddToOptionList(document.getElementById('source'), "Veterinarian Economics", "Veterinarian Economics");
		AddToOptionList(document.getElementById('source'), "Clinician's Brief", "Clinician's Brief");
	}
}
	
function ClearOptions(OptionList) {

	// Always clear an option list from the last entry to the first
	for (x = OptionList.length; x >= 0; x = x - 1) {
	  OptionList[x] = null;
	}
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
	// Add option to the bottom of the list
	OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
