
  $(document).ready(function() {
    // Contact Form Submit
    $('#contactsubmit').click( function() {
      
      if ( $('#name').val().length < 4) {
        alert('Please enter your full name');
        $('#name').focus();
        return false;
      }
      
      regexp = /^[\d\s]{4,20}$/; // Pretty rough!
      str = $('#telephone').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert('Please enter your phone number');
        $('#telephone').focus();
        return false;
      }
      
      if ( $('#email').val()=='' || !isEmailAddr($('#email').val())) {
        alert('Please enter a valid email address');
        $('#email').focus();
        return false;
      }
      
      $('#form1').submit();
      return true;
    });
    
    
    
    // Referral Form Submit
    $('#referralsubmit').click( function() {
      
      // Patient's Information      
      if ( $('#pname').val().length < 4) {
        alert("Please enter the patient's full name");
        $('#pname').focus();
        return false;
      }
      
      if ( $('#dob').val().length < 4) {
        alert("Please enter the patient's date of birth");
        $('#dob').focus();
        return false;
      }
      
      if ( $('#residence').val().length < 4) {
        alert("Please enter the patient's current place of residence");
        $('#residence').focus();
        return false;
      }
      
      if ( $('#history').val().length < 4) {
        alert("Please enter the patient's history");
        $('#history').focus();
        return false;
      }
      
      // Source of referral
      if ( $('#oname').val().length < 4) {
        alert("Please enter the full name of the reffering organisation");
        $('#oname').focus();
        return false;
      }
      
      if ( $('#address').val().length < 4) {
        alert("Please enter the organisation's address");
        $('#address').focus();
        return false;
      }
      
      if ( $('#consultant').val().length < 4) {
        alert("Please enter the consultant's name");
        $('#consultant').focus();
        return false;
      }
      
      if ( $('#referrer').val().length < 4) {
        alert("Please enter the referrer's name");
        $('#referrer').focus();
        return false;
      }
      
      if ( $('#position').val() == '') {
        alert("Please enter the referrer's position");
        $('#position').focus();
        return false;
      }
      
      regexp = /^[\d\s]{4,20}$/; // Pretty rough!
      str = $('#telephone1').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the referrer's phone number");
        $('#telephone1').focus();
        return false;
      }
      
      regexp = /^[\d\s]{4,20}$/; // Pretty rough!
      str = $('#fax').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the referrer's fax number");
        $('#fax').focus();
        return false;
      }      
      
      if ( $('#email').val()!='' && !isEmailAddr($('#email').val())) {
        alert('Please enter a valid email address');
        $('#email').focus();
        return false;
      }
      
      
      // Funder
      regexp = /^[\d\s]{4,20}$/; // Pretty rough!
      str = $('#telephone2').val();
      if ( str!='' && str.search(regexp)==-1 ) {
        alert('Please enter a valid phone number');
        $('#telephone2').focus();
        return false;
      }
      
      
      // Other information
      var dp = $("input[@name='Urgency']:checked").val();
      if ( dp == null) {
        alert('How urgent is this referral?  Please select an urgency level.');
        $('#urgency').focus();
        return false;
      }
      
      regexp = /^[\d\s]{4,20}$/; // Pretty rough!
      str = $('#telephone3').val();
      if ( str!='' && str.search(regexp)==-1 ) {
        alert('Please enter a valid phone number');
        $('#telephone3').focus();
        return false;
      }
      
      
      $('#form1').submit();
      return true;
    
    });
    
    
    
    
    
    // Vacancy Form Submit
    $('#newvacancysubmit').click( function() {
      
      regexp = /\d\d\/\d\d\/\d\d\d\d/; 
      str = $('#date').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the date in DD/MM/YYYY format");
        $('#date').focus();
        return false;
      }
      
      if ( $('#jobtitle').val()=='') {
        alert("Please enter the job title");
        $('#jobtitle').focus();
        return false;
      }
      
      if ( $('#location').val()=='') {
        alert("Please enter the job location");
        $('#location').focus();
        return false;
      }
      
      if ( $('#qualifications').val()=='') {
        alert("Please enter the required qualifications");
        $('#qualifications').focus();
        return false;
      }
      
      $('#form2').submit();
      return true;
    });
    
    $('#editvacancysubmit').click( function() {
      
      regexp = /\d\d\/\d\d\/\d\d\d\d/; 
      str = $('#date').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the date in DD/MM/YYYY format");
        $('#date').focus();
        return false;
      }
      
      if ( $('#jobtitle').val()=='') {
        alert("Please enter the job title");
        $('#jobtitle').focus();
        return false;
      }
      
      if ( $('#location').val()=='') {
        alert("Please enter the job location");
        $('#location').focus();
        return false;
      }
      
      if ( $('#qualifications').val()=='') {
        alert("Please enter the required qualifications");
        $('#qualifications').focus();
        return false;
      }
      
      $('#form2').submit();
      return true;
    });
    
    
    
    
    
    
    // News Form Submit
    $('#newnewsitemsubmit').click( function() {
      
      regexp = /\d\d\/\d\d\/\d\d\d\d/; 
      str = $('#newsdate').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the date in DD/MM/YYYY format");
        $('#newsdate').focus();
        return false;
      }
      
      if ( $('#newstitle').val()=='') {
        alert("Please enter the title");
        $('#newstitle').focus();
        return false;
      }
      
      if ( $('#summary').val()=='') {
        alert("Please enter a summary");
        $('#summary').focus();
        return false;
      }
      
      $('#form1').submit();
      return true;
    });
    
    $('#editnewsitemsubmit').click( function() {
      
      regexp = /\d\d\/\d\d\/\d\d\d\d/;
      str = $('#newsdate').val();
      if ( str=='' || str.search(regexp)==-1 ) {
        alert("Please enter the date in DD/MM/YYYY format");
        $('#newsdate').focus();
        return false;
      }
      
      if ( $('#newstitle').val()=='') {
        alert("Please enter the title");
        $('#newstitle').focus();
        return false;
      }
      
      if ( $('#summary').val()=='') {
        alert("Please enter a summary");
        $('#summary').focus();
        return false;
      }
      
      $('#form1').submit();
      return true;
    });
    
    
    
    
    
    
    
    
    
    
    
    // Delete Things
    $("a[@class='deletevacancy']").click( function() {
      return(confirm('Are you sure you want to delete this job vacancy?'));
    });
    $('#deletevacancysubmit').click( function() {
      return(confirm('Are you sure you want to delete this job vacancy?'));
    });
    $("a[@class='deletenewsitem']").click( function() {
      return(confirm('Are you sure you want to delete this news item?'));
    });
    $('#deletenewsitemsubmit').click( function() {
      return(confirm('Are you sure you want to delete this newsitem?'));
    });
    
    
    // Fade out any messages
    setTimeout(function(){$("#message").fadeOut("slow");}, 7500);
    setTimeout(function(){$("#error").fadeOut("slow");}, 7500);

    
    
    
    function isEmailAddr(email) {
      var result = false;
      var theStr = new String(email);
      var index = theStr.indexOf("@");
      if (index > 0) {
        var pindex = theStr.indexOf(".",index);
        if ((pindex > index+1) && (theStr.length > pindex+1))
          result = true;
      }
      return result;
    }
    
    
    // Hide things on the news input form
    $nl = $("#newsletter:checked").val();
    if ($nl!=null) {
      //$("#details").attr('disabled', 'disabled');
      //$("#file2").attr('disabled', 'disabled');
      $("#summaryrow").hide();
      $("#detailsrow").hide();
      $("#file2row").hide();
    }
    else {
      //$("#file1").attr('disabled', 'disabled');
      $("#file1row").hide();
    }
    
    $("#newsletter").click( function() {
      $nl = $("#newsletter:checked").val();
      if ($nl!=null) {
        $("#summaryrow").hide();
        $("#detailsrow").hide();
        $("#file2row").hide();
        
        $("#file1row").show();
      }
      else {
        $("#file1row").hide();
        
        $("#summaryrow").show();
        $("#detailsrow").show();
        $("#file2row").show();
      }
    });
    
    
  });

