var MagicCategory = new Object();
MagicCategory.options = new Object();

MagicCategory.clear = function(container) {
    var nodes = $(container).getElementsByTagName('option');
    for (var i = nodes.length - 1; i > 0 ; i--) {
        if (nodes[i].value != "") {
            nodes[i].parentNode.removeChild(nodes[i]);
        }
    }
}

MagicCategory.generate = function(container, category, value) {
    category_id = category || false;
    value = value || false;

    var nodes = $(container).getElementsByTagName('option');
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].value != "") {
            category = nodes[i].className.substring(nodes[i].className.indexOf('_') + 1);
            if (!MagicCategory.options[container]) {
                MagicCategory.options[container] = new Object();
            }
            if (!MagicCategory.options[container][category]) {
                MagicCategory.options[container][category] = new Array();
            }
            MagicCategory.options[container][category].push({id: nodes[i].value, text: nodes[i].innerHTML});
        }
    }
    MagicCategory.clear(container);
    if (category_id) {
        MagicCategory.show(container, category_id, value);
    }
}

MagicCategory.show = function(container, category, value) {
    if (category == 0) {
        return false;
    }
    value = value || false;

    MagicCategory.clear(container);
    if (MagicCategory.options[container][category]) {
        var options = MagicCategory.options[container][category];
        for (var i = 0; i < options.length; i++) {
            option = document.createElement('option');
            option.value = options[i].id;
            option.innerHTML = options[i].text;
            if (typeof value == 'object') {
                for (var j = 0; j < value.length; j++) {
                    if (value[j] && options[i].id == value[j]) {
                        option.selected = true;
                    }
                }
            }
            else {
                if (value && options[i].id == value) {
                    option.selected = true;
                }
            }
            $(container).appendChild(option);
        }
    }
}

MagicCategory.tempFixLocations = function(category_id, hide_alls) {
    category_id = category_id || "";
    hide_alls = hide_alls || false;

    compliance_options = {
        "277": "Asia",
        "278": "Australia and NZ",
        "279": "Europe",
        "280": "UK"
    };

    other_options = {
        "286": "Africa",
        "240": "Australia (all)",
        "267": "Australia - Brisbane",
        "268": "Australia - Melbourne",
        "271": "Australia - Other",
        "269": "Australia - Perth",
        "270": "Australia - Sydney",
        "241": "Bangkok",
        "242": "Belgium",
        "243": "Bermuda",
        "244": "British Virgin Islands",
        "245": "Cayman Islands",
        "246": "Channel Islands",
        "247": "Denmark",
        "258": "Eastern Europe - Moscow",
        "249": "Eastern Europe - Other",
        "250": "France",
        "251": "Germany",
        "252": "Greece",
        "253": "Hong Kong",
        "287": "India",
        "290": "Indonesia/Jakarta",
        "254": "Ireland",
        "255": "Isle of Man",
        "256": "Italy",
        "281": "Korea/Seoul",
        "248": "Middle East - Dubai",
        "257": "Middle East - Other",
        "259": "Netherlands",
        "260": "New Zealand",
        "261": "People\"s Republic of China",
        "262": "Singapore",
        "288": "South America",
        "263": "Spain",
        "289": "Switzerland/Austria",
        "264": "Tokyo",
        "222": "UK (all)",
        "223": "UK - East Anglia",
        "224": "UK - East Midlands",
        "225": "UK - London",
        "230": "UK - North East",
        "231": "UK - North West",
        "232": "UK - Northern Home Counties",
        "233": "UK - Scotland",
        "234": "UK - South East",
        "235": "UK - South West",
        "236": "UK - Thames Valley",
        "237": "UK - Wales",
        "238": "UK - West Midlands",
        "239": "UK - Yorkshire &amp; Humber",
        "265": "United States of America"
    };
    // Save and reapply the selected ones
    selected = new Object();

    // First clear the existing options
    options = $('location_id').options;
    var othersSelected = false;
    for (var i = options.length - 1; i >= 0; i--) {
        if (options[i].selected) {
            selected[options[i].value] = true;
            othersSelected = true;
        }
        $('location_id').removeChild(options[i]);
    }

    if (category_id == "95") {
        new_options = compliance_options;
    }
    else {
        new_options = other_options;
    }
    option = document.createElement('option');
    option.value = '';
    if (!othersSelected && location.href.indexOf('vacancy_id') == -1) {
        option.selected = true;
    }
    option.innerHTML = '- select all -';
    $('location_id').appendChild(option);

    for (var key in new_options) {
        option = document.createElement('option');
        option.value = key;
        option.innerHTML = new_options[key];
        if (selected[option.value]) {
            option.selected = true;
        }
        if (!(hide_alls && new_options[key].substr(new_options[key].length - 5) == "(all)")) {
            $('location_id').appendChild(option);
        }
    }
}

MagicCategory.checkSelectAll = function(el) {
    if (el.options[el.selectedIndex].value == "") {
        for (var i = el.options.length - 1; i >= 0; i--) {
            el.options[i].selected = true;
        }
        el.scrollTop = 0;
    }
}

MagicCategory.preselectLocations = function(locations) {
    var options = $('location_id').options;
    for (var i = 0; i < options.length; i++) {
        for (var j = 0; j < locations.length; j++) {
            if (options[i].value == locations[j]) {
                options[i].selected = true;
            }
        }
    }
}

MagicCategory.fixSalaries = function(location_field, salary_field_id, chosen_id, client) {
    chosen_id = chosen_id || [0];
    client = client || 'FJ';

	try {
		console.log(chosen_id);
	}
	catch (e){}

    if (client == 'FJ') {
        salaries = [{
            "430": "up to &pound;19,000",
            "431": "&pound;20,000-&pound;29,000",
            "433": "&pound;30,000-&pound;39,999",
            "434": "&pound;40,000-&pound;49,999",
            "435": "&pound;50,000-&pound;59,999",
            "436": "&pound;60,000-&pound;69,000",
            "437": "&pound;70,000-&pound;79,000",
            "438": "&pound;80,000-&pound;89,999",
            "439": "&pound;90,000-&pound;99,999",
            "440": "&pound;100,000-&pound;150,000",
            "441": "&pound;150,000+"
        },
        {
            "442": "up to A$60,000",
            "443": "A$60,000-A$80,000",
            "444": "A$80,000-A$100,000",
            "445": "A$100,000-A$120,000",
            "449": "A$120,000-A$150,000",
            "446": "A$150,000-A$200,000",
            "447": "A$200,000-A$250,000",
            "448": "A$250,000+"
        }];
        locations = {
            "195": "0",
            "196": "0",
            "197": "0",
            "198": "0",
            "199": "0",
            "200": "0",
            "201": "0",
            "202": "0",
            "203": "0",
            "204": "0",
            "205": "0",
            "206": "0",
            "207": "0",
            "209": "0",
            "210": "0",
            "211": "0",
            "212": "0",
            "283": "1",
            "284": "1",
            "285": "1",
            "272": "1",
            "273": "1",
            "276": "1",
            "274": "1",
            "275": "1",
            "213": "1",
            "214": "0",
            "216": "1",
            "217": "0",
            "221": "0"
        };
    }
    else if (client == 'CM') {
        /*
            australasia
            europe
            london
            middle east
            uk regions
            
            this array represents = location_id: item in the salaries array below
        */
        locations = {
            "292": "1",
            "291": "2",
            "192": "0",
            "194": "3",
            "193": "0"
        };
        salaries = [{
			"178": "Up to &pound;24,999",
			"179": "&pound;25,000 - 29,999",
			"180": "&pound;30,000 - 39,999",
			"181": "&pound;40,000 - 59,999",
			"182": "&pound;60,000 - 79,999",
			"183": "&pound;80,000+"

        },
        {
			"618": "Up to A$54,999",
			"619": "A$55,000-A$64,999",
			"620": "A$65,000-A$89,999",
			"621": "A$90,000-A$129,999",
			"604": "A$130,000-A$174,999",
			"605": "A$175,000+"
        },
        {
            "608": "Up to &euro;24,999",
			"609": "&euro;25,000-&euro;29,999",
			"610": "&euro;30,000-&euro;39,999",
			"606": "&euro;40,000-&euro;59,999",
			"611": "&euro;60,000-&euro;79,999",
			"612": "&euro;80,000+"

        },
        {
            "607": "Up to AED10,000 (per month)",
			"613": "AED10,000-15,000 (per month)",
			"614": "AED15,000-20,000 (per month)",
			"615": "AED20,000-35,000 (per month)",
			"616": "AED35,000-50,000 (per month)",
			"617": "AED50,000+ (per month)"
        }];
    }
    else {
        salaries = [{
            "159": "Up to &pound;39,999",
            "160": "&pound;40,000 - 69,999",
            "161": "&pound;70,000 - 99,999",
            "162": "&pound;100,000 - 149,999",
            "163": "&pound;150,000 +"
        },
        {
            "587": "up to A$59,999",
            "588": "A$60,000-A$79,999",
            "589": "A$80,000-A$99,999",
            "590": "A$100,000-A$119,999",
            "591": "A$120,000-A$149,999",
            "592": "A$150,000-A$199,999",
            "593": "A$200,000-A$249,999",
            "594": "A$250,000+"
        },
        {
            "595": "up to HK$499,999",
            "596": "HK$500,000-HK$999,999",
            "597": "HK$1,000,000-HK$1,499,999",
            "598": "HK$1,500,000-HK$1,999,999",
            "599": "HK$2,000,000+"
        }];
        locations = {
            "187": "2",
            "188": "1",
"293": "1",            
"303": "1",
            "294": "1",
            "295": "1",
            "296": "1",
            "297": "0",
            "185": "0",
            "186": "0",
            "190": "0",
            "191": "0",
            "298": "1",
            "189": "0",
            "299": "0",
            "300": "0",
            "301": "0",
            "167": "0",
            "168": "0",
            "170": "0",
            "171": "0",
            "172": "0",
            "282": "0",
            "173": "0",
            "174": "0",
            "183": "0",
            "175": "0",
            "176": "0",
            "177": "0",
            "178": "0",
            "179": "0",
            "180": "0",
            "181": "0",
            "182": "0",
            "184": "0",
            "302": "0"
        };
    }
    // Bug fix
    if (location_field.selectedIndex == -1) {
        location_field.selectedIndex = 0;
    }

    chosen_location = location_field.options[location_field.selectedIndex].value;
    salaries = salaries[locations[chosen_location]];
    //console.log(salaries);

    salary_field = $(salary_field_id);
    while (salary_field.length > 1) {
        salary_field.remove(salary_field.length - 1);
    }

    if (chosen_id == 0) {
        salary_field.selectedIndex = 0;
    }

    for (var i in salaries) {
        option = document.createElement('option');
        option.value = i;
        option.innerHTML = salaries[i];
        for (var j = 0; j < chosen_id.length; j++) {
            if (chosen_id[j] == i) {
                option.selected = true;
            }
        }
        salary_field.appendChild(option);
    }
}
