var xmlhttp=false;

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}
if (window.ActiveXObject) {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
function getTypes(source,id) {
	var target 	= document.getElementById(id);
	var merk_id	= source.options[source.selectedIndex].value;
	var text	= '';
	while(target.options.length>0)
		target.removeChild(target.options[target.options.length-1]);	
	xmlhttp.open("GET", "/includes/get_types.php?merk_id="+merk_id);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != ''){
				var vals = text.split('\n'); 
				var option;
				option = document.createElement('option'); 
				option.setAttribute('value','..'); 
				option.appendChild(document.createTextNode('..selecteer een type..')); 
				target.appendChild(option); 				
				for(var i=0; i<vals.length-1; i=i+2){ 
					option = document.createElement('option'); 
					option.setAttribute('value',vals[i]); 
					option.appendChild(document.createTextNode(vals[i+1])); 
					target.appendChild(option); 
				} 		
			}			
		}
	}
	target.selectedIndex = 0;
	xmlhttp.send(null);	
}
function ableMerk(value){
	if (value!=''){
		document.getElementById('merk_id').disabled='disabled';
		document.getElementById('types_1').disabled='disabled';
	} else {
		document.getElementById('merk_id').disabled='';
		document.getElementById('types_1').disabled='';
	}
}
function ableType(value){
	if (document.getElementById('new_merk').value==''){
		if (value != ''){		
			document.getElementById('new_merk').disabled='disabled';
			document.getElementById('types_1').disabled='disabled';		
		} else {
			document.getElementById('new_merk').disabled='';
			document.getElementById('types_1').disabled='';
		}
	}
}
function getMover(id,cat_id,merk_id,type_id,activity_id,type) {
	var text	= '';
	xmlhttp.open("GET", "/index.php?action=show&type=move&object="+type+"&cat_id="+cat_id+"&merk_id="+merk_id+"&type_id="+type_id+"&activity_id="+activity_id+"&id="+id);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != ''){
				document.getElementById('move').innerHTML = text;
			}							
		}
	}
	xmlhttp.send(null);	
}
function checkUser(username) {
	username = getAllowed(username);
	if (username == -1) {
		document.getElementById('userChecked')=0;
		return;
	}
	var text	= '';
	xmlhttp.open("GET", "/index.php?action=check&type=username&username="+username);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != ''){
				if (text==0){
					document.getElementById('userCheckResult').style.color = '#ff0000';
					document.getElementById('userCheckResult').innerHTML = "Gebruikersnaam is <strong>niet</strong> meer beschikbaar.";
				} else if (text==1) {
					document.getElementById('userCheckResult').style.color = '#1BBC05';					
					document.getElementById('userCheckResult').innerHTML = "Gebruikersnaam is beschikbaar.";
				} else if (text==2){
					document.getElementById('userCheckResult').style.color = '#ff0000';
					document.getElementById('userCheckResult').innerHTML = "Gebruikersnaam is te kort.";					
				} else if (text==3){
					document.getElementById('userCheckResult').style.color = '#ff0000';
					document.getElementById('userCheckResult').innerHTML = "Gebruikersnaam is te lang.";					
				}
				document.getElementById('userChecked').value = text;
			}							
		}
	}
	xmlhttp.send(null);	
}
function enableScale(value){
	if (value == 5){
		document.getElementById('miniatuur').style.display='block';
	} else {
		document.getElementById('miniatuur').style.display='none';				
	}
}
function ajaxDelete(url,question) {
	var conf = confirm(question);
	if (conf){
		ajaxDoDelete(url);
	}
}
function ajaxDoDelete(url){
	var text	= '';
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != 'Geen rechten'){
				document.getElementById(text).style.display='none';
			}							
		}
	}
	xmlhttp.send(null);		
}
function ajaxEdit(id,type,querystring) {
	var text	= '';	
	document.getElementById('editIcon'+id).style.display='none';
	xmlhttp.open("GET", "/index.php?action=show&id="+id+"&type="+type+querystring);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != 'xxNO POSTxx'){
				document.getElementById('text'+id).innerHTML = text;
			}							
		}
	}
	xmlhttp.send(null);		
}
function ajaxDoEdit(id,url,type){
	var text	= '';
	var posttext = document.getElementById('editfield'+id).value;	
	if (posttext.length==0){
		var conf = confirm("Het bericht is leeg, wil je het originele bericht verwijderen?");
		if (conf) ajaxDoDelete(url);
		return;
	}
	
	document.getElementById('editIcon'+id).style.display='inline';
	
	var postvars = "id="+id+"&text="+posttext;
	
	xmlhttp.open("POST", "/index.php?action=edit&type="+type);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			text = xmlhttp.responseText;
			if (text != 'xxNO POSTxx'){
				document.getElementById('text'+id).innerHTML = text;
			}							
		}
	}
	//Send the proper header information along with the request
	try  {
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postvars.length);
	xmlhttp.setRequestHeader("Connection", "close");	
	} catch(err){}
	xmlhttp.send(postvars);			
}
function quote(url,username){
	var quote = getSelText();
	quote = trim(quote.toString());
	quote = removeHTMLTags(quote);
	quote = escape(quote);
	window.location.href=url+'?username='+username+'&quote='+quote+'#reageer';
}
function trim(string) {
	var	string = string.replace(/^\s\s*/, ''),ws = /\s/,i = string.length;
	while (ws.test(string.charAt(--i)));
	return string.slice(0, i + 1);
}
function reloadCount() {
	xmlhttp.open("GET", "/index.php?action=show&type=notificationnumber");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var text = xmlhttp.responseText;
			document.getElementById('container').innerHTML = text;
		}
	}
	xmlhttp.send(null);		
}
function getSelText(){
	var txt = '';
	if (window.getSelection){
		txt = window.getSelection();
	} else if (document.getSelection){
		txt = document.getSelection();
	} else if (document.selection) {
		txt = document.selection.createRange().text;
	} 
	return txt;
}
function removeHTMLTags(strInputCode){
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;	
}
