N = (document.all) ? 0 : 1;

function $(id){
	return document.getElementById(id);
}

function DivDom() {
	return document.getElementsByTagName('div');
}

function TableDom() {
	return document.getElementsByTagName('table');
}

//display
function show(o){
	if(o.style)
		o.style.display="";
	else
		$(o).style.display="";
}
function hide(o){
	if(o.style)
		o.style.display="none";
	else
		$(o).style.display="none";
}
//position
function setX(o, _x){
	if(o.style)
		o.style.left=_x+"px";
	else
		$(o).style.left=_x+"px";
}
function setY(o, _y){
	if(o.style)
		o.style.top=_y+"px";
	else
		$(o).style.top=_y+"px";
}
function setXY(o, _x, _y){
	setX(o, _x);
	setY(o, _y);
}
//innerHTML
function getContent(f, t){
	if(! t.innerHTML)
		t=$(t);
	if(! f.innerHTML)
		f=$(f);
	f.innerHTML=t.innerHTML;
}

function setContent(id, c){
	el=$(id);
	el.innerHTML=c;
}


var timerID=null;
var running=false;
var speed=30;
var Go=true;
var tables=TableDom();

var delay=1;
var Ooo=5;
function resetOoo(){
	Ooo=5;
	delay=1;
}


function moveAddX(xq){
	if(running && speed > 0){
		for (var i = 0; i < tables.length; i++) { 
			if(tables[i].getAttribute("id")){					
				thisID=tables[i].getAttribute("id");
				if(thisID.indexOf('moveme_') != -1 ){
					addX(thisID, xq); 
				}
			}			
		}//for
		timerID = setTimeout("moveAddX("+xq+");", speed);	
		delay++;
		if(Ooo > 0 && delay%50 == 0)
			Ooo--;			
	}
	
}		
function moveAddY(yq){
	if(running && speed > 0){		
		for (var i = 0; i < tables.length; i++) { 
			if(tables[i].getAttribute("id")){					
				thisID=tables[i].getAttribute("id");
				if(thisID.indexOf('moveme_') != -1 ){
					addY(thisID, yq); 
				}
			}			
		}//for
		timerID = setTimeout("moveAddY("+yq+");", speed);	
		//delay++;
		//if(Ooo > 0 && delay%50 == 0)
			//Ooo--;			
	}
	
}

var first=false;
var last=false;
var imageElements;

function getLastFirst(){
		imageElements=new Array();
		first=false;
		for (var i = 0; i < tables.length; i++) { 
			if(tables[i].getAttribute("id")){					
				thisID=tables[i].getAttribute("id");
				if(thisID.indexOf('moveme_') != -1 ){
					imageElements.push(thisID);
					if (!first){
						first=$(thisID);
					} else {
						if( parseInt(first.style.left) > parseInt($(thisID).style.left) )
							first=$(thisID);
					}
					
					if(last){
						if( parseInt(last.style.left) < parseInt($(thisID).style.left) )
							last=$(thisID);
					}else{
						last=$(thisID);
					}
				}
			}	
		}//for				
		return imageElements.length-1;
}
function getLastFirstY(){
		imageElements=new Array();
		first=false;
		tables=TableDom();
		for (var i = 0; i < tables.length; i++) { 
			if(tables[i].getAttribute("id")){					
				thisID=tables[i].getAttribute("id");
				if(thisID.indexOf('moveme_') != -1 ){
					imageElements.push(thisID);
					if (!first){
						first=$(thisID);
					} else {
						if( parseInt(first.style.top) > parseInt($(thisID).style.top) )
							first=$(thisID);
					}
					
					if(last){
						if( parseInt(last.style.top) < parseInt($(thisID).style.top) )
							last=$(thisID);
					}else{
						last=$(thisID);
					}
				}
			}	
			
		}//for				
		return imageElements.length-1;
}

function reverseElements(xq){
	//going right
	var n;
	var Moveme;
	var MAX=parseInt($('cont').style.width);
	var numberEl=getLastFirst();
	
	if(xq>0){
		while( parseInt($(imageElements[numberEl]).style.left) > MAX){
			$(imageElements[numberEl]).style.left=( parseInt($(imageElements[0]).style.left)
																												 -parseInt($(imageElements[numberEl]).style.width)
																												 )+'px';
			Moveme=imageElements.pop();
			imageElements.unshift(Moveme);
		}//while
	//going left
	}else{
		while( parseInt($(imageElements[0]).style.left)+parseInt($(imageElements[0]).style.width) < 0){			
			$(imageElements[0]).style.left=(parseInt($(imageElements[numberEl]).style.left)
																		  +parseInt($(imageElements[numberEl]).style.width)
																			)+'px';
			Moveme=imageElements.shift();
			imageElements.push(Moveme);			
		}
	}//else
}// function

function reverseElementsY(yq){
	var n;
	var Moveme;
	var MAX=parseInt($('cont').style.height);
	var numberEl=getLastFirstY();
	//going down
	if(yq>0){
		while( parseInt($(imageElements[numberEl]).style.top) > MAX){
			$(imageElements[numberEl]).style.top=( parseInt($(imageElements[0]).style.top)
																												 -parseInt($(imageElements[numberEl]).style.height)
																												 )+'px';
			Moveme=imageElements.pop();
			imageElements.unshift(Moveme);
		}//while
	//going up
	}else{
		while( parseInt($(imageElements[0]).style.top)+parseInt($(imageElements[0]).style.height) < 0){			
			$(imageElements[0]).style.top=(parseInt($(imageElements[numberEl]).style.top)
																		  +parseInt($(imageElements[numberEl]).style.height)
																			)+'px';
			Moveme=imageElements.shift();
			imageElements.push(Moveme);			
		}
	}//else
}// function

function addX(id, xq){

	if(Go){
		reverseElements(xq);
		if(xq > 0){
			xq += Ooo;
		}else{
			xq -= Ooo;
		}

		o=$(id);
		oldX=parseInt(o.style.left);
		//+X
		if(oldX >= parseInt($('cont').style.width) && xq>0){
			if( (parseInt(first.style.left)+parseInt(first.style.width))<oldX ){
				setX(o, (parseInt(first.style.left)-parseInt(o.style.width) ) );
			}else{
				setX(o, (-1)*parseInt(o.style.width) );
			}
		//-X
		}else if(oldX <=(-1)*parseInt(o.style.width) && xq<0){
			if( (parseInt(last.style.left)+parseInt(last.style.width))>oldX ){
				setX(o, (parseInt(last.style.left)+parseInt(last.style.width)) );
			}else{
				setX(o, parseInt($('cont').style.width) );
			}
		//inside cont
		}else{
			setX(o, (xq+oldX));
		}
	}
}


function addY(id, yq){

	if(Go){
		reverseElementsY(yq);
		//if(yq > 0){
			//yq += Ooo;
		//}else{
			//yq -= Ooo;
		//}

		o=$(id);
		oldY=parseInt(o.style.top);
		//+Y
		if(oldY >= parseInt($('cont').style.height) && yq>0){
			if( (parseInt(first.style.left)+parseInt(first.style.height))<oldY ){
				setY(o, (parseInt(first.style.left)-parseInt(o.style.height) ) );
			}else{
				setY(o, (-1)*parseInt(o.style.height) );
			}
		//-Y
		}else if(oldY<=(-1)*parseInt(o.style.height) && yq<0){
			if( (parseInt(last.style.top)+parseInt(last.style.height))>parseInt($('cont').style.height) ){
				setY(o, (parseInt(last.style.top)+parseInt(last.style.height)) );
			}else{
				setY(o, parseInt($('cont').style.height) );
			}
		//inside cont
		}else{
			setY(o, (yq+oldY));
		}
	}
}


function Start(){
	if(running){		
		running=false;		
		clearTimeout(timerID);
	}else{
		resetOoo();
		running=true;
	}
}

function Stop(){
	resetOoo();
	running=false;
	clearInterval(timerID);
}

function moreSpeed(){
	if(speed > 5){
		if (speed < 40 && speed > 10)
			speed += -10;
		else
			speed += -5;
	}
}

function lessSpeed(){
	if(speed <= 180){
		if (speed > 40)
			speed += 10;
		else
			speed += 5;
	}
}

function pause(){
	Go=false;
}

function play(){
	Go=true;
}
//////////////////////////////////////////////////
//////////////      Comet      ///////////////////
//////////////////////////////////////////////////
function moveComet(e){
	// set position of comet
  var N;
	N = (document.all) ? 0 : 1;
	if (N) {
		clientX=e.clientX;
		clientY=e.clientY;
	} else {
		clientX=event.clientX;
		clientY=event.clientY;
	}
		
	$('comet').style.left=clientX+"px";
	$('comet').style.top=clientY+"px";
	// turn visible comet
	$('comet').style.display="";
	// move comet
	// get where to go	
	XToGo=parseInt($('cometToGo').style.left);
	YToGo=parseInt($('cometToGo').style.top);
	// divide H diference per 10
	// divide W diference per 10
	Xquote=(XToGo-clientX)/Q;
	Yquote=(YToGo-clientY)/Q;
	movingComet=0;
	goComet();
}
var clientX;
var clientY;
var Xquote;
var Yquote;
var XToGo;
var YToGo;
var movingComet=0
var movingID=null;
var Q=100

function goComet(){
		$('comet').style.left=Math.floor((parseInt($('comet').style.left)+Xquote))+"px";
		$('comet').style.top=Math.ceil((parseInt($('comet').style.top)+Yquote))+"px";
		movingComet++;
		if(movingComet < Q)
			movingID = setInterval("goComet();", 10);
		else{
			clearTimeout(movingID);
			$('comet').style.display="none";
		}
	// after 1 sec set invisible comet
}
//////////////////////////////////////////////////

// ajax functions
var xmlHttp
var htmlId;
var x
var y

function GetXmlHttpObject(handler){
    var objXmlHttp=null
    if (navigator.userAgent.indexOf("Opera")>=0){
        alert("This example doesn't work in Opera")
        return
    }
    if (navigator.userAgent.indexOf("MSIE")>=0){
        var strName="Msxml2.XMLHTTP"
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
            strName="Microsoft.XMLHTTP"
        }
        try{
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler
            return objXmlHttp
        }
        catch(e){
            alert("Error. Scripting for ActiveX might be disabled")
            return
        }
    }
    if (navigator.userAgent.indexOf("Mozilla")>=0){
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler
        return objXmlHttp
    }
}

// DHTML

function stateChanged() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			if($(htmlId).innerHTML)
        $(htmlId).innerHTML=xmlHttp.responseText
		}
}

//////////////////////////////////////////////////////
function ajax_show(url, id){
        htmlId=id;
        xmlHttp=GetXmlHttpObject( stateChanged )
        xmlHttp.open("GET", url , true)
        xmlHttp.send(null)
}

function ajax_show_event(url, id, e){
        htmlId=id;
        document.getElementById(htmlId).style.left=e.clientX;
        document.getElementById(htmlId).style.top=e.clientY;
        xmlHttp=GetXmlHttpObject( stateChanged )
        xmlHttp.open("GET", url , true)
        xmlHttp.send(null)
}

/* class ajax for submit/get/post of forms */
function collect(a,f){
    var n=[];for(var i=0;i<a.length;i++){
        var v=f(a[i]);if(v!=null)n.push(v)
    }
    return n
    };

ajax={};

ajax.x=function(){
    try{
        return new ActiveXObject('Msxml2.XMLHTTP')
    }
    catch(e){
        try{
            return new ActiveXObject('Microsoft.XMLHTTP')
        }
        catch(e){
            return new XMLHttpRequest()
        }
    }
};

ajax.serialize=function(f){
    var g=function(n){
        return f.getElementsByTagName(n)
    };
    var nv=function(e){
        if(e.name)
            return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);
        else return ''
    };
    var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});
    var s=collect(g('select'),nv);
    var t=collect(g('textarea'),nv);
    return i.concat(s).concat(t).join('&');
};

ajax.send=function(u,f,m,a){
    var x=ajax.x();
    x.open(m,u,true);
    x.onreadystatechange=function(){
        if(x.readyState==4)
            f(x.responseText)
    };
    if(m=='POST')
        x.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    x.send(a)
};

ajax.get=function(url,func){
	ajax.send(url,func,'GET')
};
ajax.gets=function(url){
	var x=ajax.x();
	x.open('GET',url,false);
	x.send(null);
	return x.responseText
};
ajax.post=function(url,func,args){
	ajax.send(url,func,'POST',args)
};
ajax.update=function(url,elm){
	var e=$(elm);
	var f=function(r){
		e.innerHTML=r
	};
		ajax.get(url,f)
};
ajax.submit=function(url,elm,frm){
	var e=$(elm); 
	var f=function(r){
		e.innerHTML=r;
	}
	ajax.post(url,f,ajax.serialize(frm))
}; 