//XMLhttp variable will hold the XMLHttpRequest object
var xmlhttp = false;
var xmlhttpB= false;
        
// If the user is using Mozilla/Firefox/Safari/etc
if (window.XMLHttpRequest) {
        //Intiate the object
        xmlhttp = new XMLHttpRequest();
		xmlhttpB = new XMLHttpRequest();
}
// If the user is using IE
else if (window.ActiveXObject) {
        //Intiate the object
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttpB = new ActiveXObject("Microsoft.XMLHTTP");
}


function getListe(sideno){
	
	xmlhttp.open('get', 'test.php?side=' + sideno);
	xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('hovedrute').innerHTML = xmlhttp.responseText + ' ';
			}
		}; 
	xmlhttp.send(null);
	alert(sideno);
}



function sendForm(skjema){
	var sendeStreng = formData2QueryString(skjema);
	
	xmlhttp.open('POST', 'lagrevare.php', true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			document.getElementById('tilbakemld').innerHTML = xmlhttp.responseText + ' ';
		}
	}
	xmlhttp.send(sendeStreng);
}

function redX(filnavn,dividen, idvalue){
	
	today=new Date();
	
	
	xmlhttp.open('get', filnavn+'?id='+idvalue+'&tid='+today.getTime());
	xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById(dividen).innerHTML = xmlhttp.responseText + ' ';
			}
		}; 
	xmlhttp.send(null);
}

function visX(filnavn, dividen, idvalue, dagen ){
	
	today=new Date();
	
	
	xmlhttp.open('get', filnavn+'?id='+idvalue+'&dagen='+dagen+'&tid='+today.getTime());
	xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById(dividen).innerHTML = xmlhttp.responseText + ' ';
			}
		}; 
	xmlhttp.send(null);
}

function romvalg(forvalg){
	romtype = new Array;
	rombeskrivelse = new Array;
	senger = new Array;
	romtype[0]= ''; rombeskrivelse[0]=''; senger[0]='';
	romtype[1]= 'Leilighet'; rombeskrivelse[1]='ettroms'; senger[1]='2';
	romtype[2]= 'Leilighet'; rombeskrivelse[2]='toroms'; senger[2]='4';
	romtype[3]= 'Leilighet'; rombeskrivelse[3]='treroms'; senger[3]='6';
	romtype[4]= 'Leilighet'; rombeskrivelse[4]='firetroms'; senger[4]='8';
	romtype[11]= 'Villa'; rombeskrivelse[11]='fireroms'; senger[11]='';
	romtype[21]= 'Hotell'; rombeskrivelse[21]='enkeltrom'; senger[21]='1';
	romtype[22]= 'Hotell'; rombeskrivelse[22]='dobbeltrom'; senger[22]='2';
	romtype[23]= 'Hotell'; rombeskrivelse[23]='trippelrom'; senger[23]='3';
	romtype[24]= 'Hotell'; rombeskrivelse[24]='suite'; senger[24]='4';
	document.getElementById('romtype').value=romtype[forvalg];
	document.getElementById('rombeskrivelse').value=rombeskrivelse[forvalg];
	document.getElementById('senger').value=senger[forvalg];
}

function getRadioValue(theObj) {

	for (var i=0; i<theObj.length; i++) {

	if (theObj[i].checked) { return theObj[i].value;}

	}

	return "";

}

function getOptValue(theObj) {
	var selectBox = document.getElementById(theObj);
	return selectBox.options[selectBox.selectedIndex].value;
}


function formData2QueryString(docForm) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

	
	for (i = 0; i < document.getElementById(docForm).length; i++) {
		
		formElem = document.getElementById(docForm).elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				break;
				
			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;
				
			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + escape(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;
				
		}
	}
	
	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
}
