var maxtickers = 5;
var tnames = Array( maxtickers );
var tspeeds = Array( maxtickers );
var tcounts = Array( maxtickers );
var tsteps =  Array( maxtickers );
var twidths =  Array( maxtickers );

String.prototype.trim = function()
{
	return( this.replace(/^\s+/, '').replace(/\s+$/, '') );
}

function konfirm( felirat, cel )
{
	if ( confirm( felirat ) )
		location.href = cel;
}

function tickerstart( tname, tcount, tspeed, tstep, twidth )
{
	var ret, i;

	for ( i = 0; i < maxtickers; i++ )
		if ( tcounts[ i ] == undefined )
		{
			tnames[ i ] = tname;
			tspeeds[ i ] = tspeed;
			tcounts[ i ] = tcount;
			if ( tcount == 1 )
			{
				var p1 = Pos( tname+"0" );
				var p2 = Pos( tname+"end" );
				tsteps[ i ] = p2.Left - p1.Left;
			}
			else
				tsteps[ i ] = tstep;
			twidths[ i ] = twidth;
			ret = i;
			break;
		}

	t = document.getElementById( tname+"0" );
	if ( t == null )
		return;

	if ( tcount == 1 )
	{
		t.style.left = ""+twidth+"px";
	}
	else
		for ( i = 0; ; i++ )
		{
			t = document.getElementById( tname+i );
			if ( t == null )
				break;
			t.style.left = ""+(i*tstep)+"px";
		}
	setTimeout( "tickerdraw("+ret+")",1 );
	return ret;
}

function Pos(thisitem)
{
	if(typeof(thisitem) != 'object') {thisitem = document.getElementById(thisitem);}
	var ww = thisitem.offsetWidth, hh = thisitem.offsetHeight;
	for (var xx = 0,yy = 0; thisitem != null; xx += thisitem.offsetLeft,yy += thisitem.offsetTop,thisitem = thisitem.offsetParent);
	return {Left:xx, Top:yy, Right:xx + ww, Bottom:yy + hh}
}

	function tickerdraw( tid )
	{
		if ( tspeeds[ tid ] == 0 )
		{
			setTimeout( "tickerdraw("+tid+")", 40 );
			return;
		}

		t = document.getElementById( tnames[tid]+"0" );
		if ( t == null )
			return;

// 		HACK : (igy a szelein nem tud tulmenni)
		if ( parseInt(t.style.left) >= 0 && tspeeds[tid] < 0 )
		{
 			for ( i = 0; ; i++ )
 			{
				t = document.getElementById( tnames[tid]+i );
				if ( t == null )
					break;
				t.style.left = ""+(i*tsteps[tid])+"px";
			}
			setTimeout( "tickerdraw("+tid+")", 40 );
			return;
		}
		if ( parseInt(t.style.left) <= twidths[tid]-tcounts[tid]*tsteps[tid] && tspeeds[tid] > 0 )
		{
			if ( tcounts[tid]*tsteps[tid]>twidths[tid] )
			{
	 			for ( i = 0; ; i++ )
	 			{
					t = document.getElementById( tnames[tid]+i );
					if ( t == null )
						break;
					t.style.left = ""+((twidths[tid]-tcounts[tid]*tsteps[tid])+i*tsteps[tid])+"px";
				}
			}
			setTimeout( "tickerdraw("+tid+")", 40 );
			return;
		}

// 		if ( tcounts[tid] >= 4 && parseInt(t.style.left) <= -tsteps[tid]*(tcounts[tid]-4) )
// 		{
// 			for ( i = 0; ; i++ )
// 			{
// 				t = document.getElementById( tnames[tid]+i );
// 				if ( t == null )
// 					break;
// 				t.style.left = ""+(i*tsteps[tid])+"px";
// 			}
// 		}
// 		if ( tcounts[tid] == 1 && parseInt(t.style.left) <= -tsteps[tid] )
// 		{
// 			t = document.getElementById( tnames[tid]+"0" );
// 			if ( t == null )
// 				return;
// 			t.style.left = ""+twidths[tid]+"px";
// 		}

		for ( i = 0; ; i++ )
		{
			t = document.getElementById( tnames[tid]+i );
			if ( t == null )
				break;
			t.style.left = ""+(parseInt(t.style.left)-tspeeds[tid])+"px";
		}
		setTimeout( "tickerdraw("+tid+")", 40 );
	}

function clientsize( )
{
	if ( window.innerHeight != undefined )
		h = window.innerHeight - 4;
	else
		if ( document.documentElement.clientHeight != 0 )
			h = document.documentElement.clientHeight;
		else
			h = document.body.clientHeight - 4;

	if ( window.innerWidth != undefined )
		w = window.innerWidth;
	else
		if ( document.documentElement.clientWidth != 0 )
			w = document.documentElement.clientWidth;
		else
			w = document.body.clientWidth;

	return { h:h, w:w };
}

function tickerspeed( tid, s )
{
	tspeeds[ tid ] = s;
}

function showbigimg( newimg, description )
{
	cs = clientsize();
	big = document.getElementById( "bigimg" );
	big.style.left = (cs.w-475)/2;
	big.style.top = (cs.h-475)/2;
	big.style.display = 'block';
	big.style.background = "#EFEFEF center center no-repeat url( 'img/"+newimg+"' )";
	bigd = document.getElementById( "bigimgdesc" );
	bigd.style.display = 'block';
	bigd.innerHTML = description;
}

function hidebigimg()
{
	big = document.getElementById( "bigimg" );
	big.style.display = 'none';
}

function swapdetailsimg( newimg )
{
	d = document.getElementById( "detailsimg" );
	d.src="cars/"+newimg;
	d = document.getElementById( "detailsimglnk" );
	d.href="./cars/full/"+newimg;
}

function showbidhistory( )
{
	d = document.getElementById( "bidhist" );
	d.style.display="block";
}

function ow(oldal, w, h)
{
    OpenWin = this.open(oldal, "CtrlWindow", "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no");
}

var currscreen = "", currscreennum = 0;
function changescreen( newscreen, screennum )
{
	if ( currscreen != "" )
	{
		s = document.getElementById( ""+currscreen+currscreennum );
		s.style.display = "none";

	   	s = document.getElementById( "tab"+currscreennum+"l" );
	   	//alert("tab"+currscreennum+"l");
		s.style.background = "url(g/tab1i.png)";
	   	s = document.getElementById( "tab"+currscreennum+"r" );
		s.style.background = "url(g/tab2i.png)";
	   	s = document.getElementById( "tab"+currscreennum+"m" );
		s.style.background = "url(g/tabbgi.png)";
	}

	s = document.getElementById( ""+newscreen+screennum );
	if ( s == null )
		return;
	s.style.display = "block";

	currscreen = newscreen;
	currscreennum = screennum;

   	s = document.getElementById( "tab"+screennum+"l" );
	s.style.background = "url(g/tab1.png)";
   	s = document.getElementById( "tab"+screennum+"r" );
	s.style.background = "url(g/tab2.png)";
   	s = document.getElementById( "tab"+screennum+"m" );
	s.style.background = "url(g/tabbg.png)";
}

function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function CheckInvChars( f )
{
	var	is = String.fromCharCode(255);

	if( f.indexOf( is ) != -1 || f.indexOf( "&" ) != -1 || f.indexOf( "\%" ) != -1 || f.indexOf( "!" ) != -1 || f.indexOf( "*" ) != -1 || f.indexOf( "/" ) != -1 || f.indexOf( "?" ) != -1 || f.indexOf( ":" ) != -1 || f.indexOf( "\\" ) != -1 || f.indexOf( "\"" ) != -1 || f.indexOf( "\'" ) != -1 || f.indexOf( "," ) != -1 )
		return( true );
	return( false );
}

function checkmailfield( em )
{
	if( em )
		em.value = em.value.toLowerCase();
	e = em.value.trim();
	if( e.length < 4 || e.length > 64 || e.indexOf( "@" ) == -1 || e.indexOf( "@" ) != e.lastIndexOf( "@" ) || e.indexOf( "@." ) != -1 || e.indexOf( ".@" ) != -1 || e.indexOf( "." ) == -1 || CheckInvChars( e ) == true )
	{
		alert( "A megadott email-cím érvénytelen!\n(" + em.id + "=" + em.value + ")" );
		em.focus();
		return( false );
	}
	return( true );
}

function checkregfields( )
{
	var	n = document.getElementById( 'reg_szervezetneve' );
	n.value = n.value.trim();

	if ( !checkmailfield( document.getElementById( 'reg_email' ) ) )
		return false;

	if( n.value.length < 2 || n.value.length > 64 )
	{
		alert( "A megadott név érvénytelen!" );
		n.focus();
		return( false );
	}
	return( true );
}

function forgotpassword( )
{
	if ( !checkmailfield( document.getElementById( 'login_email' ) ) )
	{
		alert( "Elfelejett jelszó esetén adja meg a regisztrációkor használt e-mail címét, majd újra kattintson ide." );
	}
	else
	{
		$("#loginform").append( "<input type=hidden name=forgot value=password>" );
		$("#loginform").submit();	
	}
}

