function fetchevents () {
	fetchevents_byfilter('all','all',"&editing=false")
}

function DoenAjax_fetchevtstep() {

}
//=============================================================
function fetchevents_byfilter(filterfield,filtervalue,extras) {
		// also add new event

		if (filtervalue!="new") $('#centreblock').html('<img src="throborange.gif">')
		// must stay here in case user is not logged in

		var poststr="filterfield="+filterfield+"&filtervalue="+filtervalue+extras
		//alert('fetching evts for:(poststr)<br>'+poststr)
    		$.ajax({
   			type: "POST",
   			url: "ajq_getevt.php",
   			data: poststr,
			dataType: "json",
   			success: function(jr){
				if ((filtervalue=="new") && (jr.respz.username==undefined)) {
					if (document.getElementById('tempthrob')) $('#tempthrob').remove()
					alert('You need to be logged in to add an event')
					return
				}

				 if ((filtervalue=="new") && ($.browser.msie)) {
					var formpack='<div><form id="formpack" action="editevt.php" method="post">'
					formpack+='<input type="hidden" name=record_id  value="new">'
					formpack+='<input type="hidden" name=editing value="true">'
					formpack+='<input type="hidden" name="formpost" value="yes">'
					formpack+='</form></div>'
/*alert ('new ie')*/
					$('#centreblock').html(formpack);
//					document.write(formpack)
//					window.setTimeout('delaysubmitaddevent()',4000)
					$('#formpack').submit()
					return
				}


				if ((filtervalue=="new") || (extras.indexOf("editing=true")>0) ) {
					 setquiet('quiet')
					 logthis("L30 to quiet: filtervalue="+filtervalue+" extras="+extras)
				}
				else {
					setquiet('go')
					 logthis("L34 to GO: filtervalue="+filtervalue+" extras="+extras)
				}

     			logthis( "fetched events: " + jr.respz.filtervalue);

				$('#centreblock').html(jr.respz.html);
				$('#centreblock').fadeIn('slow')  // in case it was hidden to show comments
				$('#commentcontainer').remove()   // in case it user jumped from comments directly here

				setupevtanchors() // except navanchors
				DoenAjax_logbannerevent('page','evt','evt-57')
     			logthis( "evtL34, after setevtanc" );
				// moved navanchoractive from here to setupevtanchors
   			}
 		});
}
function delaysubmitaddevent() {
	$('#formpack').submit()
}
//=============================================================
function DoenAjax_fetchevtdetail(evtno) { // eg for comment display
    		$.ajax({
   			type: "POST",
   			url: "ajq_getevt.php",
   			data: "evtno="+evtno,
			dataType: "json",
   			success: function(jr){
				$('#rvwfragment').html(jr.respz.html);
// TODO ??				$('#evtpreview')
   			}
 		});
}
//==================================================
function getevtpreview() {
if (validateform($('#addeventform'))!=0) {
		$('#tempthrob').remove()
		return
	}
	$('#evtpreview').remove()
	var poststr=$('#addeventform').formSerialize()
    		$.ajax({
   			type: "POST",
   			url: "ajq_getevtpreview.php",
   			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#tempthrob').remove()
//logthis('fetched preview'+jr.respz.record_id)
				if (document.getElementById('previewthisrec')) {
					logthis('#previewthisrec exists')
					msiehtmlbuffer=jr.respz.html  // global
				}
			   	else logthis('#previewthisrec not found')

 			 if ($.browser.msie) {
					$('#iehelper').html(msiehtmlbuffer).show()
					window.setTimeout('continuepreview()',1000)
/*					return	*/
				}
			else {
	 			$('#previewthisrec').after(jr.respz.html).show()
				}
//logthis('showing preview')
				$('#closepreview').unbind()
					.click(function(){
//logthis('closepreview clicked=>remove preview')
							$('#evtpreview').remove()
   								})
				$("#postthisrec").unbind().click(function(){
					addrec_evt('evts')
//logthis('post clicked=>adding preview')
					})
 				}
			});
}
//=============================================================
function continuepreview() {

 logthis('prview could continue here')

	$('#evtpreview').css('top','600px')
	scrollTo(0,800)
	return
	//	$('#previewthisrec').html(msiehtmlbuffer)



$('#iehelper').html(msiehtmlbuffer).show()
//	 			$('#previewthisrec').after(msiehtmlbuffer).show()
logthis('Dshowing preview')
				$('#closepreview').unbind()
					.click(function(){
logthis('Dclosepreview clicked=>remove preview')
							$('#evtpreview').remove()
   								})
				$("#postthisrec").unbind().click(function(){
					addrec_evt('evts')
logthis('Dpost clicked=>adding preview')
					})
}
//=============================================================
function setupevtanchors() {
//	$('.ajxupd').removeClass('ajxupd').unbind()
		 // posted as entire form

	$('#tempthrob').remove()
	$('#evtedit_back').unbind().click(function(){window.location="articles.php?wevt=all"})
		.addClass('clickable')
	$(".redigeer").each(function(i){ setClickable(this, i); })
	$("#previewthisrec").each(function(){
								$(this).unbind('click')
									   .click(function(){
//											$(this).after('<img id="tempthrob" src="tiny_red.gif">')
									   		getevtpreview()
										})
									 })

	$(".editthisrec").each(function(){
						$(this).unbind('click')
						   .click(function(){
								artno=$(this).parents('.specscontainer').attr('thisrec')
	 			 				if ($.browser.msie) {
									var formpack='<form id="formpack" action="editevt.php" method="post"><input name=record_id  value="'+artno+'"><input name=editing value="true"><input name="formpost" value="yes"></form>'
									document.write(formpack)
									$('#formpack').submit()
/*									window.location="editevt.php?record_id="+artno+"&editing=true"
									fetchevents_byfilter("record_id",artno,"&editing=true")*/
								}
								else {
									fetchevents_byfilter("record_id",artno,"&editing=true")
									}
								})
						})

	$(".deletethisrec").each(function(){
						$(this).unbind('click')
							   .click(function(){
								   	if (confirm('Are you sure you want to delete this record')) {
								   		record_id=$(this).parents('.specscontainer').attr('thisrec')
								   		deleterec_evt('evts',record_id)

									}
								})
						})

	$("#evtaddrec").unbind().click(function(){
					$(this).after('<img id="tempthrob" src="tiny_red.gif">')
					fetchevents_byfilter("record_id","new","&editing=true")
//					addrec_evt('evts')
					})

	$("#postthisrec").unbind().click(function(){
					alert('posting')
					addrec_evt('evts')
					})

	$('.calendarButton')
					.calendar({autoPopUp: 'button',
							buttonImageOnly: true,
							buttonImage: 'calendar.gif',
							buttonText: 'Calendar',
							minDate : new Date(2007, 1 - 1, 26),
							maxDate : new Date(2020, 1 - 1, 26),
							dateFormat : 'YMD/'
						})

//	$("select").not("#searchfieldselector").change(function(){  saveChanges_select(this)  })

	$("#searchfieldselector").unbind().change(function(){  setup_search2ndlevel()  })

	$("#go_search").unbind('click')
				.click(function(){
					var searchfield=$('#searchfieldselector').val()
					var searchvalue=$('#search_text').val()
					fetchevents_byfilter(searchfield,searchvalue,"")
					return false
					})

	$('.randc').each(function(){
				$(this).unbind("click")
					.click(function(){
						evtno=$(this).parents('.specscontainer').attr('thisrec')

						DoenAjax_fetchcomments_artno(evtno,'e')
						DoenAjax_fetchevtdetail(evtno)
					})
				})
	$('.mtof').unbind()
				.click(function(evt){
						$('.tellafriend').remove()
						getmailfriendform(this,evt.pageX,evt.pageY)
						})
					.addClass('clickable')

	$('.evtnavstriprow').find('.navanchoractive').each(function(){  // needs the search parameters sent via ajax
										// and returned in json data
				$(this).unbind("click")
					.click(function(){
						var matrix2 = splitHref ($(this).attr("href"))
						fetchevents_byfilter(matrix2['field'], matrix2['val'], '&step='+matrix2['step'])
/*										fetchevents_byfilter(jr.respz.filterfield,
							jr.respz.filtervalue,
							'&step='+$(this).attr("step"))*/
						return false //SEO cancel a-tag
							})
				})

}

function setup_search2ndlevel_inter() {
	alert('rerouted')
	setup_search2ndlevel()
}

function setup_search2ndlevel() {

	$('#search_text').next('.calendar_trigger').hide()

	var firstlevelval=$('#searchfieldselector').val()

	if ((firstlevelval=='evtgenre') ||(firstlevelval=='areas')){
    		$.ajax({
   			type: "POST",
   			url: "ajq_get2ndlvl.php",
			data: "what="+firstlevelval,
			dataType: "json",
   			success: function(jr){
   				$('#secondlevel').html(jr.respz.html).show()
                $('#search_text').val($('#secondlevel').children('select').val())  // default val into searchtext
				$('#secondlevel').children('select').addClass('evtsearchcontrol')
					.unbind()
					.change(function(){
						$('#search_text').val($(this).val())
//						$('#search_text').fadeIn('slow',function(){$('#search_text').hide()})
					})
				$('#search_text').hide()
   			}
 		});
	}
    else if (firstlevelval=='datetime') {
		$('#search_text').val('YYYY/MM/DD').attr("size","16")
		$('#search_text').show().unbind().one("click", function() {  $(this).val('') })
		//alert($('#search_text').parent('span').attr('class'))
		if ($('#search_text').parent('span').is('.calendar_wrap') == false) {
				$('#search_text')
					.calendar({autoPopUp: 'both',
							buttonImageOnly: true,
							buttonImage: 'calendar.gif',
							buttonText: 'Calendar',
							minDate : new Date(2007, 1 - 1, 26),
							maxDate : new Date(2020, 1 - 1, 26),
							dateFormat : 'YMD/'
						})
/*					.change(function(){saveChanges_direct(this) });*/

// 				try {
// 				if (popUpCal) {
// 					popUpCal.minDate = new Date(2007, 1 - 1, 26);
// 					popUpCal.maxDate = new Date(2020, 1 - 1, 26);
// 					popUpCal.dateFormat = 'YMD/';
// 					//popUpCal._goToday(0)
// 				}
// 				}
// 				catch (fout) {}

//			$('#search_text').find('.calendar_current').click()
		}
		else $('.calendar_trigger').show()
		$('#secondlevel').hide()
	}
	else {
		$('#secondlevel').hide()
		$('#search_text').show().unbind()
		 .attr("size","41")
		 .val('Enter Search Text Here')
         .one("click", function() {  $(this).val('') })
	}

}

//=============================================================
function addrec_evt(totable) {  // also addrec ... actions on success differ
	// specifically posts form called addeventform
	// also saves edited records
logthis('adding preview, pre validate')
	$('#closepreview').html('<img src="tiny_red.gif">')
	if (validateform($('#addeventform'))!=0) {
		$('#closepreview').click()
		return
	}
//logthis('adding preview, validated')
	var poststr=$('#addeventform').formSerialize()+"&table=evts"

    		$.ajax({
   			type: "POST",
   			url: "ajq_saveform.php",
			data: poststr ,  //with no record_id creates new record
			dataType: "json",
   			success: function(jr){
//logthis('added preview, abt to close click')
   				$('#closepreview').click()
				fetchevents_byfilter("record_id",jr.respz.record_id,"&editing=true")
				scrollTo(0,0)
   			}
 		});
}

//=============================================================
function deleterec_evt(table,record_id) {  // also addrec ... actions on success differ
    		$.ajax({
   			type: "POST",
   			url: "ajq_deleterec.php",
			data: "tablename="+table+"&record_id="+record_id ,  //with no record_id creates new record
			dataType: "json",
   			success: function(jr){
   				logthis("new rec no="+jr.respz.newid)
   				if (jr.respz.username==null) alert('You need to login to delete an event')
   				else if (jr.respz.feedback=="error") alert('Error: record not deleted')
   				else fetchevents ()
//				scrollTo(0,0)
   			}
 		});
}
