﻿
// Shows/Hides country checkboxes for a given region
function showHideCountries(regionId, cntrPrefix) {
	var cntrBlocks;
	if (typeof (cntrPrefix) != 'undefined' && cntrPrefix)
		cntrBlocks = $("div[" + cntrPrefix + "regionBoxId='" + regionId + "'] div[" + cntrPrefix + "region='" + regionId + "']");
	else
		cntrBlocks = $("div[regionBoxId='" + regionId + "'] div[region='" + regionId + "']");
	for (var i = 0; i < cntrBlocks.length; i++) {
			var cntrBlock = cntrBlocks[i];
			if (cntrBlock) {
				if (cntrBlock.style.display == "none")
					cntrBlock.style.display = "block";
				else
					cntrBlock.style.display = "none";
			}
		}
	return false;
}

// Checks/unchecks all checkboxes for countries of this region
function onRegionCheck(regionId, checked, cntrPrefix, selDivId, displayDivId) {
	if (regionId <= 0) return;
	var $chks;
	if (typeof (cntrPrefix) != 'undefined' && cntrPrefix)
		$chks = $("div[" + cntrPrefix + "region='" + regionId + "'] :checkbox");
	else
		$chks = $("div[region='" + regionId + "'] :checkbox");
		
	if ($chks.length == 0) return;
	for (var i = 0; i < $chks.length; i++) {
		$chks[i].checked = checked;
	}

	ToggleCntry(selDivId, displayDivId);
}

function onCountryCheck(regionId, checked, cntrPrefix) {
	if (regionId <= 0) return;

	var $regChkbox, $chksLen;
	if (typeof (cntrPrefix) != 'undefined' && cntrPrefix) {
		$regChkbox = $("span[" + cntrPrefix + "regionId='" + regionId + "'] :checkbox");
		$chksLen = $("div[" + cntrPrefix + "region='" + regionId + "'] input:checkbox:checked").length;
	}
	else {
		$regChkbox = $("span[regionId='" + regionId + "'] :checkbox");
		$chksLen = $("div[region='" + regionId + "'] input:checkbox:checked").length;
	}
	
	if ($regChkbox.length > 0 && $chksLen > 0)
		$regChkbox.attr('checked', true);
	else
		$regChkbox.attr('checked', false);
}

function ToggleAllCntrySel(checked, selDivId, displayDivId) {
	var $chks = $("#" + selDivId + " :checkbox");
	if ($chks.length == 0) return;
	for (var i = 0; i < $chks.length; i++) {
		$chks[i].checked = checked;
	}

	ToggleCntry(selDivId, displayDivId);
}

function ToggleCntry(selDivId, displayDivId) {
	var names = '';
	var $chks = $("#" + selDivId + " input:checkbox:checked");
	for (var i = 0; i < $chks.length; i++) {
		if (typeof ($chks[i].value) != 'undefined' && isNaN($chks[i].value) && -1 == names.indexOf($chks[i].value, 0)) {
			if (names.length > 0) 
				names += ', ';
			names += $chks[i].value;
		}
	}

	FillDisplayDiv(names, displayDivId);
}

function setCountry(selDivId, displayDivId, cntryInsertId, errMsgDivId, cntrPrefix) {
	var text = $("#" + cntryInsertId).val();
	$("#" + errMsgDivId).text('');
	var errCountries = '';

	if (text == "") return;
	var countryArray = text.split(',');
	for (i = 0; i < countryArray.length; i++) {
		var countryName = countryArray[i].replace(/^\s+|\s+$/g, ""); //remove leading and trail sym
		var found = checkCountryByName(countryName, selDivId, displayDivId, cntrPrefix);
		if (!found) {
			if (errCountries.length > 0)
				errCountries += ', ';
			errCountries += countryArray[i];
		}
	}

	$("#" + cntryInsertId).val('');
	if (errCountries.length > 0) {
		errCountries = 'Not recognized countries: ' + errCountries;
		$("#" + errMsgDivId).text(errCountries);
	}
}

function checkCountryByName(name, selDivId, displayDivId, cntrPrefix) {
	if (!name || name.length == 0) return false;

	var retVal = false;
	var names = '';
	var $chks = $("#" + selDivId + " :checkbox");
	for (var i = 0; i < $chks.length; i++) {
		if (typeof ($chks[i].value) != 'undefined' && isNaN($chks[i].value)) {
			if ($chks[i].checked && $chks[i].value.toLowerCase() == name.toLowerCase()) {
				retVal = true;
				names = FillCountryNameList(names, $chks[i].value);
			}
			else if ($chks[i].checked && $chks[i].value.toLowerCase() != name.toLowerCase()) {
				names = FillCountryNameList(names, $chks[i].value);
			}
			else if ($chks[i].value.toLowerCase() == name.toLowerCase()) {
				retVal = true;
				$chks[i].checked = true;
				names = FillCountryNameList(names, $chks[i].value);

				var regionId = $chks[i].parentNode[cntrPrefix + 'region'];
				if (typeof (regionId) != 'undefined' && regionId.length > 0) {
					onCountryCheck(parseInt(regionId), true, cntrPrefix);
				}
			}
		}
	}
	FillDisplayDiv(names, displayDivId);
	return retVal;
}

function FillCountryNameList(names, countryName) {
	if (-1 == names.indexOf(countryName, 0)) {
		if (names.length > 0)
			names += ', ';
		names += countryName;
	}
	
	return names;
}

function FillDisplayDiv(names, displayDivId) {
	if (typeof (names) == 'undefined' || names == null) return;

	if (names.length > 0) names = 'Selected: ' + names;
	else names = 'Selected: None';
	$("#" + displayDivId).text(names);
}

function getSelectedCountryIdsList() {
	var idList = '';
	var $chks = $("#" + g_GeoSelectDiv + " input:checkbox:checked");
	for (var i = 0; i < $chks.length; i++) {
		if (typeof ($chks[i].value) != 'undefined' && typeof ($chks[i].id) != 'undefined') {
			// Try to get hidden field value contains
			var index = $chks[i].id.indexOf(g_checkGeoBoxIdSuffix); // Should be parameter
			if (index >= 0) {
				var hiddenId = $chks[i].id.substr(0, index);
				hiddenId = hiddenId + g_hiddenGeoIdSuffix;
				var $hidden = $("#" + hiddenId);
				if (typeof ($hidden[0].value) != 'undefined') {
					if (idList.length > 0)
						idList += ',';
					idList += $hidden[0].value;
				}
			}
		}
	}	 
	return idList;
}
