

function setFocus() {
   document.getElementById("manuscript_number").focus();
}



// window.onload = postLoad;

// function postLoad(){
//   if (document.formStart){
//      document.formStart.manuscript_number.focus();
//   }
//   if (document.rightsStart){
//      document.rightsStart.ipaddress.focus();
//   }

//}

function validate (form) {

   var alertstr = '';
   var invalid = 0;
   var addresstype = '';

   if ( form.elements['invoice_address_type'][0] ){
      for (var loop = 0; loop < form.elements['invoice_address_type'].length; loop++) {
         if ( form.elements['invoice_address_type'][loop].checked ){
             addresstype = form.elements['invoice_address_type'][loop].value;
         }
      }

   } else {

      if ( form.elements['invoice_address_type'].checked ){
          addresstype = form.elements['invoice_address_type'].value;
      }

   }

   if ( (! addresstype.match(/\d/)) ){

       // standard text, hidden, password, or textarea box
       var invoice_surname = form.elements['invoice_surname'].value;
       if ( (! invoice_surname.match(/^\S[\S\s]+$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Surname" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_initials = form.elements['invoice_initials'].value;
       if ( (! invoice_initials.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Initials" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_institution = form.elements['invoice_institution'].value;
       if ( (! invoice_institution.match(/^\S[\S\s]+$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Institution" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_department = form.elements['invoice_department'].value;
       if ( (! invoice_department.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Department" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_street = form.elements['invoice_street'].value;
       if ( (! invoice_street.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Street" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_town = form.elements['invoice_town'].value;
       if ( (! invoice_town.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Town" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_state = form.elements['invoice_state'].value;
       if ( (! invoice_state.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice State" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_postal_code = form.elements['invoice_postal_code'].value;
       if ( (! invoice_postal_code.match(/^\S[\S\s]*$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Postal Code" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_country = form.elements['invoice_country'].value;
       if ( (! invoice_country.match(/^[A-Z][A-Z]$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Country" field\n';
           invalid++;
       }
       // standard text, hidden, password, or textarea box
       var invoice_email = form.elements['invoice_email'].value;
       if ( (! invoice_email.match(/^[\w\-\+\._]+\@[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA-Z]+$/)) ) {
           alertstr += '- You must enter a valid value for the "Invoice Email" field\n';
           invalid++;
       }

    }

    // standard text, hidden, password, or textarea box
    var manuscript_number = form.elements['manuscript_number'].value;
    if ( (! manuscript_number.match(/^\S[\S\s]+$/)) ) {
        alertstr += '- You must enter a valid value for the "Manuscript Number" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var surname = form.elements['surname'].value;
    if ( (! surname.match(/^\S[\S\s]+$/)) ) {
        alertstr += '- You must enter a valid value for the "Surname" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var initials = form.elements['initials'].value;
    if ( (! initials.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "Initials" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var institution = form.elements['institution'].value;
    if ( (! institution.match(/^\S[\S\s]+$/)) ) {
        alertstr += '- You must enter a valid value for the "Institution" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var department = form.elements['department'].value;
    if ( (! department.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "Department" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var street = form.elements['street'].value;
    if ( (! street.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "Street" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var town = form.elements['town'].value;
    if ( (! town.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "Town" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var state = form.elements['state'].value;
    if ( (! state.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "State" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var postal_code = form.elements['postal_code'].value;
    if ( (! postal_code.match(/^\S[\S\s]*$/)) ) {
        alertstr += '- You must enter a valid value for the "Postal Code" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var country = form.elements['country'].value;
    if ( (! country.match(/^[A-Z][A-Z]$/)) ) {
        alertstr += '- You must enter a valid value for the "Country" field\n';
        invalid++;
    }
    // standard text, hidden, password, or textarea box
    var email = form.elements['email'].value;
    if ( (! email.match(/^[\w\-\+\._]+\@[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA-Z]+$/)) ) {
        alertstr += '- You must enter a valid value for the "Email" field\n';
        invalid++;
    }

    

    // corresponding author grant funding
    var corresponding_author_grant_funding_declaration = '';
    for (var loop = 0; loop < form.elements['corresponding_author_grant_funding'].length; loop++) {
        var corresponding_author_grant_funding = form.elements['corresponding_author_grant_funding'][loop].value;
        if ( corresponding_author_grant_funding.match(/\S[\S\s]+$/)) {
             corresponding_author_grant_funding_declaration = corresponding_author_grant_funding;
        }
    }

    if ( (( ! corresponding_author_grant_funding_declaration && corresponding_author_grant_funding_declaration != 0) || corresponding_author_grant_funding_declaration === "")){
        alertstr += '- You must enter something in the "Corresponding Author Grant Funding" fields\n';
        invalid++;
    }

    

    // standard text, hidden, password, or textarea box
    var open_access_publication_funding = form.elements['open_access_publication_funding'].value;
    if ( (! open_access_publication_funding.match(/\S[\S\s]+$/)) ) {
        alertstr += '- You must enter a valid value for the "Open Access Publication Funding" field\n';
        invalid++;
    }
    // radio group or checkboxes
    var author_declaration = '';

    if (form.elements['author_declaration'][0]) {
        for (var loop = 0; loop < form.elements['author_declaration'].length; loop++) {
            if (form.elements['author_declaration'][loop].checked) {
                author_declaration = form.elements['author_declaration'][loop].value;
            }
        }
    } else {
        if (form.elements['author_declaration'].checked) {
            author_declaration = form.elements['author_declaration'].value;
        }
    }
    if ( ((! author_declaration && author_declaration != 0) || author_declaration === "")) {
        alertstr += '- You must choose an option for the "Author Declaration" field\n';
        invalid++;
    }
    if (invalid > 0 || alertstr != '') {
        if (! invalid) invalid = 'The following';   // catch for programmer error
        alert(''+invalid+' error(s) were encountered with your submission:'+'\n\n'+alertstr+'\n'+'Please correct these fields and try again.');
        // reset counters
        alertstr = '';
        invalid  = 0;
        return false;
    }

    return true;  // all checked ok

}


