﻿// display_exportmap.js

//
var m_exportmapToolbarId = "ExportMapToolbar";
var m_exportmapStatus = "ExportMapStatusBar";
var m_exportmapXOffset = 0;
var m_exportmapYOffset = 0;
var printWidth = 297;

function exportMap()
{
	var i_exportType = document.getElementById('ExportType');
	var i_exportDPI = document.getElementById('ExportDPI');
	var i_exportWidth = document.getElementById('ExportWidth');
	var i_exportmapStatus = document.getElementById('ExportMapStatusBar');

	var m_exportType = i_exportType.options[i_exportType.selectedIndex].value;
	var m_exportTypeName = i_exportType.options[i_exportType.selectedIndex].innerHTML.toLowerCase();
	var m_exportDPI = i_exportDPI.value;
	var m_exportWidth = i_exportWidth.value;

	var isWidthOk=checkValue(i_exportWidth);
	var isDPIOk=checkValue(i_exportDPI);
	if ((!isWidthOk) || (!isDPIOk)) {
                i_exportmapStatus.style.color="Red";
		i_exportmapStatus.innerHTML = "Введите положительное число < 9999";
		return;
	}
	else {
		i_exportmapStatus.style.color="Black";
		i_exportmapStatus.innerHTML = "&nbsp;&nbsp;";
	}

	var fpBody = document.getElementById('GridResults1');
	fpBody.innerHTML = "<div><img src='images/callbackActivityIndicator.gif' align='middle'/> Обработка информации. . .</div>";
	fpBody=document.getElementById('Results_Panel_Collapse');
	if (fpBody.style.display=="none")
	    toggleConsolePanel('Results_Panel');
//	    toggleFloatingPanelState('Results','images/collapse.gif','images/expand.gif');

	var argument = "ControlID=Map1&ControlType=Map&EventArg=ExportMap&ExportType=" + m_exportType + "&ExportDPI=" + m_exportDPI + "&ExportWidth=" + m_exportWidth;
	var context = map.controlName;

	eval(exportMapCallbackFunctionString);
}

function startExportMap() {
    m_exportmapToolbar = document.getElementById(m_exportmapToolbarId);
    if (m_exportmapToolbar!=null) {
        m_exportmapToolbar.style.visibility = "visible";
    }
}

function closeExportMapToolbarTool() {
    m_exportmapToolbar = document.getElementById(m_exportmapToolbarId);
    if (m_exportmapToolbar!=null) {
        m_exportmapToolbar.style.visibility = "hidden";
    }
	var i_exportType = document.getElementById('ExportType');
	var i_exportDPI = document.getElementById('ExportDPI');
	var i_exportWidth = document.getElementById('ExportWidth');
	var i_exportWidthLabel = document.getElementById('ExportWidthLabel');
	var i_exportmapStatus = document.getElementById('ExportMapStatusBar');
	var i_exportWidthRow = document.getElementById('ExportWidthRow');
	var i_exportDPIRow = document.getElementById('ExportDPIRow');

	i_exportType.selectedIndex = 6;
	i_exportDPI.value = "150";
	i_exportWidth.value = "";
	i_exportWidthLabel.style.color = "#999999";
	i_exportWidth.style.backgroundColor = "#d9d9d9";
	i_exportDPI.style.backgroundColor = "";
	i_exportWidth.disabled = true;
	i_exportWidth.readonly = true;
	i_exportmapStatus.style.color="Black";
	i_exportmapStatus.innerHTML = "Выберите тип и размер файла и кликните &laquo;Создать файл&raquo;";
	i_exportDPIRow.style.display="";
}


// event handler for starting to drag toolbar around... mouse down
function dragExportMapToolbarStart(e) {
    m_exportmapToolbar = document.getElementById(m_exportmapToolbarId);
    if (m_exportmapToolbar!=null) {
        getXY(e);
        var box = calcElementPosition(m_exportmapToolbarId);
        m_exportmapXOffset = mouseX - box.left;
        m_exportmapYOffset = mouseY - box.top;
    }
    m_exportmapMoveFunction = document.onmousemove; 
    document.onmousemove = dragExportMapToolbarMove;
    document.onmouseup = dragExportMapToolbarStop;
    return false;
}

// event handler for toolbar drag movement... mousemove
function dragExportMapToolbarMove(e) {
    getXY(e);
    m_exportmapToolbar.style.left = (mouseX-m_exportmapXOffset) + "px";;
    m_exportmapToolbar.style.top = (mouseY-m_exportmapYOffset) + "px";
    return false;
}

// event handler for end of toolbar drag movement... mouseup
function dragExportMapToolbarStop(e) {
    document.onmousemove = m_exportmapMoveFunction;
    document.onmouseup = null;
    return false;
}

// set up the images for transparency in IE6
function setIE6ExportMapToolbarImages() {
    var imageId = "";
    var imgSrc = ""; 
    var imgObj = document.images["ExportMapToolbar_CloseButton"];
    if (imgObj!=null) {
        imgObj.src = "images/blank.gif";
        imgObj.style.filter =  "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/aspnet_client/ESRI/WebADF/images/dismiss.png)";
    }
}

function checkValue(field) {
	if (field.value!="") {
		if ((!isNumber(field.value)) || (field.value<1) || (field.value>9999)) {
		field.style.backgroundColor="#ffb2b2";
		return false;
		}
	}
	if (field.style.backgroundColor!="#d9d9d9") field.style.backgroundColor="";
	return true;
}

function isNumber(strInput) {
	var c = "";
	for (var i=0;i<strInput.length;i++) { 
		c = strInput.charAt(i);
		if ((c < "0") || (c > "9")) return false; 
	}
	return true;
}

function checkType() {
	var i_exportWidth = document.getElementById('ExportWidth');
	var i_exportWidthLabel = document.getElementById('ExportWidthLabel');
	var i_exportDPI = document.getElementById('ExportDPI');
	var i_exportType = document.getElementById('ExportType');
	var i_exportmapStatus = document.getElementById('ExportMapStatusBar');
	var m_exportType = i_exportType.options[i_exportType.selectedIndex].value;
	
	if (m_exportType<6 || m_exportType>9) // raster format
	{
	  i_exportDPI.value = "96";
	  calcWidth(96);
	  i_exportWidth.disabled = false;
	  i_exportWidth.readonly = false;
	  i_exportWidthLabel.style.color = "";
	  i_exportWidth.style.backgroundColor = "";
	  i_exportDPI.style.backgroundColor = "";
	  i_exportmapStatus.style.color="Black";
	  i_exportmapStatus.innerHTML = "Выберите тип и размер файла и кликните &laquo;Создать файл&raquo;";
	}
	else // vector format
	{
	  i_exportDPI.value = "150";
	  i_exportWidth.value = "";
	  i_exportWidth.disabled = true;
	  i_exportWidth.readonly = true;
	  i_exportWidthLabel.style.color = "#999999";
	  i_exportWidth.style.backgroundColor = "#d9d9d9";
	  i_exportDPI.style.backgroundColor = "";
	  i_exportmapStatus.style.color="Black";
	  i_exportmapStatus.innerHTML = "Выберите тип и размер файла и кликните &laquo;Создать файл&raquo;";
	}
	
}

function updateValue(theValue,fieldName) {
	var i_exportmapStatus = document.getElementById('ExportMapStatusBar');
	var f = document.forms[0];
	if (theValue!="") {
		if (isNumber(theValue)) {
			if ((theValue>0) && (theValue<=9999)) {
				eval('if (f.'+fieldName+'.style.backgroundColor!="#d9d9d9") f.'+fieldName+'.style.backgroundColor = "";');
		  		i_exportmapStatus.style.color="Black";
				i_exportmapStatus.innerHTML = "Выберите тип и размер файла и кликните &laquo;Создать файл&raquo;";
				if (fieldName == "ExportWidth") calcDpi(theValue);
				if (fieldName == "ExportDPI") calcWidth(theValue);
			}
			else {  
			eval('f.'+fieldName+'.style.backgroundColor = "#ffb2b2";');
		                i_exportmapStatus.style.color="Red";
				i_exportmapStatus.innerHTML = "Введите положительное число < 9999";
			}
		}
		else {
			eval('f.'+fieldName+'.style.backgroundColor = "#ffb2b2";');
	                i_exportmapStatus.style.color="Red";
			i_exportmapStatus.innerHTML = "Введите положительное число < 9999";
		}
	}
}

function calcWidth(dpiValue) {
	var i_exportType = document.getElementById('ExportType');
	var i_exportWidth = document.getElementById('ExportWidth');
	var m_exportType = i_exportType.options[i_exportType.selectedIndex].value;
	var newWidth = Math.round(printWidth*dpiValue/25.4);
	if (i_exportWidth.style.backgroundColor!="#d9d9d9") i_exportWidth.style.backgroundColor="";
	if (m_exportType<6 || m_exportType>9) i_exportWidth.value = newWidth;
}

function calcDpi(widthValue) {
	var i_exportDPI = document.getElementById('ExportDPI');
	var newDpi = Math.round(widthValue*25.4/printWidth);
	i_exportDPI.style.backgroundColor = "";
	i_exportDPI.value = newDpi;
}
