window.onload=indexonload;
url="/Bergen2008/scicomm/ajaxserver.php";

var abstractid;
var scicommid;

function indexonload(event){
	event=winevent(event);
	var edits=$$('.a_comment');
	edits.each( function(edit){
		edit.onclick=onclick_comment;
	});
	var edits=$$('.a_edit');
	edits.each( function(edit){
		edit.onclick=onclick_edit;
	});
	$$('.uploadabstract').each( function(upload){
		upload.onclick=onclick_upload;
	});
	// todo - find a cookie parser
	var cookie=document.cookie;
	var cookies=cookie.split('; '); // splits into each key-value par
	//alert(cookies.inspect());
	cookies.each( function (cook){
		try{
		cookpat=cook.split('='); // splits the par key=value
		if (cookpat[0]=='scicommid'){scicommid=(cookpat[1]);} // just looking for the scicommid
		if (cookpat[0]=='username'){$('p_login').innerHTML='Logged in as '+cookpat[1].replace('+',' ');}
		}catch(e){}
	});
	var menuheight=$('menu').clientHeight;
	if ($('content').clientHeight<menuheight){
		//alert(menuheight);
		$('content').style.height=menuheight;
	}

}



function onclick_upload(event){
	if (!scicommid){alert('log in');}	
	event=winevent(event);
	var id=(Event.element(event).id);
	abstractid=id.substring(2);
	dlgupload=new Dialog(null,{modal: true, title: 'Upload abstract ('+abstractid+')', width: 400, height: 150});
	var html='<p>Upload abstract</p>';
	html+='<label for="upload">Datafil :</label>';
	html+='<input id="upload" name="datafile" type="file" /><br />';
	html+='<input type="button" id="bt_ok_upload" value="OK" />';
	html+='<input type="button" id="bt_cancel_upload" value="Cancel" />';
	dlgupload.setContent(html);
	$('bt_cancel_upload').onclick=function(event){dlgupload.close(event);};
	$('bt_ok_upload').onclick=upload;
	return false;
}


function upload(event){
	huh();
}

function positiondialog(event){
	x=event.pointerX;
	y=event.pointerY;
}

function onclick_edit(event){
	if (!scicommid){alert('log in');}	
	event=winevent(event);
	var id=(Event.element(event).id);
	abstractid=id.substring(3);
	title=$('abstracttitle_'+abstractid).innerHTML;
	dlgedit=new Dialog(null,{modal: true, title: 'Edit ('+abstractid+')', width: 300, height: 290});
	var html='<p>Title:<br /><textarea id="ta_title_edit" cols="30" rows="5"></textarea></p>';
	html+='<p>Session : <select id="sel_session_edit" style="width: 15em;"></select></p>';
	html+='<p>Type : <select id="sel_prestype_edit" style="width: 15em;"></select></p>';
	html+='<input type="button" id="bt_ok_edit" value="OK" />';
	html+='<input type="button" id="bt_cancel_edit" value="Cancel" />';
	dlgedit.setContent(html);
	var boxes=new Array('session','prestype');
	boxes=$A(boxes);
	boxes.each(function(box){
		q=$H({	
			post: 'edit',
			absid: abstractid,
			a: 'fetchdata',
			table: box
		});
		statusmsg('Fetching data');
		var myAjax=new Ajax.Request(url,
		{
			method:'get',
			parameters: q,
			onComplete: hHR_populatedialog
		});
	});
	$('ta_title_edit').value=title;
	$('bt_cancel_edit').onclick=function(event){dlgedit.close(event);};
	$('bt_ok_edit').onclick=storeedit;
	return false;
}

function storeedit(event){
	param=$H({id: abstractid
		,a: 'storeedit'
		,abstracttitle: $('ta_title_edit').value
		,sessionid: $('sel_session_edit').value
		,prestypeid: $('sel_prestype_edit').value
		});
	ajax=new Ajax.Request(url,
		{method:'get',
		 parameters: param.toQueryString(),
		 onComplete: hHR_confirm});
}
function onclick_comment(event){
	event=winevent(event);
	var id=(Event.element(event).id);
	abstractid=id.substring(3);
	dlgedit=new Dialog(null,{modal: true, title: 'Add Comment ('+abstractid+')', width: 300, height: 320});
	
	var html='<p>Score : <select id="sel_rating_comment"></select>';
	html+='<p>Session : <select id="sel_session_comment" style="width: 15em;"></select></p>';
	html+='<p>Type : <select id="sel_prestype_comment" style="width: 15em;"></select></p>';
	html+='<p>Comment : <br /><textarea id="ta_comment"></textarea></p>';
	html+='<input type="button" id="bt_ok_comment" value="OK" />';
	html+='<input type="button" id="bt_cancel_comment" value="Cancel" />';
	dlgedit.setContent(html);
	var boxes=new Array('session','prestype');
	boxes=$A(boxes);
	boxes.each(function(box){
		q={	
			post: 'comment',
			a: 'fetchdata',
			table: box
		};
		statusmsg('Fetching data');
		var myAjax=new Ajax.Request(url,
		{
			method:'get',
			parameters: q,
			onComplete: hHR_populatedialog
		});
	});
	var sel_sc=$('sel_rating_comment');
	sel_sc.options[0]=new Option(' ','---');
	for (var i=1; i<=10; i++){
		sel_sc.options[i]=new Option(i,i);
	}
	$('bt_cancel_comment').onclick=function(event){dlgedit.close(event);};
	$('bt_ok_comment').onclick=store_comment;
	return false;
}

function store_comment(event){
	param=$H({abstractid: abstractid
		,a: 'storecomment'
		,scicommid: scicommid
		,comment: $('ta_comment').value
		,rating: $('sel_rating_comment').value
		,sessionid: $('sel_session_comment').value
		,prestypeid: $('sel_prestype_comment').value
		});
	ajax=new Ajax.Request(url,
		{method:'get',
		 parameters: param.toQueryString(),
		 onComplete: hHR_confirm});
}

function hHR_confirm(response){
	var xmlDocument=response.responseXML;
	if(xmlDocument.getElementsByTagName('error')[0].firstChild.data != '0'){
		handleServerError(xmlDocument.getElementsByTagName('error')[0].firstChild.data);
	}else{
		var alerttext=xmlDocument.getElementsByTagName('responsetext')[0].firstChild.data || 'OK';
		alert(alerttext);
		dlgedit.close();
	}
 }