/*
 * 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) {
            if (status == 'success') {
               $(this).effect("highlight", { color: "#cacaca" }, 1500);
             //$(this).fadeTo(50, 0.5).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();
   }
}

// executes a ajax POST for the data contained in the master form to the specified URL
function ajaxPostData(url, success, error, onsubmit) {
   var theForm = document.forms['aspnetForm'];
   if (!theForm) {
      theForm = document.aspnetForm;
   }
   if (!onsubmit || (onsubmit() != false)) {
      var params = $(theForm).serialize();
      $.ajax({
         url: url,         
         type: "POST",
         data: params,         
         success: success,
         error: error,
         async: false
      });
   }
}

// 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];
}

// loads the given article
function showArticle(articleNo) {
   document.location.href = 'main.aspx?page=article&artno=' + urlescape(articleNo);
}

// 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);
}

// search for either documents or articles 
function genericSearch() {
   var searchFieldSelect = document.getElementById('searchfld');
   var searchTextControl = document.getElementById('searchstr');
   
   if (searchTextControl && searchFieldSelect) {
      if (searchFieldSelect.value == 'documentsearch') {
         searchDocuments(searchTextControl.value);
      }
      else {
         searchArticles(searchTextControl.value, searchFieldSelect.value);
      }
   }
   return false;
}

// 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(); // move focus on this input
               if (li[i].type == "button" || li[i].type == "image" || li[i].type == "submit") {
                  li[i].click();  // perform a click on a clickable input
               }
               return false;
            }
         }
      }
   });
});
