function openWnd(url,wnd_name,width,height,scrollbars){		
	var stScrollBar = (scrollbars ? 'yes' : 'no');
	newWnd = window.open(url,wnd_name,'width='+width+',height='+height+',status=yes,menubar=no,resizable=yes,scrollbars='+stScrollBar+',left='+String((screen.width-width)/2)+',top='+String((screen.height-height)/2));
	newWnd.focus();
	return false;
}

function set_location(str){
	document.location = str;
	return false;
}

function setplace(s_id, cur_id){
	var val = document.getElementById(s_id).value;
	var place = 0;
	if(document.getElementById('country' + cur_id).value > 0) place = document.getElementById('country' + cur_id).value;
	if(document.getElementById('region' + cur_id).value > 0) place = document.getElementById('region' + cur_id).value;
	if(document.getElementById('city' + cur_id).value > 0) place = document.getElementById('city' + cur_id).value;
	if(document.getElementById('metro' + cur_id).value > 0) place = document.getElementById('metro' + cur_id).value;
	document.getElementById('place' + cur_id).value = place;
	if(s_id == 'country' + cur_id){
		arr = get_regions_array(val);
		change_list('region', cur_id, arr);
		setplace('region' + cur_id, cur_id);
	}
	if(s_id == 'region' + cur_id){
		arr = get_cities_array(val);
		change_list('city', cur_id, arr);
		setplace('city' + cur_id, cur_id);
	}
	if(s_id == 'city' + cur_id){
		arr = get_metro_array(val);
		change_list('metro', cur_id, arr);
		setplace('metro' + cur_id, cur_id);
	}
}

function change_list(listType, listID, arr){
	list = document.getElementById(listType + listID);
	var i = 1;
	var j = 0;
	list.length = arr.length;
	newline = new Option;
	newline.value = '0';
	if(listType == 'region' || listType == 'city'){ newline.text = 'не выбран'; } else { newline.text = 'не выбрана'; }
	list.options[0] = newline;
	for(var n = 0; n < arr.length; n++){
	    newline = new Option;
	    j = arr[n].search(';');
	    newline.value = arr[n].slice(j + 1);
	    newline.text = arr[n].slice(0, j);
		list.options[i] = newline;
	    i++;
	}
	list.value = list.options[0].value;
}

function setSalesCurrents(branch, item){
	document.salesAddCatalog.branch.value = branch;
	document.salesAddCatalog.item.value = item;
	document.salesAddCatalog.submit();
	return false;
}

function showComment(num){
	var oDiv = document.getElementById('comment_' + num);
	if(oDiv.style.display == 'none'){
		oDiv.style.display = '';
	} else {
		oDiv.style.display = 'none';
	}
}

function shure(message){
	return confirm(message);
}

function showImg(thId, bDoResize){
	for(var i = 0; i < a.length; i++){
		if(oDiv = document.getElementById('div' + a[i])){
			oThumb = document.getElementById('th' + a[i]);
			oPreview = document.getElementById('preview' + a[i]);
			if(a[i] == thId){
				oDiv.style.display = '';
				oThumb.className = 'sel';
				oPreview.src = sources[i];
			} else {
				oDiv.style.display = 'none';
				oThumb.className = '';
			}
		}
	}
	if(bDoResize){
		showResizer();
	}
}

function showResizer(){
	if(oRes = document.getElementById('gt_resizer')){
		if(oMes = document.getElementById('g_measurer')){
			oRes.style.height = oMes.offsetHeight;
		}
	}
}

function showPreview(iThumbId, aIds, sNewSrc){
	var oThumb;
	var oPreview;
	for(var i = 0; i < aIds.length; i++){
		if(oThumb = document.getElementById('ph_thumb' + aIds[i])){
			if(aIds[i] == iThumbId){
				if(oPreview = document.getElementById('ph_preview')){
					oPreview.src = sNewSrc;
				}
				oThumb.className = 'sel';
			} else {
				oThumb.className = '';
			}
		}
	}
}

function showImageContainers(sToDo, iItem, iFrom, iTo, bWithDesc){
	oContainer = document.getElementById('container' + iItem);
	if(oContainer){
		if(sToDo == 'add'){
			var iCur = iFrom;
			while(oCur = document.getElementById('image' + iCur)){
				iCur++;
			}
			if(iCur <= iTo){
				var oItem = document.createElement('input');
				oItem.setAttribute('type', 'hidden');
				oItem.setAttribute('id', 'item' + iCur);
				oItem.setAttribute('name', 'item' + iCur);
				oItem.setAttribute('value', iItem);

				var oImage = document.createElement('input');
				oImage.setAttribute('type', 'file');
				oImage.setAttribute('id', 'image' + iCur);
				oImage.setAttribute('name', 'image' + iCur);

				if(bWithDesc){
					var oName = document.createElement('input');
					oName.setAttribute('type', 'text');
					oName.setAttribute('id', 'name' + iCur);
					oName.setAttribute('name', 'name' + iCur);
					oName.setAttribute('size', '30');
					oName.setAttribute('maxlength', '255');

					var oBreak1 = document.createElement('br');
					oBreak1.setAttribute('id', 'br1_' + iCur);
					var oBreak2 = document.createElement('br');
					oBreak2.setAttribute('id', 'br2_' + iCur);

					oContainer.appendChild(oBreak1);
					oContainer.appendChild(oBreak2);
				}
				oContainer.appendChild(oItem);
				oContainer.appendChild(oImage);
				if(bWithDesc){
					oContainer.appendChild(oName);
				}
				oContainer.style.height = oImage.offsetHeight * (iCur - iFrom + 1) * (1 + bWithDesc * 1.6);
			}
		}

		if(sToDo == 'del'){
			var iCur = iTo;
			while(!(oImage = document.getElementById('image' + iCur))){
				iCur--;
			}
			if(iCur > iFrom){
				oContainer.style.height = oImage.offsetHeight * (iCur - iFrom) * (1 + bWithDesc * 1.6);
				oItem = document.getElementById('item' + iCur);
				oName = document.getElementById('name' + iCur);
				oBreak1 = document.getElementById('br1_' + iCur);
				oBreak2 = document.getElementById('br2_' + iCur);
				oContainer.removeChild(oImage);
				oContainer.removeChild(oItem);
				if(bWithDesc){
					oContainer.removeChild(oName);
					oContainer.removeChild(oBreak1);
					oContainer.removeChild(oBreak2);
				}
			}
		}
	}
}

function showEvents(iMonth) {
	for (var i = 1; i <= 12; i++) {
		var oMonth = document.getElementById('month_' + i);
		var oEvents = document.getElementById('events_' + i);
		if (oMonth && oEvents) {
			oMonth.className = iMonth == i ? 'sel' : '';
			oEvents.style.display = iMonth == i ? '' : 'none';
		}
	}
}

var oHeli = false;
var oHeliCont = false;
var iStepX = 30;
var iStepT = 35;
var iTimeOut = 5000;
var iHW = 480;
var iHX = 0;
var iCW = 0;
var iCycles = 3;

function heliFlight() {
	oHeli = document.getElementById('helicopter');
	oHeliCont = document.getElementById('measurer');
	if(oHeli && oHeliCont) {
		startFlight();
	}
}

function startFlight() {
	stopFlight();
}

function stopFlight() {
	iCW = oHeliCont.offsetWidth;
	iHX = 0 - iHW - (iStepX - iCW % iStepX);
	oHeli.style.left = iHX + 'px';
	oHeli.style.width = iHW;
	if(iCycles) {
		setTimeout(heliStep, iTimeOut);
		iCycles--;
	}
}

function heliStep() {
	iCW = oHeliCont.offsetWidth;
	if(iHX + iHW <= iCW - iStepX) {
		iHX += iStepX;
		oHeli.style.left = iHX + 'px';
		setTimeout(heliStep, iStepT);
	} else if(iHX <= iCW - iStepX) {
		iHX += iStepX;
		oHeli.style.width = oHeli.offsetWidth - iStepX;
		oHeli.style.left = iHX + 'px';
		setTimeout(heliStep, iStepT);
	} else {
		stopFlight();
	}
}

function showSalePrices(sType) {
	var oSrcPrice = false, oRurPrice = false, i = 1;
	oSrcPrice = document.getElementById('srcval' + i);
	oRurPrice = document.getElementById('rurval' + i);
	while(oSrcPrice && oRurPrice) {
		if(sType == 'src') {
			oSrcPrice.style.display = '';
			oRurPrice.style.display = 'none';
			document.getElementById('rurval').className = '';
			document.getElementById('srcval').className = 'sel';
			setCookie('viewval', 'src');
		} else {
			oSrcPrice.style.display = 'none';
			oRurPrice.style.display = '';
			document.getElementById('rurval').className = 'sel';
			document.getElementById('srcval').className = '';
			setCookie('viewval', 'rur');
		}
		i++;
		oSrcPrice = document.getElementById('srcval' + i);
		oRurPrice = document.getElementById('rurval' + i);
	}
	return false;
}

function initSalePrices() {
	showSalePrices(getCookie('viewval'));
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}
