/*
 * Returns an new XMLHttpRequest object, or false if the browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  // Create XMLHttpRequest object in non-Microsoft browsers
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
      
    } catch (e1) {

      // Failed to create required ActiveXObject
      
      try {
        // Try version supported by older versions
        // of Internet Explorer
      
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest by any means
        xmlreq = false;
      }
    }
  }

return xmlreq;
}

 /*
	* Returns a function that waits for the specified XMLHttpRequest
	* to complete, then passes it XML response to the given handler function.
  * req - The XMLHttpRequest whose state is changing
  * responseXmlHandler - Function to pass the XML response to
  */
 function getReadyStateHandler(req, responseXmlHandler, objResponseCode, objResult) {

   // Return an anonymous function that listens to the XMLHttpRequest instance
   return function () {

     // If the request's status is "complete"
     if (req.readyState == 4) {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       // Check that we received a successful response from the server
       if (req.status == 200) {

         // Pass the XML payload of the response to the handler function.
         responseXmlHandler(req,objResult);

       } else {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       
         // An HTTP problem has occurred
         if(objResponseCode) objResponseCode.innerHTML += "HTTP error " + req.status + ": " + req.statusText + "\n" + req.responseText;
       }
     }else{
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
     }
   }
 }

 function getReadyStateHandler3(req, responseXmlHandler, objResponseCode, objResult,extraParam) {

   // Return an anonymous function that listens to the XMLHttpRequest instance
   return function () {

     // If the request's status is "complete"
     if (req.readyState == 4) {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       // Check that we received a successful response from the server
       if (req.status == 200) {

         // Pass the XML payload of the response to the handler function.
         responseXmlHandler(req,objResult,extraParam);

       } else {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       
         // An HTTP problem has occurred
         if(objResponseCode) objResponseCode.innerHTML += "HTTP error " + req.status + ": " + req.statusText + "\n" + req.responseText;
       }
     }else{
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
     }
   }
 }
    
function getReadyState(obj){
		if(obj.readyState == 0)
		{
			return "<img src=/images/ajax-loader-darkblue.gif alt='Sending Request...' border=0 height=32 widht=32>";
		}
		if(obj.readyState == 1)
		{
			return "<img src=/images/ajax-loader-darkblue.gif alt='Loading Response...' border=0 height=32 widht=32>";
		}
		if(obj.readyState == 2)
		{
			return "Response Loaded...";
		}
		if(obj.readyState == 3)
		{
			return "Response Ready...";
		}
		if(obj.readyState == 4)
		{
			if(obj.status == 200)
			{
				return "OK";
			}
			else if(obj.status == 404)
			{
			// Add a custom message or redirect the user to another page
				return "File not found";
			}
			else
			{
				return "There was a problem retrieving the XML.";
			}
		}
	}
	/*
	* url: url til skriptet som skal fyres av
	* responseHandler er motend som skal behandle svaret eks. showPrintet(req, objResult){objResult.innerHTML = req.responseText;}
	*/
	function makeRequest(url,responseHandler,objResponseCode,objResult){
		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, responseHandler, objResponseCode, objResult);
 		req.open("GET", url, true);
		req.send(null);
	}
	function makeRequest2(url,responseHandler,objResponseCode,objResult,extraParam){
		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler3(req, responseHandler, objResponseCode, objResult,extraParam);
 		req.open("GET", url, true);
		req.send(null);
	}
	//ikke testet enda men skal fungere for posts postDate = 'felt1=verdi&felt2=verdi'
	//ellers se makeRequest
	function makePost(url,postData, responseHandler,objResponseCode,objResult){
		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, responseHandler, objResponseCode, objResult);
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("encoding", "ISO-8859-1");
		req.send(postData);
	}
	function makePost2(url,postData, responseHandler,errorHandler,objResponseCode,objResult){
		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler2(req, responseHandler,errorHandler, objResponseCode, objResult);
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("encoding", "ISO-8859-1");
		req.send(postData);
	}
	function getPostData(form, btnClicked){
		var result = "";
		for (var i = 0; i < form.elements.length; i++) {
			var el = form.elements[i];
			if (el.tagName.toLowerCase() == "select") {
				for (var j = 0; j < el.options.length; j++) {
					var op = el.options[j];
					if (op.selected)
						result += "&" + encodeURIComponent(el.name) + "=" + escape(op.value);
				}
			} else if (el.tagName.toLowerCase() == "textarea") {
				result += "&" + encodeURIComponent(el.name) + "=" + escape(el.value);
			} else if (el.tagName.toLowerCase() == "input") {
				if (el.type.toLowerCase() == "checkbox" || el.type.toLowerCase() == "radio") {
					if (el.checked)
						result += "&" + encodeURIComponent(el.name) + "=" + escape(el.value);
				} else if (el.type.toLowerCase() == "submit") {
					if (el == btnClicked) // is "el" the submit button that fired the form submit?
						result += "&" + encodeURIComponent(el.name) + "=" + escape(el.value);
				} else if (el.type.toLowerCase() != "button") {
					result += "&" + encodeURIComponent(el.name) + "=" + escape(el.value);
				}
			}
		}
		return result.substring(1);
	}
	function getPostData2(form, btnClicked,favid){
		var result = "";
		for (var i = 0; i < form.elements.length; i++) {
			var el = form.elements[i];
			if (el.tagName.toLowerCase() == "select") {
				for (var j = 0; j < el.options.length; j++) {
					var op = el.options[j];
					if (op.selected)
						if ( encodeURIComponent(el.name).substr(encodeURIComponent(el.name).length - 2,2) == favid){
							result += "&" + encodeURIComponent(el.name).substr(0,encodeURIComponent(el.name).length - 3) + "=" + escape(op.value);
						}
				}
			} else if (el.tagName.toLowerCase() == "textarea") {
				if ( encodeURIComponent(el.name).substr(encodeURIComponent(el.name).length - 2,2) == favid){
					result += "&" + encodeURIComponent(el.name).substr(0,encodeURIComponent(el.name).length - 3) + "=" + escape(el.value);
				}
			} else if (el.tagName.toLowerCase() == "input") {
				if (el.type.toLowerCase() == "checkbox" || el.type.toLowerCase() == "radio") {
					if (el.checked)
						if ( encodeURIComponent(el.name).substr(encodeURIComponent(el.name).length - 2,2) == favid){
							result += "&" + encodeURIComponent(el.name).substr(0,encodeURIComponent(el.name).length - 3) + "=" + escape(el.value);
						}
				} else if (el.type.toLowerCase() == "submit") {
					if (el == btnClicked) // is "el" the submit button that fired the form submit?
						if ( encodeURIComponent(el.name).substr(encodeURIComponent(el.name).length - 2,2) == favid){
							result += "&" + encodeURIComponent(el.name).substr(0,encodeURIComponent(el.name).length - 3) + "=" + escape(el.value);
						}
				} else if (el.type.toLowerCase() != "button") {
					if ( encodeURIComponent(el.name).substr(encodeURIComponent(el.name).length - 2,2) == favid){
						result += "&" + encodeURIComponent(el.name).substr(0,encodeURIComponent(el.name).length - 3) + "=" + escape(el.value);
					}
				}
			}
		}
		return result.substring(1);
	}
	/*
	* Returns a function that waits for the specified XMLHttpRequest
	* to complete, then passes it XML response to the given handler function.
  * req - The XMLHttpRequest whose state is changing
  * responseXmlHandler - Function to pass the XML response to
  */
 function getReadyStateHandler2(req, responseXmlHandler, errorHandler, objResponseCode, objResult) {

   // Return an anonymous function that listens to the XMLHttpRequest instance
   return function () {

     // If the request's status is "complete"
     if (req.readyState == 4) {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       // Check that we received a successful response from the server
       if (req.status == 200) {

         // Pass the XML payload of the response to the handler function.
         responseXmlHandler(req,objResult);

       } else {
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
       
         // An HTTP problem has occurred
         if(objResponseCode) objResponseCode.innerHTML += "HTTP error " + req.status + ": " + req.statusText + "\n" + req.responseText;
         
          // Pass the XML payload of the response to the handler function.
        errorHandler(req,objResult); 
       }
       
     }else{
		if(objResponseCode) objResponseCode.innerHTML = getReadyState(req);
     }
   }
 }

