var agt=navigator.userAgent.toLowerCase();
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function openWindow(theURL,width,height,scrollbar,windowname) {
  if (!scrollbar) scrollbar = 'no';
  if (!width) width = 500;
  if (!height) height = 600;
  if (!windowname) windowname = 'window'; 
  window.open(theURL,windowname,'status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars='+scrollbar+',width='+width+',height='+height);
}
function openWindow2(theURL,width,height,scrollbar,windowname) {
  if (!scrollbar) scrollbar = 'yes';
  if (!width) width = 400;
  if (!height) height = 600;
  if (!windowname) windowname = 'window'; 
  return window.open(theURL,windowname,'status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars='+scrollbar+',width='+width+',height='+height);
}

function set_afbeelding(afbeelding, image) {
	if (afbeelding.length) {
		formfield = document.getElementById(afbeelding+"_hidden");
		imagefield = document.getElementById(afbeelding);
		imagefield.style.display = "block";
	
		imagefield.src = "../afbeeldingen/" + image;
		formfield.value = image;
		
		i = 1;
		while(document.getElementById("afbeelding_extra["+ i +"]")!=null) {
			document.getElementById("afbeelding_extra["+ i +"]").style.display = "block";
			i++;
		}
	}
}
function unset_afbeelding(afbeelding,hidden) {
	if (afbeelding.length) {
		formfield = document.getElementById(afbeelding+"_hidden");
		imagefield = document.getElementById(afbeelding);
		
		if (hidden!=false) imagefield.style.display = "none";
	
		imagefield.src = "";
		formfield.value = "";
		i = 1;
		while(document.getElementById("afbeelding_extra["+ i +"]")!=null) {
			document.getElementById("afbeelding_extra["+ i +"]").style.display = "none";
			i++;
		}
	}
}

/////////////////////////////////////
function insert_text(textarea,text)
{
    var target = document.getElementById( textarea );

    if ( target )
    {
        if (target.cursorPos )
        {
            var cursorPos = target.cursorPos;
            cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ? text + ' ' : text;
        }
        else if ( typeof target.selectionStart != 'undefined' )
        {
            var sStart = target.selectionStart;
            var sEnd = target.selectionEnd;
            target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length);
            target.selectionStart = sStart == sEnd ? sStart + text.length : sStart;
            target.selectionEnd = sStart + text.length;
        }
        else
        {
            target.value += text;
        }
        target.focus();
    }
}

function storeCursor(el) {
  if (is_ie) {
	  if (el.createTextRange) el.cursorPos = document.selection.createRange().duplicate();
  } else {
	  if (el.createTextRange) el.cursorPos = this.getSelection();
  }
}

function getSelected() {
  if (is_ie) {
	  return document.selection.createRange().duplicate().text;
  } else {
	  return window.getSelection();
  }
}

/////////////////////////////////////////////

function maxChars(i,textarea) {
	if (textarea.value.length>i) 
		textarea.value = textarea.value.substr(0,500);
}

function kopie(textarea_kort,textarea,length) {
	var inhoud = textarea.value;
	if (!length || length == 0) {
		textarea_kort.value = inhoud;
	} else {
		textarea_kort.value = inhoud.substr(0,length);
	}
}


//FORM PARSEN NAAR EEN WINDOW////////////
var set_action = false;
var formaction;
function set_formaction(action) {
	if (set_action==false) {
		formaction = action;
		set_action = true;
	}
}

function swapFormsettings(form,url,target) {
	if (!target) target = '';
	set_formaction(form.action);
	form.target=target;
	form.action=(!url)?formaction:url;
}
//////////////////////////////

function swapRadio(img,name,value,url) {
	var obj = document.getElementById(name);
	if (obj) {
		if (obj.value!=value) {
			//change en ook de andere buttons
			obj.value = value;
			for(var i = 0; i<document.images.length;i++) {
				if (document.images[i].id.indexOf("radio"+name)!=-1) document.images[i].src = url+"images/input/radio.gif";
			}
			img.src = url+"images/input/radiochecked.gif";
		}
	} else
		alert('Er is geen hidden object geplaatst.');
}

function swapCheckbox(img,name,value,url,state) {
	if (!state && state!=0) state = -1;
	var obj = document.getElementById("checkbox"+name);
	if (obj) {
		if (state==1) {
			obj.value = value;
			img.src = url+"images/input/checkboxchecked.gif";
		} else if (state==0) {
			obj.value = "";
			img.src = url+"images/input/checkbox.gif";
		} else if (img.src.indexOf("checked")!=-1) {
			//hij staat aan
			obj.value = "";
			img.src = url+"images/input/checkbox.gif";
		} else {
			obj.value = value;
			img.src = url+"images/input/checkboxchecked.gif";
		}
	}// else
		//alert('Er is geen hidden object geplaatst.');
}

function parseCheck(object, url) { //als men vinkt, kan dat ook meteen opgeslagen worden
	object.disabled = true;
	parseFrame.location.href=url;
	object.disabled = false;
}

function mouseout(object,i,bg) {
	object.className = bg;
	var obj = document.getElementById("actie"+i);
	if (obj) obj.style.display = "none";
}

function mouseon(object,i,hand) {
	object.className = 'onmouseover';
	if (hand==true) object.style.cursor = "hand";
	var obj = document.getElementById("actie"+i);
	if (obj) obj.style.display = "block";
}

function set_vars(name, hidden, visible, splitter,bgcolor) { //dynamisch opbouwen tabel
	//vars
	if (!splitter) splitter = "<br>";
	table = document.getElementById('table_'+name);
	width = '100%';
	if (!bgcolor && bgcolor!=0) bgcolor = "efefef";
	
	if (table!=null) {
		//eerst alle rijen verwijderen van onder naar boven!
		if (table.rows) {
			for(i = table.rows.length-1; i >= 0;i--) {
				table.rows[i].deleteCell(0);
				table.deleteRow(i);
			}
		} else
			alert(table.type);
		//visible splitten.... 
		v = visible.split(splitter);
		if (v.length>0) { //als er inhoud is
			for(i=0;i<v.length-1;i++) { //doorlopen...
				table.insertRow(i); //rij aanmaken
				table.rows[i].style.width = width; //lengte opgeven..

				table.rows[i].insertCell(0); //cell aanmaken
				table.rows[i].cells[0].style.width = width; //lengte
				table.rows[i].cells[0].innerHTML = v[i]; //inhoud
				if (bgcolor!=0) table.rows[i].cells[0].style.background = (i % 2) ? "ffffff" : bgcolor; //kleur
			}
			table.style.display = "block";
		} else
			table.style.display = "none";
	} else alert('Fout set_vars');
	document.getElementById(name).value = hidden; //hidden zetten
}

function set_span(name,hidden,visible) { //vullen van een lege cell
	span = document.getElementById("div_" + name);
	
	if (span!=null) {
		span.innerHTML = visible;
		span.style.display = "block";
	}
	
	document.getElementById(name).value = hidden;
}

function check(form,name,bool,url) { //checkall script
	var state = 2;
	
	for(var i=0; i < document.images.length; i++) {
		if (document.images[i].id.indexOf(name)!=-1) {
			if (document.images[i].src.indexOf('checked')!=-1) {
				if (state!=2) {
					state = 1;
					break;
				}
			} else 
				state = 1;
		}
		
	}
	if (state==2) state = 0;
	for(var i=0; i < document.images.length; i++) {
		if (document.images[i].id.indexOf(name)!=-1) {
			swapCheckbox(document.images[i],document.images[i].id.substr(8),'1',document.images[i].src.substr(0,document.images[i].src.indexOf("images/")),state);

		}
	}
}

function add_row(table_name, contents, opmaak, del, repl) {
	//kijken of table bestaat
	//->Kijken of we hem moeten leggen (welk checken of rij een id heeft die we hem toekennen (voor headers enzo te bewaren)
	//Toevoegen nieuwe contents
	//->Checken of we een verwijderen knop erachter moeten plaatsen
	table = document.getElementById(table_name);

	if (table!=null && typeof(contents)=="object") {
		var i;
		if (repl==1) {
			//ok, replacen
			for(i=table.rows.length-1;i>=0;i--) {
				if (table.rows[i].id.indexOf(table_name)!=-1) table.deleteRow(i);
			}
		}
		
		i = table.rows.length;
		
		//toevoegen
		table.insertRow(i);
		table.rows[i].id = table_name+"_"+i;
		for(var a = 0; a <contents.length;a++) {
			table.rows[i].insertCell(a);
			if (opmaak[a]!=null) table.rows[i].cells[a].width = opmaak[a];
			table.rows[i].cells[a].innerHTML = contents[a];
		}
		//del?
		if (del==1) {
			table.rows[i].insertCell(contents.length);
			table.rows[i].cells[contents.length].innerHTML = "<a href=\"javascript:del_row('"+table_name+"',document.getElementById('"+table_name+"_"+i+"').rowIndex)\"><img src=\"images/button/delete.gif\" border=0></a>";
		}
	}
}
