	/* Delete Item from Shopcar*/
	function delItem(id)
	{	
		var tbl=document.getElementById('shop_items');
		var idx=document.getElementById(id).rowIndex;
		if(tbl){
			tbl.deleteRow(idx);
			calculateTotal();
		}
	}

	/* Clear all Items from Shopping Cart*/
	function clearAll()
	{	
		var tbl=document.getElementById('shop_items');
		if(!tbl){
			createCookie("order",'0,0',0);
			return;
		}
		var tblLen=tbl.tBodies[0].rows.length;
		while(tbl.tBodies[0].rows.length) tbl.deleteRow(1);
		calculateTotal();
	}

	/* Calculate Final Price */
	function calculateTotal()
	{
		var Order=new Array();
		var tbl=document.getElementById('shop_items');
		if(!tbl) return;
		var tblLen=tbl.tBodies[0].rows.length;
		var BGL=parseFloat(document.getElementById('usd_to_bgl').value);

		var totalSum=document.getElementById('total');
		var totalSumBG=document.getElementById('totalbg');
		var platformTest=document.getElementById('platform-test');

		var total=0.0;
		for(pid=0; pid < arrPlatforms.length; pid++)
			arrPlatforms[pid][2] = 0;

		for(i=0; i<tblLen; i++)
		{
			var _row = tbl.tBodies[0].rows[i];
			try
			{
				var catName = _row.getAttribute('cname');
				var itemName = _row.getAttribute('iname');
				//alert("i "+catName);
				if(catName == 'Дъно' || catName == 'Процесор' || catName == 'Вентилатор')
				{
					for(pix=0; pix<arrPlatforms.length; pix++)
					{
						var re = new RegExp(arrPlatforms[pix][0], "g")
						if(re.test(itemName))
						{
							arrPlatforms[pix][2] = 1;
							break;
						}
					}

				}
			}
			catch(e){}

			var id=tbl.tBodies[0].rows[i].getAttribute('id');
			var BGN_CURRENCY=tbl.tBodies[0].rows[i].getAttribute('bgn');
			var Price=parseFloat(tbl.tBodies[0].rows[i].cells[1].innerHTML);
			var Quantity=parseFloat(document.getElementById("q"+id).value);		
			var subTotal=Price*Quantity;
			if(!isNaN(subTotal))
			{
				if(BGN_CURRENCY==1){ 
					curency_label='&nbsp;лв.';
					total += subTotal/BGL;
				}
				else {
					curency_label='&nbsp;$';
					total += subTotal;
				}
				tbl.tBodies[0].rows[i].cells[4].innerHTML=moneyFormat(subTotal)+curency_label;
			}
			// Add, Update Order
			Order[i]=id+","+Quantity;
		}

		// Print Platform
		var strPlatform = "";
		var cntPlatform = 0;
		for(pix=0; pix<arrPlatforms.length; pix++)
		{
			if(arrPlatforms[pix][2] == 1)
			{
				strPlatform += arrPlatforms[pix][1]+", ";
				cntPlatform++;
			}
		}
		
		if(cntPlatform > 0)	strPlatform = strPlatform.substring(0,strPlatform.length-2);
		else if(cntPlatform == 0) strPlatform = "няма";
		if(platformTest){
			platformTest.innerHTML = "Платформа: <b>"+strPlatform+"</b>";
			if(cntPlatform > 1) 
				platformTest.innerHTML += "<br><span style='color:red;'>Избрали сте компоненти от повече от една платформи!</span>"
		}

		if(isNaN(total))
		{
			totalSum.innerHTML ='Всичко (с ДДС): <b> 0.00 USD</b><br/>';
			totalSumBG.innerHTML ='Всичко (с ДДС): <b> 0.00 BGN</b>';
		}
		else {
			totalSum.innerHTML ="Всичко (с ДДС): <b>"+moneyFormat(total)+' USD</b><br/>';
			totalSumBG.innerHTML ="Всичко (с ДДС): <b>"+moneyFormat(total*BGL)+' BGN</b>';
		}
		// Create Order cookie
		var str=serialize(Order);
		if(str=='')str='0,0';
		createCookie("order",str,0);
		if(BGL) createCookie("currency_rate",BGL,0);
	}

	/* Format money - returns the amount in the 0.00 format */
	function moneyFormat(amount)
	{
		amount -= 0;
		amount=(Math.round(amount*100))/100;
		return (amount==Math.floor(amount)) ? amount + '.00' : (  (amount*10==Math.floor(amount*10)) ? amount + '0' : amount);
	}

	/* COOKIE STUFF */
	/* Create Cookie */
	function createCookie(name,value,days)
	{
		if(days)
		{
			var date=new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires="; expires="+date.toGMTString();
		}
		else var expires="";
		document.cookie=name+"="+value+expires+";";
	}

	/* Serialize Array of number */
	function serialize(obj)
	{
		if(obj.length==0) return '';
		str=new String();
		for(el in obj) str+=","+obj[el];
		return str.substring(1);
	}
	
	//MENU BEHAIVIOR 
	function showSubMenu(mi)
	{
		// Close all
		var sub_items = document.getElementById('subitems');
		if(sub_items)
		{
			var all = sub_items.getElementsByTagName("span"); 
			if(all)  
			{
				for(var j=0; j<all.length; j++) 
					all[j].style.display='none';
			}
		}

		// Show only mi (menu item)
		var a=document.getElementById(mi); 
		if(a) 
			a.style.display='block';
	}

	function select(id)
	{
		var hidden_field_cid=document.getElementById('cid');
		if(hidden_field_cid) hidden_field_cid.value=id.substring(3);

		var all=document.getElementById('comp_kind').getElementsByTagName('a');
		if(all)  
			for(var j=0; j<all.length; j++)
				if(all[j].getAttribute('id')==id) all[j].style.fontWeight='bold';
				else all[j].style.fontWeight='normal';
	}

	function addEvent(elm, evType, fn, useCapture)
	// addEvent and removeEvent
	// cross-browser event handling for IE5+,  NS6 and Mozilla
	// By Scott Andrew
	{
	  if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	  } else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	  } else {
		alert("Handler could not be removed");
	  }
	}
	addEvent(window, "load", homepage_init);
	var _hp='http://www.advance.bg/portal';
	function homepage_init()
	{
		var t=window.document.getElementById("homepage-setter"); 
		if(!t) return;
		t.style.behavior="url(#default#homepage)"; 
		t.hp_href = t.href;
		t.href="javascript:homepage_set();";
		/*
		link.disabled = true;
		link.oldhref = link.href;
		link.href = "javascript:void(0)";
		*/
	}
	function homepage_set()
	{
		var t=window.document.getElementById("homepage-setter"); 
		t.setHomePage(_hp);
	}
	
	function showCompanyForm(flag)
	{
		if(flag){
			 document.getElementById('private').style.display='none';
			 document.getElementById('company').style.display='block';
			 document.getElementById('client_type').value='1'; // Client is company
			 document.getElementById('company_label').blur();
		}
		else{
			document.getElementById('private').style.display='block';
			document.getElementById('company').style.display='none';
			document.getElementById('client_type').value='2'; // Client is ordinary person
			document.getElementById('simple_user_label').blur();
		}
	}
	function showSpeedyForm(flag)
	{
		if(flag)
		{
			var clientType= document.getElementById('client_type').value;			
			if(document.getElementById('dcity').value=='' && document.getElementById('daddr').value=='' && document.getElementById('dpostcode').value=='')
			{
				if(clientType==2) // ordinary client
				{
					document.getElementById('dcity').value=document.getElementById('city').value;
					document.getElementById('daddr').value=document.getElementById('addr').value;
					document.getElementById('dpostcode').value=document.getElementById('postcode').value;
				}
				else if(clientType==1) // company
				{
					document.getElementById('dcity').value=document.getElementById('cpcity').value;
					document.getElementById('daddr').value=document.getElementById('cpaddr').value;
					document.getElementById('dpostcode').value=document.getElementById('cppostcode').value;				
				}
			}
			document.getElementById('_delivery_').style.display='block';
			document.getElementById('with_courier').checked='cheked';
		}
		else{
			document.getElementById('_delivery_').style.display='none';
			document.getElementById('without_courier').checked='cheked';
		}
	}

	function showCFGHelp(page)
	{
		var _w=760;
		var _h=540;
		popupWin=window.open(page, 'target01_window','menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=1,dependent=1,width='+_w+',height='+_h+',left=20,top=20');
	}
	function checkBrowser(string)
	{
		var detect = navigator.userAgent.toLowerCase();
		place=detect.indexOf(string)+1;
		return place;
	}
	function updateCurrency()
	{
		var BGL=document.getElementById('usd_to_bgl').value;
		if(BGL) createCookie("currency_rate",BGL,0);
	}
	function toggle_usr_srchfrm()
	{
		var frm = document.getElementById('search-user');
		var link = document.getElementById('toggle-srcfrm');
		if(frm && link)  
			if(frm.style.display == 'block'){
				frm.style.display = '';
				link.innerHTML = 'Избери Клиент';
			}
			else {
				frm.style.display =	'block';
				link.innerHTML = 'Скрий търсачката';
				var radio = document.getElementById('ct1');				
				if(radio) radio.checked = true;
				// Set focus to seach field
				var IFrameDoc = null;
				if(frm.contentDocument) IFrameDoc = frm.contentDocument; 				// For NS6
				else if(frm.contentWindow) IFrameDoc = frm.contentWindow.document;		// For IE5.5 and IE6
				else if(frm.document) IFrameDoc = frm.document;							// For IE5
				if(IFrameDoc) IFrameDoc.getElementById('src-field').focus();
			}		
	}
	function set_user(id,name)
	{
		var client = document.getElementById('client-name');
		var cid = document.getElementById('client-id');
		if(client) client.innerHTML = '<a href="users.php?usrid='+id+'" title="Виж профила на потребителя"><b>'+name+'</b></a> | ';
		if(cid) cid.value = id;
		toggle_usr_srchfrm();
	}
	function load_note(num)
	{
		var note = document.getElementById('note_'+num);
		if(!note) return;
		tinyMCE.setContent(note.innerHTML);
	}

	// Add hover to table
	function hover2tbl() 
	{
		var catList = document.getElementById('catlist');
		if(catList) {
			var tblLen=catList.tBodies[0].rows.length;
			for(var i=0; i<tblLen; i++)
			{
				if(catList.attachEvent)	{	// IE
					catList.tBodies[0].rows[i].attachEvent('onmouseover',light.closure(catList.tBodies[0].rows[i]));
					catList.tBodies[0].rows[i].attachEvent('onmouseout',dark.closure(catList.tBodies[0].rows[i]));
				}					
				else {	// Mozilla, Firefox, Opera
					catList.tBodies[0].rows[i].setAttribute('onmouseover',"javascript:this.style.backgroundColor='#BBEEBB'; ");
					catList.tBodies[0].rows[i].setAttribute('onmouseout',"javascript:this.style.backgroundColor=''; ");				
				}
			}
		}	
	}
	function light() { this.style.backgroundColor='#BBEEBB'; }
	function dark() { this.style.backgroundColor='';}
	Function.prototype.closure = function(obj)
	{
	  // Init object storage.
	  if (!window.__objs) window.__objs = [];

	  // Init closure storage.
	  if (!this.__closureFuncs) this.__closureFuncs = [];

	  // Make sure the object has an id and is stored in the object store.
	  var objId = obj.__closureObjId;
	  if (!objId) __objs[objId = obj.__closureObjId = __objs.length] = obj;

	  // See if we previously created a closure for this object/function pair.
	  var closureFunc = this.__closureFuncs[objId];
	  if(closureFunc) return closureFunc;

	  // Clear reference to keep the object out of the closure scope.
	  obj = null;

	  // Create the closure, store in cache and return result.
	  var me = this;
	  return this.__closureFuncs[objId] = function()
	  {
		return me.apply(__objs[objId], arguments);
	  };
	};