function setFocus(f,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  document.forms[frm].elements[f].focus();
}
function testEmail(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var a = document.forms[frm].elements[f].value;
  var at = a.indexOf("@")
  var name = a.substring(0, at)
  var isp = a.substring(at + 1, a.length)
  var dot = a.lastIndexOf(".")
  if (at == -1 || at == 0 || name == "" || isp == "" || dot == -1 || dot == (a.length - 1)) {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  } else {
    return true;
  }
}
function testText(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  if(document.forms[frm].elements[f].value != ""){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  }
}
function testHidden(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  if(document.forms[frm].elements[f].value != ""){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
  }
}
function testArea(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  if(document.forms[frm].elements[f].value.length > 0){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  }
}
function testSelect(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var a = document.forms[frm].elements[f];
  if(a.selectedIndex > 0){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  }
}
function testDate(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var a = document.forms[frm].elements[f].value;
  var dateObj = new Date(a);
  if(a == ""){
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  } else if(isNaN(dateObj)){
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
    setFocus(f,d);
  } else {
    return true;
  }
}
function testRadio(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var radioName = eval('document.forms[' + frm + '].' + f);
  var str = "";
  for(i = 0; i < radioName.length; i++){
    if(radioName[i].checked == true){
      str = "yes";
	break;
    } else {
      str = "no";
    }
  }
  if(str == "yes"){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
  }
}
function testCheck(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var checkName = eval('document.forms[' + frm + '].' + f);
  var str = "";
  for(i = 0; i < checkName.length; i++){
    if(checkName[i].checked == true){
      str = "yes";
	break;
    } else {
      str = "no";
    }
  }
  if(str == "yes"){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
  }
}
function testSingleCheck(f,l,d){
  if(d.length > 0){
    var frm = d;
  } else {
    var frm = 0;
  }
  var checkName = eval('document.forms[' + frm + '].' + f);
  if(checkName.checked == true){
    return true;
  } else {
    if(l == "en"){
      alert('Please enter all mandatory fields');
    } else {
      alert('Compléter tous les champs obligatoires');
    }
  }
}
//************************************
function validateAdvert(lang){
  if( testText('FullName',lang,'') && testText('CompanyName',lang,'') && testSelect('Country',lang,'') && testText('Telephone',lang,'') && testSelect('TypeOfOrganisation',lang,'') && testText('AdvertisementText',lang,'') && testSingleCheck('AcceptTerms',lang,'') ){
    document.forms[0].submit();
  }
}
function validateRegistration(lang){
  if( testText('Name',lang,'') && testText('Address1',lang,'') && testText('City',lang,'') && testText('Postal_Code',lang,'') && testSelect('Country',lang,'') && testText('Telephone',lang,'') && testEmail('EmailAddress',lang,'') && testText('YearEstablished',lang,'') && testSelect('TypeOfOrganisation',lang,'') && testSelect('TypeOfBusiness',lang,'') && testText('NoOfEmployees',lang,'') && testCheck('WorkingLanguages',lang,'') && testText('SalesContactName',lang,'') && testText('SalesContactAddress1',lang,'') && testText('SalesContactPostalCode',lang,'') && testText('SalesContactTelephone',lang,'') && testText('ServiceContactName',lang,'') && testText('ServiceContactAddress1',lang,'') && testText('ServiceContactPostalCode',lang,'') && testText('ServiceContactTelephone',lang,'') && testText('GrossSales1',lang,'') && testText('GrossSales2',lang,'') && testText('GrossSales3',lang,'') && testText('ExportSales1',lang,'') && testText('ExportSales2',lang,'') && testText('ExportSales3',lang,'') && testText('BankDetailsName',lang,'') && testText('BankDetailsAddress1',lang,'') && testText('BankDetailsAccountNo',lang,'') && testText('BankDetailsAccountName',lang,'') && testCheck('BankDetailsCurrency',lang,'') && testSelect('BankDetailsTermsOfPayment',lang,'') && testHidden('ServicesOffered',lang,'') ){
    document.forms[0].submit();
  }
}
function validateBookTour(lang){
  var doc = document.forms[0];
  if( testText('NameOfGroup',lang,'') && testText('PersonInCharge',lang,'') && testText('Address1',lang,'') && testText('Telephone',lang,'') && testDate('DateOfVisit',lang,'') && testText('TimeofArrival',lang,'') && testText('TimeAvailableForVisit',lang,'') && testText('PreferedLanguage',lang,'') && testText('Adults',lang,'') && testText('SchoolChildren',lang,'') && testText('UniversityStudents',lang,'') && testText('SeniorCitizens',lang,'') && testText('DisabledPersons',lang,'') ){
    if( (doc.Adults.value == "0") && (doc.SchoolChildren.value == "0") && (doc.UniversityStudents.value == "0") && (doc.SeniorCitizens.value == "0") && (doc.DisabledPersons.value == "0") ){
      if(lang == "en"){
        alert('Please enter all mandatory fields');
      } else {
        alert('Compléter tous les champs obligatoires');
      }
    } else {
      doc.submit();
    }
  }
}
function validateRadio(lang){
  if( testDate('Date',lang,'') && testText('Time',lang,'') && testText('Media',lang,'') && testText('RequestedBy',lang,'') && testArea('Transmission',lang,'') ){
    document.forms[0].submit();
  }
}
function validateTelevision(lang){
  if( testRadio('Type',lang,'') && testDate('Date',lang,'') && testText('Timing',lang,'') && testText('Broadcaster',lang,'') && testText('Subject',lang,'') && testText('Telephone',lang,'') && testText('ProducerName',lang,'') && testText('ProducerTelephone',lang,'') && testText('BillingAddress1',lang,'') ){
    document.forms[0].submit();
  }
}
function validateLibrarian(lang,fm){
  if( testSelect('Enquiry',lang,'0') && testText('Subject',lang,'0') && testArea('Message',lang,'0') && testEmail('EmailAddress',lang,'0') && testText('Name',lang,'0') ){
    document.forms[fm].Enquiry.value = document.forms[0].Enquiry.value;
    document.forms[fm].Subject.value = document.forms[0].Subject.value;
    document.forms[fm].Message.value = document.forms[0].Message.value;
    document.forms[fm].EmailAddress.value = document.forms[0].EmailAddress.value;
    document.forms[fm].Name.value = document.forms[0].Name.value;
    document.forms[fm].Address1.value = document.forms[0].Address1.value;
    document.forms[fm].Address2.value = document.forms[0].Address2.value;
    document.forms[fm].Address3.value = document.forms[0].Address3.value;
    document.forms[fm].Address4.value = document.forms[0].Address4.value;
    document.forms[fm].submit();
  }
}
function validateDisarmament(lang){
  if( testSelect('MeetingType',lang,'') && testText('Agency',lang,'') && testRadio('ParticipantGender',lang,'') && testText('FamilyName',lang,'') && testText('FirstName',lang,'') && testCheck('ParticipationCategory',lang,'') && testText('OriginID',lang,'') && testText('IDNumber',lang,'') && testText('IDValidity',lang,'') && testEmail('EmailAddress',lang,'') ){
    document.forms[0].submit();
  }
}
function validateDG_GLS1(lang){
  if( testRadio('ParticipantGender',lang,'') && testText('FamilyName',lang,'') && testText('FirstName',lang,'') && testDate('DOB',lang,'') && testRadio('Affiliation',lang,'') && testText('Other',lang,'') && testText('OriginID',lang,'') && testText('IDNumber',lang,'') && testText('IDValidity',lang,'') && testEmail('EmailAddress',lang,'') ){
    document.forms[0].submit();
  }
}
function validateDG(lang){
// if (document.forms[0].UNBadge[1].checked) {alert("info for Tanja: NO")} else {alert("info for Tanja: YES")}
  if (document.forms[0].UNBadge[1].checked) {
  if( testRadio('ParticipantGender',lang,'') && testText('FamilyName',lang,'') && testText('FirstName',lang,'') && testDate('DOB',lang,'') && testRadio('Affiliation',lang,'') && testText('Other',lang,'') && testText('OriginID',lang,'') && testText('IDNumber',lang,'') && testDate('IDValidity',lang,'') && testRadio('UNBadge',lang,'') ){
    document.forms[0].submit();
    }
  } else {
  if( testRadio('ParticipantGender',lang,'') && testText('FamilyName',lang,'') && testText('FirstName',lang,'') && testDate('DOB',lang,'') && testRadio('Affiliation',lang,'') && testText('Other',lang,'') && testRadio('UNBadge',lang,'') ){
    document.forms[0].submit();
    }
  }
}
function validateNGOLounge(lang){
  if( testDate('Date',lang,'') && testText('NameOfNGO',lang,'') && testText('NameOfResponsible',lang,'') && testText('TitleOfMeeting',lang,'') && testText('Telephone',lang,'') && testText('StartTime',lang,'') && testText('EndTime',lang,'') && testText('NumberOfParticipants',lang,'') ){
    document.forms[0].submit();
  }
}
function validateNGOs(lang,fm){
  var doc = document.forms[fm];
  if( (doc.ecosoc.selectedIndex > 0) || (doc.activity.selectedIndex > 0) || (doc.GETWORD.value != "") ){
    var eco = "&ecosoc=" + doc.ecosoc.options[doc.ecosoc.selectedIndex].value;
    var act = "&activity=" + doc.activity.options[doc.activity.selectedIndex].value;
    var txt = "&GETWORD=" + doc.GETWORD.value;
    if(lang == "fr"){
      var url = "http://ngo.unog.ch/ngos_search_results.asp?TYP=LIST" + eco + act + txt + "&Lang=Fr";
    } else {
      var url = "http://ngo.unog.ch/ngos_search_results.asp?TYP=LIST" + eco + act + txt;
    }
    openPopup(url,'ngos','740','500');
  } else {
      if(lang == "en"){
        alert('Please enter all mandatory fields');
      } else {
        alert('Compléter tous les champs obligatoires');
      }
  }
}
function validateEmailAlerts(lang,unid){
  if( testEmail('Username',lang,'') && testText('Password',lang,'') ){
    location.href = "/80256EE60057F2B7/(getProfile)?OpenAgent&pass=" + replaceSubstring(document.forms[0].Password.value,' ','+') + "&email=" + replaceSubstring(document.forms[0].Username.value,' ','+') + "&unid=" + unid;
  }
}
function validateProfile(lang){
  var sectionArray = new Array("");

  for (var i=0;i<document.forms[0].elements.length;i++){
  //for (var i=0;i<document.forms[0].elements.length-1;i++){
    var e = document.forms[0].elements[i];
    if (e.type == 'checkbox' && e.checked == true) { 
      sectionArray[i] = document.forms[0].elements[i].value + ";";
    }
  }
  
  if(sectionArray.length > 1){
    var arrayText = new String(sectionArray);
    arrayText = arrayText.substring(0,(arrayText.length-1));
    var aryText = arrayText.split(';');
  } else {
    var aryText = "";
  }
  if( testText('FirstName',lang,'') && testText('LastName',lang,'') && testEmail('EmailAddress',lang,'') && testText('Password',lang,'') && testRadio('AlertFormat',lang,'') && testSelect('UpdateFrequency',lang,'')){
    if(aryText.length > 0){
      document.forms[0].submit();
    } else {
      if(lang == "en"){
        alert('You must select at least one section');
      } else {
        alert('Vous devez sélectionner au moins une section');
      }
    }
  }
}
function validateEvent(lang){
  var InternalExternal = document.forms[0].InternalExternal.value;
  var enOrganization = document.forms[0].enOrganization.value;
  var OrganizationLogin = document.forms[0].OrganizationLogin.value;
  if (InternalExternal == "external" && enOrganization == OrganizationLogin) {
    if( testText('enTitle',lang,'') && testText('frTitle',lang,'') && testDate('StartDate',lang,'') && testDate('EndDate',lang,'') && testText('enLocation',lang,'') && testText('frLocation',lang,'') && testSelect('Type',lang,'') ){
      document.forms[0].submit();
    }
  }
}

