/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
  // Convert the number to a string
  var value_string = rounded_value.toString()
  // Locate the decimal point
  var decimal_location = value_string.indexOf(".")
  // Is there a decimal point?
  if (decimal_location == -1) {
    // If no, then all decimal places will be padded with 0s
    decimal_part_length = 0
    // If decimal_places is greater than zero, tack on a decimal point
    value_string += decimal_places > 0 ? "." : ""
  } else {
    // If yes, then only the extra decimal places will be padded with 0s
    decimal_part_length = value_string.length - decimal_location - 1
  }
  // Calculate the number of decimal places that need to be padded with 0s
  var pad_total = decimal_places - decimal_part_length
  if (pad_total > 0) {
    // Pad the string with 0s
    for (var counter = 1; counter <= pad_total; counter++) 
      value_string += "0"
  }
  return value_string
}

/************** global variables ****************/
var miniwindow
var secondwindow
var currentwindowurl = "";
var currentsecondwindowurl = "";

/************** window functions ****************/

// function is used to load up generic popup window
function openwindow(htmlpage,winx,winy) {
  var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=yes,resizable=yes";
  miniwindow = window.open(htmlpage,'miniwindow',wininit);
  miniwindow.focus();
}

function opensecondwindow(htmlpage,winx,winy) {
  var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=yes,resizable=no";
  secondwindow = window.open(htmlpage,'secondwindow',wininit);
  secondwindow.focus();
}

// function is used to close down all pop-up windows when exiting page
function closewindow() {
  if (miniwindow) {
    if (!(miniwindow.closed)) miniwindow.close();
  }
  if (secondwindow) {
    if (!(secondwindow.closed)) secondwindow.close();
  }
}

function closesecondwindow() {
  if (secondwindow) {
    if (!(secondwindow.closed)) secondwindow.close();
  }
}

/************** validation functions ****************/

// function is used for confirming delete
function confirmdelete(formname,elementname,elementvalue) {
  accept = window.confirm("Are you sure you want to delete?");
  if (accept == true) {  
	document.forms[formname].elements[elementname].value = elementvalue;
	document.forms[formname].submit();
  }
  else
	return;
}

// function is used for confirming review
function confirmReview(formname,elementname,elementvalue) {
  accept = window.confirm("Once you have submitted your review to the chair, you will not be able to\nedit it.  Are you sure you want to submit to chair?");
  if (accept == true) {  
	document.forms[formname].elements[elementname].value = elementvalue;
	document.forms[formname].submit();
  }
  else
	return;
}

// function is used for confirming to create new coauthor
function confirmRequest(msg,confirmurl,declineurl) {
  accept = window.confirm(msg);
  if (accept) {  
	document.location.href = confirmurl;
  } else {
    document.location.href = declineurl;
  }
}

// function is used for changing a value and submitting form
function changeValue(form,elementname,value,reload) {
  form.elements[elementname].value = value;
  if (reload) form.submit();
}

/************** helper functions ****************/

// function is used for confirming delete
function generateNewAbstract(myform) {
  accept = window.confirm("Are you an author for this abstract?");
  if (accept == true) {
	myform.elements["is_author"].value = "Yes";
	accept = window.confirm("Are you a presenting author?");
	if (accept == true)
      myform.elements["auth_presenter"].value = "Yes";
	else
      myform.elements["auth_presenter"].value = "No";
  } else {
	myform.elements["is_author"].value = "No";
  }
  myform.submit();
}

// function is used for adding new affiliate
function actionAffiliate(form,element,htmlpage,winx,winy) {
  if (element.value.toLowerCase() == "new_affiliate") {
	element.value = "";
	openwindow(htmlpage,winx,winy);
  } else
    changeValue(form,"func","refresh",true);
}

// function is used for calculating score
function calculateScore(myform,count) {
  total = 0;
  for (i=1;i<=count;i++) {
	if (myform.elements["rev_criterion"+i+"_score"].value != '') {
	  score = parseFloat(myform.elements["rev_criterion"+i+"_score"].value) * parseFloat(myform.elements["rev_criterion"+i+"_weight"].value);
	} else
	  score = 0;
	total += score;
	document.getElementById("rev_criterion"+i+"_calc").innerHTML = round_decimals(score,2);
  }
  document.getElementById("rev_grade").innerHTML = round_decimals(total,2);
}

// function is used on reviews listing page for search filter
var cur_theme = "";

function displaySubjects(theme) {
  if (cur_theme != "" && document.getElementById(cur_theme + "_list") != null) {
	document.getElementById(cur_theme + "_list").name = "";
	document.getElementById(cur_theme + "_list").value = "";
    document.getElementById(cur_theme + "_list").style.display = "none";
  }
  if (theme != "" && document.getElementById(theme + "_list") != null) {
	document.getElementById("subjects").style.display = "";
    document.getElementById(theme + "_list").name = "abs_subject";
	document.getElementById(theme + "_list").style.display = "";
  } else
    document.getElementById("subjects").style.display = "none";
  cur_theme = theme;
}

function selectBox(form,index,highlight) {
  index_box = form.elements["highlight_id"];
  cur_box = form.elements["abs_id_" + index];
  
  if (index_box.value != "") {
	form.elements["abs_id_" + index_box.value].style.background = "";
  }

  if (highlight) {
    index_box.value = index;
	cur_box.style.background = "#CCD3E5";
  } else {
	index_box.value = "";
	cur_box.style.background = "";
  }
}

/*
function setSession(formname,id,name) {
  index = opener.document.forms[formname].elements["highlight_id"].value;
  if (index != "") {
	opener.document.forms[formname].elements["abs_id_" + index].value = id;
	opener.document.getElementById("abs_title_" + index).innerHTML = name;
  } else
	alert("Please select Session ID.");
}
*/

cur_index = "";
function highlight(form,index) {
  if (cur_index != "") {
	form.elements["abs_id_" + cur_index].style.background = "";
  }

  form.elements["abs_id_" + index].style.background = "#CCD3E5";
  cur_index = index;
}

function copyAbstract(formname,id,name) {
  opener.document.forms[formname].elements["cur_abs_id"].value = id;
  opener.document.forms[formname].elements["cur_abs_title"].value = name;
}

function pasteAbstract(form,index) {
  highlight(form,index);

  if (form.elements["cur_abs_id"].value != "") {
	form.elements["abs_id_" + index].value = form.elements["cur_abs_id"].value;
    document.getElementById("abs_title_" + index).innerHTML = form.elements["cur_abs_title"].value;
  }
}

function displayObject(id) {
  obj = document.getElementById(id);
  if (obj != null) {
	if (obj.style.display == "")
	  obj.style.display = "none";
	else
	  obj.style.display = "";
  }
}

function displayNewAuthor(form,id) {
  form.elements['con_id'].value = "";
  displayObject(id);
}

function selectNewAuthor(form,id) {
  obj = document.getElementById(id);
  if (obj != null) {
	if (form.elements['con_id'].value != "")
	  obj.style.display = "none";
  }
}
