// <script>
  function $(o) {
    if (typeof(o) == "string")
      return document.getElementById(o)
      else
      return o;
  };
  function applyStyleString(el,str){
    if(document.all && !window.opera) {
      el.style.cssText = str;
      el.setAttribute("cssText",str);
    } else {
      el.setAttribute("style",str);
    }
  };

// ---
/*
	ELO - Encapsulated Load Object, by Robert Nyman, http://www.robertnyman.com
	Inspired and influenced by Dean Edwards, Matthias Miller, and John Resig: http://dean.edwards.name/weblog/2006/06/again/
*/
var ELO = {
	loaded : false,
	timer : null,
	functionsToCallOnload : [], // Type in functions as strings here. e.g. "myFunction()"
	init : function (){
		if(ELO.loaded) return;
		ELO.loaded = true;
		ELO.load();
	},
	
	load : function (){
		if(this.timer){
			clearInterval(this.timer);
		}
		for(var i=0; i<this.functionsToCallOnload.length; i++){
			try{
				eval(this.functionsToCallOnload[i]);
			}
			catch(e){
				// Handle error here
			}
		}
	}
};
// ---
/* Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	if(document.getElementById){
		document.write("<script id=\"ieScriptLoad\" defer src=\"//:\"><\/script>");
	    document.getElementById("ieScriptLoad").onreadystatechange = function() {
	        if (this.readyState == "complete") {
	            ELO.init();
	        }
	    };
	}
/*@end @*/
// ---
/* Mozilla/Opera 9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", ELO.init, false);
}
// ---
/* Safari */
if(navigator.userAgent.search(/WebKit/i) != -1){
    ELO.timer = setInterval(function (){
		if(document.readyState.search(/loaded|complete/i) != -1) {
			ELO.init();
		}
	}, 10);
}
// ---
/* Other web browsers */
window.onload = ELO.init;
// ---

/*************************************
*    Script : swPOP v2.0             *
*    Author : Kim Steinhaug          *
*    Url    : www.steinhaug.com      *
*                                    *
*  --   --  --  --  --  --  --  --   *
* Usage :                            *
* swPOP('link','x','y','scrollbar'); *
*                                    *
* link 	    = Dokument url           *
* x    	    = Popup window X value   *
* y    	    = Popup window Y value   *
* scrollbar = 0, no                  *
*             1, yes                 *
**************************************/
function swPOP(url,Xvalue,Yvalue,scroll) {
  var w = 480, h = 340;if (document.all || document.layers) { w = screen.availWidth;h = screen.availHeight; }
  var popW = Xvalue, popH = Yvalue;var leftPos = (w-popW)/2, topPos = (h-popH)/2;
  if (scroll) { i = open(url, "displayWindow","top="+topPos+",left="+leftPos+",width="+Xvalue+",height="+Yvalue+",status=no,toolbar=1,menubar=no,resize=no,dependent=yes,scrollbars=yes");} else { i = open(url, "displayWindow","top="+topPos+",left="+leftPos+",width="+Xvalue+",height="+Yvalue+",status=1,toolbar=1,menubar=0,resize=no,dependent=yes,scrollbars=no");}
}
function validateNR(theform,felt) {
  var valid="1234567890";
  string = theform.value;
  for (var i=0; i<string.length; i++) {
    if (valid.indexOf(string.charAt(i)) < 0) {
      alert(felt+' inneholder ugyldige tegn!');
      theform.value='';
      theform.focus();
      return false;
    }
  }
  return false;
} 
function cartDelete(id,navn,qu){
  if(id=="0"){
    alert("Det har skjedd en feil, ingen vare er valgt!");
    return false;
  }
  if (confirm("Er du sikker på at du vil slette\nvaren " + navn + "?")) {
    location.href='storefront.php?' + qu + '&action=cartDelete&id=' + id;
  }
}
function cartSubtract(id,navn,qu){
  if(id=="0"){
    alert("Det har skjedd en feil, ingen vare er valgt!");
    return false;
  }
  location.href='storefront.php?' + qu + '&action=cartSubtract&id=' + id;
}
function cartAdd(id,navn,qu){
  if(id=="0"){
    alert("Det har skjedd en feil, ingen vare er valgt!");
    return false;
  }
  location.href='storefront.php?' + qu + '&action=cartAdd&id=' + id;
}
function chkLogin(el){
  if(el.usr.value<=0){
    alert("Du må skrive inn ditt brukernavn!");
    el.usr.focus();
    return false;
  }
  if(el.pas.value<=0){
    alert("Du må skrive inn ditt passord!");
    el.pas.focus();
    return false;
  }
}
function ValidateEmail(theinput){
  s=theinput.value
  if(s.search){
    return (s.search(new RegExp("^([-!#$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}$","gi"))>=0)
  }
  if(s.indexOf){
    at_character=s.indexOf('@')
    if(at_character<=0 || at_character+4>s.length)
      return false
    }
    if(s.length<6)
      return false
      else
      return true
}
function sf_search_chk(mode){
  if(mode){
    if (document.sf_search.q.value<=0){
      alert("Søkefeltet er tomt!");
      document.sf_search.q.focus();
      return false;
    } else if (document.sf_search.q.value.length<=2){
      alert("Søkeordet er for kort.\nDu må søke på minst 3 tegn.");
      document.sf_search.q.focus();
      return false;
    } else {
      return true;
    }
  } else {
    if (document.sf_search.q.value<=0){
      alert("Søkefeltet er tomt!");
      document.sf_search.q.focus();
    } else if (document.sf_search.q.value.length<=2){
      alert("Søkeordet er for kort.\nDu må søke på minst 3 tegn.");
      document.sf_search.q.focus();
    } else {
      document.sf_search.submit();
    }
  }
}
function sf_search_inline_chk(){
  if (document.sf_search_inline.q.value<=0){
    alert("Søkefeltet er tomt!");
    document.sf_search_inline.q.focus();
    return false;
  } else if (document.sf_search_inline.q.value.length<=2){
    alert("Søkeordet er for kort.\nDu må søke på minst 3 tegn.");
    document.sf_search_inline.q.focus();
    return false;
  } else {
    return true;
  }
}
function _forcenumber(myString) {
  var pattern = /\W|\D/gi;
  var newString = myString.replace(pattern,"")
  return newString;
}
function _removeWhiteSpace(myString){
  return myString.replace(/ {2,}/g,' ').replace(/[\n\r]*/g,'');
}
// redefining default features
var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=500,height=400';
/*
listen('load', window, function() {
	listen('click', 'popup-listen', event_popup );
	listen('click', 'popup-feat'  , event_popup_features('location=0,statusbar=1,menubar=1,width=190,height=300') );
	//mlisten('click', getElementsByClass('popup','a'), event_popup );
});
*/

function urlencode(d,e) {
 if (typeof(encodeURIComponent) == 'function') {
  if (e) return encodeURI(d);
  else return encodeURIComponent(d);
 } else {
  return escape(d);
 }
}

function remove_nl_text(el){
  if(el.value = 'Din e-post adresse her'){
    el.value = '';
  }
  if(el.value = 'Your email here'){
    el.value = '';
  }
}

function chkPurchase(el){
  // This function is not in use at the moment, should validate
  // that the number entered in the input field infact in numeric.
  return true;
}

/*
  Usage: 
  if(isValid.decimal(check_value)){ passed }
  if(isValid.currency(check_value)){ passed }
*/
var isValid = (function(){
  var integerTest = /^\d+$/;
  //    var decimalTest = /^\d+\.?\,?\d*$/;
  var decimalTest = /^(-|\d)\d+\.?\,?\d*$/;

  var currencyTest = /^\d+\.\d\d$/;
  return {
    integer: function(n){
      return integerTest.test(n);
    },
    decimal: function(n){
      return decimalTest.test(n);
    },
    currency: function(n){
      return currencyTest.test(n);
    }
  };
})();


        function display_with_tax_correction(amount){
          var switches_7 = EWS_switches_7;
          var tax = EWS_tax;
          var taxmode = EWS_taxmode;
          var display_with_tax = EWS_display_with_tax;

          if(switches_7){
            if(!taxmode){
              if(display_with_tax){
                amount =  amount * ((tax / 100) + 1);
              } else {
                amount = amount;
              }
            } else {
              if(display_with_tax){
                amount = amount;
              } else {
                amount = amount - (amount - (amount / ((tax / 100) + 1)));
              }
            }
          } else {
            amount = amount;
          }
          return amount;
        }

        function CURRENCY(amount,mode){


          var tax_correction = EWS_tax_correction;
          var currency = EWS_currency;
          var space = EWS_space;

          var decimal_length = EWS_decimal_length;
          var dec_point = EWS_dec_point;
          var thousands_sep = EWS_thousands_sep;

          amount = amount / (currency['rate'] / 1000);

          if(tax_correction)
            amount = number_format(display_with_tax_correction(amount),decimal_length,dec_point,thousands_sep);
            else
            amount = number_format(amount,decimal_length,dec_point,thousands_sep);
          switch(mode){
            case 1:
            case 2:
              if(currency['direction'] == 'left')
                var o = '$.100';
                else
                var o = '100.$';
                break;
            case 3:
              if(currency['direction'] == 'left')
                var o = '%now% $.100';
                else
                var o = '%now% 100.$';
                break;
            case 4:
              if(currency['direction'] == 'left')
                var o = '%before% <DEL><STRIKE>$.100</STRIKE></DEL>';
                else
                var o = '%before% <DEL><STRIKE>100.$</STRIKE></DEL>';
                break;
            case 5:
              if(currency['direction'] == 'left')
                var o = '%veiledende_price% $.100';
                else
                var o = '%veiledende_price% 100.$';
                break;
            case 6:
              if(currency['direction'] == 'left')
                var o = '%our_price% $.100';
                else
                var o = '%our_price% 100.$';
                break;
            case 7:
              if(currency['direction'] == 'left')
                var o = '%you_save% $.100';
                else
                var o = '%you_save% 100.$';
                break;
            default:
                var o = '100';
          }
          var symbols = ['$','€','£'];
          if(!in_array(currency['symbol'],symbols)){
            var symbl = currency['symbol'] + ' ';
            var symbr = ' ' + currency['symbol'];
          } else {
            var symbl = currency['symbol'];
            var symbr = currency['symbol'];
          }

          o = str_replace('%you_save%',_you_save,o);
          o = str_replace('%our_price%',_our_price,o);
          o = str_replace('%veiledende_price%',_veiledende_price,o);
          o = str_replace('%before%',_before,o);
          o = str_replace('%now%',_now,o);
          o = str_replace('.$',symbr,o);
          o = str_replace('$.',symbl,o);
          o = str_replace('100',amount,o);
         return str_replace(' ',space,o);
        }


