var spec_event_counter = 1;
//add another event constructor


function add_another_event()
{
	//document.getElementById('place_special_event').innerHTML = template_another_event;	
	var holder = document.getElementById( "place_special_event" );
	var table;
	var td;
	holder.appendChild( 
	 table = 
	 create("div", {id:"hide_div_"+spec_event_counter}, 
	   create( "table", {style:'position:relative; left:7px'}, 
		 create( "tbody", {},
		   create( "tr", {}, 
			 create( "td", {},  create("hr", {size:'1', width:'100%'}) ),
			 create( "td", {},  create("hr", {size:'1', width:'100%'}) )
		   ),
		   
		   		   
		   create( "tr", {}, 
			 create( "td", {},  "Beschrijving:" ),
			 create( "td", {}, create("textarea", {id:"se_description["+spec_event_counter+"]", name:"se_description["+spec_event_counter+"]", style:"width:300px; height:50px"}))
		   ),
		   
		   create( "tr", {}, 
			 create( "td", {},  "vanaf:" ),
			 create( "td", {}, create("input", {type:"text", id:"se_period_start["+spec_event_counter+"]", name:"se_period_start["+spec_event_counter+"]"}), create("img", {src:"img/calendar.jpg", id:"se_period_start_trigger_id["+spec_event_counter+"]", style:"position:relative; top:3px; left:3px"}), 
					 create("input", {type:"checkbox", value:"se_jaarrond["+spec_event_counter+"]", name:"se_jaarrond["+spec_event_counter+"]", id:"se_jaarrond["+spec_event_counter+"]", onClick:"specialEventJaarrondClicked("+spec_event_counter+")"}),
					 "jaarrond"   )
		   ),
		    
		   create( "tr", {}, 
			 create( "td", {},  "t/m:" ),
			 create( "td", {}, 
					 create("input", {type:"text", id:"se_period_end["+spec_event_counter+"]", name:"se_period_end["+spec_event_counter+"]"}), 
					 create("img", {src:"img/calendar.jpg", id:"se_period_end_trigger_id["+spec_event_counter+"]", style:"position:relative; top:3px; left:3px"})
					 )
		   ),
		   create( "tr", {}, 
			 create( "td", {},  "" ),
			 create( "td", {style:"padding-left:40px"}, create("a", {href:"javascript:hide_div("+spec_event_counter+")"}, "Verwijder"))
		   )
		 ) 
	  )
	 )
	);
	setup_js_calendar();
	spec_event_counter ++;
	document.getElementById('count_of_special_events').value = spec_event_counter;
}

function setup_js_calendar()
{
		Calendar.setup
		(
			{
				inputField : "se_period_start["+spec_event_counter+"]",
			ifFormat    : "%d/%m/%Y",
			align       : "Tl",
			singleClick : false,
			button      : "se_period_start_trigger_id["+spec_event_counter+"]"
			}
		);
		Calendar.setup
		(
			{
				inputField : "se_period_end["+spec_event_counter+"]",
			ifFormat    : "%d/%m/%Y",
			align       : "Tl",
			singleClick : false,
			button      : "se_period_end_trigger_id["+spec_event_counter+"]"
			}
		)
}


//create html
function create( name, attributes ) {
  var el = document.createElement( name );
  if ( typeof attributes == 'object' ) {
    for ( var i in attributes ) {
      el.setAttribute( i, attributes[i] );

      if ( i.toLowerCase() == 'class' ) {
        el.className = attributes[i];  // for IE compatibility

      } else if ( i.toLowerCase() == 'style' ) {
        el.style.cssText = attributes[i]; // for IE compatibility
      }
    }
  }
  for ( var i = 2; i<arguments.length; i++ ) {
    var val = arguments[i];
    if ( typeof val == 'string' ) { val = document.createTextNode( val ) };
    el.appendChild( val );
  }
  return el;
}

function loading_img(div)
{
	document.getElementById(div).innerHTML = 'Loading...';
}


//hide div when you want to remove additional special event
function hide_div(div)
{
	h_div = 'hide_div_'+div;

	//clear inserted data
	document.getElementById("se_description["+div+"]").value = '';
	document.getElementById("se_period_start["+div+"]").value = '';
	document.getElementById("se_period_end["+div+"]").value = '';
	
	document.getElementById(h_div).style.display = 'none';
}	

//check limit of entered data
function textCounter(field, countfield, maxlimit, output_id) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else document.getElementById(output_id).innerHTML = maxlimit - field.value.length;//countfield.value = maxlimit - field.value.length;
}


<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->


var dr = new Image(); dr.src = 'img/design/search/map/dr.gif';
var fle = new Image(); fle.src = 'img/design/search/map/fle.gif';
var fr = new Image(); fr.src = 'img/design/search/map/fr.gif';
var gl = new Image(); gl.src = 'img/design/search/map/gl.gif';
var gr = new Image(); gr.src = 'img/design/search/map/gr.gif';
var lb = new Image(); lb.src = 'img/design/search/map/lb.gif';
var nb = new Image(); nb.src = 'img/design/search/map/nb.gif';
var nh = new Image(); nh.src = 'img/design/search/map/nh.gif';
var ov = new Image(); ov.src = 'img/design/search/map/ov.gif';
var ut = new Image(); ut.src = 'img/design/search/map/ut.gif';
var zh = new Image(); zh.src = 'img/design/search/map/zh.gif';
var zl = new Image(); zl.src = 'img/design/search/map/zl.gif';

var star = new Image(); star.src = 'img/design/activity/star.gif';
var star_active = new Image(); star_active.src = 'img/design/activity/star_active.gif';
var star_active_half = new Image(); star_active_half.src = 'img/design/activity/star_active_half.gif';

function choose_province(map, id)
{
	document.getElementById(map).style.display = 'block';
	flag = true;
	if (chosen_provinces_ar.length == 0) chosen_provinces_ar.push(id);
	else
	{
		for (i=0; i < chosen_provinces_ar.length; i++)
		{
			if (chosen_provinces_ar[i] == id) 
			{
				flag = false;
				chosen_provinces_ar.splice(i,1);
				document.getElementById(map).style.display = 'none';
			}
		}
		if (flag == true) chosen_provinces_ar.push(id);
	}
	document.getElementById('chosen_province').value = chosen_provinces_ar;

}

var error_array = new Array();

error_array['title'] = 'Titel \n';
error_array['description'] = 'Beschrijving \n';
error_array['main_description'] = 'Beschrijving Hoofdactiviteit \n';
error_array['period_start'] = 'vanaf \n';
error_array['period_end'] = 't/m \n';
error_array['ia_name'] = 'Informatie organisatie -> Naam \n';
error_array['ia_street'] = 'Informatie organisatie -> Straat \n';
error_array['ia_house_num'] = 'Informatie organisatie -> Huisnummer \n';
error_array['ia_city'] = 'Informatie organisatie -> Plaats \n';
error_array['ia_postal_code'] = 'Informatie organisatie -> Postcode \n';
error_array['ia_province'] = 'Informatie organisatie -> Provincie \n';
error_array['ia_telephone'] = 'Informatie organisatie -> Telefoon \n';
error_array['ia_websiteurl'] = 'Informatie organisatie -> Website url \n';
error_array['ca_name'] = 'Contactinformatie -> Naam \n';
error_array['ca_street'] = 'Contactinformatie -> Straat \n';
error_array['ca_house_num'] = 'Contactinformatie -> Huisnummer \n';
error_array['ca_city'] = 'Contactinformatie -> Plaats \n';
error_array['ca_postal_code'] = 'Contactinformatie -> Postcode \n';
error_array['ca_telephone'] = 'Contactinformatie -> Telefoon \n';
error_array['ca_email'] = 'Contactinformatie -> E-mail \n';
error_array['foto'] = 'Foto \n';
error_array['type_of_activity'] = 'Soort activiteit \n';
error_array['category'] = 'Categorie \n';


function check_form_values()
{
	flag = true;
	//category = false;
	//type_of_activity = false;
	error = 'The volgende velden zijn verplicht of onjuist:\n\n';
	j = 0;
	with (document.submit_activity_form)
  	{
		while (j<document.submit_activity_form.length)
   		{   
	 		form_value = document.forms['submit_activity_form'].elements[j].value;	 
			field_name = document.forms['submit_activity_form'].elements[j].name;			
			//skip validation of special events
			var filter_se=/se_description\[/
			var filter_sps=/se_period_start\[/
			var filter_spe=/se_period_end\[/
			if (filter_se.test(field_name) == true || filter_sps.test(field_name) == true || filter_spe.test(field_name) == true) 
			{
				j++;
				continue;
			}	
			//if(field_name == 'se_description[0]' || field_name == 'se_period_start[0]' || field_name == 'se_period_end[]') alert('AAa');
			if (form_value == '' && field_name != 'se_description_count' && field_name != 'se_period_start' && field_name != 'se_period_end' && field_name != 'foto' && field_name != 'ca_email' && field_name != 'foto_source') 
			{
				//niet om datum zeuren als jaarrond is geselecteerd
				if (!
						(field_name == 'period_start' || field_name == 'period_end') 
						&& 
						(document.getElementById('main_jaarrond').checked)
					) {
					error = error + error_array[field_name];
					flag = false;
				}
			}
			
			
			//check correct email
			else if(field_name == 'ca_email')
			{
				//only check the field if it is not empty, since it is not obligated
				if(form_value != '')
				{
				var filter=/^.+@.+\..{2,4}$/
				  if (filter.test(form_value) == false) 
				  {
					 error = error + error_array[field_name];
					 flag = false;
				  }
				}
			}
			/*
			//check provinces checked
			if (field_name == 'category[]')
			{
				if (document.getElementById(category[1]).checked == true) category = true;
			}
			//check type of activities checked
			if (field_name == 'type_of_activity[]')
			{
				if (document.getElementById(field_name).checked == true) type_of_activity = true;
			}
			*/
			j++;
    	}
	}
	
/*
	if (category == false)
	{
		error = error + error_array['category'];
		flag = false;
	}

	if (type_of_activity == false)
	{
		error = error + error_array['type_of_activity'];
		flag = false;
	}
*/	

	if (flag == true) document.submit_activity_form.submit();
	else alert(error);
}


function copy_form_values()
{
	document.submit_activity_form.ca_street.value = document.submit_activity_form.ia_street.value;
	document.submit_activity_form.ca_house_num.value = document.submit_activity_form.ia_house_num.value;
	document.submit_activity_form.ca_city.value = document.submit_activity_form.ia_city.value;
	document.submit_activity_form.ca_postal_code.value = document.submit_activity_form.ia_postal_code.value;
	document.submit_activity_form.ca_telephone.value = document.submit_activity_form.ia_telephone.value;
}

function mark_all_holland()
{
	
	if (document.getElementById('mark_all').checked == true) chosen_provinces_ar.length = 0;

	choose_province('nh', 1);
	choose_province('zh', 2);
	choose_province('gr', 3);
	choose_province('fr', 4);
	choose_province('dr', 5);
	choose_province('ov', 6);
	choose_province('gl', 7);
	choose_province('nb', 8);
	choose_province('lb', 9);
	choose_province('zl', 10);
	choose_province('fle', 11);
	choose_province('ut', 12);
}

function popup(variable) 
{
    result = window.open('print.php?activity_id='+variable, 'myWindow', config='height=600, width=550, left=100, top=100, location=no, menubar=no, toolbar=no, scrollbars=yes, status, resizable, directories');
}
