//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @package Starweb Webshop System
 * @version See version-file
 * @copyright Copyright (c) 2000 - 2010, Ehandelslogik AB
 * 
 * @author Ehandelslogik AB, org.no 556696-9019  (Starweb)
 *  Country: Sweden
 *  Homepage: www.starweb.se
 *  E-mail: support@starweb.se
 * 
 * License:
 * This program is not "free" software and restrictions apply!
 * This file as well as all other files containing the code to our software may ONLY be used and/or redistributed with written permission from us.
 * You'll find information regarding our conditions and pricing on our homepage. Contact us immediately if any of these conditions are not clear.
 */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** Initialize: Public variables */
/** @var object - Window name to open */
var oWnd = null;
/** @var string - Browser name */
var sBrowserName = GetBrowserName();
/** @var boolean - Is popup viewed before? */
var bIsViewedDivPopupMsg = false;




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CART FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Document Ready
 * @access public
 */
$(document).ready(function()
{
   // Set: Sortorder on order List
   $("#cssOrderList tr:even td").addClass("alt");
   $("#cssOrderList tr").hover(
      function(){
         $(this).css("cursor", "pointer").css("cursor", "hand");
         $(this).find("td").addClass("over");
      },
      function(){
         $(this).addClass("out");
         $(this).find("td").removeClass("over");
      }
   );
   
   $("#cssOrderList tr").click(function(){
      location.href = $(this).find("a").attr("href");
   });

   $(".cssSortOrderName").change(function(){
      location.href = $(".cssSortOrderName option:selected").val();
   });
   
   $(".cssSortOrderType").change(function(){
      location.href = $(".cssSortOrderType option:selected").val();
   });
   
   
   // Autofocus: On customers OrgNr_2 when OrgNr_1 is filled out
   //AutofocusFld("#aItem[OrgNr_1]", "#aItem[OrgNr_2]"); - Can not be namned aItem[] in order to work
});






/**
 * @name Function: Autofocus Field
 * @access public
 *
 * @desc Autofocus: On OrgNr_2 when OrgNr_1 is filled out
 *
 * @param string sFldNameThis
 * @param string sFldNameFocus
 */
function AutofocusFld(sFldNameThis, sFldNameFocus)
{
   $(sFldNameThis).keyup(function ()
   {
      if($(this).val().length === $(this).attr("maxlength"))
      { $(sFldNameFocus).focus(); }
   });
}






/**
 * @name Function: Set Article Image As Default Transparent Png
 * @access public
 *
 * @desc Change -IngenBildVald-.jpg to -IngenBildVald-.png for transparency in article gallery and article list
 *
 * @param string sUrlImgArtNameDefault
 */
function SetArtImgAsDefaultTransparentPng(sUrlImgArtNameDefault)
{
   $(document).ready(function()
   {
      $.ajax({
         url:  sUrlImgArtNameDefault,
         type: "HEAD",
         success:
            function()
            {
               $(".cssArtGalleryImgInner img, .cssArtListImg img").hide().each(function(i)
               {
                  if($(this).attr("src").indexOf("-IngenBildVald-") != -1)
                  { $(this).attr("src", $(this).attr("src").replace(".jpg", ".png")); }
               });
               
               $(".cssArtGalleryImgInner img, .cssArtListImg img").show();
            }
      });
   });
}






/**
 * @name Function: Get Page By Url
 * @access public
 *
 * @desc Is also used when saving customer information to session on keypress in checkout
 *
 * @param string sUrlAjax
 */
function GetPageByUrl(sUrlAjax)
{
   if(sUrlAjax != "")
   {
      $.ajax({
         type:    "GET",
         url:     sUrlAjax,
         timeout: 60000,
         success: function(sFrmResponse)
         { return false; }
      });
   }
   
   return false;
}






/**
 * @name Function: Cart Show Or Hide Content
 * @access public
 *
 * @param string sLangShowCartContent
 * @param string sLangHideCartContent
 * @param boolean bIsCartOpenInSess
 * @param boolean bIsOnclick
 */
function CartShowOrHideContent(sLangShowCartContent, sLangHideCartContent, bIsCartOpenInSess, bIsOnclick)
{
   var bIsCartOpen;
   
   
   // Check: If url is clicked
   if(bIsOnclick == 1)
   {
      if($(".cssCartData").is(":hidden"))
      {
         $(".cssUrlCartPreview").html(sLangHideCartContent);
         $(".cssCartData").show();
         bIsCartOpen = 1;
      }
      else
      {
         $(".cssCartData").hide();
         $(".cssUrlCartPreview").html(sLangShowCartContent);
         bIsCartOpen = 0;
      }
      
      
      // Set: If cart is open/closed to session
      var  sUrlExt = "pgAjaxLoad.php?sFunc=bIsCartOpen&bIsCartOpen=" + bIsCartOpen;
      GetPageByUrl(sBaseUrl + "/Shop/Page/" + sUrlExt);
   }
   else
   {
      if(bIsCartOpenInSess == 1)
      {
         $(".cssUrlCartPreview").html(sLangHideCartContent);
         $(".cssCartData").show();
      }
      else
      {
         $(".cssCartData").hide();
         $(".cssUrlCartPreview").html(sLangShowCartContent);
      }
   }
}






/**
 * @name Function: Cart Reload Ajax
 * @access public
 *
 * @param object oObj
 * @param string sUrlAjax
 * @param boolean bIsAnimateCart
 * @param boolean bIsScrollToTop
 */
function CartReloadAjax(oObj, sUrlAjax, bIsAnimateCart, bIsScrollToTop)
{
   // Close: Fancybox window (if it's open)
   CloseFancybox();


   if(sUrlAjax)
   {
      // Scroll: To top of page
      if(bIsScrollToTop == true)
      { $.scrollTo({ top:0, left:0 }, 500); }


      // View: Animate cart with dash-box-movement (jQuery UI Transfer Effect)...
      if(bIsAnimateCart == true && $(".cssCartBoxInner").length && $(".cssCartBoxInner").is(":visible"))
      {
         $(oObj).effect("transfer", { to: ".cssCartBoxInner" }, 500, function()
         {
            // View: And set reloaded cart data
            ViewAndSetReloadedCartData(sUrlAjax, bIsAnimateCart);
         });
      }
      else
      {
         // View: And set reloaded cart data
         ViewAndSetReloadedCartData(sUrlAjax, bIsAnimateCart);
      }
   }
}






/**
 * @name Function: View And Set Reloaded Cart
 * @access protected
 
 * @param string sUrlAjax
 * @param boolean bIsAnimateCart
 */
function ViewAndSetReloadedCartData(sUrlAjax, bIsAnimateCart)
{
   // Get: And view cart data
   $.ajax({
      type: "GET",
      url: sUrlAjax,
      timeout: 60000,
      success: function(sFrmResponse)
      {
         // Set: Add css class
         $(".cssCartBox").addClass("cssCartBoxHasGoods");

         // Get: Form result
         var sFrmResult     = jQuery.trim(sFrmResponse);
         var aFrmResultItem = sFrmResult.split("|||");

         // Update: Cart data = ViewCartSelBox|||ViewCartCost|||ViewCartArtTxtNr|||ViewCartArtTotalQtyNr
         $(".cssCartCost").html(aFrmResultItem[1]);
         $(".cssCartArtTotalQtyNr").html(aFrmResultItem[3]);
         $(".cssCartArtTxtNr").html(aFrmResultItem[2]);
         $(".cssCartData").html(aFrmResultItem[0]);

         // View: Animate cart with shake
         if(bIsAnimateCart == true)
         {
            // Set: Shake cart when placing an article in it (jQuery UI Shake Effect)
            $(".cssCartBox").effect("shake", { times: 1, direction: "up", distance: 5 }, 100);
         }
      }
   });
}






/**
 * @name Function: View Cart Data Popup
 * @access public
 */
function ViewCartDataPopup()
{
   if($(".cssCartBox").is(":hidden"))
   { $(".cssCartBox").show("fast"); }
   else
   { 
      $(".cssCartData").show("normal", function(e){
         $(".cssCartData").fadeTo(3000, 1).hide("normal"); 
      });
   }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CART FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* JQUERY DIALOG - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Submit Page
 * @access public
 *
 * @desc Submits the page, and validates the fields
 *
 * @param string sUrlPage
 * @param string sFrmName
 * @param string sAction
 * @param boolean bIsReloadCart
 * @param string sDivName
 * @param string sFldName
 * @param string sDialogTitle
 * @param string sLocationHref
 */
function SubmitPage(sUrlPage, sFrmName, sAction, bIsReloadCart, sDivName, sFldName, sDialogTitle, sLocationHref)
{
   // Get: Settings
   var sUrlExt      = (sUrlPage.indexOf("?") != -1) ? "&" : "?";
   var sUrlAjax     = sUrlPage + sUrlExt + "sAction=" + sAction;
   var aFrmDataItem = $("#" + sFrmName).serialize();
   sDivName         = ((sDivName != "") && (sDivName != false) && (sDivName != undefined)) ? sDivName : "cssPageContainer";
   
   
   $.ajax({
      type: "POST",
      url: sUrlAjax,
      data: aFrmDataItem,
      timeout: 60000,
      success: function(sFrmResponse)
      {
         // Get: Form result
         var sFrmResult = jQuery.trim(sFrmResponse);
         
         
         // Validate: Page
         if(sAction == "bIsValidate")
         { ValidatePage(sFrmResult, sDialogTitle); }
         
         // Reload: Page due to page is submitted
         else
         {
            // View: Message
            if(sFldName)
            { $(sFldName).val(sFrmResult); }
            else
            {
               // VALID RESULT - CustLogin/CustLoginEdit = REDIRECT: View welcome customer page. Login through "Customer information"
               // See SubmitPageCustLogin() below.
               if((sFrmResult.indexOf("cssWarnMsg") == -1) && (sLocationHref != "") && (sLocationHref != undefined))
               {
                  // Close: Fancybox window (if it's open)
                  CloseFancybox();
                  location.href = sLocationHref;
               }
               
               
               $("." + sDivName).html(sFrmResult);
            }
            
            
            // View: Reload cart
            if(bIsReloadCart == true)
            { CartReloadAjax(this, sBaseUrl + "/Shop/Page/pgAjaxLoad.php?sAction=bIsReloadCart", false, false); }
         }
      }
   });
   
   
   return false;
}






/**
 * @name Function: Validate Page
 * @access public
 *
 * @param string sWarnResult
 * @param string sDialogTitle
 */
function ValidatePage(sWarnResult, sDialogTitle)
{
   // Found error, return true
   if(sWarnResult == "")
   {
      return true;
   }
   else
   {
      // Get: Settings
      var sWarnMsg   = "";
      var aErrorItem = sWarnResult.split("@@");
      sDialogTitle.replace("'", "");
      
      
      for(var iNr=1; iNr<aErrorItem.length; iNr++)
      { if(aErrorItem[iNr] != ""){ sWarnMsg += aErrorItem[iNr]; } }
      
      
      if(sWarnMsg != "")
      {
         $("#cssErrorDialogMsg").html("<ul>" + sWarnMsg + "</ul>");
         
         // Show: Dialog using jQuery UI Dialog widget
         var doOk       = function(){ $(this).dialog("destroy"); }
         var dialogOpts = {
            title: sDialogTitle,
            dialogClass: "cssDialogMsg",
            position: "top",
            autoOpen: true,
            modal: true,
            width: 400,
            overlay:
            {
               opacity: 1,
               background: "#000000"
            },
            buttons:
            { "Ok": doOk }
         }
         
         // Show: Dialog using jQuery UI Dialog widget
         $("#cssErrorDialogMsg").dialog(dialogOpts);
      }
   }
   
   
   return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* JQUERY DIALOG - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CUSTOMER FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Submit Page Customer Login
 * @access public
 *
 * @param string sFrmName
 * @param string sFrmSel
 * @param string sPageName
 * @param string sDivName
 * @param string sCustLoginAction
 * @param string sLocationHref
 *
 * @return boolean
 */
function SubmitPageCustLogin(sFrmName, sFrmSel, sPageName, sDivName, sCustLoginAction, sLocationHref)
{
   $(document).ready(function()
   {
      $("#"+ sFrmName, this).submit(function()
      {
         // Get: Settings
         var sUrlAjax     = sBaseUrl +"/Shop/Page/"+ sPageName +"sCustLoginAction="+ sCustLoginAction;
         var aFrmDataItem = $("#"+ sFrmName).serialize();
         
         
         $.ajax({
            type: "POST",
            url: sUrlAjax,
            data: aFrmDataItem,
            timeout: 60000,
            success: function(sFrmResponse)
            {
               // Get: Form result
               var sFrmResult = jQuery.trim(sFrmResponse);
               
               
               switch(true)
               {
                  // VALID RESULT - CustLostPwd = View OK message
                  case ((sFrmResult.indexOf("cssWarnMsg") == -1) && (sFrmSel == "frmCustLostPwd")):
                     $("#"+ sDivName).html(sFrmResult);
                     $("#frmCustLostPwd #sEmailAddress").val("");
                     break;
                     
                     
                  // VALID RESULT - CustLogin/CustLoginEdit = REDIRECT: View welcome customer page. Login through "Customer information"
                  // NEW customers in SHOP are redirected through SubmitPage() function in jscrPage
                  case ((sFrmResult.indexOf("cssWarnMsg") == -1) && ((sFrmSel == "frmCustLogin") || (sFrmSel == "frmCustLoginEdit")) && (sLocationHref != "")):
                     // Close: Fancybox window (if it's open)
                     CloseFancybox();
                     location.href = sLocationHref;
                     break;
                     
                     
                  // VALID RESULT - Close window, and reload checkout cart page. Login through "Checkout"
                  case (sFrmResult.indexOf("cssWarnMsg") == -1):
                     // Close: Fancybox window (if it's open)
                     CloseFancybox();
                     CheckOutReloadAjax("", "bIsCustLogin");
                     break;
                     
                     
                  // INVALID RESULT - View error message
                  default:
                     $("#"+ sDivName).html(sFrmResult);
                     break;
               }
            }
         });
         
         return false;
      });
   });
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CUSTOMER FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* FANCYBOX FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Close Fancybox
 * @access public
 */
function CloseFancybox()
{
   //var sFancyBoxWnd = $("#fancy_overlay").css("display");
   var sFancyBoxWnd = $("#fancybox-overlay").css("display");
   // Close: Fancybox window (if it's open)
   if((sFancyBoxWnd != "none") && (sFancyBoxWnd != undefined))
   //{ if($.fn){ $.fn.fancybox.close(); } }
   { $.fancybox.close(); }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* FANCYBOX FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* BROWSER FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Get Browser Name
 * @access public
 *
 * @desc Original script was found at:
 * http://parentnode.org/javascript/javascript-browser-detection-revisited/
 *
 * [if gte IE 7]
 * none => no value means only for the specified version: [if IE 7] will only work for IE 7
 * “lte” means “lower than or equal to”
 * “lt” means “lower than”
 * “gte” means “greater than or equal to”
 * “gt” means “greater than”
 * To target all IE versions, simply use <!–[if IE]>
 */
function GetBrowserName()
{
   var BO        = new Array();
   var bIsSafari = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName);
   
   
   BO["Ie"]        = false /*@cc_on || true @*/; 
   BO["Ie4"]       = BO["Ie"] && (document.getElementById == null);
   BO["Ie5"]       = BO["Ie"] && (document.namespaces == null) && (!BO["Ie4"]);
   BO["Ie6"]       = BO["Ie"] && (document.implementation != null) && (document.implementation.hasFeature != null) && (window.XMLHttpRequest == null);
   BO["Ie7"]       = BO["Ie"] && (document.implementation != null) && (document.compatMode != null) && (window.XMLHttpRequest != null);
   BO["Ie55"]      = BO["Ie"] && (document.namespaces != null) && (!BO["Ie6"]) && (!BO["Ie7"]);
   
   BO["Ns4"]       = !BO["Ie"] &&  (document.layers != null) &&  (window.confirm != null) && (document.createElement == null);
   BO["Opera"]     = (self.opera != null);
   BO["Gecko"]     = (document.getBoxObjectFor != null); // Firefox
   BO["Khtml"]     = (navigator.vendor == "KDE");
   BO["Konq"]      = ((navigator.vendor == 'KDE') || (document.childNodes) && (!document.all) && (!navigator.taintEnabled));
   
   BO["Safari1.2"] = (parseInt(0).toFixed == null) && (bIsSafari && (window.XMLHttpRequest != null));
   BO["Safari2.0"] = (parseInt(0).toFixed != null) && bIsSafari && !BO["Safari1.2"];
   BO["Safari1.1"] = bIsSafari && !BO["Safari1.2"] && !BO["Safari2.0"];
   
   
   sBrowserName = false;
   for(sKey in BO)
   {
      if(BO[sKey] == true)
      {
         // Get: Browser name
         sBrowserName = sKey;
         
         // Break the loop
         break;
      }
   }
   
   
   return (sBrowserName != false) ? sBrowserName : (navigator.appName + navigator.appVersion); //parseInt(navigator.appVersion)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* BROWSER FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* TOGGLE FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Toggle Delivery Address
 * @access public
 *
 * @desc Show/hide customer delivery address
 */
function ToggleDeliveryAddress()
{
   $("#bIsViewCustEditDeliveryAddress").livequery(function()
   {
      $(this).ready(function()
      {
          // Check: If checkbox is checked, then show delivery address
          $("#bIsViewCustEditDeliveryAddress").attr("checked") ? $("#cssCustEditDelivery").slideDown() : $("#cssCustEditDelivery").slideUp();
      });

      // Check: If checkbox is clicked
      $(this).click(function()
      {
         // Check: If checkbox is checked, then show delivery address
         $(this).attr("checked") ? $("#cssCustEditDelivery").slideDown() : $("#cssCustEditDelivery").slideUp();
      });
   });
}






/**
 * @name Function: Toggle State Separate Customer Delivery Address
 * @access public
 *
 * @desc Show/hide customer delivery address
 *
 * @param object oIsViewCustEditDeliveryAddress
 * @param string sDivName
 */
/*function ToggleStateSeparateCustDeliveryAddress(oIsViewCustEditDeliveryAddress, sDivName)
{
   if(oIsViewCustEditDeliveryAddress)
   {
      // Set: Toggle div on/off
      (oIsViewCustEditDeliveryAddress.checked == true) ?  DivToggle(sDivName, 1) : DivToggle(sDivName, 0);
   }
}*/






/**
 * @name Function: Div Toggle
 * @access public
 *
 * @param string sDivName
 * @param int iState
 */
function DivToggle(sDivName, iState)
{
   // NN4+
   if(document.layers)
   {
      var oNN4Div = document.layers[sDivName];
      
      if(oNN4Div)
      { oNN4Div.visibility = iState ? "show" : "hide"; }
   }
   
   // Gecko(NN6) + IE 5+
   else if(document.getElementById)
   {
      var oNN6Div = document.getElementById(sDivName);
      
      if(oNN6Div)
      { oNN6Div.style.visibility = iState ? "visible" : "hidden"; }
   }
   
   // IE 4
   else if(document.all)
   {
      var oIE4Div = document.all[sDivName];
      
      if(oIE4Div)
      { oIE4Div.style.visibility = iState ? "visible" : "hidden"; }
   }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* TOGGLE FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* WINDOW FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Open Popup Window
 * @access public
 *
 * @param string sUrl
 * @param int iWdtNr
 * @param int iHgtNr
 * @param boolean bIsResizable
 */
function OpenPopupWnd(sUrl, iWdtNr, iHgtNr, bIsResizable)
{
   // Get: Settings
   var iLeftNr   = ((screen.width / 2) - (iWdtNr / 2));
   var iTopNr    = ((screen.height / (2 - iHgtNr)) / (2 - (iHgtNr / 10)));
   var sFeatures = 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars='
      + bIsResizable + ', resizable=' + bIsResizable + ', width=' + iWdtNr + ', height=' + iHgtNr
      + ',alwaysRaised=yes, left=' + iLeftNr + ', top=' + iTopNr + ',screenX=0, screenY=0';
   
   
   // Close opened window
   if(oWnd && !oWnd.closed)
   { oWnd.close(); }
   
   
   oWnd = window.open(sUrl, "OpenPopupWnd", sFeatures);
   oWnd.location.href = sUrl;
   if(!oWnd.opener){ oWnd.opener = self; }
   oWnd.focus();
}






/**
 * @name Function: Center Window
 * @access public
 */
function CenterWnd()
{
   // Get: Settings
   var iScreenWdtNr = screen.availWidth;
   var iScreenHgtNr = screen.availHeight;
   
   
   // Set: Move window to middle of screen
   window.moveTo(((iScreenWdtNr-document.body.clientWidth) / 2), ((iScreenHgtNr-document.body.clientHeight) / 2));
}






/**
 * @name Function: View Div Popup Message
 * @access public
 *
 * @param string sDivName
 * @param string iFadeSecNr
 * @param boolean bIsSetCss
 * @param string iLeftNr
 * @param string iTopNr
 */
function ViewDivPopupMsg(sDivName, iFadeSecNr, bIsSetCss, iLeftNr, iTopNr)
{
   $(document).ready(function()
   {
      if(bIsViewedDivPopupMsg == false)
      {
         // Get: Scroll cordinates
         iFadeSecNr  = iFadeSecNr ? iFadeSecNr : 1300;
         
         
         // Set: Placement of popup
         if(bIsSetCss == true)
         {
            iLeftNr     = iLeftNr ? iLeftNr : (screen.width / 3);
            iTopNr      = iTopNr ? iTopNr : ((screen.height / 3) + GetScrollXY("Y"));
            
            $(sDivName).css({"top": iTopNr+"px", "left": iLeftNr+"px"});
         }
         
         
         // Set: Fade popup
         $(sDivName).fadeIn("normal").fadeTo(iFadeSecNr, 1).fadeOut("normal");
         
         
         // Set: Cart popup is only viewed once
         if(sDivName == "#cssCartBuyPopupMsg")
         { bIsViewedDivPopupMsg = true; }
      }
   });
}






/**
 * @name Function: View Newsletter Message
 * @access public
 *
 * @param string sUrlPage
 * @param string sFrmName
 * @param string sAction
 * @param boolean bIsReloadCart
 * @param string sDivName
 * @param string sCssName
 */
function ViewNewsletterMsg(sUrlPage, sFrmName, sAction, bIsReloadCart, sDivName, sFldName, sCssName)
{
   $(document).ready(function()
   {
      if(TrimStr($(sFldName).val()) != "")
      {
         $("#" + sCssName).fadeOut("normal");
         SubmitPage(sUrlPage, sFrmName, sAction, bIsReloadCart, sDivName, sFldName);
         $("#" + sCssName).fadeIn("normal");
      }
   });
}






/**
 * @name Function: Get Scroll X/Y
 * @access public
 *
 * @desc Get scroll cordinates. Is used by SettEdit etc.
 *       Script was found at: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
 *
 * @param string sXorY
 */
function GetScrollXY(sXorY)
{
  var scrOfX = 0, scrOfY = 0;
  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  
  
  return (sXorY == "X") ? scrOfX : scrOfY;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* WINDOW FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* STRING FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Trim String
 * @access public
 *
 * @param string sVal
 * 
 * @return string
 */
function TrimStr(sVal)
{
   if(sVal != '')
   {
      while(sVal.substring(0, 1) == ' ')
      { sVal = sVal.substring(1, sVal.length); }
      
      while(sVal.substring(sVal.length - 1, sVal.length) == ' ')
      { sVal = sVal.substring(0, sVal.length - 1); }
   }
   
   
   return sVal;
}






/**
 * @name Function: Get Random String
 * @access public
 */
function GetRandStr()
{
   // Get: Settings
   var iLenNr   = 30;
   var sChrList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   var sStrRnd  = "";
   
   for(var iNr=0; iNr<iLenNr; iNr++)
   {
      var iRandPosNr  = Math.floor(Math.random() * sChrList.length);
      sStrRnd        += sChrList.substring(iRandPosNr, iRandPosNr+1);
   }
   
   return sStrRnd;
}
 
 
 
 
 
 
/**
* @name Function: Clear Field
* @access public
*
* @param string sTxtBoxName
*/
function ClearFld(sTxtBoxName)
{
   oTxtBox       = document.getElementById(sTxtBoxName);
   oTxtBox.value = "";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* STRING FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* COST FUNCTIONS - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @name Function: Is Integer
 * @access public
 *
 * @param int iInt
 *
 * @return boolean
 */
function IsInteger(iInt)
{
   return (("" + parseInt(iInt)) == iInt);
}






/**
 * @name Function: Get Round Cost Number
 * @access public
 *
 * @param float fCostNr
 * 
 * @return int
 */
function GetRoundCostNr(fCostNr)
{
   if(fCostNr == '')
   { return '-'; }
   else
   {
      fCostNr = fCostNr - 0; // Force number
      iCurrDecNr = iCurrDecNr - 0; // Force number
      var fFloatVar = Math.pow(10, iCurrDecNr);
      
      //fCostNr += Math.pow(10, - (iCurrDecNr + 1));
      fCostNr = Math.round(fCostNr * fFloatVar) / fFloatVar;
      
      fCostNr += Math.pow(10, - (iCurrDecNr + 1));
      fCostNr += ""; // Force string
      fCostNrFormatted = (iCurrDecNr == 0)
         ? fCostNr.substring(0, fCostNr.indexOf('.'))
         : fCostNr.substring(0, fCostNr.indexOf('.') + (iCurrDecNr + 10));
      
      
      return GetFormattedNr(fCostNrFormatted, iCurrDecNr, ",", " ");
   }
}






/**
 * @name Function: Get Formatted Number
 * @access private
 *
 * @desc Made by Mathias Bynens <http://mathiasbynens.be/>
 *
 * @param float a
 * @param int b
 * @param string c
 * @param string d
 * 
 * @return int
 */
function GetFormattedNr(a, b, c, d)
{
   a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
   e = a + '';
   f = e.split('.');
   
   if(!f[0])
   { f[0] = '0'; }

   if (!f[1])
   { f[1] = ""; }
   
   if (f[1].length < b)
   {
      g = f[1];
      
      for(i=f[1].length+1; i<=b; i++)
      { g += '0'; }
      
      f[1] = g;
   }
   
   if(d != '' && f[0].length > 3)
   {
      h = f[0];
      f[0] = "";
      for(j = 3; j < h.length; j+=3)
      {
         i = h.slice(h.length - j, h.length - j + 3);
         f[0] = d + i +   f[0] + '';
      }
      
      j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
      f[0] = j + f[0];
   }
   
   c = (b <= 0) ? '' : c;
   
   
   return f[0] + c + f[1];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* COST FUNCTIONS - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






/**
 * @name Function: Execute Article Search
 * @access public
 *
 * @desc Redirect value in search textbox to url -> article search list page
 */
function ExecArtSearch()
{
   // Get: Article search texbox
   var oArtSearch = document.getElementById("sArtSearch");


   // Get: Url location in Main-frame to search for entered string value
   if(TrimStr(oArtSearch.value) != '')
   {
      // Get: Setting
      var sUrlRedirect = sBaseUrl + '/Shop/Page/Art/pgArtList.php?sSearchWord=' + escape(oArtSearch.value);


      // Redirect: To valid page
      location.href = sUrlRedirect;
   }


   // Set: Focus on texbox again
   oArtSearch.focus();


   return false;
}






/**
 * @name Function: Article Search Suggestions
 * @access public
 *
 * @desc View AJAX article suggestion list in search textbox using jQuery UI Autocomplete widget
 */
$(document).ready(function()
{
   var oAutoComplete;


   // Set: Autocomplete (element different in Shop/Admin)
   if($("#sArtSearch").length)
   {
      oAutoComplete = $("#sArtSearch").autocomplete({
         source    : sBaseUrl + "/Shop/Page/pgAjaxLoad.php?sFunc=bArtSearchSuggestions",
         minLength : 1,
         select    : function (event, ui) {
            // Force: Fill the searchbox with selected value
            $("#sArtSearch").val(ui.item.value);
            // Call: Search form script function when selecting a suggestion
            ExecArtSearch();
         }
      })
   }
   else if($("#aSearchItem\\[sSearchWord\\]").length)
   {
      // Set: Autocomplete disabled if something other than article search is chosen
      $("#aSearchItem\\[sSearchTbl\\]").change(function()
      {
         if ($("#aSearchItem\\[sSearchTbl\\]").val() != "Art")
         {  $("#aSearchItem\\[sSearchWord\\]").autocomplete("option", "disabled", true); }
         else
         {  $("#aSearchItem\\[sSearchWord\\]").autocomplete("option", "disabled", false); }
      });

      oAutoComplete = $("#aSearchItem\\[sSearchWord\\]").autocomplete({
         source    : sBaseUrl + "/Admin/Page/Ajax/pgAjax.php?sType=bArtSearchSuggestions",
         minLength : 1,
         select    : function (event, ui) {
            // Force: Fill the searchbox with selected value
            $("#aSearchItem\\[sSearchWord\\]").val(ui.item.value);
            // Call: Search form script function when selecting a suggestion
            ExecSearch();
         }
      });
   }

   // Set: Highlight search suggestions characters matching search term
   if(oAutoComplete != null)
   {
      oAutoComplete.data("autocomplete")._renderItem = function( ul, item ) {

         var iHighlightChars = 0;
         for(var i = 0; i < this.term.length; i++)
         {
            // Check: If characters at corresponding positions match, break otherwise
            if (this.term.toLowerCase().charAt(i) == item.label.toLowerCase().charAt(i))
            { iHighlightChars++; }
            else
            { break; }
         }

         // Set: Surround highlighted characters with strong-tags
         if (iHighlightChars > 0)
         { item.label = "<strong>" + item.label.substr(0, iHighlightChars) + "</strong>" + item.label.substr(iHighlightChars); }

         //item.label = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "i"), "<strong>$1</strong>");
         return $("<li></li>")
                 .data("item.autocomplete", item)
                 .append("<a>" + item.label + "</a>")
                 .appendTo(ul);
      };
   }
});






//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CUSTOMER EDIT FUNCTION - START */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$('document').ready(function(){
   // Tab menu functionality
   $('.cssCustWelcome .cssTabMenu li a').click(function(eEvent){
      eEvent.preventDefault();

      var sTargetId = '#'+ $(this).attr('rel');

      $('.cssCustWelcome .cssTabMenu li.cssTabActive').removeClass('cssTabActive');
      $(this).parents('li').addClass('cssTabActive');

      $('.cssTabMenuContent div.cssTabMenuContainer').each(function(){
         if($(this).hasClass('cssVisible'))
         { $(this).removeClass('cssVisible').addClass('cssHidden'); }
      });

      $(sTargetId).removeClass('cssHidden').addClass('cssVisible');
   });


   // Place previous order in cart
   $('#cssOrderItemBuyBtn').click(function(eEvent)
   {
      eEvent.preventDefault();

      var oArtList = {};

      $('table.cssTblResult tr.cssTblArtItem td.cssTblColFirst').each(function(iIndex){
         var sModelId  = $(this).children('input[name="sArtModelId"]').val();
         var iArtQtyNr = $(this).children('input[name="iArtQtyNr"]').val();

         oArtList[iIndex] = {
           ModelId  :  sModelId,
           ArtQtyNr :  iArtQtyNr
         };
      });


      $.ajax({
         url      : sBaseUrl +'/Shop/Page/pgAjaxLoad.php?sFunc=GetArtIdFromModelId',
         data     : $.param( { aaArtList : oArtList } ),
         type     : 'POST',
         dataType : 'json',
         success  : function(sJsonResponse)
         {
            for (var oArtItem in sJsonResponse)
            {
               if (sJsonResponse[oArtItem].ArtIsDeleted == false)
               {
                  var sModelId  = sJsonResponse[oArtItem].ModelId;
                  var iArt_Id   = sJsonResponse[oArtItem].Art_Id;
                  var iArtQtyNr = sJsonResponse[oArtItem].ArtQtyNr;

                  sUrlBtnBuyPty = '?sAction=bIsInsertArt&iId='+ iArt_Id +'&sArtModelId='+ stripslashes(sModelId) +'&iArtQtyNr='+ iArtQtyNr;

                  var oParent   = $('table.cssTblResult tr.cssTblArtItem td.cssTblColFirst input[value="'+ sModelId +'"]').parents('tr.cssTblArtItem');

                  CartReloadAjax(oParent, GetUrlBtnBuyArtItem(), bIsAnimateCart, bIsScrollToCart);
               }
            }
         }
      })
   });
});






/**
 * strip slashes from string
 *
 * @param string str
 *
 * @return string str
 */
function stripslashes(str) {
   str=str.replace(/\\'/g,'\'');
   str=str.replace(/\\"/g,'"');
   str=str.replace(/\\0/g,'\0');
   str=str.replace(/\\\\/g,'\\');


   return str;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* CUSTOMER EDIT FUNCTION - END */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

