﻿
function SetCookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function GetCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	



function showKBMessageBox(kbType, kbMessage, kbTitle)
{
    
    if(kbTitle != '')
    {
        document.getElementById('GenericMessageBoxTitle').style.visibility = 'visible';
    }
    else
    {
        document.getElementById('GenericMessageBoxTitle').style.visibility = 'hidden';
        document.getElementById('GenericMessageBoxTitle').innerHTML = kbTitle.toString().replace(/&#13;/gi,'<br/>');
    }    
        
    
    document.getElementById('GenericMessageBox').style.visibility = 'visible';
    document.getElementById('GenericMessageBoxMessage').innerHTML = kbMessage.toString().replace(/&#13;/gi,'<br/>');
    if(kbType == 'ERROR')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconError';
    else if(kbType == 'WARNING')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconWarning';
    else if(kbType == 'INFO')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconInfo';
    else if(kbType == 'FAILED')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconFailedOperation';
    else if(kbType == 'SUCCESS')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconSuccessOperation';
    else if(kbType == 'FAILED_WRONG_INPUT')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageIconFailedOperationWrongInput';
    else if(kbType == 'ACCESS_DENIED')
        document.getElementById('GenericMessageBoxMessageIcon').className = 'GenericMessageBoxMessageAccessDenied';
    else
        alert(kbTitle + ':' + kbMessage);
    
}

function hideKBMessageBox()
{
     document.getElementById('GenericMessageBox').style.visibility = 'hidden';
}


function hideMasterpageHeaderBodySeparator()
{
    document.getElementById('masterpageHeaderBodySeparator').style.display = 'none';   
}

/*
    Menu
*/
function overMe(id)
	{
		currentlyOver = id;
		//alert('currentlyOver=' + currentlyOver);
	}
	
	function notOverMe(id)
	{
		if(currentlyOver == id)
			currentlyOver = '';
	}
	
	function showMenuItemChilds(array)
	{
		for(i=0; i<array.length; i++)
		{
			var objMenuItem = document.getElementById(array[i]);
			if(objMenuItem == null)
				break;
			objMenuItem.style.display = 'block';
			objMenuItem.parentNode.parentNode.parentNode.style.display = 'block';
		}
	}
	function hideMenuItemChilds(array)
	{
		var parameters = '';
		var doHide = true;
		//checks if mouse is currently in any child element
		for(i=0; i<array.length; i++)
		{
			if(currentlyOver == array[i])
				doHide = false;
			
			if(i>0)
				parameters += ', ';
			parameters += '\'' + array[i] + '\'';
		}
		if(doHide){
			//alert(parameters);
			setTimeout('hideChilds(new Array(' + parameters + '))', 1000);
		}
	}
	function hideChilds(array)
	{
		//alert('hiding childs:' + array);
		var doHide = true;
		var parameters = '';
		for(i=0; i<array.length; i++)
		{
			if(currentlyOver == array[i])
				doHide = false;
				
			if(i>0)
				parameters += ', ';
			parameters += '\'' + array[i] + '\'';
		}
		
		if(currentlyOver != '')
			doHide = false;
			
		if(doHide){
			for(i=0; i<array.length; i++)
			{
				var objMenuItem = document.getElementById(array[i]);
				objMenuItem.style.display = 'none';
				objMenuItem.parentNode.parentNode.parentNode.style.display = 'none';
			}
		}
		else
		{
			setTimeout('hideMenuItemChilds(new Array(' + parameters + '))', 1000);
		}
	}
	
	function saveMenuOpen(array, link)
	{
		var parameters = '';
		for(i=0; i<array.length; i++)
		{
				
			if(i>0)
				parameters += ', ';
			parameters += '\'' + array[i] + '\'';
		}
		//alert('saving:' + parameters);
		SetCookie( 'opened_menu_items' , parameters, '', '', '');
		window.location = link;
	
	}
	function loadMenuOpen()
	{
		var parameters = GetCookie('opened_menu_items'); 
		
		if(parameters != null && parameters != ''){
			//alert('loading: ' + parameters);
			setTimeout('showMenuItemChilds(new Array(' + parameters + '))', 100);
		}
		//new Array({4})
		//var element = document.getElementById(GetCookie(menuName));
		//element.style.display = 'block';
	}			

