var complektArray = new Array;

function totalPriceFunc(thisInput,inptUAH,inptEUR)
{
	var uahOBJ = document.getElementById(inptUAH); 
	var eurOBJ = document.getElementById(inptEUR); 
	
	if (uahOBJ && eurOBJ && complektArray)
	{
		var uahStart = document.getElementById(inptUAH+"Start").value; 
		var eurStart = document.getElementById(inptEUR+"Start").value; 
		
		var tempInp = null;
		var priceArray = new Array;
		
		for (var i=0; i<complektArray.length; i++) 
		{
			tempInp = document.getElementById("complekt_"+complektArray[i]); 
			if (tempInp)
			{
				if (tempInp.checked)
				{
					priceArray = tempInp.value.split("|");

					uahStart = parseFloat(uahStart) + parseFloat(priceArray[0]);
					eurStart = parseFloat(eurStart) + parseFloat(priceArray[1]);
				}
			}
		}		
		
		uahOBJ.value = okr(uahStart);
		eurOBJ.value = okr(eurStart);
	}
}

function okr(x) 
{
	var str= Math.round(x*100)/100+'';
	str = (str.charAt(0)=='.' ? '0'+str:str);
	if (str.charAt(str.length-3)=='.') 
	{
		str+='';
	}
	else if (str.charAt(str.length-2)=='.') 
	{
		str+='0';
	}
	else 
		str+='.00';
	
	return str;
}

function resizeWindow(isCatalog)
{
	var mainWidth = document.getElementById('mainArea');
	var merka = document.getElementById('merka'); 
	var welcomeTxt = document.getElementById('welcomeTxt');
	//var welcomeTxtIns = document.getElementById('welcomeTxtIns');
	var welcomeNews = document.getElementById('welcomeNews');
	var welcomeVote = document.getElementById('welcomeVote');
	
	if (mainWidth)
	{
		//alert(merka.clientWidth);
		//alert(mainWidth.clientWidth);
		var aWidth = "";
		var cWidth = 0;
		//if (isCatalog)
		//{
			cWidth = 1250;
			aWidth = "1250px";
		//}
		//else
		//{
			//cWidth = 1250;
			//aWidth = "1250px";
		//}
			
		if (mainWidth.clientWidth > cWidth && merka.clientWidth > cWidth)
		{
			mainWidth.style.width = aWidth;
			if (welcomeTxt)
				welcomeTxt.style.width = "350px";
			//if (welcomeTxtIns)
				//welcomeTxtIns.style.width = "100%";				
			if (welcomeNews)
				welcomeNews.style.width = "430px";		
			
			if (welcomeVote)
				welcomeVote.style.width = "430px";						
		}
		else
		{
			mainWidth.style.width = "100%";
			if (welcomeTxt)
				welcomeTxt.style.width = "100%";		
			//if (welcomeTxtIns)
				//welcomeTxtIns.style.width = "100%";					
			if (welcomeNews)
				welcomeNews.style.width = "100%";	

			if (welcomeVote)
				welcomeVote.style.width = "100%";							
		}
	} 	
}

function RolloverImgs(imgObg,img) 
{ 
	//var imgObj = document.getElementById(img);
	if (imgObg && img)
	{
		imgObg.src = img;
	}
}


var prevBG = new Array;
var MenuSH = new Array;
var lastId = "";

function OverOutTD(idTd, type)
{
	var tdObj = document.getElementById(idTd);
	var widthTd = tdObj.offsetWidth;
	var addWidthTd = (tdObj.offsetWidth*30)/100;
	var widthSub = widthTd + addWidthTd;
	
	widthSub = (widthSub<150)?150:widthSub;
	
	if (tdObj)
	{
		//hideAll(idTd);
		
		var subObj = document.getElementById('sub_'+idTd);
		var aObj = document.getElementById('a_'+idTd);
		
		switch (type)
		{
			case "over":
				MenuSH[idTd]=1;	
				if (prevBG[idTd]=="" || !prevBG[idTd])
					prevBG[idTd] = tdObj.style.background;
				
				if (subObj)	
					tdObj.style.background = "url('images/menu_over2.gif') no-repeat center top";
				else
					tdObj.style.background = "url('images/menu_over1.gif') no-repeat center top";
					
				if (aObj)
				{
					aObj.className = "overMade";
				}				
				if (subObj)
				{
					//subObj.style.width = widthSub + "px";
					if (lastId == idTd)
						subObj.style.marginLeft = "-"+(addWidthTd+7)+ "px";
					//var lineObj = document.getElementById("line_"+idTd);
					//if (!lineObj)
					//{
						//addLine = "<div id=\"line_"+idTd+"\" style=\"background:#ffffff;height:4px;margin-bottom:14px;margin-left:"+(widthTd-7)+"px;\"></div>";
						//subObj.innerHTML = addLine + subObj.innerHTML;
					//} 					
					subObj.style.display = "block";
				}
				break;
				
			case "out":
				MenuSH[idTd]=0;
				setTimeout('outTD("'+idTd+'")',0);				
				break;
		}		
	}
}

function outTD(idTd)
{
	if (MenuSH[idTd]==0)
	{
		var tdObj = document.getElementById(idTd);
		if (prevBG[idTd] && prevBG[idTd]!="")
		{
			tdObj.style.background = prevBG[idTd];
			prevBG[idTd] = "";	
		}

		var subObj = document.getElementById('sub_'+idTd);
		if (subObj)
		{
			subObj.style.display = "none";
		}
				
		var aObj = document.getElementById('a_'+idTd);
		if (aObj)
		{
			aObj.className = "outMade";
		}		
	}
}

function hideAll(currentTd)
{
	for (i=0; i < MainDivs.length; i++) 
	{	
		if (MainDivs[i])
		{
			if (MainDivs[i]!=currentTd) 
			{
				var obj = document.getElementById(MainDivs[i]);
				if (obj)
				{
					if (prevBG[MainDivs[i]] && prevBG[MainDivs[i]]!="")
					{
						obj.style.background = prevBG[MainDivs[i]];
						prevBG[MainDivs[i]] = "";
					}
				}
				
				var subObj = document.getElementById('sub_'+MainDivs[i]);
				if (subObj)
				{
					subObj.style.display = "none";
				}
				
				var aObj = document.getElementById('a_'+MainDivs[i]);
				if (aObj)
				{
					aObj.className = "outMade";
				}					
			}
			
			lastId = MainDivs[i];				
		}
	}	
}

function checkKey2(e,id)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;	
	
	if (code == 13)
	{
		var submitForm = document.getElementById(id);
		if (submitForm)
		{
			submitForm.submit();
		}
	}
}


var itemInBasket = new Array;

function setBg(id,bgType,urlHome)
{
	if (itemInBasket)
		if (bgType == 2 && itemInBasket[id])
			return;
	
	var trObj = document.getElementById(id);
	
	if (trObj)
	{
		for (i=0; i<trObj.cells.length; i++)
		{
			switch (bgType)
			{
				case 1:
					color = "#ffffff";
					color2 = "#cccccc";
					break;
					
				case 2:
					color = "#afd6d2";
					color2 = "#ffffff";
					break;	
			}
			
			trObj.cells[i].style.background = color;
		}
	}
	
	var imgObj = document.getElementById('img_'+id);
	if (imgObj)
		switch (bgType)
		{
			case 1:
				imgObj.src = urlHome+"img/icons/basket2.gif";
				break;
					
			case 2:
				imgObj.src = urlHome+"img/icons/basket1.gif";
				break;	
		}
		
	var inpObj = document.getElementById('total_'+id);
	if (inpObj)
		inpObj.style.background = color2;
}

function replaceContent(id,url)
{	
	//ShowMessage();
	HTML_AJAX.replace(id,url);
}

function changeIcon(id,bgType,urlHome)
{
	var imgObj = document.getElementById('img_'+id);
	if (imgObj)
		switch (bgType)
		{
			case 1:
				imgObj.src = urlHome+"img/icons/basket_full.gif";
				break;
					
			case 2:
				imgObj.src = urlHome+"img/icons/basket.gif";
				break;	
		}	
}

//function addToBasket(id,SendURL,urlHome,idItem)
function addToBasket(id,SendURL,urlHome,idItem)
{		
	if (itemInBasket[id])
	{
		//return;
		SendURL = SendURL.replace(/a=add/gi, "a=del");
	}	
	//var inpObj = document.getElementById('total_'+id);
	//if (inpObj)
		//SendURL = SendURL + '&total['+idItem+']=' + inpObj.value;
		
	StatusMessage();
		
	//statusObj = document.getElementById('status_'+idItem);
	
	var callback = function(result) 
	{
		//setBg(id,1,urlHome);
		if (itemInBasket[id])
		{
			changeIcon(idItem,2,urlHome);
			itemInBasket[id] = 0;
		}
		else
		{
			changeIcon(idItem,1,urlHome);
			itemInBasket[id] = 1;
		}					
		
		var priceBasketSection = document.getElementById('priceBasketSection');
		if (priceBasketSection)
			if (priceBasketSection.style.display=="none")
				priceBasketSection.style.display = "block";
				
		var priceField = document.getElementById('priceBasket');
		if (priceField)
			priceField.innerHTML = result;
			
		//statusObj.style.display = "none";		
	}	

	//statusObj.style.display = "block";
	HTML_AJAX.onError = function(e) 
	{
		if (confirm("Some errors on server\r\n Repeat request?'"))
			HTML_AJAX.grab(SendURL, callback);
	}		
	
	HTML_AJAX.grab(SendURL, callback);
}

function getContent(idContent,idMenu,url)
{
	//var SendURL = "ajax.php?p="+p+"&"+url;
	var SendURL = url;
	var linkObj = null;
	var linkObjP = null;
			
	StatusMessage();
	
	var contentObj = document.getElementById(idContent);
	
	var loadingM = document.getElementById('loadingSI');
	if (loadingM)
	{
		//loadingM.style.width = "px"; 
		loadingM.style.display = "block"; 
	}
	
	var callback = function(result) 
	{
		contentObj.innerHTML = result;

		if (idMenu)
		{
			for (i=0; i<MainDivs.length;i++)
			{
				linkObj = document.getElementById(MainDivs[i]);
				linkObjP = document.getElementById('p_'+MainDivs[i]);
				
				if (linkObj && MainDivs[i] == idMenu)
				{
					linkObj.className = "current";
					linkObjP.style.display = "block";
				}
				else if (linkObj)
				{
					linkObj.className = "";
					linkObjP.style.display = "none";
				}
			}
		}		
	}	

	HTML_AJAX.onError = function(e) 
	{
		if (confirm("Some errors on server\r\n Repeat request?'"))
			HTML_AJAX.grab(SendURL, callback);
	}		
	
	HTML_AJAX.grab(SendURL, callback);
		
	
	/*
	replaceContent(idContent,SendURL);
	*/
} 

function getCalendar(id,p,url)
{
	var SendURL = "ajax.php?p="+p+"&"+url;
	//alert(SendURL);
	//window.scrollTo(0,0); 
	StatusMessage();
	
	var loadingM = document.getElementById('loadingSI');
	if (loadingM)
		loadingM.style.display = "block"; 
			
	replaceContent(id,SendURL);
}  

function setupMessage()
{
	var loading = document.getElementById('HTML_AJAX_LOADING');
	if (!loading) 
	{
		loading = document.createElement('div');
		loading.id = 'HTML_AJAX_LOADING';
		loading.innerHTML = '';		
		loading.className = 'HTML_AJAX_Loading';
		loading.count = 0;	

		document.body.insertBefore(loading,document.body.firstChild);
	}
	
	loading.style.display = "none";
}

function StatusMessage()
{
	setupMessage();
	return;
	
	//var stMessage = document.getElementById('HTML_AJAX_LOADING');
	//if (stMessage == null)
	//{
		//return;
	//}
		
	//var newPlace = document.getElementById(idContent);	
		
	//if (newPlace != null)
	//{
		//var insertInParent = (newPlace.parentNode)?newPlace.parentNode:newPlace.parent;			
		//insertInParent.appendChild(stMessage);		
	//}
	
	//var parentWidth = insertInParent.offsetWidth;
	
	//stMessage.style.left = "auto";
	//stMessage.style.right = 40 +  "px";
	//stMessage.style.width = 100 +  "px";
	//stMessage.style.height = 20 +  "px";
	//stMessage.style.top = "149px";
	//stMessage.style.display = "block";
}
