//---------------------------------------------//
//Colors of the validated inputs
//---------------------------------------------//
var sErrColor = "#FFFF99";
var sOKColor = "";

//---------------------------------------------//
// 
//---------------------------------------------//

function isValid (aErr) 
{
    var sErr = aErr.join("");
    if(sErr != "") 
    {
        alert("Please correct the following problem(s):\r\n" + sErr);
        return false;
    }
    else{
        return true;
    }    
}

//---------------------------------------------//
//Clear errors for new validation
//---------------------------------------------//
function clearErrors() 
{
        var inputs = document.getElementsByTagName("input") 
             
        for (var i=0; i < inputs.length; i++) 
        {
            inputs[i].style.backgroundColor = sOKColor;
        }
        
        var textareas = document.getElementsByTagName("textarea")
        for (var i=0; i < textareas.length; i++) 
        {
            textareas[i].style.backgroundColor = sOKColor;
        }
        
        var selects = document.getElementsByTagName("select")
        for (var i=0; i < selects.length; i++) 
        {
            selects[i].style.backgroundColor = sOKColor;
        }
        
        var spans = document.getElementsByTagName("span")
        for (var i=0; i < spans.length; i++) 
        {
            spans[i].style.backgroundColor = sOKColor;
        }
}

//--------------------------------------------//
// Highlight Control
//--------------------------------------------//
function highliteElem(control)
{
    var c = document.
    getElementById(control)
    if(c != null)
        c.style.backgroundColor = sErrColor;
}

//---------------------------------------------//
//Validate required
//---------------------------------------------//
function validateRequired(control, sFieldName) 
{
  var sErr = null;
  var c = document.getElementById(control);
  if(c != null)
  {
      if (c.value == "") 
      {
        c.style.backgroundColor = sErrColor;
        //sErr = sFieldName + " is a required field. \n";
        sErr = sFieldName + " is required. \n";
      }

  }
  return sErr;
  
}
function ValidateRequiredWithDefaultText(control, sFieldName, DefaultText)
{
    var sErr = null;
    var c = document.getElementById(control);
    if(c != null)
    {
        var thisValue = c.value.replace(DefaultText, '');
        if (thisValue == "") 
        {
            c.style.backgroundColor = sErrColor;
            sErr = sFieldName + " is required. \n";
        }

    }
    return sErr;
}
//---------------------------------------------//
//Validate required
//---------------------------------------------//
function validateRequired1(control, sFieldName, contact) 
{
  var sErr = null;
  var c = document.getElementById(control);
  var a = document.getElementById(contact).value; 
    
  if(c != null)
  {
      if (c.value == "") 
      {
        c.style.backgroundColor = sErrColor;
        sErr = sFieldName + a +  " is a required field. \n";
      }

  }
  return sErr;
  
}

function GetRadioListSelectedValue(Control)
{
    var c = document.getElementById(Control) ;
    var returnValue = "";
    
    if(c != null)
    {
        var radios = c.getElementsByTagName("input");
        for (var i=0; i < radios.length; i++) 
        {
            if (radios[i].checked == true) 
            {
                returnValue = radios[i].value;
                break;
            }
        }
    }
    return returnValue;
}

//---------------------------------------------// 
// validate the radio again // to see if Yes/No is selected
//=============================================//

function IsRadioSelected(control)
{
    var c = document.getElementById(control);
    if(c != null)
    {
        var radios = c.getElementsByTagName("input");
        var isChecked = false;
        
        for (var i=0; i < radios.length; i++) 
        {
            if (radios[i].checked == true) 
            {
                isChecked = radios[i].value == "1" ? true : false;
            }
        }
    }
    return isChecked;
}     
//---------------------------------------------//
//Validate required radio
//---------------------------------------------//
function validateRequiredRadio(control, sFieldName) 
{
    var sErr = null;
    var c = document.getElementById(control) ;
    
    if(c != null)
    {
        var radios = c.getElementsByTagName("input");
        var isChecked = false;
        
        for (var i=0; i < radios.length; i++) 
        {
            if (radios[i].checked == true) 
            {
                isChecked = true;
            }
        }
        if (!isChecked)
        {
            c.style.backgroundColor = sErrColor;
            sErr = sFieldName + " is required. \n";
            
        }
        else
            c.style.backgroundColor = "";
    }
    return sErr;
}
function ValidateRequiredCheckbox(Control, FieldName)
{
    var sErr = null;
    var c = document.getElementById(Control); 
    var isChecked = false;
    
    isChecked = c.checked;
    if (isChecked == false)
    {
        c.style.backgroundColor = sErrColor;
        sErr = FieldName + " is required. \n";
    }
    
    return sErr;
}
//*************************************
//* check to see that at lest one box is checked
//*************************************
function validateCheckItem(control, sFieldName)
    {    
    var sErr = null;
    var c = document.getElementById(control); 
    var isChecked = false;  
    if(c != null)
        { 
        var checkBox = c.getElementsByTagName("input");
        var isChecked = false;   
         
        for(var i=0; i < checkBox.length; i++)    
            {        
            if(checkBox[i].checked == true)        
                {            
                    isChecked= true; 
                    break;               
                 }  
            }     
        if (isChecked == false)
        {
         c.style.backgroundColor = sErrColor;
         sErr = sFieldName + " is required. \n";
        }    
    }
    return sErr;
    }        
//---------------------------------------------//
//Validate Date
//---------------------------------------------//
function validateDate (control, sFieldName) 
{
  var sErr = null;
  var c = document.getElementById(control);
  if(c !=null)
  {
      //if the field is empty don't validate.
      if(c.value =="")
        return "";

      c.value = c.value.replace("-", "/");

      var d  = new Date()
      
      if(!(d.setTime(Date.parse(c.value, "n/j/y"))))
      {
        sErr = sFieldName + " '" + c.value + "' is not a valid date. Format should be MM/DD/YYYY \n";
        c.style.backgroundColor = sErrColor;
      }

  }
  return sErr;
}
//---------------------------------------------//
//Validate Optional Date not null and format
//  - clear if mm/dd/yyyy
//---------------------------------------------//
function validateOptionalDate (control, sFieldName) 
{
    
  var sErr = null;
  var c = document.getElementById(control);
  if(c !=null)
  {
      //if the field is empty don't validate.
      if(c.value =="")
        return "";
      
      // if the value is still mm/dd/yyyy don't validate  
      if(c.value =="mm/dd/yyyy")
        return "";
        
      c.value = c.value.replace("-", "/");

      //var d  = new Date()
      
      if(!(isDate(c.value)))
      {
        sErr = sFieldName + " '" + c.value + "' is not a valid date.  \n";
        c.style.backgroundColor = sErrColor;
      }

  }
  return sErr;
}

function ValidateIsFutureDate(Control, FieldName)
{
    var c = document.getElementById(Control);
    var dateToCheck = Date.parse(c.value);
    var currentDate = new Date();
    currentDate = Date.parse(currentDate);
    
    var sErr;
    
    if (dateToCheck < currentDate)
    {
        sErr = FieldName + " must be a date in the future. \n"; 
        c.style.backgroundColor = sErrColor;
    }
    
    return sErr;
}

//---------------------------------------------//
//Validate Future Date
//---------------------------------------------//
function validateFutureDate (control, sFieldName) 
{
    var c = document.getElementById(control) ;
    if(c != null)
    {
        //if the field is empty don't validate.
        if(c.value =="")
          return "";
        var sErr = validateDate(control, sFieldName);    
        if(sErr == null)
        {
            var c = document.getElementById(control);
            var d = new Date();
            var now = new Date();
            if(d.setTime(Date.parse(c.value, "n/j/y")) < now) 
            {
              sErr = sFieldName + " '" + c.value + "' is not later than today. \n"; 
              c.style.backgroundColor = sErrColor;
            }

              
        } 
    }
    return sErr;
    
}

//---------------------------------------------//
//Validate Past date
//---------------------------------------------//
function validatePastDate (control, sFieldName) 
{
    
    var c = document.getElementById(control);
    if (c != null ) 
    {
        //if the field is empty don't validate.
        if(c.value =="")
          return "";
        
        var sErr = validateDate(control, sFieldName);
        
        if(sErr == "" || sErr == null)
        {
            
            var c = document.getElementById(control);
            var d = new Date();
            var now = new Date();
            if(d.setTime(Date.parse(c.value, "n/j/y")) > now) 
            {
              sErr = sFieldName + " '" + c.value + "' cannot be a future date. \n"; 
              c.style.backgroundColor = sErrColor;
            }
        
        } 

    }
    return sErr;
    
}
function ValidateSSN(ControlID, FieldName)
{
    var ssnControl = document.getElementById(ControlID);
    var ssn = ssnControl.value.replace(/-/g,'');
    var sErr = null;
    var re = /(?!000)(?!666)([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(?!00)(\d{2})\2(?!0000)(\d{4})/;
    if((ssn.search(re)==-1) || (Number(ssn)<1) || (Number(ssn)>791999999) || (Number(ssn) == 111111111) || (Number(ssn) == 222222222) || (Number(ssn) == 333333333) || (Number(ssn) == 444444444) || (Number(ssn) == 555555555) || (Number(ssn) == 666666666) || (Number(ssn) == 777777777) || (Number(ssn) == 888888888) || (Number(ssn) == 999999999))
    {
        ssnControl.style.backgroundColor = sErrColor;
        sErr = "Please enter a valid " + FieldName + ".  \n";
    }
    return sErr;
}
function ValidateAlphaOnly(ControlID, FieldName)
{
    var control = document.getElementById(ControlID);
    var sErr = null;
    var re = /^[a-zA-Z]*$/;
   
    if (control.value.search(re)==-1)
    {
        control.style.backgroundColor = sErrColor;
        sErr = "Please enter only alpha characters for " + FieldName + ".  \n";
    }
    return sErr;
    //Regex.IsMatch(Source, "^[a-zA-Z]*$")
}
function ValidatePhoneNumber(ControlID, FieldName)
{
    var phoneControl = document.getElementById(ControlID);
    var phone = phoneControl.value.replace(/-/g, '');
    var sErr = null;
    
    if ((phone.length != 10) || (isNaN(Number(phone))))
    {
        phoneControl.style.backgroundColor = sErrColor;
        sErr = "Please enter a valid " + FieldName + ".  \n";
    }
    
    return sErr;
}
function ValidateZipCode(ControlID, FieldName)
{
    var zipCodeControl = document.getElementById(ControlID);
    var zipCode = zipCodeControl.value.replace(/-/g, '');
    var sErr = null;
    
    if ((zipCode.length != 5 && zipCode.length != 9) || (isNaN(Number(zipCode))))
    {
        zipCodeControl.style.backgroundColor = sErrColor;
        sErr = "Please enter a valid " + FieldName + ".  \n";
    }
    
    return sErr;
}
function ValidatePasswords(Control1ID, Control2ID)
{
    var sErr = null;
    var password = document.getElementById(Control1ID);
    var confirmPassword = document.getElementById(Control2ID);
    if (password.value != confirmPassword.value)
    {
        password.style.backgroundColor = sErrColor;
        confirmPassword.style.backgroundColor = sErrColor;
        sErr = "The passwords you have entered do not match. \n";
    }
    return sErr;
}
function ValidateEmailAddresses(Control1ID, Control2ID)
{
    var sErr = null;
    var email = document.getElementById(Control1ID);
    var confirmEmail = document.getElementById(Control2ID);
    if (email.value != confirmEmail.value)
    {
        email.style.backgroundColor = sErrColor;
        confirmEmail.style.backgroundColor = sErrColor;
        sErr = "The email addresses you have entered do not match. \n";
    }
    return sErr;
}
function ValidateEmail(ControlID, FieldName)
{
	var sEmail = document.getElementById(ControlID).value;
	var sErr = null;
	
	if ((sEmail.length<5) || (sEmail.indexOf('@')==-1) || (sEmail.indexOf('.')==-1)) 
	{
		sErr = "Please enter a valid email address for " + FieldName + ". \n";
	}
	return sErr;
}
function ValidateTextMinLength(ControlID, MinimumLength, FieldName)
{
    var controlValue = document.getElementById(ControlID).value;
    var sErr = null;
    
    if (controlValue.length < MinimumLength)
    {
        sErr = "Please enter at least " + MinimumLength + " characters for " + FieldName + ". \n";
    }
    return sErr;
}
//---------------------------------------------//
// Validate date is before
//---------------------------------------------//
function validateDateIsBefore (control1, sFieldName1, control2, sFieldName2) 
{
    //var sErr = validateDate(control1, sFieldName1);
    //sErr += validateDate(control2, sFieldName2);
    var sErr = "";
    
    if(sErr == "")
    {
        var c1 = document.getElementById(control1);
        var c2 = document.getElementById(control2);
        
        if(c1 != null && c2 != null)
        {        
          
          var d1  = new Date(); 
          d1 = Date.parse(c1.value, "n/j/y"); 
          var d2 = new Date(); 
          d2 = Date.parse(c2.value, "n/j/y");
          
          if(d1 > d2)
          {
            sErr = sFieldName1 + " '" + c1.value + "' must be before " + sFieldName2 + " '" + c2.value +"'.  \n";
            c1.style.backgroundColor = sErrColor;
            c2.style.backgroundColor = sErrColor;
          }
          else 
            sErr = null;
        
        }
    }
    return sErr;
}
function CheckMaxChars(ControlToCheck, MaximumLength, ControlDisplay)
{
    var textLength = ControlToCheck.value.length;
    ControlDisplay.value = MaximumLength - textLength > -1 ? MaximumLength - textLength : 0;
    
    if (textLength > MaximumLength)
    {
        alert('You have exceeded the ' + MaximumLength + ' character maximum for this field.');
        ControlToCheck.value = ControlToCheck.value.substring(0, MaximumLength);
        return false;
    }
    return true;
}
function IsNumeric(Control, FieldName)
{
    var sErr = "";
    if (isNaN(gO(Control).value))
    {
        sErr = "Please enter a numeric value for " + FieldName + ". \n";
    }
    else
    {
        sErr = null;
    }
    return sErr;
}
function IsAlphaNumericOnly(Control, FieldName)
{
    var sErr = "";
    var re = /^[a-zA-Z0-9]+$/;
    var stringToCheck = gO(Control).value;
    if (stringToCheck.search(re)==-1)
    {
        sErr = "Please enter only letters and numbers for " + FieldName + ". \n";
    }
    else
    {
        sErr = null;
    }
    return sErr;
}
