// Quickbuy ///////////////////////////////////////\\\
function quickBuy(sel) {
	var selindex = sel.selectedIndex;
	if(selindex) {
		var item = sel.options[selindex];
		var pagechoice = item.attributes["inf"]?"details":"quickbuy";
		var selindex = item.attributes["redirect"]?item.attributes["redirect"].value:item.value;
		if(item.attributes["confirm"]) {
			if (!confirm(item.attributes["confirm"].value)) return false;
		}
		window.location.href = baseUrlSSL + "/article/"+pagechoice+".asp?ArticleId="+selindex
	}
}
///////////////////////////////////////////////////\\\


function toSession(em) {
	var nam = em.name;
	var val = em.value;
	if(em.type=="checkbox" && (!em.value || em.value=="on"))
		val = em.checked?"1":"";
	var url = "/_session.asp?var="+nam+"&val="+val;
	new Ajax.Request(url);
}
function toSession(nam,val) {
	var url = "/_session.asp?var="+nam+"&val="+val;
	new Ajax.Request(url);
}


function getXCoord(element) {
	var left = 0;
	if(!element.offsetParent) return element.x;
	else while(element.offsetParent) {
		left += element.offsetLeft;
		element = element.offsetParent;
	}
	return left;
}
function getYCoord(element) {
	var top = 0;
	if(!element.offsetParent) return element.y;
	else while(element.offsetParent) {
		top += element.offsetTop;
		element = element.offsetParent;
	}
	return top;
}
function findElement(element) {
	var coord = new Object();
	coord.x0 = getXCoord(element);
	coord.y0 = getYCoord(element);
	coord.x1 = coord.x0+element.offsetWidth;
	coord.y1 = coord.y0+element.offsetHeight;
	coord.width = element.offsetWidth;
	coord.height = element.offsetHeight;
	coord.em = element;
	return coord;
}

function getElementsColl(collection, sattrib, svalue) {
   var cem;
   var coll = new Array();
   var seek = new RegExp("(^|\\s)" + svalue + "(\\s|$)"); // borrowed from Robert Nyman.
   for(var i=0; i<collection.length; ++i) {
      cem = collection[i];
      if(cem.attributes && cem.attributes[sattrib]) {
         if(svalue=="") {
            coll.push(cem);
         } else
         if(seek.test(cem.getAttribute(sattrib))) {
            coll.push(cem); 
         }
      }
   }
   return coll;
}
function getElements(parent, sattrib, svalue) {
   return getElementsColl(parent.getElementsByTagName("*"),sattrib,svalue);
}
document.getElements = function(sattrib, svalue) {
   return getElements(this,sattrib,svalue);
}
function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn, false); 
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	} else {
		return false;
	}
}
function removeClass(em, clsname) {
   while(em.className.indexOf(clsname)>-1) em.className = em.className.replace(clsname,"");
}
function addClass(em, clsname) {
   if(em.className.indexOf(clsname)==-1) em.className += " "+clsname;
}




var an = navigator.userAgent;
nav = an.indexOf("Firefox")>-1?0:an.indexOf("Opera")>-1?1:an.indexOf("MSIE")>-1?2:3
FF = 0;
Opera = 1;
IE = 2;
Other = 3;


function dateDiff(p_Interval, p_Date1, p_Date2){
var dt1 = p_Date1
var dt2 = p_Date2
var iDiffMS = dt2.valueOf() - dt1.valueOf();
var dtDiff = new Date(iDiffMS);

var nYears  = dt2.getUTCFullYear() - dt1.getUTCFullYear();
var nMonths = dt2.getUTCMonth() - dt1.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
var nQuarters = parseInt(nMonths/3);

var nMilliseconds = iDiffMS;
var nSeconds = parseInt(iDiffMS/1000);
var nMinutes = parseInt(nSeconds/60);
var nHours = parseInt(nMinutes/60);
var nDays  = parseInt(nHours/24);
var nWeeks = parseInt(nDays/7);

var iDiff = 0;
switch(p_Interval.toLowerCase()){
case "yyyy": return nYears;
case "q": return nQuarters;
case "m": return nMonths;
case "y": 
case "d": return nDays;
case "w": return nDays;
case "ww":return nWeeks;
case "h": return nHours;
case "n": return nMinutes;
case "s": return nSeconds;
case "ms":return nMilliseconds;
default: return "invalid interval: '" + p_Interval + "'";
}
}

// CATCH EVENTS
if (!document.all) {document.captureEvents(Event.KEYDOWN|Event.KEYUP);}
var eventKey = new Object();
window.onblur = function() {for (var a in eventKey) eventKey[a] = false;}
document.onkeyup = function(e)
{
if (!document.all) {event = e;}
if (event.keyCode > 0) {eventKey[event.keyCode] = false;}
if (window.onKeyUp) onKeyUp();
}
document.onkeydown = function(e)
{
if (!document.all) {event = e;}
if (event.keyCode > 0) {eventKey[event.keyCode] = true;}
if (window.onKeyDown) onKeyDown();
}

// FUNCTIONS
function generalInit() {
HELPinit();
}


var __flash_timers = new Array();
function flash(fetch,alt1,alt2,maxcount,timeout1,timeout2,curcount) {
if (!curcount) {
curcount = 0;
if (!document.getElementById(fetch)) fetch = fetch.id;
var em = document.getElementById(fetch);
if (!maxcount) maxcount = 2;
if (!timeout1) timeout1 = 1100;
if (!timeout2) timeout2 = 250;
if (!alt1) alt1 = em.className;
if (!alt2) alt2 = em.className+"_flash";
}

var em = document.getElementById(fetch);
var isAlt1 = em.className==alt1;
em.className = isAlt1?alt2:alt1;
curcount+=.5;

clearTimeout(__flash_timers[fetch]);
if (curcount<maxcount) {
__flash_timers[fetch] = setTimeout("flash(\""+fetch+"\",\""+alt1+"\",\""+alt2+"\","+maxcount+","+timeout1+","+timeout2+","+curcount+");",isAlt1?timeout1:timeout2);
} else em.className=alt1;
}

function submit_qbuy(which) {
if(which>-1) document.frmqbuy.submit();
}

function trim(s)
{
return s.replace(/^(\s)+/, "").replace(/(\s)+$/, "");
}

function verifyOrgId(n)
{
var re = new RegExp("^([0-9]{6})-([0-9]{4})$", "ig");
var ma = re.exec(n);
return (ma && (calculateCheckDigit(ma[1] + ma[2].substring(0,3)) == ma[2].charAt(3)));
}

function calculateCheckDigit(num)
{
    var i, n, sum = 0, l;
    
    num = num.toString();
    l = num.length;
    if(l==10) l--;
    for(i = l - 1; i >= 0; i--)
    {
        n = parseInt(num.charAt(i)) * (2 - ((l + 3 - i) % 2));
        sum += parseInt(Math.floor(n / 10) + (n % 10));
}
return (10 - (sum % 10)) % 10;
}

function verifyEmail(e, mandatory)
{
if (trim(e) == "") return !mandatory;
var re = /^.+@.+\..+$/ig;
return (re.exec(e) != null);
}

function verifyPhone(p, mandatory)
{
if(trim(p) == "") return !mandatory;
var re = /^([0-9]|\s|\+|\-|\(|\))+$/ig;
return (re.exec(p) != null);
}

function parseFloatEx(f)
{
return parseFloat(f.toString().replace(",", "."));
}

// Returnerar -1 om allt är ok, annars 0 och uppåt. (den plats där första felet påträffades)
function checkAllowedChar(str)
{
var allowedchar = /[^-\^_\^a-z\^A-Z\^0-9]/gi;
var check = str.search(allowedchar);
return check;
}

function checkEmail(s)
{
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
var emailad=s;

if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1))
{
return false;
} else 
{
return true;
}

}

function parseDate(dstr)
{
var re = /^([0-9]{4})-([0-1][0-9])-([0-3][0-9])$/ig;
var match = re.exec(dstr);
if(!match) return null;
if(match[2] < 1 || match[2] > 12) return null;
var days = new Array(31,(parseInt(match[1]) % 4 ? 28 : 29),31,30,31,30,31,31,30,31,30,31);
if(match[3] < 1 || match[3] > days[match[2] - 1]) return null;
return new Date(match[1], match[2], match[3]);
}

function time(n)
{
var re = /^(([0-2])?[0-9])\:([0-5][0-9])$/ig;
var ma = re.exec(trim(n));

if(!ma)
this.valid = false;
else
{
this.valid = (parseInt(ma[1]) < 24);
this.hour = parseInt(ma[1]);
this.minute = parseInt(ma[3]);
}
}
function formatPrice(p, l, delim)
{
var b = Math.pow(10, l);

p = parseFloatEx(p);
p = Math.round(p * b) / b;

p = p.toString().split(".");

var h = "", d = "", j = 0;

var nil = "0000000000000000000";

if(p.length == 1)
d = nil.slice(0, l);
else if(p[1].length < l)
d = p[1] + nil.slice(0, l - p[1].length);
else
d = p[1];

for(var i = p[0].length - 1; i >= 0; i--)
{
if(j && (j % 3) == 0) h = " " + h;
h = p[0].charAt(i) + h;
j++;
}

if(l > 0)
{
if(!delim) delim = ",";
}
else
delim = "";

return h + delim + d;
}
function round(number, numdecdig)
{
if (numdecdig == null) return number;

var n = Math.pow(10, numdecdig);

number = Math.round(number * n);
number = number / n;

return number;
}

function coalesce(val1, val2)
{
if (val1 != null && trim(val1) != "")
return val1;
else
return val2;
}

function makeStringSafe(str)
{
str = str.replace(/ö/gi, "o");
str = str.replace(/å/gi, "a");
str = str.replace(/ä/gi, "a");
str = str.replace(/\&/gi, "o");
str = str.replace(/ü/gi, "u");
str = str.replace(/é/gi, "e");
str = str.replace(/è/gi, "e");
str = str.replace(/á/gi, "a");
str = str.replace(/à/gi, "a");
str = str.replace(/\//gi, "-");
str = str.replace(/\\/gi, "-");
str = str.replace(/\$/gi, "S");
str = str.replace(/\£/gi, "L");
str = str.replace(/\*/gi, "x");
str = str.replace(/\s/gi, "_");

str = str.replace(/(\W|^-)/gi, "");

return str;
}

function list_setSelectedItem(sel, value)
{
for (var i = 0; i < sel.options.length && sel.options[i].value != value; i++);

var ok = (i < sel.options.length);

if (ok) sel.selectedIndex = i;

return ok;
}

function list_moveSelected(sourceSel, targetSel)
{
for (var i = 0; i < sourceSel.options.length; )
{
if (sourceSel.options[i].selected)
{
//var newOption = new Option(sourceSel.options[i].text, sourceSel.options[i].value, false, false);
list_insert(targetSel, sourceSel.options[i].text, sourceSel.options[i].value);
sourceSel.remove(i);
}
else
i++;
}
list_sort(targetSel);
}

function list_insert(targetSel, optionText, optionValue)
{
newOption = new Option(optionText, optionValue, false, false);
targetSel.options[targetSel.options.length] = newOption;
}

function selectSort(a, b)
{
var aVal = a.split("\n")[1].toUpperCase();
var bVal = b.split("\n")[1].toUpperCase();

if (aVal < bVal)
return -1;
else if (aVal > bVal)
return 1;
return 0;
}

function list_sort(sel)
{
//don't work if there are one \n in the string

var opt = new Array();
for (var i = 0; i < sel.options.length; i++)
{
opt[i] = sel.options[i].value + "\n" + sel.options[i].text;
}

opt.sort(selectSort);

for (var i = 0; i < opt.length; i++)
{
var optData = opt[i].split("\n");
sel.options[i].value = optData[0];
sel.options[i].text = optData[1];
}
}

function list_excludeContent(sourceSel, targetSel)
{
var j, targetVal, i = 0;

for (j = 0; j < targetSel.options.length; j++)
{
targetVal = targetSel.options[j].value;
while (i < sourceSel.options.length && parseInt(sourceSel.options[i].value) != parseInt(targetVal)) i++;
if (i != sourceSel.options.length) sourceSel.remove(i);
i = 0;
}
}

function list_getValueString(sel)
{
var str = "";
for(var i = 0; i < sel.options.length; i++)
{
if (i) str += "|";
str += sel.options[i].value;
}
return str;
}

function list_removeAll(sel)
{
while (sel.length > 0)
sel.remove(0);
}


Math.roundUp = function(number)
{
var i = parseInt(number);
if (i < number) i++;
return i;
}

function countLines(string, lineLetter)
{
var v = string.split("\n");
var lc = v.length;

for (var i = 0; i < v.length; i++)
lc += max(Math.roundUp(v[i].length / lineLetter) - 1, 0);

return lc;
}

function min(value1, value2)
{
return (value1 < value2 ? value1 : value2);
}

function max(value1, value2)
{
return (value1 > value2 ? value1 : value2);
}

function openModalDialog(url, string, properties)
{
if (string == null) string = "";
var name = "win" + Math.round(Math.random() * 1000);
return window.showModalDialog("../include/modalcontainer.asp?String=" + escape(string) + "&Page=" + escape(url), name, properties);
}

function imagePopup(image, name) {
open("../include/imagepopup.asp?ImageId="+image,"_popup","left=50, top=50, width=250, height=250, resizable=no, scrollable=no, toolbar=no, status=no, menubar=no")
}
function popup(src, width, height) {
	return !window.open(src,"_lspop","scrollbars=yes, left=50, top=50, width="+width+", height="+height+", resizable=no, toolbar=no, status=no, menubar=no");
}


function openCalender(field, currentDate)
{
if (currentDate == null) currentDate = "";
var url = "smallcal.asp?CurrentDate=" + escape(currentDate);
var date = openModalDialog(url, "", "dialogWidth: 160px; dialogHeight: 200px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
if (date)
{
eval(field).value = date;
if (window.setSaveStatus)
setSaveStatus();
}
}

function openTime(field, currentTime)
{
if (currentTime == null) currentTime = "";
var url = "time.asp?CurrentTime=" + escape(currentTime) + "#" + currentTime.replace(":", "_");
var tm = openModalDialog(url, "", "dialogWidth: 100px; dialogHeight: 300px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
if (tm)
{
eval(field).value = tm;
if (window.setSaveStatus)
setSaveStatus();
}
}

function form_getFieldValues(f)
{
var fieldValue = new Array();
for (var i = 0; i < f.elements.length; i++)
{
var e = f.elements[i];
var tmp;

if (e.type == "select-one")
{
if (e.options.length)
tmp = e.options[e.selectedIndex].value;
}
else if (e.type == "checkbox")
tmp = e.checked;
else
tmp = e.value;
fieldValue[e.name] = tmp;
}
return fieldValue;
}

function array_isEqual(array0, array1)
{
if (array0.length != array1.length) return false;
for (var k in array0)
{
if (array0[k].toString() != array1[k].toString())
return false;
}
return true;
}