﻿function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	" ;path=/";
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function pushNavigationCookie() {
	var fullPath = getCookie("ulmas_back_path");
	var steps = fullPath.split(":");
	
	if (steps[steps.length-1] == location.pathname)
		return;
	
	var startPush = ((steps.length >= 10)?1:0);
	
	fullPath = "";
	for (var i = startPush;i<steps.length;i++) {
		fullPath = fullPath+steps[i]+":";
	}
	
	fullPath = fullPath + location.pathname;
	setCookie("ulmas_back_path",fullPath,1);
}

function popNavigationCookie() {
	var fullPath = getCookie("ulmas_back_path");
	var steps = fullPath.split(":");

	if (steps.length == 0)
		return "/";
		
	if (steps.length == 1) {
		setCookie("ulmas_back_path","/",1);
		return "/";		
	}
			
	fullPath = steps[0];
	for (var i = 1;i<steps.length-1;i++) {
		fullPath = fullPath+":"+steps[i];
	}
	
	setCookie("ulmas_back_path",fullPath,1);
	
	return steps[steps.length-2];
}

var poppedBackLink = null;

function cleverGoBack() {
	cleverGoBack(1);
}

function cleverGoBack(steps) {
	if (steps > 1) {
		popNavigationCookie();
		cleverGoBack(steps-1);
		return;
	}

	if (poppedBackLink == null)
		poppedBackLink = popNavigationCookie();
		
	location.replace("http://"+location.hostname+poppedBackLink+"#");
}

function cleverSavePath() {
	pushNavigationCookie();
}

function popUp(URL)
{
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=200,left = 540,top = 412');");
}

function hideUnhide(elemId)
{
  if (document.getElementById(elemId).style.display != 'none')
  {
    document.getElementById(elemId).style.display = 'none';
  }
  else
  {
    document.getElementById(elemId).style.display = '';
  }
}

function validate(id)
{
  if (isNaN(document.getElementById(id).value))
  {
    alert('Neteisingas kiekis!');
    document.getElementById(id).value = 0;
  }
  else
  {
    if(document.getElementById(id).value < 0)
    {
      alert('Neteisingas kiekis!');
      document.getElementById(id).value = 0;
    }
  }
}

function scrollTo(pid)
{
  var clean = parent.opener.location + '';
  var str = parent.opener.location + '#'+pid;
  clean = clean.substring(0, str.indexOf('#'));

  parent.opener.location = clean + '&#' + pid;
}

function timeOut(pid)
{
  //document.forms.main_form.submit();
  document.getElementById('main_form').submit();
  //parent.opener.location = parent.opener.location;
  var clean = new String(parent.opener.location);
  var str = parent.opener.location + '#'+pid;
  clean = clean.substring(0, str.indexOf('#'));

  parent.opener.location = clean + '#' + pid;
  window.opener.location.reload(true);
  window.close();  
}

function submitCartForm(pid) {
	var form = document.getElementById('cart_form_'+pid);
	if (!form.amount.value.match(/[0-9]+/) || form.amount.value == "0") {
		alert("Nurodykite prekių kiekį ir bandykite dar kartą");
		return;
	} else {
		form.submit();
	}
}

function clearField()
{
  if(document.getElementById('search_hidden_field').value == '__search__')
  {
    document.getElementById('SearchForm_SearchForm_Search').value = '';
  }
}

function appendField(value)
{
  if(document.getElementById('SearchForm_SearchForm_Search').value == '')
  {
    document.getElementById('SearchForm_SearchForm_Search').value = value;
    document.getElementById('search_hidden_field').value = '__search__'
  }
  else
  {
    document.getElementById('search_hidden_field').value = '';
  }
}

function getElementsByName_iefix1(tag, name)
{

     var elem = document.getElementsByTagName(tag);

     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("title");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function getElementsByName_iefix(tag, name)
{

     var elem = document.getElementsByTagName(tag);

     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function hideUnhideAll(arr_cilds, selected, link)
{
  var all_elements = getElementsByName_iefix('div', 'subgroup');

  for(var i=0;i<all_elements.length;i++)
  {
    all_elements[i].style.display = 'none';
  }

  for(var i=0;i<arr_cilds.length;i++)
  {
    if(arr_cilds[i] !== undefined && arr_cilds[i] !== 0 )
    {
      if(document.getElementById(arr_cilds[i]) != null)
      {
        document.getElementById(arr_cilds[i]).style.display = '';
      }
    }

  }

  if(selected != 'false')
  {
    document.getElementById(selected).className = "second_lvl menu_selected";
  }

  if(link != '')
  {
    window.location.href=link;
  }
  
  /*var first_elements = getElementsByClassName('first_lvl_a');

  if (first_elements != null);
	for (var i = 0;i<first_elements.length;i++) {
		first_elements[i].style.color="white";
	}
  
  var second_elements = getElementsByClassName('second_lvl_a');
  if (second_elements)
	  for (var i = 0;i<second_elements.length;i++) {
		second_elements[i].style.color="white";
	  }
  
  var first_elements_selected = getElementsByClassName('first_lvl_a_selected');
  if (first_elements_selected)
	  for (var i = 0;i<first_elements_selected.length;i++) {
		first_elements_selected[i].style.color="white";
	  }
  
  var second_elements_selected = getElementsByClassName('second_lvl_a_selected');
  if (second_elements_selected)
	  for (var i = 0;i<second_elements_selected.length;i++) {
		second_elements_selected[i].style.color="white";
	  }*/
}

function onLoadShow(id_show, link)
{
  var all_elements = document.getElementsByName("subgroup");

  for(var i=0;i<all_elements.length;i++)
  {
    all_elements[i].style.display = 'none';
  }

  document.getElementById(id_show).style.display = '';
  document.getElementById(id_show).className = "second_lvl menu_selected";
}

function realignExtendedImages() {
	var pictures = getElementsByClassName("extended_product_picture_img");
	var maxPictureWidth = 0;
	for (var i=0;i<pictures.length;i++) {
		maxPictureWidth = Math.max(maxPictureWidth,pictures[i].offsetWidth);
	}
	
	var divs = getElementsByClassName("extended_product_picture");
	
	for (var i=0;i<divs .length;i++) {
	  divs [i].style.width = maxPictureWidth+"px";
	  divs [i].style.minWidth = maxPictureWidth+"px";
	  divs [i].style.maxWidth = maxPictureWidth+"px";
	}
}

function realignExtendedParams() {
	var paramTables = document.getElementsByName("ExtendedParamTable");

	var maxColWidth = new Array();
	for (var i=0;i<100;i++) {
	  maxColWidth[i]=0;
	}

	for (var t=0;t<paramTables.length;t++) {
		var table = paramTables[t];
		
		var rows = table.getElementsByTagName("tr");
		var cols = rows[0].getElementsByTagName("td");
		
		for (var c=0;c<cols.length;c++) {
		  maxColWidth[c]=Math.min(100,Math.max(maxColWidth[c],cols[c].offsetWidth));
		}
	}
	
	for (var t=0;t<paramTables.length;t++) {
		var table = paramTables[t];

		var rows = table.getElementsByTagName("tr");
		var cols = rows[0].getElementsByTagName("td");

		for (var c=0;c<cols.length;c++) {
    	cols[c].style.width = (maxColWidth[c]-15)+"px";
	  	cols[c].style.minWidth = (maxColWidth[c]-15)+"px";
	  	cols[c].style.maxWidth = (maxColWidth[c]-15)+"px";
		}
	}
}

function realignSimpleImages() {
	var pictures = document.getElementsByName("SimplePicture");
	var maxPictureWidth = 0;
	for (var i=0;i<pictures.length;i++) {
		maxPictureWidth = Math.max(maxPictureWidth,pictures[i].offsetWidth);
	}

	for (var i=0;i<pictures.length;i++) {
	  pictures[i].style.width = maxPictureWidth+"px";
	  pictures[i].style.minWidth = maxPictureWidth+"px";
	  pictures[i].style.maxWidth = maxPictureWidth+"px";
	}
}

function realignSimpleParams() {
	var products = getElementsByClassName("product_list_item");

	var maxColWidth = new Array();
	for (var i=0;i<100;i++) {
	  maxColWidth[i]=0;
	}
	
	for (var p=0;p<products.length;p++) {
		var product = products[p];
		var cols = getElementsByClassName("param",null,product);
		
		for (var c=0;c<cols.length;c++) {
		  maxColWidth[c]=Math.min(85,Math.max(maxColWidth[c],cols[c].offsetWidth));
		}
	}
	
	for (var p=0;p<products.length;p++) {
		var product = products[p];
		var cols = getElementsByClassName("param",null,product);

  	for (var c=0;c<cols.length;c++) {
    	cols[c].style.width = (maxColWidth[c])+"px";
	  	cols[c].style.minWidth = (maxColWidth[c])+"px";
	  	cols[c].style.maxWidth = (maxColWidth[c])+"px";
		}
	}
}

