﻿/*
This script is used for myZoover functionality
*/
var _LoginBoxContainerId = "myZooverLoginBoxContainer";
var _ForgotPasswordBoxContainerId = "MyZooverModalContent";
var _Email = "";

// Gets the HTML for the login box
function GetLoginBox() {
    var proxy = new NetMatch.Zoover.Web.MyZoover.Services.MyZooverUIService();
    proxy.GetLoginBox(getMlmmCode(), getLoginBoxHTML, onError);
}

// Gets the HTMl for the Forgot Password popup
function GetForgotPasswordBox() {
	var proxy = new NetMatch.Zoover.Web.MyZoover.Services.MyZooverUIService();
	proxy.GetForgotPasswordBox(getMlmmCode(), getForgotPasswordBoxHTML, onError);
}

// Performs the login
function PerformLogin(user, pass, rememberMe) {
    var proxy = new NetMatch.Zoover.Web.MyZoover.Services.MyZooverUIService();
    proxy.PerformLogin(user, pass, rememberMe, getMlmmCode(), doLoginResult, onError);
}

// Performs the logout
function DoLogout() {
    var proxy = new NetMatch.Zoover.Web.MyZoover.Services.MyZooverUIService();
    proxy.PerformLogout(getMlmmCode(), doLogoutResult);
}

// Performs the forgot password action on a myZoover account email
function ForgotPassword(email) {
	_Email = email;
	var proxy = new NetMatch.Zoover.Web.MyZoover.Services.MyZooverUIService();
	proxy.ForgotPassword(email, getMlmmCode(), ForgotPasswordResult);
}

// Shows the error message in case the forgot password did not work as expected
function ForgotPasswordResult(result) {
	if (result != '') { alert(result); }
	else {
		alert(Password_Remembered.replace("{0}", _Email));
		RemoveForgotPasswordModal();
	}
}

// Refresh the login box if the login was successful
function doLoginResult(result) {
    if (result != '') { alert(result); }
    else {
        GetLoginBox();
    }
}

// Renders the HTML in the login box container
function getLoginBoxHTML(result) {
    if (result != '') { $get(_LoginBoxContainerId).innerHTML = result; }
}

// Renders the HTML in the login box container
function getForgotPasswordBoxHTML(result) {
	if (result != '') { $get(_ForgotPasswordBoxContainerId).innerHTML = result; }
}

// Refresh the login box after the logout
function doLogoutResult(result, userContext, methodName) {
    GetLoginBox();
}

function onError(result) {
	if (result.get_statusCode() != '0')
		alert(result.get_message());
}

function getMlmmCode() {
    if (typeof (MLMMCode) == 'undefined') {
        alert('There is no SiteContext defined!');
    }
    return MLMMCode;
}

// DoLogin function used here is registered from code
function MyZooverKeyPress(event) {
    if (navigator.appName == 'Netscape') {
        if (event.which == 13) {
            event.cancelBubble = true;
            event.returnValue = false;
            DoLogin();
            return false;
        }
    }
    else {
        if (window.event.keyCode == 13) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
            DoLogin();
            return false;
        }
    }
    return true;
}

function DoForgotPassword() {
	var lEmail = document.getElementById('txtMyZooverFPEmail');
	if (lEmail.value != '') {
		ForgotPassword(lEmail.value);
	} else {
		alert(Error_EmailRequired);
	}
}

function ForgotPasswordModal() {
	ShowDropDownsIE6(false);

	ShowCountryFlashMap(false);

	ShowVideoPlayer(false);

	var modalDiv = document.getElementById("MyZooverModalBg");
	if (modalDiv == null) {
		modalDiv = document.createElement("div");
		modalDiv.name = "MyZooverModalBg";
		modalDiv.id = "MyZooverModalBg";
		modalDiv.className = "modalBackground";
		modalDiv.style.width = document.body.offsetWidth + 'px';
		modalDiv.style.height = screen.height + 'px';

		if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) {
			modalDiv.className += " modalBackgroundIE";
			modalDiv.style.height = document.body.offsetHeight;
		}
		document.body.appendChild(modalDiv);
	}
	else {
		modalDiv.style.display = "block";
	}

	var popUpDiv = document.getElementById("MyZooverModalMainContainer");
	if (popUpDiv == null) {
		popUpDiv = document.createElement("div");
		popUpDiv.name = "MyZooverModalMainContainer";
		popUpDiv.id = "MyZooverModalMainContainer";
		popUpDiv.className = "popUpDivClassMyZoover";

		popUpWindow = document.createElement('div');
		popUpWindow.id = 'MyZooverModalContent';
		popUpWindow.className = "popUpDivClassMyZoover";

		popUpDiv.appendChild(popUpWindow);

		document.body.appendChild(popUpDiv);
	}
	else {
		popUpDiv.style.display = "block";
	}
	
	var left = (document.body.offsetWidth - 500) / 2;
	if (left < 0) left = 0;
	popUpDiv.style.left = left + "px";

	SetDefaultMyZooverModalTopPosition();

	GetForgotPasswordBox();
}

function RemoveForgotPasswordModal() {
	var modalDiv = document.getElementById("MyZooverModalBg");
	if (modalDiv != null) {
		modalDiv.style.display = "none";
	}

	var popUpDiv = document.getElementById("MyZooverModalMainContainer");
	if (popUpDiv != null) {
		popUpDiv.style.display = "none";
	}

	ShowCountryFlashMap(true);

	ShowVideoPlayer(true);

	ShowDropDownsIE6(true);
}

// show - bool value to show or hide drop down lists (true = visible)
function ShowDropDownsIE6(show) {
	if (typeof document.body.style.maxHeight == "undefined") // This returns "undefined" only for IE6
	{
		var ddls = document.getElementsByTagName("select");
		for (i = 0; i < ddls.length; i++) {
			ddls[i].style.visibility = show ? "visible" : "hidden";
		}
	}
}

function ShowVideoPlayer(show) {
	var videoPlayer = document.getElementById('frameVideoPlayer');
	if (videoPlayer != null) {
		if (show) {
			videoPlayer.style.visibility = 'visible';
		}
		else {
			videoPlayer.style.visibility = 'hidden';
		}
	}
}

function SetDefaultMyZooverModalTopPosition() {
	var lScrollY, lDefaultY = 250;
	var lMyZooverMainModalContainer;

	if (window.scrollY)
		lScrollY = window.scrollY;
	else
		if (document.documentElement.scrollTop)
		lScrollY = document.documentElement.scrollTop;
	else
		lScrollY = document.body.scrollTop;

	lMyZooverMainModalContainer = document.getElementById('MyZooverModalMainContainer');
	if (lMyZooverMainModalContainer != null) {
		lMyZooverMainModalContainer.style.top = lScrollY + lDefaultY + 'px';
	}
}