﻿/*

This file contains useful miscellaneous functions

*/

// Converts a true/false boolean into an integer boolean
function getBooleanInteger(textBool){
    if (textBool){
        return 1;
    }
    return 0;
}

// Determine if the object we're working with exists and is not null
function isValid(object){
    return ((typeof object != 'undefined') && (object != null));
}

// Converts paired coordinates into decimal degrees.
// http://support.microsoft.com/kb/213449
function convertToDecimalDegrees(direction, degree, minute, second){
    var decimalDegree = null;
    
    try{
        decimalDegree = parseFloat(degree) + parseFloat(minute)/60 + parseFloat(second)/3600;
    
    
        direction = direction.toLowerCase();
        if (direction == "s" || direction == "w"){
            decimalDegree *= -1;
        }
        else if (direction != "n" && direction != "e"){
            decimalDegree = null;
        }
    }
    catch(err){}
    
    return decimalDegree;
}

// Injects a style sheet into the current page.
// http://cse-mjmcl.cse.bris.ac.uk/blog/2005/08/18/1124396539593.html
function addStyleSheet(sheetURL){
    
    if(document.createStyleSheet) {
        document.createStyleSheet(sheetURL);
    }

    else {
        var styles = "@import url(" + sheetURL + ");";
        var newSS=document.createElement('link');
        newSS.rel='stylesheet';
        newSS.href='data:text/css,'+escape(styles);
        document.getElementsByTagName("head")[0].appendChild(newSS);
    }
}

// Injects a javascript file into the current page.
// http://www.codingforums.com/showthread.php?t=11083
function addJavaScript(scriptURL){
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = scriptURL;
    document.getElementsByTagName('head')[0].appendChild(script);
}

// Checks if the current browser is an IE flavour
function isBrowserIE(){
    if (navigator.appName.indexOf("Microsoft")!=-1){
        return true;
    }
    return false;
}

// Checks if the current browser is specifically IE6
function isBrowserIE6(){
    if (navigator.userAgent.toLowerCase().indexOf("msie 6.")!=-1){
        return true;
    }
    return false;
}

// Nicely reformats a delimited string.
function reformatString(str,delimiter){

    if (delimiter == null){
        delimiter = ",";
    }

    var strings = str.split(delimiter);
    
    var newString = "";
    
    for (var i = 0; i < strings.length; i++){
        if (strings[i].length > 1){
            newString += strings[i] + ", ";
        }
    }
    
    newString = newString.substr(0, newString.length - 2);
    
    return newString;
}

// Sleep function in javascript.
function sleep(milliseconds, callback){
    var now = new Date();
    var then = new Date(now.getTime() + milliseconds);
    
    for (var i = 0; now >= then; i++){
        // Take a nap.
    }
    
    if (isValid(callback)){
        callback();
    }
    return;
}

// Trims a float to a given number of decimal places, defaults to 2.
function trimFloat(num, precision){

    var default_precision = 2;
    
    if (!isValid(num)){
        return null;
    }
    if (precision == null){
        precision = default_precision;
    }
    
    num = String(num);
    var decimalPos = num.indexOf(".") + 1;
    
    var newNum = num;
    if (decimalPos >= 0){   
        newNum = num.substr(0, Math.min(num.length, decimalPos + precision));
    }
    
    return parseFloat(newNum);
}
