
// Javascript validation functions

// Method to strip spaces from a value ...
function stripSpaces(strval) {
    strval = (strval.replace(/^\W+/,'')).replace(/\W+$/,'');
    return strval;
}

// Check for empty field ...
function isFieldEmpty(aTextField) 
{
   var fldval = aTextField.value;
   if (fldval != null )
       fldval = stripSpaces(fldval);
   
   if ( (fldval==null) ||
        (fldval.length==0) ) 
   {
      aTextField.value = fldval;
      return true;
   }
   else 
   {
     return false; 
   }
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
    var bracket = 3;
    strPhone = trim(strPhone);
    if(strPhone.indexOf("+")>1) 
        return false;
        
    if (strPhone.indexOf("-")!=-1)
        bracket = bracket+1;
        
    if (strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket) 
        return false;
        
    var brchr = strPhone.indexOf("(");
    
    if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")") 
        return false;
        
    if(strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) 
        return false;
        
    s = stripCharsInBag(strPhone,validWorldPhoneChars);
    
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkPhoneNumber(phonefld)
{
      if ( !isFieldEmpty(phonefld) )
      {
    	if (checkInternationalPhone(Phone.value)==false)
        {
    		alert("Please Enter a Valid Phone Number")
    		Phone.focus()
    		return false
    	}
      
      }
      else
      {
         alert("Mandatory Field. Please enter a valid Phone Number and retry");
         phonefld.focus();
         return false;
      }
    
      return true
 }


//function to check valid email address in emailfld
function isValidEmail(emailfld){

  if ( !isFieldEmpty(emailfld) )
  {
      //validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
      validRegExp = "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$";
      strEmail = emailfld.value;
    
       // search email text for regular exp matches
        if (strEmail.search(validRegExp) == -1) 
       {
          alert('Invalid e-mail address format! \nPlease enter a valid e-mail address and retry');
          emailfld.focus();
          return false;
       } 
       return true; 
  }
  else
  {
     alert("Mandatory Field. Please enter a valid e-mail address and retry");
     emailfld.focus();
     return false;
  }
}

//function to check valid email address in emailfld
function checkConfirmPassword(passwordfld, confirmFld){
  
  if ( passwordfld != null && !isFieldEmpty(passwordfld) )
  {
      if ( confirmFld != null && !isFieldEmpty(confirmFld) )
      {
         var password = passwordfld.value;
         var confirm = confirmFld.value;
         if ( password == confirm )
             return true;
         else
         {
            alert('Password and confirm values do not match.\n Please enter same password and retry');
            passwordfld.focus();
            return false;
         }
      }
      else
      {
         alert('Please enter a value for confirm password and retry');
         confirmFld.focus();
         return false;
      }
  }
  else
  {
     alert('Please enter a value for Password and retry');
     passwordfld.focus();
     return false;
  }
  return false;
}

// Validate Contact us form ...
function checkContactUsForm(contact_Us)
{
    if ( isFieldEmpty(contact_Us.elements["reason"]) )
    {
          alert("Please enter a value for Reason and retry");
          contact_Us.elements["reason"].focus();
          return false;
    }

    if ( isFieldEmpty(contact_Us.elements["name"]) )
    {
          alert("Please enter Your Name and retry");
          contact_Us.elements["name"].focus();
          return false;
    }
    
    if ( !checkPhoneNumber(contact_Us.elements["phone"]) )
    {
          return false;
    }

    if ( isValidEmail(contact_Us.elements["email_address"]) )
    {
          return false;
    }

    if ( isFieldEmpty(contact_Us.elements["comment"]) )
    {
          alert("Please enter comment and retry");
          contact_Us.elements["comment"].focus();
          return false;
    }

    return false;    
}

function checkIntegerRange(val, min, max )
{
    if ( isInteger(val) )
    {
          // Now, explicitly change the type to integer via parseInt
          // so that the comparison code below will work both on
          // JavaScript 1.2 (which typechecks in equality comparisons)
          // and JavaScript 1.1 and before (which doesn't).
          var num = parseInt (val);
          return ((num >= min) && (num <= max));
    }
    return false;

}

// Validate Contact us form ...
function checkRowsForm(rowCount)
{
    if ( isFieldEmpty(rowCount.elements["pagesize"]) )
    {
          alert("Please enter a valid Row Count value from 5 to 1000 and retry");
          rowCount.elements["pagesize"].focus();
          return false;
    }

    val = rowCount.elements["pagesize"].value;
    if ( !checkIntegerRange(val, 5, 1000) )
    {
          alert("Please enter a valid Row Count value from 5 to 1000 and retry");
          rowCount.elements["pagesize"].focus();
          return false;
    }

    return true;    
}


function checkForgotPasswordForm(formObj)
{
    if ( !isValidEmail(formObj.elements["email_address"]) )
    {
        return false;
    }

    return true;    
}



function checkLoginForm(formObj)
{
    if ( !isValidEmail(formObj.elements["email_address"]) )
    {
        return false;
    }

    return true;    
}


function checkDonateForm(formObj)
{
    if ( isFieldEmpty(formObj.elements["amount"]) )
    {
          alert("Please enter a value for Donation Amount and retry");
          formObj.elements["amount"].focus();
          return false;
    }

    return true;    
}


function checkCreateAccountForm(formObj)
{
    //alert("First Name = " + formObj.elements["firstname"].value );
    
    if ( isFieldEmpty(formObj.elements["firstname"]) )
    {
          alert("Please enter a value for first name and retry");
          formObj.elements["firstname"].focus();
          return false;
    }
    
    else if ( isFieldEmpty(formObj.elements["lastname"]) )
    {
          alert("Please enter a value for last name and retry");
          formObj.elements["lastname"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["street_address"]) )
    {
          alert("Please enter a value for street address and retry");
          formObj.elements["street_address"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["city"]) )
    {
          alert("Please enter a value for city and retry");
          formObj.elements["city"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["state"]) )
    {
          alert("Please enter a value for state and retry");
          formObj.elements["state"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["telephone"]) )
    {
          alert("Please enter a value for telephone and retry");
          formObj.elements["telephone"].focus();
          return false;
    }

    if ( isValidEmail(formObj.elements["email_address"]) )
    {
        if ( !checkConfirmPassword(formObj.elements["password"], formObj.elements["confirmation"]) )
            return false;
    }
    else
          return false;

    
    return true;
}



function checkAdminUpdateAccountForm(formObj)
{
    //alert("First Name = " + formObj.elements["firstname"].value );
    
    if ( isFieldEmpty(formObj.elements["firstname"]) )
    {
          alert("Please enter a value for first name and retry");
          formObj.elements["firstname"].focus();
          return false;
    }
    
    else if ( isFieldEmpty(formObj.elements["lastname"]) )
    {
          alert("Please enter a value for last name and retry");
          formObj.elements["lastname"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["street_address"]) )
    {
          alert("Please enter a value for street address and retry");
          formObj.elements["street_address"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["city"]) )
    {
          alert("Please enter a value for city and retry");
          formObj.elements["city"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["state"]) )
    {
          alert("Please enter a value for state and retry");
          formObj.elements["state"].focus();
          return false;
    }
    else if ( isFieldEmpty(formObj.elements["telephone"]) )
    {
          alert("Please enter a value for telephone and retry");
          formObj.elements["telephone"].focus();
          return false;
    }

    if ( !isValidEmail(formObj.elements["email_address"]) )
    {
       return false;
    }

    
    return true;
}




