

var returnfromserver='';
var allowrollon=true;

var commentdiv;
var quotediv;

var browser='';


function getHttpRequest() {


	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			browser='firefox';
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml');
				// See note below about this line
			}
		}
		else if (window.ActiveXObject) { // IE
		
			browser='ie';
			
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
						   try {
								httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
							   }
							 catch (e) {}
						  }
									   }

		if (!httpRequest) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
	}

	return httpRequest;

}


function getoldcomment() {

	var userid = document.myform.user_id.value;
	var aboutuserid = document.myform.aboutuser_id.value;
	var hash = document.myform.hash.value;

	var url = "getpersonalcomment.php?uid="+userid+"&auid="+aboutuserid+"&hash="+hash;
	
	httpRequest.onreadystatechange = updateComment;
	httpRequest.open('GET', url, true);
	//httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpRequest.send(null);

}

function updateComment() {


	//alert("updating comment readystate : "+httpRequest.readyState);
	
	
	if (httpRequest.readyState == 4) {
		
		//alert ("status is " + httpRequest.status);
		
		if (httpRequest.status == 200) {


			//alert ("status is now 200");
			
			commentdiv = document.getElementById('mycommentdiv');			
			commentdiv.onclick=changeToTextarea;	
			
			
			//var xmldoc=httpRequest.responseXML.documentElement;			

			var returned = httpRequest.responseText;			
			var splitat = returned.indexOf('|');
			
			//var comment = xmldoc.getElementsByTagName("comment")[0].childNodes[0].nodeValue;
			var quote=returned.substring(6,splitat);
			splitat=splitat+1;
			var comment = returned.substr(splitat);

			
			commentdiv.innerHTML=comment;
	
			//var quote = xmldoc.getElementsByTagName("quote")[0].childNodes[0].nodeValue;
			
			if (quote.length>0) {
				quotediv.innerHTML='<br><br>Uw score: <img src="/img/'+quote+'.jpg">';
			}
	
			document.forms.myform.oldquote.value=quote;
	

		}
	}
	
	
}

function cancelinput() {

	
	inputbox = document.myform.mycomment;
	commentdiv.align='left';
	
	commentdiv.onclick=changeToTextarea;	
	commentdiv.innerHTML=oldvalue;
	allowrollon=true;		
	
}

function savecomment(comment, quote) {

	
	
	
	var userid = document.myform.user_id.value;
	var aboutuserid = document.myform.aboutuser_id.value;
	var hash = document.myform.hash.value;
	
	url = "savepersonalcomment.php?uid="+userid+"&auid="+aboutuserid+"&hash="+hash+"&comment="+comment+"&quote="+quote;
	
	
	
	httpRequest.open('GET', url, true);
	httpRequest.onreadystatechange = updateComment;
	//httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
	

	httpRequest.send(null);

}




function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function



function closeDown() {
	
	
	
	commentdiv.align='left';
	inputtedvalue = document.myform.mycomment.value;
	quote = getSelectedRadioValue(document.myform.quote);
	
	
	commentdiv.innerHTML='Commentaar wordt opgeslagen...';


	var quotediv = document.getElementById('myquotediv');

	
	savecomment(inputtedvalue, quote);

//	commentdiv.onclick=getoldcomment;
	allowrollon=true;	

	
	//window.setTimeout('makeRequest(document.myform.mycomment.value)', 5000);
	
	
}

function resetValue(box) {
	
	if (box.value == 'Nog geen commentaar. Klik hier om commentaar toe te voegen') {
		box.value='';
	}
	
}

function changeToTextarea() {
	
	

	commentdiv = document.getElementById('mycommentdiv');
	quotediv = document.getElementById('myquotediv');
	
	
		
		
	var oldquote = document.forms.myform.oldquote.value;	
	
	
	//area.onclick = '';

	

	
	commentdiv.onclick='';
	commentdiv.className='commentregular';
	commentdiv.align='center';
	allowrollon=false;


	
	quotediv.innerHTML='';
	
	
	//commentdiv.style.background='#FFFFFF';
	oldvalue=commentdiv.innerHTML;


	commentdiv.innerHTML='';

	
	
	
	var nloldvalue=oldvalue;
	
	
	
	nloldvalue = nloldvalue.replace(/<br \/>/ig, '\n');
	nloldvalue = nloldvalue.replace(/<br\/>/ig, '\n');
	nloldvalue = nloldvalue.replace(/<br >/ig, '\n');
	nloldvalue = nloldvalue.replace(/<br>/ig, '\n');
	
	
	str = '<textarea style="background: #FFFFD3; border:0px" rows=7 cols=70 id="mycomment" name="mycomment">'+nloldvalue+'</textarea><br>';
	

	str = str + '<table width="400"><tr><td><input type="radio" class="checkbox" name="quote" value="ttu"'

	if (oldquote == 'ttu') {
		str = str + 'checked'

	}

	str += '><img src="img/ttu.jpg"></td><td><input type="radio" class="checkbox" name="quote" value="tu"'

	if (oldquote == 'tu') {
		str = str + 'checked'
	}

	str += '><img src="img/tu.jpg"></td><td><input type="radio" class="checkbox" name="quote" value="td"'

	if (oldquote == 'td') {
		str = str + 'checked'
	}

	str += '><img src="img/td.jpg"></td><td><input type="radio" class="checkbox" name="quote" value="ttd"'

	if (oldquote == 'ttd') {
		str = str + 'checked'
	}

	str += '><img src="img/ttd.jpg"></td></tr></table><br><input type="button" name="submit" value="Save" onClick="closeDown()">';
    str += '<input type="button" name="cancel" value="Cancel" onClick="cancelinput()">';
    
	commentdiv.innerHTML=str;

	document.myform.mycomment.focus();
	box = document.myform.mycomment;
	resetValue(box);
	
	
	//alert("marking converted true");
	isconverted=true;

	//checkConverted();
		
		
	

	
	
	
	
	
}

function alertonclick() {
	alert('on click');
}

function showstatusconverted() {
	
	if (isconverted) {
		alert('is converted');
	}
	else {
		alert ('not converted');
	}
	
}


function dynedit() {
	
	var currentarea = '';


	
	function unhoover() {
		
		// turn the class back to normal
		
		currentarea.className='commentregular';
		
	}


	this.setarea = function (area) {
		// keep track of what our current area is 
		// this is required for the setTimeOut function in rolloff
		
		currentarea = area;
		
	}
	
	this.rollon = function () {
		
		// change the class to the hoovered one
		if (allowrollon) {
			currentarea.className='commenthover';
		}
		
		
	}

	this.rolloff = function () {
		
		// call function to turn the class back to normal
		// call only after 1 sec - so the hoover stays for a while longer
		
		
		setTimeout(unhoover, 1000);
		
		
		
	}	
			
	

}

function checksitemails(otheruser, loggedinuser, hash) {
	

	var contactsbutton = document.getElementById('checkcontactsbutton');
	contactsbutton.innerHTML='';
	
	var sitemailsdiv = document.getElementById('contactsbetweenus');
	sitemailsdiv.innerHTML = 'Berichten worden opgehaald ...';
	

	

	var url = "getsitemails.php";
	httpRequest.onreadystatechange = updateSitemails;
	httpRequest.open('POST', url, true);
	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	httpRequest.send("uid="+loggedinuser+"&ouid="+otheruser+"&hash="+hash);	
	
}

function updateSitemails() {
	

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {



			var sitemails = httpRequest.responseText;
    

			var sitemailsdiv = document.getElementById('contactsbetweenus');
			sitemailsdiv.innerHTML=sitemails;
			
		}
	}
	

	
}


var httpRequest;
if (!httpRequest) {
	httpRequest = getHttpRequest();
}
