function encode_utf8( s ){
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s ){
  return decodeURIComponent( escape( s ) );
}

function ajax(inControl, inURL){
	var xmlhttp = null;
	document.body.style.cursor = 'wait';
	
	inURL = encodeURI(inURL);
	
	if (window.XMLHttpRequest) { // Firefox, Opera, Safari och Netscape
	   xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // Internet Explorers olika versioner
	   try {
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");	
	   } catch (e) {
		  try {
			 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (e) { alert("Din webbläsare stödjer inte Ajax"); }
	   }
	}
	
	if (xmlhttp){
		var url = inURL
		xmlhttp.open("POST",url,false);
		//xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Content-Type","text/html; charset=UTF-8");
		xmlhttp.send(null);
		
		var docObj = document.getElementById(inControl);
	
		/* Kontrollerar om vi ska skriva till value eller innerHTML */
		with(docObj){
			var objType = docObj.type;
			
			switch(objType){
				case "text":
				case "button":
					docObj.value = xmlhttp.responseText;					
					break;
				default:
					docObj.innerHTML = xmlhttp.responseText;
					break;
			}		
		}
		
		xmlhttp.abort();
	}
	
	document.body.style.cursor = 'auto';	
} 

function savePost(form, url, fel_id){
	var urlstring;
	
	with(form){
		for(var i = 0; i < elements.length; i++){
			//elements[i].value = elements[i].value.replace(/[\r\n]+/g, "");		
			if(i == 0){
				//responseText.replace(/[\r\n]+/g, "");
				urlstring = '?'+elements[i].name+'='+elements[i].value;
			}else{
				urlstring += '&'+elements[i].name+'='+elements[i].value;
			}
		}	
		
		urlstring = urlstring.replace(new RegExp( "\\n", "g" ),"<br>");
 
		ajax('guestbook', url+'guestbookajax.php'+urlstring+'&action=save');	
	}
}
