
var path
var data
var dor1 = new Array("-1","-1")
var dor2 = new Array("-1","-1")
var bill1 = new Array("-1","-1")
var bill2 = new Array("-1","-1")
var bill3 = new Array("-1","-1")

function node(id,name,num, price) 
{
    // свойство узла
    this.id = id
    this.name = name
    this.num = num
    this.price = parseFloat(price)
    this.divId=function(){ 	alert( "n"+this.id)  }
}
function tree()
{
	this.data = new Array()
	this.addNode=function (id,name,num, price)
	{
		
		this.data['z'+id]=new node(id,name, num, price)
			//alert(this.data['z'+id].num)
	}
	this.clean=function (where){
	
		for(var key in this.data) 
 			delete(this.data[key])
 		this.show(where)
	}
	this.cleanEnd=function (){
	
		for(var key in this.data) 
 			delete(this.data[key])
 	
	}
	this.remove=function (id){
 		delete(this.data[id])
 		
	}
	this.orderToStr=function(){
		var str="";
		allprice=0
		var size=0
		
		for(var key in this.data) 
 		{
 			size++
 			str=str+"<tr><td>"+size+ "</td><td>" + this.data[key].name+ "</td><td>"+ this.data[key].price.toFixed(2) +"</td><td>"+ this.data[key].num+ "</td><td>"+ (this.data[key].price*this.data[key].num).toFixed(2) + "</td></tr>"
			allprice=allprice+this.data[key].num*this.data[key].price
		}
		
		return "<table class=tgood><tr><td width=30>№</td><td align=center >Наименование</td><td    align=center width=100>Цена за ед.</td><td    align=center width=50 align=center>Кол-во</td><td    align=center width=60 align=center>Сумма</td></tr>"+str+"</table><table><tr><td> Всего наименований: </td><td width=30>"+size + "</td><td align=right>  Итого: </td><td>" +(allprice).toFixed(2) + " руб</td></tr></table>"
		
	
	}
/*	this.showOrder=function (){
	
		var size=0
		var str="";
		this.basketCalc()
		for(var key in this.data) 
		{
				str=str+"<td>"+ this.data[key].name+ "</td><td>"+ this.data[key].num+ "</td><td>"+ this.data[key].price.toFixed(2) + "</td><td>"+(this.data[key].num*this.data[key].price).toFixed(2)+ "</td><td><a href='/removeC/"+key+"' title='удалить товар'>x</a></td></tr>"
 			size++
 		}
		if(login==1)
		{
			if(size>0)
				document.getElementById("center").innerHTML="<h1>Ваш заказ</h1><a href='/cleanC'>Очистить заказ</a><br><br><table class=tgood><tr><td    align=center >Наименование</td><td    align=center width=50 align=center>Кол-во</td><td    align=center width=70>Цена за ед.</td><td    align=center width=70>Сумма</td><td   align=center width=20>X</td></tr>"+str+"</table>"+aut+"<br><a href='/getOrder'>Оформить заказ</a>"
			else
				document.getElementById("center").innerHTML="<h1>Ваш заказ</h1>Нет товаров <br>"+aut+"<br>"
				
			//document.getElementById("right").innerHTML=""
			document.getElementById("bottom").innerHTML=""
		}
		else
		{
			if(size>0)
				document.getElementById("center").innerHTML="<h1>Ваш заказ</h1><a href='/cleanC'>Очистить заказ</a><br><br><table class=tgood><tr><td    align=center >Наименование</td><td    align=center width=50 align=center>Кол-во</td><td    align=center width=70>Цена за ед.</td><td    align=center width=70>Сумма</td><td width=20 align=center>X</td></tr>"+str+"</table>"
			else
						document.getElementById("center").innerHTML="<h1>Ваш заказ</h1>Нет товаров "+autC
		
		//	document.getElementById("right").innerHTML=""
			document.getElementById("bottom").innerHTML=""
		
		}
	}*/
	/*this.basketCalc=function (){
		if(user['proc']!=null && user['proc']>0 && skidka==0)
		{
			for(var key in this.data) 
 				this.data[key].price=this.data[key].price-(this.data[key].price*user['proc'])/100
 			skidka=user['proc']
 			document.getElementById("center").innerHTML='Ваша скидка составляет: '+ skidka+' %'
		}
	}*/
	this.changebasket=function (k, inp){
		//alert(this.data['z'+k].num)
			this.data['z'+k].num=inp.value
		//	obj=inp.value
			this.show('bottom')
	}
	this.show=function (where){
		var str="";
		
		var strbill="<table class=tgood width='100%'><tr><th>Наименование</th><th>Время начала</th><th>Время завершения</th><th>Цена</th></tr>";
		
		var size=0
		var itogo=0
		var priceBill=0
		var bch=360 // будни до 18
		var bch1=660// будни после 18
		var bch2=540// выходные до 18
		var bch3=840// выходные после 18
			
		var	bill1ch=300
		var	bill2ch=180
		var	bill3ch=500
		var allpriceBill=0
		
		// если нет товаров, то "Корзина пуста"
		//this.basketCalc()
		if(dor1[0]!=-1 && dor1[1]!=-1)
		{
			/*if(dor1[0] > dor1[1])
			{
				alert("Выберите корректное время")
				document.getElementById("dor1_0").focus()
				return
			}*/
			if(document.getElementById("holiday").checked)
			{
				if(dor1[1]<19)
				{
					 priceBill=(dor1[1]-dor1[0])*bch2
				}
				else if(dor1[0]>17)
				{
						priceBill=(dor1[1]-dor1[0])*bch3
				}
				else if(dor1[1]>17 && dor1[0]<19 )
				{
					priceBill=(18-dor1[0])*bch2 + (dor1[1]-18)*bch3
				}
			}
			else
			{
				if(dor1[1]<19)
				{
					 priceBill=(dor1[1]-dor1[0])*bch
				}
				else if(dor1[0]>17)
				{
						priceBill=(dor1[1]-dor1[0])*bch1
				}
				else if(dor1[1]>17 && dor1[0]<19 )
				{
					priceBill=(18-dor1[0])*bch + (dor1[1]-18)*bch1
				}
			}	
				if(dor1[0]==25) dor1[0]=1
				if(dor1[0]==26) dor1[0]=2
				if(dor1[1]==25) dor1[1]=1
				if(dor1[1]==26) dor1[1]=2
					
				strbill=strbill+"<tr><td align=center>Дорожка № 1</td><td align=center>"+dor1[0]+":00</td><td align=center>"+dor1[1]+":00</td><td>"+priceBill+"</td><tr>"
				allpriceBill=allpriceBill+priceBill
		}
		if(dor2[0]!=-1 && dor2[1]!=-1)
		{
			/*	if(dor2[0] > dor2[1])
			{
				alert("Выберите корректное время")
				document.getElementById("dor2_0").focus()
				return
			}*/
		if(document.getElementById("holiday").checked)
			{
				if(dor2[1]<19)
				{
					 priceBill=(dor2[1]-dor2[0])*bch2
				}
				else if(dor2[0]>17)
				{
						priceBill=(dor2[1]-dor2[0])*bch3
				}
				else if(dor2[1]>17 && dor2[0]<19 )
				{
					priceBill=(18-dor2[0])*bch2 + (dor2[1]-18)*bch3
				}
			}
			else
			{
				if(dor2[1]<19)
				{
					 priceBill=(dor2[1]-dor2[0])*bch
				}
				else if(dor2[0]>17)
				{
						priceBill=(dor2[1]-dor2[0])*bch1
				}
				else if(dor2[1]>17 && dor2[0]<19 )
				{
					priceBill=(18-dor2[0])*bch + (dor2[1]-18)*bch1
				}
			}	
				if(dor2[0]==25) dor2[0]=1
				if(dor2[0]==26) dor2[0]=2
				if(dor2[1]==25) dor2[1]=1
				if(dor2[1]==26) dor2[1]=2
				strbill=strbill+"<tr><td align=center>Дорожка № 2</td><td align=center>"+dor2[0]+":00</td><td align=center>"+dor2[1]+":00</td><td>"+priceBill+"</td><tr>"
			allpriceBill=allpriceBill+priceBill
		}
		if(bill1[0]!=-1 && bill1[0]!=-1)
		{	
			priceBill=(bill1[1]-bill1[0])*bill1ch
				if(bill1[0]==25) bill1[0]=1
				if(bill1[0]==26) bill1[0]=2
				if(bill1[1]==25) bill1[1]=1
				if(bill1[1]==26) bill1[1]=2
			strbill=strbill+"<tr><td align=center>русская пирамида(9 футов)</td><td align=center>"+bill1[0]+":00</td><td align=center>"+bill1[1]+":00</td><td>"+priceBill+"</td><tr>"
		allpriceBill=allpriceBill+priceBill
		}
		if(bill2[0]!=-1 && bill2[0]!=-1)
		{	
		
			priceBill=(bill2[1]-bill2[0])*bill2ch
				if(bill2[0]==25) bill2[0]=1
				if(bill2[0]==26) bill2[0]=2
				if(bill2[1]==25) bill2[1]=1
				if(bill2[1]==26) bill2[1]=2
			strbill=strbill+"<tr><td align=center>американский пул(9 футов)</td><td align=center>"+bill2[0]+":00</td><td align=center>"+bill2[1]+":00</td><td>"+priceBill+"</td><tr>"
			allpriceBill=allpriceBill+priceBill
		}
		if(bill3[0]!=-1 && bill3[0]!=-1)
		{	
			priceBill=(bill3[1]-bill3[0])*bill3ch
			if(bill3[0]==25) bill3[0]=1
			if(bill3[0]==26) bill3[0]=2
			if(bill3[1]==25) bill3[1]=1
			if(bill3[1]==26) bill3[1]=2
			strbill=strbill+"<tr><td align=center>русская пирамида(12 футов VIP-зал)</td><td align=center>"+bill3[0]+":00</td><td align=center>"+bill3[1]+":00</td><td>"+priceBill+"</td><tr>"
			allpriceBill=allpriceBill+priceBill
		}
		strbill=strbill+"</table>"
		if(allpriceBill==0)
			strbill=""
		for(var key in this.data) 
 		{
 		//alert(this.data[key].num)
 			str=str+"<tr><td>"+ this.data[key].name+  "</td><td><input class=ord type=text  onBlur='Tree.changebasket("+this.data[key].id+", this);'  value='"+ this.data[key].num+ "' ></td><td>"+ this.data[key].price + "</td><td>"+(this.data[key].num*this.data[key].price)+"</td><td align=center><a href='/remove/"+key+"' title='удалить'>x</a></td></tr>"
 			itogo=itogo+this.data[key].num*this.data[key].price
 			size++
 					
 		}
	
		if(size==0)
		{	
			document.getElementById(where).innerHTML="<h1>Ваш счет</h1>Из меню ничего не выбрано<br>"+strbill
			
			//document.getElementById("right").innerHTML=aut
				
		}		
		else
		{
			
				document.getElementById(where).innerHTML="<h1>Ваш счет</h1><table class=tgood width=100%><tr><th>Наименование</th><th    align=center width=45 >Кол-во</th><th   width=50>Цена</th><th     width=70>Сумма</th><th    align=center   >X</th></tr>"+str+"</table><br>"+strbill+"<div align=right><span class=itogo>Всего выбранно на сумму:</span><span class=price> "+(itogo+allpriceBill)+"р.</span></div>"
			//	document.getElementById("right").innerHTML=aut
		
		}
		document.getElementById("bottom").style.display="block"
		document.getElementById("bottom2").style.display="block"
	}
}
var Tree=new tree()
function showLink(event) {
	
	event = event || window.event
// для старой оперы event.target
var activ = document.activeElement || event.target
	
if((event.type == "click") && (event.button == 0))// клик левой клавишей
	if(activ.tagName=="A")// если ссылка
	{
		//alert(activ)
		
		//выделяем данные
		var tdata=""
		var tpath=""
		var newHref=activ.href
		if(newHref.lastIndexOf("?")!=-1)
			tdata=newHref.substring(newHref.lastIndexOf("?")+1, newHref.length)
		//alert(data)
		// выделяем путь
		if(tdata!="")
			tpath=newHref.replace("?"+tdata,"")
		else
			tpath=newHref
		// убираем префиксы
		tpath=tpath.replace("http://","")
		// убираем последний слеш	
		if(tpath.lastIndexOf ("/")==tpath.length-1)
		  tpath=tpath.substring(0, tpath.length-1)
		 	
		path=tpath.split("/")
		data=tdata.split("&")
			
		if(isFile(path))
			return
		event.returnValue=false
		if(!browser.isIE) // определение версии браузера  html включить в шаблон или библиотека
			event.preventDefault()
		load(path, data)
		return false;
	}
}
function isFile(сpath)
{
	// если указывает на  другой сайт то переходим
	if(location.href.indexOf(сpath[0])==-1)
		return true
	// если указывает на адресс текущего сайта и не на файл
	if(сpath.length==1 || сpath[сpath.length-1].indexOf(".")==-1)
		return false
	
	// если указывает на файл то переходим по ссылке
	return true
}
function load(cpath,cdata)
{
	//alert(cpath.length)
	//alert(cpath[cpath.length-1])
//	smenu(cpath[1])
	if(cpath[1]=="addbill"){
		
		dor1[0]=document.getElementById("dor1_0").value
		dor1[1]=document.getElementById("dor1_1").value
		
		dor2[0]=document.getElementById("dor2_0").value
		dor2[1]=document.getElementById("dor2_1").value
		
		bill1[0]=document.getElementById("bill1_0").value
		bill1[1]=document.getElementById("bill1_1").value
		
		bill2[0]=document.getElementById("bill2_0").value
		bill2[1]=document.getElementById("bill2_1").value
	
		bill3[0]=document.getElementById("bill3_0").value
		bill3[1]=document.getElementById("bill3_1").value
			
		Tree.show('bottom')
			return;
		//alert(dor1[0]+":"+dor1[1]+":"+dor2[0]+":"+dor2[1]+":"+bill1[0]+":"+bill1[1]+":"+bill2[0]+":"+bill2[1]+":"+bill3[0]+":"+bill3[1])//+":"+dor2[0]+":"+dor2[1]+":"+bill1[0]+":"+bill1[1]+":"+bill2[0]+":"+bill2[1]+":"+bill3[0]+":"+bill3[1])
		//return;
		/*if(kol==0)
		{		alert("выберите количество товара")
				return false;
		}
		
		var name=document.getElementById("n"+cpath[2]).innerHTML
		// проверка на число
		var price=document.getElementById("p"+cpath[2]).innerHTML
		Tree.addNode(cpath[2],name,kol, price)
		Tree.show('bottom')
		return*/
	}
	
	if(cpath[1]=="add"){
		
		var kol=document.getElementById("z"+cpath[2]).value
		if(kol==0)
		{		alert("выберите количество порций")
				return false;
		}
		
		var name=document.getElementById("n"+cpath[2]).innerHTML
		// проверка на число
		var price=document.getElementById("p"+cpath[2]).innerHTML
		Tree.addNode(cpath[2],name,kol, price)
		Tree.show('bottom')
		return
	}
	if(cpath[1]=="remove"){
		Tree.remove(cpath[2])
		Tree.show('bottom')
		return;
	}
	if(cpath[1]!="galery" && cpath[1]!="bill")
		document.getElementById("bottom").style.display="none"
	if(cpath[1]=="bill" && cpath[2]!="menubill")
	{
		document.getElementById("bottom").style.display="none"
		document.getElementById("bottom2").style.display="none"
	}
	if(cpath[1]=="bill" && cpath[2]=="menubill")
			document.getElementById("bottom2").style.display="block"
	doLoad("tree", "link", cpath, cdata, "")

}
function smenu(cpath)
{

	//return	// для строки меню
	var menu=document.getElementById("mmenu")
	for(var i=0; i<menu.childNodes.length; i++)
	{
			if(menu.childNodes[i].firstChild.href.indexOf(cpath)!=-1)
			{
				menu.childNodes[i].className="tmenu_sel"
				menu.childNodes[i].firstChild.className="menu_sel"
			}
			else
			{
			
				menu.childNodes[i].className="tmenu"
				menu.childNodes[i].firstChild.className="menu"
			}
	}
}
function smenu2(cpath)
{
	// для строки меню
	var menu=document.getElementById("cmenu").firstChild.firstChild
//	alert(menu.tagName)
	for(var i=0; i<menu.childNodes.length; i++)
	{
		if(menu.childNodes[i].childNodes.length>0)
			if(menu.childNodes[i].firstChild.href.indexOf(cpath)!=-1)
			{	//alert(menu.childNodes[i].firstChild.href)
			
				menu.childNodes[i].className="tmenu_sel"
				menu.childNodes[i].firstChild.className="menu_sel"
			}
			else
			{
			
				menu.childNodes[i].className="tmenu"
				menu.childNodes[i].firstChild.className="menu"
			}
		//if(menu.childNodes[i].firstChild)
	}
	//menu.firstChild

}