

function checkForm(theForm) {

    if (!checkNames(theForm.ClientNames))  return (false);
    if (!checkEmail (theForm.ClientEmail)) return (false);
    if (!checkPhone (theForm.ClientPhone)) return (false);
    
}

function nigCheckForm(theForm) {      
    if (!checkNigNames (theForm.nigNames))  return (false);
    if (!checkNigLabname (theForm.nigLabName)) return (false);
    if (!checkEmail (theForm.nigEmail1)) return (false);
    if (!checkEmail (theForm.nigEmail2)) return (false);
    if (!checkNigEmail (theForm.nigEmail2, theForm.nigEmail1)) return (false);
    if (!checkPhone (theForm.nigPhone)) return (false);
    if (!checkNigAddress (theForm.nigAddress)) return (false);
    }

function validLogin(theForm)  {
    if (!checkEmail (theForm.userText)) return (false);
    if (!checkPassword (theForm.userPass)) return (false);
    theForm.action="loginChk.php";
    theForm.submit();
}

function validRetrieve(theForm)  {
    if (!checkEmail (theForm.userEmail)) return (false);
    theForm.action="loginSendEmail.php";
    theForm.submit();
}

function validRegister(theForm)  {
     if (!checkNigNames (theForm.regName)) return (false);
     if (!checkEmail (theForm.regEmail1)) return (false);
     if (!checkEmail (theForm.regEmail2)) return (false);
     if (!checkNigEmail (theForm.regEmail2, theForm.regEmail1)) return (false);
     if (!checkPassword (theForm.regPass1)) return (false);
     if (!checkPassword2 (theForm.regPass2)) return (false);
     if (!checkRegPass (theForm.regPass1, theForm.regPass2)) return (false);   
     if (!checkPhone (theForm.regTel)) return (false); 
     theForm.action="loginRegister.php";
     theForm.submit();
}

function expressCheckForm(theForm) {      
    if (!checkXsubject (theForm.subject))  return (false);
    if (!checkXtask (theForm.comment)) return (false);
 }


/*

    Validate: E-mail address

    ========================

*/

function best(addressField)
{
try
  {
  adddlert(addressField.length);
  }
catch(err)
  {
  txt="There was an error on this page.\n\n";
  txt+="Error description: " + err.description + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
}



function checkEmail ( addressField ) {
/*
     alert("1st") ;
     if ( best (addressField) ) {
       alert("pass") ;
     }  
     else {
     alert("failed");
     }
    return (true);

*/
    if ( stringEmpty (addressField.value ) )

        alert ( "Error! There is no E-Mail address entered" );

    else if ( noAtSign ( addressField.value ) )

        alert ( "Error! The E-Mail address does not contain an '@'character" );

    else if ( nothingBeforeAt ( addressField.value ) )

        alert ( "Error! An E-Mail address must contain at least one character before the '@' character" );

    else if ( noLeftBracket ( addressField.value ) )

        alert ( "Error! The E-Mail address contains a right square bracket ']',\nbut no corresponding left square bracket '['" );

    else if ( noRightBracket ( addressField.value ) )

        alert ( "Error! The E-Mail address contains a left square bracket '[',\nbut no corresponding right square bracket ']'" );

    else if ( noValidPeriod ( addressField.value ) )

        alert ( "Error! An E-Mail address must contain a period ('.') character" );

    else if ( noValidSuffix ( addressField.value ) )

        alert ( "Error! An E-Mail address must contain a two or three character suffix" );

    else 
      
         return (true);
   
    return ( false );

}







function linkCheckValidation ( formField ) {

    if ( checkValidation ( formField ) == true ) {

        alert ( 'E-Mail Address Validates OK' );

    }

    return ( false );

}

function stringEmpty ( address ) {

    // CHECK THAT THE STRING IS NOT EMPTY
    if ( address.length < 1 ) {

        return ( true );

    } else {
         return ( false );

    }

}


function stringLength (address) {
   // Return the length of the string
   return (address.length);
}

function noAtSign ( address ) {

    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING

    if ( address.indexOf ( '@', 0 ) == -1 ) {

        return ( true )

    } else {

        return ( false );

    }

}

function nothingBeforeAt ( address ) {

    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER

    if ( address.indexOf ( '@', 0 ) < 1 ) {

        return ( true )

    } else {

        return ( false );

    }

}

function noLeftBracket ( address ) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET

    if ( address.indexOf ( '[', 0 ) == -1 && address.charAt ( address.length - 1 ) == ']' ) {

        return ( true )

    } else {

        return ( false );

    }

}

function noRightBracket ( address ) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET

    if ( address.indexOf ( '[', 0 ) > -1 && address.charAt ( address.length - 1 ) != ']' ) {

        return ( true );

    } else {

        return ( false );

    }

}

function noValidPeriod ( address ) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED

    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )

        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING

    if ( address.indexOf ( '.', 0 ) == -1 )

        return ( true );

    return ( false );

}



function noValidSuffix ( address ) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED

    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )

        return ( false );

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD

    var len = address.length;

    var pos = address.lastIndexOf ( '.', len - 1 ) + 1;

    if ( ( len - pos ) < 2 || ( len - pos ) > 3 ) {

        return ( true );

    } else {

        return ( false );

    }

}





/*

    Validate: Phone No

    ==================

*/



function isDigit(c){

      return ((c >= "0") && (c <= "9") || (c==".") || (c=="-"));

}



function isNumber(str){

      for (var i= 0; i < str.length; i++)

    {

        // Check that current character is number.

        var c = str.charAt(i);



        if (!isDigit(c))

             return false;

    }

      return true;

}



function checkPhone ( PhoneField ) {

      if (!isNumber( PhoneField.value)) {

         alert ( "Error! Phone No must consist of only digits 0 - 9" );

         return (false);

      }

      if ( stringEmpty (PhoneField.value ) ){

        alert ( "Error! There is no Phone No entered" );

        return (false);

      }

      return (true);

}





/*

   Validates: Names

   ================

*/



function isCharacter(c){

      return ((c >= "a") && (c <= "z")||(c >= "A") && (c <= "Z") || (c=" "))

}





function isAlphabets(str){

      for (var i= 0; i < str.length; i++){

        // Check that current character is number.

        var c = str.charAt(i);

        if (!isCharacter(c)) return false;

    }

return true;

}





function checkNames (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter your name(s)" );

         return (false);

      }

      if (!isAlphabets(NameField.value)) {

         alert ( "Error! Names must only be alphabets separated by space if neccesary");

         return (false);

      }

      return (true);

}

function checkNigNames (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter your title and name(s)" );

         return (false);

      }
     return (true);
}


function checkXsubject (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter the subject of the IT task " );

         return (false);

      }
     return (true);
}

function checkXtask (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter the IT task that you need " );

         return (false);

      }
      
     if ( stringLength (NameField.value ) < 10 ){

         alert ( "Error! Please enter more details of the task " );

         return (false);

      }
       
      
     return (true);
}







function checkPassword (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter your password" );

         return (false);

      }
     return (true);
}


function checkPassword2 (NameField ) {

      if ( stringEmpty (NameField.value ) ){

         alert ( "Error! Please enter the confirmed password" );

         return (false);

      }
     return (true);
}

function checkNigLabname (NameField ) {
     if ( stringEmpty (NameField.value ) ){
         alert ( "Error! Please enter the name of the lab or Hospital" );
         return (false);
      }
      return (true);

}

function checkNigEmail (NameField1,NameField2 ) {

      if ( (NameField1.value!=NameField2.value ) ){

         alert ( "Error! The two Emails are not the same" );

         return (false);

      }
     return (true);

}

function checkRegPass (NameField1,NameField2 ) {

      if ( (NameField1.value!=NameField2.value ) ){

         alert ( "Error! The two passwords are not the same" );

         return (false);

      }
     return (true);

}



 
function checkNigAddress (NameField ) {
      if ( stringEmpty (NameField.value ) ){
         alert ( "Error! Please enter the address " );
         return (false);
      }
      
      if (stringLength(NameField.value) < 10 ) {
         alert ( "Error! Enter more text for the address " );
         return (false);
      }
      
     return (true);
}

