//
// Hide e-mail addresses
//
// Sune.Karlsson@hhs.se 2003-04-25

function Obfuscate( b, a ) {
  
  document.write( '<a href="mailto:' + a + '&#64;' + b + '">' + a + '&#64;' + b + '</a>' );
  
}


//
// Simple form validation for S-WoPEc/S-WoBA forms
//
// Sune.Karlsson@hhs.se 2001-11-09
//

function MsgAndFocus( msg, formelement ) {

eval( formelement + ".focus()" );
alert( msg );

}

function CheckLogin(button) {

var lform = button.form;

if ( lform.s.selectedIndex <= 0 ) {
  alert('You must choose a series');
  lform.s.focus();
  return false;
  }

var coordinator = false;
var author = false;
var wpnum = false;
for ( var i = 0; i < lform.length; i++ ) {
  if ( lform.elements[i].name == 'wpcp' ) {
    coordinator = true;
    }
  if ( lform.elements[i].name == 'p' ) {
    author = true;
    }
  if ( lform.elements[i].name == 'n' ) {
    wpnum = true;
    }
  }
if ( author && coordinator ) {
  if ( lform.wpcp.value=="" && lform.p.value=="" ) {
    alert('You must supply a author or coordinator password');
    return false;
    }
  if ( lform.wpcp.value!=="" && lform.p.value!=="" ) {
    alert('You can not supply both author and coordinator passwords');
    return false;
    }
  }
else
if ( author ) {
  if ( lform.p.value=="" ) {
    alert('You must supply a password');
    lform.p.focus();
    return false;
    }
  }
else 
if ( coordinator ) {
  if ( lform.wpcp.value=="" ) {
    alert('You must supply a password');
    lform.wpcp.focus();
    return false;
    }
  }
if ( wpnum ) {
  if ( lform.n.value=="" ) {
    alert('You must supply a Working Paper number');
    lform.n.focus();
    return false;
    }
  }

}

//------------------------

function CheckConfirmPaper(button) {

if ( button.form.c.value == "" ) {
  alert('You must supply a code for the paper' );
  button.form.c.focus();
  return false;
  }

if ( button.name == 'C_accept' ) {
  if ( button.form.n.value == "" ) {
    alert('You must supply a number for the paper when adding it to the series' );
    button.form.n.focus();
    return false;
    }
  }
if ( button.name == 'C_reject' ) {
  if ( button.form.n.value !== "" ) {
    if ( confirm('You have given a number for the paper and you are rejecting it.\nDo you really want to REJECT the paper?' ) ) {
      return true;
      }
    else {
      button.form.n.focus();
      return false;
      }
    }
  }
}

//--------------------------

function SFPF(item) {

var cmd = "document.PaperForm." + item + ".focus()";
eval( cmd );

}

//--------------------------

function SFBF(item) {

var cmd = "document.BookForm." + item + ".focus()";
eval( cmd );

}

//--------------------------

function SFLF(item) {

var cmd = "document.LinkFileForm." + item + ".focus()";
eval( cmd );

}

//--------------------------

function SFUF(item) {

var cmd = "document.UploadFileForm." + item + ".focus()";
eval( cmd );

}

//--------------------------

function CheckPaperForm(button) {

var pf = button.form;

if ( pf.title.value == "" ) {
  alert( 'The Title is missing' );
  pf.title.focus();
  return false;
  }

if ( ( pf.F_Namn1.value == "" ) || ( pf.L_Namn1.value == "" ) ) {
  alert( 'First or last name missing for first author' );
  if ( pf.F_Namn1.value == "" ) {
    pf.F_Namn1.focus();
    }
  else {
    pf.L_Namn1.focus();
    }
  return false;
  }

var emailseen = false;
for ( var i = 1; i <= pf.OldNumAuth.value; i++ ) {
  var em = eval( "pf.Email" + i + ".value" );
  if ( em ) {
    emailseen = true;
    }
  }
for ( var i = 1; i <= pf.OldNumAuth.value; i++ ) {
  var fn = eval( "pf.F_Namn" + i + ".value" );
  var ln = eval( "pf.L_Namn" + i + ".value" );
  var em = eval( "pf.Email" + i + ".value" );
  if ( fn !== "" || ln !== "" || em !== "" ) {
    if ( fn == "" ) {
      alert( 'First name missing for author' );
      eval( "pf.F_Namn" + i + ".focus()" );
      return false;
      }
    if ( ln == "" ) {
      alert( 'Last name missing for author' );
      eval( "pf.L_Namn" + i + ".focus()" );
      return false;
      }
    if ( ( em == "" ) && ( !pf.noemail.checked && !emailseen ) ) {
      alert( "E-mail required for at least one author unless the 'No author has an email address' box is checked" );
      eval( "pf.Email" + i + ".focus()" );
      return false;
      }
    }
  }


if ( pf.abstrakt.value == "" && pf.DynNoAbs.value == "false" && ( !pf.NoAbs.checked || pf.NoAbs.value == "false" ) ) {
  alert( 'The Abstract is missing' );
  pf.abstrakt.focus();
  return false;
  }

if ( pf.key.value == "" && pf.DynNoKey.value == "false" && ( !pf.NoKey.checked || pf.NoKey.value == "false" ) ) {
  alert( 'No Keywords supplied' );
  pf.key.focus();
  return false;
  }

if ( pf.language ) {
  if ( pf.language.value == '--' ) {
    alert( 'No Language selected' );
    pf.language.focus();
    return false;
  }
}

if ( document.getElementById('Jel') ) {
  if ( pf.Jel.value == "" && pf.DynNoJel.value == "false" && !( pf.NoJel.checked || pf.NoJel.value == "false" ) ) {
    alert( 'No JEL-codes supplied' );
    pf.Jel.focus();
    return false;
  }
}

if ( document.getElementById('sid') ) {
  if ( pf.sid.value == "" ) {
    alert( 'The number of pages is missing' );
    pf.sid.focus();
    return false;
  }
}

if ( pf.date.value == "" ) {
  alert( 'The date is missing' );
  pf.date.focus();
  return false;
  }
  
if ( pf.wpcp.value !== "" && pf.n.value == "" ) {
  alert( 'The Working Paper number is missing' );
  pf.n.focus();
  return false;
  }

if ( pf.myemail.value == "" ) {
  alert( 'Your e-mail is missing' );
  pf.myemail.focus();
  return false;
  }

if ( pf.Updating.value !== "" ) {
  if ( !pf.revising[0].checked && !pf.revising[1].checked ) {
    alert( "Specify if this is a revised version or correcting the WP data" );
    pf.revising[0].focus();
    return false;
    }
  if ( pf.revising[0].checked && pf.revised.value == "" ) {
    alert( "Revision Date is missing" );
    pf.revised.focus();
    return false;
    }
  if ( pf.revising[1].checked && pf.revised.value !== "" ) {
    alert( "Revision date is ignored when correcting the bibliographic data" );
    pf.revised.focus();
    return false;
    }
  }

}


//--------------------------

function CheckBookForm(button) {

var pf = button.form;

if ( pf.title.value == "" ) {
  alert( 'The Title is missing' );
  pf.title.focus();
  return false;
  }

if ( !pf.authoreditor[0].checked && !pf.authoreditor[1].checked ) {
  alert( 'Specify if the book is authored or edited' );
  pf.authoreditor[0].focus();
  return false;
  }

if ( ( pf.F_Namn1.value == "" ) || ( pf.L_Namn1.value == "" ) ) {
  alert( 'First or last name missing for first author' );
  if ( pf.F_Namn1.value == "" ) {
    pf.F_Namn1.focus();
    }
  else {
    pf.L_Namn1.focus();
    }
  return false;
  }

var emailseen = false;
for ( var i = 1; i <= pf.OldNumAuth.value; i++ ) {
  var em = eval( "pf.Email" + i + ".value" );
  if ( em ) {
    emailseen = true;
    }
  }
for ( var i = 1; i <= pf.OldNumAuth.value; i++ ) {
  var fn = eval( "pf.F_Namn" + i + ".value" );
  var ln = eval( "pf.L_Namn" + i + ".value" );
  var em = eval( "pf.Email" + i + ".value" );
  if ( fn !== "" || ln !== "" || em !== "" ) {
    if ( fn == "" ) {
      alert( 'First name missing for author' );
      eval( "pf.F_Namn" + i + ".focus()" );
      return false;
      }
    if ( ln == "" ) {
      alert( 'Last name missing for author' );
      eval( "pf.L_Namn" + i + ".focus()" );
      return false;
      }
//    if ( ( em == "" ) && ( !pf.noemail.checked && !emailseen ) ) {
//      alert( "E-mail required for at least one author unless the 'No author has an email address' box is checked" );
//      eval( "pf.Email" + i + ".focus()" );
//      return false;
//      }
    }
  }


//if ( pf.abstrakt.value == "" && pf.DynNoAbs.value == "false" && ( !pf.NoAbs.checked || pf.NoAbs.value == "false" ) ) {
//  alert( 'The Abstract is missing' );
//  pf.abstrakt.focus();
//  return false;
//  }

//if ( pf.key.value == "" && pf.DynNoKey.value == "false" && ( !pf.NoKey.checked || pf.NoKey.value == "false" ) ) {
//  alert( 'No Keywords supplied' );
//  pf.key.focus();
//  return false;
//  }

if ( pf.language ) {
  if ( pf.language.value == '--' ) {
    alert( 'No Language selected' );
    pf.language.focus();
    return false;
  }
}

//if ( document.getElementById('Jel') ) {
//  if ( pf.Jel.value == "" && pf.DynNoJel.value == "false" && !( pf.NoJel.checked || pf.NoJel.value == "false" ) ) {
//    alert( 'No JEL-codes supplied' );
//    pf.Jel.focus();
//    return false;
//  }
//}

//if ( document.getElementById('sid') ) {
//  if ( pf.sid.value == "" ) {
//    alert( 'The number of pages is missing' );
//    pf.sid.focus();
//    return false;
//  }
//}

//if ( pf.date.value == "" ) {
//  alert( 'The date is missing' );
//  pf.date.focus();
//  return false;
//  }

if ( pf.year.value == "" ) {
  alert( 'The year is missing' );
  pf.year.focus();
  return false;
  }

if ( pf.provider.value == "" ) {
  alert( 'The publisher is missing' );
  pf.provider.focus();
  return false;
  }
  
if ( pf.wpcp.value !== "" && pf.n.value == "" ) {
  alert( 'The Working Paper number is missing' );
  pf.n.focus();
  return false;
  }

if ( pf.myemail.value == "" ) {
  alert( 'Your e-mail is missing' );
  pf.myemail.focus();
  return false;
  }

//if ( pf.Updating.value !== "" ) {
//  if ( !pf.revising[0].checked && !pf.revising[1].checked ) {
//    alert( "Specify if this is a revised version or correcting the WP data" );
//    pf.revising[0].focus();
//    return false;
//    }
//  if ( pf.revising[0].checked && pf.revised.value == "" ) {
//    alert( "Revision Date is missing" );
//    pf.revised.focus();
//    return false;
//    }
//  if ( pf.revising[1].checked && pf.revised.value !== "" ) {
//    alert( "Revision date is ignored when correcting the bibliographic data" );
//    pf.revised.focus();
//    return false;
//    }
//  }

}
//--------------------------

function CheckLinkForm(button) {

var of = button.form;

if ( of.linkfile.value == "" ) {
  alert( "No file to link!" );
  of.linkfile.focus();
  return false;
}

if ( of.filedesc.value == "" ) {
  alert( "Description of file is missing!" );
  of.filedesc.focus();
  return false;
}

if ( of.fileformat.value == "--" ) {
  alert( "File format is missing!" );
  of.fileformat.focus();
  return false;
}

var n = of.fileformat.length-1;
var t = eval( "of.fileformat.SelectedIndex" );


if ( of.fileotherformat.value == "" && of.fileformat[n].selected ) {
  alert( 'Specify an "Other" file format' );
  of.fileotherformat.focus();
  return false;
}

if ( of.fileotherformat.value != "" && !of.fileformat[n].selected ) {
  alert( 'Specify only one file format' );
  of.fileotherformat.focus();
  return false;
}

if ( document.getElementById('revised') ) {
  if ( !of.revising[0].checked && !of.revising[1].checked ) {
    alert( "Specify if the file is for a revised version or not" );
    of.revising[0].focus();
    return false;
    }
  if ( of.revising[0].checked && of.revised.value == "" ) {
    alert( "Revision Date is missing" );
    of.revised.focus();
    return false;
    }
  if ( of.revising[1].checked && of.revised.value !== "" ) {
    alert( "Revision date is ignored" );
    of.revised.focus();
    return false;
    }
  }
  
if ( of.myemail.value == "" ) {
  alert( 'Your e-mail is missing' );
  of.myemail.focus();
  return false;
  }

}

//--------------------------

function CheckUploadForm(button) {

var of = button.form;

if ( of.uploadfile.value == "" ) {
  alert( "No file to upload!" );
  of.uploadfile.focus();
  return false;
}

if ( of.filedesc.value == "" ) {
  alert( "Description of file is missing!" );
  of.filedesc.focus();
  return false;
}

var n = of.fileformat.length-1;
var t = eval( "of.fileformat.SelectedIndex" );

if ( of.fileformat.value == "--" ) {
  if ( of.fileotherformat.value == "" ) {
    alert( "File format is missing!" );
    of.fileformat.focus();
    return false;
  } else {
    of.fileformat[n].selected = true;
  }
}

if ( of.fileotherformat.value == "" && of.fileformat[n].selected ) {
  alert( 'Specify an "Other" file format' );
  of.fileotherformat.focus();
  return false;
}

if ( of.fileotherformat.value != "" && !of.fileformat[n].selected ) {
  alert( 'Specify only one file format' );
  of.fileotherformat.focus();
  return false;
}

if ( document.getElementById('revised') ) {
  if ( !of.revising[0].checked && !of.revising[1].checked ) {
    alert( "Specify if the file is for a revised version or not" );
    of.revising[0].focus();
    return false;
    }
  if ( of.revising[0].checked && of.revised.value == "" ) {
    alert( "Revision Date is missing" );
    of.revised.focus();
    return false;
    }
  if ( of.revising[1].checked && of.revised.value !== "" ) {
    alert( "Revision date is ignored" );
    of.revised.focus();
    return false;
    }
  }
  
if ( of.myemail.value == "" ) {
  alert( 'Your e-mail is missing' );
  of.myemail.focus();
  return false;
  }

}

//---------------------

function CheckEmail(button) {

if ( button.form.myemail.value == "" ) {
  alert( 'Your e-mail is missing' );
  button.form.myemail.focus();
  return false;
  }

}

/////////////////////////////////////////
// Turn on and off display of additional
// contact info for authors
/////////////////////////////////////////

function showhide(cid) {

document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="inline") ? "inline" : "none"

return false;

}

//////////////////////////////////////////
// Submit form for displaying file content
// when approving uploads
//////////////////////////////////////////

function ViewFile( fname ) {

document.ViewForm.lname.value = fname;
document.ViewForm.submit();

}

function CheckApproveForm(button) {

var of = button.form;

if ( !of.dispose[0].checked && !of.dispose[1].checked && !of.dispose[2].checked ) {
  alert( "Specify what to do with the file" );
  return false;
  }

}