var store = new Array()

store[1] = new Array("Accounts Payable/Receivable","2","Financial Management","3","Qualified Accounting","4","Audit and Risk","5","Consulting","6","Recovery","7","Taxation","8","Superannuation","9","Other Accounting","10");
store[11] = new Array("Administration","12","Data Entry","13","Office Management","14","Personal Assistant","15","Reception","16","Other Administration","17");
store[18] = new Array("Aeronautical Engineer","19","Electrical Engineer","20","Mechanical Engineer","21","Other Engineer","22","Other Technician","23","RADAR","24","Structural Engineer","25","Avionics Engineer","26","Project Manager","27","AME","28","LAME","29","Avionics Technician","30","Weapon Systems","31","Other Aerospace","32");
store[55] = new Array("Navy","56","Army","57","Air Force","58","Government","59");
store[33] = new Array("Architecture","34","Construction Management","35","Drafting","36","Project Management","37","Surveying","38","Other Construction","39");
store[40] = new Array("IT and Telecommunications","41","Recruitment","42","Engineering","43","Sales","44","Security","45","Naval Projects","46","Aerospace Projects","47","Land Projects","48","Oil and Gas","49","Systems Engineering","50","Project Management","51","Scientific","52","Documentation and Tenders","53","Other Consulting","54");
store[60] = new Array("Aerospace","61","Naval Projects","62","Land Projects","63","Civil","64","Structural","65","Combat Systems","66","Electrical","67","Mechanical","68","Electronics","69","Mining","70","Simulation","71","Project Management","72","Oil and Gas","73","Mining","74","Vehicles - Military","75","Other Engineering (Tertiary Qualification)","76");
store[77] = new Array("Aerospace","78","Naval Projects","79","Land Projects","80","Civil","81","Structural","82","Combat Systems","83","Electrical","84","IT and T","85","Mechanical","86","Mechatronics","87","Mining","88","Electronics","89","Simulation","90","Project Management","91","Oil and Gas","92","Mining","93","Vehicles - Military","94","Other Engineering (Trade Qualification)","95");
store[96] = new Array("Aerospace","97","Naval Projects","98","Land Projects","99","Civil","100","Structural","101","Combat Systems","102","Electrical","103","IT and T","104","Mechanical","105","Mechatronics","106","Mining","107","Naval Architecture","108","Electronics","109","Simulation","110","Project Management","111","Oil and Gas","112","Mining","113","Other Graduate","114");
store[115] = new Array("Consultant","116","Engineer","117","Tradesman","118","Project Manager","119","Other Homeland Security","120");
store[121] = new Array("HR - Generalist","122","Management","123","Occupational Health and Safety","124","Recruitment","125","Training and Development","126","Other Human Resources","127");
store[128] = new Array("Communications-Military","129","Drafting - CAD","130","Database Development","131","Hardware Engineering","132","Helpdesk","133","Information Architecture","134","Management","135","Network Administration","136","Project Management","137","Consulting","138","Software Engineering","139","System Architecture","140","Technical Writing","141","Testing","142","Training","143","Other IT and T","144");
store[145] = new Array("Civil","146","Structural","147","Combat Systems","148","Communications - Military","149","Electrical","150","Electronic Warfare","151","IT and T","152","Management","153","Mechanical","154","Mechatronics","155","RADAR","156","Electronics","157","Simulation","158","Systems Engineering","159","Project Management","160","Vehicles - Military","161","Weapon Systems","162","Other Land Defence Projects","163");
store[164] = new Array("Administration","165","Business Development/Sales","166","Integrated Logistics Support (ILS)","167","Management","168","Operations","169","Transport","170","Marine Industry","171","Other Logistics Transport and Supply","172");
store[173] = new Array("Structural","174","Naval Architecture","175","Combat Systems","176","Communications - Military","177","Electrical","178","Electronic Warfare","179","IT and T","180","Management","181","Mechanical","182","Mechatronics","183","RADAR","184","Electronics","185","Simulation","186","SONAR","187","Systems Engineering","188","Project Management","189","Weapon Systems","190","Other Naval Defence Projects","191");
store[192] = new Array("Marine Projects","193","Civil","194","Consulting","195","Structural","196","Systems","197","Electrical","198","IT and T","199","Management","200","Mechanical","201","Mechatronics","202","Mining","203","Electronics","204","Simulation","205","Project Management","206","Oil and Gas","207","Mining","208","Other Offshore Oil and Gas","209");
store[210] = new Array("Aerospace","211","Communications - Military","212","Naval Projects","213","Land Projects","214","Civil","215","Structural","216","Combat Systems","217","Electrical","218","Mechanical","219","IT and T","220","Electronics","221","Mining","222","Simulation","223","Project Management","224","Oil and Gas","225","Mining","226","Vehicles - Military","227","Other Project Management","228");
store[229] = new Array("Aerospace","230","Communications - Military","231","Naval Projects","232","Land Projects","233","Civil","234","Structural","235","Combat Systems","236","Electrical","237","Mechanical","238","IT and T","239","Electronics","240","Mining","241","Simulation","242","Project Management","243","Oil and Gas","244","Mining","245","Vehicles - Military","246","Other Sales and Business Development","247");
store[248] = new Array("Aerospace","249","Naval Projects","250","Land Projects","251","Civil","252","Structural","253","Combat Systems","254","Electrical","255","Mechanical","256","IT and T","257","Electronics","258","Mining","259","Simulation","260","Project Management","261","Oil and Gas","262","Mining","263","Vehicles - Military","264","Other Simulation","265");
store[266] = new Array("Aerospace","267","Naval Projects","268","Land Projects","269","Civil","270","Structural","271","Combat Systems","272","Communications - Military","273","Electrical","274","Mechanical","275","IT and T","276","Electronics","277","Mining","278","Simulation","279","Project Management","280","Oil and Gas","281","Mining","282","Vehicles - Military","283","Other Systems Engineering","284");
store[285] = new Array("Aerospace","286","Naval Projects","287","Land Projects","288","Civil","289","Structural","290","Combat Systems","291","Electrical","292","Mechanical","293","IT and T","294","Electronics","295","Mining","296","Simulation","297","Project Management","298","Oil and Gas","299","Mining","300","Vehicles - Military","301","Other Trades","302");

// Helper functions

function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}
function compareValue (option1, option2) {
  return option1.value < option2.value ? -1 :
    option1.value > option2.value ? 1 : 0;
}
function compareTextAsFloat (option1, option2) {
  var value1 = parseFloat(option1.text);
  var value2 = parseFloat(option2.text);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
function compareValueAsFloat (option1, option2) {
  var value1 = parseFloat(option1.value);
  var value2 = parseFloat(option2.value);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
function sortSelect (select, compareFunction) {
  if (!compareFunction)
    compareFunction = compareText;
  var options = new Array (select.options.length);
  for (var i = 0; i < options.length; i++)
    options[i] = 
      new Option (
        select.options[i].text,
        select.options[i].value,
        select.options[i].defaultSelected,
        select.options[i].selected
      );
  options.sort(compareFunction);
  select.options.length = 0;
  for (var i = 0; i < options.length; i++)
    select.options[i] = options[i];
}

function insertOptionAt (select, option, index) {
    for (var i = select.options.length; i > index; i--) {
      select.options[i] = select.options[i - 1];
    }
    select.options[index] = option;
}


// populate subcategory drop-down list based on selected category
function populate(selectObj,subcatId,selectAll)
{
	// Find out which categories have been selected
	categoryIds = new Array();
	j=0;
	for(i=0;i<selectObj.options.length;i++) {
		if (selectObj.options[i].selected &&
		    selectObj.options[i].value != 0) {
			categoryIds[j++] = selectObj.options[i].value;
		}
	}

	var subcat = document.getElementById(subcatId);

	// Find out which subcategories have currently been selected
	// so we can reselect them later
	selectedSubcategoryIds = new Array();
	j=0;
	for(i=0;i<subcat.options.length;i++) {
		if (subcat.options[i].selected &&
		    subcat.options[i].value != 0) {
			selectedSubcategoryIds[subcat.options[i].value] = true;
		}
	}

	// Remove existing options
	subcat.options.length = 0;

    // If a category was selected, create subcategories
	if (categoryIds.length > 0) {
		for(i=0; i<categoryIds.length; i++) {
			var list = store[categoryIds[i]];
			offset = subcat.options.length;
    		// create a new list of options
    		for(j=0;j<list.length;j+=2) {
    			subcat.options[j/2 + offset] = new Option(list[j],list[j+1]);
    		}
		}
    }

	subcatSelected = false;

	// Try to reselect any previously selected subcategories
	// Note that they may no longer be available
	if (selectedSubcategoryIds.length > 0) {
		for(i=0; i<subcat.options.length; i++) {
			if (selectedSubcategoryIds[subcat.options[i].value]) {
				subcat.options[i].selected = true;
				subcatSelected = true;
			}
		}
	}

	// Sort subcat options
	sortSelect(subcat, compareText);

	// Add a 'select all' option if flag is set
	if (selectAll) {
		option = new Option('All Subcategories',"");
		subcat.options.add(option,0);
	}

	// If there are no selected subcategories and 
	// there is a 'select all' option, select it
	if (!subcatSelected && selectAll) {
		subcat.selectedIndex = 0;
	}
}

// This will keep our selected (sub)categories
var selectedCategoryIds    = new Array();
var selectedSubCategoryIds = new Array(2);

function limitSubCatSelection(selectObj)
{
	index = selectObj.id.substr(-1)-1;
	count = this.length;
	
	categoryIds = getSelectedCategoryIds(selectObj);
	selectedCategoryIds = selectedSubCategoryIds[index];

	if (categoryIds.length > 2) {
		// More than two selected
		alert("You can't select more than two subcategories");
	} else {
		// Copy the selected into selectedSubCategoryIds[index]
		selectedSubCategoryIds[index] = new Array();
		for(i=0;i<categoryIds.length;i++) {
			selectedSubCategoryIds[index][i] = categoryIds[i];
		}
	}

	// Re-select the saved categories
	for(i=0;i<selectObj.options.length;i++) {
		selectObj.options[i].selected = false;
		for(j=0;j<selectedSubCategoryIds.length;j++) {
			if (selectObj.options[i].value == selectedSubCategoryIds[index][j]) {
				selectObj.options[i].selected = true;
			}
		}
	}
}

function callback(selectObj)
{
	count = selectedCategoryIds.length;

	// Find out which categories have been selected
	categoryIds = getSelectedCategoryIds(selectObj);

	// Find out what the user did
	if (categoryIds.length < selectedCategoryIds.length) {
		if (categoryIds.length == 0) {
			// No selection, nothing to display
			hideSubCatSelectBox(1);
			hideSubCatSelectBox(2);
			// Remember selection
			selectedCategoryIds.length=0;
		} else {
			// User selected a completely different option
			if (!inArray(categoryIds[0],selectedCategoryIds)) {
				// Find the selected option
				option = findAddedOption(selectObj,selectedCategoryIds,categoryIds);
				id   = option.value;
				name = option.text;
				// Remember selection
				selectedCategoryIds = new Array;
				selectedCategoryIds[0] = id;
				// Show top select box
				initSubCatSelectBox(1,name,id);
				showSubCatSelectBox(1);
				hideSubCatSelectBox(2);
			} else {
				// User deselected one of the two existing options
				// Which one?
				
				// The remaining option should be at the top
				if (selectedCategoryIds[0] == categoryIds[0]) {
					// Move the bottom box up
					moveSubCatSelectBox(2,1);
				}
				// Hide bottom box
				hideSubCatSelectBox(2);

				// Remember selection
				selectedCategoryIds = new Array();
				selectedCategoryIds[0] = categoryIds[0];
			}
		}
	} else if ((categoryIds.length > count) && (count > 0)) {
		if (count == 2) {
			alert("You can't select more than two categories");
		} else {
			// USER SELECTED AN ADDITIONAL CATEGORY

			// Find out which category has been added
			option = findAddedOption(selectObj,selectedCategoryIds,categoryIds);
			id     = option.value;
			name   = option.text;

			// Remember selection
			selectedCategoryIds[1] = id;

			// Move the top box down
			moveSubCatSelectBox(1,2);

			// Init and display top box
			initSubCatSelectBox(1,name,id);
			showSubCatSelectBox(1);
		}
	} else {
		// Find the selected option
		option = findAddedOption(selectObj,selectedCategoryIds,categoryIds);
		id   = option.value;
		name = option.text;
		// Remember selection
		selectedCategoryIds = new Array;
		selectedCategoryIds[0] = id;
		// Show top select box
		initSubCatSelectBox(1,name,id);
		showSubCatSelectBox(1);
		hideSubCatSelectBox(2);
	}

	// Make sure the saved categories are selected
	for(i=0;i<selectObj.options.length;i++) {
		selectObj.options[i].selected = false;
		for(j=0;j<selectedCategoryIds.length;j++) {
			if (selectObj.options[i].value == selectedCategoryIds[j]) {
				selectObj.options[i].selected = true;
			}
		}
	}
}

function getSelectedCategoryIds(selectObj)
{
	ids = new Array();
	j=0;
	for(i=0;i<selectObj.options.length;i++) {
		if (selectObj.options[i].selected &&
		    selectObj.options[i].value != 0) {
			ids[j++] = selectObj.options[i].value;
		}
	}
	return ids;
}

function findRemovedOption(selectObj,before,after)
{
	// Find the ID in before that's not in after
	for(i=0;i<before.length;i++) {
	   if (inArray(before[i],after) == false)
			id = before[i];
	}
	// Find the option with that ID
	for(i=0;i<selectObj.options.length;i++) {
		if (selectObj.options[i].value == id) {
			return selectObj.options[i];
		}
	}
}

function inArray(value,array)
{
	for(j=0;j<array.length;j++) {
		if (array[j]==value) return true;
	}
	return false;
}

function findAddedOption(selectObj,before,after)
{
	// Find the ID in after that's not in before
	for(i=0;i<after.length;i++) {
	   if (inArray(after[i],before) == false)
			id = after[i];
	}
	// Find the option with that ID
	for(i=0;i<selectObj.options.length;i++) {
		if (selectObj.options[i].value == id) {
			return selectObj.options[i];
		}
	}
}

/**
 * Copy the content from one box to the next
 * Show the to box, hide the from box
 * @param int fromIndex 1 or 2
 * @param int toIndex 1 or 2
 */
function moveSubCatSelectBox(fromIndex,toIndex)
{
	to   = document.getElementById('subcategories' + toIndex);
	from = document.getElementById('subcategories' + fromIndex);

	// Copy options
	to.options.length=0;
	for(i=0;i<from.options.length;i++) {
		to.options[i] = new Option(from.options[i].text,from.options[i].value);
		to.options[i].selected = from.options[i].selected;
	}
	// Copy name
	document.getElementById('sub' + toIndex + 'name').innerHTML = document.getElementById('sub' + fromIndex + 'name').innerHTML;

	// Display
	showSubCatSelectBox(toIndex);
	hideSubCatSelectBox(fromIndex);
}

/**
 * Set options and name
 * @param int index 1 or 2
 * @param string name name of the category
 * @param int categoryId id of the category
 */
function initSubCatSelectBox(index,name,categoryId)
{
	list = store[categoryId];
	// create a new list of options
	for(j=0;j<store[categoryId].length;j+=2) {
		document.getElementById('subcategories'+index).options[j/2] = new Option(list[j],list[j+1]);
	}
	document.getElementById('sub' + index + 'name').innerHTML = name;
}

/**
 * Remove options and hide box
 * @param int index 1 or 2
 */
function hideSubCatSelectBox(index)
{
	document.getElementById('sub' + index).style.display='none';
	document.getElementById('subcategories' + index).options.length=0;
}

/**
 * Show box
 * @param int index 1 or 2
 */
function showSubCatSelectBox(index)
{
	document.getElementById('sub' + index).style.display='block';
	document.getElementById('sub' + index).style.visibility='visible';
}