function onReadyMain() 
{
	jQuery.ajaxSetup({
		error:function(x,e){
			if (x.status==0)
			{
				alert('You are offline!!\n Please Check Your Network.');
			}
			else if (x.status==404)
			{
				alert('Requested URL not found.');
			}
			else if (x.status==500)
			{
				alert('Internel Server Error.');
			}
			else if (e=='parsererror')
			{
				alert('Error.\nParsing JSON Request failed.');
			}
			else if (e=='timeout')
			{
				alert('Request Time out.');
			}
			else
			{
				alert('Unknow Error.\n'+x.responseText);
			}
		}
	});
	// Disable Highlight for all areas	
	jQuery('#map_world_map area').each(function() {
		var data = jQuery(this).data('maphilight') || {};
		data.neverOn = !data.neverOn;
		jQuery(this).data('maphilight', data);
	});

	jQuery('#tblCountryList #CountryLink').live('click', function() {
		doSearchDBByCountry(
			jQuery('#CountryIso' , this).text(),
			jQuery('#CountryName', this).text()
		);
		return false;
	});


	jQuery('#map_world_map area').click(function() {
		var data = jQuery(this).data('maphilight') || {};
		if (data.alwaysOn)
		{
			var CountryIso = jQuery(this).attr('alt');
			var CountryName = jQuery(this).attr('title');
			doSearchDBByCountry(CountryIso, CountryName);
		}
		return false;
	});

	jQuery('.contentBlock').hide();
	if (jQuery('#url').val() == 'database')
	{
		jQuery('#divCountryList').show();
	}
	else
	{
		jQuery('#divGeneralInfo').show();
	}

	// Get Country List and HighLight those areas...
	jQuery.get(jQuery('#desinventarUrl').val() + '?cmd=cmdSearchCountryList',
		{},
		function(data) {
			// Keep Selected the following Countries
			if (jQuery('#url').val() == 'database')
			{
				jQuery.each(data.CountryList, function(index, value) {
					jQuery('#tblCountryList tbody').append(jQuery('#tblCountryList tbody>tr:first').clone());
					var row = jQuery('#tblCountryList tbody>tr:last');
					jQuery('#CountryIso', row).html(value.CountryIso);
					jQuery('#CountryName', row).text(value.CountryName);
					jQuery(row).show();
				});
			}
			jQuery.each(data.CountryList, function(index, value) {
				jQuery('#map_world_map area[alt="' + value.CountryIso + '"]').each(function()
				{
					var data = jQuery(this).data('maphilight') || {};
					data.alwaysOn = !data.alwaysOn;
					jQuery(this).data('maphilight', data).trigger('alwaysOn.maphilight');
				});
			});
			// This line should be the last one to avoid IE7/IE8 hangs...
			jQuery('#map_world').maphilight({fillColor:'3399cc',strokeColor:'2288bb',fillOpacity:0.5});
		},
		'jsonp'		
	);
} //onReadyMain

function doSearchDBByCountry(CountryIso,CountryName)
{
	var myUrl = jQuery('#desinventarUrl').val() + '/';
	console.log(myUrl);
	jQuery.post(myUrl,
		{
			cmd : 'cmdSearchDB',
			searchDBQuery : CountryIso,
			searchDBCountry : 1,
			searchDBType : 'FULLINFO',
			LangIsoCode : 'eng'
		},
		function (data) {
			if (data.Status == 'OK')
			{
				jQuery('#tbodyRegionList').find('>tr:gt(0)').remove();
				jQuery.each(data.RegionList, function(RegionId, value) {
					jQuery('#tbodyRegionList').append(jQuery('#tbodyRegionList #templaterow').clone().removeAttr('id'));
					var row = jQuery('#tbodyRegionList>tr:last');
					jQuery('#regionid'       ,row).html(RegionId);
					var myURL = jQuery('#desinventarUrlDB').val() + '?r=' + RegionId;
					jQuery('#regionlink', row).attr('href', myURL);
					jQuery('#regionlabel'    ,row).text(value.RegionLabel);
					var period = '';
					if (value.NumDatacards > 0)
					{
						period = value.PeriodBeginDate + ' - ' + value.PeriodEndDate;
					}
					jQuery('#period'         ,row).html(period);
					jQuery('#numberofrecords',row).html(value.NumDatacards);
				});
				jQuery('#tbodyRegionList #templaterow').css('background', '#888888');
				jQuery('#tbodyRegionList tr').css('background', '#dddddd');
				jQuery('#CountryNameTitle').text(CountryName);
				jQuery('.contentBlock').hide();
				jQuery('#divRegionInfo').show();
			}
		},
		'jsonp'
	);
}

