﻿function NParams(initStr)
{
    this.keys = Array();
    if (initStr.indexOf(';') != -1) {
        var params = initStr.split(';');
        for (var i = 0; i < params.length; i = i + 2) {
            this[params[i]] = params[i + 1];
            this.keys[this.keys.length] = params[i];
        }
    }


    this.add = function(key, value) {
        if (key == 'keys') {
            return false;
        }
        if (this[key] == null) {
            this.keys[this.keys.length] = key;
        }

        this[key] = value;
        return true;
    }
    
    this.change = function(key , value)
    {
        var naslo = false;
        for(var i = 0; i < this.keys.length; i++)
        {
            if(this.keys[i] == key)
            {
                naslo = true;
            }
        }
        if(naslo == true)
        {
            this[key] = value;
            return true;
        }
        return false;
    }
    
    this.addOrChange = function(key , value)
    {
        if( this.change(key,value) == false)
        {
            this.add(key,value);
            return 1;
        }
        return 0;
    }
    
    this.toString = function()
    {
        var output = "";
        for(var i = 0; i < this.keys.length; i++)
        {
            if(output != "")
            {
                output += ";";
            }
            output += this.keys[i] + ";" + this[this.keys[i]];
        } 
        return output;
    }
    
    this.changeFE  = function(input,fireEvent)
    {
        var output = {}
        for(prop in input)
        {
            var zarad = false;
            var naslo = false;
            for (var i = 0; i < this.keys.length; i++) {
                if (this.keys[i] == prop) {
                    naslo = true;
                }
            }
            if (naslo == true) {
                if(this[prop]!=input[prop]){
                    zarad = true;
                    this[prop] = input[prop];
                }
                
            }
            else{
                zarad = true;
                this.add(prop, input[prop])    
            }
            if(zarad == true){
                output[prop] = input[prop]
            }
            
        }
        if(fireEvent == true){
            barISPage.fireEvent('nParamsChanged',output);
        }
        return output;
    }
}

function ChainComboNParam(sourceId, targetId, targetNParam)
{
    var source = $get(sourceId);
    try
    {
        source.attachEvent('onchange',function(){AddNParamToCombo(sourceId, targetId, targetNParam)});
    }
    catch(err)
    {
        source.addEventListener('onchange',function(){alert('halo')});
    }
}

function AddNParamToCombo(sourceId,targetId, targetNParam)
{
    var source = $get(sourceId + "VAL");
    var target = $get(targetId);
    if((source != null) && (target != null))
    {
        var beh = Sys.UI.Behavior.getBehaviors(target)
        if( beh != null && beh.length > 0 )
        {
            var tempNParams = new NParams(beh[0]._extraParam);
            tempNParams.addOrChange(targetNParam,source.value.replace(/'/gi,""));
	        beh[0]._extraParam = tempNParams.toString();
        }
        else
        {
            return;
        }
    }
    return;
}
