// Library Functions

// All the functions that should have been included in Javascript .....

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 (typeof a == 'object' && !!a) || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
}

function isInteger(val){

	if (!isNumber(val)){return false}
	var x=val.toString();
	if (x=='') { return false}
	for(var i=0;i<x.length;i++){
		if(x.charAt(i)=='.'){
			return false;
			}
		}
	return true;
}

function trim(s){
    
    if (!isString(s)){
    	return "";
    }
    
   
    var l=0; var r=s.length -1;
    while(l < s.length && s[l] == ' ')
    {     l++; }
    while(r > l && s[r] == ' ')
    {     r-=1;     }
    return s.substring(l, r+1);
} 


// End Library Functions

function GetCaretPosition(o){

	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.value.length)
		return r.text.length
	} else {
		return  o.selectionEnd
	}

}

function setCaretPosition(ctrl, pos)
{
   if (ctrl.setSelectionRange)
   {
      ctrl.focus();
      ctrl.setSelectionRange(pos,pos);
   }
   else if (ctrl.createTextRange)
   {
      var range = ctrl.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
   }
}


// VBscript equivalent functions
function left(str, n){
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                // entire string
	else // Valid bound, return appropriate substring
		return String(str).substring(0,n);
}

function chr(lngASCII){

	return String.fromCharCode(lngASCII);

}


function getElementAbsolutePosition(obj) {

	/*
		Returns an array containing x and y coordinates of an HTML element

	*/

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// PHP equivalent functions
function strpos( haystack, needle, offset){ 
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function number_format_from_bytes(lngBytes){

	//Ensure this runs in descending order !

	if (!lngBytes){
		return "0 b";
	}
	

	if (lngBytes>=  1099511627776){
		return (lngBytes/1099511627776).toFixed() + " TB";
	}


	if (lngBytes>=  1073741824){
		return (lngBytes/1073741824).toFixed() + " GB";
	}

	if (lngBytes>= 1048576){
		return (lngBytes/1048576).toFixed() + " MB";
	}


	if (lngBytes >= 1024){
		return (lngBytes/1024).toFixed() + " KB";
	}

	return lngBytes +  " bytes";



}



