﻿/* 
    --------------------------------------------------------
        Common Functions
    -------------------------------------------------------- 
*/

// Opens a new window in center of screen
function windowOpener(windowHeight, windowWidth, windowName, windowUri)
{
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;

    newWindow = window.open(windowUri, windowName, 'resizable=1,scrollbars=yes,width=' + windowWidth + 
        ',height=' + windowHeight + 
        ',left=' + centerWidth + 
        ',top=' + centerHeight);

    newWindow.focus();
    return true;
}

//
// Browser detect
//
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE 6.0",
			identity: "Explorer6",
			versionSearch: "MSIE 6.0"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE 7.0",
			identity: "Explorer7",
			versionSearch: "MSIE 7.0"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

//
// Get the x pos from an object
//
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;
}

//
// Get the y pos from an object
// 
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;
}

//
// Prevent SPAM to mail addresses
//
function PreventSpam(name, domain) 
{
    return '<a href=\"mailto:' + name + '@' + domain + '\">' + name + '@' + domain + '</a>'; 
}

//
// Function to highlight a textbox when selected
//
function TextBoxWaterMark(txtBox, eventName, strValue) 
{
    var textBox = document.getElementById(txtBox);
    var colBGBlur = '#FFFFFF';
    var colTxtBlur = '#CCCCCC';
    var colBGFocus = '#FFFFE2';
    var colTxtFocus = '#666666';
    var colBGNormal = '#FFFFFF';
    var colTxtNormal = '#666666';
    
    if (eventName == 'onfocus') 
    {
        if (textBox.value == strValue || textBox.value == '')  
        {
            textBox.value = '';
        }
    
        textBox.style.backgroundColor = colBGFocus;
        textBox.style.color = colTxtFocus;
    } 
    else if (eventName == 'onblur') 
    {
        if (textBox.value == strValue || textBox.value == '') 
        {
            textBox.style.backgroundColor = colBGBlur;
            textBox.style.color = colTxtBlur;
            textBox.value = strValue;
        } 
        else 
        {
            textBox.style.backgroundColor = colBGNormal;
            textBox.style.color = colTxtNormal;
        }
    }
}

//
// Delegate function to disable a control, for example a button on submit
//
function DisableControl(ctrl) 
{
    var ctrlObj = document.getElementById(ctrl);
    ctrlObj.disabled = true;
}

//
// Shows a message to the user when updating comments to news
// Autohides the message after 500 milliseconds
//
function DisplayPostingMessage(ctrl, targetCtrl, strMsg) 
{
    var strMsgContainer = document.getElementById('updateInnerText');
    var msgBox = document.getElementById('updateContainer');
    var srcCtrl = document.getElementById(targetCtrl);

    strMsgContainer.innerHTML = strMsg;
    msgBox.style.display = 'block';
    //msgBox.style.left = findPosY(srcCtrl);
    msgBox.style.top = findPosX(srcCtrl);
    
    window.setTimeout("HidePostingMessage('updateContainer')", 500);
}

//
// Displays a posting message at a fixed location on the screen
//
function DisplayPostingMessageFixed(strMsg, offsetLeft, offsetTop) 
{
    var strMsgContainer = document.getElementById('updateInnerText');
    var msgBox = document.getElementById('updateContainer');

    strMsgContainer.innerHTML = strMsg;
    msgBox.style.display = 'block';
    msgBox.style.left = offsetLeft;
    msgBox.style.top = offsetTop;
}

//
// Delegate function to hide a control
//
function HidePostingMessage(ctrl) 
{
    var msgBox = document.getElementById(ctrl);
    msgBox.style.display = 'none';
}

//
// Callback function to hide posting message when update finished
//
function HidePostingMessageCallback() 
{
    var msgBox = document.getElementById('updateContainer');
    msgBox.style.display = 'none';
}


/* 
    --------------------------------------------------------
        Forum Functions
    -------------------------------------------------------- 
*/
function GetUserDetail(PKID, strImgPath)
{
    Brook.WebServices.UserDetails.GetUserDetails(PKID, strImgPath, OnGetUserDetailComplete);
}

function OnGetUserDetailComplete(result)
{
    var divUserFullName = document.getElementById("divUserFullName");
    var imgUserImage = document.getElementById("imgUserImage");
    var divUserClub = document.getElementById("divUserClub");
    var divUserMember = document.getElementById("divUserMember");
    var divUserPosts = document.getElementById("divUserPosts");

    divUserFullName.innerHTML = result.FullName;
    imgUserImage.src = result.UserImage;
    divUserClub.innerHTML = result.Club;
    divUserMember.innerHTML = result.CreateDate;
    divUserPosts.innerHTML = result.ForumPosts;

    showUserDetail();
}

function posUserDetail(x, y) 
{
    //alert(x + "," + y);
    var detailPane = document.getElementById('divUserDetail');
    detailPane.style.left = x + 25 + 'px';
    detailPane.style.top = y  - 230 + 'px';
}

function showUserDetail()
{
    var detailPane = document.getElementById('divUserDetail');
    detailPane.style.display = 'block';
}

function closeUserDetail()
{
    var detailPane = document.getElementById('divUserDetail');
    detailPane.style.display = 'none';
}


    
/* 
    --------------------------------------------------------
        News Functions
    -------------------------------------------------------- 
*/

function ToggleNewsComments(repId, btnId, comments) 
{
    var newsComments = document.getElementById(repId);
    var btnComments = document.getElementById(btnId);

    if (newsComments.style.display == 'none') 
    {
        newsComments.style.display = 'block';
        btnComments.innerHTML = 'Dölj kommentarer';
    }
    else
    {
        newsComments.style.display = 'none';
        btnComments.innerHTML = 'Läs kommentarer (' + comments + ' st)';
    }
    
}

function ToggleNewsCommentsNoId(repId, btnId) 
{
    var newsComments = document.getElementById(repId);
    var btnComments = document.getElementById(btnId);

    if (newsComments.style.display == 'none') 
    {
        newsComments.style.display = 'block';
        btnComments.innerHTML = 'Dölj';
    }
    else
    {
        newsComments.style.display = 'none';
        btnComments.innerHTML = 'Skriv kommentar';
    }
    
}





/* 
    --------------------------------------------------------
        Calendar Functions
    -------------------------------------------------------- 
*/

// Toggles the menu color
function toggleMenu(menuId)
{
    menuItem = document.getElementById(menuId);
    menuStyle = menuItem.currentStyle['color'];
    
    if (menuStyle == '#ffffff') 
    {
        menuItem.style.color = '#4B660C';
    } else
    {
        menuItem.style.color = '#ffffff';
    }

}

// Change the cursor when hovering the menu
function toggleCursor(menuId, cursorStyle) 
{
    menuItem = document.getElementById(menuId);
    menuItem.style.cursor = cursorStyle;
}


// Variable to hold the visible layers id
var intCurrentVisible = 0;

// Positions the calendar popup acoording to the pressed button
function posCalendarPopUp(imgId, x, y)
{
    
    // Hide other layers before displaying this
    if (intCurrentVisible != 0) 
    {
        document.getElementById(intCurrentVisible).style.display = 'none';
        document.getElementById(intCurrentVisible + '_shadow').style.display = 'none';
        document.getElementById(intCurrentVisible + '_shadowTop').style.display = 'none';
    }
    
    // Get the menu layer and the shadow layer which fires the close menu event
    var imgObj = document.getElementById(imgId);
    var imgObjShadow = document.getElementById(imgId + '_shadow');
    var imgObjShadowTop = document.getElementById(imgId + '_shadowTop');
    //var objIFrame = document.getElementById('iframe');
    
    
    // The margins top and left from the calendar control to the top and left margin
    if (BrowserDetect.browser == 'Firefox') 
    {
        var intLeft = 105;
        var intTop = 153;
    } 
    else if (BrowserDetect.browser == 'Explorer6') 
    {
        var intLeft = 142;
        var intTop = 153;    
    }
    else if (BrowserDetect.browser == 'Explorer7') 
    {
        var intLeft = 103;
        var intTop = 153;    
    } 
    else 
    {
        var intLeft = 105;
        var intTop = 153;    
    }
    
    // Position the IFrame
    //objIFrame.style.left = x - (intLeft - 40) + 'px';
    //objIFrame.style.top = y - (intTop - 120) + 'px';
    //objIFrame.style.display = 'block';
    
    // Position the menu layer
    imgObj.style.left = x - intLeft + 'px';
    imgObj.style.top = y - intTop + 'px';
    
    // Position the shadow layer
    imgObjShadow.style.left = (x - intLeft) - 10 + 'px';
    imgObjShadow.style.top = y - (intTop) + 'px';

    // Position the top shadow layer
    imgObjShadowTop.style.left = (x - intLeft) - 65 + 'px';
    imgObjShadowTop.style.top = y - (intTop + 60) + 'px';
        
    imgObj.style.display = 'block';
    imgObjShadow.style.display = 'block';
    imgObjShadowTop.style.display = 'block';

    
    intCurrentVisible = imgId;

}

// Hides the meny layer and the corresponding shadow layer
function hideCalendarPopUp(imgId)
{
    // Get the menu layer and the shadow layer which fires the close menu event
    var imgObj = document.getElementById(imgId);
    var imgObjShadow = document.getElementById(imgId + '_shadow');
    var imgObjShadowTop = document.getElementById(imgId + '_shadowTop');
    
    // Hide the layers        
    imgObj.style.display = 'none';
    imgObjShadow.style.display = 'none';
    imgObjShadowTop.style.display = 'none';
}


// Variable to hold the visible layers id
var intCompInfoCurrentVisible = 0;

// Positions the calendar popup acoording to the pressed button
function posCompInfoPopUp(imgId, shadowId, x, y, linkCount)
{
    
    // Hide other layers before displaying this
    if (intCompInfoCurrentVisible != 0) 
    {
        document.getElementById(intCompInfoCurrentVisible).style.display = 'none';
        document.getElementById(shadowId).style.display = 'none';
    }

    // Get the menu layer and the shadow layer which fires the close menu event
    var imgObj = document.getElementById(imgId);
    var imgObjShadow = document.getElementById(shadowId);

    var intTop = 153;
    
    // The margins top and left from the calendar control to the top and left margin
    if (BrowserDetect.browser == 'Firefox') 
    {
        var intLeft = 45;
    } 
    else if (BrowserDetect.browser == 'Explorer6') 
    {
        var intLeft = 45;
    }
    else if (BrowserDetect.browser == 'Explorer7') 
    {
        var intLeft = 45;
    } 
    else 
    {
        var intLeft = 45;
    }

    // Position the menu layer
    imgObj.style.left = x - intLeft + 'px';
    imgObj.style.top = y - intTop + 'px';
    
    // Position the shadow layer
    imgObjShadow.style.left = (x - intLeft) - 10 + 'px';
    imgObjShadow.style.top = y - (intTop) + 'px';
    
    // Calculate the shadow layer height
    var shadowHeight = (linkCount * 20) + 30;
    imgObjShadow.style.height = shadowHeight + 'px';
   
    imgObj.style.display = 'block';
    imgObjShadow.style.display = 'block';

    
    intCompInfoCurrentVisible = imgId;

}

// Hides the meny layer and the corresponding shadow layer
function hideCompInfoPopUp(imgId, shadowId)
{
    // Get the menu layer and the shadow layer which fires the close menu event
    var imgObj = document.getElementById(imgId);
    var imgObjShadow = document.getElementById(shadowId);
    
    // Hide the layers        
    imgObj.style.display = 'none';
    imgObjShadow.style.display = 'none';
}


// Positions the calendar popup acoording to the pressed button
function posNewsCalendarPopUp(calId, x, y)
{
     
    // Get the calendar layer
    var calObj = document.getElementById(calId);

    // The margins top and left from the calendar control to the top and left margin
    if (BrowserDetect.browser == 'Firefox') 
    {
        var intLeft = 345;
        var intTop = 50;   
    } 
    else if (BrowserDetect.browser == 'Explorer6') 
    {
        var intLeft = 345;
        var intTop = 50;   
    }
    else if (BrowserDetect.browser == 'Explorer7') 
    {
        var intLeft = 345;
        var intTop = 50;    
    } 
    else 
    {
        var intLeft = 345;
        var intTop = 50;   
    }

    // Position the menu layer
    calObj.style.left = x + intLeft + 'px';
    calObj.style.top = y + intTop + 'px';

    calObj.style.display = 'block';
}

// AJAX function to update drop down menu items
function updateMenuItems(value) 
{
    var behavior = $find('dynExtender1');

    if (behavior) 
    {
        behavior.populate(value);
    }
}

// AJAX function to update dynamic image
function updateImageContainer(value) 
{
    var behavior = $find('dynImgContainer');

    if (behavior) 
    {
        behavior.populate(value);
    }
}

/* 
    --------------------------------------------------------
        Member Functions
    -------------------------------------------------------- 
*/

//
// Function to autogenerate a user name based on firstname and lastname
//
function GenerateUserName(txtFirstName, txtLastName, txtUserName) 
{
    var firstName = document.getElementById(txtFirstName).value.toLowerCase();
    var lastName = document.getElementById(txtLastName).value.toLowerCase();
    var userName = "bro" + lastName.substr(0,2) + firstName.substr(0,2);

    document.getElementById(txtUserName).value = userName;
    document.getElementById(txtUserName).focus();
}

//
// Confirms reset password
//
function ConfirmResetPassword() 
{
    return confirm('VARNING!\nDetta skapar ett nytt lösenord för användaren och kan inte ångras.\nVill du fortsätta med åtgärden?');
}

//
// Confirms user deletion
//
function ConfirmDeleteUser() 
{
    return confirm('VARNING!\nDetta raderar användaren permanent och kan inte ångras.\nVill du fortsätta med åtgärden?');
}

//
// Confirms news deletion
//
function ConfirmDeleteNews()
{
    return confirm('Detta raderar nyheten permanent och kan inte ångras.\nVill du fortsätta med åtgärden?');
}


/* 
    --------------------------------------------------------
        Activity Functions
    -------------------------------------------------------- 
*/

function GetActivityDetail(aId)
{
    Brook.WebServices.ActivityDetail.GetActivityDetail(aId, OnGetActivityDetailComplete);
}

function OnGetActivityDetailComplete(result)
{
    var aDate = document.getElementById("aDate");
    var aTime = document.getElementById("aTime");
    var aDetails = document.getElementById("aDetails");
    var aPlace = document.getElementById("aPlace");
    var aResponsible = document.getElementById("aResponsible");
    var aOther = document.getElementById("aOther");
    var aContact = document.getElementById("aContact");

    aDate.innerHTML = result.ActivityDate;
    aTime.innerHTML = result.ActivityTime;
    aDetails.innerHTML = result.ActivityDescription;
    aPlace.innerHTML = result.ActivityPlace;
    aResponsible.innerHTML = result.ActivityResponsible;
    aOther.innerHTML = result.ActivityOther;
    aContact.innerHTML = result.ActivityContact;

    var divActivityDetail = document.getElementById("divActivityDetail");
    divActivityDetail.style.display = 'block';
}

function posActivityDetail(x, y) 
{
    //alert(x + "," + y);
    var detailPane = document.getElementById('divActivityDetail');
    detailPane.style.left = x - 60 + 'px';
    detailPane.style.top = y - 370 + 'px';
}

function showActivityDetail()
{
    var detailPane = document.getElementById('divActivityDetail');
    detailPane.style.display = 'none';
}

function closeActivityDetail()
{
    var detailPane = document.getElementById('divActivityDetail');
    detailPane.style.display = 'none';
}