
// detect the iweb date types
jQuery.fn.dataTableExt.aTypes.unshift(
	function ( sData )
	{
        // remove the html
        sData = typeof sData.replace == 'function' ? sData.replace( /<.*?>/g, "" ) : sData;
    	sData = $.trim(sData);

		if (sData.match(/^(0?[1-9]|[12][0-9]|3[01]).(0?[1-9]|1[012]).(19|20|21)\d\d$/)) {
			return 'iweb_current_date';
		} else if (sData.match(/^\d{4}-\d{2}-\d{2}$/)) {
			return 'iweb_iso_date';
		} else {  //http://www.datatables.net/plug-ins/type-detection#numbers_html
            var sValidFirstChars = "0123456789-";
            var sValidChars = "0123456789.";
            var Char;
            var bDecimal = false;

            /* Check for a valid first char (no period and allow negatives) */
            Char = sData.charAt(0);
            if (sValidFirstChars.indexOf(Char) == -1) {
                return null;
            }

            /* Check all the other characters are valid */
            for ( var i=1 ; i<sData.length ; i++ ) {
                Char = sData.charAt(i);
                if (sValidChars.indexOf(Char) == -1) {
                    return null;
                }

                /* Only allowed one decimal place... */
                if ( Char == "." ) {
                    if ( bDecimal ) {
                        return null;
                    }
                    bDecimal = true;
                }
            }
            return 'num-html';
        }
		return null;
	}
);

jQuery.fn.dataTableExt.oSort['iweb_current_date-asc']  = function(a,b) {
    // remove the html
    var currentDatea = a.replace( /<.*?>/g, "" );
    var currentDateb = b.replace( /<.*?>/g, "" );
	currentDatea = currentDatea.split('.');
	currentDateb = currentDateb.split('.');

	var x = (currentDatea[2] * 10000 + currentDatea[1] * 100 + currentDatea[0]) * 1;
	var y = (currentDateb[2] * 10000 + currentDateb[1] * 100 + currentDateb[0]) * 1;

	return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};

jQuery.fn.dataTableExt.oSort['iweb_current_date-desc'] = function(a,b) {
    // remove the html
    var currentDatea = a.replace( /<.*?>/g, "" );
    var currentDateb = b.replace( /<.*?>/g, "" );
	currentDatea = currentDatea.split('.');
	currentDateb = currentDateb.split('.');

	var x = (currentDatea[2] * 10000 + currentDatea[1] * 100 + currentDatea[0]) * 1;
	var y = (currentDateb[2] * 10000 + currentDateb[1] * 100 + currentDateb[0]) * 1;

	return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};

jQuery.fn.dataTableExt.oSort['iweb_iso_date-asc']  = function(a,b) {
    // remove the html
    var currentDatea = a.replace( /<.*?>/g, "" );
    var currentDateb = b.replace( /<.*?>/g, "" );
	currentDatea = currentDatea.split('-');
	currentDateb = currentDateb.split('-');

	var x = (currentDatea[0] * 10000 + currentDatea[1] * 100 + currentDatea[2]) * 1;
	var y = (currentDateb[0] * 10000 + currentDateb[1] * 100 + currentDateb[2]) * 1;


	return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};

jQuery.fn.dataTableExt.oSort['iweb_iso_date-desc'] = function(a,b) {
    // remove the html
    var currentDatea = a.replace( /<.*?>/g, "" );
    var currentDateb = b.replace( /<.*?>/g, "" );
	currentDatea = currentDatea.split('-');
	currentDateb = currentDateb.split('-');

	var x = (currentDatea[0] * 10000 + currentDatea[1] * 100 + currentDatea[2]) * 1;
	var y = (currentDateb[0] * 10000 + currentDateb[1] * 100 + currentDateb[2]) * 1;

	return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};

//http://www.datatables.net/plug-ins/sorting#numbers_html
jQuery.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
	var x = a.replace( /<.*?>/g, "" );
	var y = b.replace( /<.*?>/g, "" );
	x = parseFloat( x );
	y = parseFloat( y );
	return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};

jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
	var x = a.replace( /<.*?>/g, "" );
	var y = b.replace( /<.*?>/g, "" );
	x = parseFloat( x );
	y = parseFloat( y );
	return ((x < y) ?  1 : ((x > y) ? -1 : 0));
};
