$(document).ready(function() {
	
	$('#federation').change(function() {
		$.getJSON('/reports/json_countries/' + $(this).val(), function(data){
			//console.log(data);
			$('#country option').remove();
			var option = $('<option></option>').attr('value', '').text('Select...');
			var selected_country = $('#prev-country').val() || '';
			$('#country').append( option );
			for( i in data )
			{
				option = $('<option></option>').attr('value', data[i]['name']).text(html_entity_decode(data[i]['name']));
				if( selected_country == html_entity_decode(data[i]['name']) )
				{
					option.attr('selected', true);
				}
				$('#country').append( option );
			}
		});
	});
	
	$('#country').change(function() {
		$.getJSON('/reports/json_country_regions/' + $(this).val(), function(data){
			//console.log(data);
			$('#region option').remove();
			var option = $('<option></option>').attr('value', '').text('Select...');
			var selected_region = $('#prev-region').val() || '';
			$('#region').append( option );
			for( i in data )
			{
				option = $('<option></option>').attr('value', data[i]).text(html_entity_decode(data[i]));
				if( selected_region == html_entity_decode(data[i]) )
				{
					option.attr('selected', true);
				}
				$('#region').append( option );
			}
		});
	});
	
	if( $('#federation').val() != '' && $('#federation').val() != undefined )
	{
		var selected_country = $('#prev-country').val();

		$.getJSON('/reports/json_countries/' + $('#federation').val(), function(data) {
			$('#country option').remove();
			var option = $('<option></option>').attr('value', '').text('Select...');
			$('#country').append( option );
			for( i in data )
			{
				option = $('<option></option>').attr('value', data[i]['name']).text(html_entity_decode(data[i]['name']));
				if( selected_country == html_entity_decode(data[i]['name']) )
				{
					option.attr('selected', true);
				}
				$('#country').append( option );
			}
		});
		
		if( selected_country != '' )
		{
			$.getJSON('/reports/json_country_regions/' + selected_country, function(data) {
				//console.log(data);
				$('#region option').remove();
				var option = $('<option></option>').attr('value', '').text('Select...');
				var selected_region = $('#prev-region').val();
				$('#region').append( option );
				for( i in data )
				{
					option = $('<option></option>').attr('value', data[i]).text(html_entity_decode(data[i]));
					if( selected_region == html_entity_decode(data[i]) )
					{
						option.attr('selected', true);
					}
					$('#region').append( option );
				}
			});
		}

	}
	
	$('.charactercount').keyup(function() {
		var classes = $(this).attr('class').split(' ');
		for( var i in classes )
		{
			console.log(classes[i]);
			if( classes[i].indexOf('max:') != -1 )
			{
				$('#' + $(this).attr('name')).html('Chars Left: ' + (parseInt(classes[i].replace('max:', '')) - $(this).val().length));
				break;
			}
		}
	});
	
});

function html_entity_decode(str)
{
    //jd-tech.net
    var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
}

function confirmDelete(url)
{

	var agree = confirm("You are about to delete this report, this action is irreversable. Are you sure you want to continue?\n\nClick 'Ok' to continue");
	if (agree) {
		window.location.href = url;
	} else {
		// nothing
	}
}

function confirmExport(url)
{

	var agree = confirm("You are about the export these results to Acrobat PDF file format.\n\nPlease Note:\n1) This may take a few minutes depending on your connection speed.\n2) We limit the number of exported reports to 50 items.\n\n\nClick 'Ok' to proceed.");
	if (agree) {
		window.location.href = url;
	} else {
		// nothing
	}
}

function checkLength(ref, max, div)
{
	if (ref.value.length > max) {
		var orig_string = ref.value;
		crop = orig_string.substring(0, max);
		ref.value = crop;
	}
	rewriteDiv(max - ref.value.length, div);
}

function rewriteDiv(value, div)
{
	value = "Chars Left: "+value;
	if (document.getElementById) {
		//console.log("document.getElementById " + div);
		document.getElementById(div).innerHTML = value;
	} else if (document.all) {
		//console.log("document.all");
		document.all[div].innerHTML = value;
	} else if (document.layers) {
		//console.log("document.layers");
		document.layers[div].document.open();
		document.layers[div].document.write(value);
		document.layers[div].document.close();
	} else {
		//console.log("else");
		// nothing
	}
}

function toggleDiv(DivID)
{
	if (isVisible(DivID)) {
		turnOff(DivID);
	} else {
		turnOn(DivID);
	}
}

function turnOff(DivID)
{
	if (document.getElementById) { //gecko(NN6) & IE 5+
		document.getElementById(DivID).style.visibility = "hidden";
		document.getElementById(DivID).style.display = "none";
	} else if (document.all) { // IE 4+
		document.all[DivID].style.visibility = "hidden";
		document.all[DivID].style.display = "none";
	} else if (document.layers) { // NS4+
		document.layers[DivID].visibility = "hide";
		document.layers[DivID].display = "none";
	} else {
		// nothing
	}
}

function turnOn(DivID)
{
	if (document.getElementById) { //gecko(NN6) & IE 5+
		document.getElementById(DivID).style.visibility = "visible";
		document.getElementById(DivID).style.display = "block";
	} else if (document.all) { // IE 4+
		document.all[DivID].style.visibility = "visible";
		document.all[DivID].style.display = "block";
	} else if (document.layers) { // NS4+
		document.layers[DivID].visibility = "show";
		document.layers[DivID].display = "block";
	} else {
		// nothing
	}
}

function isVisible(DivID)
{
	if (document.getElementById) { //gecko(NN6) & IE 5+
		if (document.getElementById(DivID).style.visibility == "visible") {
			return true;
		} else {
			return false;
		}
	} else if (document.all) { // IE 4+
		if (document.all[DivID].style.visibility = "visible") {
			return true;
		} else {
			return false;
		}
	} else if (document.layers) { // NS4+
		if (document.layers[DivID].visibility = "show") {
			return true;
		} else {
			return false;
		}
	} else {
		// nothing
	}
}
