﻿function depBrandCheck(strWordList, showMessage, message, nofiticationControl, initialNotificationText){
    if(strWordList==null){throw 'the wordlist with registered brands is empty.';}
    this.showMessage = showMessage;
    this.message = message;
    this.ctlNotification = nofiticationControl;
    this.initialNotificationText = initialNotificationText;
    if(typeof(this.ctlNotification) == 'undefined'){throw 'notificationControl must be a valid control';}
    if(this.initialNotificationText == ''){throw 'notificationText can not be empty';}
    
    var i = 0;
    this.brandlist = new Array();
    for(wordvalue in strWordList){
        this.brandlist[i] = strWordList[wordvalue].split('~');
        i++;
    }
   
    //this event needs to be attached, or the validator won't work
    depBrandCheck.prototype.txtControl_onkeydown = function(txtControl, e){
        //var event = (e)?e:(window.event)?window.event:null;
        var key = (window.Event) ? e.which : e.keyCode;
        if (String.fromCharCode(key)==" " || key==13){ //enter key or space
            //doe dit regel per regel, woord voor woord
            var regels = this.userInput(txtControl).split(String.fromCharCode(13));
            for(var regel in regels){
                var foundWords = regels[regel].split(" ");
                for(var wordId in foundWords){
                    var oldword = this.wordCleanup(foundWords[wordId]);
                    var correctedWord = this.validateWord(oldword);
                    if((correctedWord != '' && oldword != '') && (correctedWord != oldword)){
                        this.replaceWord(txtControl, oldword, correctedWord);
                    }
                }
            }
        }
    };
    //this event needs to be attached, or the validator won't work
    depBrandCheck.prototype.txtControl_onchange = function(txtControl){ //lostfocus
        var regels = this.userInput(txtControl).split(String.fromCharCode(13));
        for(var regel in regels){
            var foundWords = regels[regels.length-1].split(" ");
            for(var wordId in foundWords){
                var oldword = this.wordCleanup(foundWords[wordId]);
                var correctedWord = this.validateWord(oldword);
                if((correctedWord != '' && oldword != '') && (correctedWord != oldword)){
                    this.replaceWord(txtControl, oldword, correctedWord);
                }
            }
        }
    };

    depBrandCheck.prototype.validateWord = function(word){
        for(var brand in this.brandlist){
            word = this.wordCleanup(word);
            if(this.brandlist[brand][0].toLowerCase() == word.toLowerCase()){ //brand[0] = word to check
                return this.brandlist[brand][1]; //brand[1] = correct word
            }
        }
        return '';
    };
    
    depBrandCheck.prototype.userInput = function(txtControl){
        if(txtControl.value){return txtControl.value;}
        if(txtControl.innerText){return txtControl.value;}
        return '';
    };
    
    depBrandCheck.prototype.updateUserInput = function(txtControl, newValue){
        if(txtControl.value){txtControl.value = newValue;}
        if(txtControl.innerText){txtControl.innerText = newValue;}
        return '';
    };
    
    depBrandCheck.prototype.showReplaceNotification = function(brandname){
        var a = null;
        if(typeof(this.ctlNotification) != 'undefined'){
            //show this control
            this.ctlNotification.style.display='block';
            //replace $brandname with the correct brand
            this.ctlNotification.innerHTML = this.initialNotificationText.replace('$brandname',brandname);
            //set hiding handlers, autohide after 10 seconds
            this.ctlNotification.onclick=function(){this.style.display='none';}
            setTimeout('$get(\'' + this.ctlNotification.id + '\').style.display=\'none\';$get(\'' + this.ctlNotification.id + '\').onclick=null;',10000);//10 seconds
        }
    }
    
    depBrandCheck.prototype.replaceWord = function(txtControl, oldWord, newWord){
        if(this.showMessage==true){
            if(confirm(message.replace('%oldWord%',oldWord).replace('%newWord%',newWord))){
                var newValue = this.replaceIt(txtControl, oldWord, newWord);
                this.updateUserInput(txtControl, newValue);
                this.showReplaceNotification(newWord);
            }
        }else{
            var newValue = this.replaceIt(txtControl, oldWord, newWord);
            this.updateUserInput(txtControl, newValue);
            this.showReplaceNotification(newWord);
        }
    };
    
    depBrandCheck.prototype.replaceIt = function(txtControl, toMatch, notToMatch){
        var splits = ("|" + notToMatch + "|").split(new RegExp(toMatch, "gim"));
        //replaces 
        if(splits.length == 1){
            var toReplace = this.userInput(txtControl);
            return toReplace.replace(new RegExp(toMatch, "gim"),notToMatch);
        }else{
            var re = new RegExp((splits[0] == '|' ? '' : "(?<!" + splits[0].replace('|','') + ")") + toMatch + 
                    (splits[1] == '|' ? '' : "(?!" + splits[1].replace('|','') + ")") , "gim");
            var toReplace = this.userInput(txtControl);
            return toReplace.replace(re,notToMatch).replace(new RegExp(notToMatch, "gim"), notToMatch);
        }
    }
    
    depBrandCheck.prototype.wordCleanup = function(word){
        word = word.replace('.','');
        word = word.replace(',','');
        word = word.replace('\'','');
        word = word.replace('"','');
        word = word.replace('%','');
        word = word.replace('´','');
        word = word.replace('`','');
        word = word.replace('.','');
        word = word.replace('/','');
        word = word.replace('\\','');
        word = word.replace('[','');
        word = word.replace(']','');
        word = word.replace('(','');
        word = word.replace(')','');
        word = word.replace('{','');
        word = word.replace('}','');
        word = word.replace('@','');
        word = word.replace('&','');
        word = word.replace('#','');
        word = word.replace('^','');
        word = word.replace('!','');
        word = word.replace('?','');
        word = word.replace(':','');
        word = word.replace(';','');
        word = word.replace('+','');
        word = word.replace('*','');
        word = word.replace('=','');
        word = word.replace('_','');
        word = word.replace('>','');
        word = word.replace('<','');
        word = word.replace('\n','');
        return word;
    }
    
    function translateText(key, functionOnComplete){
    /// <summary>sends a request to the webservice which in turn uses a resource file to translate values</summary>
    /// <param name="key">the key as it is listed in the resource file</param>
    /// <param name="functionOnComplete">create a function object which will be called once asynch translation completes
    ///        for example: var onTranslated = function(result, eventArgs){mycontrol.value=result;};
    /// </param>
        var test = CCV.kkServices.Translate(key, functionOnComplete, translateText_Failed);
    }
    function translateText_Failed(result, eventArgs){
        alert(result._message);
    }
}