/*
'Author					: Stefan Kruger
'Date Created			: 13-May-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 23-May-2005
'Version				: 1.0.23052005
'Comments:
'Date:			Desc:															
'--------------------------------------------------------------------------------------------
'13-May-2005	Created a custom input class (after a couple of tries))
'23-May-2005	Created a custom table class that includes paging to replace the changed
				grid default one - TO RESTORE COOPERATION WITH FUTURE GRID RELEASES

'ToDoList:
'Date:			Desc:															Status:
'--------------------------------------------------------------------------------------------

/*
Custom grid classes
*/

if (!window.Expert) var Expert = {};
if (!Expert.Templates) Expert.Templates = {};
if (!Expert.Text) Expert.Text = {};

Expert.Text.Table = Active.HTTP.Request.subclass();
Expert.Text.Table.create = function(){
	var obj=this.prototype;
	/*Start new properties*/
	obj.defineProperty("currentPage","");
	obj.defineProperty("maxPage","");
	obj.defineProperty("rowLimit","");
	obj.defineProperty("recordCount","");
	/*End new properties*/
	var _super=this.superclass.prototype;
	obj.response = function(input){//Parameter used to be called "text"
		/*Start new code*/
		//Handle paging info
		var self = this;
		var text = "";
		var splitArray;
		//Workaround for filtering blank result set error.
		if (input != "") {
			splitArray = input.split("|^^|")
		}
		else {
			splitArray = [""];
		}
		//Process array and display
		//Case : Only one split element --> Assume no paging info present, only data.
		if (splitArray.length == 1) {
			//Set grid datatext
			self.setProperty("currentPage",1);
			self.setProperty("maxPage",1);
			if (splitArray[0]) {
				text = splitArray[0];
			}
		}
		//Case : Two split elements --> Assume paging first, then data.
		else if (splitArray.length == 2) {
			//Set properties
			if (splitArray[0]) {
				var piSplit = splitArray[0].split("^||^");
				if (piSplit.length == 4) {
					self.setProperty("currentPage",parseInt(piSplit[0]));
					self.setProperty("maxPage",parseInt(piSplit[1]));
					self.setProperty("rowLimit",parseInt(piSplit[2]));
					//Update recordCount to reflect the page size limit
					if (parseInt(piSplit[3]) > parseInt(piSplit[2])){
						self.setProperty("recordCount",parseInt(piSplit[2]));
					}
					else {
						self.setProperty("recordCount",parseInt(piSplit[3]));
					}
				}
			}
			//Set grid datatext
			if (splitArray[1]) {
				text = splitArray[1];
			}
		}
		//Else : Unrecognised response --> Don't do anything.
		else {
			return;
		}
		/*End new code*/
		
		//Now process the table bound data
		var i,s,table=[],a=text.split(/\r*\n/);
		var pattern=new RegExp("(^|\\t|,)(\"*|'*)(.*?)\\2(?=,|\\t|$)","g");
		for(i=0;i<a.length;i++){
			s=a[i].replace(/""/g,"'");
			s=s.replace(pattern,"$3\t");
			s=s.replace(/\t$/,"");
			if(s){table[i]=s.split(/\t/)}
		}
		this._data=table;
		_super.response.call(this)
	};
	obj._data=[];
	obj.getCount=function(){return this._data.length};
	obj.getIndex=function(i){return i};
	obj.getText=function(i,j){
		return this._data[i][j];
	};
	obj.getImage=function(){return "none"};
	obj.getLink=function(){return ""};
	obj.getValue=function(i,j){
		var text=this.getText(i,j);
		var value=Number(text.replace(/[,%\$]/gi,"").replace(/\((.*)\)/,"-$1"));
		return isNaN(value)?text.toLowerCase()+" ":value
	}
};
Expert.Text.Table.create();

/****************************************************************
    Input Cell template.
*****************************************************************/

Expert.Templates.Input = Active.System.Template.subclass();
Expert.Templates.Input.create = function(){
    var obj = this.prototype;
    var _super = this.superclass.prototype;

    obj.getName = function() {
        var r = this.$owner.$index;
        var c = this.getColumnProperty("index");
        return "item_"+r+"_"+c;
    }
    obj.setTag("input");
    obj.setClass("templates","input");
    obj.setClass("input",function(){return this.getColumnProperty("index")});
    obj.setAttribute("type","text");
    obj.setAttribute("value",function(){return this.getItemProperty("text");});
    obj.setAttribute("name", function(){return obj.getName.call(this);})
    obj.setEvent("onblur", function(event, src) {this.onBlurAction( event, src );});
    obj.setEvent("onfocus", function(event, src) {this.onFocusAction( event, src );});
	
	obj.onFocusAction = function(event) {
		var currentVal = this.element().value;
        if (currentVal == 0) {
			this.element().value = "";
        }
        this.element().select();
        /*alert((this.$owner).$owner);
        alert((this.$owner).$owner instanceof Active.Text.Table);*/
    }
    obj.onBlurAction = function(event) {
        var name = this.element().name;
        var row = this.getRowProperty("index");
        //var col = this.getColumnProperty("index");
        //var originalVal = this.getItemProperty("text");
        var currentVal = this.element().value;
        
        /*Start New*/
		var prodid = this.getDataProperty("text",2,row);
		var price = this.getDataProperty("text",4,row);
        
        //Error checking and writeback
        if (isNaN(currentVal) || currentVal == "") {
			currentVal = 0;
			this.element().value = currentVal;
        }
        else {
			currentVal = parseInt(Math.round(currentVal));
			this.element().value = currentVal;
        }
        /*End New*/
        
        //if (currentVal!=originalVal) {
			this.onChangeAction(currentVal,prodid,price);
        //}
    }

    obj.onChangeAction = function(newval,prodid,price) {
         doGridBasketAdd(newval,prodid,price);
    }
};

Expert.Templates.Input.create(); 

Expert.Templates.Input2 = Active.System.Template.subclass();
Expert.Templates.Input2.create = function(){
    var obj = this.prototype;
    var _super = this.superclass.prototype;

    obj.getName = function() {
        var r = this.$owner.$index;
        var c = this.getColumnProperty("index");
        return "item_"+r+"_"+c;
    }
    obj.setTag("input");
    obj.setClass("templates","input");
    obj.setClass("input",function(){return this.getColumnProperty("index")});
    obj.setAttribute("type","text");
    obj.setAttribute("value",function(){return this.getItemProperty("text");});
    obj.setAttribute("name", function(){return obj.getName.call(this);})
    obj.setEvent("onblur", function(event, src) {this.onBlurAction( event, src );});
    obj.setEvent("onfocus", function(event, src) {this.onFocusAction( event, src );});
	
	obj.onFocusAction = function(event) {
		var currentVal = this.element().value;
        if (currentVal == 0) {
			this.element().value = "";
        }
        this.element().select();
        /*alert((this.$owner).$owner);
        alert((this.$owner).$owner instanceof Active.Text.Table);*/
    }
    obj.onBlurAction = function(event) {
        var name = this.element().name;
        var row = this.getRowProperty("index");
        //var col = this.getColumnProperty("index");
        //var originalVal = this.getItemProperty("text");
        var currentVal = this.element().value;
        
        /*Start New*/
		var prodid = this.getDataProperty("text",2,row);
		var price = this.getDataProperty("text",6,row);
        
        //Error checking and writeback
        if (isNaN(currentVal) || currentVal == "") {
			currentVal = 0;
			this.element().value = currentVal;
        }
        else {
			currentVal = parseInt(Math.round(currentVal));
			this.element().value = currentVal;
        }
        /*End New*/
        
        //if (currentVal!=originalVal) {
			this.onChangeAction(currentVal,prodid,price);
        //}
    }

    obj.onChangeAction = function(newval,prodid,price) {
         doGridBasketAdd(newval,prodid,price);
    }
};

Expert.Templates.Input2.create(); 

// ****************************************************************
// Other now reduddant sample input cell templates.
// ****************************************************************

/*
Expert.Templates.Input = Active.Templates.Text.subclass();

Expert.Templates.Input.create = function(){
    var obj = this.prototype;
	//editor is not part of the template,
	//there is only one single instance of editor object.
    var editor = new Active.HTML.INPUT;
    editor.setClass("templates", "input");
    editor.setAttribute("type", "text");
    editor.setAttribute("value", function(){
        return template.getItemProperty("text");
    });
	//template variable provides temporary reference
	//to the parent template during edit mode.
    var template;

    function switchToEditMode(){
		alert(template);
        if (template) {
            //switchToTextMode();
        }
        template = this;
        template.element().style.padding = 0;
        template.element().innerHTML = editor;
        editor.element().focus();
		editor.setEvent("ondblclick", switchToTextMode);
		editor.setEvent("onselectstart", null);
    }

    obj.setEvent("onfocus", switchToEditMode);

    function switchToTextMode(){
        var value = editor.element().value;
		var originalText = template.getItemProperty("text");
		if (originalText != value) {
			//alert(template.getItemProperty("text"));
			try {
				template.setItemProperty("text",value);
			}
			catch (ex) {
				alert(template.getItemProperty("text"));
				alert(template);
				alert(editor);
			}
			template.refresh();
			template.action("changeInputAction");
		}
		else {
			template.refresh();
		}
		template = null;
    }

    editor.setEvent("onblur", switchToTextMode);
};

Expert.Templates.Input.create();*/
//////////////////////////////////////////////

//////////////////////////////////////////////////////////
/*
Expert.Templates.Input3 = Active.Templates.Text.subclass();

Expert.Templates.Input3.create = function() {
    var obj = this.prototype;
	//editor is not part of the template,
	//there is only one single instance of editor object.
    var editor = new Active.HTML.INPUT;
    editor.setClass("templates", "input");
    editor.setAttribute("type", "text");
    editor.setAttribute("value", function(){
        return template.getItemProperty("text");
    });

	//template variable provides temporary reference
	//to the parent template during edit mode.
    var template;

    function switchToEditMode(){
        if (template) {
            switchToTextMode()
        }
        template = this;
        template.element().style.padding = 0;
        template.element().innerHTML = editor;
        editor.element().focus();
    }

    obj.setEvent("onclick", switchToEditMode);

    function switchToTextMode(){
        var value = editor.element().value;
        template.setItemProperty("text", value);
        template.refresh();
        template = null;
    }

    editor.setEvent("onblur", switchToTextMode);
};

Expert.Templates.Input3.create(); 
//////////////////////////////////////////////////////////

Expert.Templates.InputDraft = Active.Templates.Text.subclass();

Expert.Templates.InputDraft.create = function() {
    var obj = this.prototype;
    obj.template = null;
	//editor is not part of the template,
	//there is only one single instance of editor object.
    var editor = new Active.HTML.INPUT;
    editor.setClass("templates", "input");
    editor.setAttribute("type", "text");
    editor.setAttribute("value", function(){
        return obj.template.getItemProperty("text");
    });

	//template variable provides temporary reference
	//to the parent template during edit mode.
    //var template;

    function switchToEditMode(){
		//var self = this;
        if (obj.template) {
            switchToTextMode();
        }
        alert(this instanceof Active.Templates.Text);
        obj.template = this;
        obj.template.element().style.padding = 0;
        obj.template.element().innerHTML = editor;
        //alert(obj.template.getItemProperty("text"));
        //obj.template.setItemProperty("text","-2");
        //alert(obj.template.getItemProperty("text"));
        editor.element().focus();
    }

    obj.setEvent("onclick", switchToEditMode);

    function switchToTextMode(){
        var value = editor.element().value;
        var myobj = new Active.Templates.Text;
        //obj.template.setItemProperty("text", value);
        //Attempted workaround
        
        //obj.template.element().innerHTML = value;
        alert(value);
        alert(obj.template.innerHTML());
        alert(obj.template.element());
        alert(obj.template.element().innerHTML);
		alert(obj.template.getItemProperty("text"));
		
        obj.template.refresh();
        obj.template = null;
    }

    editor.setEvent("onblur", switchToTextMode);
};

Expert.Templates.InputDraft.create(); */
