function openWindow(url,width,height) {
	var availheight=((screen.availHeight-height)/2);
	var availwidth=((screen.availWidth-width)/2);
	winStats='resizable=yes,toolbar=no,status=no,location=no,directories=no,menubar=no,';
	winStats+='scrollbars=yes,width='+width+',height='+height;
	if (navigator.appName.indexOf("Microsoft")>=0) {
		winStats+=',left='+availwidth+',top='+availheight;
	}else{
		winStats+=',screenX='+availwidth+',screenY='+availheight;
	}
	floater=window.open(url,"",winStats)
}
function termSearch(formName,letter) {
	document.forms[formName].searchText.value = letter+'*';
	document.forms[formName].submit();
}

function showHide(elementID) {
	var target = document.getElementById(elementID);
	if (target.style.visibility == 'hidden') {
		target.style.visibility = 'visible';
	}else if (target.style.visibility == '') {
		target.style.visibility = 'visible';
	}else{
		target.style.visibility = 'hidden';
	}
}

var httpRequest;
//httpRequest = getHTTPObject();

function getHTTPObject(){
  var x=null; 
  try{
    x=new ActiveXObject("Msxml2.XMLHTTP")
  }catch(e){
    try{    
      x=new ActiveXObject("Microsoft.XMLHTTP")
    } catch(oc){
      x=null  
    }
  }
  if(!x && typeof XMLHttpRequest != "undefined") {
    x=new XMLHttpRequest()
  }
  return x;

}

function ajaxManager() {
	var args = ajaxManager.arguments;
	//var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP"): new XMLHttpRequest();
	
	httpRequest = getHTTPObject();
	
	switch (args[0]) {
		case "load_page":
			if (httpRequest) {
				httpRequest.onreadystatechange = function(){
				
					motherElement = document.getElementById(args[3]);
					
					if (httpRequest.readyState == 4 && httpRequest.status == 200){
						// The response is complete; you can get the server's response and use it.
					  
					  //motherElement.style.backgroundColor="#F2ECD6";
						motherElement.style.visibility = 'visible';
					
						element = document.getElementById(args[2]);
						element.innerHTML = httpRequest.responseText;
					
						// update totalPrice
						shoppingCartSum.innerHTML = document.getElementById('totalprice').value;
					
					} else if (httpRequest.readyState == 2 || httpRequest.readyState == 3){
						//if 2 - The request was sent and is in process (you can usually get content headers from the response at this point).
						//if 3 - The request is in process; often some partial data is available from the response, but the server isn't finished with its response.
						
						//motherElement.style.backgroundColor="#B32C07";
						//motherElement.style.visibility = 'visible';
            
						element = document.getElementById(args[2]);
						element.innerHTML = 'Loading...';
          }
				}
				httpRequest.open("GET", args[1], true);
				httpRequest.send(null);
			}
			break;
		case "preload":
			ajaxManager('load_js', 'scripts/glide.js');
			break;
    case "evaluate":
 			if (httpRequest) {
				httpRequest.onreadystatechange = function(){
					if (httpRequest.readyState == 4 && httpRequest.status == 200){
        		
        		//callbackFunction
        		eval(args[2]);
        		
 					}
				}
				httpRequest.open("GET", args[1], true);
				httpRequest.send(null);
			}
      break;			
    case "submitForm":
      querystring = '';
      
      for (var i = 0; i<document.forms[args[4]].elements.length; i++) {
		  	if ((document.forms[args[4]].elements[i].name.indexOf('product') > -1)) {
		  		fieldValue = document.forms[args[4]].elements[i].value;

		  		if(fieldValue > 0){
		  	  	querystring+= "&" + document.forms[args[4]].elements[i].name + "=" + fieldValue;
		  	  }
		  	  document.forms[args[4]].elements[i].value = 0;	
		    }
		    
	    }	    
	    requestUrl = args[1] + querystring;
	    

			if (httpRequest) {
				httpRequest.onreadystatechange = function(){
				
					motherElement = document.getElementById(args[3]);


				  					
					if (httpRequest.readyState == 4 && httpRequest.status == 200){
						// The response is complete.
					  
	    			resetQuantity(args[4]);
	    			
						motherElement.style.visibility = 'visible';
					
						element = document.getElementById(args[2]);
						element.innerHTML = httpRequest.responseText;
					
						// update totalPrice
						shoppingCartSum.innerHTML = document.getElementById('totalprice').value;
						
					} else if (httpRequest.readyState == 2 || httpRequest.readyState == 3){
						//if 2 - The request was sent and is in process (you can usually get content headers from the response at this point).
						//if 3 - The request is in process; often some partial data is available from the response, but the server isn't finished with its response.
						
						element = document.getElementById(args[2]);
						element.innerHTML = 'Loading...';
          }
				}
				httpRequest.open("GET", requestUrl, true);
				httpRequest.send(null);
			}
      break;
	}


	
}

function resetQuantity(formName){
	// reset quantities
	for (var i = 0; i<document.forms[formName].elements.length; i++) {
		if ((document.forms[formName].elements[i].name.indexOf('product') > -1)) {
			fieldValue = document.forms[formName].elements[i].value;
			
			document.forms[formName].elements[i].value = 0;	
		}
		    
	}	
}

/**
* Shows total Price (Response as html, could be also xml).
**/
function getTotalPrice(){
  //response = httpRequest.responseXML.documentElement;
  //totalPrice = response.getElementsByTagName('totalPrice')[0].firstChild.data;
  //document.getElementById['shoppingCartSum'].innerHTML = httpRequest.responseText;  
  shoppingCartSum.innerHTML = httpRequest.responseText;
  
}	
	
function changeImg(imgid) {
	litteratur = document.getElementById('img_litteratur');
	litteratur_off = document.getElementById('img_litteratur_off');
	fakta = document.getElementById('img_fakta');
	fakta_off = document.getElementById('img_fakta_off');
	barnogungdom = document.getElementById('img_barnogungdom');
	barnogungdom_off = document.getElementById('img_barnogungdom_off');
	reise = document.getElementById('img_reise');
	reise_off = document.getElementById('img_reise_off');
	barnehage = document.getElementById('img_barnehage');
	barnehage_off = document.getElementById('img_barnehage_off');
	grunnskole = document.getElementById('img_grunnskole');
	grunnskole_off = document.getElementById('img_grunnskole_off');
	videregaaende = document.getElementById('img_videregaaende');
	videregaaende_off = document.getElementById('img_videregaaende_off');
	/* forfattere = document.getElementById('img_forfattere');
	forfattere_off = document.getElementById('img_forfattere_off'); */
	
	if (imgid != 'img_litteratur') {
		litteratur.style.zIndex = '1';
		litteratur_off.style.zIndex = '2';
	}
	if (imgid != 'img_fakta') {
		fakta.style.zIndex = '1';
		fakta_off.style.zIndex = '2';
	}
	if (imgid != 'img_barnogungdom') {
		barnogungdom.style.zIndex = '1';
		barnogungdom_off.style.zIndex = '2';
	}
	if (imgid != 'img_reise') {
		reise.style.zIndex = '1';
		reise_off.style.zIndex = '2';
	}
	if (imgid != 'img_barnehage') {
		barnehage.style.zIndex = '1';
		barnehage_off.style.zIndex = '2';
	}
	if (imgid != 'img_grunnskole') {
		grunnskole.style.zIndex = '1';
		grunnskole_off.style.zIndex = '2';
	}
	if (imgid != 'img_videregaaende') {
		videregaaende.style.zIndex = '1';
		videregaaende_off.style.zIndex = '2';
	}
/* 	if (imgid != 'img_forfattere') {
		forfattere.style.zIndex = '1';
		forfattere_off.style.zIndex = '2';
	} */
	if (imgid == '') {
		litteratur_off.style.zIndex = '1';
		fakta_off.style.zIndex = '1';
		barnogungdom_off.style.zIndex = '1';
		reise_off.style.zIndex = '1';
		barnehage_off.style.zIndex = '1';
		grunnskole_off.style.zIndex = '1';
		videregaaende_off.style.zIndex = '1';
		/* forfattere_off.style.zIndex = '1'; */
		litteratur.style.zIndex = '2';
		fakta.style.zIndex = '2';	
		barnogungdom.style.zIndex = '2';	
		reise.style.zIndex = '2';	
		barnehage.style.zIndex = '2';	
		grunnskole.style.zIndex = '2';	
		videregaaende.style.zIndex = '2';	
		/* forfattere.style.zIndex = '2';	 */
	}
}
