﻿// 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;
}

var twitMessage = "";
function twitThis(page) {
    var msg = $('twitMeText');
    if (msg.value.length > 140) {
        $('twitThisMsg').innerHTML = JsmUtil["charExceed"];
        return;
    }
    if (Trim(msg.value).length == 0) {
        $('twitThisMsg').innerHTML = JsmUtil["write"];
        return;
    }

    if (isLn) {
        var pars = "action=twitThis&twitMsg=" + msg.value;
        $('twitThisMsg').innerHTML = JsmUtil["posting"] + "...";
        new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: TwitThisComplete });
    }
    else {
        $('twitThisMsg').innerHTML = JsmUtil["posting"] + "...";
        twitMessage = msg.value;
        AjaxLogin('twitThis', page);
        //$('twitThisMsg').style.color = "#ff0000";
        //setTimeout("AjaxLogin('twitThis', '" + page + "')", 1000);
    }
}

//called when returned from oAuth
function twitThisDirect(msg) {
    if (isLn) {
        $('twitMeText').value = msg;
        var pars = "action=twitThis&twitMsg=" + msg;
        $('twitThisMsg').innerHTML = JsmUtil["posting"] + "...";
        new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: TwitThisComplete });
    }
}

function TwitThisComplete(resp) {
    $('twitThisMsg').innerHTML = resp.responseText;
    if (resp.responseText == JsmUtil["posted"])
        $('twitMeText').value = "";
}

function twitThisCount(obj) {
    var cnt=obj.value.length;
    if (cnt > 140) {
        $('twitThisMsg').innerHTML = JsmUtil["charExceed"];
        $('twitThisMsg').style.color = "#ff0000";
        obj.style.backgroundColor = "#EAC1C1";
        
    }
    else {
        var rem=140-cnt;
        $('twitThisMsg').innerHTML = JsmUtil["remaining"] + ": " + rem;
        $('twitThisMsg').style.color = "#316AC5";
        obj.style.backgroundColor="#ffffff";
    }
}

function SelectLanguage() {
    if (document.getElementById('LangMenuList').style.display == "none") {
        document.getElementById('LangMenuLoading').style.display = 'none';
        document.getElementById('LangMenuList').style.display = 'block';
    }
    else {
        document.getElementById('LangMenuList').style.display = 'none';
    }
}

function ChangeLanguage(languageCode) {
    var pars = "action=changeLanguage&languageCode=" + languageCode;
    new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: ChangeLanguageComplete });
    document.getElementById('LangMenuList').style.display = 'none';
    document.getElementById('LangMenuLoading').style.display = 'block';
    document.getElementById('LangMenuLoading').innerHTML = '<img src="images/progress.gif" style="height: 20px; width: 20px;" alt="" /> ' + JsmUtil["loading"] + ' ...';
}

function ChangeLanguageComplete(resp) {
    if (resp.responseText == "true") {
        window.location.reload();
    }
    else {
        document.getElementById('LangMenuLoading').style.display = 'block';
        document.getElementById('LangMenuLoading').innerHTML = 'Failed to load';
        setTimeout('document.getElementById("LangMenuLoading").style.display="none";', 5000);
    }
}
