
var authorsArray = new Array();

/**
  repaints the authors html
 */
function repaint() {
	var s = "";
	var i = 0;
	var cnt = 1;
	while ( i < authorsArray.length ) {
		if (authorsArray[i] != null ) {
			getSetInfoFromIndex(i);
			s = s +
				"<table>"+
					"<tr>"+
						"<td class='textbold' width='20px;'> "+cnt+". </td>"+
							"<input type='hidden' name='author_rank_"+i+"' value='"+cnt+"'>" + 
						"<td>"+
							"<table border='0' cellspacing='2' cellpadding='3'>"+
								"<tr>"+
									"<td class='tableheader'>First Name (*)</td>"+
									"<td class='tablerow' style='border: 0px;'><input type='text' size='60' name='author_fname_"+i+"' value='"+authorsArray[i][0]+"' class='formtext'></td>"+
								"</tr>"+
								"<tr>"+
									"<td class='tableheader'>Last Name (*)</td>"+
									"<td class='tablerow' style='border: 0px;'><input type='text' size='60' name='author_lname_"+i+"' value='"+authorsArray[i][1]+"' class='formtext'></td>"+
								"</tr>"+
								"<tr>"+
									"<td class='tableheader'>Company or Institution (*)</td>"+
									"<td class='tablerow' style='border: 0px;'><input type='text' size='60' name='author_institution_"+i+"' value='"+authorsArray[i][2]+"' class='formtext'></td>"+
								"</tr>"+
								"<tr>"+
									"<td class='tableheader'>Email (*)</td>"+
									"<td class='tablerow' style='border: 0px;'><input type='text' size='60' name='author_email_"+i+"' value='"+authorsArray[i][3]+"' class='formtext'></td>"+
								"</tr>"+
								"<tr>"+
									"<td class='text' colspan='2'><hr size='1' noshade></td>"+
								"</tr>"+
							"</table>"+
						"</td>"+
						"<td class='text'>"+
							"<input type='button' value='Remove Author' class='formtext' onClick=\"removeAuthor("+i+");\">"+
						"</td>"+
					"</tr>"+
				"</table>" ;
							//"<a class='menutext' href='javascript:removeAuthor("+i+");'>Remove Author</a>"+
			++cnt;
		}
		++i;
	}

	//if (document.getElementById || document.all || document.layers) {
	if (document.getElementById) {
		x = document.getElementById('add_authors');
		x.innerHTML = '';
		x.innerHTML = s + getAddAuthorLink();
	}
}

function getDocumentValue(name) {
	return trim(document.forms[0][name].value);
}

function setDocumentValue(name, val) {
	document.forms[0][name].value = trim(val);
	return true;
}

/**
  returns an array with author info from the specified index
  and writes that info to the authorsArray
  This function needed by repaint()
 */
function getSetInfoFromIndex(index) {
	var info = null;
	if (document.forms[0]['author_fname_'+index]) {
		info = new Array(4);
		info[0] = trim(document.forms[0]['author_fname_'+index].value);
		info[1] = trim(document.forms[0]['author_lname_'+index].value);
		info[2] = trim(document.forms[0]['author_institution_'+index].value);
		info[3] = trim(document.forms[0]['author_email_'+index].value);
		authorsArray[index] = info;

		document.forms[0]['author_fname_'+index].value			= info[0];
		document.forms[0]['author_lname_'+index].value			= info[1];
		document.forms[0]['author_institution_'+index].value	= info[2];
		document.forms[0]['author_email_'+index].value			= info[3];
	}
	return info;
}

function trim(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}

function getAddAuthorLink() {
	return "" +
		"<table>"+
			"<tr>"+
				"<td class='textbold' width='20px;'> &nbsp; </td>"+
				"<td colspan='2' align='left' class='text' style='padding-left: 20px;'>" +
					"<input type='button' value='Add Author' class='formtext' onClick=\"addAuthor(null, null, null, null);\">"+
				"</td>"+
			"</tr>"+
		"</table>";
		//"<a class='menutext' href='javascript:addAuthor(null, null, null, null);'>Add Author</a>"+
}

/**
  adds an author to the authors array
 */
function addAuthor(fname, lname, institution, email) {
	var fn	 	= ( fname == null ) 		? '' : trim(fname);
	var ln 		= ( lname == null ) 		? '' : trim(lname);
	var inst 	= ( institution == null )	? '' : trim(institution);
	var em		= ( email == null )			? '' : trim(email);


	var i = authorsArray.length;
	authorsArray[i] 	= new Array(4);
	authorsArray[i][0] 	= fn;
	authorsArray[i][1] 	= ln;
	authorsArray[i][2] 	= inst;
	authorsArray[i][3] 	= em;

	repaint();
}

function removeAuthor(index) {
	authorsArray[index] = null;

	repaint();
}

/**
  if the contact person is also an author
*/
function addAuthorInfo(fname, lname, institution, email) {
	if ( document.forms[0]['isContactPersonAuthor'].checked ) {
		addAuthor(fname, lname, institution, email);
	} else {
		removeAuthorInfo(fname, lname, institution, email);
	}
	return true;
}

function removeAuthorInfo(fname, lname, institution, email) {
	var len = authorsArray.length;
	var i = 0;
	var fn	 	= trim(fname);
	var ln 		= trim(lname);
	var inst 	= trim(institution);
	var em		= trim(email);


	while( i < len ) {
		if ( authorsArray[i] != null ) {
			if ( authorsArray[i][0] == fn && authorsArray[i][1] == ln &&
					authorsArray[i][2] == inst && authorsArray[i][3] == em ) {
				authorsArray[i] = null;
			}
		}
		++i;
	}

	repaint();
	return true;
}

function checkForm(f, full) {
	var val = null;
	var i = 0;
	var j = 0;
	var k = 0;

	//title
	val = trim(f.title.value);
	if ( val == '' ) {
		alert("Paper Title required");
		return false;
	} else {
		f.title.value = val;
	}

	//topics
	k = parseInt(f.totalNumOfTopics.value, 10);
	var s = "";
	i = 0;
	while( i < k ) {
		if ( document.forms[0]['topic_id_'+i].checked ) {
			++j;
			s = s + i + " ";
		}
		++i;
	}
	if ( j == 0 && k > 0 ) {
		alert("Topics required");
		return false;
	} else {
		document.forms[0]['activeTopics'].value = s;
	}
	
	//keywords
	/*
	val = trim(f.keywords.value);
	if ( val == '' ) {
		alert("Keywords required");
		return false;
	} else {
		f.keywords.value = val;
	}
	*/


	//abstract
	val = trim(document.forms[0]['abstract'].value);
	if ( val == '' ) {
		alert("Abstract required");
		return false;
	} else {
		document.forms[0]['abstract'].value = val;
	}


	if ( full ) {
		if ( document.forms[0]['pc_author_conflict'].checked ) {
			if ( document.forms[0]['pc_members[]'].length > 2 && document.forms[0]['pc_members[]'].selectedIndex > 0 ) {
				//ok
			} else if ( document.forms[0]['pc_members[]'].length > 2 && document.forms[0]['pc_members[]'].selectedIndex < 0 ) {
				alert("You must select members from Program Committee");
				return false;
			}
		} else if ( ! document.forms[0]['pc_author_conflict'].checked && document.forms[0]['pc_members[]'].length > 2 && document.forms[0]['pc_members[]'].selectedIndex > 0 ) {
			alert("Since you selected members from program committee, the above checkbox has been checked");
			document.forms[0]['pc_author_conflict'].checked = true;
			return false;
		} else {
			//ok
		}
	}

	return checkAuthors(f);
	//return false;
}

function validEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)) {
		return true;
	} else {
		return false;
	}
}


function getAuthorRank(index) {
	return document.forms[0]['author_rank_'+index].value;
}

function isAuthorInfoValid(i) {
	fname = trim(document.forms[0]['author_fname_'+i].value);
	lname = trim(document.forms[0]['author_lname_'+i].value);
	institution = trim(document.forms[0]['author_institution_'+i].value);
	email = trim(document.forms[0]['author_email_'+i].value);

	if ( fname == '' && lname == '' && institution == '' && email == '' ) {
		//ok...
	} else if ( fname == '' ) {
		alert("First Name required for author #"+getAuthorRank(i));
		return false;
	} else if ( lname == '' ) {
		alert("Last Name required for author #"+getAuthorRank(i));
		return false;
	} else if ( institution == '' ) {
		alert("Institution required for author #"+getAuthorRank(i));
		return false;
	} else if ( email == '' ) {
		alert("Email required for author #"+getAuthorRank(i));
		return false;
	} else if ( ! validEmail(email) ) {
		alert("Invalid Email for author #"+getAuthorRank(i));
		return false;
	} else {
		//everything is fine ... put the trimmed values
		document.forms[0]['author_fname_'+i].value = fname;
		document.forms[0]['author_lname_'+i].value = lname;
		document.forms[0]['author_institution_'+i].value = institution;
		document.forms[0]['author_email_'+i].value = email;
	}

	return true;
}


function checkAuthors(f) {
	var i = 0 ;
	var fname = null;
	var lname = null;
	var institution = null;
	var email = null;

	for( i = 0 ; i < authorsArray.length ; ++i ) {
		if ( authorsArray[i] != null ) {
			if ( isAuthorInfoValid(i) ) {
				//ok...
			} else {
				return false;
			}
		}
	}

	setActiveAuthorNumbers();
	return true;
}

function setActiveAuthorNumbers() {
	var str = "";
	var i = 0;
	while ( i < authorsArray.length ) {
		if (authorsArray[i] != null && isAuthorInfoValid(i) && trim(document.forms[0]['author_fname_'+i].value) != '' ) {
			str = str + "_" + i + " ";
		}
		++i;
	}

	document.forms[0]['activeAthors'].value = trim(str);
}

