var editor = null;
var x      = 0;
var oude_tab = 'tab_omschrijving';


function hasOptions(obj) {if (obj!=null && obj.options!=null) { return true; }	return false;}
function swapOptions(obj,i,j) {	var o = obj.options;	var i_selected = o[i].selected;	var j_selected = o[j].selected;	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);	o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}
function moveOptionUp(obj) {if (!hasOptions(obj)) { return; }	for (i=0; i<obj.options.length; i++) {if (obj.options[i].selected) {if (i != 0 && !obj.options[i-1].selected) { swapOptions(obj,i,i-1);	obj.options[i-1].selected = true;}}} volgordeclasses ();}
function moveOptionDown(obj) {if (!hasOptions(obj)) { return; }	for (i=obj.options.length-1; i>=0; i--) {if (obj.options[i].selected) {if (i != (obj.options.length-1) && ! obj.options[i+1].selected) { swapOptions(obj,i,i+1);	obj.options[i+1].selected = true; }}} volgordeclasses ();}
function submitForm(url, veld) { var	list = document.getElementById(veld); 	var velden = '';	for (i = 0; i <= list.options.length - 1; i++) { 	velden = velden + '_SPLIT_' + list.options[i].value;}	window.location.href = url + velden; }
function volgordeclasses () {
	list		= document.getElementById('volgorde');
	var	kleur	= 'nee';

	for (i = 0; i <= list.options.length - 1; i++) {
		if (kleur == 'ja') {
			list.options[i].className = 'een';
			kleur = 'nee';
		} else {
			list.options[i].className = 'twee';
			kleur = 'ja';
		}

		if (typeof(volgordetitel) == "object") {
			for (a=0;a < volgordetitel.length;a++) {
				if (volgordetitel[a] == list.options[i].value) {
						list.options[i].style.color = 'red';
				}
			}
		}
	}
}

function doorsturen (id) {
  popUp(webadres + 'doorsturen/' + id, 400, 300);
}

function advertentie_email (id) {
  popUp(webadres + 'emailen/' + id, 500, 400);
}

function popUp(URL, breed, hoog) {
//	day = new Date();
//	id = day.getTime();
	scherm = window.open(URL, 'popup', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=' + breed + ',height=' + hoog + ',left=150,top=150');
  if (window.focus) {scherm.focus()}
}

function popUpcontent(name, inhoud, breed, hoog) {
  var generator=window.open(name,'popup','height=' + hoog + ',width=' + breed + 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,left=150,top=150');
  generator.document.write('<html><head><title>' + name + '</title><link rel="stylesheet" href="/css/site_popup.css" type="text/css"/></head><body>');
  generator.document.write(inhoud);
  generator.document.write('</body></html>');
  generator.document.close();
}

function htmlarealaden () {
	if (typeof (velden) == "object") {
		for (x=0; x < velden.length; x++) { 
			editor = new HTMLArea(velden[x]);
			editor.config.pageStyle = "@import url(/css/templates/htmlarealayout.css);";


	   //editor.config.btnList["verwijzing"] = [  "Verwijzing naar database maken", webadres + "jsf/htmlarea/images/ed_database.gif", 0, function() { database_verwijzing ();}, false ];
     //editor.config.btnList["vraaginvoegen"] = [  "Vraag positie invoegen", webadres + "jsf/htmlarea/images/ed_vraag.gif", 0, function() { vraag_invoegen ();}, false ];
	   //editor.config.toolbar.push(["verwijzing", "vraaginvoegen"]);

		 editor.generate();
		}
	}
}

function putfocus (formInst, elementInst) {
	if (document.forms.length > 0) {
		document.forms[formInst].elements[elementInst].focus();
		document.forms[formInst].elements[elementInst].select();
	}
}

function parsepagina () {
	htmlarealaden ();
  
	if (document.getElementById('plaats')) {
	  if(document.getElementById('plaatsactie')) {
    	var options = {
    		script: "/php/verwerk/plaatsen.php?json=true&limit=6&",
    		varname:"input",
    		json:true,
    		shownoresults:false,
    		maxresults:6,
				callback: function (obj) { document.location.href = document.getElementById('plaatsactie').value + '/plaats=' + obj.value; }
    	};
		} else {
    	var options = {
    		script: "/php/verwerk/plaatsen.php?json=true&limit=6&",
    		varname:"input",
    		json:true,
    		shownoresults:false,
    		maxresults:6
    	};
			
      //aanmeldenLoad();			
		}
		
  	var as_json = new bsn.AutoSuggest('plaats', options);
	}	
	
	if (document.getElementById('land')) {
	  if(document.getElementById('landactie')) {
    	var options = {
    		script: "/php/verwerk/landen.php?json=true&limit=6&",
    		varname:"input",
    		json:true,
    		shownoresults:false,
    		maxresults:6,
				callback: function (obj) { document.location.href = document.getElementById('landactie').value + '/land=' + obj.value; }
    	};
		} else {
    	var options = {
    		script: "/php/verwerk/landen.php?json=true&limit=6&",
    		varname:"input",
    		json:true,
    		shownoresults:false,
    		maxresults:6
    	};	
		}
		
  	var as_json = new bsn.AutoSuggest('land', options);
	}		
	
	if (document.getElementById('object_land')) {
  	var options = {
  		script: "/php/verwerk/landen.php?json=true&limit=6&",
  		varname:"input",
  		json:true,
  		shownoresults:false,
  		maxresults:6
  	};
  	var as_json = new bsn.AutoSuggest('object_land', options);
	}		
	
	
	if (document.getElementById('object_plaats')) {
  	var options = {
  		script: "/php/verwerk/plaatsen.php?json=true&limit=6&",
  		varname:"input",
  		json:true,
  		shownoresults:false,
  		maxresults:6
  	};
  	var as_json = new bsn.AutoSuggest('object_plaats', options);
	}		
	
	if (document.getElementById('type1')) {
	  box1 = document.getElementById('type1');
	  box2 = document.getElementById('type2');
		
		box1.onclick = function () {
		  updateAanmelden(1);
		}
		
		box2.onclick = function () {
		  updateAanmelden(2);
		}		
		
		if (box1.checked) {
		  updateAanmelden(1);
		} else {
		  updateAanmelden(2);		
		}
	}
	
	if (document.getElementById('formulier_rij_1112')) {
	  box1 = document.getElementById('prijsa');
		box2 = document.getElementById('prijs_type1');
		box3 = document.getElementById('prijs_type2');
		box4 = document.getElementById('prijs_type3');
		
		box1.onkeydown = function () { updatePrijzen(1); };
		box2.onclick = function () { updatePrijzen(0); }
		box3.onclick = function () { updatePrijzen(0); }
		box4.onclick = function () { updatePrijzen(0); }
	}
	
	
var divs = document.getElementsByTagName("img"); 
    for (var i=0; i<divs.length; i++) {
      if (divs[i].className == 'item_img') {
			  if(divs[i].offsetWidth > 120) {
			    divs[i].style.width = 120;
				}
			}
		} 	
	
  oude_obj = document.getElementById('omschrijving_a');
}

window.onload = parsepagina;

function updatePrijzen(type) {
	  box1 = document.getElementById('prijsa');
		box2 = document.getElementById('prijs_type1');
		box3 = document.getElementById('prijs_type2');
		box4 = document.getElementById('prijs_type3');
		
	  if (type == 1) {
			box2.checked = false;
			box3.checked = false;
			box4.checked = false;
		} else {
			box1.value = '';
		}
}

function updateAanmelden(type) {
  box1 = document.getElementById('formulier_rij_903');
  box2 = document.getElementById('formulier_rij_902');
	
	if (type == 1) {
	  box1.style.display = 'none';
	  box2.style.display = 'none';
	} else {
	  box1.style.display = '';
	  box2.style.display = '';
	}
}

function in_array(my_array,my_value){
	caseSensitive = in_array.arguments.length<3?0:in_array.arguments[2];

	for(i=0;i<my_array.length;i++){
		if((caseSensitive==0?my_array[i]:my_array[i].toUpperCase())==(caseSensitive==0?my_value:my_value.toUpperCase())){
			return true;
		}
	}

	return false;
}

function radiovalideren(veld) {
  var box = document.getElementById(veld + '_veldaantal');
  var radio_choice = false;

  for (counter = 1; counter <= box.value; counter++) {
    waarde = document.getElementById(veld + counter);

    if (waarde.checked) {
      radio_choice = true; 
    }
  }

  if (!radio_choice) {
    return (false);
  }
  
  return (true);
}



function valideren (velden) {
  if (velden.length) {
    fouten = 0;
    
    for (x = 0;x < velden.length;x++) {
      if (document.getElementById(velden[x])) {
        box = document.getElementById(velden[x]);
        
        box.className = 'normaal';

        if (document.getElementById(velden[x] + '_veldtype')) {
          box2 = document.getElementById(velden[x] + '_veldtype');

					if (box2.value == 'driestuks') {
    			  boxa1 = document.getElementById(velden[x] + 'a');
    				boxa2 = document.getElementById(velden[x] + 'b');
    				boxa3 = document.getElementById(velden[x] + 'c');
    				
    				if (!boxa1.value || !boxa2.value || !boxa3.value) {
    				  box.className = 'fout';
    					fouten = 1;
    				}				
					} else {
            if (!radiovalideren(velden[x])) {
              box.className = 'fout';
              fouten = 1;
  					}
					}
        } else {             
          if ((!box.value || box.value == 0) && !box.selectedIndex && box.selectedIndex != 'undefined') {
            box.className = 'fout';
            fouten = 1;
          }
        }
      } else if (document.getElementById(velden[x] + 'a')) {
        box = document.getElementById(velden[x] + 'a');

        box.className = 'normaal';
        
        if (!box.value || (box.value == 0 && document.getElementById('mastercategorie').value != 8)) {
          box.className = 'fout';
          fouten = 1;
        }
      }
    }
    
    if (fouten) {
      alert('Niet alle velden zijn correct ingevuld.\nDe ontbrekende velden zijn met een rode rand aangegeven.');
    
      return false;
    } else {
      return true;
    }
  }
}

function voorwaarden_akkoord () {
  if (document.getElementById('voorwaarden').checked == true) {
    return confirm('Weet u zeker dat u deze advertentie wilt plaatsen?');
  } else {
    alert('U dient akkoord te gaan met de Algemene Voorwaarden voordat u een advertentie kunt plaatsen.');
    return false;    
  }
}

function plaats (waarde) {
  return URLEncode(waarde);
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function valideer_doorsturen () {
  box1 = document.getElementById('naam1');
  box2 = document.getElementById('naam2');
  box3 = document.getElementById('email');
  
  if (!box1.value || !box2.value || !box3.value) {
    alert('Niet alle verplichte velden zijn ingevuld.');
    return false;
  } else {
    return true;
  }
}

function actief_tabblad (obj, tab) {
  if (tab != oude_tab) {
    box = document.getElementById(tab);
    
    box.style.display = 'block';
    obj.className = 'actief';
    
    if (oude_tab) {
      box = document.getElementById(oude_tab);
      box.style.display = 'none';
      
      if (oude_obj) {
        oude_obj.className = 'normaal';
      }
    }
    
    oude_tab = tab;
    oude_obj = obj;
  }
}

function verander_foto (foto) {
  box = document.getElementById('fotodiv');
  box.innerHTML = '<img src="' + webadres + 'php/verwerk/bestanden.php?code=' + foto + '">';
}

function swfHeader () {
  document.write('<object type="application/x-shockwave-flash" data="' + webadres + 'swf/header.swf" width="100%" height="95"><param name="bgcolor" value="#f09300"><param name="movie" value="' + webadres + 'swf/header.swf"><embed src="' + webadres + 'swf/header.swf" quality="high" bgcolor="#f09300" width="100%" height="95" name="headerSWF" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}

function wachtwoordverandering () {
  box1 = document.getElementById('email');
  box2 = document.getElementById('wachtwoord1');
  box3 = document.getElementById('wachtwoord2');
  
  if (!box1.value) {
    alert('Vul eerst een e-mail adres in');  
  } else if (box2.value != box3.value) {
    alert('De ingevulde wachtwoorden zijn niet gelijk');
  } else if (box2.value.length == 0) {
    alert('Vul eerst een nieuw wachtwoord in');
  } else {
    return true;
  }  
  
  return false;
}

function veranderStatus(soort) {
  if (soort == 'TE KOOP') {
	  document.getElementById('woning_vraagprijs_titel').innerHTML = 'Koopprijs <span class="verplicht">*</span>';
	} else {
	  document.getElementById('woning_vraagprijs_titel').innerHTML = 'Huurprijs <span class="verplicht">*</span>';
	}
}


function scroll_down () {
  box = document.getElementById('fotothumbs');
	
	box.scrollTop = box.scrollTop + 2;
}

function scroll_up () {
  box = document.getElementById('fotothumbs');
	
	box.scrollTop = box.scrollTop - 2;
}

function set_scroll_down (type) {
  if (type) {
	 down_timer = setInterval('scroll_down()', 5); 
	} else {
	 clearInterval(down_timer);
	}
}

function set_scroll_up (type) {
  if (type) {
	 up_timer = setInterval('scroll_up()', 5); 
	} else {
	 clearInterval(up_timer);
	}
}

function updateBanner(nr, categorie, provincie) {
    var req = new XMLHttpRequest();
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
          //alert(req.responseText + 'test' + box1.value);
					box = document.getElementById('reclameblok' + nr);
					box.innerHTML = req.responseText;
        }
      };

      req.open('GET', webadres + 'php/verwerk/banner.php?categorie=' + categorie + '&provincie=' + provincie);
      req.send(null);
    }	
}

function veranderVraag(nr) {
  box = document.getElementById('antwoord' + nr);

	if (box.style.display == 'none') {
	  box.style.display = 'block';
	} else {
	  box.style.display = 'none';
	}
}