﻿// JScript file contains some commonly used functions
//****************************************************

// Regular expression for valid email id
var ValidEmail = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z][a-zA-Z]+)$/;

// Regular expression for emptyString
var EmptyString = /^\s*$/;

// Regular expression for Not a number;
var IsNaN = /\D/;

// Trim based on Regular Expression
function Trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

// Method to pop a div so that it can cover select boxes
// Method uses Iframe shim technique to hide select boxes.
// divId -> Id of the div to be poped out.
// iframeId -> Id of the shim Iframe.
function IShimPopTL(divId, iframeId)
{
    var DivRef = document.getElementById(divId);
    var IfrRef = document.getElementById(iframeId);
        
    if(DivRef.style.display == "none")
    {
        DivRef.style.display = "block";
        IfrRef.style.width = DivRef.offsetWidth + "px";
        IfrRef.style.height = DivRef.offsetHeight + "px";
        IfrRef.style.top = DivRef.offsetTop;
        IfrRef.style.left = DivRef.offsetLeft;
        IfrRef.style.zIndex = DivRef.style.zIndex - 1;
        IfrRef.style.display = "block";
    }
    else
    {
        DivRef.style.display = "none";
        IfrRef.style.display = "none";
    }
}

// method to ger a browser object aware of browser in use
// this.isIE is true when the browser is Internet Explorer
// this.isNS is true if the browser is Netscape or mozilla or other Gecko browser
// var browser = new Browser(); // syntax to use.
function Browser()
{
    var userAgent, browserKey, i;

    this.isIE    = false;
    this.isNS    = false;
    this.version = null;

    userAgent = navigator.userAgent;

    browserKey = "MSIE";
    if ((i = userAgent.indexOf(browserKey)) >= 0)
    {
        this.isIE = true;
        this.version = parseFloat(userAgent.substr(i + browserKey.length));
        return;
    }

    browserKey = "Netscape6/";
    if ((i = userAgent.indexOf(browserKey)) >= 0)
    {
        this.isNS = true;
        this.version = parseFloat(userAgent.substr(i + browserKey.length));
        return;
    }

    // Treat any other "Gecko" browser as NS 6.1.
    browserKey = "Gecko";
    if ((i = userAgent.indexOf(browserKey)) >= 0)
    {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}

function GetWindowSize()
{
    var width =
        window.innerWidth && window.innerWidth ||
        document.documentElement && document.documentElement.clientWidth ||
        document.body && document.body.clientWidth ||
        document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
        0;

    var height =
        window.innerHeight && window.innerHeight ||
        document.documentElement && document.documentElement.clientHeight ||
        document.body && document.body.clientHeight ||
        document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
        0;
  		
    return {x: width, y: height};
}

function UrlEncode(inputString)
{
    var encodedString = escape(inputString);
    encodedString = encodedString.replace("+", "%2B");
    encodedString = encodedString.replace("/", "%2F");
    return encodedString;
}

function AjaxLogin(methodName, param) {
    var page;
    var qstring="";
    if (methodName == LinkToPage) {
        page = param;
    }
    else if (methodName == 'twitThis') {
        page = param;
        param = methodName;
        qstring = "&twitMsg=" + twitMessage;
        twitMessage = "";
    }
    else {
        page = "Main.aspx";
    }
    window.location.href = "oauth.aspx?oauth_callback=" + param + "&page=" + page + qstring;
}

function GetSMTimeString(time)
{
    var timeString = '';
    var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
    var tmin = Math.round(((new Date()).getTime() - time) / 60000);
    tdate = new Date();
    tdate.setTime(time);
    var thour = Math.round(tmin / 60);
    var tdays = Math.round(thour / 24);
    if (tmin < 1) {
        timeString = "less than a minute ago";
    }
    else if (tmin < 2) {
        timeString = "about a minute ago";
    }
    else if (tmin < 60) {
        timeString = tmin + " minutes ago";
    }
    else if (thour < 2) {
        timeString = "about an hour ago";
    }
    else if (thour < 24) {
        timeString = thour + " hours ago";
    }
    else if (tdays < 2) {
        timeString = "yesterday at " + (tdate.getHours() % 12) +
            ":" + tdate.getMinutes() + " " + (tdate.getHours() / 12 > 0 ? "PM" : "AM");
    }
    else {
        timeString = tdate.getDate() + " " + months[tdate.getMonth()] + " at " + (tdate.getHours() % 12) +
            ":" + tdate.getMinutes() + " " + (tdate.getHours() / 12 > 0 ? "PM" : "AM");
        //timeString = time.ToString(@"d MMM a\t hh:mm tt");
    }
    return timeString;
}

function ParseUrl(str) {
    var urlRegex = /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g;
    var matches = str.match(urlRegex);
    var urls = new Array();
    for (var i = 0; i < matches.length; i++) {
        urls[i] = matches[i];
    }
    return urls;
}

// Buzzom JS
function ToggleSwitchUserMenu() {
    if (document.getElementById('SwitchUserMenu').style.display == 'none') {
        document.getElementById('SwitchUserMenuArrow').innerHTML = '&#9650;';
    }
    else {
        document.getElementById('SwitchUserMenuArrow').innerHTML = '&#9660;';
    }
    jQuery('#SwitchUserMenu').slideToggle('fast');
}
