/**
 * String Editor
 */
function StringEditor(inParams){
    this.params = inParams;
    this.editor = null;
}

StringEditor.prototype.getEditor = function(){
    if (this.editor == null){
        this.editor = window.document.createElement("INPUT");
        this.editor.type = "text";
        this.editor.className = "cssText";
        this.editor.style.backgroundColor = "white";

        if (this.params != null){
            this.params = this.params.split(",");
            this.editor.maxLength = this.params[0];

            if(this.params.length > 1){
                switch (this.params[1]){
                case "U":
                    this.editor.onkeypress = UpperCaseHandler;
                    break;
                case "L":
                    this.editor.onkeypress = LowerCaseHandler;
                    break;
                }
            }
        }
    }

    return this.editor;
}

StringEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.value = data;
    } else {
        this.editor.value = _DEFAULT_NULL;
    }
}

StringEditor.prototype.getValue = function(){
    if (this.editor.value == _DEFAULT_NULL){
        return null;
    } else {
        return this.editor.value;
    }
}

StringEditor.prototype.focus = function(){
    this.editor.focus();
    this.editor.select();
}

/**
 * Number Editor
 */
function NumberEditor(inParams){
    this.params = inParams;
    this.integerDigits = 9;
    this.fractionDigits = 0;
    this.editor = null;
}

NumberEditor.prototype.getEditor = function(){
    if (this.editor == null){
        this.editor = window.document.createElement("INPUT");
        this.editor.type = "text";
        this.editor.className = "cssNumText";
        this.editor.style.backgroundColor = "white";
        this.editor.style.paddingRight = 2;

        if (this.params != null){
            this.params = this.params.split(",");
            this.integerDigits = parseInt(this.params[0]);

            if(this.params.length > 1){
                this.fractionDigits = parseInt(this.params[1]);
            }
        }

        this.editor.onkeypress = NumberKeyHandler;
    }

    return this.editor;
}

NumberEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.value = data;
    } else {
        this.editor.value = _DEFAULT_NULL;
    }
}

NumberEditor.prototype.getValue = function(){
    if (this.editor.value == _DEFAULT_NULL){
        return null;
    } else {
        return NumberHandler(this.editor.value, this.integerDigits, this.fractionDigits);
    }
}

NumberEditor.prototype.focus = function(){
    this.editor.focus();
    this.editor.select();
}

/**
 * Boolean Editor
 */
function BooleanEditor(){
    this.editor = null;
}

BooleanEditor.prototype.getEditor = function(){
    if (this.editor == null){
        this.editor = window.document.createElement("INPUT");
        this.editor.type = "checkbox";
        this.editor.style.textAlign = "center";
    }

    return this.editor;
}

BooleanEditor.prototype.setValue = function(data){
    this.editor.checked = (data == "true");
}

BooleanEditor.prototype.getValue = function(){
    return this.editor.checked.toString();
}

BooleanEditor.prototype.focus = function(){
    this.editor.focus();
}

/**
 * Time Editor
 */
function TimeEditor(inParams){
    this.editor = null;
}

TimeEditor.prototype.getEditor = function(){
    if (this.editor == null){
        this.editor = window.document.createElement("INPUT");
        this.editor.type = "text";
        this.editor.className = "cssText";
        this.editor.maxLength = 4;
        this.editor.style.backgroundColor = "white";
        this.editor.style.textAlign = "right";
        this.editor.style.paddingRight = 2;

        this.editor.onkeypress = DateTimeKeyHandler;
    }

    return this.editor;
}

TimeEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.value = data;
    } else {
        this.editor.value = _DEFAULT_NULL;
    }
}

TimeEditor.prototype.getValue = function(){
    if (this.editor.value == _DEFAULT_NULL){
        return null;
    } else {
        return TimeHandler(this.editor.value);
    }
}

TimeEditor.prototype.focus = function(){
    this.editor.focus();
    this.editor.select();
}

/**
 * Id Editor
 */
function IdEditor(){
    this.data = null;
    this.editor = null;
}

IdEditor.prototype.getEditor = function(){
    if (this.editor == null){
        this.editor = window.document.createElement("INPUT");
        this.editor.type = "checkbox";
        this.editor.style.textAlign = "center";
    }

    return this.editor;
}

IdEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.disabled = false;
        this.editor.checked = (data.split("^")[1] == "1");
        this.data = data;
    } else {
        this.editor.disabled = true;
    }
}

IdEditor.prototype.getValue = function(){
    var data = this.data;
    if (data != null){
        data = setIdSelected(data, this.editor.checked);
    }

    return data;
}

IdEditor.prototype.focus = function(){
    if (!this.editor.disabled){
        this.editor.focus();
    }
}

/**
 * Select Editor
 */
function SelectEditor(inEditorId){
    this.editor = null;
    this.editorTemp = null;
    this.editorId = inEditorId;
}

SelectEditor.prototype.getEditor = function(){
    if (this.editorTemp == null){
        this.editorTemp = document.all(this.editorId);
    }

    this.editor = this.editorTemp.cloneNode(true);
    this.editor.style.display = "block";
    this.editor.style.width = "100%";

    return this.editor;
}

SelectEditor.prototype.setValue = function(data){
    for (var i=0; i < this.editor.options.length; i++){
        if (this.editor.options[i].value == data){
            this.editor.options[i].selected = true;
            break;
        }
    }
}

SelectEditor.prototype.getValue = function(){
    if (this.editor.selectedIndex >= 0){
        return this.editor.value;
    } else {
        return null;
    }
}

SelectEditor.prototype.focus = function(){
    this.editor.focus();
}

/**
 * Date Editor
 */
function DateEditor(inParams){
    this.editorTemp = null;
    this.editor = null;
    this.popup = new DatePopup();

    this.max = null;
    this.min = null;

    if (inParams != null && inParams.length > 0){
        var params = inParams.split(",");
        this.min = params[0];
        this.max = params[1];
    }
}

DateEditor.prototype.getEditor = function(){
    if (this.editorTemp == null){
        this.editorTemp = window.document.createElement("DIV");
        this.editorTemp.style.width = "100%";

        var dateInput = window.document.createElement("INPUT");
        dateInput.type = "text";
        dateInput.className = "cssText";
        dateInput.style.backgroundColor = "white";
        dateInput.style.width = "75%";
        this.editorTemp.appendChild(dateInput);

        var dateBtn = window.document.createElement("INPUT");
        dateBtn.type = "button";
        dateBtn.className = "popBtn";
        dateBtn.value = "...";
        dateBtn.skipTab = true;
        this.editorTemp.appendChild(dateBtn);
    }

    this.editor = this.editorTemp.cloneNode(true);
    this.editor.children[0].onkeypress = DateTimeKeyHandler;

    this.popup.setSrcElement(this.editor.children[1]);

    return this.editor;
}

DateEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.children[0].value = data.replace(/-/g, "");
    } else {
        this.editor.children[0].value = _DEFAULT_NULL;
    }
}

DateEditor.prototype.getValue = function(){
    if (this.editor.children[0].value == _DEFAULT_NULL){
        return null;
    } else {
        return DateHandler(this.editor.children[0].value, this.min, this.max);
    }
}

DateEditor.prototype.focus = function(){
    this.editor.children[0].focus();
    this.editor.children[0].select();
}

/**
 * Party Editor
 */
function PartyEditor(inParams){
    this.editorTemp = null;
    this.editor = null;

    if (inParams != null && inParams.length > 0){
        this.params = inParams;
    }
}

PartyEditor.prototype.getEditor = function(){
    if (this.editorTemp == null){
        this.editorTemp = window.document.createElement("DIV");
        this.editorTemp.style.width = "100%";

        var dateInput = window.document.createElement("INPUT");
        dateInput.className = "cssText";
        dateInput.style.backgroundColor = "white";
        dateInput.style.width = "75%";
        dateInput.onkeypress = UpperCaseHandler;
        dateInput.maxLength = "12";
        this.editorTemp.appendChild(dateInput);

        var dateBtn = window.document.createElement("INPUT");
        dateBtn.type = "button";
        dateBtn.className = "popbtn";
        dateBtn.value = "...";
        dateBtn.skipTab = true;
        dateBtn.asEditor = true;
        this.editorTemp.appendChild(dateBtn);
    }

    this.editor = this.editorTemp.cloneNode(true);

    if (typeof window._PartyPopup == "undefined"){
        window._PartyPopup = new PartyPopup();
    }
    window._PartyPopup.boundFields = this.editor.firstChild;

    this.editor.children[1].onclick = window._PartyPopup.doClick;

    return this.editor;
}

PartyEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.children[0].value = data;
    } else {
        this.editor.children[0].value = _DEFAULT_NULL;
    }
}

PartyEditor.prototype.getValue = function(){
    if (this.editor.children[0].value == _DEFAULT_NULL){
        return null;
    } else {
        var params = this.params.split("|");

        var returnValue = new ActiveXObject("Scripting.Dictionary");
        if (typeof this.editor.children[0].returnValue != "undefined"){
            for (var i=0; i<params.length; i++){
                var paramPart = params[i].split("=");
                returnValue.Add(paramPart[1], this.editor.children[0].returnValue.Item(paramPart[0]));
            }
        }

        return returnValue;
    }
}

PartyEditor.prototype.focus = function(){
    this.editor.children[1].focus();
}

/**
 * SKU Editor
 */
function SKUEditor(inParams){
    this.editorTemp = null;
    this.editor = null;

    if (inParams != null && inParams.length > 0){
        this.params = inParams;
    }
}

SKUEditor.prototype.getEditor = function(){
    if (this.editorTemp == null){
        this.editorTemp = window.document.createElement("DIV");
        this.editorTemp.style.width = "100%";

        var dateInput = window.document.createElement("INPUT");
        dateInput.className = "cssText";
        dateInput.style.backgroundColor = "white";
        dateInput.style.width = "75%";
        dateInput.onkeypress = UpperCaseHandler;
        dateInput.maxLength = "20";
        this.editorTemp.appendChild(dateInput);

        var dateBtn = window.document.createElement("INPUT");
        dateBtn.type = "button";
        dateBtn.className = "popbtn";
        dateBtn.value = "...";
        dateBtn.partyid = this.params.split("|")[0].split("=")[1];
        
        //check if the searchfields option is in the editorparms; 
        editoparams = this.params.split("|");
        myEditorParams = new String(editoparams)
        extenstion=myEditorParams.indexOf("searchfields")
        if (editoparams == null || extenstion == -1) {
        } else {
			dateBtn.searchfields = this.params.split("|")[1].split("=")[1];
		}
		//...
        dateBtn.skipTab = true;
        this.editorTemp.appendChild(dateBtn);
    }

    this.editor = this.editorTemp.cloneNode(true);

    this.editor.children[1].onclick = function(){
        var params = new ActiveXObject("Scripting.Dictionary");
        params.Add("sku", event.srcElement.previousSibling.value);

        if (typeof event.srcElement.partyid != "undefined"){
            eval("params.Add(\"partyid\", " + event.srcElement.partyid + ");");
        }

        if (typeof event.srcElement.searchfields != "undefined"){
            eval("params.Add(\"searchfields\", " + event.srcElement.searchfields + ");");
        }

        window.top.showPopup('sku', 700, 450, '/portal/common/skusearch.do', params, event.srcElement);
    };

    this.editor.children[1].doClose = function(srcObj, returnValue){
        if(typeof srcObj != "undefined"){
            if (returnValue != null && typeof returnValue != "undefined"){
                srcObj.previousSibling.value = returnValue.Item("sku");
                srcObj.previousSibling.returnValue = returnValue;
            }

            srcObj.previousSibling.focus();
            srcObj.previousSibling.select();
        }
    }

    return this.editor;
}

SKUEditor.prototype.setValue = function(data){
    if (data != null){
        this.editor.children[0].value = data;
    } else {
        this.editor.children[0].value = _DEFAULT_NULL;
    }
}

SKUEditor.prototype.getValue = function(){
    if (this.editor.children[0].value == _DEFAULT_NULL){
        return null;
    } else {
        var params = this.params.split("|");

        var returnValue = new ActiveXObject("Scripting.Dictionary");

        if (typeof this.editor.children[0].returnValue != "undefined"){
            for (var i=0; i<params.length; i++){
                var paramPart = params[i].split("=");
                returnValue.Add(paramPart[1], this.editor.children[0].returnValue.Item(paramPart[0]));
            }
        }

        return returnValue;
    }
}

SKUEditor.prototype.focus = function(){
    this.editor.children[1].focus();
}