﻿jQuery.fx.off = true;

function PleaseWaitPopUp(bacgroundcolor) {
    $.nyroModalManual({
    bgColor: bacgroundcolor,
        content: "<div id='AjaxLoading'>" + Translate('Common..Ajax.PleaseWait') + "</div>",
        modal: true,
        resizeable: false,
        autoSizable: false,
        closeButton: null,
        minWidth: 130,
        minHeight: 25,
        width: 130,
        height: 25
    });
}

function WebRequestManagerCompletedRequest(sender, eventArgs) {
    $.nyroModalRemove();
}

function WebRequestManagerInvokingRequest(sender, eventArgs) {
    PleaseWaitPopUp('transparent');
}



function PopUpDescription(ID, Type) {
    $.nyroModalManual({
        url: '/IDescription/?ID=' + ID + '&Type=' + Type,
        minWidth: 500,
        minHeight: 250,
        autoSizable: true,
        ltr: !RTL
    });
}


function PopUpLoadingMessage() {
    $.nyroModalManual({
        bgColor: '#ffffff',
        content: "<div id='AjaxLoadingImage' style='overlow:visible;white-space:nowrap;'>" + Translate('Common..Ajax.PleaseWaitLoadingImage') + "</div>",
        modal: true,
        resizeable: false,
        autoSizable: true,
        closeButton: null,
        minWidth: 10,
        minHeight: 25,
        //width: 300,
        height: 25
    });
}


// -------------------------------------------- DisplayMessage --------------------------------------------
function DisplayMessage(MessageObject) {
    var MessageBox = $("<p></p>");
    MessageBox.setTemplateElement("MessageBoxTemplate", null, { filter_data: (MessageObject.DisplayHtmlMessage != undefined) ? (!MessageObject.DisplayHtmlMessage) : false });
    MessageBox.processTemplate(MessageObject);
     $.nyroModalManual({
        bgColor: '#bbbbbb',
        content: unescape(MessageBox.html()),
        modal: false,
        resizeable: false,
        closeButton: null,
        minWidth: 0,
        minHeight: 0,
		css: { // Default CSS option for the nyroModal Div. Some will be overwritten or updated when using IE6
			bg: {
				position: 'absolute',
				overflow: 'hidden',
				top: 0,
				left: 0,
				height: '100%',
				width: '100%'
			},
			wrapper: {
				position: 'absolute',
				top: '50%',
				left: '50%',
				border: 'none',
				'background-color': 'transparent'
			},
			wrapper2: {
			},
			content: {
				overflow: 'auto'
			},
			loading: {
				position: 'absolute',
				top: '50%',
				left: '50%',
				marginTop: '-50px',
				marginLeft: '-50px'

			}
		}        
    });
}


var User = {
    Login: function() {
    if (!$(".LoginPanelContainer").jForm().validate()) return;    
        var Email = $("#fldLoginEmail").val();
        var Password = $("#fldLoginPassword").val();
        B2CPrint.G1.WebSite.WS.User.Login(Email, Password, this.LoginOnSucceeded, this.LoginOnFailed);
    },

    LoginOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
            window.location.reload();
        }
    },

    LoginOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },

    Logout: function() {
        B2CPrint.G1.WebSite.WS.User.Logout(this.LogoutOnSucceeded, this.LogoutOnFailed);
    },

    LogoutOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
			var str='x'+this.location;
			str = str.toLowerCase();
			str_arr = str.split('/');
			url = str_arr[0].replace('x','')+'//'+str_arr[2]+'/Logout';
			location.href=url;
            // window.location.reload();
        }
    },

    LogoutOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    }
};

function goHome() {

	var thePageUrl='x'+this.location;
	thePageUrl = thePageUrl.toLowerCase();
	var homeUrl = thePageUrl.replace('x','');
	homeUrl = homeUrl.replace('logout','');
	location.href=homeUrl;
}

try {
	var thePageUrl='x'+this.location;
	thePageUrl = thePageUrl.toLowerCase();
	var isLogOutUrl = thePageUrl.indexOf('logout');
	if (isLogOutUrl!=-1) {
			var homeUrl = thePageUrl.replace('x','');
			homeUrl = homeUrl.replace('logout','');
			setTimeout("goHome()", 5000);
	}
} catch(e) { };

function showDiv(obj) {
    
    var d = document.getElementById(obj);
    var pd = d.parentNode
    
    $("#ProductsMenu1").attr("z-index", "99999")
    $("#ProductsMenu1").show();
    $("#ProductsMenu1").attr("style", "position:absolute;top:" + Number(findPosY(pd)-110) + "px;left:" + Number(findPosX(pd)-160) + "px;width:100px")
};
function hideDiv(obj) {
    $("#ProductsMenu1").hide();
};
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
};

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

