$(document).ready(function(){
    // init the date picker
	$("#id_date").datepicker();
	
	function populateCountries(country_select) {
	    country_select.append("<option value=''></option>");
	    $.getJSON("/js/getCountries/",
		    function(data) {
		        $.each(data, function(i, item){
		            var country = item.fields.name;
		            if (country == "United States") {
		                country_select.append("<option selected value='"+country+"'>"+country+"</option>");
		            } else {
	                    country_select.append("<option value='"+country+"'>"+country+"</option>");
                    }
		        });
		    }
		);
	}
	
	function populateStates(state_select) {
	    // populate the states drop down
	    state_select.append("<option value=''></option>");
		$.getJSON("/js/getStatesForCountry/?country=United States",
		    function(data) {
                $.each(data, function(i, item){
                    var state = item.fields.name;
                    state_select.append("<option value='"+state+"'>"+state+"</option>");
                });
		    }
		);
	}
	
	if($("#id_conferenceType")[0].value == "multipoint") {
		$("#id_bridge").parent().show();
		$("#id_bridgeVendor").parent().show();
	}

	$("#id_conferenceType").change(function(event) {
		if (event.target.value == "multipoint") {
			// show the affinity bridge and vendor fields.
			$("#id_bridge").parent().show();
			if ($("#id_bridge")[0].value == "No") {
				$("#id_bridgeVendor").parent().show();con
			}
		} else {
			$("#id_bridge").parent().hide();
			$("#id_bridgeVendor").parent().hide();
		}
	});
	
	$("#id_bridge").change(function(event) {
		if (event.target.value == "No") {
			$("#id_bridgeVendor").parent().show();
		} else {
			$("#id_bridgeVendor").parent().hide();
		}
	});
	
	// function for when the add new site link is clicked
	$("#addAffinitySite").click(function(event) {
		event.preventDefault();
		var management_form_total = $("#id_loc-TOTAL_FORMS")[0];
		var i = parseInt(management_form_total.value)
		$("#affinitySites").append(createAffinitySiteForm(i));
		var form_num = i + 1;
		management_form_total.value = form_num;

		var num = form_num - 1;
		var country_select = $("#id_loc-"+num+"-country");
		var state_select = $("#id_loc-"+num+"-state");
		var city_select = $("#id_loc-"+num+"-city");

		// populate the countries drop down
		populateCountries(country_select);
		// populate the states drop down
		populateStates(state_select);

		// attach the event to the select changes
		country_select.change(function(event) {
			countryChange(event, country_select, state_select, city_select);
		});
		
		state_select.change(function(event) {
			city_select.empty();
			var selected_state = state_select[0].value;
			$.getJSON("/js/getCityForState/?state="+selected_state,
			    function(data) {
			        $.each(data, function(i, item){
			            var city = item.fields.name;
			            city_select.append("<option value='"+city+"'>"+city+"</option>");
			        });
			    }
			);
		});
	});
	
	$("#addCompanySite").click(function(event) {
		event.preventDefault();
		var management_form_total = $("#id_site-TOTAL_FORMS")[0];
		var i = parseInt(management_form_total.value);
		$("#companySites").append(createCompanySiteForm(i));
		
		var form_num = i + 1;
		management_form_total.value = form_num;
		
		var num = form_num - 1;
		var country_select = $("#id_site-"+num+"-country");
		var state_select = $("#id_site-"+num+"-state");
		
		// populate the countries drop down
		populateCountries(country_select);

		populateStates(state_select);
		
		country_select.change(function(event) {
			if (event.target.value == "United States") {
				state_select.parent().show();
			} else {
				state_select[0].value = "";
				state_select.parent().hide();
			}
		});
	});
	
	// function for when the country is changed
	$('[id^="id_loc"]').filter('[id$="-country"]').each(function(i) {
	    if ($("#id_loc-"+i+"-country")[0].value != "United States") {
	        $("#id_loc-"+i+"-state").parent().hide();
	    }
		$(this).change(function(event){
			var country_select = this;	
			var state_select = $("#id_loc-"+i+"-state");
			var city_select = $("#id_loc-"+i+"-city");
			countryChange(event, country_select, state_select, city_select);
		});
	});
	
	// function for when the state is changed
	$("[id^=id_loc]").filter("[id$=-state]").each(function(i) {
		$(this).change(function(event) {
			// populate the cities
			var city_select = $("#id_loc-"+i+"-city");
			var selected_state = $("#id_loc-"+i+"-state")[0].value;
			city_select.empty();
			$.getJSON("/js/getCityForState/?state="+selected_state,
			    function(data) {
			        $.each(data, function(i, item){
			            var city = item.fields.name;
			            city_select.append("<option value='"+city+"'>"+city+"</option>");
			        });
			    }
			);
		});
	});
	
	$("[id^=id_site]").filter("[id$=-country]").each(function(i) {
    // /$("#id_site-0-country").change(function(event) {
        if ($("#id_site-"+i+"-country")[0].value != "United States") {
	        $("#id_site-"+i+"-state").parent().hide();
	    }
	    $(this).change(function(event){
		    var state_select = $("#id_site-0-state");
		    if (event.target.value == "United States") {
			    state_select.parent().show();
		    } else {
			    state_select[0].value = "";
			    state_select.parent().hide();
		    }
	    });
	});
	
	// hide the state field for non-us billing addresses
	$("#id_country").change(function(event){
		var state_select = $("#id_state");
		if (event.target.value == "United States") {
			state_select.parent().show();
		} else {
			state_select[0].value = "";
			state_select.parent().hide();
		}
	});
	/*
	if ($("#id_different_contact")[0].checked) {
		$("#roomContactFields").show();
	}
	$("#id_different_contact").click(function(event) {
		$("#roomContactFields").toggle();
	});
	*/
 });

function createAffinitySiteForm(i) {
	return "<span><li class=\"subSet\">Affinity Site #"+(i+1)+"</li>"
	+"<li><label for=\"id_loc-"+i+"-country\">Country:<em><img src=\"/site_media/images/bullet_required.gif\" alt=\"required\"></em></label></th><td><select name=\"loc-"+i+"-country\" id=\"id_loc-"+i+"-country\">"
	+"</select></li>"
	+"<li><label for=\"id_loc-"+i+"-state\">State:<em><img src=\"/site_media/images/bullet_required.gif\" alt=\"required\"></em></label></th><td><select name=\"loc-"+i+"-state\" id=\"id_loc-"+i+"-state\">"
	+"</select></li>"
	+"<li><label for=\"id_loc-"+i+"-city\">City:<em><img src=\"/site_media/images/bullet_required.gif\" alt=\"required\"></em></label></th><td><select name=\"loc-"+i+"-city\" id=\"id_loc-"+i+"-city\">"
	+"</select></li>"
	+"<li><label for=\"id_loc-"+i+"-zip\">Zip / Postal Code:</label></th><td><input type=\"text\" name=\"loc-"+i+"-zip\" id=\"id_loc-"+i+"-zip\" /></li>"
	+"<li><label for=\"id_loc-"+i+"-name\">Participant Name:</label></th><td><input type=\"text\" name=\"loc-"+i+"-name\" id=\"id_loc-"+i+"-name\" /></li>"
	+"<li><label for=\"id_loc-"+i+"-phone\">Phone:</label></th><td><input type=\"text\" name=\"loc-"+i+"-phone\" id=\"id_loc-"+i+"-phone\" /></li>"
	+"<li><label for=\"id_loc-"+i+"-email\">Email:</label></th><td><input type=\"text\" name=\"loc-"+i+"-email\" id=\"id_loc-"+i+"-email\" /></li>"
	+"<li><label for=\"id_loc-"+i+"-number\">Number of Participants:</label></th><td><select name=\"loc-"+i+"-number\" id=\"id_loc-"+i+"-number\">"
	+"<option value=\"1\" selected=\"selected\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option>"
    +"<option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option>"
    +"<option value=\"10\">10</option><option value=\"11\">11</option><option value=\"12\">12</option><option value=\"13\">13</option><option value=\"14\">14</option>"
    +"<option value=\"15\">15</option><option value=\"16\">16</option><option value=\"17\">17</option><option value=\"18\">18</option><option value=\"19\">19</option>"
    +"<option value=\"20\">20</option><option value=\"21\">21</option><option value=\"22\">22</option><option value=\"23\">23</option><option value=\"24\">24</option>"
    +"<option value=\"25\">25</oNbption><option value=\"more than 25\">more than 25</option></select></li>"
	+"</span>";
}

function createCompanySiteForm(i) {
	return "<span><li class=\"subSet\">Your Site #"+(i+1)+"</li>"
	+"<li><label for=\"id_site-"+i+"-country\">Country:</label><select name=\"site-"+i+"-country\" id=\"id_site-"+i+"-country\"></select>"
	+"</li><li><label for=\"id_site-"+i+"-state\">State/Prov:</label><select name=\"site-"+i+"-state\" id=\"id_site-"+i+"-state\"></select>"
	+"</li><li><label for=\"id_site-"+i+"-city\">City:</label><input id=\"id_site-"+i+"-city\" type=\"text\" class=\"text\" name=\"site-"+i+"-city\" maxlength=\"200\" />"
	+"</li><li><label for=\"id_site-"+i+"-zip\">Zip/Postal Code:</label><input id=\"id_site-"+i+"-zip\" type=\"text\" class=\"text\" name=\"site-"+i+"-zip\" maxlength=\"10\" />"
	+"</li><li><label for=\"id_site-"+i+"-name\">Participant Name:</label><input id=\"id_site-"+i+"-name\" type=\"text\" class=\"text\" name=\"site-"+i+"-name\" />"
	+"</li><li><label for=\"id_site-"+i+"-phone\">Room phone:</label><input id=\"id_site-"+i+"-phone\" type=\"text\" class=\"text\" name=\"site-"+i+"-phone\" />"
	+"<li><label for=\"id_site-"+i+"-network\">Network:</label><select name=\"site-"+i+"-network\" id=\"id_site-"+i+"-network\"><option value=\"\" selected=\"selected\"></option>"
    +"<option value=\"ISDN\" selected>ISDN</option><option value=\"IP\">IP</option><option value=\"Both\">Both</option></select></li>"
	+"</li><li><label for=\"id_site-"+i+"-number\">ISDN Number or IP Number:</label>"
	+"<input id=\"id_site-"+i+"-number\" type=\"text\" class=\"text\" name=\"site-"+i+"-number\" /></li>"
	+"<li style=\"border-top:1px solid #E7E6E4;\"><label for=\"id_roomContact\">Room Contact:</label><input id=\"id_site-"+i+"-roomContact\" type=\"text\" class=\"text\" name=\"site-"+i+"-roomContact\" /> <em class=\"small\">If different than primary contact above</em>"
    +"</li><li><label for=\"id_contactPhone\">Contact Phone:</label><input id=\"id_site-"+i+"-contactPhone\" type=\"text\" class=\"text\" name=\"site-"+i+"-contactPhone\" />"
    +"</li><li><label for=\"id_altPhone\">Contact Alternate Phone:</label><input id=\"id_site-"+i+"-altPhone\" type=\"text\" class=\"text\" name=\"site-"+i+"-altPhone\" />"
    +"</li><li style=\"border-top:1px solid #E7E6E4;\"><label for=\"id_instructions\">Special Instructions:</label><textarea id=\"id_site-"+i+"-instructions\" rows=\"10\" cols=\"40\" name=\"site-"+i+"-instructions\"></textarea>"
    +"</li></span>";
}
