// General functions

popup = function(header, page) {
	draw.box('500', '800', header, 'custom_'+page, '');
}

drawSitemap = function(obj) {
	draw.box('500', '800', 'Sidekart', 'sitemap', '');
}

doPageSearch = function(obj) {
	var searchText = encodeURIComponent(document.getElementById('pageSearch').value);
	
	draw.box('500', '800', 'Søk..', '$_pageSearch', searchText);
}

doNewPageSearch = function(obj) {
	
	document.getElementById('search_preloader').style.display = 'block';
	
	var searchText = encodeURIComponent(document.getElementById('new_pageSearch').value);
	
	//preload(true);
	var url = 'request.php';
	var parameters = 'action=$_pageSearch&optionalParams='+searchText+'&newSearch=true';
		
	var httpRequest = GetXmlHttpObject();
		
	//Ready
	httpRequest.onreadystatechange = function() { 
		
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
					
				document.getElementById('newpageSearchWrapper').innerHTML = httpRequest.responseText;
			}
			
			else {
				document.getElementById('newpageSearchWrapper').innerHTML = 'En feil har oppstått, vennligst prøv igjen senere.';
			}
			document.getElementById('search_preloader').style.display = 'none';
		}
	}
		
	makePostRequest(url, parameters, httpRequest);
	
	
	//alert(searchText);
}


zoom = function() {
	
	return { image : function(imagePath) {
		
		var validId = imagePath.replace(/[^0-9A-Za-z]/g, '');
	
		if (!document.getElementById('imageContainer_'+validId)) {
			var scrollHeight = getScrollHeight();
	
			var image = new Image();
			image.src = imagePath;
			var height = image.height;
			var width = image.width;
			image = null;
			
			var divLock = document.createElement('DIV');
			divLock.id = 'divLock_'+validId;
			
			with (divLock.style) {
				position = 'fixed';
				height = '100%';
				width = '100%';
				background = 'black';
				top = '0';
				left = '0';
				zIndex = '1000';
				opacity = '0';
				MozOpacity = '0';
				KhtmlOpacity = '0';
				filter = 'alpha(opacity=0)';
			}
			
			var imageContainer = document.createElement('DIV');
			imageContainer.id = 'imageContainer_'+validId;
			
			with (imageContainer.style) {
				position = 'absolute';
				height = '100%';
				width = '100%';
				top = '0';
				left = '0';
				zIndex = '1001';
			}
			
			var imageWrapper = document.createElement('DIV');
			
			with (imageWrapper.style) {
				border = '1px #CCCCCC solid';
				marginLeft = 'auto';
				marginRight = 'auto';
				marginTop = (scrollHeight+50)+'px';
				height = '1px';
				width = '10px';
				textAlign = 'left';
				backgroundColor = 'white';
				overflow = 'hidden';
				padding = '10px';
			}
	
			imageWrapper.onclick = function(i) { return function() { zoom.image(i); } } (imagePath);
			
			document.body.appendChild(divLock);
			document.body.appendChild(imageContainer);
			imageContainer.appendChild(imageWrapper);
			imageWrapper.innerHTML = '<img src="'+imagePath+'" alt="Lukk" />';
			
			expandHeight(imageWrapper, height);
			setTimeout(function(o, w) { return function() { expandWidth(o, w); } } (imageWrapper, width), 200);
		
		} else {
			
			var divLock = document.getElementById('divLock_'+validId);
			divLock.parentNode.removeChild(divLock);
			
			var imageContainer = document.getElementById('imageContainer_'+validId);
			imageContainer.parentNode.removeChild(imageContainer);
		}
	}
} } ();

				
				
				
draw = function() {
	
	return { box : function(height, width, headerText, id, optionalParams) {
	
	responseId = id;
	
	if (!document.getElementById(id)) {
		
		var headerTextDiv = document.createElement('DIV');
		with (headerTextDiv.style) {
			position = 'absolute';
			cssFloat = 'left';
			styleFloat = 'left';
			marginTop = '5px';
			marginLeft = '5px';
			fontWeight = 'bold';
			fontSize = '15px';
		}
		
		var div = document.createElement('DIV');
		var divWrapper = document.createElement('DIV');
		
		var divBg = document.createElement('DIV');
		
		with (divBg.style) {
			position = 'fixed';
			height = '100%';
			width = '100%';
			background = 'black';
			top = '0';
			left = '0';
			zIndex = '1000';
			color = 'black';
			opacity = '0';
			MozOpacity = '0';
			KhtmlOpacity = '0';
			filter = 'alpha(opacity=0)';
		}
		
		divBg.id = 'bg_'+id;
		
		with (divWrapper.style) {
			position = 'fixed';
			height = '100%';
			width = '100%';
			top = '0';
			left = '0';
			zIndex = '1001';
		}
		
		divWrapper.id = id;
		

		div.className = 'windowPopupBody';
		
		div.id = 'boxWrapper_'+id;
		
		divHeader = document.createElement('DIV');
		
		divHeader.className = 'windowPopupHead';
		
		
		divBody = document.createElement('DIV');
		divBody.id = id+'Body';
		var bodyHeight = height - 25;
		with (divBody.style) {
			overflowX = 'hidden';
			overflowY = 'auto';
			paddingLeft = '5px';
			paddingRight = '5px';
			height = bodyHeight+'px';
		}

		
		closeBox = document.createElement('DIV');
		with (closeBox.style) {
			background = "url('shared/commonFiles/box/boxClose.png') no-repeat";
			height = '16px';
			width = '29px';
			cssFloat = 'right';
			styleFloat = 'right';
			marginTop = '5px';
			marginRight = '5px';
		}
		
		closeBox.onclick = function(h, w, ht, i) { return function() { draw.box(h, w, ht, i); } } (height, width, headerText, id);
		
		document.body.appendChild(divBg);
		document.body.appendChild(divWrapper);
		
		divWrapper.appendChild(div);
		div.appendChild(divHeader);
		
		divHeader.appendChild(headerTextDiv);
		headerTextDiv.innerHTML = headerText;
		
		div.appendChild(divBody);
		
		divHeader.appendChild(closeBox);
		
		var clearBox = document.createElement('DIV');
		clearBox.style.clear = 'both';
		divHeader.appendChild(clearBox);

		//addOpacity(div, '100', '0', 100);
		expandHeight(div, height);
		setTimeout(function(o, w) { return function() { expandWidth(o, w); } } (div, width), 200);
		//expandWidth(div, 500);
		
		divBody.innerHTML = '<div id="popup_preloader" style="position:absolute"><img src="shared/xmlhttp/preloader.gif" alt="" /></div>';
		
		//preload(true);
		var url = 'request.php';
		var parameters = 'action='+id+'&optionalParams='+optionalParams;
		
		var httpRequest = GetXmlHttpObject();
		
		//Ready
		httpRequest.onreadystatechange = function() { 
		
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					document.getElementById(responseId+'Body').innerHTML = httpRequest.responseText;
				}
			
				else {
					document.getElementById(responseId+'Body').innerHTML = 'En feil har oppstått';
				}
				preload(false, 'popup_preloader');
			}
		}
		
		makePostRequest(url, parameters, httpRequest);
	} else {

		var element = document.getElementById(id);
  		element.parentNode.removeChild(element);
		
		var bg = document.getElementById('bg_'+id);
  		bg.parentNode.removeChild(bg);

	}
}
} } ();

hideBox = function(id) {
	var element = document.getElementById(id);
  	element.parentNode.removeChild(element);
		
	var bg = document.getElementById('bg_'+id);
  	bg.parentNode.removeChild(bg);
}

function preload(condition, id) {
	if (document.getElementById(id)) {
		var preload = document.getElementById(id);
		if (condition == false) {
			preload.style.display = 'none';
		} else {
			preload.style.display = 'block';
		}
	}
}
		
		


function makePostRequest(url, parameters, httpRequest) {
	
	httpRequest.open("POST", url, true);
	httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	httpRequest.send(parameters);		
}


expandHeight = function(obj, height) {
	var currentHeight = +obj.style.height.replace('px','');

	obj.style.height = Math.ceil(currentHeight + ((height - currentHeight) / 2)) + 'px';

	if (currentHeight < height) {
		expandHeightTimeout = setTimeout(function(o, h) { return function() { expandHeight(o, h); } }(obj, height),30);
	}
	
	else {
		return true;
	}
}

expandWidth = function(obj, width) {
	var currentWidth = +obj.style.width.replace('px','');

	obj.style.width = Math.ceil(currentWidth + ((width - currentWidth) / 2)) + 'px';

	if (currentWidth < width) {
		expandWidthTimeout = setTimeout(function(o, w) { return function() { expandWidth(o, w); } }(obj, width),30);
	}
	
	else {
		return true;
	}
}

addOpacity = function(obj, maxOpacity, startOpacity, time) {

	var pr = startOpacity / 100;
	
	with (obj.style) {
		opacity = pr;
		MozOpacity = pr;
		KhtmlOpacity = pr;
		filter = 'alpha(opacity=' + startOpacity + ')';
	}
	
	var startOpacity = Math.ceil(startOpacity + 10);  //Math.ceil(startOpacity + (maxOpacity - startOpacity) / 2);

	if (startOpacity < maxOpacity) {
		addOpacityTimeout = setTimeout(function(o, mo, s, t) { return function() { addOpacity(o, mo, s, t); } }(obj, maxOpacity, startOpacity, time),time);
	}
	
	else {
		with (obj.style) {
			opacity = '10';
			MozOpacity = '10';
			KhtmlOpacity = '10';
			filter = 'alpha(opacity=100)';
		}
	}
}


shrinkWidth = function(id, newWidth) {
	if (document.getElementById(id)) {
	
		var obj = document.getElementById(id);
		var currentWidth = +obj.style.width.replace('px','');
		obj.style.width =  Math.ceil(currentWidth / 2) + 'px';

		if (currentWidth > newWidth) {
    		shrinkWidthTimeout = setTimeout(function(i, w) { return function() { shrinkWidth(i, w); } }(id, newWidth),30);
		}
	}
}

shrinkHeight = function(id, newHeight) {
	if (document.getElementById(id)) {
		var obj = document.getElementById(id);
		var currentHeight = +obj.style.height.replace('px','');
		obj.style.height =  Math.ceil(currentHeight / 2) + 'px';

		if (currentHeight > newHeight) {
    		shrinkHeightTimeout = setTimeout(function(i, h) { return function() { shrinkHeight(i, h); } }(id, newHeight),30);
		}
	}
}



printThis = function() {
	window.print();
}


formatNumber = function(num,dec,thou,pnt,curr1,curr2,n1,n2) { 

	var x = Math.round(num * Math.pow(10,dec)); 
	if (x >= 0) n1=n2=''; 
  
	var y = (''+Math.abs(x)).split(''); 
	var z = y.length - dec; 
  
	if (z<0) z--; 
  
	for(var i = z; i < 0; i++)  
	y.unshift('0'); 
  
	y.splice(z, 0, pnt); 
	if(y[0] == pnt) y.unshift('0'); 
 
	while (z > 3) { 
		z-=3; 
		y.splice(z,0,thou); 
	}  
  		
	var r = curr1+n1+y.join('')+n2+curr2; 
	var d = r.slice(-3);
	if (d == ',00') return r.replace(',00',',-');
	
	else return r;
}

	
	
	function fadeIn(obj) {
		colorFade(obj,'color', '000000', 'A70000', 5, 40);
	}

	function fadeOut(obj) {
		colorFade(obj,'color', 'A70000', '000000', 5, 40);
	}

	function fadeInSub(obj) {
		colorFade(obj,'color', '000000', 'A70000', 5, 40);
	}

	function fadeOutSub(obj) {
		colorFade(obj,'color', 'A70000', '000000', 5, 40);
	}
	
	function clearField(obj, text) {
		if (obj.value == text) {
			obj.value = '';
		}
	}
	
	function setField(obj, text) {
		if (obj.value == '') {
			obj.value = text;
		}
	}
	
function getBrowserHeight() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myHeight = document.body.clientHeight;
	}
	
	return myHeight;
}

function getBrowserWidth() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	window.alert( 'Width = ' + myWidth );
	window.alert( 'Height = ' + myHeight );
}

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}


function evalForm() {
	var table = document.getElementById('formTable');
	var inputElms = table.getElementsByTagName('input');
	var textareaElms = table.getElementsByTagName('textarea');
	
	for (var i=0; i<inputElms.length; i++) {
		if (inputElms[i].value == '') {
			var ctn = false;
		}
	}
	
	for (var i=0; i<textareaElms.length; i++) {
		if (textareaElms[i].value == '') {
			var ctn = false;
		}
	}
	
	if (ctn != false) {
		alert('Din forespørsel er sendt!');
		return true;
	}
	else {
		alert('Du må fylle ut alle felt');
		return false;
	}
}


fadeOutObject = function(obj, steps) {
	
	fadeProgress = true;
	
	var pr = steps/100;

	with (obj.style) {
        opacity = pr;
        MozOpacity = pr;
        KhtmlOpacity = pr;
        filter = 'alpha(opacity=' + steps + ')';
	}
	
	newOpa = steps - 10;
	
	if (newOpa > 0) {
		setTimeout(function(o, s) { return function() { fadeOutObject(o, s) } } (obj, newOpa), 30);
	}
	
	else {
		with (obj.style) {
			opacity = '0';
			MozOpacity = '0';
			KhtmlOpacity = '0';
			filter = 'alpha(opacity=0)';
			display = 'none';
		}
		fadeProgress = false;
	}
}

function toggleGroup(id, obj) {
	
	if (document.getElementById('sub_'+id)) {
		//alert('blaat');
		var wrapper = document.getElementById('sub_'+id);
		
		//alert(wrapper.className);
		if (wrapper.className == 'productGroupActive') {
			wrapper.className = 'productGroupInactive';
			obj.className = 'productGroup';
		}
			
		else {
			wrapper.className = 'productGroupActive';
			obj.className = 'productGroup productGroupActive productGroupOpen';
		}
		return true;
	}
}
