var http		= getHTTPObject();
var isWorking	= false;

function createPopup(picture, height, directory){
	if(!height) height = 442;
	if(!directory) directory = 'disney_groot';
	window.open('http://www.ververexport.nl/website/popup.php?picture='+picture + '&directory=' + directory,'picture','width=610,height='+height+',scrollbars=no,toolbar=no,location=no');
	return false;
}

function createPopup2(picture, height, text){
	if(!height) height = 442;
	window.open('http://www.ververexport.nl/website/popup.php?picture='+picture+'&text='+text,'picture','width=610,height='+height+',scrollbars=no,toolbar=no,location=no');
	return false;
}

function getHTTPObject() {
  var xmlhttp;
/*@cc_on
  @if (@_jscript_version >= 5)
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function resetCultivars(){
	setVal('geslacht',0);
	setVal('indeling',0);
	setVal('primaire_kleur',0);
	setVal('secundaire_kleur',0);
	setVal('kleurvorm',0);
	initCultivars('');
}

function initCultivars(current){
	setVal('current',current);
	var val = getVal(current);
	setVal(current+'_sel',val);
	if (!isWorking) {
		var geslacht = getVal('geslacht');
		var indeling = getVal('indeling');
		var kleur1 = getVal('primaire_kleur');
		var kleur2 = getVal('secundaire_kleur');
		var kleurvorm = getVal('kleurvorm');
		var taalcode = getVal('taalcode');
		http.open("GET", 'search.php?g='+geslacht+'&i='+indeling+'&k1='+kleur1+'&k2='+kleur2+'&k='+kleurvorm+'&l='+taalcode);
		isWorking = true;
		http.onreadystatechange = putCultivars;
		http.send(null);
	}
}

function putCultivars(){
	if (http.readyState == 4) {
		isWorking = false;
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			try {
				if(getVal('current') != 'geslacht' || getVal('geslacht') == 0){
					var selectObj = document.forms[0].geslacht;
					buildOptionBox(xmlDocument, selectObj,'geslacht','geslacht');
				}

				if(getVal('current') != 'indeling' || getVal('indeling') == 0){
					var selectObj = document.forms[0].indeling;
					buildOptionBox(xmlDocument, selectObj,'indeling','indeling');
				}

				if(getVal('current') != 'kleur1' || getVal('kleur1') == 0){
					var selectObj = document.forms[0].primaire_kleur;
					buildOptionBox(xmlDocument, selectObj,'kleur1','primaire_kleur');
				}

				if(getVal('current') != 'kleur2' || getVal('kleur2') == 0){
					var selectObj = document.forms[0].secundaire_kleur;
					buildOptionBox(xmlDocument, selectObj,'kleur2','secundaire_kleur');
				}

				if(getVal('current') != 'kleurvorm' || getVal('kleurvorm') == 0){
					var selectObj = document.forms[0].kleurvorm;
					buildOptionBox(xmlDocument, selectObj,'kleurvorm','kleurvorm');
				}
			} catch (e) {
				alert(e);
			}
		}
	}
}

function buildOptionBox(xmlDocument, selectObj, xmlfield, formfield){
	var alles = getVal('alles');
	with (selectObj) {
		var sel_item = getVal(formfield+'_sel');
		var getal = xmlDocument.getElementsByTagName(xmlfield).item(0).childNodes.length;
		selectObj.options.length = '';
		var option = new Option();
		option.text = alles;
		option.value = '0';
		selectObj.options[0] = option;
		for(var i=1; i < getal+1; i++){
			var option = new Option();
			option.text = xmlDocument.getElementsByTagName(xmlfield).item(0).childNodes[i-1].firstChild.data;
			option.value = xmlDocument.getElementsByTagName(xmlfield).item(0).childNodes[i-1].tagName.substr(3);
			selectObj.options[i] = option;
		}
		//if(getal==1) selectObj.selectedIndex = 1;
		if(sel_item) selectObj.value = sel_item;
	}
}

function getVal(id){
	return document.getElementById(id).value;
}

function setVal(id,val){
	document.getElementById(id).value = val;
}