function trim(s)
{
  return rtrim(ltrim(s));
}

function ltrim(s)
{
  return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
  return s.replace(/\s+$/, ''); 
}

$(document).ready(function()
	{
		testGuestBookForm(true);
		
		var href = document.location.href;
		
		if(href.lastIndexOf("admin") == (-1))
		{
			$.ajax({
					type: "POST",
					url: "/cart/30/showcart",
					cache: false,
					data: "ajax=1",
					dataType: "json",
					async: false,
					success:        function(msg)
									{
										if(msg[0] > 0)
											{
												$("#cart_container").addClass('Cart_full');
												$("#cart_container").removeClass('Cart');
											}
										else
											{
												$("#cart_container").addClass('Cart');
												$("#cart_container").removeClass('Cart_full');
											}
										$("#cart_container").html(msg[1].substring(1));
									}
				});
			
				$(".r3_Tabs a").live("click", function()
					{
						var id_elem = $(this).attr("id");
						var id_content = id_elem + "_content";

						$(".content_container").hide();
						$("#" + id_content).show();

						$(".r3_Tabs td").removeClass("active");
						$(this).parent().addClass("active");
					}
				);
		
				$(".order_link").live("click", function()
					{
						var id_elem = $(this).attr("id");
						var id_item = id_elem.split("_");
						var id_cont = "order_" + id_item[2];
						$.ajax({
							type: "POST",
							url: "/cart/30/addtocart",
							cache: false,
							data: "ajax=1&oper=additem&item_id="+id_item[2],
							dataType: "json",
							async: false,
							success:        function(msg)
											{
												if(msg[0] > 0)
													{
														$("#cart_container").addClass('Cart_full');
														$("#cart_container").removeClass('Cart');
													}
												else
													{
														$("#cart_container").addClass('Cart');
														$("#cart_container").removeClass('Cart_full');
													}
												$("#cart_container").html(msg[1].substring(1));
												$("#" + id_cont).show();
											}
						});
					}
				);			
		}
	}

);

function loadSections(id)
{
	var hash = location.hash;
	if(hash != "")
		id = hash.substring(1);
	var id_content = id + "_content";
	$("#" + id_content).show();
	$("#" + id).parent().addClass("active");
}
function getMapContent(reg, city)
{
	$("#mapdata").hide();
	$("#waiting").show();
    $.ajax({
    			type: "POST",
                url: "/buy/1/mapcontent",
                cache: false,
                data: "ajax=true&reg=" + reg + "&city="+city,
                async: false,
                success:        function(msg)
                				{
                					$("#mapdata").html(msg);
									$("#waiting").hide();
									$("#mapdata").show();
									if(city)
										location.hash = "#"+city;
									if(reg)	
										location.hash = "#"+reg;
								}
			});
}
function testGuestBookForm(init)
{
		
	$("#error_message").hide();
	$("#error_email").hide();

	if(!init)
		{
			var flag = 0;
	
			var re = /\S+/;
			if(!($("#message").val().match(re)))
				{
					$("#error_message").show();
					flag = 1;
				}	
			if($("#needemail").attr("checked"))
				{
					if(!($("#author_email").val().match(re)))
						{
							$("#error_email").show();
							flag = 1;
						}
				}
			if(!flag)	
				{
					$("#guest_form").submit();
				}
		}
}

function killBlock(bid)
{
   document.getElementById(bid).style.display='none';
}

function hideBlock(id) {
        document.getElementById(id).style.display='none';
}

function viewBlock(id) {
        document.getElementById(id).style.display='inline';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
    {
      setTimeout("closereload()", 200);
    }
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
    for(j=0; j<shorts.length; j++)
      {
        d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
        if(d)
          {
            d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

            if(d.checked)
              {
                d1.style.display = '';
              }
            else
              {
                d1.style.display = 'none';
              }
          }
      }
  }
}

function chechInquiry(source)
{
  d = document.getElementById(source);
  d.checked = true;
}

function checkboxes(do_check, elem)
{
  var elts = document.forms['search'].elements[elem];
  if(elts[0])
    {
      for(var i = 0; i < elts.length; i++)
        elts[i].checked = do_check;
    }
  else
      elts.checked = do_check;
}

function checkstring(form, string, boxes)
{
  d  = document.getElementById(form);
  s  = document.getElementById(string);

  var cond1 = '';

  var elts = document.forms[form].elements[boxes];
  if(elts[0])
    {
      for(var i = 0; i < elts.length; i++)
        {
          if(elts[i].checked)
            cond1 = true;
        }
    }
  else
    {
      if(elts.checked)
        cond1 = true;
    }

  if(!(s.value))
    alert("Вы не ввели строку для поиска");
  else if(!cond1)
    alert("Вы не выбрали направление поиска");
  else
    d.submit();
}

function showhide(what,what2)
{
  if (what.style.display=='none')
    {
      what.style.display='';
    }
  else
    {
      what.style.display='none'
    }
}













var myWidth=0;
function init_gallery() {
        setInterval('startLine()', 1);
}

function startLine() {
        if (document.body.clientWidth != myWidth) {
                myWidth = document.body.clientWidth;
                inLine();
        }
}
function inLine() {
        var td = document.getElementById('photo-parent').getElementsByTagName('td');
        var table = document.getElementById('photo-parent').getElementsByTagName('table');
        var count = 0;
        var row = false;

        for (var i = 1; (table.item(i)); i++) {
                if ((row == false) && (table.item(i).offsetTop == table.item(i-1).offsetTop)) count++; else row = true;
                td.item(i).style.height = 'auto';
        }
        td.item(0).style.height = 'auto';
        count++;

        for (var i = 0; (td.item(i)); i=i+count) {
                myHeight = 0;
                for (var o = 0; (td.item(i+o) && o < count); o++) if (td.item(i+o).offsetHeight > myHeight) myHeight = td.item(i+o).offsetHeight;
                for (var o = 0; (td.item(i+o) && o < count); o++) td.item(i+o).style.height = myHeight + 'px';
        }
        document.getElementById('photo-parent').style.visibility = 'visible';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
    {
      setTimeout("closereload()", 200);
    }
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
    for(j=0; j<shorts.length; j++)
      {
        d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
        if(d)
          {
            d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

            if(d.checked)
              {
                d1.style.display = '';
              }
            else
              {
                d1.style.display = 'none';
              }
          }
      }
  }
}



function setMenuOver(elem) {
   elem.className = 'over';
}

function setMenuOut(elem) {
   elem.className = 'default';
}

function ColorBlueMenu(){
	var name, urlname;
    name = document.location.href.split('/');
    urlname = name[3];
    if(name[3]==""){
        urlname = "home_page";
    }
	document.getElementById(urlname).style.backgroundImage = "url(/images/bg_menu.gif)";
	if(name[3] == "products"){
			document.getElementById('s_products').style.display ="block";
		}
	if(name[3] == "solutions"){
			document.getElementById('s_solutions').style.display ="block";
		}
	
	document.getElementById(name[4]).style.display ="block";
	
}


