/*
 * Zpider eShop JavaScript library
 * ------------------------------- 
 * Copyright (c) 2008 Visma Software AS
 *
 */

// escapes a string that is a part of a URL
function urlescape(str) 
{
   return encodeURIComponent(str);
}
  
// remove the white spaces from the beginning and the end of the input string 
function trim(str)
{
   return (str) ? str.replace(/^\s*|\s*$/g,"") : null;
}

// adds the specified article to shopping cart
function articleToCart(articleNo, amount)
{
   $('#shoppingcart').load(
         'main.aspx?zaction=add&module=shoppingcart&artno=' + urlescape(articleNo) + '&amount=' + urlescape(amount), 
         function(html, status) {
            $('#shoppingcart').fadeTo(50, 0.5);
            $('#shoppingcart').fadeTo(200, 1.0);
         }
   );
}

// adds the specified article with amount defined by input element
function elementToCart(articleNo, elementId)
{
   var qtElement = document.getElementById(elementId);
   articleToCart(articleNo, (qtElement) ? qtElement.value : 1); 
}

function showDiscountDetails(element, articleno)
{
   if (element) {
      var coords = $(element).offset();
      var details = $('#discountdetails');
      
      if (!details[0]) {
         details = $('<div id="discountdetails" />');
         details.appendTo('body');
      }
      
      details.css({ 
            'display': 'block', 
            'position': 'absolute',
            'width': '150px',
            'left': coords.left + $(element).width() + 5 + 'px',
            'top': coords.top + 'px'
      });

      details.load('main.aspx?module=articlediscount&artno=' + urlescape(articleno)); 
   }
}

function hideDiscountDetails()
{
   var dde = $('#discountdetails');
   if (dde) dde.html('');
}

// executes a POST for the data contained in the master form to the specified URL
function postData(url, onsubmit) {   
   var theForm = document.forms['aspnetForm'];
   if (!theForm) {
       theForm = document.aspnetForm;
   }
   if (!onsubmit || (onsubmit() != false)) {
      theForm.action = url;
      theForm.method = 'POST';
      theForm.submit();
   }
}

// executes a GET for the data contained in the master form to the specified URL
function getData(url, onsubmit) {   
   var theForm = document.forms['aspnetForm'];
   if (!theForm) {
       theForm = document.aspnetForm;
   }
   if (!onSubmit || (onSubmit() != false)) {
      theForm.action = url;
      theForm.method = 'GET';
      theForm.submit();
   }
}

// returns the position of the given element
// [not in use - may be removed]
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

// search for documents, input the search string
function searchDocuments(search) {
   document.location.href = 'main.aspx?page=documentlist&searchfld=documentsearch&searchstr=' + urlescape(search);
}

// search for articles, input the search string and search field
function searchArticles(search, field) {
   document.location.href = 'main.aspx?page=articlelist&requery=1&searchstr=' + urlescape(search) + '&searchfld=' + urlescape(field);
}

// when the document is ready (loaded)
$(document).ready(function() {
   // attach keypress event to all input controls
   $("input").bind("keypress", function(e) { 
      var key = e.which || e.keyCode;
      if (key == 13) { // enter was pressed
         var li = document.getElementsByTagName("input"); 
         for (var i = 0, sl = false; i < li.length; i++) { 
            if (!sl) { sl = (li[i] == this); continue; }
            if (li[i] && li[i].disabled == false && li[i].type != "hidden") {
               li[i].focus();
               if (li[i].type == "button") li[i].click(); 
               return false;
            }
         }      
      }      
   });
});
