var http;

if (navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
    http = new XMLHttpRequest();
}

// remove multiple, leading or trailing spaces
function trim(s) {
	s = s.replace(/(^\s*)|(\s*$)/gi,"");
	s = s.replace(/[ ]{2,}/gi," ");
	s = s.replace(/\n /,"\n");
	return s;
}

function clearStatus() {
    document.getElementById('result').innerHTML = "";
}

function isAdmin (user) {
    return ((user == "admin@sensor.network.com") || (user == "javier.rubio@vodafone.com"));
}

function isInteger(str) {
   var validChars = "0123456789";

   for (i = 0; i < str.length; i++) {
      if (validChars.indexOf(str.charAt(i)) == -1) return false;
   }

   return true;
}

function isNumeric(str) {
   var validChars = "0123456789.";

   for (i = 0; i < str.length; i++) {
      if (validChars.indexOf(str.charAt(i)) == -1) return false;
   }

   return true;
}

function isInt(str) {
   var validChars = "-0123456789";

   for (i = 0; i < str.length; i++) {
      if (validChars.indexOf(str.charAt(i)) == -1) return false;
   }

   return true;
}

function isFloat(str) {
   var validChars = "-0123456789.";

   for (i = 0; i < str.length; i++) {
      if (validChars.indexOf(str.charAt(i)) == -1) return false;
   }

   return true;
}

function isBoolean(str) {
    return ((str.toLowerCase() == "true") || (str.toLowerCase() == "false"));
}

// Whitelist validation
// Return true if str contains characters other than
// 0-9, A-Z, a-z, _, space, quote, hyphen
function checkDatastreamName(str) {
   var validRegex = /[^.\w\s'-]+/;
   return validRegex.test(str);
}

// Whitelist validation
// Return true if str contains characters other than
// 0-9, A-Z, a-z, _, comma, space, quote, hyphen
function checkDesc(str) {
   var validRegex = /[^.\w,\s'-]+/;
   return validRegex.test(str);
}

// Whitelist validation
// Return true if str contains characters other than
// 0-9, A-Z, a-z, _, space, hyphen
// Return false otherwise
function checkValueName(str) {
   var validRegex = /[^\w\s-]+/;
   return validRegex.test(str);
}

// Whitelist validation
// Return true if str contains characters other than
// 0-9, A-Z, a-z, _, period, space, hyphen. Comma separates individual tags
// Cannot use checkDatastreamName() due to the commas separating the values.
// Return false otherwise
function checkTags(str) {

   if (/^,/.test(str) || /,$/.test(str)) {
       return true;
   }

   var validRegex = /[^.\w,\s'-]+/;
   return validRegex.test(str);
}

function dateToTimestamp(d) {
    var ts = "";

    ts = d.getFullYear() + "-" +
        ((d.getMonth() < 9) ? "0" : "") + (d.getMonth() + 1) + "-" +
        ((d.getDate() < 10) ? "0" : "") + d.getDate() + " " +
        ((d.getHours() < 10) ? "0" : "") + d.getHours() + ":" +
        ((d.getMinutes() < 10) ? "0" : "") + d.getMinutes() + ":" +
        ((d.getSeconds() < 10) ? "0" : "") + d.getSeconds() + "." +
        d.getMilliseconds();

    return ts;
}

function getTimestamp(millis) {
    var d = new Date();

    d.setTime(millis);
    return dateToTimestamp(d);
}

function stringStartsWith(string1, starter){
    return string1.substr(0, starter.length) == starter;
}

function getCurrentTimestamp() {
    var d = new Date();

    return dateToTimestamp(d);
}

function dateToXMLDateTime(d) {
    var ts = "";

    ts = d.getUTCFullYear() + "-" +
        ((d.getUTCMonth() < 9) ? "0" : "") + (d.getUTCMonth() + 1) + "-" +
        ((d.getUTCDate() < 10) ? "0" : "") + d.getUTCDate() + "T" +
        ((d.getUTCHours() < 10) ? "0" : "") + d.getUTCHours() + ":" +
        ((d.getUTCMinutes() < 10) ? "0" : "") + d.getUTCMinutes() + ":" +
        ((d.getUTCSeconds() < 10) ? "0" : "") + d.getUTCSeconds() + "." +
        d.getUTCMilliseconds() + "Z";

    return ts;
}

/**
 * Gets the part of the URL for this docuemnt that is "up" n parents (directories).
 * So getSubURL(0) == document.URL
 *
 * NOTE: In the returned string, a trailing "/" is always omitted.
 */
function getDocumentSubURL(n){
    return getSubURL(document.URL, n);
}

function getSubURL(fullURL, n){
    var tokens = fullURL.split("/");
    url = "";
    for(i = 0; i < tokens.length - n; i++){
        url += tokens[i];
        if(i < tokens.length - n - 1) url += "/";
    }
    return url;
}

function dateToCompactTimestamp(d) {
    var ts = "";

    ts = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate() + " " +
    d.getHours() + ":" + ((d.getMinutes() < 10) ? "0" : "") +
    d.getMinutes() + ":" + ((d.getSeconds() < 10) ? "0" : "") +
    d.getSeconds();

    return ts;
}

function getCompactTimestamp(millis) {
    var d = new Date();

    d.setTime(millis);
    return dateToCompactTimestamp(d);
}

function getCompactCurrentTimestamp() {
    var d = new Date();

    return dateToCompactTimestamp(d);
}

function millisToString(milliseconds) {
    var tmp = milliseconds/1000;
    var ret = "";

    if (tmp > 0x1e13380) {
        ret = ret + (tmp / 0x1e13380).toFixed(0) + " yr ";
        tmp %= 0x1e13380;
    }
    if (tmp > 0x15180) {
        ret = ret + (tmp / 0x15180).toFixed(0) + " day ";
        tmp %= 0x15180;
    }
    if (tmp > 3600) {
        ret = ret + (tmp / 3600).toFixed(0) + " hr ";
        tmp %= 3600;
    }
    if (tmp > 60) {
        ret = ret + (tmp / 60).toFixed(0) + " min ";
        tmp %= 60;
    }

    if (ret == "")
        ret = ret + tmp.toFixed(1) + " sec";

    return ret;
}

function getDurationEstimate(millis) {
    var tmp = millis/1000;
    var ret = "";

    if (tmp > 0x1e13380) {
        ret = (tmp / 0x1e13380).toFixed(0) + " yrs ";
    } else if (tmp > 0x15180) {
        ret = (tmp / 0x15180).toFixed(0) + " days ";
    } else if (tmp > 3600) {
        ret = (tmp / 3600).toFixed(0) + " hrs ";
    } else if (tmp > 60) {
        ret = (tmp / 60).toFixed(0) + " mins ";
    } else {
        ret = tmp.toFixed(0) + "sec ";
    }

    return (ret + "ago");
}
