/**
 * Utility for moderntrade project
 *
 **/

String.prototype.ltrim = function () {  return this.replace(/\s*((\S+\s*)*)/, "$1");}
String.prototype.rtrim = function() { return this.replace(/((\s*\S+)*)\s*/, "$1");}
String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, ""); }

document.setCookie = function(name, value, days, path) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    if(!path) path = "/";
    this.cookie = name + "=" + value + expires + "; path="+path;
}

document.getCookie = function(name) {
    var nameEQ = name + "=";
    var ca = this.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

document.clearCookie = function(name) {
    this.setCookie(name, "", -1);
}

jQuery.fn.check = function() {
    return this.each(function() { this.checked = true; });
};

jQuery.fn.uncheck =  function() {
    return this.each(function() { this.checked = false; });
};

jQuery.fn.dataFormToObject = function() {
    var val = {};
    this.each(function() { jQuery(":input:not(:button)", this).each(function(){ val[this.name]=this.value; }); });
    return val;
};

jQuery.fn.serializeJson = function() {
    var val = {};
    this.each(function() {
        if($(this).is(':input')) {
            val[this.name]=this.value;
        } else {
            jQuery(":input:not(:button)", this).each(function(){ val[this.name]=this.value; });
        }
    });
    return val;
}

jQuery.fn.disabled = function() { return this.each(function() { this.disabled = true; }); };

jQuery.fn.enable = function(b) {
    if (b == undefined) b = true;
    return this.each(function() {
        this.disabled = !b
    });
};

if(typeof jQuery.fn.buttonStyle == "undefined") {
    jQuery.fn.buttonStyle = function() {
        this.each(function(){
            if($(this).is(':button,:submit,:reset')) {
               $(this).hover(function(){ $(this).addClass('btn_tools_focus'); },function(){ $(this).removeClass('btn_tools_focus'); })
               .focus(function(){ this.hideFocus=true; $(this).addClass('btn_tools_focus'); })
               .blur(function(){ $(this).removeClass('btn_tools_focus'); });
            }
        });
    }
}

function commaFormatted(data) {
    var delimiter = ","; // replace comma if desired
    var a = data.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n =  i.toString();
    var a = [];
    while(n.length > 3) {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { data = n; }
    else { data = n + '.' + d; }
    data = minus + data;
    return data;
}

function currencyFormatted(data) {
    var i = parseFloat(data);

    if(isNaN(i)) { i = 0.00; }

    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    var s = i.toString();
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;

    return s;
}
var errNumber = 'Please input character 0-9 only!';
function isNumber(obj){
	 var testresult = null;
	 if(obj.value == ''){
	      return null;
	 }else{
	      var anum=/(^\d+$)|(^\d+\.\d+$)/
	      if (anum.test(obj.value))
		    testresult=true;
	      else{
		    alert(errNumber);
	        testresult=false;
	        obj.value = '';
	      }
	    return (testresult);
	 }
}
