function getObj(id)
{
	return document.getElementById(id)? document.getElementById(id): false;
}

function showDialog(text)
{
	var size = new Array();
	size[0] = screen.width;
	size[1] = screen.height;

	topPos  = (size[1] - 350) / 2;
	leftPos = (size[0] - 350) / 2;
	window.open('dialog.php?text=' + text, '', 'width=450, height=250, top=' + topPos + ', left=' + leftPos ).focus();
//	window.showModalDialog('dialog.php?text=' + text, '', 'dialogWidth: 450px; dialogHeight: 250px; scroll: 0;');
	return false;
}

function setPredmet(obj)
{
	var predmet = getObj("catItem");
	var len = predmet.length;
	var nowSel = obj.value;

	if(floors[nowSel])
	{
		for(var i = len - 1; i >= 0; i--)
		{
			predmet[i].removeNode();
		}

		for(var i in floors[nowSel]) {
			var tmpElm = document.createElement("OPTION");
			tmpElm.setAttribute("value", i);
			tmpElm.innerHTML = floors[nowSel][i];
			predmet.insertAdjacentElement("afterBegin", tmpElm);
		}
	}
}

function setSearchPad(obj)
{
	var i = getMenuNum(obj.id);
	i++;
	while(getObj("sel_" + i))
	{
		getObj("sel_" + i).removeNode(true);
		i++;
	}

	var selId = obj.options[obj.selectedIndex].value;
	var tmpForm = document.createElement("FORM");
	tmpForm.setAttribute("action", "prog/search_pad.php");
	tmpForm.setAttribute("method", "POST");
	tmpForm.setAttribute("target", "search_frame");

	var tmpInp1 = document.createElement("INPUT");
	tmpInp1.setAttribute("type", "hidden");
	tmpInp1.setAttribute("name", "get_id");
	tmpInp1.setAttribute("value", selId);
	tmpForm.appendChild(tmpInp1);

	var tmpInp2 = document.createElement("INPUT");
	tmpInp2.setAttribute("type", "hidden");
	tmpInp2.setAttribute("name", "sel_id");
	tmpInp2.setAttribute("value", getMenuNum(obj.name));
	tmpForm.appendChild(tmpInp2);

	getObj("search_pad_form").appendChild(tmpForm);
	tmpForm.submit();
	tmpForm.removeNode(true);
}

function getMenuNum(objID)
{	
	var num = "";
	for(i = objID.length; i > 0; --i)
	{
		if(!isNaN(parseInt(objID.charAt(i), 10)))
			num = objID.charAt(i) + num;
	}
	return num;
}

function openHomework()
{
	window.location.href = "/homework/c" + getObj("catItem").options[getObj("catItem").selectedIndex].value;
}


if(window.HTMLElement){
	HTMLElement.prototype.removeNode = function(removeChildren) {
	  if (Boolean(removeChildren))
		return this.parentNode.removeChild(this);
	  else {
		var r=document.createRange();
		r.selectNodeContents(this);
		return this.parentNode.replaceChild(r.extractContents(),this);
	  }
	}
}

function updateCash(newPrice)
{
	getObj("userCash").innerHTML = newPrice;
}

function generateBannerCode(pid)
{
	var i = 0;
	var img = "";
	while(getObj("r" + i))
	{
		if(getObj("r" + i).checked == true)
			img = getObj("r" + i).value;
		i++;
	}
	if(img == "")
	{
		alert("Не выбрано изображение!");
		return;
	}

	var bText = "<img src=\"http://www.spishy.ru/hw_banners/" + img + "\" alt=\"Готовые домашние задания\" title=\"Готовые домашние задания\" />";
	if(img == "text")
		bText = getObj("t0").value;
	
	getObj("tout").value = "<a href=\"http://www.spishy.ru/homework/?partner=" + pid + "\">" + bText + "</a>";
}