/* GLOBALS */
var httpAjax = false; 
var bAjaxBusy=false;
// target div for ajax ops
var target="";

function getFormValues(fobj){ 
// encodeURIComponent -> encodeURI
var str = ""; 
for(var i = 0;i < fobj.elements.length;i++) { 
	switch(fobj.elements[i].type){ 
	case "text": 
	case "textarea": 
	case "password": 
		if (!fobj.elements[i].disabled) {
			if (fobj.elements[i].className=="rb")
				break;
			str += fobj.elements[i].name + "=" + encodeURI(fobj.elements[i].value) + "&"; 
		}
		break; 
	case "hidden": 
	//hidden cannot be disabled 
	str += fobj.elements[i].name + "=" + encodeURI(fobj.elements[i].value) + "&"; 
		break; 
	case "checkbox": 
	case "radio": 
		if(fobj.elements[i].checked && !fobj.elements[i].disabled)
			str += fobj.elements[i].name + "=" + encodeURI(fobj.elements[i].value) + "&"; 
		break; 
	case "select-one": 
		if (!fobj.elements[i].disabled) 
			str += fobj.elements[i].name + "=" + encodeURI(fobj.elements[i].options[fobj.elements[i].selectedIndex].value) + "&";
		break;
	case "select-multiple": 
		if (!fobj.elements[i].disabled){ 
			for (var j = 0; j < fobj.elements[i].length; j++){ 
				var optElem = fobj.elements[i].options[j]; 
				if (optElem.selected == true){ 
					str += fobj.elements[i].name + "[]" + "=" + encodeURI(optElem.value) + "&"; 
				} 
			} // end for
		} // end if
		break; 
	} 	// end switch
}	// end for 
//Strip final &amp; 
str = str.substr(0,(str.length - 1)); 
return str; 
} // end function

function doAjax(url,sTarget,bPost,str) { 
    httpAjax = false; 
    handler = false;
	bXml=false; 
	target=sTarget;
    if (window.XMLHttpRequest) { 
        httpAjax = new XMLHttpRequest(); 
        if (httpAjax.overrideMimeType) { 
            // set type accordingly to anticipated content type
		    if (bXml)
			    httpAjax.overrideMimeType('text/xml'); 
			else
			    httpAjax.overrideMimeType('text/html');
			} 
    } else if (window.ActiveXObject) { 
        try { 
            httpAjax = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try { 
                httpAjax = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) { 
                alert("Your Browser doesn't support AJAX."); 
                return false; 
            } 
        } 
    } 
        
	try { 
        if (bXml)
	        httpAjax.onreadystatechange = getXML; 
		else
			httpAjax.onreadystatechange = getText; 
    } catch (e) { 
        alert("onreadystatechange didnīt go well!"); 
        return false; 
    } 
        
	try { 
		if (bPost) {
			httpAjax.open("POST", url, true); 
			// httpAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1"); 
			httpAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			// httpAjax.setRequestHeader("encoding", "ISO-8859-1");
			// httpAjax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			// httpAjax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			// httpAjax.setRequestHeader("Pragma", "no-cache");
		    httpAjax.setRequestHeader("Content-length", str.length);
      		httpAjax.setRequestHeader("Connection", "close");
		}
		else {
			httpAjax.open("GET", url, true);
			// Detta har ingen effekt här:
			// httpAjax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			// httpAjax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			// httpAjax.setRequestHeader("Pragma", "no-cache");
		}
    } catch (e) { 
        alert("Couldnīt open url."); 
        return false; 
    } 
        
	try { 
        if (bPost)
			httpAjax.send(str);
		else
			httpAjax.send(null); 
    } catch (e) { 
        alert("Couldnīt send request."); 
        return false; 
    } 
	bAjaxBusy=true;
    return true; 
} 
function getXML() {
	/* Not implented here */
	if (httpAjax.readyState == 4){ 	
		document.getElementById("status2").className="hilight3";
		document.getElementById("status2").innerHTML="Ready";
		bAjaxBusy=false;
		window.status="Ready";
		onBody();
		return true; 
	}
	else { 
		document.getElementById("status2").className="hilight";
		document.getElementById("status2").innerHTML="Page is loading, Please wait...";
		window.status="Page is loading, Please wait...";
    }
}
function getText() {
	if (httpAjax.readyState == 4){ 	
		// Exceptions thanks to Bill Gates, Combo-box
		/*
		if (target=="selvars" || target=="selmatch" || target=="lemma")
			document.getElementById(target).outerHTML = httpAjax.responseText;
		*/
		if (target=="curid") {
			// target is <INPUT>, must have ID=curid
			var curid = httpAjax.responseText;
			if (curid > 0) {
				document.getElementById(target).value=curid;
				document.getElementById("status2").className="hilight3";
				document.getElementById("status2").innerHTML = "Observation saved";
			}
			else if (curid==-1) {
				document.getElementById("status2").className="hilight";
				document.getElementById("status2").innerHTML = "Wrong Code";
			}
			else {
				document.getElementById("status2").className="hilight";
				document.getElementById("status2").innerHTML = "Observation NOT saved";
			}
		}
		else if (target=="status2") {
			// Responsetext to status2
			document.getElementById(target).innerHTML = httpAjax.responseText;
		}
		else {
			document.getElementById(target).innerHTML = httpAjax.responseText;
			document.getElementById("status2").className="hilight3";
			document.getElementById("status2").innerHTML="Ready";
		}
		bAjaxBusy=false;
		window.status="Ready";
		onBody();
		return true; 
	}
	else { 
		document.getElementById("status2").className="hilight";
		document.getElementById("status2").innerHTML="Page is loading, Please wait...";
		window.status="Page is loading, Please wait...";
    } 
}
	
// Cf. Jscript: encodeURI(); decodeURI()
function urlencode(string) {
	return escape(string.toString().replace(/\+/g, '%2B'));
}

function urldecode(string) {
	string = string.toString().replace(/\+/g, ' ');
	while(string.match('%2B')) {
		string = string.replace('%2B', '+');
	}
	return unescape(string);
}
