﻿// JScript File
// Store general Javascript that is not specifically for the API
// i.e. this page provides general javascript functionality.

// Page Specific code.

// ### Login page (flProfile.ashx)

function flLogin () {
    var formErrors = '';
    if (document.frmFlLogin.email.value == "") {
        formErrors = "<li>You must enter your email or user ID</li>";
    } else {
        //if (!(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i).test(document.getElementById("email").value)) {
        //    formErrors = "<li>You must enter a valid email address</li>";            
        //}
    }
    if (document.frmFlLogin.password.value == "") {
        formErrors = formErrors + "<li>You must enter your password</li>";
    }
    
    if (formErrors == "") {
        var queryStrings = parseGetVars();
        if (typeof currentPage != "undefined") {
            document.cookie = "fApiUrl=" + escape(((typeof currentPage.previousURL != "undefined")? currentPage.previousURL : '')); // Set cookie of location to return in the api.
            // API Pages
            if (typeof queryStrings["fReturnUrl"] == "undefined") {
                document.frmFlLogin.action = globalApiPath + "flLogin.ashx?fReturnUrl=" + escape(window.location.href) + "&displayMode=html";
            } else {
                document.frmFlLogin.action = globalApiPath + "flLogin.ashx?fReturnUrl=" + queryStrings["fReturnUrl"] + "&displayMode=html";
            }
        }
        document.frmFlLogin.submit();
    
    
        // Continue with login process
        //navigate('flLogin.ashx?email=' + document.getElementById("email").value + '&password=' + document.getElementById("password").value);
    } else {
        // Error occured. Display friendly message
        document.getElementById("flMessage").style.display = "block";
        document.getElementById("flMessage").innerHTML = 'The following errors occured:<br /><ul>' + formErrors + '</ul>';
    }
}

function flRegisterLogin (returnUrl) {
    var queryStrings = parseGetVars();
    document.cookie = "fApiUrl=" + escape(returnUrl); // Set cookie of location to return in the api.
    // API Pages
    if (typeof queryStrings["fReturnUrl"] == "undefined") {
        document.frmFlLogin.action = globalApiPath + "flLogin.ashx?fReturnUrl=" + escape(window.location.href) + "&displayMode=html";
    } else {
        document.frmFlLogin.action = globalApiPath + "flLogin.ashx?fReturnUrl=" + queryStrings["fReturnUrl"] + "&displayMode=html";
    }
    document.frmFlLogin.submit();
}

function flLogout() {
    var queryStrings = parseGetVars();
    document.frmFlLogin.action = globalApiPath + "flLogout.ashx?fReturnUrl=" + escape(window.location.href) + "&displayMode=html";
    if (typeof currentPage != "undefined") {
        // API Pages
        document.cookie = "fApiUrl=" + escape(((typeof currentPage.previousURL != "undefined")? currentPage.previousURL : ''));
        if (typeof queryStrings["fReturnUrl"] == "undefined") {
            document.frmFlLogin.action = globalApiPath + "flLogout.ashx?fReturnUrl=" + escape(window.location.href) + "&displayMode=html";
        } else {
            document.frmFlLogin.action = globalApiPath + "flLogout.ashx?fReturnUrl=" + queryStrings["fReturnUrl"] + "&displayMode=html";
        }
    }
    document.frmFlLogin.submit();
}

// Popup Menu for Profile info.
//finds the position of an element
function findPos(obj) {
   var curleft = curtop = 0;
   if (obj.offsetParent) {
          curleft = obj.offsetLeft
          curtop = obj.offsetTop
          while (obj = obj.offsetParent) {
                 curleft += obj.offsetLeft
                 curtop += obj.offsetTop
          }
   }
   return [curleft,curtop];
}

function showPopup(hoveritem) {
    hp = document.getElementById("hover_menu");
    // Set position of hover-over popup
    
    
    hp.style.top = findPos(hoveritem)[1] + hoveritem.offsetHeight - 1 + 'px';
    hp.style.left = findPos(hoveritem)[0] + 'px';
    // Set popup to visible
    hp.style.display = "block";
    
    
    if (parseInt(findPos(hp)[0]) > parseInt(hp.style.left)) {
        // Recalculate left position if wrong
        hp.style.left = parseInt(hp.style.left) - (parseInt(findPos(hp)[0]) - parseInt(hp.style.left)) + 'px';
    }
    
    if (parseInt(findPos(hp)[1]) > parseInt(hp.style.top)) {
        // Recalculate top position if wrong
        hp.style.top = parseInt(hp.style.top) - (parseInt(findPos(hp)[1]) - parseInt(hp.style.top)) + 'px';
    }
    
}

function hidePopup(event) {
    if(event){
        hp = document.getElementById("hover_menu");
        var mouseX = event.clientX + document.body.scrollLeft;
        var mouseY = event.clientY + document.body.scrollTop;
        var bounds = new Array();
        var pos = findPos(hp);
        bounds[0] = pos[1];
        bounds[1] = pos[0]+hp.clientWidth;
        bounds[2] = pos[1]+hp.clientHeight;
        bounds[3] = pos[0];
        
        if(mouseY<bounds[0] || mouseY>bounds[2] || mouseX<bounds[3] || mouseX>bounds[1]){
            hp.style.display = "none";
        }
    }
}

function showHoverPanel(panelID) {
    hp = document.getElementById("hoverPanel" + panelID);
    hoveritem = document.getElementById("icon" + panelID);
    // Set position of hover-over popup

    // Set popup to visible    
    hp.style.display = "block";
        
    hp.style.top = findPos(hoveritem)[1] + hoveritem.offsetHeight - 1 + 'px';
    hp.style.left = findPos(hoveritem)[0] - (hp.offsetWidth/2) + 6 + 'px'; // Try and centre the panel on the icon.
    
    
    
    
    if (parseInt(findPos(hp)[0]) > parseInt(hp.style.left)) {
        // Recalculate left position if wrong
        hp.style.left = parseInt(hp.style.left) - (parseInt(findPos(hp)[0]) - parseInt(hp.style.left)) + 'px';
    }
    
    if (parseInt(findPos(hp)[1]) > parseInt(hp.style.top)) {
        // Recalculate top position if wrong
        hp.style.top = parseInt(hp.style.top) - (parseInt(findPos(hp)[1]) - parseInt(hp.style.top)) + 'px';
    }
    
    
}

function showHoverPanel_DAC(panelID) {
    hp = document.getElementById("hoverPanel" + panelID);
    hoveritem = document.getElementById("DAC_Icon" + panelID);
    // Set position of hover-over popup

    // Set popup to visible    
    hp.style.display = "block";
        
    hp.style.top = findPos(hoveritem)[1] + hoveritem.offsetHeight - 1 + 'px';
    hp.style.left = findPos(hoveritem)[0] - 6 + 'px'; // Try and centre the panel on the icon.
    
    
    
    
    if (parseInt(findPos(hp)[0]) > parseInt(hp.style.left)) {
        // Recalculate left position if wrong
        hp.style.left = parseInt(hp.style.left) - (parseInt(findPos(hp)[0]) - parseInt(hp.style.left)) + 'px';
    }
    
    if (parseInt(findPos(hp)[1]) > parseInt(hp.style.top)) {
        // Recalculate top position if wrong
        hp.style.top = parseInt(hp.style.top) - (parseInt(findPos(hp)[1]) - parseInt(hp.style.top)) + 'px';
    }
    
    
}

function hideHoverPanel(panelID) {
    hp = document.getElementById("hoverPanel" + panelID);
    hp.style.display = "none";
}


// Hover panel for generic views
function showGenericHoverPanel(panel,link) {
    hp = document.getElementById(panel);
    hoveritem = document.getElementById(link);
    // Set position of hover-over popup


    // Set popup to visible
    
    hp.style.display = "block";
    
    hp.style.top = findPos(hoveritem)[1] - hp.offsetHeight + 'px';
    hp.style.left = findPos(hoveritem)[0] + 'px'; // Try and centre the panel on the icon.
    
    if (parseInt(findPos(hp)[0]) > parseInt(hp.style.left)) {
        // Recalculate left position if wrong
        hp.style.left = parseInt(hp.style.left) - (parseInt(findPos(hp)[0]) - parseInt(hp.style.left)) + 'px';
    }
    
    if (parseInt(findPos(hp)[1]) > parseInt(hp.style.top)) {
        // Recalculate top position if wrong
        hp.style.top = parseInt(hp.style.top) - (parseInt(findPos(hp)[1]) - parseInt(hp.style.top)) + 'px';
    }

}
function hideGenericHoverPanel(panel) {
    hp = document.getElementById(panel);
    hp.style.display = "none";
}



// Parse Query String

function parseGetVars() {
	var getVars = new Array();
	var qString = top.location.search.substring(1);
	if (qString!='') {
		var pairs = qString.split(/\&/);
		for (var i in pairs) {
			//alert('found:'+pairs[i]);
			var nameVal = pairs[i].split(/\=/);
			getVars[unescape(nameVal[0])] = unescape(nameVal[1]);
		}
	}
	return getVars;
}


/*
Cup competition dynamic js
*/
var flCurrentCupRound = 1;
var flTotalCupRounds;

//used for standard style cup display
function changeCupRound(increment){
    //initialise flTotalCupRounds
    if(flTotalCupRounds===undefined){
        flTotalCupRounds=0;
        for(var j=0;j<=7;j++){
        if(document.getElementById("round"+j)){
                flTotalCupRounds++;
            }
        }
    }
    
    //fix the refresh bug implicit check on currently displayed round
    if(document.getElementById("round1").style.display==''){
        //alert(flCurrentCupRound);
        flCurrentCupRound=1;
    }
    
    if(increment>0 && flCurrentCupRound<flTotalCupRounds-1){
        flCurrentCupRound += increment;
         show(document.getElementById("cupPrevTop"));
         show(document.getElementById("cupPrevBottom"));
         hide(document.getElementById("round"+(flCurrentCupRound-1)));
         hide(document.getElementById("roundType"+(flCurrentCupRound-1)));
         show(document.getElementById("round"+(flCurrentCupRound+1)));       
         show(document.getElementById("roundType"+(flCurrentCupRound+1)));   
    }
    //hide next buttons when final round reached
     if(increment>0 && flCurrentCupRound==flTotalCupRounds-1){
        hide(document.getElementById("cupNextTop"));
        hide(document.getElementById("cupNextBottom"));
    }
    
    if(increment<0 && flCurrentCupRound>1){
         flCurrentCupRound += increment;
         show(document.getElementById("cupNextTop"));
         show(document.getElementById("cupNextBottom"));
         hide(document.getElementById("round"+(flCurrentCupRound+2)));
         hide(document.getElementById("roundType"+(flCurrentCupRound+2)));
         show(document.getElementById("round"+(flCurrentCupRound)));       
         show(document.getElementById("roundType"+(flCurrentCupRound)));   
         
    }
    //hide previous buttons when 1st round reached
    if(increment<0 && flCurrentCupRound==1){
        hide(document.getElementById("cupPrevTop"));
        hide(document.getElementById("cupPrevBottom"));
    }
}

//used for table style cup display
function showRound(roundNum){
    if(flTotalCupRounds===undefined){
        flTotalCupRounds=0;
        for(var j=0;j<=10;j++){
        if(document.getElementById("round"+j)){
                flTotalCupRounds++;
            }
        }
    }
    for(var i=0;i<=flTotalCupRounds;i++){
        hide(document.getElementById("round"+i));
    }
    show(document.getElementById("round"+roundNum));
}

function hide(element) {
    if(element){
        element.style.display = 'none';
    }
}
    
function show(element) {
    if(element){
        element.style.display = '';
    }
}


/* End Cup Compeition js */

function enterListener(element,event){
    var el = document.getElementById(element);
    if (event.keyCode==13){
        eval(el.href);
    }
}