﻿


// Returns the value minus numeric special characters
function utilCleanNumber(text) {
    // Remove special characters
    text = text.replace(/\,/g, '')
    text = text.replace(/\ /g, '')
    text = text.replace(/\$/g, '')
    text = text.replace(/\%/g, '')
    text = text.replace(/\-/g, '')
    text = text.replace(/\(/g, '')
    text = text.replace(/\)/g, '')

    if (isNaN(parseFloat(text))) {
        // If we parse the text and it is not numeric return empty string
        text = ''
    }
    else if (parseFloat(text) == 0) {
        // If we parse the text and get a 0 value return empty string
        text = ''
    }

    return text
}

function utilTrim(value) {
    value = (!value || value == 0 ? '' : value)
    value = value.replace(/^\s+/g, '')
    value = value.replace(/\s+$/g, '')
    return value;
}

function utilParseInt(value) {
    value = (!value || value == 0 ? '' : value)
    value = utilCleanNumber(value.toString())
    return (isNaN(parseInt(value)) ? 0 : parseInt(value));
}

function utilParseFloat(value) {
    value = (!value || value == 0 ? '' : value)
    value = utilCleanNumber(value.toString())
    return (isNaN(parseFloat(value)) ? 0 : parseFloat(value));
}

function utilValidateEmail(value) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

    if (!value.match(re))
        return (false);
    else
        return (true);
}

function utilValidatePhone(value) {
    var phone = utilParseInt(value)

    if (phone.toString().length >= 7 && phone.toString().length <= 11) 
        return true;
    else 
        return false;
}

function MarkInvalid(element, invalid) {

    if (element.tagName == 'INPUT')
        element.className = 'txt'
    else if (element.tagName == 'TEXTAREA')
        element.className = 'txtlarge'

    if (invalid)
        element.className = element.className + 'invalid'
}        
