var symposDescMaxLength = 3500;
var symposSummaryMaxLength = 900;

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { 
	elm.addEventListener(evType, fn, useCapture); 
	return true; 
	}
	else if (elm.attachEvent) { 
	var r = elm.attachEvent('on' + evType, fn); 
	EventCache.add(elm, evType, fn);
	return r; 
	}
	else {
	elm['on' + evType] = fn;
	}
};

/**
 *
 */

function getEventSrc(e) {
	if (!e) e = window.event;

	if (e.originalTarget)
	return e.originalTarget;
	else if (e.srcElement)
	return e.srcElement;
};

/**
 *
 */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = 
		function() {
			oldonload();
			func();
		}
	}
};

/**
 *
 */

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();

function checkFieldValue(obj) 
{
	if (obj.value.substr(0,1) == '<') obj.value='';
}

function popImg(img, width, height, title)
{
	var x = (screen.width/2) - (width/2);
	var y = (screen.height/2) - (height/2);	
	popPhoto = window.open("", "ROOMGARDENPic", "width="+width+",height="+(height+45)+",resizable=no,status=no,scrollbars=no, left="+x+", top="+y+"");
	popPhoto.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>'+title+'</title></head><html><body style="margin:0;padding:0"><img src="'+img+'" width="'+width+'" height="'+height+'" alt="'+title+'" title="'+title+'"><p style="text-align:center;font-size:.7em"><a href="javascript:window.close()">Fenster schlie&szlig;en</a></p></body></html>');
}

function styleCallForPapersForm()
{
	if (document.getElementById('call-for-papers')) {
		document.getElementById('single').style.display = (document.getElementById('singleRadio').checked || document.getElementById('posterRadio').checked)? 'block' :'none';
		document.getElementById('sympos').style.display = document.getElementById('symposRadio').checked ? 'block' :'none';
	}
}

function styleSymposFields(obj)
{
	document.getElementById('sympos-fields').style.display = 'block';
	document.getElementById('list-attendees').style.display = (obj.checked && obj.value == 1) ? 'block' :'none';
	document.getElementById('co_presenter').style.display = (obj.checked && obj.value == 1) ? 'block' :'none';
	document.getElementById('sympos-hint').style.display = (obj.checked && obj.value == 1) ? 'block' :'none';
	document.getElementById('sympos-hint-contribute').style.display = (obj.checked && obj.value == 1) ? 'none' :'block';
	document.getElementById('sympos-contributor-name').style.display = (obj.checked && obj.value == 1) ? 'none' :'block';
	document.getElementById('sympos_paper_title').style.display = (obj.checked && obj.value == 1) ? 'none' :'block';
	document.getElementById('sympos-strands').style.display = (obj.checked && obj.value == 1) ? 'block' :'none';
	document.getElementById('sympos-organisator-option').style.display = (obj.checked && obj.value == 1) ? 'block' :'none';
	document.getElementById('sympos-not-organisator-option').style.display = (obj.checked && obj.value == 1) ? 'none' :'block';
	
	if (document.getElementsByTagName('BODY')[0].id == 'aila2008-en') {
		document.getElementById('sympos-proposal-label').innerHTML = (obj.checked && obj.value == 1) ? 'Your proposal' : 'Your abstract';
	}
	if (obj.checked && obj.value == 1) {
		symposDescMaxLength = 3500;
		symposSummaryMaxLength = 900;		
		calcCharsLeft('description_sympos', symposDescMaxLength);
		calcCharsLeft('summary_sympos', symposSummaryMaxLength);

	} else {
		symposDescMaxLength = 2500;
		symposSummaryMaxLength = 500;		
		calcCharsLeft('description_sympos', symposDescMaxLength);
		calcCharsLeft('summary_sympos', symposSummaryMaxLength);
	}
}

function calcCharsLeft(obj, max)
{
	if (!obj.name) {
		obj = document.getElementById(obj);
	}
	document.getElementById(obj.name+'_chars_left').value = (max-obj.value.length);
	if ((max-obj.value.length) < 0) {
		document.getElementById(obj.name+'_chars_left').style.color = 'red';
	} else {
		document.getElementById(obj.name+'_chars_left').style.color = 'black';
	}
}

addEvent(window, 'load', styleCallForPapersForm, false);