// Center a positionable element whose name is passed as 
// a parameter in the current window/frame, and show it
function centerOnWindow(elemID) {
    // 'obj' is the positionable object
    var obj = getRawObject(elemID);
    // window scroll factors
    var scrollX = 0, scrollY = 0;
    if (document.body && typeof document.body.scrollTop != "undefined") {
        scrollX += document.body.scrollLeft;
        scrollY += document.body.scrollTop;
        if (document.body.parentNode && 
            typeof document.body.parentNode.scrollTop != "undefined") {
            scrollX += document.body.parentNode.scrollLeft;
            scrollY += document.body.parentNode.scrollTop;
        }
    } else if (typeof window.pageXOffset != "undefined") {
        scrollX += window.pageXOffset;
        scrollY += window.pageYOffset;
    }
    var x = Math.round((getInsideWindowWidth()/2) - 
        (getObjectWidth(obj)/2)) + scrollX;
    var y = Math.round((getInsideWindowHeight()/2) - 
        (getObjectHeight(obj)/2)) + scrollY;
    shiftTo(obj, x, y);
    // show(obj);
}


// Return the available content height space in browser window
function getInsideWindowHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (isIE6CSS) {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}

function showOptions (param, defcheck, optionsType) {
	if (confirmPopupClosing()) {
		var paramid = param + '_id';
		var templateName = 'getparamlist.mhtml';
		switch (optionsType) {
			case 'country': templateName = 'getcountrylist.mhtml'; break;
			case 'producer': templateName = 'getproducerlist.mhtml'; break;
		}
		var url = '/' + templateName + '?param='+param+'&paramid='+paramid+'&defcheck='+defcheck+'&SessionID='+ses;
		var title = "Выбор параметров";
		showPopup(url, title);
		return false;
	}
}

function showInfo(url) {
	var title = "Помощь";
	// showPopup(url, title);
	var width = 450;
	var height = 450;
	var left = (screen.availWidth - width) / 2;
	var top = (screen.availHeight - height) / 2;
	var mypopup = window.open(url,'help','toolbar=no,scrollbars=no,directories=no,status=yes,menubar=no,resizable=yes,width=' + width + ',height=' + '450' + ',top=' + top + ',left=' + left);
	return false;
}

function showPopup(url, title) {
	var popup = document.getElementById('popupOptions');
	var optionsFrame1 = popup.getElementsByTagName('iframe')[0]; // iframe в index.mhtml
	if (optionsFrame1.contentWindow) {
		var optionsFrame1Doc = optionsFrame1.contentWindow.document;
		var optionsFrame2 = optionsFrame1Doc.getElementsByTagName('iframe')[0]; // iframe внутри iframe
		var popupTitle = optionsFrame1Doc.getElementsByTagName('h5')[0].firstChild;
	
		popup.style.display = 'block';
		popup.style.visibility = 'hidden';
		popupTitle.nodeValue = title;
	
		centerOnWindow('popupOptions');
		optionsFrame2.src = url;
	} else {
		var mypopup = window.open(url,'popup','toolbar=no,scrollbars=no,directories=no,status=yes,menubar=no,resizable=yes,width=450,height=450');
		// mypopup.window.title = title;
	}
}

function showPopupFinal() {
	if (!window.opener) {
		var popup = window.parent.parent.document.getElementById('popupOptions');
		popup.style.visibility = 'visible';
	}
}

function showCurCountFinal() {
	var popup = window.parent.document.getElementById('curResultsCount');
	popup.style.display = 'block';
	popup.style.visibility = 'visible';
}

function refreshParam(elem) {
	var myFrameBox = document.getElementById('curResultsCount');
	var myFrame = myFrameBox.getElementsByTagName('iframe')[0];

	if(elem) {
		myFrameBox.style.display = 'block';
		myFrameBox.style.visibility = 'hidden';
		var param = elem.name;
		if (elem.type == 'checkbox') {
			var value = elem.checked;
		} else {
			var value = elem.value;
		}
		var url = '/refreshparam.mhtml?param=' + param + '&value=' + value;
		myFrame.contentWindow.document.location.href = url;
	}
}

function confirmPopupClosing() {
	if (document.getElementById('popupOptions')) {
		var changed = document.getElementById('popupOptions').getElementsByTagName('iframe')[0].contentWindow.document.getElementsByTagName('iframe')[0].contentWindow.changed;
	} else {
		if (document.getElementsByTagName('iframe')[0]) {
			var changed = document.getElementsByTagName('iframe')[0].contentWindow.changed;
		}
	}
	if (changed) {
		var mymsg='Вы уверены, что не хотите сохранить выбранные параметры?';
		return window.confirm(mymsg);
	}
	return true;
}