///////////////
// variables //
///////////////
var s1, s2, s3, s4, s5, s6;
var win;
/////////////// 
// functions //
/////////////// 
// caches current sort order at onload
function cacheSort () {
  for (var i = 1; i <= 6; i++)
    eval('s' + i + '= document.forms.facultyForm.sort0' + i + '.value;');
}

// sort order enforcer
function sortOrder (inputNum) {
    var newVal =
      eval('document.forms.facultyForm.sort0' + inputNum + '.value');
    var oldVal = eval('s' + inputNum);
    newVal = parseInt(newVal);
    if (newVal == 0) {
      alert('Cannot have a sort order of 0!!!\nAttempting revision to previous value...');
      newVal = oldVal;
    }
    if (newVal < 7 && newVal > 0) {
      for (var i = 1; i <= 6; i++) {
        var tmp = eval('document.forms.facultyForm.sort0' + i + '.value');
        if (newVal > oldVal) {
          if (tmp <= newVal && tmp > oldVal && i != inputNum) {
            tmp--;
            eval('document.forms.facultyForm.sort0' + i + '.value = ' + tmp + ';');
          }
        } 
        else if (newVal < oldVal) {
          if (tmp >= newVal && tmp < oldVal  && i != inputNum) {
            tmp++;
            eval('document.forms.facultyForm.sort0' + i + '.value = ' + tmp + ';');
          }
        }
        else if (i == inputNum && newVal == oldVal)
          eval('document.forms.facultyForm.sort0' + i + '.value = ' + oldVal + ';'); 
      }
    }  
    else {
      for (var i = 1; i <= 6; i++)
        eval('document.forms.facultyForm.sort0' + i + '.value = s' + i + ';');
    }
    cacheSort();
}

// form required field checker
// mode a = add
function checkRequired(mode ) {
  var warning = '';
  var tmp = '';
  var dir = false;
  var blank = false;
  var tmp2 = '*';

  // set dir
  if (document.forms.facultyForm.faculty.value == 'Non-Faculty')
    dir = true;

  //if (false && dir)
  //  tmp2 = '&#9674;';

  // if it will be a blank profile limit accordingly
  if (document.forms.facultyForm.faculty.value == 'Non-Neuroscience Joint')
      blank = true;
  // check required fields
  if (!document.forms.facultyForm.portraitFile.value && mode == 'a' && !blank  &&
      document.forms.facultyForm.affiliation.value == 0)
    warning += "\t" + tmp2 + " Current Image\n";
  if (!document.forms.facultyForm.firstName.value.length)
    warning += "\t" + tmp2 + " First Name\n";
  if (!document.forms.facultyForm.lastName.value.length)
    warning += "\t" + tmp2 + " Last Name\n";
  if (document.forms.facultyForm.faculty.value == 0 && !dir)
    warning += "\t" + tmp2 + " Faculty\n";
  if (!document.forms.facultyForm.affiliation.value.length && dir)
    warning += "\t" + tmp2 + " Group\n";
  if (!document.forms.facultyForm.degree.value.length && !dir && !blank)
    warning += "\t" + tmp2 + " Degree(s)\n";
  if (!document.forms.facultyForm.title.value.length && !dir && !blank)
    warning += "\t" + tmp2 + " Title\n";
  if (document.forms.facultyForm.department.value == 0 && !dir)
    warning += "\t" + tmp2 + " Department\n";
  //if (document.forms.facultyForm.dbbs.value == 0 && !dir && !blank)
  //  warning += "\t" + tmp2 + " Member DBBS\n";
  //for (var i = 1; i < 7; i++)
  //  tmp += eval('document.forms.facultyForm.area' + i + '.value');
  //if (!tmp.length)
  //  warning += "\t" + tmp2 + " Research Area\n";
  if (!document.forms.facultyForm.dbbs[0].checked &&
     !document.forms.facultyForm.dbbs[1].checked && !dir && !blank)
    warning += "\t" + tmp2 + " Member DBBS\n";
  if (!document.forms.facultyForm.username.value.length)
    warning += "\t" + tmp2 + " Email\n";
  //    document.forms.facultyForm.username.value.match(/@/) == null))
//  if (!document.forms.facultyForm.researchTitle.value.length)
//    warning += "\t" + tmp2 + " Research Title\n";
  if (!document.forms.facultyForm.researchDescription.value.length && !dir && !blank)
    warning += "\t" + tmp2 + " Research Description\n";
	
  // check website
  tmp = document.forms.facultyForm.website.value;
  if (tmp.length &&
      tmp.match(/^http:\/\/\w+\.\w+/) == null &&
      tmp.match(/^ftp:\/\/\w+\.\w+/) == null &&
      tmp.match(/^https:\/\/\w+\.\w+/) == null)
        warning += "\t" + tmp2 + " Website (include prefix, e.g. 'http://')\n";
  // check more publications
  tmp = document.forms.facultyForm.morePublications.value;
  if (tmp.length &&
      tmp.match(/^http:\/\/\w+\.\w+/) == null &&
      tmp.match(/^ftp:\/\/\w+\.\w+/) == null &&
      tmp.match(/^https:\/\/\w+\.\w+/) == null && !dir)
        warning += "\t" + tmp2 + " More Publication (include prefix, e.g. 'http://')\n";
  // check pubmed
  for (var i = 1; i < 7; i++) {
    tmp = eval('document.forms.facultyForm.pubMed0' + i + '.value');
    if (tmp.length) {
      if (tmp.match(/^\s*\d{1,8}\s*$/) == null &&
          tmp.match(/^http:\/\/\w+\.\w+/) == null &&
          tmp.match(/^ftp:\/\/\w+\.\w+/) == null &&
          tmp.match(/^https:\/\/\w+\.\w+/) == null && !blank) {
        warning += "\t" + tmp2 + " Publication" + i + " PubMed ID or URL\n";
      }
    }
  }
  // check office phone and location
  if (dir && document.forms.facultyForm.officePhone.value == 0)
    warning += "\t" + tmp2 + " Office Phone\n";
  if (dir && document.forms.facultyForm.campusAddress.value == 0)
    warning += "\t" + tmp2 + " Office Location\n";
  if (warning.length)
    alert("Please check the following field(s) and try again:\n" + warning);
  else {
    // check sort order
    var error = false;
    for (var i = 1; i <= 6; i++) {
       eval('tmp = document.forms.facultyForm.sort0' + i + '.value;');
       if (tmp == 0) { error = true; }
    }
    if (error) { 
      alert('Cannot have a sort order of 0!!!\nReverting to default sort order values...');
      for (var i = 1; i <= 6; i++)
        eval('document.forms.facultyForm.sort0' + i + '.value = ' + i + ';');
    }
    document.forms.facultyForm.submit();
  }
}

// faculty deletion verification
function verify (id, fn, ln) {
  if (confirm('Delete ' + fn + ' ' + ln + '?')) {
    document.forms.deleteForm.i.value = id;
    document.forms.deleteForm.submit();
  }
}

function facultyPop(url) {
  MM_openBrWindow(url,'pop','scrollbars=yes,width=660,height=screen.availHeight,resizable=yes');
}
function imgPop(url,width,height,imgH) {
 if (!width) width = 500;
 if (!height) height = 500;
 wwidth =  40 + parseInt(width);
 wheight = 70 + parseInt(height);// + Math.floor((height-imgH)/2);
 features = 'scrollbars=no,width='+wwidth+',height='+wheight+',resizable=no';
 margin_top = Math.floor((height-imgH)/2) + 20
 win = window.open('','WUNeuroscience',features);
 win.resizeTo(wwidth,wheight);
 win.document.open();
 win.document.write('<html><head><title>WU Neuroscience</title>');
 win.document.write('<style>body{background-color:#000;padding:0');
 win.document.write('px;margin:0 auto;text-align:center;}');
 win.document.write('img{padding:0px;margin:0px;');
 win.document.write('margin-top:'+margin_top+'px;}');
 win.document.write('div.s{text-align:center;height:'+height+'px;width:'+width+'px;margin:0px;}');
 win.document.write('</style></head>');
 win.document.write('<body onload="window.focus()"><center><div class="s">');
 win.document.write('<img src="'+url+'" height="'+imgH+'" /></div></center></body></html>');
 win.document.close();
 win.focus();
}

