CompareUrlDataBas = '/add_to_backet.phtm';
CompareWarez = '/add_to_compare.phtm';

function AddToCart(warez_id, name)
{
   alert("Товар добавлен в корзину!");
id_tov="";
kol_tov="0";

if (name =="") 
{
	id_tov=warez_id;
	kol_tov="1";
}
else 
{
	if (name.elements.length > 0)
	{
		for (var i=0; i<(name.elements.length); i++)
		{
			if ((name.elements[i].type == "text") && (name.elements[i].value > 0) || (name.elements[i].type == "hidden"))
			{
				id_tov = warez_id;
				kol_tov = name.elements[i].value;
			}
		}
	}

}


if ((id_tov != "")&&(kol_tov != ""))
{
	loadSource(CompareUrlDataBas + "?ware_id=" + id_tov + "&count=" + kol_tov);
	//alert(CompareUrlDataBas + "?ware_id=" + id_tov + "&count=" + kol_tov);
	id_tov="";
	kol_tov="0";
}
}


function AddToCompare(warez_id, grupp_id)
{
id_tov="";
id_tov=warez_id;


if (id_tov != "")
{
	loadSource(CompareWarez + "?ware_id=" + id_tov + "&grupp_id=" + grupp_id);
	id_tov="";
}
}




function loadSource(url) {
		document.getElementById('HiddenFrame').src = url;
}

function preload(name)
{   
   pics = new Image(); 
   pics.src = name;
}



function FindObj(n, d)
{
	var p,i,x;
	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
    	d=parent.frames[n.substring(p+1)].document;
    	n=n.substring(0,p);
   	}
  	if(!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=FindObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n);
   	return x; 
}




function ChangeQnty(f, id, sign) {
	if (eval("document."+ f +".quantity" + id + ".value") >= '0')
	{
		Qnty = parseInt(eval("document." + f +".quantity" + id + ".value"));
		Qnty = Qnty + parseInt(sign);
		
		if (Qnty <= 0) Qnty = "0";
		eval("document."+ f +".quantity" + id + ".value=" + Qnty);
	}

	if ((eval("document."+ f +".quantity" + id + ".value") == "") && (sign == 1)) 
	{	
		Qnty = 1;
		eval("document."+ f +".quantity" + id + ".value=" + Qnty);
	}
	
	return;

}

function ChangeQntyEmpty(name)
{
if((obj=FindObj(name))!=null)
{
	for (var i=0; i<(eval("document." + name + ".elements.length")); i++)
	{
		if (eval("document." + name + ".elements[" + i + "].type") == "text")
		{
			eval("document."+ name +".elements[" + i + "].value=''");
		}
	}
}
}


function checkFormCard(formname)
{      
      
	var inputs;
		inputs = new Array();
		inputs[0]=new Array();
		inputs[0][0]="id"; inputs[0][1]="ID карты";
		inputs[1]=new Array();
		inputs[1][0]="password"; inputs[1][1]="Пароль карты";
		
	
	var emptyInputCount = 0;
	var fullInput = 0;
	for (var i=0; i<inputs.length; i++)
	{
			if (formname[inputs[i][0]].value == "")
			{
							
				alert('Вы не заполнили поле ' + inputs[i][1]);
				formname[inputs[i][0]].focus();
				var emptyInputCount = 1;
				break;				
			}
	}


	if (emptyInputCount == 0){ formname.submit();}
	else { return false}


		
} 

function checkForm(formname)
{      
      
	var inputs;
		inputs = new Array();
		inputs[0]=new Array();
		inputs[0][0]="fio"; inputs[0][1]="ФИО";
		inputs[1]=new Array();
		inputs[1][0]="address"; inputs[1][1]="Адрес";
		inputs[2]=new Array();
		inputs[2][0]="phone"; inputs[2][1]="Телефон";
		
	
	var emptyInputCount = 0;
	var fullInput = 0;
	for (var i=0; i<inputs.length; i++)
	{
			if (formname[inputs[i][0]].value == "")
			{
							
				alert('Вы не заполнили поле ' + inputs[i][1]);
				formname[inputs[i][0]].focus();
				var emptyInputCount = 1;
				break;				
			}
	}

	if((formname['email'].value != "")&&(!isEmail(formname['email'].value))) 
				{
					alert('Необходимо указывать правильный e-mail.'); 
					formname['email'].focus(); 
					emptyInputCount = 1; 
				} 

	if (emptyInputCount == 0){ formname.submit();}
	else { return false}


		
} 

function checkEmail(formname) {
	
 	if(!isEmail(document.forms[formname].email.value)) 
				{
					alert('Необходимо указывать правильный e-mail.'); 
					document.forms[formname].email.focus(); 
					return false;
				} 
	else { document.forms[formname].submit(); }
}

function isEmail(string) {	
	return string.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}

	

// массив с флагами "открыт/закрыт" пунктов меню
var inelem = [];

var timerOver = [];
var timerOut = [];


startList = function() {
	if (document.getElementById) {
		// получаем список с пунктами меню
		navRoot = document.getElementById("nav");

/*		navRoot.onmouseout = function() {
			for (k = 0; k < this.childNodes.length; k++) {
				siblingNode = this.childNodes[k];
				if ((siblingNode.nodeName=="LI") && (siblingNode.className.indexOf("cur") == -1) && (siblingNode.className.indexOf("line")==-1)) {
			 		hideLI(siblingNode, k);
				}
			}
		}
*/
		// для всех дочерних нод списка с меню
		for (i=0; i<navRoot.childNodes.length; i++) {
			// текущая дочерняя нода
			node = navRoot.childNodes[i];

			// если текущая нода LI и она не является активным пунктом меню (он всегда открыт) и не является разделительной линией
			if ((node.nodeName=="LI") && (node.className.indexOf("cur") == -1) && (node.className.indexOf("line")==-1)) {

				// инициализируем флаг "открыт/закрыт"
				inelem[i] = false;
				
				var h3 = node.getElementsByTagName("h3");
				h3 = h3[0];
				

				// назначаем событие на нажатие мышки
				h3.onmousedown = function(ii) {
					return function() {
						inelem[ii] = true;

          						// сбрасываем все флаги кроме текущего, текущий наоборот взводим
          					for(k = 0; k < inelem.length; k++) {
          						inelem[k] = k == ii;
          						}

								// навсякий случай пробегаем по остальным пунктам меню и закрываем их
								for (k = 0; k < navRoot.childNodes.length; k++) {
									siblingNode = this.parentNode.parentNode.childNodes[k];
									if ((siblingNode.nodeName=="LI") && (siblingNode.className.indexOf("cur") == -1) && (siblingNode.className.indexOf("line")==-1)) {
										if(inelem[k])
											showLI(siblingNode, k);
										else
								 			hideLI(siblingNode, k);
									}
								}
	
        						// показываем текущий пункт меню
        						showLI(this.parentNode, ii);
        				}

				}(i);

/*				node.onmouseout = function(ii) {
					return function() {
						inelem[ii] = false;
						var li = this;

						var outTimeout = setTimeout('doOut();', 150);
						doOut = function() {
							if(!inelem[ii]) {
								hideLI(li, ii);
							}
						}
					}
				}(i);
				
				for (j=0; j<node.childNodes.length; j++) {
					nodechild = node.childNodes[j];
					nodechild.onmouseover = function(ii) {
						return function() {
							if(!inelem[ii]) {
								for(k = 0; k < inelem.length; k++) {
									inelem[k] = k == ii;
								}
							}
             			}
					}(i);

					nodechild.onmouseout = function(ii) {
						return function() {
							inelem[ii] = false;
						}
					}(i);
				}*/
			}
		}
	}
}


function showitem(id, height, maxheight) {
	var navRoot = document.getElementById("nav");
	var li = navRoot.childNodes[id];

	var lists = li.getElementsByTagName("ul");
	var list = lists[0];

       	
       	height += Math.round((maxheight - height) / 1.5);
       	if (height >= maxheight){
       		list.style.height="auto";
//       		list.style.height=maxheight;
       	} else {
      		if (li.className.indexOf("active") == -1) {
      			li.className+=" active";
      		}
       		list.style.height = height+'px';
       		timerOver[id] = setTimeout('showitem('+id+', '+height+', '+maxheight+')', 1);
 	}
}


showLI = function(li, id) {
       	var lists = li.getElementsByTagName("ul");
       	var list = lists[0];
       	if (li.className.indexOf("active") != -1) {
       		inelem[id] = false;
			list.style.height = "auto";
       		hideLI(li, id);
       		return;
       	}

       	list.style.top = '-10000px';
       	list.style.left = '-10000px';
       	list.style.position = "absolute";
       	list.style.width = '450px';
       	list.style.height = "auto";
       	list.style.display = "block";
       	var maxHeight =  Math.round(list.offsetHeight) - 10;
		//alert(maxHeight);
       	list.style.display = "";
       	list.style.position = "";
       	list.style.left = '';
       	list.style.width = '';

       	list.style.height="0px";

       	listheight = parseInt(list.style.height);
			
       	timerOver[id] = setTimeout('showitem('+id+', '+listheight+', '+maxHeight+')', 1);
}

function hideitem(id, height) {
	var navRoot = document.getElementById("nav");
	var li = navRoot.childNodes[id];

	var lists = li.getElementsByTagName("ul");
	var list = lists[0];

       	height -= Math.round(height / 2);
       	if (height <= 0) {
       		list.style.height = '0px';
       		li.className=li.className.replace("active", "");
       	} else {
       		list.style.height = height+'px';
       		timerOut[id] = setTimeout('hideitem('+id+', '+height+')', 1);
       	}
}


hideLI = function(li, id) {
	if(inelem[id]) return;

       	var lists = li.getElementsByTagName("ul");
       	var list = lists[0];

	if (list.style.height == 'auto' && list.offsetHeight > 0) {
		list.style.height = (list.offsetHeight) + "px";
	} else {
//		alert(list.style.height);
		list.style.height = "0px";
	}

	listheight = parseInt(list.style.height);
       	timerOut[id] = setTimeout('hideitem('+id+', '+listheight+');', 1);
}


window.onload=startList;

