var SortTypes = {
	stripTagsRE : /<\/?[^>]+>/gi,
	stripRe : /[\$,%]/g, 
	 
    none : function(s){
        return s;
    },
    asText : function(s){
        return String(s).replace(this.stripTagsRE, "");
    },
    asUCText : function(s){
        return String(s).toUpperCase().replace(this.stripTagsRE, "");
    },
    asUCString : function(s) {
    	return String(s).toUpperCase();
    },
    asDate : function(s) {
        if(!s){
            return 0;
        }
        if(s instanceof Date){
            return s.getTime();
        }
        return Date.parse(String(s));
    },
    asFloat : function(s) {
    	var val = parseFloat(String(s).replace(/,/g, ""));
        if(isNaN(val)) val = 0;
    	return val;
    },
    asInt : function(s) {
        var val = parseInt(String(s).replace(/,/g, ""));
        if(isNaN(val)) val = 0;
    	return val;
    },
    
    convert: function (type) 
    {
        var cv;
        switch(type)
        {
            case "":
            case "auto":
            case undefined:
                cv = function(v){ return v; };
                break;
            case "string":
                cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };
                break;
            case "int":
                cv = function(v){
                    return v !== undefined && v !== null && v !== '' ?
                           parseInt(String(v).replace(this.stripRe, ""), 10) : '';
                    };
                break;
            case "float":
                cv = function(v){
                    return v !== undefined && v !== null && v !== '' ?
                           parseFloat(String(v).replace(this.stripRe, ""), 10) : ''; 
                    };
                break;
            case "bool":
            case "boolean":
                cv = function(v){ return v === true || v === "true" || v == 1; };
                break;
            case "date":
                cv = function(v){
                	if(!v){
                        return '';
                    }
                    if(v instanceof Date){
                        return v;
                    }
                   
                    var parsed = mysqlTimeStampToDate(v);
                         return parsed;
                };
             break;
        }
        
        return cv;
    }
};

function sortByProperty(source, property, direction, type){
	
	direction = direction || 'ASC';
    
	var dsc = String(direction).toUpperCase() == "DESC" ? -1 : 1;
	
    var convertFn = SortTypes.convert(type);
    
    var fn = function(r1, r2){
    	var v1 = convertFn(r1[property]), v2 = convertFn(r2[property]);
        var v =  dsc * (  v1 > v2 ? 1 : (v1 < v2 ? -1 : 0)  );
        if(v == 0){
            v = (r1.index < r2.index ? -1 : 1);
        }
        return v;
    } ;
        
    return source.sort(fn);
}

function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1],parts[2],parts[3],parts[4],parts[5]);
  }
