﻿function getBrowserHeight(id){
    var x = document.getElementById(id);
	x.style.minHeight = (window.screen.availHeight - 401) + 'px';
//    if (window.innerWidth){
//        return window.innerWidth;}  
//    else if (document.documentElement && document.documentElement.clientWidth != 0){
//        return document.documentElement.clientWidth;    }
//    else if (document.body){return document.body.clientWidth;}      
//        return 0;
}
// JScript File
// Håller koll på vart musen är på sidan
// Läser in GET-variablerna i URLen
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
        
// Redirectar till article.aspx med korrekt id och användare
function RedirectOnClick(id)
{
    var AddPurchaserID;
    AddPurchaserID = '';
    var hash = getUrlVars();
    if ('purchaser_id' in hash)
        AddPurchaserID = '&purchaser_id=' + hash['purchaser_id'];
            
    window.location = 'article.aspx?article_id=' + id + AddPurchaserID;            
}

// Redirectar till product.aspx med korrekt id och användare
function RedirectOnClick2(id)
{
    var AddPurchaserID;
    AddPurchaserID = '';
    var hash = getUrlVars();
    if ('purchaser_id' in hash)
        AddPurchaserID = '&purchaser_id=' + hash['purchaser_id'];
            
    window.location = 'product.aspx?product_id=' + id + AddPurchaserID;            
}

// Redirectar menypunkterna ifall man klickar någonstans på menypunkten och inte endast på texten.
function DivLink(url)
{
    window.location = url;
    //alert('Yes ' + url);    
//    var div = document.getElementById('TopMenu' + id);
//    div.onClick = 
}

// Hanterar defaultknapp på sidan
function clickButton(e, buttonid){ 
    var bt = document.getElementById(buttonid); 
    if (typeof bt == 'object'){ 
        if(navigator.appName.indexOf("Netscape")>(-1)){ 
            if (e.keyCode == 13){ 
                bt.click(); 
                return false; 
            } 
        } 
        if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
            if (event.keyCode == 13){ 
                bt.click(); 
                return false; 
            } 
        } 
    } 
} 
function cursorPosition(lay, hide) {   
    if (document.all)				//Explorer 4,5
	{
	//document.writeln(document.all.scrollTop);
	//document.writeln(Xpos+document.body.scrollLeft);
	//document.writeln(Xpos);
	document.writeln(Ypos+document.body.scrollTop);
	document.writeln(Ypos);
		document.alllay.style.left = Xpos+document.body.scrollLeft;
		document.alllay.style.top = Ypos+document.body.scrollTop;
	}
	else if (document.layers)			//Netscape 4
	{
		document.layerslay.left = Xpos;
		document.layerslay.top = Ypos;
	}
	else if (document.getElementById)	//Netscape 6
	{
		document.getElementById(lay).style.left = Xpos;
		document.getElementById(lay).style.top = Ypos;
	}
	HideLayer(lay, hide);
}


function HideLayer(lay, hide) {
    if (document.all)				//Explorer 4,5
	{
		document.alllay.style.visibility = (hide) ? 'hidden' : 'visible';
	}
	else if (document.layers)			//Netscape 4
	{
		document.layerslay.visibility = (hide) ? 'hidden' : 'visible';
	}
	else if (document.getElementById)	//Netscape 6
	{
		document.getElementById(lay).style.visibility = (hide) ? 'hidden' : 'visible';
	}
}
// Fokussättningen på inloggningen (
function clearContents(orgvalue, textbox, type){
    if (type == 2) {
        fhide(true);
    }
    else if (orgvalue == textbox.value){
        textbox.value = "";
    }
}
function restoreContents(orgvalue, textbox, type){
    if (type == 1 && textbox.value == "") 
        textbox.value = orgvalue;
    if (type == 2 && textbox.value == ""){
        fhide(false);
        textbox.value = "";
    }
}

function fhide(hide)
{
	var x = document.getElementById('passwordlayer');
	x.style.visibility = (hide) ? 'hidden' : 'visible';
}

var winOpenHelp
function f_OpenHelpWin(s_Url)
{  	
	if (winOpenHelp && winOpenHelp.open && !winOpenHelp.closed) winOpenHelp.close();
	winOpenHelp = window.open(s_Url,'WinHelp','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,left=40,top=40,copyhistory=no,width=500,height=300');
	winOpenHelp.focus();
}

function CheckQtyKVH(minqty, tbx, textDiv) {
	var minqty,qty;
	//window.confirm(navigator.appName);
	//window.confirm(parseInt(navigator.appVersion));
//	if (document.all)
//	    qty = document.all(tbx).value;
//	else
	    qty = document.getElementById(tbx).value;
	
    if ((qty % minqty) != 0)
    {
        
        
               //document.all(textDiv).value = '<div style="background: yellow; border: solid thin black; text-align: center; padding: 2px" class="text1">Välj att köpa obruten förpackning i stället. Då spar du pengar och det går snabbare att plocka din order från lagret.</div><br />'; 
        if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) <= 4) {
            document.getElementById(textDiv).style.removeAttribute('display'); }
        else {
            document.getElementById(textDiv).style.removeProperty('display'); }
        //window.confirm('MinQty: ' + minqty + ' qty: ' + qty + '\n minqty % qty: ' + (minqty % qty) + '\n qty % minqty: ' + (qty % minqty));
    }   
    else
        document.getElementById(textDiv).style.display = 'none';
}

function CheckQty(minqty, tbx, intype) {
	var qtysaveok, okqty, minqty, qty;
	qty = document.all(tbx).value;

	qtysaveok = false;
	okqty = qty % minqty;
	if (okqty == 0) 
		qtysaveok = true;
	else
		okqty = minqty;
	// Angivet antal är ej ok, sätt minsta tillåtna och meddela att ändring gjorts.
	if (qtysaveok == false) {
		document.all(tbx).value = okqty;
		if (intype == 1)
		    return window.confirm('OBS: Vald artikel är ej lagervara, vilket innebär en längre leveranstid \noch att ni endast kan beställa hela prissatta förpackningar. \nBeställt antal har satts till minsta tillåtna förpackning(' + okqty + '). \n\nBekräfta genom att trycka på OK.');
		else
		    return window.confirm('OBS: Vald artikel kan endast beställas i hela prissatta förpackningar. \nBeställt antal har satts till minsta tillåtna förpackning(' + okqty + '). \n\nBekräfta genom att trycka på OK.');
	}
	else if (intype == 1)
		return window.confirm('OBS: Vald artikel är ej lagervara, vilket innebär en längre leveranstid \noch att ni endast kan beställa hela prissatta förpackningar. \nEventuell frakt tillkommer. \n \nBekräfta genom att trycka på OK.'); 
}
function CheckQtyHelm(minqty, tbx, intype) {
	var qtysaveok, okqty, minqty, qty;
	qty = document.all(tbx).value;

	qtysaveok = false;
	okqty = qty % minqty;
	if (okqty == 0) 
		qtysaveok = true;
	else
		okqty = minqty;
	// Angivet antal är ej ok, sätt minsta tillåtna och meddela att ändring gjorts.
	if (qtysaveok == false) {
		document.all(tbx).value = okqty;
		if (intype == 1)
		    return window.confirm('OBS: Vald artikel är ej lagervara, vilket innebär en längre leveranstid \noch att ni endast kan beställa hela prissatta förpackningar. \nBeställt antal har satts till minsta tillåtna förpackning(' + okqty + '). \n\nBekräfta genom att trycka på OK.');
		else
		    return window.confirm('OBS: Vald artikel kan endast beställas i hela prissatta förpackningar. \nBeställt antal har satts till minsta tillåtna förpackning(' + okqty + '). \n\nBekräfta genom att trycka på OK.');
	}
	else if (intype == 1)
		return window.confirm('OBS: Vald artikel är ej lagervara, vilket innebär en längre leveranstid \noch att ni endast kan beställa hela prissatta förpackningar. \nEventuell frakt och hemtagningskostnad (85kr) tillkommer. \n \nBekräfta genom att trycka på OK.'); 
}

// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
currentYPos = 0; // Current mouseposition on screen including scroll
currentXPos = 0;

function doSomething(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}




function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has 
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        //alert('1');
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
        currentYPos = e.pageY - window.pageYOffset;
        currentXPos = e.pageX - window.pageXOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position 
        // reflects the position from the top/left of the screen the 
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no 
        // matter if the user has scrolled or not.
        //alert('2');
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
         var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
         if (ieversion>=9)
         {
          xMousePos = e.pageX;
          yMousePos = e.pageY;
          xMousePosMax = window.innerWidth+window.pageXOffset;
          yMousePosMax = window.innerHeight+window.pageYOffset;
          currentYPos = e.pageY - window.pageYOffset;
          currentXPos = e.pageX - window.pageXOffset;
         }
         else
         {
//         else if (ieversion>=8)
//          alert("You're using IE8 or above")
//         else if (ieversion>=7)
//          alert("You're using IE7.x")
//         else if (ieversion>=6)
//          alert("You're using IE6.x")
//         else if (ieversion>=5)
//          alert("You're using IE5.x")
        
            xMousePos = window.event.x+document.body.scrollLeft;
            yMousePos = window.event.y+document.body.scrollTop;
            xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
            yMousePosMax = document.body.clientHeight+document.body.scrollTop;
            currentYPos = window.event.y-document.documentElement.scrollTop;
            currentXPos = window.event.x-document.documentElement.scrollLeft;
        }
       }
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard 
        //alert('3');
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
        currentYPos = e.pageY - window.pageYOffset;
        currentXPos = e.pageX - window.pageXOffset;
    }
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
    while(1) 
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function ChangeBigImage(obj, BigImageID, storBildUrl){

    var imgagen =  document.getElementById(BigImageID);
    if (imgagen.src!=storBildUrl) {
        imgagen.src=storBildUrl;
        
        var storDiven =  document.getElementById('storBildDiv');
        storDiven.style.visibility = 'visible';
        //alert(event.clientX);
        //alert(event.clientY);
//        alert(screen.height);
        //alert(event.x);
        //alert(e.pageX);
        //alert(screen.width / 2);
        //alert(xMousePos);
        //alert(xMousePos);
        //alert(xMousePosMax);
        //alert('yMousePos: ' + yMousePos + ' document.documentElement.scrollTop: ' + (window.event.y - document.documentElement.scrollTop) + ' screen.height: ' + (screen.height / 2) + ' - xMousePos: ' + xMousePos + ' screen.width: ' + (screen.width / 2)); 
        //var ou;
        //var hv;
        //if (event.clientY > (screen.height / 2))
        if ((currentYPos) > (screen.height / 2)){
            storDiven.style.top = ((-1 * imgagen.height) + yMousePos - 10) + 'px'; // Visa bilden över
            ou = 'Över'
            }
        else {
            storDiven.style.top = (yMousePos + 10) + 'px'; // Visa bilden under            
            ou = 'Under'
            }
        //if (event.clientX > (screen.width / 2))
        if ((xMousePos) > (screen.width / 2)){
            storDiven.style.left = ((-1 * imgagen.width) + xMousePos - 10) + 'px'; //Visa vänster
            vh = ' Vänster'
             }
        else{
            storDiven.style.left = (xMousePos + 10) + 'px'; //Visa höger
            vh = ' Höger'
             }
//        alert(storDiven.style.top);
//        alert(ou + vh);
    }
}
function HideBigImage(obj){
    document.getElementById('storBildDiv').style.visibility = 'hidden';
}

function MouseOverMenu(Node, Divider, CssText){
    var MenuNode;
    MenuNode = 'TopMenu' + Node;
    document.getElementById(MenuNode).className = CssText + ' TopMenuStyleHover' + Divider;
    //document.getElementById(MenuNode).className = 'TopMenuStyle TopMenuStyleHover' + Divider;
    
}

function MouseOutMenu(Node, Divider, Active, CssText){
    var MenuNode;
    MenuNode = 'TopMenu' + Node;
    //alert(Active);
    if (Active == '1'){ 
        document.getElementById(MenuNode).className = CssText + ' TopMenuStyleActive' + Divider; 
        }
    else { document.getElementById(MenuNode).className = CssText + ' TopMenuStyle' + Divider; }
}

function MouseOverButton(Id, i){
    var TheTable;
    TheTable = Id.parentNode.parentNode
    TheTable.cells[0 + i].className = 'btnLeftMouseOver'
    TheTable.cells[1 + i].className = 'btnMiddleMouseOver'
    TheTable.cells[2 + i].className = 'btnRightMouseOver'    
}

function MouseOutButton(Id, i){
    var TheTable;
    TheTable = Id.parentNode.parentNode
    TheTable.cells[0 + i].className = 'btnLeft'
    TheTable.cells[1 + i].className = 'btnMiddle'
    TheTable.cells[2 + i].className = 'btnRight'    
}

function MouseOverButton2(Id, i){
    var TheTable;
    TheTable = Id.parentNode.parentNode
    TheTable.cells[0 + i].className = 'btnLeftMouseOver'
    TheTable.cells[1 + i].className = 'btnMiddleMouseOver'
    TheTable.cells[2 + i].className = 'btnRightMouseOver'    
}

function MouseOutButton2(Id, i){
    var TheTable;
    TheTable = Id.parentNode.parentNode
    TheTable.cells[0 + i].className = 'btnLeftWhite'
    TheTable.cells[1 + i].className = 'btnMiddleWhite'
    TheTable.cells[2 + i].className = 'btnRightWhite'    
}

function setTdWidth()
{
    //tabellen: tblArticleInfo, celler: td_descr, td_image
    //window.defaultStatus = "This is the status bar";
    //document.getElementById("ctl00_Main_tblArticleInfo").offsetHeight
    
    var descrHeight = 0;
    var descrWidth = 0;
    var imgHeight = 0;
    
    if (document.getElementById)			//Netscape 6
    {
        descrHeight = document.getElementById('ctl00_Main_td_descr').offsetHeight;
        descrWidth = document.getElementById('ctl00_Main_td_descr').offsetWidth;
        imgHeight = document.getElementById('ctl00_Main_td_image').offsetHeight;
        //alert('test3' + descrWidth);
    }
    else if (document.layers)				//Netscape 4
    {
        descrHeight = document.layers['ctl00_Main_td_descr'].document.height;
        descrWidth = document.layers['ctl00_Main_td_descr'].document.width;
        imgHeight = document.layers['ctl00_Main_td_image'].document.height;
        //alert('test2' + descrWidth);
    }
    else if (document.all)					//Explorer 4,5
    {
        descrHeight = document.all['ctl00_Main_td_descr'].clientHeight;
        descrWidth = document.all['ctl00_Main_td_descr'].clientWidth;
        imgHeight = document.all['ctl00_Main_td_image'].clientHeight;
        //alert('test' + descrWidth);
    }
        
    //var imgWidth = document.getElementById("ctl00_Main_td_image").offsetWidth;
    //var tblWidth = document.getElementById("ctl00_Main_tblArticleInfo").offsetWidth;
    if ((descrWidth < 200 && descrWidth > 100) || (imgHeight > 20 && imgHeight < descrHeight))
    {
        if (document.getElementById)			//Netscape 6
            document.getElementById("ctl00_Main_tblArticleInfo").style.width = '660px';
        else if (document.layers)				//Netscape 4
            document.layers['ctl00_Main_tblArticleInfo'].style.width = '660px';
        else if (document.all)					//Explorer 4,5
            document.all['ctl00_Main_tblArticleInfo'].style.width = '660px';
            
    }
     
    if (descrWidth == 0) {
        if (document.getElementById)			//Netscape 6
        {
            document.getElementById("ctl00_Main_td_descr").style.width = '220px';
            document.getElementById("ctl00_Main_td_descr").appendChild(document.createTextNode('&nbsp;&nbsp;&nbsp;'));
        }
        else if (document.layers)				//Netscape 4
        {
            document.layers("ctl00_Main_td_descr").style.width = '220px';
            document.layers("ctl00_Main_td_descr").appendChild(document.createTextNode('&nbsp;&nbsp;&nbsp;'));
        }
        else if (document.all)					//Explorer 4,5
        {
            document.all("ctl00_Main_td_descr").style.width = '220px';
            document.all("ctl00_Main_td_descr").appendChild(document.createTextNode('&nbsp;&nbsp;&nbsp;'));
        }
    }
}
