//
// Objecte per implementar delegats
//
function Manager() {
  this.callback=function () {}; // do nothing
  this.registerCallback=function(callbackFunction) {
    this.callback=(this.callback).andThen(callbackFunction);
  }
}

//
// objecte apoyo per crear delegats
// 
Function.prototype.andThen=function(g) {
  var f=this;
  return function() {
    f();g();
  }
};

//Amaga o ensenya els submenus
//Parametres:
//    srcSubmenu:	El menú a ensenyar amagar
function ExpandSubmenu( srcSubmenu,srcMenu )
{
	if ( srcSubmenu.style.display == "none" )
	{
		HideSubmenus();
		srcSubmenu.style.display = "block";
		srcMenu.style.background = "#f6ffbf";
		//
		//srcMenu.style.border-top = "solid 1px #000000";
		//
	}
	else
	{
		srcSubmenu.style.display = "none";
		srcMenu.style.background = "#ffffff";
	}
}
function HideSubmenus()
{
	document.getElementById('submenureceive').style.display = "none";
	document.getElementById('submenuadd').style.display = "none";	
	document.getElementById('submenuautors').style.display = "none";
	document.getElementById('submenutools').style.display = "none";
	
	document.getElementById('menuPrincipaltools').style.background = "#ffffff";
	document.getElementById('menuPrincipalReceive').style.background = "#ffffff";
	document.getElementById('menuPrincipalAdd').style.background = "#ffffff";
	//document.getElementById('menuprincipalclubpremium').style.background = "#ffffff";
	document.getElementById('menuPrincipaltools').style.background = "#ffffff";
	
	if (document.getElementById('menuPrincipalAutors')) document.getElementById('menuPrincipalAutors').style.background = "#ffffff";
	
}

//Amaga o ensenya la div del contingut d'una caixa, deixant només visible el títol
//
//Parametres:
//		srcDiv:		La DIV que amagarem o ensenyarem
//		srcImage:	La imatge del botó de amagar o ensenyar que anirà canviant dinamicament quan cliquem
//		imgShow:	URL de la imatge de "Mostrar"
//		imgHide:	URL de la imatge de "Amagar"
//		txtShow:	Text del Tooltip de "Mostrar"
//		txtHide:	Text del Tooltip de "Amagar"
//		guid:		Guid del control	
function ExpandContent( srcDiv, srcImage, imgShow, imgHide, txtShow, txtHide, guid )
{
	if ( srcDiv.style.display == "none" )
	{
		srcDiv.style.display = "block";
		srcImage.src = imgHide;
		srcImage.title = txtHide;
		srcImage.alt = txtHide;
		if( guid )
		{ 
			SaveCookie(guid + 'expandcookie', 'block');
		}
	}
	else
	{
		srcDiv.style.display = "none";
		srcImage.src = imgShow;
		srcImage.title = txtShow;
		srcImage.alt = txtShow;
		if( guid )
		{ 
			SaveCookie(guid + 'expandcookie', 'none');
		}
	}
}

function ShouldSubmit( position )
{	
	//
	// si no volem moure, entrarà un -1
	//
	if ( position < 1 )
	{
		return false;
	}
	
	var JavaPresent;
	
	if ( position < 99 )
	{
		JavaPresent = JavaPresentCenter;
	}
	else
	{
		JavaPresent = JavaPresentRight;
	}
		
	return JavaPresent;
}

//Treu espais del principi i del final de cadenes string
//
//Parametres:
//		str:	cadena a la que li treurem els espais
//
//Retorna un string sense espais ni al principi ni al final
function Trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


//Obre un diàleg centrat a la pantalla amb les mides específicades per paràmetre
//
//Parametres:
//		url:			Pàgina que obrirà la finestra
//		windowName:		Identificador que donarem a la finestra per referint-se a ella si ho necessitem  
//		width:			Amplada
//		height:			Alçada
function OpenCenteredWindow( url, windowName, width, height )
{
	LeftPosition = ( screen.width ) ? ( screen.width - width ) /2 : 0; 
	TopPosition = ( screen.height ) ? ( screen.height - height ) /2 : 0; 
	window.open( url , windowName, 'height=' + height +', width=' + width +', scrollbars=Yes, menubar=no, screenX=110, left='+LeftPosition+', screenY=10, top=' + TopPosition ); 
}


//Obre un diàleg centrat a la pantalla amb les mides específicades per paràmetre. maximitxable
//
//Parametres:
//		url:			Pàgina que obrirà la finestra
//		windowName:		Identificador que donarem a la finestra per referint-se a ella si ho necessitem  
//		width:			Amplada
//		height:			Alçada
function OpenCenteredWindowMaximizable( url, windowName, width, height )
{
	LeftPosition = ( screen.width ) ? ( screen.width - width ) /2 : 0; 
	TopPosition = ( screen.height ) ? ( screen.height - height ) /2 : 0; 
	window.open( url , windowName, 'height=' + height +', width=' + width +', scrollbars=Yes, menubar=no, resizable=1, screenX=110, left='+LeftPosition+', screenY=10, top=' + TopPosition ); 
}

//
//Fa Submit
//
function mySubmit()
{
	document.getElementById('Form1').submit();
}

//
//Redirecciona la pàgina principal, tancant tots els popups si n'hi ha d'oberts
//
function ReStartApp( )
{
	//
	//	És pop up
	//
	if ( window.opener )	
	{
		//
		//És pop up d'un pop up
		//
		if ( window.opener.opener )		
		{
			window.opener.opener.location.href = "login.aspx";
			window.opener.close();
			window.close();
		}
		else
		{
			window.opener.location.href = "login.aspx";
			window.close();
		}
	}
	else	
	{
		//
		//	No és pop up	
		//
		window.location.href = "login.aspx";
	}
}

//
// canvia la chicha de lloc
//
function changePosition( id1, id2 )
{
	if ( id1 == id2 ) return false;
		
	var chicha1 = document.getElementById( id1 ).innerHTML;
	var chicha2 = document.getElementById( id2 ).innerHTML;
	
	document.getElementById( id1 ).innerHTML = chicha2;
	document.getElementById( id2 ).innerHTML = chicha1;
}

//
// dedueix la posició a partir del guid
//
function getPosition( guid )
{
	var flag = false;
	
	for ( var i = 0; i < 199; i++ )
	{
		if ( MovementControl[ i ] != null )
		{
			if ( MovementControl[ i ] == guid )
			{
				return i;
			}
		}
		else
		{
			if ( flag )
			{
				if ( i < 99 )
				{
					i = 99;
				}
				flag = false;
			}
			else
			{
				flag = true;
			}
		}
	}
}

//
//Guarda una cookie (mètode privat, utilitzat pel SaveCookie)
//
function setCookie(pv_nom,pv_valor,pv_expira,pv_path,domain,secure)
{
	document.cookie = pv_nom + '=' + escape(pv_valor) + ((pv_expira == null)?'':('; expires=' + pv_expira.toGMTString())) + ((pv_path)?';path=' + pv_path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
}

//
// Obté una cookie pel nom
//
function GetCookie(pv_nom) {
    if(document.cookie == '') 
    {
		return false; 
    } 
    else 
    {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(pv_nom);
		var NN2Hack = firstChar + pv_nom.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) 
		{
			firstChar += pv_nom.length + 1;
			//
			// Find the end of the value string (i.e. the next ';').
			//
			lastChar = theBigCookie.indexOf(';', firstChar); 
			if(lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} 
		else 
		{
			return false;
		}
    }	
}

//
//Guarda una cookie
//
function SaveCookie(pv_nom,pv_valor)
{
	var today;
	today = new Date();
	var expires;
	expires = new Date();
	expires.setTime(today.getTime() + 10000*60*60*24*365);
	setCookie(pv_nom, pv_valor, expires,"/");
}

//Obté la visibilitat del control actual ( oculta o expandida )
//
//Parametres:
//		controlGuid:	Guid del control
//		srcDiv:			div de contingut del control
//		srcBtn:			botó de ensenyar-amagar del control
function GetVisibility( controlGuid, srcDiv, srcBtn )
{
	var displaying;
	var src;
	var title;
	var alt;
	
	if ( GetCookie( controlGuid + 'expandcookie' ) )
	{
		displaying = GetCookie( controlGuid + 'expandcookie' );
	}
	else
	{
		displaying = 'block';
	}
	
	if ( displaying == 'none')
	{
		src = 'img/maximize.gif';
		title = 'Show';
	}
	else
	{
		src = 'img/minimize.gif';
		title = 'Hide';
	}
		
	srcDiv.style.display = displaying;	
	srcBtn.src = src;
	srcBtn.title = title;
	srcBtn.alt = title;
}

//Obté el nombre de checkboxs total per cada secció i el nombre de seleccionats ( EmailReports, Alerts, Reports )
//
//Parametres:
//		numberSections:	Nombre de seccions ( divs ) que té la pàgina
//
//Per més info mirar codebehind dels usercontrols ReportsAdmin, EmailReportsAdmin, i Alerts
//Funciona perfectament en Explorer, Netscape i Firefox. Dóna problemes amb el collons de Opera, però a qui li importa??
function CountCheckBoxBySection( numberSections )
{
	//
	//Recorrem totes les seccions ( divs ) identifiquem cada div per que el seu id és 'div' + numero
	//
	for ( i = 0; i < parseInt(numberSections); i++ )
	{
		//Agafem tots els elements input que té la div actual
		//Hauriem de fer una comprovació de l'atribut 'type' per acabar d'assegurar que només agafem checkbox
		//però no he fet per que ja se que aquestes divs només tenen inputs checkbox
		var checkboxes;
		checkboxes = document.getElementById('div' + i.toString()).getElementsByTagName('input');
		var checkeds = 0;
		for ( j = 0; j < checkboxes.length; j++ )
		{
			if ( checkboxes[j].checked )
			{
				checkeds++;
			}
		}
		//
		//Assignem les valors de seleccionats i totals als spans corresponents de la div actual
		//
		document.getElementById('checked' + i.toString()).innerHTML = checkeds.toString();
		document.getElementById('total' + i.toString()).innerHTML = checkboxes.length.toString();
		document.getElementById('slash' + i.toString()).innerHTML = "/";
			
	}
}

//
// Estableix els menus clicables o no depenent de si el controls estàn oberts o no 
//
function CheckMenus()
{
	//
	//FX Direct Line
	//
	//CheckMenu( 'CEA090E3-D076-4121-8ABA-967EF365827E', 'submenuadd_directline_label', 'submenuadd_directline_href' );
	//
	//Reports
	//
	CheckMenu( 'FBD056CB-02BF-451A-8DBC-43DF3683F9D1', 'submenuadd_reports_label', 'submenuadd_reports_href' );
	//
	//DowJones
	//
	CheckMenu( 'D96FA60F-BF0A-4779-BCBE-4FCD4E92F720', 'submenuadd_dowjones_label', 'submenuadd_dowjones_href' );
	//
	//4Cast
	//
	CheckMenu( '19010633-8BDC-43C2-A865-02B73C3E2E88', 'submenuadd_4cast_label', 'submenuadd_4cast_href' );
	//
	//Premium Reports
	//
	CheckMenu( '9E19D7C9-CEF4-42BF-BC1E-AF133F4224C6', 'submenuadd_premiumreports_label', 'submenuadd_premiumreports_href' );
	//
	//Special Coverage
	//
	CheckMenu( '7C657D69-D76D-4E40-859E-91AC64006D1F', 'submenuadd_specialcoverage_label', 'submenuadd_specialcoverage_href' );
	//
	//Comments
	//
	CheckMenu( '953054AD-4D2D-4DBB-A4B3-C4A079A69E3A', 'submenuadd_comments_label', 'submenuadd_comments_href' );
	//
	//Economic Alerts
	//
	CheckMenu( '06D6FA6A-CA44-45A8-9B8A-CA3E5EFD4D53', 'submenuadd_economicalerts_label', 'submenuadd_economicalerts_href' );
	//
	//Q&A
	//
	//CheckMenu( 'E3FB2BBB-5E30-482F-911F-4E5DE0482DFE', 'submenuadd_qanda_label', 'submenuadd_qanda_href' );
	//
	//Last Updated Reports
	//
	CheckMenu( 'D9391DB3-34A3-4F0F-8D50-31204DA72673', 'submenuadd_lastupdatedreports_label', 'submenuadd_lastupdatedreports_href' );
	//
	//Highlights
	//
	CheckMenu( 'E745BED1-6CA5-4164-8B81-36B9D56CDD51', 'submenuadd_highlights_label', 'submenuadd_highlights_href' );
	//
	//Live Market
	//
	CheckMenu( '44700BF6-FDC8-456E-9ADC-731A9B732FB0', 'submenuadd_livemarket_label', 'submenuadd_livemarket_href' );
	//
	//Education
	//
	CheckMenu( '32C166E4-AF71-49C5-ACCA-2A2481392F69', 'submenuadd_education_label', 'submenuadd_education_href' );	
}

//
//Estableix el menu clicable o no depenent de si el control està obert o no
//
function CheckMenu( guid, label, link )
{
	if ( IsVisible( guid ) )
	{
		document.getElementById( label ).style.display = "inline";
		document.getElementById( link ).style.display = "none";		
	}
	else
	{
		document.getElementById( label ).style.display = "none";
		document.getElementById( link ).style.display = "inline";
	}
}

//
//Comprova si un control està obert per l'usuari
//
function IsVisible( guid )
{
	//
	// mirem si està al centre
	//
	for ( var i = 1; i < 20; i++ )
	{
		if ( document.getElementById( i ).attributes["control"].value == guid ) 
		{
			return true;
		}		
	}
	//
	// mirem si està a la dreta
	//
	for ( var i = 101; i < 120; i++ )
	{
		if ( document.getElementById( i ).attributes["control"].value == guid ) 
		{
			return true;
		}
	}
	return false;
}