﻿// Fleet Observer utility library
// (c) 2010 Fleet Observer Ltd

/*** Divs ***/
function ShowDiv(divname) { document.getElementById(divname).style.display = 'block'; }

function HideDiv(divname) { document.getElementById(divname).style.display = 'none'; }

function show(el) { document.getElementById(el).style.visibility = 'visible'; }

function hide(el) { document.getElementById(el).style.visibility = 'hidden'; }

function disable(el) { document.getElementById(el).disabled = "disabled"; }

function enable(el) { document.getElementById(el).disabled = ""; }

/*** Numbers ***/
function isEven(num) { return !(num % 2); }

function isOdd(num) { return !isEven(num); }

function pad2(number) {return (number < 10 ? '0' : '') + number; }

/*** URLs ***/
function GetFileNameFromURL(URL) {
    URL = URL || location.pathname;
    var a = URL.split("/");
    return a[a.length - 1].toLowerCase();
}

/*** Object manipulation ***/
function cloneObject(what) {
    for (i in what) {
        this[i] = what[i];
    }
}

/*** Window size ***/
function FindWidth() {
    var intWidth = 0;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        intWidth = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        //IE 6+ in 'standards compliant mode'
        intWidth = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        //IE 4 compatible
        intWidth = document.body.clientWidth;
    }

    return intWidth;
}

function FindHeight() {
    var intHeight = 0;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        intHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        intHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
        intHeight = document.body.clientHeight;
    }

    return intHeight;
}

/*** String extensions ***/
// String trim functions
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

String.prototype.IsSpace = function() {
    // checks to see is string is all spaces
    var hasSpace = true;
    var k = 0;
    for (k = 0; k <= this.length - 1; k++) {
        if (this.charAt(k) != " ") {
            hasSpace = false;
            break;
        }
    }
    return hasSpace;
}

String.prototype.IsDate = function() {
    //sees if string is a date
    var isADate = true;
    var aDate = Date.parse(this);
    if (isNaN(parseInt(aDate))) {
        isADate = false;
    }
    return isADate;
}


String.prototype.IsTime = function() {
    //sees if string is a time need to add a default date
    var temp = "01/01/2001 " + this
    var isADate = true;
    var aDate = Date.parse(temp);
    if (isNaN(parseInt(aDate))) {
        isADate = false;
    }
    return isADate;
}

String.prototype.IsNumeric = function() {
    //sees if string is a number
    var isANumber = true;
    //check if a space

    if (this.trim() == "") {
        isANumber = false;
    }

    if (isNaN(this)) {
        isANumber = false;

    }
    return isANumber;
}

// Break out of a frameset
function breakout() {
    if (top.location != location) {
        top.location.href = document.location.href;
    }
}

// Web method error check - returns true if ok, false if error
function checkJsonResult(msg) {
    if (msg.d.Error) {
        alert(msg.d.Error);
        return false;
    }
    return true;
}

function checkJsonResultString(msg) {
    if (msg.d && msg.d != "") {
        alert(msg.d);
        return false;
    }
    return true;
}

function parseASPNETDate(d) {
    return eval(d.replace(/\/(Date\([0-9-]+\))\//gi, 'new $1;'));
}
