// Trim function
String.prototype.trim = function() {
    return ((ar = /^\s*([\s\S]*\S+)\s*$/.exec(this)) ? ar[1] : "");
}

String.prototype.startsWith = function(str) { return (this.match("^" + str) == str) }
String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

function isValidFileExtension(string, extWithoutDot) {
    return ((string.toUpperCase()).lastIndexOf(extWithoutDot.toUpperCase()) != -1);
}

// check for empty strings as well as declare it to be invalid if first char is space
function check_empty(string) {
    if (string.trim() == "")
    // true means invalid
        return true;
    else
        return false;
}

// used if the length of string is to be fixed example Year
function check_length(string, nLength) {
    if (string.length != nLength)
        return false;
    else
        return true;
}

// check day, month and year making sure they are valid
function check_date(day, mth, year) {
    if (day == "0" || mth == "0" || year == "0")
        return false;

    // months of Apr, Jun, Sep and Nov have 31 days
    if (mth == "4" || mth == "6" ||
	     mth == "9" || mth == "11") {
        if (day == "31")
            return false;
    }
    // check for Feb
    else if (mth == "2") {
        // leap year
        if ((eval(year) % 4) == 0) {
            // max no. of days is 29
            if (eval(day) > 29) {
                return false;
            }
        }
        // non leap year
        else {
            // max no. of days is 28
            if (eval(day) > 28) {
                return false;
            }
        }
    }

    return true;
}

// check if string is numeric
function IsNumeric(string) {
    var i;
    var nLength = string.length;
    var valid = true;

    for (i = 0; i < nLength; i++) {
        if (string.charAt(i) >= '0' && string.charAt(i) <= '9')
            continue;
        else {
            valid = false;
            break;
        }
    }

    return valid;
}

// check if string is a float
function IsFloat(string) {
    if (isNaN(string)) return false;
    string = string / 1
    return (("" + parseFloat(string)) == string);
}

// validates email
function check_email(string) {
    var nPos;

    nPos = string.indexOf('@');

    if (string.charAt(0) != ' ') {
        if (nPos == -1 || nPos == 0)
            return false;
        else {
            if (string.lastIndexOf('@') > nPos)
                return false;
            else {
                if (string.indexOf('.') == -1)
                    return false;
                else {
                    if (string.indexOf(' ') > 0)
                        return false;
                    else
                        return true;
                }
            }
        }
    }
    else
        return false;
}

function check_space(string) {
    var i;
    var invalid = false;

    for (i = 0; i < string.length; i++) {
        if (string.charAt(i) == ' ') {
            invalid = true;
            break;
        }
    }

    return invalid;
}

function check_alphanum_mix(str, ignoreEmpty) {
    if (ignoreEmpty && check_empty(str))
        return true;

    var valid;
    valid = true;

    var foundNum, foundAlpha;
    foundNum = false;
    foundAlpha = false;

    for (i = 0; i < str.length; i++) {
        ch = str.charAt(i);
        if (ch >= '0' && ch <= '9') {
            foundNum = true;
            continue;
        }
        if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
            foundAlpha = true;
            continue;
        }

        // reach here means invalid character
        valid = false;
        break;
    }

    if (!valid)
        return false;

    valid = (foundNum && foundAlpha);
    return valid;
}

function datediff(strDate1, strDate2) {
    // strDate2 - strDate
    // yyyy/mm/dd
    // return days diff

    datDate1 = Date.parse(strDate1);
    datDate2 = Date.parse(strDate2);

    return ((datDate2 - datDate1) / (24 * 60 * 60 * 1000));
}

function datetimediff(strDate1, strDate2) {
    // strDate2 - strDate
    // yyyy/mm/dd HH:MM
    // return seconds diff

    datDate1 = Date.parse(strDate1);
    datDate2 = Date.parse(strDate2);
    //alert(datDate1 + '\n' + datDate2 + '\n' + (datDate2-datDate1)/(60*1000));

    return ((datDate2 - datDate1) / (60 * 1000));
}
