//-----------------------------------------------------------------------------------
// MyDom basic functions - DOM type detection and finding of DOM address.
//-----------------------------------------------------------------------------------
var isDHTML = 0;
var isLayers = 0;
var isAll = 0;
var isID = 0;

if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
	browserVersion = parseInt(navigator.appVersion);
	if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
	else {
	if (document.all) {isAll = 1; isDHTML = 1;}
}}

//----- Inner function - not to be used outside of this module -----
function findDOMinLayersDeeper( objectID, searchRoot ) {
	var curobj;
	var to_see = new Array();

	to_see.push( searchRoot );
	while ( to_see.length > 0 )
	{
		curobj = to_see.pop();
		if ( curobj.layers ) { // ------------
			if ( curobj.layers[objectID] ) {
				return curobj.layers[objectID];
			}
		}
		else { // no layers under curobj -----
			continue;
		}
		// there are layers under curobj -----

		for ( var i = curobj.layers.length - 1; i >= 0; i-- ) {
			testObj = curobj.layers[i];
			to_see.push( testObj );
		}
	}
	return null;
}

//----- Inner function - not to be used outside of this module -----
function findDOMinLayers( objectID ) {
	if (!isLayers) { return null; }
	return findDOMinLayersDeeper( objectID, document );
}

function findDOM(objectID,withStyle) {
	if (withStyle == 1) {
		if (isID) { return (document.getElementById(objectID).style) ; }
		else { 
			if (isAll) { return (document.all[objectID].style); }
		else {
			if (isLayers) { return findDOMinLayers( objectID ); }
		};}
	}
	else {
		if (isID) { return (document.getElementById(objectID)) ; }
		else { 
			if (isAll) { return (document.all[objectID]); }
		else {
			if (isLayers) { return findDOMinLayers( objectID ); }
		};}
	}
	return null;
}

//-----------------------------------------------------------------------------------
// MyDom - unified and simplified DOM for most browsers.
//-----------------------------------------------------------------------------------

//----- Finding object's properties: width -----
function MyDom_getWidth()
{
	if ( this.dom_addr.offsetWidth ) 
		return this.dom_addr.offsetWidth;
	if ( this.dom_addr.clip.width )
		return this.dom_addr.clip.width;
	return null;
}

//----- Finding object's properties: height -----
function MyDom_getHeight()
{
	if ( this.dom_addr.offsetHeight ) 
		return this.dom_addr.offsetHeight;
	if ( this.dom_addr.clip.height )
		return this.dom_addr.clip.height;
	return null;
}

//----- Finding object's properties: left -----
function MyDom_getLeft()
{
	if (this.dom_style.left)
		return this.dom_style.left;
	if (this.dom_style.pixelLeft)
		return this.dom_style.pixelLeft;
	if (this.dom_addr.offsetLeft)
		return this.dom_addr.offsetLeft;
	return null;
}

//----- Finding object's properties: top -----
function MyDom_getTop()
{
	if (this.dom_style.top)
		return this.dom_style.top;
	if (this.dom_style.pixelTop)
		return this.dom_style.pixelTop;
	if (this.dom_addr.offsetTop)
		return this.dom_addr.offsetTop;
	return null;
}

//----- Finding object's properties: right -----
function MyDom_getRight()
{
	var domStyle = this.dom_style;
	var dom      = this.dom_addr;

	if (dom.left)
		return (domStyle.left + domStyle.clip.width);
	if (domStyle.pixelLeft)
		return (domStyle.pixelLeft + dom.offsetWidth);
	if (dom.offsetLeft)
		return (dom.offsetLeft + dom.offsetWidth);
	return null;
}

//----- Finding object's properties: bottom -----
function MyDom_getBottom()
{
	var domStyle = this.dom_style;
	var dom      = this.dom_addr;

	if (domStyle.top)
		return (domStyle.top + domStyle.clip.height);
	if (domStyle.pixelTop)
		return (domStyle.pixelTop + dom.offsetHeight);
	if (dom.offsetTop)
		return (dom.offsetTop + dom.offsetHeight);  
	return null;
}

//----- Finding object's properties: z-index -----
function MyDom_getZIndex()
{
	var domStyle = this.dom_style;
	if ( domStyle.zIndex != null )
		return domStyle.zIndex;
	return null;
}

//----- Finding object's properties: visibility (1=visible; 0=not) -----
function MyDom_isVisible()
{
	if ( isDHTML ) {
		vis1 = this.dom_style.visibility;
		if (( vis1 == 'show') || ( vis1 == 'visible')) {
			return 1;
		}
		return 0;
	}
	return null;
}

//----- Finding object's properties: value -----
function MyDom_getValue()
{
	if ( isDHTML ) {
		return this.dom_addr.value;
	}
	return null;
}

//----- Finding object's properties: value -----
function MyDom_isChecked()
{
	if ( isDHTML ) {
		return this.dom_addr.checked;
	}
	return null;
}

//--------------------------------------
//----- Changing object properties -----
//--------------------------------------
function MyDom_setZIndex( layerNum )
{
	if ( isDHTML ) {
		this.dom_style.zIndex = layerNum;
	}
}

//----- Setting of visibility (1=visible; 0=not) -----
function MyDom_setVisibility( value )
{
	if ( isDHTML ) {
		if (( value == 'hide') || ( value == 'hidden') || ( (value+'') == '0' )) {
			val1 = 'hidden';
		}
		else {
			val1 = 'visible';
		}

		this.dom_style.visibility = val1;
	}
}

//----- Setting of display (1=visible; 0=not) -----
function MyDom_setDisplay( value )
{
	if ( isDHTML ) {
		if (( value+'' == 'none') || ( (value+'') == '0' )) {
			val1 = 'none';
		}
		else {
			val1 = 'inline';
		}

		this.dom_style.display = val1;
	}
}

//----- Setting of visibility (1=visible; 0=not) -----
function MyDom_toggleVisibility()
{
	if ( isDHTML ) {
		this.setVisibility( this.isVisible() ? 0 : 1 );
	}
}

//----- Moves object (top left corner) to (x, y) -----
function MyDom_moveTo( x, y )
{
	if ( isDHTML ) {
		this.dom_style.left = x;
		this.dom_style.top  = y;
	}
}

//----- Moves object (top left corner) by (deltaX, deltaY) -----
function MyDom_moveBy( deltaX, deltaY ) {
	var domStyle = this.dom_style;
	var dom      = this.dom_addr;

	if ( !isDHTML ) { return; }

	if (domStyle.pixelLeft) {
		domStyle.pixelLeft += deltaX;
		domStyle.pixelTop  += deltaY;
	}
	else if (dom.offsetLeft != null) {
		var plusLeft = dom.offsetLeft;
		var plusTop  = dom.offsetTop;

		domStyle.left = deltaX + plusLeft;
		domStyle.top  = deltaY + plusTop;
	}
	else dom.moveBy(deltaX,deltaY);
}

//----- Changes object style -----
function MyDom_changeStyle( styleName, newVal ) {
	if ( isDHTML && !isLayers ) {
		this.dom_style [styleName] = newVal;
	}
}

//----- Changes object class -----
function MyDom_setClass( newClass ) {
	if ( isDHTML && !isLayers ) {
		this.dom_addr.className = newClass;
	}
}

//----- Checking object's properties: value -----
function MyDom_isValidDate() {
  var val1 = this.getValue();

  if ( val1 == null || (val1+'') == '' ) {
    return false;
  }

  var val2 = new String( val1 );
  var re = new RegExp( "([0-9]+)\\.([0-9]+)\\.([0-9]+)" ); 

  var reg1 = val2.match( re );
  if ( reg1 == null ) {
    return false;
  }

  day   = reg1[1]*1;
  month = reg1[2]*1;
  year  = reg1[3]*1;

  if (day<1 || day>31) {
    return false;
  }

  if (month<1 || month>12) {
    return false;
  }

  if ( day==31 && ( month == 2 || month == 4 || month == 6 || month == 9 || month == 11 ) ) {
    return false;
  }

  if ( day>29 && month==2 ) {
    return false;
  }

  if (year<1) {
    return false;
  }

  if (year>99 && year<1000) {
    return false;
  }

  if (year<70) {
    year = year*1 + 2000;
  }
  else if (year<100) {
    year = year*1 + 1900;
  }

  newval = (day<10? '0':'') + day + '.' + (month<10? '0':'') + month + '.' + year;

  this.dom_addr.value = newval;

  return true;
}

//----- Set MyDom object to point to the given DOM object -----
function MyDom_setId( objId )
{
	this.id  = objId;
	this.dom_addr = findDOM( objId, 0 );
	if ( isLayers ) {
		this.dom_style = this.dom_addr; // for time economy
	}
	else {
		this.dom_style = findDOM( objId, 1 );
	}
}

//----- MyDom Constructor ----------------------------
function MyDom( objId )
{
	//----- Attributes: -----
	this.id        = null;
	this.dom_addr  = null;
	this.dom_style = null;
	
	//----- Methods: -----
	this.setId = MyDom_setId;

	if ( arguments.length > 0 ) { 
		this.setId( objId );
	}

	this.getWidth  = MyDom_getWidth;
	this.getHeight = MyDom_getHeight;
	this.getLeft   = MyDom_getLeft;
	this.getTop    = MyDom_getTop;
	this.getRight  = MyDom_getRight;
	this.getBottom = MyDom_getBottom;
	this.getZIndex = MyDom_getZIndex;

	this.isVisible = MyDom_isVisible;

	this.setZIndex        = MyDom_setZIndex;
	this.setVisibility    = MyDom_setVisibility;
	this.toggleVisibility = MyDom_toggleVisibility;
	this.setDisplay       = MyDom_setDisplay;

	this.moveTo = MyDom_moveTo;
	this.moveBy = MyDom_moveBy;

	this.changeStyle = MyDom_changeStyle;
	this.setClass    = MyDom_setClass;
}