var win;
var numbers=new Array();
var assoc	=new Array();

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_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];}
}

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_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_openBrWindow(theURL,winName,features) { //v2.0
	win=window.open(theURL,winName,features);
	win.focus();
}


function act(id,action){
	var f=getElById(id);
	var a=getElById('action');
	a.value=action;
	f.submit();
}


function getElById(id){
	return(document.getElementById==null) ? (document.all==null ? null : document.all[id]) : document.getElementById(id);
}




function inputForm(ob,name){
	
	switch(name){
		case 'First':
		
		case 'Last':
			var val=ob.value;
		break;
		
		default:
			var address= new Array();
			var val='';
			address[0]='Department';
			address[1]='Institution';
			address[2]='Street';
			address[3]='City';
			address[4]='PostalCode';
			address[5]='Country';
			for(na in address){
				sepchar=(na==4 ? ' ' : ', ');
				val+=(val.length > 0 ?  sepchar : '')+getElById(address[na]).value
			}
		break;
	}
	getElById(name).value=val;
}

function fit_cells(){

	var tab=getElById('list');
	var Tabs=getElById('header');
	var setTab=Tabs.rows[0].cells;
	
	if(tab.rows.length > 0){
		s1=0;
		s2=0;
		row=tab.rows[0];
		cell=row.cells;
		
		for(i=0;i<cell.length-1;i++){
			var dv=document.defaultView;
			if(dv){
				st1=dv.getComputedStyle(cell[i],null);
				l1=Math.round(st1.getPropertyCSSValue('width').getFloatValue(CSSPrimitiveValue.CSS_PX));
				if((l1+s1-s2-1)>=0){
					setTab[i].width=l1+s1-s2-1;
					st2=document.defaultView.getComputedStyle(setTab[i],null);
					l2=Math.round(st2.getPropertyCSSValue('width').getFloatValue(CSSPrimitiveValue.CSS_PX));
					if(l1!=l2){
						setTab[i].width=Math.max(1,l1+l1-l2+s1-s2);
					}
					l2=Math.round(st2.getPropertyCSSValue('width').getFloatValue(CSSPrimitiveValue.CSS_PX))+(i==0 ? 0 : 1);
					s1+=l1;
					s2+=l2;	
				}
			}
			else if((l1=cell[i].clientWidth) > 0 ){
				if((l1+s1-s2-1)>=0){
					setTab[i].width=l1+s1-s2-1;
					l2=setTab[i].clientWidth+(i==0 ? 0 : 1);
					if(l1!=l2){
						setTab[i].width=Math.max(1,l1+l1-l2+s1-s2);
					}
					l2=setTab[i].clientWidth;
					s1+=l1;
					s2+=l2;
				}
			}
		}
	}
	
}

function make_style(style,idx){
	
	trow=getElById(idx);
	if(trow){
		switch(style){
			case'click':
				trow.style.background='#ffccff';
			break;
			case'over':
				if((!win || win.closed) && trow.style.background !='#ffccff' && trow.style.background !='#cccccc'){	
					trow.style.background='#ffffcc';
				}
			break;
			case'out':
				if((!win || win.closed) && trow.style.background !='#ffccff' && trow.style.background !='#cccccc'){
					trow.style.background='#ffffff';
				}
			break;
		}
	}
}

function ope_makStyle(idx){
	
	if(opener && opener.getElById){
		trow=opener.getElById(idx);
		if(trow.style.background !='#ffccff' && trow.style.background !='#cccccc'){	
			trow.style.background='#ccffcc';
		}
	}
}

function check_value(val,idx){
	
	f=document.forms[0].elements[val]
	v=f.value;
	n=f.name;
	var c;
	var va;
	var ch;
	if(v.length && !parseInt(v)){
		alert("Please enter only integer values >0\nor leave the field empty !!");
		f.value='';
		f.focus();
		return;
	}
	for(na in assoc){
		if(v==assoc[na]){
			c=na;
			va=assoc[na];
			break;
		}
	}
	if(!v.length){
		assoc[n]=false;
		numbers[v]=false;
	}
	else{
		assoc[n]=v;
		numbers[v]=v;
	}
// 	if(numbers[v] && c && n!=c){
// 		alert("Position '"+v+"' is already occupied !!");
// 		f.value='';
// 		f.focus();
// 		return;		
// 	}
	if(v.length){
		make_style('click',idx);
	}
}

function create_array(idx){
	val=getElById(idx).value;
	if(val){
		arr1=val.split(',');
		for(i=0;i<arr1.length;i++){
			trow=getElById(arr1[i])
			if(trow){
				trow.style.background='#cccccc';
			}
		}
	}
	pos=getElById('namefield').value
	if(pos){
		arr2=pos.split(',');
		for(i=0;i<arr2.length;i++){
			set=arr2[i].split('|');
			numbers[set[1]]=set[1];
			assoc[set[0]]=set[1]
		}
	}
}

function init(idx){
	fit_cells();
	create_array(idx);
	sel=location.href.split('=');
	if(sel[1]==2001){
		select_list('selid',false);
	}
}

function do_order(fname,orderrow){
	//alert(orderrow);
	getElById('order').value=orderrow;
	act(fname,'order');
}

function write_file(type,col,ord){
	
	if(type =='word'){
		if(confirm("Do you want to export the Abstractbook?\nOnly those records which have a position number\nwill be exported.")){
		exportframe.location.href="export.php?type="+type+'&col='+col+'&order='+ord;
		}
	}
	else if(type =='word_all'){
		if(confirm("Do you want to export all abstracts sorted as displayed into a MS Word document?")){
		exportframe.location.href="export.php?type="+type+'&col='+col+'&order='+ord;
		}
	}
	else{	
		if(confirm("Do you want to export the participant table sorted as displayed into a MS Excel sheet?")){
		exportframe.location.href="export.php?type="+type+'&col='+col+'&order='+ord;
		}
	}
}

function select_list(n,s){
	
	set=getElById(n);
	tmp=set.value
	undo=getElById('r'+tmp);
	
	if(s){
		set.value=s;
	}
	trow= s ? getElById('r'+s) :getElById('r'+tmp);
	if(trow){
		undo.style.background='#ffffff';
		trow.style.background='#ffff33';
		pos=trow.offsetTop;
		trow.scrollIntoView("true");
	}
}

function conf(text,id){

	set=getElById(id).value;
	text=text.replace(/%UID%/,'"'+set+'"');
	return confirm(text);
}


function doOffset(name,div){
	
	if(navigator.platform!='Win32'){
		window.setTimeout('doit("'+name+'","scrolli")',300); 
	}
	else{
		var headert=getElById(name);
		var d=getElById(div);
		headert.style.left=-d.scrollLeft;
	}
	//headert.style.left=-div.scrollLeft;
	//headert.style.width=-div.scrollLeft;
}

function doit(h,d){
	
	var headert=getElById(h);
	var div=getElById(d);
	headert.style.left=-div.scrollLeft;
}
