   var http_request = false;
   var tick = 0;
   
   function makePOSTRequest(url, parameters, area) {
      http_request = false;
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) {
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
	  if(area == 'price'){
		 showload(true);
         http_request.onreadystatechange = alertContentsPrice;
	  } else if(area == 'tinyurl'){
	     showload(true);
         http_request.onreadystatechange = alertContentsTinyUrl;
	  } else if(area == 'addition'){
		 http_request.onreadystatechange = alertContentsAddition;
	  } else if(area == 'submit'){
		 http_request.onreadystatechange = alertContentsSubmit;
	  }
      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 alertContentsSubmit() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('submit').innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function alertContentsAddition() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('addition').innerHTML = result;
			if(gup('step3') == '1'){
				step3_change();
			}
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function alertContentsPrice() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('price').innerHTML = result;
			document.getElementById('loading').innerHTML = '';
			tick = 0;
			if(gup('step2') == '1'){
				step2_submit_change();
			} else if(gup('step3') == '1'){
				try{
					var test = document.getElementById('button_test').value;
				} catch(e){
					step3_submit_change();
				}
			}
         } else {
            alert('There was a problem with the request.');
         }
      } else{
		if(tick > 0){
		  document.getElementById('loading').innerHTML = '<img src="images/loading.gif">';
		}
	  }
   }
   
   function alertContentsTinyUrl() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            //document.getElementById('tinyurl').innerHTML = result;
			document.getElementById('loading').innerHTML = '';
			tick = 0;
			window.location.href='mailto:?body='+result+'&subject=Bygg din BRIG';
         } else {
            alert('There was a problem with the request.');
         }
      } else{
		if(tick > 0){
		  document.getElementById('loading').innerHTML = '<img src="images/loading.gif">';
		}
	  }
   }

   	function showload(proceed){
		if(proceed == true){
			setTimeout('showload(false)',2800);
		} else{
			tick++;
			return;
		}
	}
   
   function get(obj, poststr, area, url) {
       makePOSTRequest(url, poststr, area);
   }
   
	function boat_change(obj){
		var boat_id;
		if(encodeURI( document.getElementById("boat_id_0").value ) != 0) boat_id = encodeURI( document.getElementById("boat_id_0").value );
		else if(encodeURI( document.getElementById("boat_id_1").value ) != 0) boat_id = encodeURI( document.getElementById("boat_id_1").value );
		else boat_id = encodeURI( document.getElementById("boat_id_2").value );
		var poststr = "show=submit&submit=1"
		+ "&boat_id=" + boat_id;
		get(obj, poststr, 'submit', 'calc_data2.php');
	}
	
	function cat_change(obj){
		var poststr = "show=boat"
		+ "&cat_id=" + encodeURI( document.getElementById("cat_id").value );
		get(obj, poststr, 'addition', 'calc_data2.php');
	}

	function step2_change(obj){
		var poststr = "boat_id=" + gup('boat_id')
		+ "&engine=" + escape(encodeURI(document.getElementById("engine").value));
		var tmp;
		for(var k=0; k<10; k++){
			try{
				if(document.getElementById("mount_"+k).value == 1){
					if(document.getElementById("mount_"+k).checked == true){
						poststr += "&mount_"+k+"=1";
						tmp = k;
					} else{
						poststr += "&mount_"+k+"=0";
					}
				}
			} catch(e){}
		}
		get(obj, poststr, 'price', 'calc_price.php');
	}
	
	function step2_submit_change(obj){
		var poststr = "show=submit&submit=2"
		+ "&boat_id=" + gup('boat_id') 
		+ "&engine=" + escape(encodeURI(document.getElementById("engine").value));
		var tmp;
		for(var k=0; k<10; k++){
			try{
				if(document.getElementById("mount_"+k).value == 1){
					if(document.getElementById("mount_"+k).checked == true){
						tmp = k;
						break;
					}
				}
			} catch(e){}
		}
		poststr += "&mount=" + k;
		get(obj, poststr, 'submit', 'calc_data2.php');
	}
	
	function step3_getobj(obj){
		var poststr = "show=color"
		+ "&boat_id=" + gup('boat_id')
		+ "&hypa=" + escape(encodeURI(document.getElementById("hypa").value));
		get(obj, poststr, 'addition', 'calc_data2.php');
	}
		
	function step3_change(obj){
		var poststr = "step3=1&boat_id=" + gup('boat_id')
		+ "&mount_" + gup('mount') + "=1"
		+ "&mount=" + gup('mount')
		+ "&engine=" + gup('engine')
		+ "&color=" + escape(encodeURI(document.getElementById("color").value))
		+ "&hypa=" + escape(encodeURI(document.getElementById("hypa").value))
		+ "&gelcoat=" + escape(encodeURI(document.getElementById("gelcoat").value))
		+ "&cushion=" + escape(encodeURI(document.getElementById("cushion").value));
		get(obj, poststr, 'price', 'calc_price.php');
	}
	
	function step3_submit_change(obj){
		var poststr = "show=submit&submit=3"
		+ "&boat_id=" + gup('boat_id')
		+ "&mount_" + gup('mount') + "=1"
		+ "&mount=" + gup('mount')
		+ "&engine=" + gup('engine')
		+ "&color=" + escape(encodeURI(document.getElementById("color").value))
		+ "&hypa=" + escape(encodeURI(document.getElementById("hypa").value))
		+ "&gelcoat=" + escape(encodeURI(document.getElementById("gelcoat").value))
		+ "&cushion=" + escape(encodeURI(document.getElementById("cushion").value));
		get(obj, poststr, 'submit', 'calc_data2.php');
	}

	function step4_change(obj, add){
		document.getElementById('tinyurl').innerHTML = '';
		var poststr = "step4=1&boat_id=" + gup('boat_id')
		+ "&mount_" + gup('mount') + "=1"
		+ "&mount=" + gup('mount')
		+ "&engine=" + gup('engine')
		+ "&color=" + gup('color')
		+ "&hypa=" + gup('hypa')
		+ "&gelcoat=" + gup('gelcoat')
		+ "&cushion=" + gup('cushion')
		+ "&ac_total=" + document.getElementById('ac_total').value;
		for(var i=0; i<32; i++){
			try{
				if(document.getElementById(i).checked == true){
					poststr += "&"+i+"=" + escape(encodeURI(document.getElementById(i).value));
				} else{
					poststr += "&"+i+"=0";
				}
			}
			catch(e){};
		}
		for(var j=0; j<(document.getElementById('ac_total').value); j++){
			try{
				if(document.getElementById('ac_'+j).checked == true){
					poststr += "&"+'ac_'+j+"=" + escape(encodeURI(document.getElementById('ac_'+j).value));
				} else{
					poststr += "&"+'ac_'+j+"=0";
				}
			}
			catch(e){};
		}
		//changeURL('?'+poststr+'#calcing');
		if(add == 1){
			window.location.href="admin_stock.php?add=true&retail="+escape(encodeURI(document.getElementById("retail").value))+"&"+poststr;
		} else{
			get(obj, poststr, 'price', 'calc_price.php');
		}
	}

	function gup( name ){
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return "";
		else
			return results[1];
	}
	
	function changeURL(urlPath){
		window.history.pushState(null,"",urlPath);
	}
	
	function getTinyUrl(obj, script){
		var url = '';
		if(document.getElementById('url').value){
			url = script+document.getElementById('url').value;
		} else{
			url = document.URL;
		}
		var poststr = 'url='+escape(encodeURI(url));
		//alert(url);
		get(obj, poststr, 'tinyurl', 'tinyurl_process.php');
	}

