function showhide(id)
{
	var e = document.getElementById(id);
	if (e.style.display != 'none') {
		e.style.display = 'none';
	}
	else {
		e.style.display = 'block';
	}
	return false;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var show_e;
var show_img;

var flag=0;

function sendForm(form) {
	var e = null;
	
	for(var i=0;i<form.childNodes.length;i++) {
		e = form.childNodes[i];
		
		if(e.getAttribute) {
			if(e.disabled) e.disabled = false;
			
			if((e.type=='checkbox')&&(!e.checked)) {
				e.value = '';
				e.checked = true;
			}
		}
	}
	
	/*for(i=0;i<form.elements.length;i++) {
		e = form.elements[i];
		if(e.disabled) e.disabled = false;
		
		if((e.type=='checkbox')&&(!e.checked)) {
			e.value = '';
			e.checked = true;
		}
	}*/
}

function itemTestForm(form) {
	var e = null;
	var res = '';
	var require;
	var mincount;
	var fieldname;

	if(form)
	for(var i=0;i<form.childNodes.length;i++) {
		e = form.childNodes[i];
		
		if(e.getAttribute) {
			require = e.getAttribute('require');
			mincount = e.getAttribute('mincount');
			fieldname = e.getAttribute('fieldname');
			
			if(require) {
				if((e.value==0)||(e.value=='')) {
					//if(res) res += '\n';
					res = res + 'не заполнено поле ' + require + '\n';
				}
			}
			
			if(mincount) {
				if((e.value.length<mincount)) {
					//if(res) res += '\n';
					res = res + 'неверное количество символов в поле ' + fieldname + '\n';
				}
			}
			
			res = res + itemTestForm(e);
		}
	}
	
	return res;
}

function testForm(form) {
	var e = null;
	var res = '';
	var require;
	var mincount;
	var fieldname;

	res = itemTestForm(form);
	
	/*for(var i=0;i<form.childNodes.length;i++) {
		e = form.childNodes[i];
		
		if(e.getAttribute) {
			require = e.getAttribute('require');
			mincount = e.getAttribute('mincount');
			fieldname = e.getAttribute('fieldname');
			
			if(require) {
				if((e.value==0)||(e.value=='')) {
					if(res) res += '\n';
					res = res + 'не заполнено поле ' + require;
				}
			}
			
			if(mincount) {
				if((e.value.length<mincount)) {
					if(res) res += '\n';
					res = res + 'неверное количество символов в поле ' + fieldname;
				}
			}
		}
	}*/
	
	/*for(i=0;i<form.elements.length;i++) {
		e = form.elements[i];
		
		if(e.require) {
			if((e.value==0)||(e.value=='')) {
				if(res) res += '\n';
				res = res + 'не заполнено поле ' + e.require;
			}
		}
		
		if(e.mincount) {
			if((e.value.length<e.mincount)) {
				if(res) res += '\n';
				res = res + 'неверное количество символов в поле ' + e.fieldname;
			}
		}
	}*/
	
	if(res) {
		res = 'Не верно заполнены поля:\n' + res;
		alert(res);
		return false;
	}
	
	return true;
}

function confirmQuery(text, url) {
	if(confirm(text)) location.replace(url);
}

function setSelect(name, value) {
	e = document.getElementById(name);
	for(var i=0;i<e.length;i++) {
		if(e.options[i].value==value) e.selectedIndex = i;
	}
}

function writeFlash(w, h, url) {
	document.write('<object id="flash_obj" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '" id="final" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="' + url + '" />');
	document.write('<param name="loop" value="true" />');
	document.write('<param name="menu" value="false" />');
	document.write('<param name="quality" value="best" />');
	document.write('<param name="scale" value="noscale" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="#000000" />');
	document.write('<embed src="' + url + '" loop="true" menu="false" quality="best" scale="noscale" wmode="transparent" bgcolor="#000000" width="' + w + '" height="' + h + '" name="final" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function showFlashWin(x,y,flash,title) {
	if (flag == 1) {
		sw.close();
		flag=0;
	}

	//x = x + 20;
	//y = y + 20;

	
	eval("sw=window.open('',title,'top=20,left=220,width='+x+',height='+y+',scrollbars=no,resizable=no')");

	sw.document.write("<html><head><title>"+title);
	sw.document.write("</title></head><body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>");
	sw.document.write("<center><table width='100%' height='100%' cellspacing=0 cellpadding=0 border=0><tr><td align=center>");
	sw.document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='"+x+"' height='"+y+"'><param name='movie' value='"+flash+"'><param name='quality' value='high'><embed src='"+flash+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='400' height='240'></embed></object>");
	sw.document.write("</td></tr></table>");
	sw.document.write("</center>");
	sw.document.write("</body></html>");
	sw.document.close();
	
	sw.focus();
	flag = 1;
}

function showwin(x,y,url) {
	showfoto(x, y, url);
}

function showfoto(x, y, url) {
	if (flag == 1) {
		sw.close();
		flag=0;
	}

	maxWidth = screen.availWidth;
	maxHeight = screen.availHeight;
	if ((x) > maxWidth)
		width = maxWidth;
	if ((y) > maxHeight)
		height = maxHeight - 50;

	eval("sw=window.open('','Изображение','top='+((maxHeight-y)/2-20)+',left='+((maxWidth-x)/2)+',width='+x+',height='+y+',scrollbars=no,resizable=no')");

	sw.document.write("<html><head><title>Изображение");
	sw.document.write("</title></head><body style='margin:0; background:#fff;'>");
	sw.document.write("<center><table width='100%' height='100%' cellspacing=0 cellpadding=0 border=0><tr><td align=center>");
	sw.document.write("<img border='0' src='"+url+"'>");
	sw.document.write("</td></tr></table>");
	sw.document.write("</center>");
	sw.document.write("</body></html>");
	sw.document.close();

	sw.focus();
	flag = 1;
}

function showphoto(x, y, url, title) {
	if (flag == 1) {
		sw.close();
		flag=0;
	}

	maxWidth = screen.availWidth;
	maxHeight = screen.availHeight;
	if ((x) > maxWidth)
		width = maxWidth;
	if ((y) > maxHeight)
		height = maxHeight - 50;

	var s = "sw=window.open('','"+title+"','top='+((maxHeight-y)/2-20)+',left='+((maxWidth-x)/2)+',width='+x+',height='+y+',scrollbars=no,resizable=no')";
	eval(s);

	sw.document.write("<html><head><title>"+title);
	sw.document.write("</title></head><body style='margin:0; background:#fff;'>");
	sw.document.write("<center><table width='100%' height='100%' cellspacing=0 cellpadding=0 border=0><tr><td align=center>");
	sw.document.write("<img border='0' src='"+url+"'>");
	sw.document.write("</td></tr></table>");
	sw.document.write("</center>");
	sw.document.write("</body></html>");
	sw.document.close();

	sw.focus();
	flag = 1;
}

function showflash(page,x,y)
{
	eval("sf=window.open(Name=\""+page+"\",\"FLASH\",\"top=20,left=220,width="+x+",height="+y+",scrollbars=no,resizable=no\")")
}

function domenu(id,e,img) { 
	if(id == pageid) {
		e = document.getElementById(e);
		img = document.getElementById(img);
		if(e&&img) {
			//show(e,img);
			show_e = e;
			show_img = img;
		} else alert("Ошибка");
	}
	return 0;
}

function showmenu() {
	if(show_e && show_img) show(show_e, show_img);
}

function catalog_bask(zena,count) {
	if(document.getElementById('catalog_count')) catalog_count.innerText = count;
	if(document.getElementById('catalog_zena')) catalog_zena.innerText = zena;
}

var ArrayBaskId = new Array();

function parce_baskinfo() {
	var s = document.getElementById('baskinfo').innerHTML;
	listID = s.split(';');
	for (var i = 0; i < listID.length; i++) {
		if(listID[i] != "") {
			m = listID[i].split(':');
			ArrayBaskId[m[0]] = m[1];
		}
	}
}

function catalog_check(id,e) {
	if(ArrayBaskId[id]!=null) {
		e = document.getElementById(e);
		/*if(e) e.checked = "true";
		else e.checked = "false";*/
		if(e) e.value = ArrayBaskId[id];
	}
}

function catalog_buy(cost,id,e) {
	e = document.getElementById(e);
	//ordertype = document.getElementById(ordertype);

	if(e==null) alert("Ошибка скрипта");
		
	//check = e.checked;
	colbuy = e.value;
	if(colbuy=="") colbuy = 0;
	
	location.href='./buy.php?id=' + id + '&colbuy=' + colbuy;
	return;
	
	if(window.showModalDialog==null) {
		location.href='./?act=buy&page=' + id + '&colbuy=' + colbuy;
		return;
	}
	
	/*if(document.frames('doframe')) document.frames('doframe').location.href='./?act=buy&frame=1&id=' + id;
	else location.href='./?act=buy&id=' + id;*/
	
	if(document.getElementById('catalog_count')) count = catalog_count.innerText;
	if(document.getElementById('catalog_zena')) oldzena = catalog_zena.innerText;
	
	if(zena==null) zena = 0;
	if(count==null) count = 0;
	
	oldcol = 0;
	if((ArrayBaskId[id]!=null)&&(ArrayBaskId[id]!=0)) {
		oldcol = ArrayBaskId[id];
		if(colbuy==0) {
			count--;
		}
	} else {
		if(colbuy>0) count++;
	}
	ArrayBaskId[id] = colbuy;
	
	cost = parseInt(zena) * colbuy + parseInt(oldzena) - parseInt(zena) * oldcol;
	
	catalog_bask(cost,count);

	window.showModalDialog('./?act=buy&frame=1&page=' + id + '&colbuy=' + colbuy, '', 'menubar=no,directories=no,location=no,toolbar=no,scrollbars=no,status=no,resizable=no,height=15,width=350,dialogWidth:350px; dialogHeight:15px');
}

function catalog_col_keypres(id,onewcol,zena,ocol,ordertype) {
	if(event.keyCode==13) catalog_modify(id,onewcol,zena,ocol,ordertype);
	if((event.keyCode<48)||(event.keyCode>57)) event.keyCode = 0;
}

function catalog_newcol_keypres(zena,id,text_buy,ordertype) {
	if(event.keyCode==13) catalog_buy(zena,id,text_buy,ordertype);
	if((event.keyCode<48)||(event.keyCode>57)) event.keyCode = 0;
}

function catalog_modify(id,onewcol,zena,ocol,ordertype) {
	onewcol = document.getElementById(onewcol);
	ocol = document.getElementById(ocol);
	ordertype = document.getElementById(ordertype);
	
	if((onewcol==null)||(ocol==null)||(ordertype==null)) alert('Ошибка определения ID. \n Ваш браузер не поддрердивает JavaScript');
	
	if(ordertype.checked) ot = 1;
	else ot = 0;

	newcol = onewcol.value;
	if(newcol=="") newcol = 0;
	oldcol = ocol.value;
	ocol.value = newcol;
	
	/*var sum = document.all.catalog_sum.innerText;
	var nalcurs = document.all.catalog_nalcurs.innerText;
	var beznalcurs = document.all.catalog_beznalcurs.innerText;
	var nalitogo = document.all.catalog_nalitogo.innerText;
	var beznalitogo = document.all.catalog_beznalitogo.innerText;
	
	sum = parseInt(sum,10) + zena*(newcol-oldcol);
	nalitogo = parseInt(nalitogo,10) + zena*(newcol-oldcol)*nalcurs;
	beznalitogo = parseInt(beznalitogo,10) + zena*(newcol-oldcol)*beznalcurs;
	
	document.all.catalog_sum.innerText = sum;
	document.all.catalog_nalitogo.innerText = nalitogo;
	document.all.catalog_beznalitogo.innerText = beznalitogo;
	
	onewcol.select();
	
	if(document.all.doframe) document.all.doframe.src='./?act=modify&fast=1&id=' + id + '&center=buy&newcol='+newcol;
	else location.href='./?act=modify&id=' + id + '&center=buy&newcol='+newcol;*/
	
	location.href='./?act=modify&id=' + id + '&center=buy&newcol='+newcol+'&ordertype='+ot;
}

// Создание массива для хранения ссылок на дочерние окна. 
/* Каждый член данного массива будет окном объекта, созданного
ниже с использованием метода createWindow. */
var windows = new Array();

function newWindow(url, wname) {
// Конструктор для окна
/* Данная функция может быть вызвана только функцией
   createWindow, которая приведена ниже. */
var features = "";
if (null != arguments[2])
   features = arguments[2];
return window.open(url, wname, features);
}

function createWindow(url, wname) {
// Добавление окна в семейство окон.
var features = arguments[2] == null ? "" : arguments[2];
windows[wname] = new newWindow(url, wname, features);
windows[wname].focus();
}

function closeWindows() {
// Закрытие всех окон, открытых методом addWindow. 
/* Для закрытия окна вызывается его метод close. */ 
/* Данная функция может быть вызвана в ходе события onunload
   для автоматического закрытия всех открытых окон.  */
for (w in windows)
   if (!windows[w].closed)
	  windows[w].close();
}

window.onunload=closeWindows;
