function checkLoginEnter(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
	handleLoginSubmit();
	return false;
    }
    else
	return true;
}
function checkLoginEnterSingleton(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
	handleLoginSubmitSingleton();
	return false;
    }
    else
	return true;
}
function handleLoginSubmitSingleton()
{
    var i;
   
    
    
    if ((document.forms['popuplogin'].email.value.length>2) && (document.forms['popuplogin'].password.value.length>2))
    {
	
	if (document.forms['popuplogin'].autologin.checked==true)
	{
	    try
	    {
	    setCookie('c2autologin',document.forms['popuplogin'].autologin.checked,90);
	    
	    
	    setCookie('c2email',document.forms['popuplogin'].email.value,90);
	
	    var xor_key=7;
	    var to_enc=document.forms['popuplogin'].password.value;
	    
	    var enc='';
	    for (i=0;i<to_enc.length;++i)
	    {
		    enc+=String.fromCharCode(xor_key^to_enc.charCodeAt(i));
	    }
	    
	    setCookie('c2pw',enc,90);
	    }
	    catch (err)
	    {
		
	    }
	    
	}
	
	popupshow(false);
	
	//safari workaround
	//document.forms[0].submit();
	localSubmit(document.getElementById('popuplogin'));
	
    }
   
    return false;
}

function handleLoginSubmit()
{
    var i;
   
    if(navigator.userAgent.indexOf("BlackBerry")!=-1)
    {
        alert ("Welcome, BlackBerry user. We are working on improving BlackBerry support. Let us know how we are doing.");
        
        document.forms['popuplogin'].submit();
        return;
    }
    
    if ((document.forms['popuplogin'].email.value.length>2) && (document.forms['popuplogin'].password.value.length>2))
    {
	
	if (document.forms['popuplogin'].autologin.checked==true)
	{
	    
	    setCookie('c2autologin',document.forms['popuplogin'].autologin.checked,90);
	    
	    
	    setCookie('c2email',document.forms['popuplogin'].email.value,90);
	
	    var xor_key=7;
	    var to_enc=document.forms['popuplogin'].password.value;
	    
	    var enc='';
	    for (i=0;i<to_enc.length;++i)
	    {
		    enc+=String.fromCharCode(xor_key^to_enc.charCodeAt(i));
	    }
	    
	    setCookie('c2pw',enc,90);
	    
	    
	}
	
	popupshow(false);
	
	document.forms['popuplogin'].submit();
	
    }
   
    return false;
}
function loadLoginCookies()
{
    var i;
    
   
    var autologin=getCookie('c2autologin');
    if (autologin!=null && autologin!="" && autologin!==undefined)
    {
	
	try
	{
	document.forms['popuplogin'].autologin.checked=autologin;
	}
	catch (e) {}
	
	if (autologin=='true')
	{
	    
	    var email=getCookie('c2email');
	    if (email!=null && email!="")
	    {
		
		  try {document.forms['popuplogin'].email.value=email;}
		  catch (e) {}
	    }
	    
	    
	    var pw=getCookie('c2pw');
	    if (pw!=null && pw!="")
	    {
		var xor_key=7;
		var res=''
		for(i=0;i<pw.length;i++)
		{
			res+=String.fromCharCode(xor_key^pw.charCodeAt(i));
		} 
		  try {document.forms['popuplogin'].password.value=res;}
		  catch (e) {}
		
	    }
	    
	    
	}
    }
    

    
}

function loginpopupshowInside(sw,pageElName,setLoginForCC)
{

    // if this browser is IE6, bring them to the login page (no popup)
    if ( browserString == 'Explorer 6' )
    {
	if ( ! setLoginForCC ) setLoginForCC = 0;
	window.location="/cgi-perl/login.mpl?want=display&login_for_cc=" + setLoginForCC;
	return false;
    }

    if (sw==false)
    {
	document.getElementById('popupContentDiv').innerHTML=gSavePopupContent;
    }
    else
    {
    gSavePopupContent=document.getElementById('popupContentDiv').innerHTML;
    var host=top.location.host;
    var html ='<div style="padding-left:110px;padding-top:30px;"><h3>Log in to your account</h3> <div style="position:relative; top:-30px;right:6px;"><a href="#" class="btnClose" onclick="loginpopupshowInside(false,\''+pageElName+'\');">Close</a></div></div><br />\
<div style="padding:20px 110px 20px 110px;"><form id="popuplogin" name="popuplogin" method="POST" action="https://'+host+'/cgi-perl/login.mpl">\
<div style="margin:20px 0 20px 140px;"><table cellspacing="4"><tr><td style="color:#ffffff;font-size:120%">Email&nbsp; </td><td><input type="text" name="email" size="30" tabindex="1" onKeyPress="return checkLoginEnter(this,event);"/></td></tr>\
<tr><td style="color:#ffffff;font-size:120%">Password&nbsp; </td><td><input type="password" name="password" size="30" tabindex="2" onKeyPress="return checkLoginEnter(this,event);" /></td></tr>\
<tr><td colspan="2"><div style="margin:20px 0 30px 120px;">\
<a href="#" onclick="handleLoginSubmit();" class="btn"><span><span><span>Login</span></span></span></a>\
</div></td></tr><tr><td colspan="2" align="center">\
<div style="margin-top:10px;color:#fff;font-size:12px;"><input type="checkbox" name="autologin" value="1" tabindex="4"> Keep password stored on this computer?</div></td></tr>\
</table></div><div style="display:inline;float:left;color:#fff;font-size:12px;">\
<a href="https://'+host+'/cgi-perl/signup200.mpl?sec=1">\
Sign up (it\'s free)</a></div><div style="display:inline;float:right;font-size:12px;"><a href="/static/forgot.htm?nologinreturn=1">Forget your password?</a></div>\
<div style="clear:both;"></div>';

if ( setLoginForCC )
{
    html += '<input type="hidden" name="setLoginForCC" value="1" />'; 
}

html += '</form></div>';
    
document.getElementById('popupContentDiv').innerHTML = html;

    loadLoginCookies();
    }
    
    loginpopupshow(sw,pageElName);
}

function loginpopupshow(sw,pageElName)
{
	popupshow(sw,pageElName);
	if (sw)
	{
		document.forms['popuplogin'].email.focus();
	}
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    var c_start;
    var c_end;
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
	{ 
	c_start=c_start + c_name.length+1; 
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	} 
      }
    return "";
}


function rotateSystem(w,h,splashvar)
{
    
    if (gRotate.length<1)
    {
        return;
    }
    if (splashvar<1)
    {
        splashvar='';
    }
    gBufferedSID=gRotate.shift();
    gBufferedName=gRotate.shift();
    gBufferedTrades=gRotate.shift();
    gBufferedAutotrade=gRotate.shift();
    
    var R=Math.random();
    gBufferedImageName='/cgi-perl/dashcharts.mpl?sid='+gBufferedSID+'&width='+w+'&height='+h+'&switch=splash'+splashvar+'&R='+R;
    gBufferedImage.src='/cgi-perl/dashcharts.mpl?sid='+gBufferedSID+'&width='+w+'&height='+h+'&switch=splash'+splashvar+'&R='+R;
    
    setTimeout('fadeOutCurrent()',1000);
    setTimeout('fadeInNew()',1700);
    setTimeout('rotateSystem('+w+','+h+','+splashvar+');',8000);
    
}

function fadeOutCurrent()
{
    new Effect.Opacity('picholder',{duration:.7, from:1, to:.2});
}
function fadeInNew()
{
    
    new Effect.Opacity('picholder',{duration:.7, from:.2, to:1});
    document.getElementById('picholder').innerHTML='<a href="/cgi-perl/system'+gBufferedSID +'"><img id="rotateChartPic" name="rotateChartPic" src="'+  gBufferedImageName  +'"></a>';
    document.getElementById('sysname').innerHTML=gBufferedName;
    document.getElementById('tradeswhat').innerHTML=gBufferedTrades;
    document.getElementById('autotrade').innerHTML=gBufferedAutotrade;
}

function m_over(e,num)
{
    if (num>0)
    {
        e.className='container3ON';
        
        var h;
        if (num==1)
        {
            h='<a href="/static/info/intro.html">... because you can pick from thousands of systems that have already been developed and audited.<span class="arrow">&nbsp;&raquo;</span></a>';
        }
        else if (num==2)
        {
            h='<a href="/static/aboutAutoTrade.htm">... because we don\'t force you to work with one particular broker. Use the one you already have.<span class="arrow">&nbsp;&raquo;</span></a>';
        }
        else if (num==3)
        {
            h='<a href="/static/aboutAutoTrade.htm">... because we\'ve spent six years developing automated trading for regular investors.<span class="arrow">&nbsp;&raquo;</span></a>';
        }
        document.getElementById('dtext').innerHTML=h;
        
    }
    else
    {
        e.className='container3';
    }
}



function splashVideo(anchordiv,url,offsetx,offsety,videowidth,videoheight)
{
    
	var vidx;
	var vidy;
	
	if (undefined == videowidth)
	{
	   videowidth=590;
	}
	if (undefined == videoheight)
	{
	   videoheight=425;
	}

	win = new Window({className: "alphacube", title: "Introduction to Collective2", 
                     width:videowidth, height:videoheight, destroyOnClose: true, recenterAuto:false, minimizable:false, maximizable:false, resizable:false, url:url})
		
	if (anchordiv.length==0)
	{
	   vidx=0;
	   vidy=0;
	}
	else
	{
	   var anchorel=document.getElementById(anchordiv);
	   vidx=findPosX(anchorel);
	   vidy=findPosY(anchorel);
	}
	vidy=vidy+offsety;
    vidx=vidx+offsetx;
    
    win.setLocation(vidy, vidx);
    
    win.show();
    
    return(win);
	
}

function choiceOn(e,on)
{
    if (!on)
    {
        document.getElementById(e).className='textoff';
    }
    else
    {
        document.getElementById(e).className='texton';
        var o;
        if (e=='spanVideo')
        {
            o='spanFinder';
        }
        else
        {
            o='spanVideo';
        }
        document.getElementById(o).className='textoff';
    }
}
