// HTML DOM helpers
// (c) SimX Corp. 2002

if( !basic_loaded ) {

function UserAgentType() {
 var  d     = document;
 this.Agent = navigator.userAgent.toLowerCase();
 this.major = parseInt(navigator.appVersion);
 this.DOM   = (d.getElementById) ? 1 : 0;
 this.ns    = (d.layers);
 this.ns4up = (this.ns && this.major >=4);
 this.ns6   = (this.DOM && navigator.appName == "Netscape");
 this.op    = (window.opera ? 1:0);
 this.ie    = (d.all);
 this.ie4   = (d.all && !this.DOM ) ? 1 : 0;
 this.ie4up = (this.ie && this.major >= 4);
 this.ie5   = (d.all && this.DOM);
 this.win   = ((this.Agent.indexOf("win")!=-1) || (this.Agent.indexOf("16bit")!=-1));
 this.mac   = (this.Agent.indexOf("mac")!=-1);
}

var ua = new UserAgentType();

//window.onerror = function( d,f,l ) { alert("Sorry, but a client-side error has occured \nin file "+f+" line "+l+":\n"+d); }

if( document.getElementById ) {
    var GetElem = function( id ) {
        return document.getElementById( id );
    }
    var GetElemsOf = function( obj, tag_name ) {
        return obj.getElementsByTagName( tag_name );
    }
}
else
if( document.all ) {
    var GetElem = function( id ) {
        return document.all[ id ];
    }
    var GetElemsOf = function( obj, tag_name ) {
        return obj.all.tags( tag_name );
    }
}
else {
    var GetElem = function( id ) {
        return document.forms[ 0 ].elements[ id ]; // Warning! For controls only!
    }
    var GetElemsOf = function( obj, tag_name ) {
        return null;
    }
}

function Elem( friendly_name )
{
    if( name_map )
        return GetElem( name_map[friendly_name] );
}

function StopEvent( e )
{
    if( !e ) e = window.event;
    if( !e ) return;
    e.cancelBubble = true;
    if( e.stopPropagation ) e.stopPropagation();
}

function changeHandler( control_id, event )
{
    StopEvent( event );
    dirty_too[control_id]=true;
}

function Sbm( form_name, out_ctrl, trg_name, recid, ext_str, flags, cnf, pb_delay ) 
{
    Sbt( { f:form_name, h:out_ctrl, t:trg_name, r:recid, e:ext_str, b:flags, c:cnf, d:pb_delay });
}

function Sbt( submit_struct ) {
    if( submit_struct.b & 0x80 && submit_struct.c && submit_struct.c.length && !window.confirm( submit_struct.c ) ) return false;
    if( window.event ) event.cancelBubble = true;
    pgform = document.forms[submit_struct.f];
    if( !pgform ) pgform = document.forms[0];
    if( !pgform ) {
        alert("HTML form is unknown");
        return;
    }
    var URL_string = pgform.action;
    var ssl = submit_struct.b & 2; // SUBMIT_SECURE
    if( ssl ) {
        if( URL_string.indexOf( "http://" ) == 0 )
            URL_string = "https://" + URL_string.substr( 7 );
    }
    pgform.action = URL_string;
    pgform.sbmctr.value = submit_struct.h;
    pgform.recid.value = submit_struct.r;

    if( submit_struct.e && submit_struct.t && submit_struct.t.length && submit_struct.t != '_self' &&
        ( !page_name || submit_struct.t != page_name ) &&
        top.frames[submit_struct.t] && submit_struct.e.substring(0, 4) == 'http' ) {
        top.frames[submit_struct.t].location = submit_struct.e;
        submit_struct.t = '';
    }
    else
        pgform.extra.value=submit_struct.e ? submit_struct.e : '';

    if( !submit_only &&
        typeof DynamicUpdate != 'undefined' && 
               DynamicUpdate( submit_struct.t, ssl, submit_struct.b, null, submit_struct.d, submit_struct.p ) ) 
                return;
    pgform.cmode.value = "1";
    if( submit_struct.b & 0x40 ) pgform.sp.value="1";    // SUBMIT_SAFEPOST
    RealSubmit( submit_struct.t, submit_struct.p );
    return;
}

function RealSubmit( trg_name, custom_params )
{
    pgform.target=trg_name?trg_name:"";
    if( trg_name == '' || trg_name == "_self" || ( custom_params && custom_params.length ) ) {
        var URL_string
        URL_string = pgform.action + "?comp="  + pgform.comp.value +
                                    "&recid="  + pgform.recid.value +
                                    "&uiinfo=" + pgform.uiinfo.value +
                                    "&mvars="  + pgform.mvars.value +
                                    "&extra="  + pgform.extra.value +
                                    "&" + custom_params;
        pgform.action = URL_string;
    }
    pgform.submit();
}

function FindRecId( elem, iter_n, recid_n, iter )
{
    var pnt = elem.parentNode;
    if( !pnt ) return;
    if( pnt.id == iter_n ) {
        var recid_id = recid_n + "_" + iter;
        var inputs = pnt.getElementsByTagName('INPUT');
        if( !inputs.length )
             inputs = document.getElementsByTagName('INPUT');
        for( var i = 0; i < inputs.length; i++ ) {
            if( inputs.item(i).id == recid_id  )
                return inputs.item(i).value;
        }
        return;
    }
    return FindRecId( pnt, iter_n, recid_n, iter );
}

function AddClass(o,c)
{
    var class_names = o.className.split(/\s+/);
    for( n in class_names ) if(class_names[n]==c) return;
    o.className += " "+c;
}

function GetText(o)
{
    if( o.value ) return o.value;
    if( o.textContent ) return o.textContent;
    if( o.innerText ) return o.innerText;
    return null;
}

function SetText(o,t)
{
    if( o.value != null )
        o.value = t;
    else
    if( o.textContent != null )
        o.textContent = t;
    else
    if( o.innerText != null )
        o.innerText = t;
}

function GetXPos( o )
{
    var x = (ua.ns)? o.left : o.offsetLeft;
    return x;
}

function SetXPos( o, x )
{
    (ua.ns) ? o.left = x : o.style.left = x;
}

function GetYPos( o )
{
    var y = (ua.ns)? o.top : o.offsetTop;
    return y;
}

function SetYPos( o, y )
{
    (ua.ie || ua.DOM)? o.style.top = y : (ua.ns)? o.top = y : o.style.pixelTop = y;
}

function GetPageXPos( o )
{
    if(ua.ns) {
        alert("Upgrade your browser!");
        var x=(o.pageX)? o.pageX:o.x;
        return x;
    }
    else {
        var x=0;
        while(eval(o)) {
            x+=o.offsetLeft;
            o=o.offsetParent;
        }
        return x;
    }
}

function GetPageYPos(o)
{
    if(ua.ns) {
        var y=(o.pageY) ? o.pageY : o.y;
        return y;
    } else {
        var y=0;
        while(eval(o)) {
            y+=o.offsetTop;
            o=o.offsetParent;
        }
        return y;
    }
}


function GetHeight(o)
{
    var h=0;
    if(ua.ns) {
        h=(o.height) ? o.height : o.clip.height;
        return h;
    }
    return o.offsetHeight;
}

function GetPageWidth()
{
    if(!ua.ie || ua.op) // all except Explorer
	    return window.innerWidth;
    else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	    return document.documentElement.clientWidth;
    else if (document.body) // other Explorers
	    return document.body.clientWidth;
}

function SetHeight(o,h)
{
    if(ua.ns) {
        if(o.clip)
            o.clip.bottom=h;
    }
    else
       o.style.height=h;
}


function GetWidth(o)
{
    var w=0;
    if(ua.ns) {
        return o.width ? o.width : o.clip.width;
    }
    return o.offsetWidth;
}

function SetWidth(o,w)
{
    if(ua.ns) {
        if(o.clip)
           o.clip.right=w;
    } else o.style.width=w;
}

function GetPageHeight()
{
    if(!ua.ie || ua.op) // all except Explorer
	    return window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	    return document.documentElement.clientHeight;
    else if(document.body) // other Explorers
	    return document.body.clientHeight;
}

function GetXScroll()
{
    if(self.pageYOffset) // all except Explorer
	    return window.scrollX;
    else if(document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	    return document.documentElement.scrollLeft;
    else if(document.body) // all other Explorers
	    return document.body.scrollLeft;
}

function GetYScroll()
{
    if(self.pageYOffset) // all except Explorer
	    return window.scrollY;
    else if(document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	    return document.documentElement.scrollTop;
    else if(document.body) // all other Explorers
	    return document.body.scrollTop;
}

function RefreshControls()
{
    for( var u in updates) {
        if( typeof updates[u] == 'function' ) {
            try {
                if(p) p.out('refreshing control ' + u);
                updates[u]();
            }
            catch( e ) {
            }
        }
    }
}

function OnLoadInit()
{
    if( popup && popup.length )
        alert(popup);
    RefreshControls();
    try {
        if( model_list && model_list.length ) {
            var top_target_frame = top;
            if( !top.Update ) {
                for( var i=0; i< top.frames.length; i++ ) {
                    if( top.frames[i].Update ) {
                        top_target_frame = top.frames[i];
                        break;
                    }
                }
            }
/*// IE7 itself is a top frame for its opened tabs. In favor of Nikita's blocking subsystem this code was disabled.
            if( top_target_frame.Update )
                top_target_frame.Update(model_list, document.forms[0].name);
            if( window.opener && window.opener.Update )
                window.opener.Update( model_list, document.forms[0].name );
*/

        }
        var script_pos = document.location.search.indexOf("javascript");
        if( script_pos >= 0 ) {
            var str = document.location.search.substr( script_pos + 6 );
            eval( unescape(str) );
        }
    } catch( e ) {}
    progress_image = new Image();
    progress_image.src='/Target/WebResources/progress.gif';
    if( typeof Custom != "undefined" ) Custom();
}

if( location.search.indexOf("$console$") > 0 && !window.console ) {
    window.console = new function() {
        this.wnd = null;
        this.checkOpen = function() {
            if( !this.wnd || this.wnd.closed) {
             this.wnd = window.open( "", "Console", "width=400,height=800,scrollbars=yes,resizable=yes" );
             this.wnd.document.writeln( "<div style='position: absolute; bottom: 5px;'>" ); 
            }
        }
        this.info = function(s) { 
            this.checkOpen();
            this.wnd.document.writeln( "<pre style='margin:0px;'>"+s+"</pre>" ); 
        }
        this.error = function(s) { 
            this.checkOpen();
            this.wnd.document.writeln( "<pre style='margin:0px; color: red;'>"+s+"</pre>" ); 
        }
        this.time = function(s) {}
        this.timeEnd = function(s) {}
        this.profile = function(s) {}
        this.profileEnd = function(s) {}
    }
}

var IsTrue = function( q ) { return q==true||q=="true"||q=='1'||q==1; }

var DataHolder = function( data ) {
    this.data = data;
    if( this.data ) this.data["disabled"]=this.data.getAttribute("disabled");
}
DataHolder.prototype.GetDataElem = function() { return this.data; }
DataHolder.prototype.set   = function(dirty) { if( this.data ) { this.data.value='1'; if( dirty ) changeHandler(this.data.id); } }
DataHolder.prototype.clear = function(dirty) { if( this.data ) { this.data.value='';  if( dirty ) changeHandler(this.data.id); } }
DataHolder.prototype.isOn = function() { return this.data?IsTrue(this.data.value):false; }
DataHolder.prototype.isDisabled = function() { return this.data?IsTrue(this.data.disabled ):false; }

var submit_only = false;
var ctrl_drty = new Array;
var dirty_too = new Array;
var name_map  = new Array;
var varheight = new Array;
var offheight = new Array;
var recids  = new Array;
var models  = new Array;
var updates = new Array;
var ow = null;

var basic_loaded = true;

}
