﻿/* Counts how many checkboxes are selected */
function GetSelectedCount(className) {
	var count = 0;

	//Finds all state checkboxes and adds their value to the array
	jQuery('.' + className + ' input').each(
		function(box) {
			if (this.checked) {
				count += 1;
			}
		}
	);

	return count;
}

function toggleCountries(link) {

    // True if they are all checked, false otherwise
    var toggled = link.innerHTML == 'Clear';

    // First flip the toggle, then set the check box to the toggle state    
    var i = 0;
    jQuery('.states input').each(function(idx, state) { state.checked = !toggled; });

    // Change the link text
    link.innerHTML = toggled ? 'All' : 'Clear';
}

function validateComparison() {
	var errors = new Array();

	var indCount = GetSelectedCount('topics');
	if (indCount < 1) {
		errors.push(' - Please select an indicator to compare.');
	}

	var tfCount = GetSelectedCount('characteristic');
	if (tfCount < 1) {
		errors.push(' - Please select at least one characteristic for the comparison.');
	}

	var locCount = GetSelectedCount('countries');
	if (locCount < 1) {
		errors.push(' - Please select at least one country for the comparison.');
	}

	if (errors.length == 0) {
		return true;
	}

	alert('Errors were found in the selected comparison.\n' + errors.join('\n'));
	return false;
}

function CreatePowerpoint() {

	var locations = FindVisibleLocations();
	
	if (locations.length == 0 || locations.length > 5) {
		alert('You must have at least one and at most five countries selected.');
		return;
	}
	
	var ind = parseUri(document.location).queryKey['ind'];
	var loc = getLocationUrlString(locations);

	var cat = jQuery('.CategoryId')[0].value;
	var powerpointURL = '/ihp/powerpointhandler.axd?ind=' + ind + '&loc=' + loc + '&cat=' + cat;
	window.location = powerpointURL;
}

function FindVisibleLocations() {
	var locations = new Array();

	jQuery('td.location_bar').each(function() {
		if (this.parentNode.style.display != 'none') {
			var id = this.id.substring(4);
			if (id != '') {
				locations.push(id);
			}
		}
	});

	return locations;
}

function getLocationUrlString(locations) {
	var locationString = '';
	for (var i = 0; i < locations.length; i++) {
		if (i > 0) locationString += ',';
		locationString += locations[i];
	}

	return locationString;
}

function parseUri(str) {
	var o = parseUri.options,
		m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
	q: {
		name: "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
