/*
    Copyright (C) 2008  Ivo Toby, Internet Voor Ondernemers, http://www.i-v-o.nl / http://syntacticsugar.nl 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Crudder Version : 0.50b
 */


	var crudderField = Class.create({
		
		initialize:function(field, data, pob){
			this.field = field;
			this.data = data;
			this.pob = pob;
			this.pKey = this.pob.pKey;
			this.sorted = '';
			this.shouldStore = true;
			this.controller = this.pob.controller;
			this.instID = 'crudderField.' + parseInt(Math.random()*1000000);
		},
		
		drawGridHead:function(container){
			var div = new Element('div').update(this.field.friendlyName);
			var img = new Element('img', {src:this.controller.imgPath + 'arrow_down.gif', id:this.field.name+"_down"});
			div.insert(img);
			if (this.sorted != 'desc') img.hide();

			var img = new Element('img', {src:this.controller.imgPath + 'arrow_up.gif', id:this.field.name+"_up"});
			div.insert(img);
			if (this.sorted != 'asc') img.hide();

			var td = new Element('td').update(div);
			container.insert(td);
			td.observe('click', this.sort.bind(this));
		},
		
		sort : function(event){
			// called from header; delegate to table with myself as argument;
			if (this.sorted == 'desc'){
				this.sorted = 'asc';
				$(this.field.name+"_down").show();
				$(this.field.name+"_up").hide();
			}else{
				this.sorted = 'desc';
				$(this.field.name+"_down").hide();
				$(this.field.name+"_up").show();
			}
			this.pob.sort(this);
		},
		
		reset : function(){
			$(this.field.name+"_down").hide();
			$(this.field.name+"_up").hide();
			this.sorted = '';
		},
		
		getTitle : function(){
			var div= new Element('div');
			div.insert(this.field.friendlyName);
			if (this.field.not_null == 1){
				div.insert(" *");
			}
			this.errContainer = new Element('div', {className:'crudderError', id:this.instID+"_error"});
			div.insert(this.errContainer);
			this.errContainer.hide();
			return div;
		},

		drawEditor : function(container,record){
			var value = '';
			if (record[this.field.name]){
				value = record[this.field.name];
			}else if (this.field.value && this.field.value !='' ){
				value = this.field.value;
			}
			value = stripslashes(value);
			value = value.toString().trim();
			this.input = new Element('input', {type:'text', className:"crudderRecordInput", value:value});
			if (this.field.metaType.indexOf('text') > -1){
				if (this.field.max_length){
					this.input.setAttribute("maxlength", this.field.max_length);
				}
			}
			container.insert(this.input);
		},
		
		validate : function(){
			var ok = true;
			if (!this.shouldStore) return true;

			var value = this.input.getValue();
			value = value.replace(' ', '');
			
			if (this.field.metaType.indexOf('text') > -1){
				// varchar field, check length of value;
				if (value.toString().length > this.field.max_length){
					var text = this.controller.lang.get('error_toolong')
					this.setError(text + this.field.max_length);
					return false;
				}
			}
			
			if (this.controller.metaData.config.thoroughCheck){
				if (!this.field.nullable || this.field.nullable == ''){
					if (value == ''){
						var text = this.controller.lang.get('error_mandatory')
						this.setError(text);
						return false;
					}else{
						ok = true;
					}
				}
			}
			
			if (this.field.validations && this.field.validations.length){
				this.field.validations.each(
					(function(validationStr){
						var isValid = this[validationStr](this.getRPCValue());
						if (isValid !== true){
							ok = false;
							this.setError(isValid);
						}else{
							this.clearError();
						}
					}).bind(this)
				)
				return ok;
			}else{
				return true;
			}
		},
		
		setError: function(message){
			if (this.input){
				this.input.setStyle('backgroundColor:#ff5656;');
			}
			this.errContainer.update(message);
			this.errContainer.show();
		},
		
		clearError : function(){
			this.errContainer.update('');
			this.errContainer.hide();
		},
		
		getRPCValue: function(){
			if (this.input){
				return '<![CDATA[' + encodeURIComponent(this.input.getValue()) + "]]>";
			}
		},
		
		close:function(){}
		
	})
