﻿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 ReloadSelf()
{
    var str=window.location.href;
    window.location.href=str;
}

function GetBaseURL()
{
    var prefix="";
    if(IsPageOnRoot)
    {
        prefix="";
    }
    else
    {
        prefix="../"
    }
    return prefix;
}

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;
        }
    }   
}

function Session_Out()
{
    ShowPermissionMessage(0,0);
}

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
var CONFIRM_YES_BUTTON_TEXT = "Yes";
var CONFIRM_NO_BUTTON_TEXT = "No";
var CONFIRM_TITLE="Finalaya Confirm Box";

function ValidateFloat(value)
{
    var floatRegExp=/^\d+(.\d{1,2})?$/;
    var reNumber =  /^([0-9]*|\d*\.\d{1}?\d*)$/;
    
    if(!(reNumber.test(value.trim()) && floatRegExp.test(value.trim())))
    {
        return false;
    }
    else
    {
        return true;
    }
}
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,10) - 100 +  d.documentElement.scrollTop;
    alertObj.style.top = he  + "px";
    
    // center the alert box
    var wid=parseInt((d.documentElement.clientWidth/2) - 150,10)
    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,10) - 100 +  GetdocumentScrollTop();
    alertObj.style.top = he  + "px";

    // center the alert box
    var wid=parseInt((d.documentElement.clientWidth/2) - 150,10)
    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;
	
}

function GetdocumentScrollTop()
{
    var oScrollTop = 0;
    if(document.documentElement && document.documentElement.scrollTop)   
    {
        oScrollTop = parseInt(document.documentElement.scrollTop,10);  
    }
    else
    {
        if(document.body)   
        {   
            oScrollTop = parseInt(document.body.scrollTop,10);
        }
    }
    return oScrollTop;
}

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();
    if(document.getElementById("modalContainer"))
        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!=null && document.documentElement.scrollTop!=null)   
    {   
        var oTop = document.documentElement.scrollTop;   
        document.documentElement.style.overflow = "hidden";   
        document.documentElement.scroll = "no";  
        document.documentElement.scrollTop = oTop;   
    } else if(document.body!=null)   
    {   
        var oTop = document.body.scrollTop;   
        document.body.style.overflow = "hidden";   
        document.body.scroll = "no";   
        document.body.scrollTop = oTop;   
    }
}   

function EnableScroll()   
{   
    if(document.documentElement!=null && document.documentElement.scrollTop!=null)   
    {   
        var oTop = document.documentElement.scrollTop; 
        document.documentElement.scroll = "auto";   
        document.documentElement.style.overflow = "auto";
        document.documentElement.scrollTop = oTop;
    } 
    else if(document.body!=null)   
    {   
        var oTop = document.body.scrollTop; 
        document.body.scroll = "auto";   
        document.body.style.overflow = "auto"; 
        document.body.scrollTop = oTop;
    }
    if(navigator.appName=='Microsoft Internet Explorer')
    {
        document.body.style.overflow = '';
        document.documentElement.style.overflow = '';
    }
}

// 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);
}

function ShowAlert_CloseRefreshParent(txtMsg)
{
    var url;
    
    if(window.opener)
    {
        url = window.opener.location.href;
        createCustomAlert(txtMsg,'',url,true);
    }
    else
    {
        createCustomAlert(txtMsg,'','',true);
    }
}


// Show alert and redirect to another page.
function ShowAlert(txtMsg,redirectSelfURL)
{  
    var url="";
    if(window.opener)
    {
        url = window.opener.location.href;
    }
    createCustomAlert(txtMsg,redirectSelfURL,url,false);
}

// Redirect the parent window and leave self as it is.
function ShowAlertRedirectParent(txtMsg,redirectParentURL)
{
    createCustomAlert(txtMsg,'',redirectParentURL,false);
} 

function ShowAlertRefreshParent(txtMsg)
{
    var url;
    if(window.opener)
    {
        url = window.opener.location.href;
        createCustomAlert(txtMsg,'',url,false);
    }
    else
    {
        createCustomAlert(txtMsg,'','',false);
    }
}

function CheckFeatureAccessible(feature)
{
    for (var i = 0; i < Features.length; i++)
    {
        if(Features[i]==feature)
        {
            return true;
            break;
        }
    }
    return false;
}



function SetDivAtCenter(div)
{
    div.style.display = "block";
    
    var divHeight = parseInt(div.offsetHeight,10);
    var divWidth = parseInt(div.offsetWidth,10);
    
    var oTop = (screen.height - 130 - divHeight)/2  + GetdocumentScrollTop();
    var oLeft = (screen.width - divWidth)/2;
        
    div.style.top = oTop + "px";
    div.style.left = oLeft + "px";
}


function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}
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;
}

