function popWindow(url,w,h,scrollbars) 
{
	if(scrollbars) scrollbars = ",scrollbars=yes";
	else scrollbars = "";
	window.open(url, '',  'top=0,left=0,width='+w+',height='+h+scrollbars);
}
function isEmptyStr(strValue) 
{
   strValue = trimAll(strValue);
   if(strValue.length > 0){
     return false;
   }
   return true;
}
function check_all(name)
{
	for (var i = 0; i < document.forms['form'].elements.length; i++)
	{
		var e = document.forms['form'].elements[i];
		if (e.name != name)
		{
			e.checked = eval("document.forms['form']."+name).checked;
		}
	}
}
function trimAll( strValue ) 
{
 var objRegExp = /^(\s*)$/;

    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
function ChangeCountry()
{
	if($('country_id').value == 26) 
	{
		$('region_id').disabled = false;
	}
	else 
	{
		$('region_id').disabled = true;
	}
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
function countMsgCharacters(obj,chars,remainingChars)
{
   var msg= obj.value;
   var len = msg.length;
   
   if (len > chars)
   {
	   obj.value=msg.substr(0,chars);
	   len = chars;
   }
   if (typeof remainingChars == 'undefined') remainingChars = $('remainingChars');
   remainingChars.value=chars-len;
}
var showRateMessage = function (t) 
{
	$('ratePr').style.width = t.responseText+'%';
	disableRating();
}
var DiasabledRating = false;
function disableRating()
{
	var hElm = document.getElementsByClassName('star-rating');
	var a = hElm[0].getElementsByTagName('a');
	for(var i=0; i<a.length; i++)
	{
		a[i].className = '';
	}
	DiasabledRating = true;
}
function rate(obj,id,rating)
{
	if(!DiasabledRating) new Ajax.Request('/'+obj+'/do_rate', {method: 'post', onSuccess: showRateMessage,  parameters: {id: id,rating: rating}});
}
var reloadWin = function (t)
{
	//alert(t.responseText);
	window.location.reload();
}
//article del comments
function delComment(commentID,t1)
{
	if(confirm(t1))
	{
		new Ajax.Request('/comments/admin_delcomment', {method: 'post', onSuccess: reloadWin,  parameters: {id: commentID}});
	}
}
var OnAddToCart = function (t)
{
	$('add_to_cart_txt').innerHTML = "Добави в кошницата";
	$('cart_ico').src = '/images/cart_ico.gif';
	ReloadCart();
}
function add_to_cart(id)
{
	hl_element = id;
	$('add_to_cart_txt').innerHTML = "Моля изчакайте...";
	$('cart_ico').src = '/images/move_cart.gif';
	new Ajax.Request('/products/add_to_cart', {method: 'post', onSuccess: OnAddToCart,  parameters: {id: id}});
}
var OnRemoveFromCart = function (t)
{
	if(t.responseText == 'empty') $('cart').innerHTML = "";
}
function remove_from_cart(id)
{
	if(confirm("Сигурни ли сте, че искате да изтриете продукта?"))
	{
		Element.Methods.remove('cart_item_'+id);
		new Ajax.Request('/products/remove_from_cart', {method: 'post', onSuccess: OnRemoveFromCart,  parameters: {id: id}});
	}
}
var OnReloadCart = function (t)
{
	if(!isEmptyStr(t.responseText)) 
	{
		$('cart').innerHTML = t.responseText;
		if(hl_element != null)
		{
			new Effect.Highlight('cart_item_'+hl_element, {endcolor: '#F5F4F4'});
			hl_element = null;
		}
	}
	
}
var hl_element = null;
function ReloadCart()
{
	new Ajax.Request('/products/cart', {method: 'post', onSuccess: OnReloadCart});
}

var ProductSelector = Class.create();
ProductSelector.prototype = 
{
	initialize: function(name,url,item_id) 
	{
		this.name = name;
		this.url = url;
		this.item_id = item_id;
		var arr = $$('#'+this.name+'_in_list div');
		for(var i=0; i<arr.length; i++)
		{
			var ItemId = arr[i].id.replace(/[a-zA-Z]+/, '');
			this.addItem(ItemId);
			arr[i].ItemId = ItemId;
			arr[i].mainInstance = this;
			arr[i].onclick = this.onClickRemove;
		}
		this.search();
	},
	addItem: function(value)
	{
		var in_items = $('selected_'+this.name).value;
		if(in_items.length > 0) in_items += ","+value;
		else in_items = value;
		$('selected_'+this.name).value = in_items;
	},
	removeItem: function(value)
	{
		var in_items = $('selected_'+this.name).value.split(",");
		in_items = in_items.without(value);
		$('selected_'+this.name).value = in_items.join(",");
	},
	search: function()
	{
		new Ajax.Request(this.url, {method: 'post', onSuccess: this.doShowSearchedItems.bind(this),  parameters: {value: $(this.name).value, cat_id: $(this.name+'_cat_id').value, without: $('selected_'+this.name).value, item_id: this.item_id}});
	},
	doShowSearchedItems: function (t) 
	{
		$(this.name+'_auto_complete').innerHTML = t.responseText;
		
		var arr = $$('#'+this.name+'_auto_complete div');
		for(var i=0; i<arr.length; i++)
		{
			arr[i].mainInstance = this;
			arr[i].onclick = function()
			{
				var div = document.createElement('div');
				div.ItemId = this.id.replace(/[a-zA-Z]+/, '');
				this.mainInstance.addItem(div.ItemId);
				div.innerHTML = this.innerHTML;
				div.id = this.id;
				div.mainInstance = this.mainInstance;
				div.onclick = this.mainInstance.onClickRemove;
				$(this.mainInstance.name+'_in_list').appendChild(div);
				$(this.mainInstance.name+'_auto_complete').removeChild(this);
			}
		}
	},
	onClickRemove: function()
	{
		$(this.mainInstance.name+'_in_list').removeChild(this);
		this.mainInstance.removeItem(this.ItemId);
		this.mainInstance.search();
	}
}
function ajaxfilemanager(field_name, url, type, win) 
{
	var ajaxfilemanagerurl = "/filemanager/ajaxfilemanager.php?editor=tinymce";
	switch (type) {
		case "image":
			break;
		case "media":
			break;
		case "flash": 
			break;
		case "file":
			break;
		default:
			return false;
	}
	tinyMCE.activeEditor.windowManager.open({
		url: "/filemanager/ajaxfilemanager.php?editor=tinymce",
		width: 782,
		height: 440,
		inline : "yes",
		close_previous : "no"
	},{
		window : win,
		input : field_name
	});
	
/*            return false;			
	var fileBrowserWindow = new Array();
	fileBrowserWindow["file"] = ajaxfilemanagerurl;
	fileBrowserWindow["title"] = "Ajax File Manager";
	fileBrowserWindow["width"] = "782";
	fileBrowserWindow["height"] = "440";
	fileBrowserWindow["close_previous"] = "no";
	tinyMCE.openWindow(fileBrowserWindow, {
	  window : win,
	  input : field_name,
	  resizable : "yes",
	  inline : "yes",
	  editor_id : tinyMCE.getWindowArg("editor_id")
	});
	
	return false;*/
}
function check_vip_offer()
{
	if($('vip').checked) $('vip_deadline').show(); else $('vip_deadline').hide();	
}
var opened_menu = 0;
function showMenu(id)
{
	var e = document.getElementById('cat-menu').getElementsByClassName('o');
	for(var i=0; i<e.length; i++)
	{
		e[i].style.display="none";	
	}
	if(opened_menu != id) 
	{
		opened_menu = id;
		setCookie('opened_menu',id,0,"/","");
		
		var e = document.getElementById('cat-menu').getElementsByClassName('other'+id);
		for(var i=0; i<e.length; i++)
		{
			e[i].style.display="block";	
		}
	}
	else 
	{
		opened_menu = 0;
		deleteCookie('opened_menu',"/","");
	}
}

function setCookie(name,value,days,path,domain,secure) {
	var expires, date;
	if(days>0) {
		date = new Date();
		date.setTime( date.getTime() + (days*86400) );
			expires = date.toGMTString();
	} else expires = 0;
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape(c.substring(nameq.length, c.length));
  }
  return null;
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
function reasonTxt(approved)
{
	if($('approved').checked) $('reason_li').style.display = 'none';
	else $('reason_li').style.display = 'block';
	
}
function setHomepage(url)
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
  		document.body.setHomePage(url);
 
    }
    else if (window.sidebar)
    {
    if(window.netscape)
    {
         try
   {  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         }  
         catch(e)  
         {  
    alert("Това действие е забранено от вашия броузър. Ако искате да го разрешите моля напишете \"about:config\" в адресната лента и променете стойността на променливата \"signed.applets.codebase_principal_support\" на \"true\"");  
         }
    } 
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage',url);
 }
}
function addToFavorites(urlAddress,pageName)
{
	if ( window.sidebar )
	{
		window.sidebar.addPanel( pageName, urlAddress, "" );
	}
	else if ( window.external )
	{
		window.external.AddFavorite( urlAddress, pageName );
	}
}
