var FXStreet = 
{
	Version: '1.0.0',
	Libraries: [],
	OnLoadFunctions: [],
	roadBlock: '',
	showBrokersPopup: true,
	jsPath : '',
	httpPath : '',
	strPageIndex : '-page',
	HttpPath: function()
	{
		return this.httpPath;
	},
	require: function(libraryName, onLoadFunction) 
	{
		//
		//	Comprueba si ya está cargada la libreria para no duplicarla.
		//
		for (var i =0;i<this.Libraries.length;i++) 
		{
			if (this.Libraries[i] == libraryName) return;
		}
		this.Libraries[this.Libraries.length] = libraryName;

		var src = libraryName.substring(0,4).toLowerCase() != "http" ? this.jsPath + libraryName : libraryName ;
		document.write('<script type="text/javascript" src="' + src + '"></script>');
		
		for (var i = 1 ; i < arguments.length ; i++)
		{
			this.registerOnLoadFunction(arguments[i]);
		}
	},
	registerOnLoadFunction: function(func)
	{
			this.OnLoadFunctions[this.OnLoadFunctions.length] = func;
	},
	init: function(path)
	{
		this.jsPath = path + "/js/";
		this.httpPath = path;
	},
	load: function() 
	{
		var showed = this.showRoadBlock();
		//
		//	Aqui hacemos el window.onload de todo lo que tengamos definido
		//
		for (var i = 0 ; i < this.OnLoadFunctions.length ; i++) 
		{
			try
			{
				eval(this.OnLoadFunctions[i]);
			}
			catch (exception)
			{
				alert("Error " + exception.message + " - Ejecutando " + this.OnLoadFunctions[i]);
			}
		}
		if (!showed) this.showRoadBlock();
	},
	trim: function(str)
	{
		return str.replace(/^\s*|\s*$/g,"");
	},
	totalTrim: function(str)
	{
		return str.replace(' ','');
	},
	openwin: function(url, name, width, height, center, scrollbars, menubar, resizable)
	{
		var args = 'width=' + width +', height=' + height;

		if (center)
		{
			wleft = ( screen.width ) ? ( screen.width - width ) /2 : 0;
			wtop = ( screen.height ) ? ( screen.height - height ) /2 : 0;
			args += ',left=' + wleft + ',top=' + wtop;
		}
		args += ',scrollbars='	+ scrollbars;
		args += ',menubar='		+ menubar;
		args += ',resizable='	+ resizable;
		
		window.open( url , name, args);
		return false;
	},
	resize: function(width, height, center)
	{
		if (center)
		{
			wleft = ( screen.width ) ? ( screen.width - width ) /2 : 0;
			wtop = ( screen.height ) ? ( screen.height - height ) /2 : 0;
			window.moveTo(wleft,wtop);
		}
		window.resizeTo(width, height);
	},	
	popup: function(url)
	{
		return this.openwin(url, '_blank', 800, 600, 1, 1, 1);
	},
	maximize: function()
	{
		var w = screen.width - 15;
		var h = screen.height - 85;
		var wleft = ( screen.width ) ? ( screen.width - w ) /2 : 0;
		var	wtop = ( screen.height ) ? ( screen.height - h ) /2 : 0;
		window.moveTo(wleft,wtop);
		window.resizeTo(w, h);
	},
	forgotPassword: function()
	{
		var url = this.httpPath + "/account/forgotpassword/default.aspx";
		this.openwin(url, "_blank", 300, 200, 1, 0, 0);
	},
	acceptChatRules: function()
	{
		var url = this.httpPath + "/live/sessions/rules/default.aspx";
		this.openwin(url, "_blank", 600, 620, 1, 0, 0);
	},
	registerUser: function(level)
	{
		var url = this.httpPath + "/signup/default.aspx?la=";
		if (arguments.length == 0) level = 1;
		url += level;
		if (window.opener)
		{
			url += "&referer=" + window.opener.location;
			window.opener.location = url;
			window.close();		
		}
		else
		{
			url += "&referer=" + window.location;
			window.location = url;
		}
	},	
	uploadFile: function()
	{
		//
		//	Reservado para el upload de ficheros futuro
		//
		//var url = this.httpPath + "/
	},
	createBookmark: function() 
	{
		if (window.sidebar) 
		{
			window.sidebar.addPanel(title, url,""); 
		} 
		else if( document.all ) 
		{
			window.external.AddFavorite( url, title);
		} 
		else if( window.opera && window.print ) 
		{
			return true;
		}	
	},
	sendToDelicius: function()
	{
		//URL of this document
		var loc=location.href;
		//Strip out any anchors
		var apos=loc.indexOf('#');
		loc=(apos>0?loc.substring(0,apos):loc);
		//Redirect to del.icio.us
		location.href='http://del.icio.us/post?v=2&amp;url='
					 + encodeURIComponent(loc)
					 + '&amp;title='
					 + encodeURIComponent(document.title);
		//Return false so the link won't be activated. 
		return false;	
	},
	send: function(url)
	{
		var targeturl = this.httpPath + "/email/sendtofriend/?url=" + url;		
		this.openwin(targeturl, "_blank", 400, 300, 1, 0, 0);
	},
	print: function()
	{
		window.print();
	},
	previous: function(uniqueId)
	{
		//
		//	incrementar contador
		//
		var pagecounter = document.getElementById(uniqueId + this.strPageIndex);
		var form = document.getElementById(uniqueId + '-myform')
		var action = document.location.toString();
		var endChar = action.charAt(action.length - 1);
		if (endChar == "/") action += "default.aspx";
		form.action = action;
		
		if (!pagecounter) 
		{
			pagecounter = document.getElementById(uniqueId +"-pageIndex");
		}
		
		if (!pagecounter) 
		{
			alert("No existe el contador de pagina " + uniqueId + this.strPageIndex + " ni " + uniqueId +"-pageIndex");
			return false;
		}
		if (!form) 
		{
			alert("No existe el formulario " + uniqueId + '-myform');
			return false;
		}
		var page = parseInt(pagecounter.value, 10);
		if (isNaN(page))
		{
			alert("Page " + pagecounter.value + " no es un numero");
			return false;
		}
		pagecounter.value = page + 1;
		form.submit();
	},
		//
	//paginacion con type de tipo: ?type=wb&page=4
	//
	previousType: function(uniqueId)
	{
	//
		//	incrementar contador
		//
		
		var form = document.getElementById(uniqueId + '-myform');
		
		if (!form) 
		{
			alert("No existe el formulario " + uniqueId + '-myform');
			return false;
		}
		var pagecounter = form.elements['page'];
		var action = document.location.toString();
		var endChar = action.charAt(action.length - 1);
		if (endChar == "/") action += "default.aspx";
		form.action = action;
		
		if (!pagecounter) 
		{
			pagecounter = document.getElementById(uniqueId +"-pageIndex");
		}
		
		if (!pagecounter) 
		{
			alert("No existe el contador de pagina " + uniqueId + this.strPageIndex + " ni " + uniqueId +"-pageIndex");
			return false;
		}
		
		var page = parseInt(pagecounter.value, 10);
		if (isNaN(page))
		{
			alert("Page " + pagecounter.value + " no es un numero");
			return false;
		}
		pagecounter.value = page + 1;
		form.submit();
	},
	next: function(uniqueId)
	{
		//
		//	incrementar contador
		//
		var pagecounter = document.getElementById(uniqueId +this.strPageIndex);
		var form = document.getElementById(uniqueId + '-myform')
		var action = document.location.toString();
		var endChar = action.charAt(action.length - 1);
		if (endChar == "/") action += "default.aspx";
		form.action = action;

		if (!pagecounter) 
		{
			pagecounter = document.getElementById(uniqueId +"-pageIndex");
		}
		
		if (!pagecounter) 
		{
			alert("No existe el contador de pagina " + uniqueId + this.strPageIndex + " ni " + uniqueId +"-pageIndex");
			return false;
		}
		if (!form) 
		{
			alert("No existe el formulario " + uniqueId + '-myform');
			return false;
		}
		var page = parseInt(pagecounter.value, 10);
		if (isNaN(page))
		{
			alert("Page " + pagecounter.value + " no es un numero");
			return false;
		}
		pagecounter.value = page - 1;
		form.submit();
	},
		nextType: function(uniqueId)
	{
		//
		//	incrementar contador
		//
		var form = document.getElementById(uniqueId + '-myform');
		if (!form) 
		{
			alert("No existe el formulario " + uniqueId + '-myform');
			return false;
		}
		var pagecounter = form.elements['page'];
		
		var action = document.location.toString();
		var endChar = action.charAt(action.length - 1);
		if (endChar == "/") action += "default.aspx";
		form.action = action;

		if (!pagecounter) 
		{
			pagecounter = document.getElementById(uniqueId +"-pageIndex");
		}
		
		if (!pagecounter) 
		{
			alert("No existe el contador de pagina " + uniqueId + this.strPageIndex + " ni " + uniqueId +"-pageIndex");
			return false;
		}

		var page = parseInt(pagecounter.value, 10);
		if (isNaN(page))
		{
			alert("Page " + pagecounter.value + " no es un numero");
			return false;
		}
		pagecounter.value = page - 1;
		form.submit();
	},
	disableBrokersPopup : function()
	{
		this.showBrokersPopup = false;
	},
	contactBroker: function(showed)
	{
		if (showed || !this.showBrokersPopup) return;
		
		var brokerscookie = new cookieObject("BrokersPopup", 72, "/");
		if (brokerscookie.found) return;
		var brokerscookie2 = new cookieObject("BrokersPopup2", 72, "/");
		if (brokerscookie2.found) return;
		var url = this.httpPath + "/brokers/contact/popup.aspx";
		this.roadBlock = 'Advertising,' + url + ',550,755,0,BrokersPopup,76';
	},
	setRoadBlock: function(title, url, height, width)
	{
		this.roadBlock = title + ',' + url + ',' + height + ',' + width + ',16,RoadBlockShowed,24';
	},
	showRoadBlock: function()
	{
		if (this.roadBlock.length > 0)
		{
			var roadblockdata = this.roadBlock.toString().split(",");
			var title = roadblockdata[0];
			var url = roadblockdata[1];
			var height = roadblockdata[2];
			var width = roadblockdata[3];
			var hide = parseInt(roadblockdata[4]);
			var cookiename = this.trim(roadblockdata[5]);
			var expire = parseInt(roadblockdata[6], 10);
		
			var RoadBlockCookie = new cookieObject(cookiename, expire, "/");
			
			if (RoadBlockCookie.found) return;

			RoadBlockCookie.write();
			document.getElementById("bg").style.display = "none";
			GB_show(title, url, height, width);
			if (hide > 0)
			{
				window.setTimeout('GB_hide()',hide * 1000);
				window.setTimeout('document.getElementById("bg").style.display = "block"', hide * 1100);
			}
			return true;
		}
		else
		{
			return false;
		}
	},
	clearInnerHTML: function( obj )
	{
		if ( obj )
		{
			while(obj.firstChild) obj.removeChild(obj.firstChild);
		}
	},
	getParameterUrl: function( name )
	{
		var regexS = "[\?&]"+name+"=([^&#]*)";
		var regex = new RegExp ( regexS );
		var tmpURL = window.location.href;
		var results = regex.exec( tmpURL );

		if( results == null )
			return"";
		else
			return results[1];
	},
	ResetFilter: function()
	{
	var reguladors = document.getElementById("Reguladors");
	var leverage = document.getElementById("Leverage");
	var pipspread = document.getElementById("PipSpread");
	var premium = document.getElementById("Premium");
	var minAccountSize = document.getElementById("MinAccountSize");
	var RegularAccountSize = document.getElementById("RegularAccountSize");	
	
	reguladors.selectedIndex = 0;
	leverage.selectedIndex = 0;
	pipspread.selectedIndex = 0;
	premium.selectedIndex = 0;
	minAccountSize.selectedIndex = 0;
	RegularAccountSize.selectedIndex = 0;
	},
  Trace : function(message, clear)
  {
      if ($("debug"))
      {
          if (clear)
          {
              $("debug").value = message;
          }
          else
          {
              $("debug").value += message;
          }
          $("debug").value += "\n";
      }
  }, 
  setCookie : function (name, value, lifespan, access_path) 
  {
		var cookietext = name + "=" + escape(value);
		if (lifespan != null)
		{
     	var today=new Date();
     	var expiredate = new Date();
     	expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan);
     	cookietext += "; expires=" + expiredate.toGMTString();
   	}
   	if (access_path != null)
   	{
     	cookietext += "; PATH="+access_path;
   	}
  	document.cookie = cookietext;
  	return null ;
	}, 
	setDatedCookie : function (name, value, expire, access_path) 
	{
  	var cookietext = name + "=" + escape(value);
									 + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
     if (access_path != null) { 
      cookietext += "; PATH="+access_path 
     }
   document.cookie = cookietext 
   return null        
	},
	getCookie : function (Name) 
	{
  	var search = Name + "="                       
  	var CookieString = document.cookie            
  	var result = null                               
  	if (CookieString.length > 0) 
  	{
    	offset = CookieString.indexOf(search)       
    	if (offset != -1)
    	{
      	offset += search.length                   
      	end = CookieString.indexOf(";", offset)   
      	if (end == -1)                            
        	end = CookieString.length               
      	result = unescape(CookieString.substring(offset, end))         
      } 
    }
		return result                                
	},
	deleteCookie : function (Name, Path) 
	{
  	setCookie(Name,"Deleted", -1, Path);
	}
}
