/**
  *  @projectDescription
  *  <p>A collection of global utilities</p>
  *
  *  @author			Rick Farmer
  *  @version			2.0
  *
  */

window.scrollTo(0,0); 
parent.scrollTo(0,0);

var debug = new oDebug(true,"div","debug"); 
function oDebug(on,to,div){

    this.on  = (on)?on:false;  // 1 = show debug() code | 0 = hide debug() code
    this.to  = (to)?to:"inline"; //alert | status | inline | div (div id="debug"...)
    this.div = (div)?div:"debug"; // The id of the div to output to if the debug to is "div"
    
    this.out = function(s,noBreak,to){
        to = (to)?to:this.to;
        if(s){
            s = (noBreak)?s + "\n":s + "<br>\n";

            if(this.on){
                    if(to=="status"){
                        window.status=s;
                    }else if(to=="alert"){
                        alert(s);
                    }else if(to=="div"){
                        if(document.getElementById(this.div)){
                            document.getElementById(this.div).innerHTML += ("<pre>" + s + "</pre>");
                        }
                    }else{
                        document.write(s);
                    }
            return true;
            }
        return false;
        }
    }
} 
 
function isMaxLength(obj, maxLength){

	if (obj.getAttribute && obj.value.length>maxLength){
		//alert("is over max length");
		obj.value=obj.value.substring(0,maxLength);
	}

}

function printDocument(){
	if (!window.print) {
		vbPrintPage();
	}
	else {
		window.print();
	}
}

//Note: printDocument() - Requires the following inline vbscript
/*
<script language="VBScript" type="text/vbscript">
	Sub vbPrintPage()
		OLECMDID_PRINT = 6
		OLECMDEXECOPT_DONTPROMPTUSER = 2 
		OLECMDEXECOPT_PROMPTUSER = 1
		On Error Resume Next
		WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER
	End Sub
</script>

*/

function getParam(paramName,pos) {
	var startPos = (pos) ? pos : 0;
	var url,paramBegin,paramEnd,valueBegin,paramValue;
	url = window.location.search;
	paramBegin = url.indexOf(paramName,startPos);

	if (paramBegin == -1) { return false; }
	paramEnd = paramBegin + paramName.length;
	param = url.substring(paramBegin,paramEnd);
	
	var valBegin = url.indexOf("=",paramEnd);
	var nextParam = url.indexOf("&",paramEnd);
	var valLen = (nextParam == -1) ? (url.length-(valBegin)) : (nextParam-valBegin);
	var valEnd = (nextParam == -1) ? valBegin+valLen : nextParam;

    valueBegin = paramEnd + 1;
    paramValue = url.substring(valueBegin, valEnd);

	if ((nextParam != -1) && (valBegin > nextParam)) { return getParam(param,paramEnd); }
	else {
		 for (i = 0; i < paramValue.length; i++) {
			 paramValue = paramValue.replace("+", " ");
			 paramValue = paramValue.replace("%20", " ");
			 paramValue = paramValue.replace("%26", "&");
			 paramValue = paramValue.replace("%27", "'");
			 paramValue = paramValue.replace("%28", "(");
			 paramValue = paramValue.replace("%29", ")");
		}
    	return paramValue;
	}
}





/**
  *     JavaScript prototypical functions
  */



Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

Function.method('inherits', function (parent) {
    var d = 0, p = (this.prototype = new parent());
    this.method('uber', function uber(name) {
        var f, r, t = d, v = parent.prototype;
        if (t) {
            while (t) {
                v = v.constructor.prototype;
                t -= 1;
            }
            f = v[name];
        } else {
            f = p[name];
            if (f == this[name]) {
                f = v[name];
            }
        }
        d += 1;
        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
        d -= 1;
        return r;
    });
    return this;
});

function hasValue(a) {

    if(isUndefined(a)){
        return false;
    }else if(isNull(a)){
        return false;
    }else if(a.length==0){
        return false;
    }else if(isWhitespace(a)){
        return false;
    }else if(a=="<!---->"){
        return false;
    }else if(a=="undefined"){
        return false;
    }else if(a==""){
        return false;
    }else{
        return true;
    }
}

function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function isWhitespace(a){
    //var a = (isObject(a))?eval(a):a;
    var whitespace = " \x0B\x09\x0D\x0A\x0C";

    if((a==null)||(a.length==0))
        return true;

	for(var i=0;i<a.length;i++)
	{
	 var c=a.charAt(i);
	 if(whitespace.indexOf(c)==-1)
	 		return false;
	}
	return true;
}

String.method('entityify', function () {
    return this.replace(/&/g, "&amp;").replace(/</g,
        "&lt;").replace(/>/g, "&gt;");
});

String.method('supplant', function (o) {
    var i, j, s = this, v;
    for (;;) {
        i = s.lastIndexOf('{');
        if (i < 0) {
            break;
        }
        j = s.indexOf('}', i);
        if (i + 1 >= j) {
            break;
        }
        v = o[s.substring(i + 1, j)];
        if (!isString(v) && !isNumber(v)) {
            break;
        }
        s = s.substring(0, i) + v + s.substring(j + 1);
    }
    return s;
});

String.method('trim', function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
});

if (!isFunction(Function.apply)) {
    Function.method('apply', function (o, a) {
        var r, x = '____apply';
        if (!isObject(o)) {
            o = {};
        }
        o[x] = this;
        switch ((a && a.length) || 0) {
        case 0:
            r = o[x]();
            break;
        case 1:
            r = o[x](a[0]);
            break;
        case 2:
            r = o[x](a[0], a[1]);
            break;
        case 3:
            r = o[x](a[0], a[1], a[2]);
            break;
        case 4:
            r = o[x](a[0], a[1], a[2], a[3]);
            break;
        case 5:
            r = o[x](a[0], a[1], a[2], a[3], a[4]);
            break;
        case 6:
            r = o[x](a[0], a[1], a[2], a[3], a[4], a[5]);
            break;
        default:
            alert('Too many arguments to apply.');
        }
        delete o[x];
        return r;
    });
}

if (!isFunction(Array.prototype.pop)) {
    Array.method('pop', function () {
        return this.splice(this.length - 1, 1)[0];
    });
}

if (!isFunction(Array.prototype.push)) {
    Array.method('push', function () {
        this.splice.apply(this,
            [this.length, 0].concat(Array.prototype.slice.apply(arguments)));
        return this.length;
    });
}

if (!isFunction(Array.prototype.shift)) {
    Array.method('shift', function () {
        return this.splice(0, 1)[0];
    });
}

if (!isFunction(Array.prototype.splice)) {
    Array.method('splice', function (s, d) {
        var max = Math.max,
            min = Math.min,
            a = [], // The return value array
            e,  // element
            i = max(arguments.length - 2, 0),   // insert count
            k = 0,
            l = this.length,
            n,  // new length
            v,  // delta
            x;  // shift count

        s = s || 0;
        if (s < 0) {
            s += l;
        }
        s = max(min(s, l), 0);  // start point
        d = max(min(isNumber(d) ? d : l, l - s), 0);    // delete count
        v = i - d;
        n = l + v;
        while (k < d) {
            e = this[s + k];
            if (!isUndefined(e)) {
                a[k] = e;
            }
            k += 1;
        }
        x = l - s - d;
        if (v < 0) {
            k = s + i;
            while (x) {
                this[k] = this[k - v];
                k += 1;
                x -= 1;
            }
            this.length = n;
        } else if (v > 0) {
            k = 1;
            while (x) {
                this[n - k] = this[l - k];
                k += 1;
                x -= 1;
            }
        }
        for (k = 0; k < i; ++k) {
            this[s + k] = arguments[k + 2];
        }
        return a;
    });
}

if (!isFunction(Array.prototype.unshift)) {
    Array.method('unshift', function () {
        this.splice.apply(this,
            [0, 0].concat(Array.prototype.slice.apply(arguments)));
        return this.length;
    });
}

function wait(sec){
d = new Date() //today's date
while (1)
{
    mill=new Date() // Date Now
    diff = mill-d //difference in milliseconds
    if( diff > sec ) {break;}
}
}


function dateDiff(start,end,interval,rounding){

    var iOut = 0;

    // Create 2 error messages, 1 for each argument.
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;

    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;

    // check that the start parameter is a valid Date.
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        return startMsg;
    }

    // check that an interval parameter was not numeric.
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error.
        return intervalMsg;
    }

    var number = bufferB-bufferA ;

    // what kind of add to do?
    switch (interval.charAt(0))
    {
        case 'd': case 'D':
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error.
        return intervalMsg;
    }

    return iOut ;
}
