//window.onresize = function(){
//	Fix();
//}

var SITEURL = 'www.storeys.no';

function setSiteUrl( url )
{
	SITEURL = url;
}

function getSiteUrl()
{
	return "http://" + SITEURL + "/";	
}

function getSiteUrlPlain()
{
	return "http://" + SITEURL + "/";	
}

function fixPageHeight( )
{

	setTimeout("Fix()",0);

}

function showHide( div )
{
	tmpdiv = document.getElementById( div );
	
	if ( tmpdiv.style.display == 'inline' )
		tmpdiv.style.display = 'none';	
	else
		tmpdiv.style.display = 'inline';	
}

function showHideCol( div, pointer )
{
	tmpdiv = document.getElementById( div );
	aobj = document.getElementById( pointer );
	
	if ( tmpdiv.style.display == 'inline' )
	{
		tmpdiv.style.display = 'none';	
		aobj.style.color = '#000';
	}
	else
	{
		tmpdiv.style.display = 'inline';	
		aobj.style.color = '#ed1c24';
	}
}


function swapGalleryImg( path, height, counter )
{
	document.getElementById( 'showImage' ).height = height + 'px';
	document.getElementById( 'theimg' ).src = path;
}

function onGalleryImg( counter )
{
	document.getElementById( 'imagex' + counter ).style.border = '2px solid #333333';
}

function outGalleryImg( counter )
{
	document.getElementById( 'imagex' + counter ).style.border = '2px solid #e2e2e2';
}




function Fix()
{
	var wrap 		= document.getElementById('wrap');
	var wrapHeight 	= wrap.offsetHeight+20;
	var winHeight 	= windowHeight();
	
	var marginTop = parseInt( (winHeight-wrapHeight)/2 );
	
	if ( marginTop > 0 )
		wrap.style.marginTop = marginTop + "px";
	
}

function windowHeight()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	
	return myHeight;
}


function swapImg( direction, maxx )
{
	var current = document.getElementById( 'current' ).value;
	
	var newcurrent;
	
	if ( direction == 'prev' )
	{
		if ( current == 0 )
			newcurrent = maxx-1;	
		else
			newcurrent = parseInt( current-1 );
	}
	else
	{
		if ( current == parseInt( maxx-1 ) )
			newcurrent = 0;	
		else
			newcurrent = parseInt( current+1 );
	}
	
	document.getElementById( "current" ).value = newcurrent;
	document.getElementById( "swapimg" + current ).style.display='none';
	document.getElementById( "swapimg" + newcurrent ).style.display='inline';
}

var COUNTER;
var COLORNAME;
var IMAGE;
var GROUPCODE;
var PARENTNODE;
var PRICELISTID;
var CUSTOMERID;
var ALTTEXT;

function swapColor( counter, colorname, alttext, groupcode, parentnode, image, pricelistid, customerid )
{
	COUNTER = counter;
	COLORNAME = colorname;
	ALTTEXT = alttext;
	IMAGE = image;
	GROUPCODE = groupcode;
	PARENTNODE = parentnode;
	PRICELISTID = pricelistid;
	CUSTOMERID = customerid;
	
	getSimilar();
}

var http_request = false;
var myFunction = '';
   
function postRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Kan ikke opprette XMLHTTP instansen. Bytt browser');
	 return false;
  }
  
  http_request.onreadystatechange = hentPHP;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function hentPHP() {
  if ( http_request.readyState == 4 ) {
	 if ( http_request.status == 200 ) 
	 {
		//alert(http_request.responseText);
		result = http_request.responseText;
		if ( myFunction == 'getSimilar' )
		{
			document.getElementById( 'selectedcolor' + COUNTER ).innerHTML = COLORNAME + " <span>" + ALTTEXT + "</span>";
			str =  '<a href=\"javascript:clickImage(' + COUNTER + ');\">';
			str += '<img src=\"' + getSiteUrlPlain() +  IMAGE + '\">';
			str += '</a>';
			document.getElementById( 'swapimage' + COUNTER ).innerHTML = str;
			
			var splitArr = result.split( ";;" );
			var serials;
			var serialsitem;
			var serialids = '';
			
			removeList( 'products' + COUNTER );

			nodeids = '';
			
			var userID = document.getElementById( 'userID' ).value;
			
			for (i = 0; i < splitArr.length; i++)
			{
				splitTwo = splitArr[i].split( ",," );
				
				if ( splitTwo.length == 9 )
				{
					newdiv = document.createElement('div');
					newdiv.className ='itemline';
					
					superSTR = "<div class=\"item item1\">" + splitTwo[0] + "</div>";
					superSTR = superSTR + "<div class=\"item item2\">" + splitTwo[4] + "</div>";

					superSTR = superSTR + "<div class=\"item item3\">" + splitTwo[5] + "</div>";
					
					if ( userID != 10 )
					{
						if ( splitTwo[6] == '' )
							superSTR = superSTR + "<div class=\"item item4\">&nbsp;</div>";
						else
							superSTR = superSTR + "<div class=\"item item4\"><img onclick=\"javascript:alert('" + splitTwo[6] + "');\" width=\"15\" height=\"15\" src=\"http://www.storeys.no/extension/vztlibrary/design/vztdesign/images/vzt/NB.png\" /></div>";
	
						serials = splitTwo[8];
						serialsRow = serials.split( "---" );
						
						if ( serials.length > 2 )
							superSTR = superSTR + "<div class=\"item item5\"><a href=\"javascript:showHide('serial_" + splitTwo[2] + "');\">Vis</a></div>";
						else
							superSTR = superSTR + "<div class=\"item item5\">&nbsp;</div>";
						
	
						if ( splitTwo[3] == 0 )
							superSTR = superSTR + "<div class=\"item item6\"><span style=\"color: red;\">0</span></div>";
						else
							superSTR = superSTR + "<div class=\"item item6\">" + splitTwo[3] + "</div>";
							
						//superSTR = superSTR + "<div class=\"item item3\"><img src=\"http://www.gulvex.no/extension/vztlibrary/design/vztdesign/images/vzt/instock.gif\" /></div>";
						
						
						superSTR = superSTR + "<div class=\"item item7\">" + splitTwo[1] + ",-</div>";
						
						superSTR = superSTR + "<div class=\"item item8\"><input type=\"text\" class=\"antbox\" id=\"antbox_" + splitTwo[2] +"\"></div>";
						
						superSTR = superSTR + "<div id=\"serial_" + splitTwo[2] + "\" style=\"display: none;\">";
						
						for (j = 0; j < serialsRow.length; j++)
						{
							superSTR = superSTR + "<div class=\"itemline\">";
							
							serialsitem = serialsRow[j].split( "||" );
							superSTR = superSTR + "<div class=\"item item1\">&nbsp;</div>";
							superSTR = superSTR + "<div class=\"item item2\">&nbsp;</div>";
							superSTR = superSTR + "<div class=\"item item3\">Batch:</div>";
							superSTR = superSTR + "<div class=\"item item4\" style=\"width:10px;font-size:1px;\">&nbsp;</div>";
							superSTR = superSTR + "<div class=\"item item5\" style=\"width:54px;\">" + serialsitem[0] + "</div>";
	
							if ( serialsitem[1] == 0 )
								superSTR = superSTR + "<div class=\"item item6\"><span style=\"color: red;\">0</span></div>";
							else
								superSTR = superSTR + "<div class=\"item item6\">" + serialsitem[1] + "</div>";
							
							superSTR = superSTR + "<div class=\"item item7\">&nbsp;</div>";
							superSTR = superSTR + "<div class=\"item item8\"><input type=\"text\" class=\"antbox\" id=\"antbox_" + splitTwo[2] + "_" + serialsitem[0] + "\"></div>";
							superSTR = superSTR + "</div>";
							
							serialids = serialids + splitTwo[2] + "_" + serialsitem[0] + ";";
						}
						superSTR = superSTR + "<div style\"clear:both;\"></div>";
						superSTR = superSTR + "</div>";
					}
					
					newdiv.innerHTML = superSTR;
					document.getElementById( 'products' + COUNTER ).appendChild( newdiv );
					
					nodeids = nodeids + splitTwo[2] + ";";
				}
				else
				{
					newdiv = document.createElement('div');
					newdiv.className ='itemline';
					
					superSTR = "<div class=\"item item1\">" + splitTwo[0] + "</div>";
					superSTR = superSTR + "<div class=\"item item2\">" + splitTwo[1] + ",-</div>";
					
					if ( splitTwo[3] == 0 )
						superSTR = superSTR + "<div class=\"item item3\">&nbsp;</div>";
					else
						superSTR = superSTR + "<div class=\"item item3\"><img src=\"http://www.gulvex.no/extension/vztlibrary/design/vztdesign/images/vzt/instock.gif\" /></div>";
					
					if ( userID != 10 )
					{
						superSTR = superSTR + "<div class=\"item item4\"><input type=\"text\" class=\"antbox\" id=\"antbox_" + splitTwo[2] +"\"></div>";
					}
					
					newdiv.innerHTML = superSTR;
					document.getElementById( 'products' + COUNTER ).appendChild( newdiv );
					
					nodeids = nodeids + splitTwo[2] + ";";
				}
			}
				document.getElementById( 'serialids' + COUNTER ).value = serialids;
				document.getElementById( 'nodeids' + COUNTER ).value = nodeids;


		}
		
		if ( myFunction == 'addToBasket' )
		{
			if ( result == 1 )
				showDialog();
		}
		
		
		myFunction = '';
		
	} 
	 else 
	 {
		alert( 'Problemer med å finne fila det postes til.' );
	 }
  }
}
 
var numx; 
var node_id;
   
function getSimilar() 
{
	siteURL = getSiteUrl();
	myFunction = 'getSimilar';
	//alert( GROUPCODE + " - " + COLORNAME + " - " + PARENTNODE + " - " + CUSTOMERID + " - " + PRICELISTID );
	
	var poststr = "GROUPCODE=" + encodeURI( GROUPCODE ) + "&COLOR=" + encodeURI( COLORNAME ) + "&PARENT=" + encodeURI( PARENTNODE ) + "&CUSTOMERID=" + encodeURI( CUSTOMERID )+ "&PRICELISTID=" + encodeURI( PRICELISTID );
	postRequest( siteURL + "layout/set/ajax/vztlibrary/getsimilar/", poststr);

}

function removeList( div )
{
	hasSearchResult = 0;
	selected = 0;

	result = document.getElementById(div);
	
	if ( result.hasChildNodes() )
	{
		while ( result.childNodes.length >= 1 )
		{
			result.removeChild( result.firstChild );       
		} 
	}	
}

var basketAntallTOT;
var posCounter;

function addToBasket( counter )
{
	basketAntallTOT = 0;
	posCounter = counter;
	
	myFunction = 'addToBasket';
	
	var nodeIDS 	= document.getElementById( 'nodeids' + counter ).value;
	
	var is_serial 	= document.getElementById( 'is_serial' + counter ).value;
	
	if ( is_serial == 1 )
		var serialIDS 	= document.getElementById( 'serialids' + counter ).value;
	
	splitted = nodeIDS.split( ";" );
	var orderStr = '';
	var ok = 0;
	
	for (var i = 0; i < splitted.length-1; i++)
	{
		nodeID = splitted[i];
		
		antall = document.getElementById( 'antbox_' + nodeID ).value;

		if ( isInteger( antall ) )
		{
			// OK! Samle opp alle som er OK og legg i basket
			//alert( nodeID + " - - - " + antall );
			orderStr = orderStr + nodeID + ",," + antall + ";;";
			
			basketAntallTOT = basketAntallTOT + parseInt( antall );
			
			ok = 1;
		}
	}

	if ( is_serial == 1 )
	{
		splittedtwo = serialIDS.split( ";" );
	
		for (var j = 0; j < splittedtwo.length-1; j++)
		{
			nodeID = splittedtwo[j];
			antall = document.getElementById( 'antbox_' + nodeID ).value;
	
			if ( isInteger( antall ) )
			{
				orderStr = orderStr + nodeID + ",," + antall + ";;";
				
				basketAntallTOT = basketAntallTOT + parseInt( antall );
				
				ok = 1;
			}
		}
	}
	
	if ( ok == 1 )
	{
		//alert( orderStr );
		var poststr = "orderStr=" + encodeURI( orderStr );
		postRequest( getSiteUrl() + "layout/set/ajax/vztlibrary/addtobasket/", poststr);
	}
	else
		alert( "Tast inn et gyldig antall" );
	
}

function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
	 var c = s.charAt(i);

	 if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function showDialog()
{
	/*document.getElementById( 'tot' ).innerHTML = basketAntall;*/
	document.getElementById( 'totant' ).innerHTML = parseInt( basketAntallTOT );
	
	var oldnumitems = document.getElementById( 'numitems' ).innerHTML;
	var newsumitems = parseInt( oldnumitems ) + parseInt( basketAntallTOT );
	document.getElementById( 'numitems' ).innerHTML = newsumitems;

	if ( parseInt( basketAntallTOT ) == 1 )
	{
			
	}
	else
		document.getElementById( 'flertall' ).innerHTML = "er" ;
	
		
	
	posLink = document.getElementById( 'link' + posCounter );

	var x = findPosX( posLink );
	var y = findPosY( posLink );
	
	newY = y-50;
	newX = x-50;
	
	var layer = document.getElementById( 'addToBasketLayer' );

	layer.style.left = newX + "px";
	layer.style.top = newY + "px";
	showHide( 'addToBasketLayer' );
	vzttimeout = setTimeout(fadeOut, 2000);

}

function setOpacity(level) 
{
	element = document.getElementById( 'addToBasketLayer' );

	if ( level < 0.1 )
	{
		clearTimeout(vzttimeout);
		element.style.display = 'none';
		newOpac();
	}
	else
	{
		element.style.opacity = level;
		element.style.MozOpacity = level;
		element.style.KhtmlOpacity = level;
		element.style.filter = "alpha(opacity=" + (level * 100) + ");";
	}
}


function fadeOut() {
	for (i = 0; i <= 1; i += (1 / 20)) {
		setTimeout("setOpacity(" + (1 - i) + ")", i * 1000);
	}
}

function newOpac()
{
	element = document.getElementById( 'addToBasketLayer' );
	element.style.opacity = 1;
	element.style.MozOpacity = 1;
	element.style.KhtmlOpacity = 1;
	element.style.filter = "alpha(opacity=100);";		
}

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 switchfane( num )
{
	if ( num == 1 )
	{
		document.getElementById( 'avegg' ).className = 'selected';
		document.getElementById( 'agulv' ).className = '';
		document.getElementById( 'vegg' ).style.display = 'inline';
		document.getElementById( 'gulv' ).style.display = 'none';
	}
	else
	{
		document.getElementById( 'avegg' ).className = '';
		document.getElementById( 'agulv' ).className = 'selected';
		document.getElementById( 'vegg' ).style.display = 'none';
		document.getElementById( 'gulv' ).style.display = 'inline';
	}
}

function clickImage( num )
{
	var swapimg = document.getElementById( 'swapimage' + num );
	var child = swapimg.getElementsByTagName('img');
	
	var src;
	var newsrc;

	for (var i = 0; i < child.length; i++)
	{
		src = child[i].src;
		if ( src.search( '_medium' ) != -1 )
			newsrc = src.replace('_medium', '');
		else
			newsrc = src.replace('.jpg', '_medium.jpg');

		child[i].src = newsrc;

		break;
	}
}


function showGroup( counter )
{
	
	//document.getElementById( 'subitems' + counter ).style.visibility = 'visible';	
	
	document.getElementById( 'itemgroupleft' + counter ).className = 'itemgroupleft selectedgroup';
	document.getElementById( 'itemgroupright' + counter ).className = 'itemgroupright selectedgroupright';
}

function hideGroup( counter )
{
	//document.getElementById( 'subitems' + counter ).style.visibility = 'hidden';	
	document.getElementById( 'itemgroupleft' + counter ).className = 'itemgroupleft';
	document.getElementById( 'itemgroupright' + counter ).className = 'itemgroupright';
}






/* KALKIS */

var cform = null;
var ono1  = null;
var ono2  = null;
var ono3  = null;
var adjustWin = false;
var w0 = null;
var w1 = null;
var w2 = null;

function setCookie(cName, cValue, cExpInDays) {
	var cString = cName + "=" + escape(cValue);
	if (cExpInDays) {
		var expire_date = new Date();
		var ms_from_now = cExpInDays * 24 * 60 * 60 * 1000;
		expire_date.setTime(expire_date.getTime() + ms_from_now);
		var expire_string = expire_date.toGMTString();
		cString += "; expires=" + expire_string;
	}
	document.cookie = cString;
}
function getCookie(cName) {
    var dc = document.cookie;
    var fnd =0;
    var cookieList = new Array();
    var aCookie = new Array();
    if (dc) {
        cookieList = dc.split(";");
        fnd = cookieList.length;
    }
    for (var i = 0; i<fnd; i++) {
        aCookie = cookieList[i].split("=");
        if (aCookie.length==2) {
            var temp = aCookie[0].replace(" ","")
            if (temp==cName) {
                return unescape(aCookie[1]);
            }
        }
    }
    return null;
}
function doToggle() {
	if ((adjustWin && !document.getElementById('togglewin').checked) || (!adjustWin && document.getElementById('togglewin').checked)) {
		/*
		adjustWin = document.getElementById('togglewin').checked;
		
		if (adjustWin) {
			w1.className='cborder';
			w2.className='cborder';
		} else {
			w1.className='clsHide';
			w2.className='clsHide';
		}
		*/

		doCalc();
	}
}

function initVal() {
        cform = document.getElementById("calculator");

        ono1 = document.getElementById("cfi_o1n");

        ono2 = document.getElementById("cfi_o2n");

        ono3 = document.getElementById("cfi_o3n");

        ono4 = document.getElementById("cfi_o4n");

        w0 = document.getElementById("sectionwindow0");

        w1 = document.getElementById("sectionwindow1");

        w2 = document.getElementById("sectionwindow2");
	// tapestry values
	
	
	//cform.cfi_tw.value=twdef;
	//cform.cfi_th.value=thdef;
	//cform.cfi_tr.value=trdef;
	
	document.getElementById('cfi_tw').value = twdef;
	document.getElementById('cfi_th').value = thdef;
	document.getElementById('cfi_tr').value = trdef;
	
	
	// user values from previous session
	var ck = getCookie("tpmxrww");
	if (ck!=null) { cform.cfi_ww.value=ck;}
	ck = getCookie("tpmxrwh");
	if (ck!=null) { cform.cfi_wh.value=ck;}
	ck = getCookie("tpmxro1w");
	if (ck!=null) { cform.cfi_o1w.value=ck;}
	ck = getCookie("tpmxro1h");
	if (ck!=null) { cform.cfi_o1h.value=ck;}
	ck = getCookie("tpmxro2w");
	if (ck!=null) { cform.cfi_o2w.value=ck;}
	ck = getCookie("tpmxro2h");
	if (ck!=null) { cform.cfi_o2h.value=ck;}
	ck = getCookie("tpmxro3w");
	if (ck!=null) { cform.cfi_o3w.value=ck;}
	ck = getCookie("tpmxro3h");
	if (ck!=null) { cform.cfi_o3h.value=ck;}
	ck = getCookie("tpmxro4w");
	if (ck!=null) { cform.cfi_o4w.value=ck;}
	ck = getCookie("tpmxro4h");
	if (ck!=null) { cform.cfi_o4h.value=ck;}

	ck = getCookie("tpmxro1n");
	if (ck!=null) { ono1.options[ck].selected=true;}
	ck = getCookie("tpmxro2n");
	if (ck!=null) { ono2.options[ck].selected=true;}
	ck = getCookie("tpmxro3n");
	if (ck!=null) { ono3.options[ck].selected=true;}
	ck = getCookie("tpmxro4n");
	if (ck!=null) { ono4.options[ck].selected=true;}

	doCalc();
}
function doCalc() {
	//if ((adjustWin && document.getElementById('togglewin').checked) || (!adjustWin && document.getElementById('togglewin').checked)) {
		
		adjustWin = document.getElementById('togglewin').checked;
		if (adjustWin) {
			document.getElementById('sectionwindow1').className='cborder';
			document.getElementById('sectionwindow2').className='cborder';
		} else {
			document.getElementById('sectionwindow1').className='clsHide';
			document.getElementById('sectionwindow2').className='clsHide';
		}
	//}
	var th = parseFloat(document.getElementById('cfi_th').value.replace(/,/,'.'));th = (isNaN(th)||th <3.0) ? thdef : th; document.getElementById('cfi_th').value=th;
	var tw = parseInt(document.getElementById('cfi_tw').value,10);tw = (isNaN(tw)||tw <20) ? twdef : tw; document.getElementById('cfi_tw').value=tw;
	tw=0.0+tw/100;
	var tr = parseInt(document.getElementById('cfi_tr').value,10);tr = (isNaN (tr)||tr<1) ? trdef : tr; document.getElementById('cfi_tr').value=tr;
	tr=0.0+tr/100;
	var ts = parseInt(document.getElementById('cfi_ts').value,10);ts = (isNaN (ts)||ts<0) ? 2 : ts; document.getElementById('cfi_ts').value=ts;
	ts=0.0+ts/100;
	var ww = parseFloat(document.getElementById('cfi_ww').value.replace(/,/,'.'));ww = isNaN (ww) ? 1.0 : ww; ww = ww<1.0 ? 1.0 : ww; document.getElementById('cfi_ww').value=ww;
	var wh = parseFloat(document.getElementById('cfi_wh').value.replace(/,/,'.'));wh = isNaN (wh) ? 1.0 : wh; wh = wh<1.0 ? 1.0 : wh; document.getElementById('cfi_wh').value=wh;
	var o1w = parseFloat(document.getElementById('cfi_o1w').value.replace(/,/,'.'));o1w = isNaN (o1w) ? 0.0 : o1w; document.getElementById('cfi_o1w').value=o1w;
	var o1h = parseFloat(document.getElementById('cfi_o1h').value.replace(/,/,'.'));o1h = isNaN (o1h) ? 0.0 : o1h; document.getElementById('cfi_o1h').value=o1h;
	var o2w = parseFloat(document.getElementById('cfi_o2w').value.replace(/,/,'.'));o2w = isNaN (o2w) ? 0.0 : o2w; document.getElementById('cfi_o2w').value=o2w;
	var o2h = parseFloat(document.getElementById('cfi_o2h').value.replace(/,/,'.'));o2h = isNaN (o2h) ? 0.0 : o2h; document.getElementById('cfi_o2h').value=o2h;
	var o3w = parseFloat(document.getElementById('cfi_o3w').value.replace(/,/,'.'));o3w = isNaN (o3w) ? 0.0 : o3w; document.getElementById('cfi_o3w').value=o3w;
	var o3h = parseFloat(document.getElementById('cfi_o3h').value.replace(/,/,'.'));o3h = isNaN (o3h) ? 0.0 : o3h; document.getElementById('cfi_o3h').value=o3h;
	var o4w = parseFloat(document.getElementById('cfi_o4w').value.replace(/,/,'.'));o4w = isNaN (o4w) ? 0.0 : o4w; document.getElementById('cfi_o4w').value=o4w;
	var o4h = parseFloat(document.getElementById('cfi_o4h').value.replace(/,/,'.'));o4h = isNaN (o4h) ? 0.0 : o4h; document.getElementById('cfi_o4h').value=o4h;
	setCookie("tpmxrww",ww,30,false,false,false);
	setCookie("tpmxrwh",wh,30,false,false,false);
	setCookie("tpmxro1w",o1w,30,false,false,false);
	setCookie("tpmxro1h",o1h,30,false,false,false);
	setCookie("tpmxro2w",o2w,30,false,false,false);
	setCookie("tpmxro2h",o2h,30,false,false,false);
	setCookie("tpmxro3w",o3w,30,false,false,false);
	setCookie("tpmxro3h",o3h,30,false,false,false);
	setCookie("tpmxro4w",o4w,30,false,false,false);
	setCookie("tpmxro4h",o4h,30,false,false,false);
	
	
	var o1n = 0;
	var o2n = 0;
	var o3n = 0;
	var o4n = 0;
	
	for (var j=0;j<document.getElementById('cfi_o1n').length;j++) {
		if (document.getElementById('cfi_o1n').options[j].selected) {
			o1n=j;break;
		}
	}
	for (var j=0;j<document.getElementById('cfi_o2n').length;j++) {
		if (document.getElementById('cfi_o2n').options[j].selected) {
			o2n=j;break;
		}
	}
	for (var j=0;j<document.getElementById('cfi_o3n').length;j++) {
		if (document.getElementById('cfi_o3n').options[j].selected) {
			o3n=j;break;
		}
	}
	for (var j=0;j<document.getElementById('cfi_o4n').length;j++) {
		if (document.getElementById('cfi_o4n').options[j].selected) {
			o4n=j;break;
		}
	}
	setCookie("tpmxro1n",o1n,30,false,false,false);
	setCookie("tpmxro2n",o2n,30,false,false,false);
	setCookie("tpmxro3n",o3n,30,false,false,false);
	setCookie("tpmxro4n",o4n,30,false,false,false);
	
	var noOfLengths= Math.ceil(ww/tw);
	// wh is never 0
	var LengthsPerRoll = Math.floor(th/(wh+tr+ts));
	
	
	
	var restHeight = th - LengthsPerRoll*(wh+tr+ts);
	var txt = "";
	if (LengthsPerRoll>0) {
		if (adjustWin) {
			if (tr>0) {
				o1h = o2h = 0;
				o3h = tr * Math.floor(o3h/tr);
				o3h = o3h+restHeight>wh ? 1 : 0;
				o4h = tr * Math.floor(o4h/tr);
				o4h = o4h+restHeight>wh ? 1 : 0;
			} else {
				o1h = o1h+restHeight>wh ? 1 : 0;
				o2h = o2h+restHeight>wh ? 1 : 0;
				o3h = o3h+restHeight>wh ? 1 : 0;
				o4h = o4h+restHeight>wh ? 1 : 0;
			}
		} else {
			o1h = o2h = o3h = oh4 = 0;
		}
		// extra heights - windows
		var openingWn = Math.floor(o1w/tw);
		var openinggross = openingWn*o1h*o1n;
		var openingnet = ((openingWn-1)<1 ? 0 : openingWn-1)*o1h*o1n;
		openingWn = Math.floor(o2w/tw);
		openinggross+= openingWn*o2h*o2n;
		openingnet+= ((openingWn-1)<1 ? 0 : openingWn-1)*o2h*o2n;
		// doors
		openingWn = Math.floor(o3w/tw);
		openinggross+= openingWn*o3h*o3n;
		openingnet+= ((openingWn-1)<1 ? 0 : openingWn-1)*o3h*o3n;
		openingWn = Math.floor(o4w/tw);
		openinggross+= openingWn*o4h*o4n;
		openingnet+= ((openingWn-1)<1 ? 0 : openingWn-1)*o4h*o4n;


		var noOfRollsGross = (noOfLengths-openingnet)/LengthsPerRoll;
		var noOfRollsNet = (noOfLengths-openinggross)/LengthsPerRoll;

		var displaydec = (Math.ceil(noOfRollsGross)==noOfRollsGross) ? ".0" : "";
		
		if (tr>0) {
			txt = "M\u00f8nstret tapet med "+tr*100+ " cm repetisjon: "+(noOfLengths-openingnet)+ " lengder";
			if (openingnet>0) {
				txt+= " (etter at "+openingnet+" lengder er trukket fra)";
			} else if (adjustWin) {
				txt+= " (\u00e5pningene er ikke store nok til \u00e5 gi noen innsparing)";
			}
			txt+= ".\nDu trenger "+ (Math.ceil(noOfRollsGross*100)/100) +displaydec+ " = " + (Math.ceil(noOfRollsGross)) + " ruller.";
		} else {
			txt = "Tapet uten m\u00f8nster: "+(noOfLengths-openingnet)+ " lengder";
			if (openingnet>0) {
				txt+= " (etter at "+openingnet+" lengder er trukket fra)";
			} else if (adjustWin) {
				txt+= " (\u00e5pningene er ikke store nok til \u00e5 gi noen innsparing)";
			}
			txt+= ".\nDu trenger "+ (Math.ceil(noOfRollsGross*100)/100) +displaydec+ " = " + (Math.ceil(noOfRollsGross)) + " ruller.";
			if (noOfRollsGross>(noOfRollsNet+0.05)) {
				txt+= "\nHvis tapeten kan plasseres slik at den alltid treffer midt i \u00e5pningen trenger du "+ (Math.ceil(noOfRollsNet*100)/100) + " = " + (Math.ceil(noOfRollsNet)) + " ruller.";
			}
		}
	} else {
		txt = "Summen av veggh\u00f8yde, rapport og skj\u00e6ringsmonn er st\u00f8rre enn lengden av rullen!";
	}
	document.getElementById('cfo_txt').value = txt;
}

