// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)


addEvent = function(o, e, f){
    var a, r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [o[e]] : []);
    r[r.length] = f, o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        //for(f in r) 
        r.each(function(f){
//            a = f, a.call ? a.call(o, e) : (o._ = a, o._(e), o._ = null);
        });
        e = null;
    }
};

removeEvent = function(o, e, f){
    for(var i in e = o["_on" + e])
        if(e[i] == f)
            return delete e[i];
    return false;
};

// Set-up to use getMouseXY function onMouseMove
addEvent(document,'mousemove',getMouseXY);

// Temporary variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true
}

//------------------------------------------------

function ShowAlt(text){
	document.getElementById('about_text').innerHTML = text;
	document.getElementById('about_text').style.top = mouseY;
	document.getElementById('about_text').style.left = mouseX;
	document.getElementById('about_text').style.visibility = 'visible';
}

function HiddenAlt(){
	document.getElementById('about_text').style.visibility = 'hidden';
}

//------------------------------------------------

function setRefreshTime(){
	min = min - 1;
	if ((min<0)){
   		document.getElementById('refresh_time').innerHTML=("0");
	}
    else {
     	document.getElementById('refresh_time').innerHTML=min;
     	setTimeout("setRefreshTime()",60*1000) 
    }
}
	
function CheckTab(el) {
  	if ((document.all) && (9==event.keyCode) && (event.ctrlKey)) {
    	el.selection=document.selection.createRange(); 
    	setTimeout("ProcessTab('" + el.id + "')",0)
    }
}

function ProcessTab(id) {
  	document.all[id].selection.text=String.fromCharCode(9);
  	document.all[id].focus();
}

//------------------------------------------------

MaskInput = function( f, m ){ //v1.0
    for( var i in ( !/^(.)\^(.*)$/.test( m ) && f.setAttribute( 'maxlength', m.length ), { keypress: 0, keyup: 1 } ) )
        addEvent( f, i, function( e ){
        	if (e.altKey) return false;
    		if (e.ctrlKey) return false;
            var patterns = { "1": /[A-Z]/i, "2": /[0-9]/, "4": /[?-?]/i, "8": /./ }, rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8 };
            function validChar( c, rule ){
                var r = rules[rule] || 0;
                
                for( var i = 1; i <= r; i<<=1 )
                    if( r & i && patterns[i].test( c ) )
                        break;
                return i <= r || c == rule;
            }
            var k, mC, r, c = String.fromCharCode( k = e.key ), l = f.value.length;
            ( !k || k == 8 ? 1 : ( r = /^(.)\^(.*)$/.exec( m ) ) && ( r[0] = r[2].indexOf( c ) + 1 ) + 1 ? r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : validChar( c, r[1] ) || r[0] : ( l = ( f.value += m.substr( l, ( r = /[A|9|C|\*]/i.exec( m.substr( l ) ) ) ? r.index : l ) ).length ) < m.length && validChar( c, m.charAt( l ) ) ) || e.preventDefault();
         } );
};

function htmlSpecialCharsReversed(str)
{
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&quot;/gi, '"');
	str = str.replace(/&gt;/gi, '>');
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/>/, '>');
	str = str.replace(/</, '<');
	str = str.replace(/'/, '\'');
	str = str.replace(/"/, '\"');
	str = str.replace(/&/, '&');
	
	return str;
}

function ReplaceImageUrl(url, str){
	str = htmlSpecialCharsReversed(str);
	var re = /<img[^>]*src=<sport-img>([^<]*)<\/sport-img>/i;
	
	while(str.match(re)){
		filename = RegExp.$1;
		
		var re2 = new RegExp('src=<sport-img>' + filename + '<\/sport-img>', "i");

		str = str.replace(re2, 'src="' + url + 'load_image.php?img_id=' + filename + '"');
	}
	return str;
}

function UnreplaceImageUrl(url, str){
		var re = /<img[^>]*src="([^"]*)\/load_image.php\?img_id=([^"]+)"/i
		while(str.match(re))
		{
			str1 = RegExp.$1;
			filename = RegExp.$2;
	
			var re2 = 'src="' + str1 + '/load_image.php?img_id=' + filename + '"';
			str = str.replace(re2, 'src=<sport-img>' + filename + '<\/sport-img>');
		}
		return str;
	}
	
function OpenEditor(field){
window.open(
	'editor.php?field=' + field, 
	'image_window', 
	'status=yes, toolbar=no, location=no, menubar=no, scrollbars=no, resizable=no, width=670, height=520, top=100, left=100');
}

function findPosX(obj)
{
    if (obj)
    {
        var curleft = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curleft += obj.offsetLeft
                obj = obj.offsetParent;
            }
        }
        else if (obj.x)
            curleft += obj.x;
        return curleft;
    }
    else
    {
        return -1;
    }
}

function findPosY(obj)
{
    if (obj)
    {
        var curtop = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curtop += obj.offsetTop
                obj = obj.offsetParent;
            }
        }
        else if (obj.y)
            curtop += obj.y;
        return curtop;
    }
    else
    {
        return -1;
    }
}

function moveMenu(){
	document.getElementById('menu1').style.top = findPosY(document.getElementById('Nmenu1')) + 15 + 'px'; 
	document.getElementById('menu1').style.left = findPosX(document.getElementById('Nmenu1')) - 20 + 'px';
}