﻿/****************************************************************************/
///Validator takes one parameter, that is the location in which you wish 
///to display the validation message that it returns.
/****************************************************************************/

Validator = function(validationMessageLocation){
    Validator.Instance = this;
    this.ElementsToValidate = [];
    this.ValidationMessage = "";
    this.ValidationMessageLocation = (typeof validationMessageLocation == "string") 
                                        ? $get(validationMessageLocation)
                                        : (typeof validationMessageLocation == "object")
                                        ? validationMessageLocation 
                                        : alert("Invalid instance of Validator! \n typeof validationMessageLocation must == string || object");
    this.IsValid = true;
};


/****************************************************************************/
///Takes one parameter, that is an [] of elements that you wish to validate
///This [] may contain strings of elements id attributes or the object itself
///or a mix of both
/*==========================================================================*/

Validator.prototype.Validate = function(elementsToValidate){
    
    this.ElementsToValidate = [];
        for(var i=0; elementsToValidate[i]; i++){
        
            var elementToValidate = (typeof elementsToValidate[i] == "string") 
                                    ? $get(elementsToValidate[i])
                                    : elementsToValidate[i];
                                    
            if(elementToValidate.getAttribute('validationType') != undefined &&
                elementToValidate.disabled != true){
                this.ElementsToValidate.push(elementToValidate);
            }
        }
        
    this.IsValid = true;
    this.ClearValidationMessage();
    
    for(var i=0; this.ElementsToValidate[i]; i++){    
        this.ElementsToValidate[i].className = '';
        if(this.ElementsToValidate[i].type == "text"){//Check type for "text"
            var txt = this.ElementsToValidate[i].value;//Get inputs value
            
            switch(this.ElementsToValidate[i].getAttribute('validationType')){
                case 'date' :
                    if(txt.match(/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/) == null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Enter date in mm/dd/yyyy');
                    }
                break;
                case 'familySize' :
                    if(txt.match(/^\d+$/) == null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Enter a positive whole number');
                    }
                break;
                case 'income' :
                    if((txt.search(/^\$?0\d/) != -1) ||
                        txt.match(/^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$/) == null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Must Enter a valid positive dollar amount');
                    }
                    else{
                        var txtVal = parseFloat(txt.replace(/$|,/g,""))
                        if(txtVal<1 || txtVal>1000000){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Income must between $1.00 and & $1,000,000.00');
                        }
                    }
                break;
                case 'int' :
                    if(txt.match(/^\d+$/) == null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Enter a positive whole number');
                    }
                    else{
                        var txtVal = parseFloat(txt);//parse the contents to a float
                        if(txtVal<1 || txtVal>20){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Family size must be between 1 and 20');
                        }
                    }
                break;
                case 'interestRate' :
                    if(txt.search(/^ *\d{0,2}(\.\d{1,4})? *%?$/) == -1){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Intrest Rate must be between 1% and 18%');
                    }
                    else if(txt.match(/^0\d+/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Remove leading zero if input is not a decimal');
                    }
                    else if(txt.match(/^$/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Must enter a valid Interest Rate');
                    }
                    else{
                        var txtVal = parseFloat(txt);//parse the contents to a float
                        if(txtVal<1 || txtVal>18 ){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Interest Rate must be between 1% and 18%');
                        }
                    }
                break;
                case 'interimTerm' :
                    if(txt.match(/[^\d]/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Interim months to repay must be a positive whole number');
                    }
                    else if(txt.match(/^0\d+/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Interim months to repay cannot have a leading zero');
                    }
                    else if(txt.match(/^$/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Must enter a valid interim months to repay');
                    }
                    else{
                        var txtVal = parseFloat(txt);//parse the contents to a float
                        if(txtVal<1 || txtVal>360){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Interim months to repay must be between 1 and 360 months');
                        }
                    }
                break;
                case 'loanBalance' :
                    if((txt.search(/^\$?0\d/) != -1) ||
                        txt.match(/^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$/) == null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Must Enter a valid positive dollar amount');
                    }
                    else{
                        var txtVal = parseFloat(txt.replace(/$|,/g,""))
                        if(txtVal<1000 || txtVal>1000000){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Balance must be between $1,000.00 and & $1,000,000.00');
                        }
                    }
                break;
                case 'term' :
                    if(txt.match(/[^\d]/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Months to Repay must be a positive whole number');
                    }
                    else if(txt.match(/^0\d+/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Months to Repay cannot have a leading zero');
                    }
                    else if(txt.match(/^$/) != null){
                        this.IsValid = false;
                        this.SetElementInvalid(this.ElementsToValidate[i]);
                        this.AddToMessage('Must enter a valid Months to Repay');
                    }
                    else{
                        var txtVal = parseFloat(txt);//parse the contents to a float
                        if(txtVal<12 || txtVal>360){
                            this.IsValid = false;
                            this.SetElementInvalid(this.ElementsToValidate[i]);
                            this.AddToMessage('Months to Repay must be between 12 and 360 months');
                        }
                    }
                break;  
            }   
        }
    }
    (this.IsValid) ? this.ClearValidationMessage() : this.ShowInvalidInputs();
    return this.IsValid;
}
/*==========================================================================*/
/****************************************************************************/

Validator.prototype.AddToMessage = function(message){
    this.ValidationMessage += "*&nbsp;"+message+"<br/>"
}

Validator.prototype.BuildValidationMarker = function(ele){
    var marker = document.createElement('span');
    marker.className = "validation";
    marker.appendChild(document.createTextNode(" *"));
    return marker;
}

Validator.prototype.ClearValidationMessage = function(){
    this.ValidationMessage = "";
    this.ValidationMessageLocation.innerHTML = this.GetValidationMessage();
    for(var i=0; this.ElementsToValidate[i]; i++){
          if(this.ElementsToValidate[i].nextSibling.className == "validation"){
            this.ElementsToValidate[i].parentNode.removeChild(this.ElementsToValidate[i].nextSibling);
          }
    } 
}

Validator.prototype.FocusFirstInvalid = function(){
    for(var i = 0; this.ElementsToValidate[i]; i++){
        if(this.ElementsToValidate[i].className == 'inValid'){
            this.ElementsToValidate[i].select();
            break;
        }
    }
}

Validator.prototype.GetValidationMessage = function(){
    return this.ValidationMessage;
}

Validator.prototype.SetElementInvalid = function(ele){
    ele.className = 'inValid';
}

Validator.prototype.ShowInvalidInputs = function(){
    for(var i=0; this.ElementsToValidate[i]; i++){
        if(this.ElementsToValidate[i].className == 'inValid'){
            if(this.ElementsToValidate[i].nextSibling.className != "validation"){
                this.ElementsToValidate[i].
                    parentNode.insertBefore(
                        this.BuildValidationMarker(),this.ElementsToValidate[i].nextSibling);
            }
        }
    }
    this.ValidationMessageLocation.innerHTML = this.GetValidationMessage();
    this.FocusFirstInvalid();
}