
var XML_ELEMENT_NODE = 				1;		//Node is an element 
var XML_ATTRIBUTE_NODE = 				2;		//Node is an attribute 
var XML_TEXT_NODE = 						3;		//Node is a piece of text 
var XML_CDATA_SECTION_NODE = 	4;
var XML_ENTITY_REF_NODE = 			5;
var XML_ENTITY_NODE = 					6;		//Node is an entity like &nbsp; 
var XML_PI_NODE = 							7;		//Node is a processing instruction 
var XML_COMMENT_NODE = 				8;		//Node is a comment 
var XML_DOCUMENT_NODE = 			9;		//Node is a document 
var XML_DOCUMENT_TYPE_NODE = 	10;
var XML_DOCUMENT_FRAG_NODE = 	11;
var XML_NOTATION_NODE = 				12;


/**
 * 
 * 
 * 
 */
function selectOption( select, value ) {
	
	//alert( "value = " + value );
	
	var options = select.options;
	
	for ( var i = 0; i < options.length; i++ ) {
		
		//alert( "options[" + i + "] = " + options[i].value );
		
		if ( options[i].value == value ) {
			
			options[i].selected = true;
			
			break;
		}
	}
}

/**
 *
 * array dump
 * "38. Malenkie hitrosti JavaScript" D.Koterov
 *
 */
function dump( d, l ) {
	
	if ( l == null ) l = 1;

    var s = '';

    if ( typeof( d ) == "object" ) {
		
		s += typeof(d) + " {\n";
        
		for (var k in d) {
            
			for ( var i=0; i<l; i++ ) s += "  ";
            
			s += k+": " + dump( d[k], l+1 );
        }
		for ( var i=0; i<l-1; i++ ) s += "  ";
        
		s += "}\n"
    
	} else {
        
		s += "" + d + "\n";
    }
	return s;
}


/**
 *
 * not unicode analogue escape()
 * IE 5.5+
 * http://xpoint.ru/know-how/JavaScript/PoleznyieFunktsii?26#EscapeSovmestimyiySRusskimiBuk
 *
 */
( function initRightEscape() { //alert( 'loadRightEscape' );

	var trans = [];
	for (var i = 0x410; i <= 0x44F; i++) trans[i] = i - 0x350;
	trans[0x401] = 0xA8;
	trans[0x451] = 0xB8;
	
	window.rightEscape = function rightEscape(str) {
		
		var ret = [];
		
		for ( var i = 0; i < str.length; i++ ) {
			
			var n = str.charCodeAt(i);
			
			if (typeof trans[n] != 'undefined') n = trans[n];
			if (n <= 0xFF) ret.push(n);
		}
		return escape( String.fromCharCode.apply(null, ret) );
	}
	
} )();

/**
 * 
 * PHP: array_key_exists
 *
 */
function arrayKeyExists( key, arr ) {
	
	return arr && ( arr[key] != undefined );
}

/**
 * 
 * PHP: in_array
 *
 */
function inArray( value, arr  ) {

	res = false;
	
	if ( arr && ( arr instanceof Array ) ) for ( var i; i < arr.length; i++ ) {
		
		if ( arr[i] == value ) {
		
			res = true;
			
			break;
		}
	}
	
	return res;
}


/**
 * 
 * double figure int to string
 *
 */
function intToStr2Char( intNum ) {

	var str = '';
	
	if ( intNum <= 9 ) {

		str += '0';
	}

	str += intNum;
	
	return str;
}

/**
 * 
 * analogous Math.min
 *
 */
function min( arr, sortFunction ) {
	
	arr.sort( sortFunction );
	
	return arr[0];
}

/**
 * 
 * 
 *
 */
function innerText( nParent, str ) {
	
	if ( str != undefined ) {
	
		if ( nParent.innerText != undefined ) {
	
			nParent.innerText = str;
	
		} else {
	
			removeChilds( nParent );
	
			nParent.appendChild( document.createTextNode( str ) );
		}
	
	} else {
		
		str = '';
		
		if ( nParent.innerText != undefined ) {
	
			str = nParent.innerText;
	
		} else {
		
			if ( nParent.hasChildNodes() ) {
			
				var childNodes = nParent.childNodes;
				
				var textNodesValues = new Array();
				
				for ( var i=0; i < childNodes.length; i++ ) {
					
					if ( childNodes[i].nodeType == XML_TEXT_NODE ) {
						
						textNodesValues[ textNodesValues.length ] = childNodes[i].nodeValue;
					
					} else if ( childNodes[i].nodeType == XML_ELEMENT_NODE ) {
						
						textNodesValues[ textNodesValues.length ] = innerText( childNodes[i] );
					}
					
					str = textNodesValues.join('');
				}
			}
		}
	}
	
	return str;
}

/**
 * 
 * 
 *
 */
function innerHTML( nParent, str ) {
	
	if ( str != undefined ) { // 
	
		if ( nParent.innerHTML != undefined ) { // Mozilla and WinMSIE6
	
			 nParent.innerHTML = str;
	
		} else {
			
			// TODO
		}
	
	} else {
		
		str = '';
		
		if ( nParent.innerHTML != undefined ) { // Mozilla and WinMSIE6
	
			str = nParent.innerHTML;
	
		} else { 
			
			// TODO
		}
	}
	
	return str;
}

/**
 * 
 * 
 *
 */
function insertAdjacentTextAfterBegin( nParent, str ) {
	
	if ( nParent.insertAdjacentText ) { // IE
		
		nParent.insertAdjacentText( 'AfterBegin', str );
		
	} else { // Mozilla
		
		var textNode = document.createTextNode( str );
		
		if ( nParent.hasChildNodes() ) {
			
			nParent.insertBefore( textNode, nParent.firstChild );
		
		} else {
			
			nParent.appendChild( textNode );
		}
	}
}

/**
 * 
 * 
 *
 */
function insertAdjacentHTMLAfterBegin( nParent, str ) {
	
	if ( nParent.insertAdjacentHTML ) {
		
		nParent.insertAdjacentHTML( 'AfterBegin', str );
		
	} else {
	
		innerHTML( nParent, str + innerHTML( nParent ) );
	}
}

/**
 * 
 * 
 *
 */
function insertAdjacentTextBeforeEnd( nParent, str ) {
	
	if ( nParent.insertAdjacentText ) {
		
		nParent.insertAdjacentText( 'BeforeEnd', str );
		
	} else {
			
		nParent.appendChild( document.createTextNode( str ) );
	}
}

/**
 * 
 * 
 *
 */
function insertAdjacentHTMLBeforeEnd( nParent, str ) {
	
	if ( nParent.insertAdjacentHTML ) {
		
		nParent.insertAdjacentHTML( 'BeforeEnd', str );
		
	} else {
	
		innerHTML( nParent, innerHTML( nParent ) + str );
	}
}

/**
 * 
 * 
 *
 */
function removeChilds( nParent, deep ) {
	
	if ( nParent.nodeType == XML_ELEMENT_NODE ) {
	
		deep = ( deep ) ? true : false;
	
		var childNodes = nParent.childNodes;
		var lastIndexChildNodes = childNodes.length - 1;
		
		for ( var i=lastIndexChildNodes; i >= 0; i-- ) {
				
			if ( deep ) removeChilds( childNodes[i], deep );
			
			nParent.removeChild( childNodes[i] );
		}
	}
}

/**
 * 
 * ???
 *
 */
function removeDeep( nNode) {
	
	removeChilds( nNode, deep );
	if ( nNode.parentNode ) nNode.parentNode.removeChild( nNode );
}

/**
 * 
 * 
 *
 */
function remove( nNode ) {

	if ( nNode.parentNode ) nNode.parentNode.removeChild( nNode );
	//nNode = null; not work !!!
}

/**
 * 
 * 
 *
 */
function hasAttribute( nElement, attrName ) {
	
	var res = false;
	
	if ( nElement.nodeType && ( nElement.nodeType == XML_ELEMENT_NODE ) ) {
		
		if ( nElement.hasAttribute ) { // Mozilla
		
			res = nElement.hasAttribute( attrName );
		
		} else { // IE
			
			res = attrName in nElement;
		}
	}
	
	return res;
}

/**
 * 
 * 
 *
 */
function getAttribute( nElement, attrName ) {
	
	var res = undefined;
	
	if ( hasAttribute( nElement, attrName ) ) {
		
		if ( nElement.getAttribute ) { // Mozilla
		
			res = nElement.getAttribute( attrName );
		
		} else { // IE
			
			res = nElement.getAttribute[attrName];
		}
	}
	
	return res;
}

/**
 * 
 * 
 *
 */
function cursorHand( nElement ) {
	
	if ( document.getElementById ) {
	
		nElement.style.cursor = 'pointer'; // Mozilla and WinMSIE6
	
	} else {
		
		nElement.style.cursor = 'hand'; // old IE
	}
}

/**
 * 
 * 
 *
 */
function setCursor( nElement, cursor ) {
		
	nElement.style.cursor = cursor;
}

/**
 * 
 * 
 *
 */
function getEvent( event ) {
	
	return  ( event ) ? event : window.event;
}

/**
 * 
 * 
 *
 */
function getEventTarget( event ) {
	
	return ( event.target != undefined ) ? event.target : event.srcElement
}

/**
 * 
 * 
 *
 */
function getAllElements() {
	
	return ( document.all ) ? document.all : document.getElementsByTagName('*');
}

/**
 * 
 * 
 *
 */
function getKeyCode( event ) { //alert( event.keyCode ); //alert( event.which );
	
	return event.keyCode ? event.keyCode : ( event.which ? event.which : null );
}

/**
 * 
 * 
 *
 */
function functionFromName( functionName ) { //alert( functionName );
	
	var func = null;
	
	if ( functionName ) {
	
		var properties = functionName.split( '.' );  //alert( dump( properties ) );
			
		func = window;
		
		for ( var k=0; k<properties.length; k++ ) {
		
			func = func[ properties[k] ];
		}
	}
	
	//alert( func );
	
	return func;
}

/**
 * 
 * 
 *
 */
function getCurrentStyle( nElement, prop ) { 
	
	var propValue = '';
  
	// external stylesheet for Mozilla, Opera 7+ and Safari 1.3+ 
	if ( document.defaultView && document.defaultView.getComputedStyle ) { 
		
		if ( prop.match(/[A-Z]/) ) prop = prop.replace( /([A-Z])/g, '-$1' ).toLowerCase(); 
		propValue = document.defaultView.getComputedStyle( nElement, '' ).getPropertyValue(prop); 
	} 
  
	// external stylesheet for Explorer and Opera 9 
	if ( nElement.currentStyle ) { 
	    
	    var i; 
	    while ( (i=prop.indexOf('-'))!=-1 ) prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2); 
	    propValue = nElement.currentStyle[prop]; 
	} 
  
  return propValue; 
}

/**
 * 
 * PHP: round
 *
 */
Math.roundWithPrecision = function( value, precision ) {

	var mn = Math.pow( 10, precision );
	
	return Math.round( value * mn ) / mn;
}

/**
 * 
 * 0.7*0.75 == 0.53 !!!
 *
 */
Math.superRoundWithPrecision = function( value, precision ) {

	value = Math.roundWithPrecision( value, precision + 1 );
	value = Math.roundWithPrecision( value, precision );
	
	return value;
}

/**
 * 
 * PHP: sprintf( '%01.2f', $number ) -> 0.00 or 1.50
 *
 */
Math.floatToStrWithPrecision = function( number, precision ) {
	
	number = Math.superRoundWithPrecision( number, precision );
	var strNumber = number.toString();
	
	var dr = number - Math.floor( number );
	dr = Math.superRoundWithPrecision( dr, precision );
	
	var strDr = dr.toString(); //alert( strDr );
	var countDrFigures = strDr.substring( 2, strDr.length ).length; //alert( countDrFigures );
	
	var countNull = precision - countDrFigures;
	if ( precision && ( ! countDrFigures ) ) strNumber += '.';
	for ( var i=0; i<countNull; i++ ) strNumber += '0';
	
	return strNumber;
}

/**
 * 
 * 
 *
 */
function equals( v1, v2 ) {

	var eq = false;
	
	if ( typeof( v1 ) == typeof( v2 ) ) {
	
		if ( typeof( v1 ) == 'object' ) {
			
			if ( ( v1 instanceof Array ) && ( v2 instanceof Array ) ) {
			
				if ( v1.length == v2.length ) {
				
					var res = true;
					
					for ( var i=0; i<v1.length; i++ ) {
						
						if ( ! equals( v1[i], v2[i] ) ) {
							
							res = false;
							break;
						}
					}
					
					eq = res;
				}
			
			} else if ( ( v1 instanceof Boolean ) && ( v2 instanceof Boolean ) ) {
			
				eq = ( v1 == v2 );
			
			} else if ( ( v1 instanceof Function ) && ( v2 instanceof Function ) ) {
			
				eq = ( v1.toString() == v2.toString() );
			
			} else if ( ( v1 instanceof Date ) && ( v2 instanceof Date ) ) {
			
				// TODO
			
			} else if ( ( v1 instanceof Number ) && ( v2 instanceof Number ) ) {
			
				eq = ( v1 == v2 );
			
			} else if ( ( v1 instanceof String ) && ( v2 instanceof String ) ) {
			
				eq = ( v1 == v2 );
			
			} else {
				
				var res = true;
					
				for ( var i in v1 ) {
					
					if ( ! equals( v1[i], v2[i] ) ) {
						
						res = false;
						break;
					}
				}
				
				eq = res;
			}
			
		} else { // not 'object'
			
			eq = ( v1 == v2 );
		}
	
	}
	
	return eq;
}

/**
 * 
 */
Math.EPS = 1E-10;

 /**
 * 
 * The return value is 0 if the two operands are equal.
 * If the $leftOperand is larger than the $rightOperand the return value is +1 and
 * if the $leftOperand is less than the $rightOperand the return value is -1.
 * 
 */
Math.cmpFloat = function( leftOperand, rightOperand ) {
	
	var res = null;
	
	var difference = leftOperand - rightOperand;
	
	if ( Math.abs( difference ) < Math.EPS ) {
		
		res = 0;
		
	} else if ( difference > 0 ) {
		
		res = 1;
		
	} else {
		
		res = -1;
	
	}
	
	return res;
}

/**
 * 
 * 
 *
 */
function getScriptPath( scriptFileName ) {

	var scriptPath = undefined;
	
	var re = new RegExp( '^(.*\/)?'+scriptFileName+'$' );
	
	var scriptElements = document.getElementsByTagName('script');
	
	for ( var i = 0; i < scriptElements.length; ++i ) {
		
		var nScriptElement = scriptElements[i];
		
		var src = getAttribute( nScriptElement, 'src' );
    
		if ( src && src.match( re ) ) {
			
			scriptPath = RegExp.$1;
			break;
		}
	}
	
	return scriptPath;
}

/**
 * 
 * 
 *
 */
function loadScript( scriptPath, headElement ) {
	
	headElement = headElement || document.getElementsByTagName('head')[0];
		
	var scriptElement = document.createElement('script');
    
    scriptElement.type = 'text/javascript';
    scriptElement.src = scriptPaths[i];
    
    headElement.appendChild( scriptElement );
}

/**
 * 
 * 
 *
 */
function loadScripts( scriptPaths ) {
	
	var headElement = document.getElementsByTagName('head')[0];
	
	for ( var i = 0; i < scriptPaths.length; ++i ) {
		
		loadScript( scriptPaths[i], headElement );
    }
}

/**
 * 
 * 
 *
 */
loadedScripts = {};

/**
 * 
 * 
 *
 */
function include( scriptPath ) {
	
	if ( loadedScripts[scriptPath] == undefined ) {
		
		loadedScripts[scriptPath] = true;
		
		loadScript( scriptPath );
	}
}




