/**
 * Trek m.b.v. AJAX de states van het geselecteerde land uit de database en
 * duw deze in het state pulldown menu.
 */
function updateStates(country_menu, national) {

	
	var params = 'country_id='+country_menu.options[country_menu.selectedIndex].value;
	if (national) {
		params = params + '&national=1';
	}
	
	new Ajax.Request('/extra/states.php',
		{
		method: 'get',
		parameters: params,
		onSuccess:
		function(transport) {
			var state_menu = $('state');
			
			if (transport.responseText == "") {
				alert("Error occurred while fetching states");
				return;
			}
			
			while (state_menu.length > 0) {
	                state_menu.remove(0);
	        }
			if (transport.responseText == "EMPTY") {
				$('state_div').style.display="none";
				return;
			}
			
			var states = transport.responseText.split("\n");
			
			// Reset the account drop down menu first
			        
       while (state_menu.length > 0) {
               state_menu.remove(0);
       }
       var initial_option = document.createElement("option");


       initial_option.value = "";
       initial_option.text = "-- Choose State/Region --";

       state_menu[state_menu.length] = initial_option;
      
			for (var j = 0; j < states.length; j++) {
				var state_info = states[j].split("|");


				if (state_info.length < 2) {
					continue;
				}

				// Compile the name.
				var state_info_name = "";
				for (var i = 1; i < state_info.length; i++) {
					state_info_name = state_info_name + state_info[i];
					if (i < state_info.length - 1) {
						state_info_name = state_info_name + "|";
					}
				}

				// Now that we have the id and name, add them to the account drop down menu
				var state_option = document.createElement("option");
				state_option.value = state_info[0];
				state_option.text = state_info_name;

				state_menu[state_menu.length] = state_option;
				
				if ($('state_div').style.display!="table-row") {
					$('state_div').style.display="table-row";
				}
			}

			
			
		}
		}
	);
}

/**
 * Checken op selecteren van maximaal 3 checkboxes met een bepaalde naam
 */
function max3checks(name) {
	// job sectors
	var items = document.getElementsByName(name);
	var item_count = true;

	for (i=0;i<items.length;i++) {
		
		if (items[i].checked) {
			item_count = item_count + 1;
		}
	}
	
	if (item_count > 4) {
		alert("You cannot make more than 3 choices here.");
		return false;
	}
	else {
		return true;
	}
}
/**
 * Trek m.b.v. AJAX de states van het geselecteerde land uit de database en
 * duw deze in het state pulldown menu.
 */
function updateStatesDesired(country_menu, national) {

	
	var params = 'country_id='+country_menu.options[country_menu.selectedIndex].value;
	if (national) {
		params = params + '&national=1';
	}
	
	new Ajax.Request('/extra/states.php',
		{
		method: 'get',
		parameters: params,
		onSuccess:
		function(transport) {
			var state_menu = $('desired_state');
			
			if (transport.responseText == "") {
				alert("Error occurred while fetching states");
				return;
			}
			
			while (state_menu.length > 0) {
	                state_menu.remove(0);
	        }
			if (transport.responseText == "EMPTY") {
				$('state_row').style.display="none";
				return;
			}
			
			var states = transport.responseText.split("\n");
			
			// Reset the account drop down menu first
			        
       while (state_menu.length > 0) {
               state_menu.remove(0);
       }
       var initial_option = document.createElement("option");


       initial_option.value = "";
       initial_option.text = "-- Choose State/Region --";

       //state_menu[state_menu.length] = initial_option;
      
			for (var j = 0; j < states.length; j++) {
				var state_info = states[j].split("|");


				if (state_info.length < 2) {
					continue;
				}

				// Compile the name.
				var state_info_name = "";
				for (var i = 1; i < state_info.length; i++) {
					state_info_name = state_info_name + state_info[i];
					if (i < state_info.length - 1) {
						state_info_name = state_info_name + "|";
					}
				}

				// Now that we have the id and name, add them to the account drop down menu
				var state_option = document.createElement("option");
				state_option.value = state_info[0];
				state_option.text = state_info_name;

				state_menu[state_menu.length] = state_option;
				
				if ($('state_row').style.display!="table-row") {
					$('state_row').style.display="table-row";
				}
			}

			
			
		}
		}
	);
}
