//
// Cross Browsers JS Model
//
var ObjectsType;

if(document.getElementById) ObjectsType="DOM2";
else if(document.all) ObjectsType="IEDOM1"
else if(document.layers) ObjectsType="NCDOM1";

function GetStyleById(IdName){
    switch(ObjectsType){
        case "DOM2":
            if (GetObjectById(IdName)) {
                return document.getElementById(IdName).style;
			}
            break;
        case "IEDOM1":
			if (GetObjectById(IdName)) {
                return document.all[IdName].style;
			}
            break;
        case "NCDOM1":
            return document.layers[IdName];
    }
}

function GetObjectById(IdName, FormName){
    switch(ObjectsType){
        case "DOM2":
            if (FormName) {
                return eval('document.getElementById("'+FormName+'").'+IdName) ;
            } else
                return document.getElementById(IdName);
        case "IEDOM1":
            if (FormName)
                return eval('document.all["'+FormName+'"].'+IdName) ;
            else
                return document.all[IdName];
        case "NCDOM1":
            if (FormName)
                return eval('document.layers["'+FormName+'"].'+IdName) ;
            else
                return document.layers[IdName];
    }
}

function GetObjectByName(IdName, FormName){
    switch(ObjectsType) {
        case "DOM2":
            if (FormName) {
                return eval('document.getElementsByName("'+FormName+'").'+IdName) ;
            } else
                return document.getElementsByName(IdName);
        case "IEDOM1":
            if (FormName)
                return eval('document.all["'+FormName+'"].'+IdName) ;
            else
                return document.all[IdName];
        case "NCDOM1":
            if (FormName)
                return eval('document.layers["'+FormName+'"].'+IdName) ;
            else
                return document.layers[IdName];
    }
}

function MyNewWindow(mypage,myname,w,h,scroll,ret) 
{
	// showModalDialog for IE
 	// MyNewWindow() - centering new window if user's screen resolution > 800x600
 	if (h<=0) { h = 480; }
 	if (w<=0) { w = 640; }
 	var scW = screen.availWidth ? screen.availWidth : screen.width;
	var scH = screen.availHeight ? screen.availHeight : screen.height;
	var winl = (scW-w)/2; 
	var wint = (scH-h)/2;
	
  	if (!screen) {
	    winl = 0; wint = 0;
	}
  	var settings  ='height='+h+',';
      	settings +='width='+w+',';
      	settings +='top='+wint+',';
      	settings +='left='+winl+',';
      	settings +='status=0,';
      	settings +='scrollbars='+scroll+',';
      	settings +='resizable=yes';
    win = null;
  	win=window.open(mypage,myname,settings);
    win.opener = window;
  	if( !window.opera ) { win.moveTo(winl,wint); }
  	if (parseInt(navigator.appVersion) >= 4) {win.window.focus();}
  	if (ret) { return win ; }
}

function _showPopup(modal,url,value,properties,winname,alignment) {
    if (modal && !properties) properties = "dialogWidth=230px;dialogHeight=155px;status=0;scroll=0;help=0" ;
    if (modal && typeof window.showModalDialog != 'undefined') {
        var thevalues = value.split(/,/);
        return window.showModalDialog(url, thevalues, properties);
    }

    if (!properties) {
        var w = 640 ;
        var h = 480 ;
        var scW = screen.availWidth ? screen.availWidth : screen.width;
        var scH = screen.availHeight ? screen.availHeight : screen.height;
        var winl = (scW-w)/2;
        var wint = (scH-h)/2;
        properties = 'width='+w+',height='+h+',status=0,scrollbars=1,top='+wint+',left='+winl ;
    }
    if (alignment) {
        switch(alignment) {
            case "center":
                var w = properties.replace(/^.*width=(\d+),.*$/i, '$1') ;
                var h = properties.replace(/^.*height=(\d+),.*$/i, '$1') ;
                var scW = screen.availWidth ? screen.availWidth : screen.width;
                var scH = screen.availHeight ? screen.availHeight : screen.height;
                var winl = (scW-w)/2;
                var wint = (scH-h)/2;
                properties = properties + ',resizable=1,top='+wint+',left='+winl ;
                break;
        }
    }
    var win=window.open(url,winname?'POPUP'+winname:'_BLANK',properties);    
    if (!win) {
        alert('We have detected that you are using popup blocking software. Please turn off popup blocking for this site!');
    }
    //win.resizeTo(w,h);
  	if (parseInt(navigator.appVersion) >= 4) {win.window.focus();}
    else win.focus();
    return win ;
}

function _selectall(tableName, state) {
    // applicable to table with rows and cols only, no nested tables
    var tbl = GetObjectById(tableName) ;
    if (!tbl) return ;
    for (i = 0; i < tbl.rows.length; i++) {
        for (j = 0; j < tbl.rows[i].cells.length; j++) {
            for (k =0; k < tbl.rows[i].cells[j].childNodes.length; k++) {
                cb = tbl.rows[i].cells[j].childNodes[k];
                if (!cb || cb.type != "checkbox") continue;
                if (cb.disabled) 
                    cb.checked = false;
                else
                    cb.checked = state ;
            }
        }
    }
}

function xmlGet(IDTag, xmlURL, postAction) {
    var Area = GetObjectById(IDTag);
    //Area.innerHTML = 'Retrieving in progress, please wait...';
    var xmlHttp = XmlHttp.create();
    xmlHttp.open('GET', xmlURL+'&r='+Math.random(), true);
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4) {
            Area.innerHTML = xmlHttp.responseText ;
            if (postAction) eval(postAction);
        }
    }
    xmlHttp.send(null);
}

function doOrdering(InputName, type) {
    var oSelect = document.getElementById(InputName);
    var swap;

    if (oSelect.options.length==0) return;
    if (type == 1 && !oSelect.options[0].selected) {
        for (var i = 0; i < oSelect.options.length; i++) {
            if (oSelect.options[i].selected) {
                swap = oSelect.options[i-1];
                if (!swap.selected) oSelect.options[i].swapNode(swap);
            }
        }
    } else if (type == 2 && !oSelect.options[oSelect.options.length-1].selected) {
        for (var i = oSelect.options.length - 1; i >= 0; i--) {
            if (oSelect.options[i].selected) {
                swap = oSelect.options[i+1];
                if (!swap.selected) oSelect.options[i].swapNode(swap);
            }
        }
    }
}

function _setDefault (attr_id, val) {
    try {
        var Inp = GetObjectById('INP['+attr_id+'][]');    
    } catch(e) {
        return false;
    }
    if (!Inp) return false;
    if (val==null || val=="" ) return false;
    switch (Inp.type) {
    case "text":
    case "textarea":
    case "password":
    case "hidden":
        if (val!='') Inp.value = val ;
        break;
    case "select-one":
        for (var j = 0; j < Inp.options.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp.options[j].value+']');
            if (v!=null && (Inp.options[j].value==val || v.innerHTML.toUpperCase()==val.toUpperCase())) {
                Inp.selectedIndex = j;
                break;
            }
        }
        break;
    case "select-multiple":
        val = ','+val+',';
        for (var j = 0; j < Inp.options.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp.options[j].value+']');
            if (v!=null && (val.match(','+Inp.options[j].value+',') || val.toUpperCase().match(','+v.innerHTML.toUpperCase()+','))) 
                Inp.options[j].selected = true;
            else
                Inp.options[j].selected = false;
        }
        break;
    case "radio":
        var Inp2 = GetObjectByName('INP['+attr_id+'][]');
        for (var j = 0; j < Inp2.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp2[j].value+']');
            if (v!=null && (Inp2[j].value==val || v.innerHTML.toUpperCase()==val.toUpperCase())) {
                Inp2[j].checked = true;
                break;
            }
        }
        break;
    case "checkbox":
        var Inp2 = GetObjectByName('INP['+attr_id+'][]');    
        val = ','+val+',';
        for (var j = 0; j < Inp2.length; j++) {
            v = GetObjectById('INP['+attr_id+']['+Inp2[j].value+']');
            if (v!=null && (val.match(','+Inp2[j].value+',') || val.toUpperCase().match(','+v.innerHTML.replace("+","\\+").toUpperCase()+','))) 
                Inp2[j].checked = true;
            else
                Inp2[j].checked = false;
        }
        break;
    }
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else{
    obj.removeEventListener( type, fn, false );
  } 
}

function showhide(id){
    if (document.getElementById){
        obj = document.getElementById(id);
        if (obj.style.display == "none"){
            obj.style.display = "";
        } else {
            obj.style.display = "none";
        }
    }
}



