var popup;

/* Говно под названием Microsoft Internet Explorer снова отличилось в версии 7. Если версия 6 реализовывала
свойство DOM2 textContent, то в версии 7 зачем-то ушли от стандарта и назвали его innerText */
function getText(n) {
	if('textContent' in n) { return n.textContent; } else if('innerText' in n) { return n.innerText; } else { }
}
function putText(n,text) {
	if('textContent' in n) { n.textContent = text; } else if('innerText' in n) { n.innerText = text; } else { }
}

function product_dialog(p_id) {
	id = p_id;

	// Сколько уже выбрано
	var p_count = Number(getText(document.getElementById('p_'+p_id+'_c')));

	// Делаем так, чтобы при первом нажатии было выбрано 1
	if (!p_count) p_count = 1;

	// Сколько уже выбрано
	var p_price = Number(getText(document.getElementById('p_'+p_id+'_p')));

	// Название продукта
	var p_name = getText(document.getElementById('p_'+p_id+'_n'));

	// Описание продукта
	var p_description = getText(document.getElementById('p_'+p_id+'_d'));

	// Изображение, если есть
	var p_image = document.getElementById('p_'+p_id+'_i');
	p_image = (p_image == null)?false:true;

	window.status='Добавление '+p_name+' в корзину...';

	// Собрав всю эту информацию, генерируем на её основе всплывающее окошко
	popup = open('','product_dialog','width=500,height=400,location=0,menubar=0,resizeable=0,scrollbars=0,toolbar=0,directories=0,status=1');
	popup.document.open('text/html');

	popup.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
	popup.document.writeln('	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
	popup.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru" dir="ltr">');
	popup.document.writeln('	<head>');
	popup.document.writeln('		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
	popup.document.writeln('		<title>Добавление '+p_name+' в корзину :: Воронеж-Экспресс</title>');
	popup.document.writeln('		<link rel="stylesheet" media="all" type="text/css" href="style/style.css" />');
	popup.document.writeln('		<link rel="shortcut icon" href="images/favicon.png" />');
	popup.document.writeln('		<script language="Javascript">');
	popup.document.writeln('function putText(n,text) { if(\'textContent\' in n) { n.textContent = text; } else if(\'innerText\' in n) { n.innerText = text; } else { } }');
	popup.document.writeln('		</script>');
	popup.document.writeln('	</head>');
	popup.document.writeln('	<body><table border="0" width="100%" cellspacing="0" cellpadding="0">');
	popup.document.writeln('		<tr style="background-color: #E4E4E4">');
	popup.document.writeln('			<td width="19"><img border="0" src="images/ugol1.jpg" width="19" height="19"></td>');
	popup.document.writeln('			<td background="images/gor_up.gif" width="90%" colspan="3">&nbsp;</td>');
	popup.document.writeln('			<td width="19"><img border="0" src="images/ugol2.jpg" width="19" height="19"></td>');
	popup.document.writeln('		</tr>');
	popup.document.writeln('		<tr>');
	popup.document.writeln('			<td bgcolor="#E4E4E4"><img border="0" src="images/gray_left.gif" width="19" height="34"></td>');
	popup.document.writeln('			<td width="10" bgcolor="#ADADAD" background="images/red.gif">');
	popup.document.writeln('			<img border="0" src="images/red_left.gif" width="10" height="34"></td>');
	popup.document.writeln('			<td width="100%" background="images/red.gif" bgcolor="#DA251D" class="head"><nobr>'+p_name+'</nobr></td>');
	popup.document.writeln('			<td width="10" bgcolor="#ADADAD" background="images/red.gif"><img border="0" src="images/red_right.gif" width="10" height="34"></td>');
	popup.document.writeln('			<td bgcolor="#E4E4E4"><img border="0" src="images/gray_right.gif" width="19" height="34"></td>');
	popup.document.writeln('		</tr>');
	popup.document.writeln('		<tr>');
	popup.document.writeln('			<td background="images/vert_left.gif" bgcolor="#E4E4E4">&nbsp;</td>');
	popup.document.writeln('			<td colspan="3"><table>');
	popup.document.writeln('				<tr>');
	popup.document.writeln('					<td><img border="0" src="'+((p_image)?'products-image.php?product='+p_id+'&mode=normal':'images/favicon.png')+'" alt="'+p_name+'" align="left" hspace="20" vspace="14"></td>');
	popup.document.writeln('					<td valign="top">');
	popup.document.writeln('						'+p_description+'');
	popup.document.writeln('<!--');
	popup.document.writeln('						<p class="important">Объектив: <b>28мм F5.6</b><br>Фокусировка: <b>FF</b></p>');
	popup.document.writeln('						<p>');
	popup.document.writeln('							Компактная и стильная камера, с которой Вы всегда и в любом месте будете выглядеть современно.<br> Эта камера с фиксированным фокусным расстоянием подходит даже самым неопытным владельцам, поскольку имеет самые необходимые функции - например, режим подавления эффекта красных глаз, позволяющий избежать этой самой распространенной в фотосъемке ошибки!');
	popup.document.writeln('						</p>');
	popup.document.writeln('-->');
	popup.document.writeln('						<h5>Цена: '+p_price+' руб.</h5>');
	popup.document.writeln('					</td>');
	popup.document.writeln('				</tr>');
	popup.document.writeln('				<tr><td colspan="2"><form name="product_choose" style="display: inline">');
	popup.document.writeln('					<input type="hidden" name="product" value="'+p_id+'" />');
	popup.document.writeln('					<p class="с">');
	popup.document.writeln('						Введите количество: <input type="text" name="count" value="'+p_count+'" size="3" onchange="tpr = document.getElementById(\'tprice\'); if (tpr) putText(tpr, this.value * this.form.price.value);" />&nbsp;');
	popup.document.writeln('						Итоговая сумма: <b><span id="tprice">'+p_price*p_count+'</span> руб</b>');
	popup.document.writeln('					</p>');
	popup.document.writeln('<!--					<p class="с"></p><br>-->');
	popup.document.writeln('					<p class="с">');
	popup.document.writeln('						<input type="hidden" name="price" value="'+p_price+'" size="3" />');
	popup.document.writeln('						<input type="image" onMouseover="src=\'images/ok_on.gif\'; this.style.cursor=\'hand\';" onMouseout="src=\'images/ok.gif\';" src="images/ok.gif" alt="OK" border="0" width="41" height="18" onclick="opener.p_confirm(this.form); return false;">&nbsp;');
	popup.document.writeln('						<input type="image" onMouseover="src=\'images/cancel_on.gif\'; this.style.cursor=\'hand\';" onMouseout="src=\'images/cancel.gif\';" src="images/cancel.gif" alt="Отмена" border="0" width="58" height="18" onclick="opener.p_reset(); return false;">');
	popup.document.writeln('					</p>');
	popup.document.writeln('				</form></td></tr>');
	popup.document.writeln('			</table></td>');
	popup.document.writeln('			<td background="images/vert_right.gif" bgcolor="#E4E4E4">&nbsp;</td>');
	popup.document.writeln('		</tr>');
	popup.document.writeln('		<tr style="background-color: #E4E4E4">');
	popup.document.writeln('			<td><img border="0" src="images/ugol4.jpg" width="19" height="19"></td>');
	popup.document.writeln('			<td background="images/gor_down.gif" colspan="3">&nbsp;</td>');
	popup.document.writeln('			<td><img border="0" src="images/ugol3.jpg" width="19" height="19"></td>');
	popup.document.writeln('		</tr>');
	popup.document.writeln('	</table></body>');
	popup.document.writeln('</html>');

	popup.document.close();
	popup.focus();
}

function product_delete(p_id) {
	var id = Number(p_id)
	loadXMLDoc('minibasket.php?product='+id+'&count=0&hash='+Math.random());
	c = document.getElementById('p_'+id+'_c'); if (c) putText(c,'');
	t = document.getElementById('p_'+id+'_t'); if (t) putText(t,0);
	prow = document.getElementById('product_'+id); if (prow) prow.style.display = 'none';
}

function p_basket(itemcount,typecount,totalprice) {
	i = document.getElementById('itemcount'); if (i) putText(i,itemcount);
	t = document.getElementById('typecount'); if (t) putText(t,typecount);
	p = document.getElementById('totalprice'); if (p) putText(p,totalprice);
}

function p_confirm(obj) {
	var id = Number(obj.product.value)
	var count = Number(obj.count.value);
	loadXMLDoc('minibasket.php?product='+id+'&count='+count+'&doreset=1&hash='+Math.random());

	c = document.getElementById('p_'+id+'_c'); if (c) putText(c,(count==0)?'':count);
	t = document.getElementById('p_'+id+'_t'); if (t) putText(t,count*Number(obj.price.value));
}

function p_reset() {
	popup.close();
	window.status='';
}

