var _rwid = '';
var _rwcss = '';

function iecheck() {
  if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
      var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
      var iever = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) <= 7 );
  }
  return iever;
}


function collect(a,f){
  var n=[];
  for(var i=0;i<a.length;i++){
    var v=f(a[i]);
    if(v!=null)n.push(v)
  }

  return n
}


function serialize(f){
  var g=function(n){
    return f.getElementsByTagName(n)
  };
  var nv=function(e){
    if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value)
  };
  var i=collect(
    g('input'),
    function(i){
      if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)
    }
  );
  var s=collect(g('select'),nv);
  var t=collect(g('textarea'),nv);
  return i.concat(s).concat(t).join('&')
}

function embedWidget( ) {
	var form = '<div id="dl_form"><div id="ajaxinfo" ><p>Stel hier anoniem je vraag. Je krijgt binnen de vijf werkdagen een antwoord.<br />Lees voor je je vraag intypt even de <a href="http://www.druglijn.be/spelregels.html" target="_blank">spelregels</a>.</p></div><form id="mailform" method="post" ><input type="hidden"  name="via" id="via" value="'+_rwid+'" /><fieldset id="persdata"><legend>jouw gegevens</legend><div class="instructions">Het formulier is anoniem, je mag een schuilnaam opgeven. Je e-mailadres is wel verplicht. Als je wil, kan je op het internet een anoniem e-mailadres aanmaken.<br/></div><div ><label for="naam">naam:</label><input name="naam" id="naam" type="text" /></div><div ><label for="email">e-mailadres*:</label><input class="req" name="email" id="email" type="text" /></div><div><label for="email-repeat">herhaal e-mail*:</label><input class="req"  name="email-repeat" id="email-repeat" type="text" /></div><div><label for="leeftijd">leeftijd:</label> <input name="leeftijd" id="leeftijd" type="text" /></div><div><label for="geslacht">geslacht*:</label>  <select name="geslacht" id="geslacht" class="req"><option></option><option value="0">Man</option><option value="1">Vrouw</option> </select></div><div> <label for="regio">regio:</label> <select name="regio" id="regio" ><option value=""></option><option value="Antwerpen">Antwerpen</option><option value="Brussel">Brussel</option><option value="Limburg">Limburg</option><option value="Oost-Vlaanderen">Oost-Vlaanderen</option><option value="Vlaams-Brabant">Vlaams-Brabant</option><option value="West-Vlaanderen">West-Vlaanderen</option><option value="Walloni&euml;">Walloni&euml;</option><option value="Nederland">Nederland</option></select></div></fieldset><fieldset id="vragen"><legend>stel je vraag</legend><div><label for="onderwerp">onderwerp*:</label> <input class="req" name="onderwerp" id="onderwerp" type="text" /></div><div><label for="vraag">vraag*:</label> <textarea class="req" name="vraag" id="vraag" rows="10" cols="40"></textarea></div></fieldset><div id="required_label">* verplichte velden</div><div id="voorwaarden_label"><label for="voorwaarden"></label><input type="checkbox" class="req" name="voorwaarden" id="voorwaarden"  />Ik ga akkoord met de <a href="http://www.druglijn.be/spelregels.html">spelregels</a>*</div><div id="submit" > <input name="Submit" value="Verzend" type="submit" /></div></form>'

	if( _rwid == '' ) {
		form = '<div id="form" style="form-error">Contact widget kan niet worden embed. Geef aub de meegeleverde embed code op!</div>'
	}
	
	if( _rwcss != '' ) {
		dl_contact_widget.STYLESHEET = _rwcss;
	}

	var div = document.getElementById(dl_contact_widget.ROOT);
    div.innerHTML = form;  
    div.style.display = 'block';
	
	mini.form.errmsg = '<p class="form-error" >Je hebt niet al de verplichte velden ingevuld.</p>';
	mini.form.errclass = 'form-error';
	
	var script = document.createElement('script');
	script.text = "var f = document.getElementById( 'mailform' ); var a = document.getElementById( 'ajaxinfo' ); f.onsubmit = function(){ var valid = mini.form.validate(this); if(valid){dl_contact_widget.requestContent(this); }; window.scrollTo(0,0); return false; };";
	document.getElementsByTagName('head')[0].appendChild(script);

}

function serverResponse(data) {
	var div
	if( data == '300' ) {
		div = document.getElementById("dl_form");
		document.getElementById("ajaxinfo").style.display = 'none';
	} else {
		div = document.getElementById("ajaxinfo");
		document.getElementById(data).className=mini.form.errclass;
	}

    div.innerHTML = dl_contact_widget.MESSAGES[data];  
    div.style.display = 'block';
}

dl_contact_widget = 
{
  BASE_URL: 'http://www.druglijn.be/widgets/contact/',
  STYLESHEET: "form.css",
  FORM_JS: "form.js",
  FLASH_URL:'rar.swf',
  VALIDATION:'validation.js',
  ROOT: 'DL_Contact_Widget_Container',
  
  MESSAGES: {
	100:'<p class="form-error" >De opgegeven emailadressen komen niet overeen.</p>',
	200:'<p class="form-error" >Gelieve alle verplichte velden in te vullen.</p>',
	email:'<p class="form-error" >Het opgegeven e-mailadres is ongeldig.</p>',
	geslacht:'<p class="form-error" >Het opgegeven geslacht is ongeldig.</p>',
	onderwerp:'<p class="form-error" >Het opgegeven onderwerp is ongeldig.</p>',
	vraag:'<p class="form-error" >De opgegeven vraag is ongeldig.</p>',
	300:'<fieldset id="response"><legend>De DrugLijn</legend><p class="greeting">De druglijn heeft je e-mail goed ontvangen. Binnen de 5 werkdagen mag je een antwoord verwachten.</p></fieldset>'
  },
  
  initWidget: function() {
	  this.requestScriptElement( this.BASE_URL + this.FORM_JS );
	  this.requestStylesheet( this.BASE_URL + this.STYLESHEET);
	  this.buildContainer(this.ROOT);	  
  },

  buildContainer: function(id) {
	  document.write("<div id='" + id + "' style='display: none'></div>");
  },
  
  requestScriptElement: function( src ) {
    var script = document.createElement('script');
	script.type = "text/javascript";
    script.src = src;
    document.getElementsByTagName('head')[0].appendChild(script);
  },
  
  requestStylesheet: function(stylesheet_url) {
    stylesheet = document.createElement("link");
    stylesheet.rel = "stylesheet";
    stylesheet.type = "text/css";
    stylesheet.href = stylesheet_url;
    stylesheet.media = "all";
    document.lastChild.firstChild.appendChild(stylesheet);
  },
  
  requestContent: function( form ) {
	var data = '<div>even geduld, je gegevens worden verstuurd...<br/><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="1" height="1"><param name="movie" value="'+ this.BASE_URL + this.FLASH_URL +'"><param name="allowscriptaccess" value="always"/><param name="flashvars" value="remoteurl=http://www.druglijn.be/widgets/contact/sendinfo.asp&vars='+ encodeURIComponent( serialize( form ) )+'"><embed src="'+ this.BASE_URL + this.FLASH_URL +'" allowscriptaccess="always" flashvars="remoteurl=http://www.druglijn.be/widgets/contact/sendinfo.asp&vars='+ encodeURIComponent( serialize( form ) ) +'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="1" height="1"></embed></object></div>'
	var div = document.getElementById("ajaxinfo");
    div.innerHTML = data;  
    div.style.display = 'block';
  }

}

dl_contact_widget.initWidget();

