function kirjoita2(muuttuja) {
  document.write(muuttuja);
}

function mixIt(begin1, begin2, end1, end2, arg, chop){
	kirjoita2("<a href=\"mail");
	kirjoita2("to:");
	kirjoita2(begin1);
	kirjoita2(begin2+'@');
	kirjoita2(end1);
	kirjoita2(end2+'">');
	if (!arg) {
	  kirjoita2(begin1);
	  kirjoita2(begin2);
	  if(chop) kirjoita2('@<br />'+end1);
	  else kirjoita2('@'+end1);
	  kirjoita2(end2);
	}
	else {
	  kirjoita2(mixIt.arguments[4]);
	}
	kirjoita2('</a>');
}

function searchContacts(currentValue){
	if(currentValue != "" && currentValue != "__null"){
		document.cregSearchForm.submit();
	}
}

function disableType(item, isLast){//typeId, status, isLast){
	var curType;
	var status = item.checked;
	var typeId = item.value;

	// if checked item is the last item
	if(isLast == true){
		for(var i in mapTypes){
			curType = $('#mapInput_'+i);

			curType.attr('checked', (status == true ? 'checked' : ''));

			makeCall(new Array(i, status));
		}
	}
	else{
		if (status == true) {
			var checked = new Array();
			
			for (var i in mapTypes) {
				if (i != lastItem) {
					curType = $('#mapInput_' + i);
					
					if (curType.attr('checked') == true) {
						checked.push(i);
					}
				}
			}
			
			curType = $('#mapInput_' + lastItem);
			curType.attr('checked', (checked.length == (mapTypesCount) ? 'checked' : ''));
		}
		else{
			curType = $('#mapInput_' + lastItem);
			curType.attr('checked', '');
			
			curType = $('#mapInput_' + lastItem);
			curType.attr('checked', false);
		}
		
		makeCall(new Array(typeId, status));
	}
}

function mapUpdateLeftCol(params){
	var leftCol = $("#mapFirstCell");
	var leftColContent = $("#mapFirstCellContent");
	var backLinkContainer = $("#mapBackLinkContainer");

	backLinkContainer.empty();
	backLinkContainer.css('display','none');

	if (params[0] == 'province') {
		var html = '';
		var productionHtml = '';
		var partnerHtml = '';
		
		var hasProduction = false;
		var hasPartners = false;
		
		leftCol.removeClass();

		leftCol.addClass('provinceView');
		
		if(!params[1]){
			provinciesIndex = selectedProvince;
		}
		else{
			provinciesIndex = params[1];
			
			selectedProvince = params[1];
		}

		if(provincies[provinciesIndex]){
			var itemId;
			var itemName;
			
			for(var i in provincies[provinciesIndex]){
				itemId = i;
				itemName = provincies[provinciesIndex][i];

				if (mapItemTypes[i] != 6) {
					productionHtml += '<div><a href="javascript:selectMapItem(' + i + ');" title="' + itemName + '" class="mapLeftColItemsItemLink">' + itemName + '</a></div>';
					
					hasProduction = true;
				}
				else{
					partnerHtml += '<div><a href="javascript:selectMapItem(' + i + ');" title="' + itemName + '" class="mapLeftColItemsItemLink">' + itemName + '</a></div>';
					
					hasPartners = true;
				}
			}
		}
		
		if(hasProduction == true){
			productionHtml = '<div class="mapLeftColObjectLabel">'+leftColViewTexts[params[0]]+'</div>'+productionHtml;
		}
		
		if(hasPartners == true){
			partnerHtml = '<div class="mapLeftColObjectLabel mapLeftColObjectMiddleLabel">'+partnerLabel+'</div>'+partnerHtml;
		}
		
		html += productionHtml;
		html += partnerHtml;
		
		leftColContent.html(html);
		
		updateRightCol('province');
	}
	else if(params[0] == 'country'){
		leftCol.removeClass();
		
		leftCol.addClass('mapDefault');

		leftColContent.html(leftColViewTexts[params[0]]);
		
		updateRightCol('country');
	}
	else if(params[0] == 'type'){
		var html = '';
		
		leftCol.removeClass();
		
		leftCol.addClass(typeClasses[params[1]]);

		linkHtml = '<div class="mapBackToProvince">'
		+'<a href="javascript:gotoProvince();" title="" class="mapBackLink">'+backLink+'</a>'
		+'</div>';
		
		html += '<div class="mapLeftColItemName">'+leftColViewTexts[params[0]][params[2]]['name']+'</div>';
		
		if (leftColViewTexts[params[0]][params[2]]['info'] != "") {
			html += '<div class="mapLeftColItemInfo">' + leftColViewTexts[params[0]][params[2]]['info'] + '</div>';
		}
		
		if (leftColViewTexts[params[0]][params[2]]['description'] != "") {
			html += '<div class="mapLeftColItemDescription">' + leftColViewTexts[params[0]][params[2]]['description'] + '</div>';
		}
		
		leftColContent.html(html);
		
		backLinkContainer.html(linkHtml);
		backLinkContainer.css('display','block');
		
		updateRightCol('province');
	}
}

function updateRightCol(view){
	var rightCol = $("#mapThirdCellFirstItem");
	
	if(rightColTexts[view]){
		rightCol.html(rightColTexts[view]);
	}
	else{
		rightCol.empty()
	}
}

function gotoProvince(){
	mapUpdateLeftCol(new Array('province'));
	
	unSelectItems();
}

function initMap(){
	mapUpdateLeftCol(new Array('country'));
}

