﻿String.prototype.bool = function() { return (/^true$/i).test(this); };
var VisitedURL="";
var IsPopUp=false;
var UserTypeID=0;
var UserCategoryID=0;

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function SetUserDetails(userTypeID,userCategoryID)
{
    UserTypeID=userTypeID;
    UserCategoryID=userCategoryID;
}

function SetVisitedURL(URL)
{
    VisitedURL=URL;
}

function SetIsPopUp(value)
{
    IsPopUp=value.bool();
}

function SetIsPageOnRoot(value)
{
    IsPageOnRoot=value.bool();
}

function RedirectPage(url)
{
    var prefix="";
    if(IsPageOnRoot)
    {
        prefix="";
    }
    else
    {
        prefix="../"
    }
    window.location.href=prefix + url;
}

function ShowPermissionMessage(userTypeID, userCategoryID)
{
    if(userTypeID!=null)
        UserTypeID=userTypeID;
        
    if(userCategoryID!=null)
        UserCategoryID=userCategoryID;
        
    var prefix="";
    if(IsPageOnRoot)
    {
        prefix="";
    }
    else
    {
        prefix="../"
    }
    if(UserTypeID!=0 && UserCategoryID==2)
    {
        if(IsPopUp)
        {
            ShowAlert_Close('Sorry, you are not authorized to view this information.');
        }
        else
        {
            var returnUrl="";
            
            switch (UserTypeID)
            {
                case 4:
                    if(VisitedURL.toLowerCase().indexOf("corpdashboard")<0)
                        returnUrl = "CorpUser/CorpDashboard.aspx";
                    break;
                case 6:
                    if(VisitedURL.toLowerCase().indexOf("ipomgr_dashboard")<0)
                        returnUrl = "IPOManager/IPOMgr_Dashboard.aspx?CM=MyDashboard";
                    break;
                case 5:
                    if(VisitedURL.toLowerCase().indexOf("admindashboard")<0)
                        returnUrl = "Admin/AdminDashboard.aspx";
                    break;
                case 7:
                    if(VisitedURL.toLowerCase().indexOf("amcmanager_dashboard")<0)
                        returnUrl = "AMCManager/AMCManager_Dashboard.aspx";
                    break;
                case 8:
                    if(VisitedURL.toLowerCase().indexOf("brokerdashboard")<0)
                        returnUrl = "Broker/BrokerDashboard.aspx";
                    break;
                default:
                    if(VisitedURL.toLowerCase().indexOf("dashboard")<0)
                        returnUrl = "Secure/Dashboard.aspx";
                    break;
            }
           
            if(newRequest != null && newRequest)
            {
                ShowAlert('Sorry, you are not authorized to view this information.', prefix + returnUrl);
            }
            else
                alert('Sorry, you are not authorized to view this information.');
        }
    }
    else
    {
        switch(UserTypeID)
        {
            case 0 :
                    if(IsPopUp)
                    {
                        if(window.opener)
                            window.opener.location.href = window.opener.location.href;
                        RedirectPage('PopUpLogin.aspx?SPM=true&VisitedURL=' + VisitedURL);
                    }
                    else
                        RedirectPage('Login.aspx?SPM=true&VisitedURL=' + VisitedURL);
                    break;
            case 2 :
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
            case 4 : 
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
            case 5 : 
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
            case 6 : 
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
            case 7 : 
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
            case 8 : 
                    ShowAlert("This is a premium feature. Please subscribe to our premium services.",prefix + "error.aspx?popup=" + IsPopUp + "&VisitedURL=" + VisitedURL);
                    break;
        }
    }   
}
var CONFIRM_YES_BUTTON_TEXT = "Yes";
var CONFIRM_NO_BUTTON_TEXT = "No";
var CONFIRM_TITLE="Finalaya Confirm Box";

function ShowConfirmBox(txtMsg,fun)
{
    createCustomConfirm(txtMsg,fun);
}

function createCustomConfirm(txt,fun) {
    // shortcut reference to the document object
    d = document;

    // if the modalContainer object already exists in the DOM, bail out.
    if(d.getElementById("modalContainer")) return;

    // create the modalContainer div as a child of the BODY element
    mObj = d.getElementById("bodydiv").appendChild(d.createElement("div"));
    mObj.id = "modalContainer";
     // make sure its as tall as it needs to be to overlay all the content on the page
    mObj.style.height = document.documentElement.scrollHeight + "px";

    // create the DIV that will be the alert 
    alertObj = mObj.appendChild(d.createElement("div"));
    alertObj.id = "alertBox";
    // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
    //if(d.all && !window.opera) 
    var he=parseInt(d.documentElement.clientHeight/2) - 100 +  d.documentElement.scrollTop;
    alertObj.style.top = he  + "px";
    
    // center the alert box
    var wid=parseInt((d.documentElement.clientWidth/2) - 150)
    alertObj.style.left = wid + "px";
    
    // create an H1 element as the title bar
    h1 = alertObj.appendChild(d.createElement("h1"));
    h1.appendChild(d.createTextNode(CONFIRM_TITLE));

    // create a paragraph element to contain the txt argument
    msg = alertObj.appendChild(d.createElement("p"));
    msg.innerHTML = txt;
	
    // create an anchor element to use as the confirmation button.
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "yesBtn";
    btn.appendChild(d.createTextNode(CONFIRM_YES_BUTTON_TEXT));
    btn.href = "#";
    
    
    // set up the onclick event to remove the alert when the anchor is clicked
    btn.onclick = function() { ConfirmYesButton_Click(fun);}
    
    // create an anchor element to use as the confirmation button.
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "noBtn";
    btn.appendChild(d.createTextNode(CONFIRM_NO_BUTTON_TEXT));
    btn.href = "#";
    
    
    // set up the onclick event to remove the alert when the anchor is clicked
    btn.onclick = function() { ConfirmNoButton_Click();}
    
    
    document.documentElement.style.overflow="hidden";
    d.getElementById("bodydiv").style.display="block";
    return false;
	
}

// removes the custom alert from the DOM
function ConfirmYesButton_Click(fun) {
    document.getElementById("bodydiv").style.display="none";
    document.documentElement.style.overflow="";
    document.getElementById("bodydiv").removeChild(document.getElementById("modalContainer"));
    eval(fun);
}

// Confirm Cancel button click
function ConfirmNoButton_Click() {
    document.getElementById("bodydiv").style.display="none";
    document.documentElement.style.overflow="";
    document.getElementById("bodydiv").removeChild(document.getElementById("modalContainer"));
}

var ALERT_TITLE = "Finalaya Alert!";
var ALERT_BUTTON_TEXT = "Ok";

function createCustomAlert(str,redirectSelfURL,redirectParentURL,closeSelf) {
    var txt=str.toString();
    if(redirectParentURL!="" || redirectSelfURL!="" || closeSelf)
    {
        if(navigator.appName=='Microsoft Internet Explorer')
            document.execCommand('Stop');
        else
            window.stop();
    }
    // shortcut reference to the document object
    d = document;

    // if the modalContainer object already exists in the DOM, bail out.
    if(d.getElementById("modalContainer")) return;

    // create the modalContainer div as a child of the BODY element
    mObj = d.getElementById("bodydiv").appendChild(d.createElement("div"));
    mObj.id = "modalContainer";
     // make sure its as tall as it needs to be to overlay all the content on the page
    mObj.style.height = document.documentElement.scrollHeight + "px";

    // create the DIV that will be the alert 
    alertObj = mObj.appendChild(d.createElement("div"));
    alertObj.id = "alertBox";
    // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
    //if(d.all && !window.opera) 
    var he=parseInt(d.documentElement.clientHeight/2) - 100 +  d.documentElement.scrollTop;
    alertObj.style.top = he  + "px";

    // center the alert box
    var wid=parseInt((d.documentElement.clientWidth/2) - 150)
    alertObj.style.left = wid + "px";
    
    // create an H1 element as the title bar
    h1 = alertObj.appendChild(d.createElement("h1"));
    h1.appendChild(d.createTextNode(ALERT_TITLE));

    // create a paragraph element to contain the txt argument
    msg = alertObj.appendChild(d.createElement("p"));
    msg.innerHTML = txt;
	
    // create an anchor element to use as the confirmation button.
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "closeBtn";
    btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
    btn.href = "javascript:void(0);";
    // set up the onclick event to remove the alert when the anchor is clicked
    btn.onclick = function() { removeCustomAlert(redirectSelfURL,redirectParentURL,closeSelf);return false; }
    d.getElementById("bodydiv").style.display="block";
    if(navigator.appName=='Microsoft Internet Explorer')
    {
        var ver = getInternetExplorerVersion();
        if(ver<7.0)
        {
            ShowHideAllDropDowns(false);
        }
    }
    DisableScroll();
    return false;
	
}

var AllInvisibleDropdown = new Array();

function ShowHideAllDropDowns(show)
{
    var allsel = document.getElementsByTagName('Select');
    for(i=0;i<allsel.length;i++)
    {
        if(show)
        {
            if(allsel[i].style.display=='none' && !contains(AllInvisibleDropdown,allsel[i].id))
            {
                allsel[i].style.display='inline';
            }            
        }
        else
        {
            if(allsel[i].style.display=='none')
            {
                AllInvisibleDropdown[AllInvisibleDropdown.length] = allsel[i].id;
            }
            else
            {
                allsel[i].style.display='none';
            }
        }
    }
}

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}

// removes the custom alert from the DOM
function removeCustomAlert(redirectSelfURL,redirectParentURL,closeSelf) {
    document.getElementById("bodydiv").style.display="none";
    EnableScroll();
    document.getElementById("bodydiv").removeChild(document.getElementById("modalContainer"));
    if(navigator.appName=='Microsoft Internet Explorer')
    {
        var ver = getInternetExplorerVersion();
        if(ver<7.0)
        {
            ShowHideAllDropDowns(true);
        }
    }
    if(redirectParentURL!='')
    {
        if(redirectParentURL=='reload')
            if(window.opener!=null) window.opener.location.href=window.opener.location.href;  else  window.location.href=window.location.href;
        else
            if(window.opener!=null) window.opener.location.href=redirectParentURL;  else  window.location.href=redirectParentURL;
    }
    
    if(closeSelf)
    {
        window.close();
    }
    if(redirectSelfURL!='')
    {
        window.location.href=redirectSelfURL;
    }
}
/* For Model AlertBox */
// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
//if(document.getElementById) {

if(window.location.href.toLowerCase().indexOf("documentview")<0)
{
    window.alert = function(txt) {createCustomAlert(txt,'','',false);}
}

function DisableScroll()   
{    
    if(document.documentElement && document.documentElement.scrollTop)   
    {   
        var oTop = document.documentElement.scrollTop;   
        document.documentElement.scroll = "no";   
        document.documentElement.style.overflow = "hidden";   
        document.documentElement.scrollTop = oTop;   
           
    } else if(document.body)   
    {   
        var oTop = document.body.scrollTop;   
        document.body.scroll = "no";   
        document.body.style.overflow = "hidden";   
        document.body.scrollTop = oTop;   
    }   
}   

function EnableScroll()   
{   
    if(document.documentElement && document.documentElement.scrollTop)   
    {   
        var oTop = document.documentElement.scrollTop; 
        document.documentElement.scroll = "auto";   
        document.documentElement.style.overflow = "auto";
        document.documentElement.scrollTop = oTop;
        
    } 
    else if(document.body)   
    {   
        var oTop = document.body.scrollTop; 
        document.body.scroll = "auto";   
        document.body.style.overflow = "auto"; 
        document.body.scrollTop = oTop;  
    }   
}

// Redirect the parent window and Close popup.
function ShowAlertCloseRedirect(txtMsg,redirectParentURL)
{
    createCustomAlert(txtMsg,'',redirectParentURL,true);
}

// Show alert and Close popup.
function ShowAlert_Close(txtMsg)
{
    createCustomAlert(txtMsg,'','',true);
}

// Show alert and redirect to another page.
function ShowAlert(txtMsg,redirectSelfURL)
{
    createCustomAlert(txtMsg,redirectSelfURL,'',false);
}

// Redirect the parent window.
function ShowAlertRedirectParent(txtMsg,redirectParentURL)
{
    createCustomAlert(txtMsg,'',redirectParentURL,false);
}

function CheckFeatureAccessible(feature)
{
    for (var i = 1; i < Features.length; i++)
    {
        if(Features[i]==feature)
        {
            return true;
            break;
        }
    }
    return false;
}
