var tabs = ["id_edu", "id_awa", "id_exp", "id_inf", "id_bhp", "id_otp"];
var ITEMS_PER_PAGE = 50; /*max number of items on the page*/
var pagination_on; /*flag*/
var pages; /*total pages*/
var authors; /*array with the authors*/

function getEl(id) { return document.getElementById(id); }

function addEl(p, el) { if (p && el) p.appendChild(el); }

function clEl(el) { if (el) { while (el.hasChildNodes()) { el.removeChild(el.firstChild); } } }

function crEl(type, d) {
	var el = document.createElement(type);
	if (d) {
		if (d.className)	{ el.className	= d.className;	}
		if (d.innerHTML)	{ el.innerHTML	= d.innerHTML;	}
		if (d.onclick)		{ el.onclick	= d.onclick;	}
		if (d.rel)			{ el.rel		= d.rel;		}
		if (d.children) {
			for (var i = 0; i < d.children.length; i++) { addEl(el, d.children[i]); }
		}
	}
	return el;
}
/*resets the search form*/
function resetSearchForm() {
	getEl('id_name').value = "";
	getEl('id_country').value = "";
	getEl('id_affiliation').value = "";
	
	getEl('formareas_forsearch').value = "";
	getEl('formarea_names_forsearch').value = "";
	getEl('areas_forsearch').innerHTML = "";
}
/*fired when tab is clicked*/
function tabClicked(el) {
	for (var i = 0; i < tabs.length; i++) {
		var tab = getEl(tabs[i] + '_tab');
		if  (tab == el) {
			tab.className = "tab sel";
			getEl(tabs[i]).className = "show";
		} else if (tab.className != "tab pas") {
			tab.className = "tab";
			getEl(tabs[i]).className = "hide";
		}
	}
}
/*fired when image failed to load*/
function noImg() {
	var div = getEl("id_imgCont");
	div.className = "noimage";
	div.innerHTML = "No Photo";
}
/*Activates the first available tab*/
function showFirstTab() {
	for (var i = 0; i < tabs.length; i++) {
		var tab = getEl(tabs[i] + '_tab');
		if (tab.className != "tab pas") {
			tab.className = "tab sel";
			getEl(tabs[i]).className = "show";
			return;
		}
	}
}

/*ALL page, pagination available*/
function showAllAuthors() {
	pagination_on = (authors.length > ITEMS_PER_PAGE);
	
	if (pagination_on) {
		pages = Math.ceil(authors.length / ITEMS_PER_PAGE);
		pageClicked(1);
	} else {
		getEl('id_PAG').className = 'pagination hide';
		buildDetailsTable(authors);
	}
}

function pageClicked(q) {
	var p = (q ? (isNaN(q) ? q.target.innerHTML : q) : window.event.srcElement.innerHTML);
	buildDetailsTable(getAuthorsForPage(p));
	redrawPagination(p);
}

/*returns the authors for the page*/
function redrawPagination(p) {
	var div = getEl('id_PAG');
	clEl(div);
	div.className = 'pagination show';
	addEl(div, crEl('span', {innerHTML:'Pages:'}));
	for (var i = 1; i <= pages; i++) {
		var el = crEl('span', {innerHTML:i, onclick:pageClicked});
		if (i == p) el.className = 'cur';
		addEl(div, el);
	}
}

/*returns the authors for the page*/
function getAuthorsForPage(p) {
	return authors.slice(ITEMS_PER_PAGE * (p - 1), (ITEMS_PER_PAGE * p > authors.length ? authors.length - 1 : ITEMS_PER_PAGE * p - 1));
}

/*fired when letter is clicked*/
function letterClicked(el) {
	var letter = el.innerHTML;
	if (letter == 'All') {
		showAllAuthors();
	} else {
		var tmp_authors = [];
		for (var i = 0; i < authors.length; i++) {
			if (authors[i].letter == letter) {
				tmp_authors.push(authors[i]);
			}
		}
		buildDetailsTable(tmp_authors);
		getEl('id_PAG').className = 'pagination hide';
	}
	var letters = getEl('id_AL').getElementsByTagName('span');
	
	for (var i = 0; i < letters.length; i++) {
		if (letters[i].innerHTML == letter) {
			letters[i].className = 'curr';
		} else if (letters[i].className != 'pass') {
			letters[i].className = '';
		}
	}
}

/*populates a table with the authors*/
function buildDetailsTable(tmp_authors) {
	var table = getEl('id_DT');
	clEl(table);
	var tbody = crEl('tbody');
	addEl(table, tbody);
	var header = crEl('tr', {children:[
		crEl('th', {className:'no_bR', innerHTML:'Last Name'}),
		crEl('th', {className:'bL', innerHTML:'<p>First Name</p>'}),
		crEl('th', {innerHTML:'Specialisation Area'}),
		crEl('th', {innerHTML:'Country'})
	]});
	addEl(tbody, header);
	
	for (var i = 0; i < tmp_authors.length; i++) {
		var row = crEl('tr', {onclick:authorClicked, children:[
			crEl('td', {rel:tmp_authors[i].author_id, className:'no_bR nowrap bold', innerHTML:tmp_authors[i].lastname}),
			crEl('td', {rel:tmp_authors[i].author_id, className:'nowrap', innerHTML:tmp_authors[i].firstname}),
			crEl('td', {rel:tmp_authors[i].author_id, innerHTML:tmp_authors[i].area_name}),
			crEl('td', {rel:tmp_authors[i].author_id, className:'nowrap', innerHTML:tmp_authors[i].country})
		]});
		if (i % 2 != 0) row.className = 'shade';
		addEl(tbody, row);
	}
}

/*fired when the author is clicked*/
function authorClicked(e) {
	var rel = e ? e.target.rel : window.event.srcElement.rel;
	window.open('/search/details/' + rel + '.html', 'Information','width=900,height=550,scrollbars=yes,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no') 
}

function showHideObj(node, id){
	if (node && node.className && getEl(id) && getEl(id).className){
		if (node.className == 'close'){
			node.className = 'open';
			getEl(id).className = 'show';
		} else {
			node.className = 'close';
			getEl(id).className = 'hide';
		}
	}
}

function selectAll(value){
	var inps = document.getElementsByClassName('a_chkboxes');
	var i;
	for(i = 0; i < inps.length; i++){
		inps[i].checked = value;
	}
}

function checkSelect(el){
	var arrValues = new Array;
	var counter = 0;
	for (var i=0; i < el.options.length; i++){
		if (el.options[i].selected && i != 0){
			arrValues.push(el.options[i].value);
			if (++counter > _MAX_AREAS_){
				el.options[i].selected = false;
			}
		}
	}
	if (counter > _MAX_AREAS_) alert ('You can select max '+_MAX_AREAS_+' research areas');
}
 
function checkSelectedSpecial(curbox){
	var selected_items = 0;/*here we will count how many checkboxes have been already checked*/
	var items = document.getElementsByClassName('a_chkboxes'); /*array of all input elements*/
	if (curbox.checked) {
		var i;
		for (i=0; i<items.length; i++){
			if (items[i].checked){
				selected_items++;
				if (selected_items > _MAX_AREAS_) {
					alert ('You can select max '+_MAX_AREAS_+' research areas');
					curbox.checked = false;
					break;
				}
			}
		}
	}
	return true;
}

function set_action(action){
	document.getElementById('action').value = action;
	/*if ( action == 'save'){
		var new_loc = 'http://'+_SERV_NAME_+'/specialist/add.html';
		location.replace(new_loc);
	}*/
}
/* if(document.getElementById('overlay')){
	document.getElementById('overlay').style.height = document.getElementById('main').clientHeight+'px';
}  */