js_syntax=0;
compatible=0;
cookies=0;
core=new Object();
debugging=0;

function returnOne() { return(1); }

if(window.onerror) {
    core.olderror=window.onerror
    window.onerror=returnOne;
}


/* js implementations with archaic syntax will be caught
 by the following lines.  
 I think anonymous functions means javascript 1.5.  */
core.f=function () {
    var x;
    x=1==3;
    try {} catch(e) {}
    x=[1];
    x=true;
    var x=/d/;
}
/* we should have reasonable syntax, so it's safe to proceed. */

if(core.olderror) window.onerror=core.olderror; 
compatible=true;
REQcompatible=false;
cookies=true;
js_syntax=true;

if(!document.getElementById) compatible=false;

if(!"".match) compatible=false;
if(compatible==false&&debugging) alert('Not Compatible Super Early');

core.global=this;

if(!this.XMLHttpRequest) {
    core.XMLHttpRequest=function() {
        try {
            var obj=new ActiveXObject("MSXML2.XMLHTTP.3.0");
            return obj; 
        } catch(e) {}
        return false;
    }
}
else {
    core.XMLHttpRequest=function() {
        return new XMLHttpRequest();
    }
}
try {
    if(core.XMLHttpRequest()) REQcompatible=true;
} catch(e) { }    
    
if(REQcompatible==false&&debugging) alert('Requests fail');

core.unFrame=function () {
    if(window!==parent) parent.location.href=window.location.href;
}

/* name, value, expires, path, domain, secure */
core.setCookie=function() {
    if(!cookies) return;
    if(arguments.length>2) {
        var exp=arguments[2];
        if(exp.toGMTString) exp=exp.toGMTString(); 
        }
    var curCookie = arguments[0] + "=" + escape(arguments[1]) +
            ((arguments.length>2&&arguments[2]) ? "; expires=" + exp : "") +
            ((arguments.length>3&&arguments[3]) ? "; path=" + arguments[3] : "") +
            ((arguments.length>4&&arguments[4]) ? "; domain=" + arguments[4] : "") +
            ((arguments.length>5&&arguments[5]) ? "; secure" : "");
    document.cookie = curCookie;
}

core.delCookie=function(name) {
    core.setCookie(name,'','Thu, 31-Dec-1998 00:00:00 GMT');
}

core.getCookie=function(name) {
    if(!cookies) return null;
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/* construct an associated array (Object) out of (key,value) pairs.  This is necessitated by the fact
that JS can handle associative arrays, but has no literal syntax for constructing them, except for those
using valid property names as keys */
function assocArray(pairs) {
    x=new Object();
    for(var i in pairs) {
        var p=pairs[i];
        if(!p) continue; /*shouldn't be necessary */
        x[p[0]]=p[1];
    }
    return x;
}

core.popup=function(mylink,name,dparam) {
    var param=false;
    if (!window.open) return true;
    var href;
    if (typeof(mylink) == 'string') href=mylink;
    else href=mylink.href;
    for(var i in dparam) {
        if(param==false) param="";
        else param+=",";
        param+=i+"="+dparam[i];
    }
    win=window.open(href, name, param);
    if(win.focus) win.focus();
    return false;
}

core.getNamedControls=function(form,name) {
    var r=new Array();
    if(typeof(form)=='string') form=document.getElementById(form);
    try {
        /* MS.  Also gets ID=name */
        var cs=form.elements(name);
    }
    catch(e) {
        /* W3C. Gets name from every form */
        var cs=document.getElementsByName(name);
    }
    if(!cs) return null;
    if(cs.form) { /* MS and non-array */
        if(cs.name==name&&cs.form==form) r.push(cs); 
    }
    else {
        for(var i in cs) {
            var c=cs[i];
            if(c.name==name&&c.form==form) r.push(c);
        }
    }
    return r;
}

core.contains=function(outer,inner) {
    if(!outer||!inner) return(false);
    if(outer.compareDocumentPosition) {
        return !!(outer.compareDocumentPosition(inner) & 16)
    }
    while(1) { 
        inner=inner.parentNode;
        if(!inner) return(false);
        if(outer===inner) return(true);
    }
}

/* get a closure, without extraneous scope data 
function getClosure(f,args) {
    return function() {
        f.apply(this,args.concat(arguments));    
    }
}*/
function evalFunc(code) {
    eval(code);
}

function DOMElementData() {
    this.mycounter=0;
    this.index=new Array();
    this.mapping=new Object();
    this.sweepindex=0;
    if(document.uniqueID) this.unique=true;
    /* uniqueID is an MS extension.  The uniqueID is guaranteed
    not to change, so I don't have to make the expensive check for 
    a renamed id if the browser can do this. */
    else this.unique=false;
}

DOMElementData.prototype.delindex=function(i) {
    var top=this.index.pop();
    if(i<this.index.length) this.index[i]=top;
}

DOMElementData.prototype.del=function(i) {
    var id=this.getID(this.index[i].element);
    this.delindex(i);
    delete this.mapping[id];
}

DOMElementData.prototype.getID=function(e) {
    if(!e) return false;
    if(this.unique) {
        if(e.uniqueID) return e.uniqueID;
        else if(e===window) return '__win';
        else return false;
    }
    else return e.id;
}

DOMElementData.prototype.checkExists=function(e) {
    if(e===window||e===document) return true;
    return core.contains(document,e);
}

DOMElementData.prototype.fixID=function(obj,index) {
    while(1) {
        var id=this.getID(obj.element);
        var obj2=this.mapping[id];
        if(obj===obj2) {
            if(index<this.index.length) this.delindex(index);
            return;
        }
        this.index[index]=obj;
        if(!obj) alert('ASSERTION FAILED: obj not passed');
        this.mapping[id]=obj;
        if(!obj2||!this.checkExists(obj2.element)) break;  
        obj=obj2;
        index=this.index.length;
    }
}

DOMElementData.prototype.check=function(index) {
    var obj=this.index[index];
    var e=obj.element;
    var id=this.getID(e);
    if(!this.checkExists(e)) {
        var obj2=this.mapping[id];
        this.delindex(index);
        if(obj==obj2) delete this.mapping[id];
        obj.element=false;
        return 1; 
    }
    this.mapping[id]=obj;
    return 0; /* ok */ 
}

DOMElementData.prototype.get=function(e) {
    var id=this.getID(e);
    if(id===false) return(false);
    var obj=this.mapping[id]; 
    var id2;
    if(obj&&obj.element===e) return(obj);
    alert('not found');
    if(this.unique) return(null);
    /* either the element is gone, or the id has changed. */
    alert('not yet implemented for id='+id);
}

DOMElementData.prototype.clean=function() {
    while(this.index.length>0) {
        if(this.sweepindex==0) this.sweepindex=this.index.length;
        this.sweepindex-=1;
        if(this.check(this.sweepindex)==0) break; 
    }
}

function DOMElementAssocObj(e) {
    this.element=e;
}

DOMElementData.prototype.add=function(e) {
    var id=this.getID(e);
    var obj2=false;
    this.clean();
    if(!id) {  
        this.mycounter+=1;
        id='__yajax_'+this.mycounter; 
        e.id=id;
    }
    else obj2=this.mapping[id];
    if(obj2) {
        if(obj2.element===e) {
            alert('Double ObjElement addition');
            return(obj2);
        }
    }
    var obj=new DOMElementAssocObj(e);
    if(!obj) alert('ASSERTION FAILED: obj not returned');
    this.mapping[id]=obj;
    this.index.push(obj);
    return obj;
}

coreDOMElementData=new DOMElementData();

core.getDOMObj=function(e) {
    if(typeof(e)=='string') {
        e=document.getElementById(e);
    }
    return coreDOMElementData.get(e);
}
core.addDOMObj=function(e) {
    if(typeof(e)=='string') e=document.getElementById(e);
    return coreDOMElementData.add(e);
}

function EventHandler() {
    this.submitClick=null;
}

EventHandler.prototype.stopHandling=function() {
    this.boolStopHandling=true;
}

EventHandler.prototype.stopPropagation=function() {
    var ev=this.event;
    if(ev.stopPropagation) ev.stopPropagation();
    else ev.cancelBubble=true;
}

EventHandler.prototype.eventFunc=function(ev) {
    if(!ev) ev=window.event; 
    var e=this;
    var eh=eventHandler;
    var event_name=ev.type;
    if(ev.target) eh.target=ev.target;
    else if(ev.srcElement) eh.target=ev.srcElement;
    else eh.target=e;
    if(event_name=='click') {
        if((eh.target.nodeName=='INPUT'||eh.target.nodeName=='BUTTON')&&eh.target.type=='submit')
            eh.submitClick=eh.target;
    }
    eh.currentTarget=e;
    eh.boolStopHandling=false;
    eh.preventDefault=false;
    eh.event=ev;
    if(ev.target) eh.target=ev.target;
    else eh.target=ev.srcElement;
    var eObj=coreDOMElementData.get(e);
    eh.eObj=eObj;
    if(!eObj) {
        alert('OOPS!  No eObj '+event_name);
        alert('data: '+e.id);
    }
    var evhs=eObj['on'+event_name];
    for(var i in evhs) {
        var f=evhs[i];
        if(f.call) f.call(eh.currentTarget,ev);
        else evalFunc.call(eh.currentTarget,f);
        if(eh.boolStopHandling) {
            eh.preventDefault=true;
            break;
        }
    }
    if(eh.preventDefault) {
        if(ev.returnValue)
            ev.returnValue=false; // hack for MS
        if(ev.preventDefault) ev.preventDefault();
    }
    return !eh.preventDefault;
}

EventHandler.prototype.handleEvent=function(e,event) {
    var eObj;
    if(e instanceof DOMElementAssocObj) {
        eObj=e;
        e=e.element;
    }
    else {
        if(typeof(e)=='string') {
            var s=e;
            e=document.getElementById(e);
            if(!e) throw('handleEvent: No such id:'+s);
        }
        eObj=core.getDOMObj(e);
    }
    var evh=eObj['on'+event]; 
    if(evh) return(evh);
    evh=new Array();
    eObj['on'+event]=evh;
    if(e.addEventListener) {
        try {e.addEventListener(event,this.eventFunc,false);} catch(e) { }
    }
    else if(e==window&&e.attachEvent) {
        e.attachEvent('on'+event,this.eventFunc);
    }
    else {
        e['on'+event]=this.eventFunc; 
    }
    return evh;
}

eventHandler=new EventHandler();

core.addDOMObj(window);

core.urlAdds=function(href,val) {
    if(href.indexOf('?')==-1) href+='?'+val;
    else href+='&'+val;
    return href;
}

if(document.cookie&&document.cookie.indexOf&&document.cookie.indexOf(';')!=-1); 
else if(typeof document.cookie!='undefined') {
    core.setCookie('x','chkcookie');
    ck=core.getCookie('x');
    if(ck!='chkcookie') cookies=false;
    else core.delCookie('x');
}
if(cookies==false) compatible=false;
if(!compatible) REQcompatible=false;

