//CONSTANT
var NS_AND = 0;
var NS_OR = 1;

var NS_IS = 2
var NS_ISIN = 3;
var NS_ISBETWEEN = 4;
var NS_ISNOTBETWEEN = 5;
var NS_CONTAINS = 6;
var NS_GREATER = 7;
var NS_LESS = 8;

function Condition(){
    this.conditions = new Array();
    this.groupConditionType = NS_AND;
    
    
    this.addCondition = function(question, segment, values, type){
        var newCondition = {
            question: question,
            segment: segment,
            values: values,
            type: type
        };
        
        this.conditions[this.conditions.length] = newCondition;
    }
    
    this.validate = function(){
        if(this.conditions.length == 0) return true;
    
        var validConditionCount = 0;
        
        for(var i = 0;i < this.conditions.length;i++){
            var condition = this.conditions[i];
            var conditionValid = false;
            
            switch(condition.question.sQuestionType){
                case 'LIKERT':
                    conditionValid = this.validateLikert(condition);
                    break;
                case 'SEMANTICS':
                    conditionValid = this.validateSemantics(condition);
                    break;
                case 'MULTIPLE_CHOICES':
                    conditionValid = this.validateMultipleChoices(condition);
                    break;
                case 'MULTIPLE_ANSWERS':
                    conditionValid = this.validateMultipleAnswers(condition);
                    break;
                case 'DROP_DOWN_CHOICES':
                    conditionValid = this.validateDropDown(condition);
                    break;
                case 'SHORT_TEXT':
                    conditionValid = this.validateShortText(condition);
                    break;
            }
            
            if(this.groupConditionType == NS_OR && conditionValid){
                return true;
            }else if(this.groupConditionType == NS_AND && conditionValid){
                validConditionCount++;
            }
        }
        
        if(this.groupConditionType == NS_AND && validConditionCount == this.conditions.length){
            return true;
        }else{
            return false;
        }
    }
    
    this.validateLikert = function(condition){
        var answer = condition.question.iCurrChoiceID[condition.segment];
        if(answer == null) return false;
        
        answer++; //Fix
        
        switch(condition.type){
            case NS_IS: case NS_ISIN:
                if(condition.values.indexOf(answer) > -1)
                    return true;
                else
                    return false;
                    
                break;
            case NS_ISBETWEEN: case NS_ISNOTBETWEEN:
                var between = (answer > parseInt(condition.values[0]) && answer < parseInt(condition.values[1]));
            
                return (condition.type == NS_ISBETWEEN ? between : !between);
                
                break;
            case NS_LESS:
                if(parseInt(answer) < parseInt(condition.values[0]))
                    return true;
                else
                    return false;
                    
                break;
            case NS_GREATER:
                if(parseInt(answer) > parseInt(condition.values[0]))
                    return true;
                else
                    return false;
                    
                break;
        }
    }
    
    this.validateSemantics = function(condition){
        return this.validateLikert(condition);
    }
    
    this.validateMultipleChoices = function(condition){
        var answer = condition.question.iCurrChoiceID;
        if(answer == null) return false;
            
        if(condition.values.indexOf(answer) > -1)
            return true;
        else
            return false;
        
    }
    
    this.validateMultipleAnswers = function(condition){
        var answer = condition.question.bCurrChoices;
        if(answer == null) return false;
        
        if(condition.type == NS_ISIN){
            for(var i = 0;i < answer.length;i++){
                if(answer[i] && condition.values.indexOf(i) > -1)
                    return true;
            }
            
            return false;
        }else{
            var validAnswerCount = 0;
            for(var i = 0;i < answer.length;i++){
                if(answer[i] && condition.values.indexOf(i) > -1)
                    validAnswerCount++;
                else if(answer[i] && condition.values.indexOf(i) == -1)
                    return false;
            }
            
            if(validAnswerCount == condition.values.length)
                return true;
            else
                return false;
        }
    }
    this.validateDropDown = function(condition){
        var answer = condition.question.iCurrChoiceID[condition.segment];
        if(answer == null) return false;
            
        if(condition.values.indexOf(answer) > -1)
            return true;
        else
            return false;
    }
    this.validateShortText = function(condition){
        var input = condition.question.aChoices[condition.segment].oInput;
        if(input == null) return false;
        var answer = input.value;
        if(answer == null) return false;
        
        switch(condition.type){
            case NS_IS: case NS_ISIN:
                if(answer == condition.values[0])
                    return true;
                else
                    return false;
                    
                break;
            case NS_CONTAINS:
                if(answer.toLowerCase().indexOf(condition.values[0].toLowerCase()) > -1)
                    return true;
                else
                    return false;
                    
                break;
            case NS_ISBETWEEN: case NS_ISNOTBETWEEN:
                between = (parseInt(answer) > parseInt(condition.values[0]) && parseInt(answer) < parseInt(condition.values[1]));
            
                return (condition.type == NS_ISBETWEEN ? between : !between);
                break;
                
            case NS_GREATER:
                if(parseInt(answer) > parseInt(condition.values[0]))
                    return true;
                else
                    return false;
                    
                break;
            case NS_LESS:
                if(parseInt(answer) < parseInt(condition.values[0]))
                    return true;
                else
                    return false;
                    
                break;
            
        }
    }
}

