function intLimiter(e) {
    var code = (e.which) ? e.which : e.keyCode;
    
    if ( code > 31 && (code < 48 || code > 57)) {
        return false;
    }
    
    return true;
}

function applyFormat (code_pre, code_suf) {
    if ( navigator.userAgent.indexOf('Firefox') != -1 ) {
        details.document.execCommand('removeFormat', false, null); // Prevents tag overlaps
    }
    
    details.document.execCommand('underline', false, null); // Set a dummy underline tag so we can replace it with the correct tag
    
    var b = navigator.userAgent;
    var c = details.document.body.innerHTML;
    
    if (c.indexOf('<span style="text-decoration: underline;">') != -1 ) {
        c = c.replace(/<span style="text-decoration: underline;">/gi, code_pre);
    } else if ( c.indexOf('<u>') != -1 || c.indexOf('<U>') != -1 ) {
        c = c.replace(/<u>/gi, code_pre);
        c = c.replace(/<\/u>/gi, code_suf);
    }
    
    details.document.body.innerHTML = c;
}

function setColor (element) {
    var color = (element.value == 'CUSTOM') ? prompt('Enter a color or hexadecimal value (include pound if hexadecimal value.') : element.value;
    var pound = (element.value == 'CUSTOM') ? "" : '#';
    
    applyFormat('<span style="color:' + pound + color + '">', '</span>');
    
    element.selectedIndex = 0;
}

function processData () {
    if ( typeof details.document.body.innerText == 'undefined' ) {
        if ( strip_tags(details.document.body.textContent) == "" ) {
            alert('Please enter details about your character.');
            return false;
        }
    }
    else if ( strip_tags(details.document.body.innerText) == "" ) {
        alert('Please enter details about your character.');
        return false;
    }
    
    hiddenElement = document.createElement('input');
	hiddenElement.type = 'hidden';
	hiddenElement.name = 'details';
	hiddenElement.value = details.document.body.innerHTML;
	document.forms[1].appendChild(hiddenElement);
	document.forms[1].submit();
    return true;
}

function bold () {
    applyFormat('<b>', '</b>');
}

function italic () {
    applyFormat('<i>', '</i>');
}

function remove () {
    details.document.execCommand('removeFormat', false, null);
}

// This code obtained from http://phpjs.org/functions/strip_tags:535
function strip_tags (input, allowed) {
    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) { return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
    });
}
