var notIeXml = (document.implementation && document.implementation.createDocument);
var doc = null;

function getDateIndex( date ) {
	var tmp = date.split( "-" );
	var datetmp = new Date();
	datetmp.setFullYear(  parseInt( tmp[ 0 ], 10 ) , parseInt( tmp[ 1 ], 10 ) - 1 , parseInt( tmp[ 2 ], 10 ) );

	return getDateIndex2( datetmp, 0, doc.childNodes.length - 1 );
}

function getDateIndex2( date, from, to ) {
	if ( from ==  to )
		return from;

	var index = Math.floor( ( to - from ) / 2 ) + from;


	
	var tmp			= ( doc.childNodes[ index ].childNodes[ 0 ].textContent ) ?
		doc.childNodes[ index ].childNodes[ 0 ].textContent.split( "-" )
		: doc.childNodes[ index ].childNodes[ 0 ].text.split( "-" );
	var datetmp = new Date();
	datetmp.setFullYear(  parseInt( tmp[ 0 ], 10 ) , parseInt( tmp[ 1 ], 10 ) - 1 , parseInt( tmp[ 2 ], 10 ) );

	//debug( date.toLocaleDateString() + ' ? ' + datetmp.toLocaleDateString() );

	if ( date.toLocaleDateString() == datetmp.toLocaleDateString() )
		return index;
	
	if ( Math.floor( ( to - from ) / 2 ) == 0 ) {
		var tmp2			= ( doc.childNodes[ to ].childNodes[ 0 ].textContent ) ?
			doc.childNodes[ to ].childNodes[ 0 ].textContent.split( "-" )
			: doc.childNodes[ to ].childNodes[ 0 ].text.split( "-" );
		var datetmp2 = new Date();
		datetmp2.setFullYear(  parseInt( tmp2[ 0 ], 10 ) , parseInt( tmp2[ 1 ], 10 ) - 1 , parseInt( tmp2[ 2 ], 10 ) );
		if ( date.toLocaleDateString() == datetmp2.toLocaleDateString() )
			return to;
	}

	if ( datetmp > date ) {
		if ( index == to )
			return index;
		return getDateIndex2( date, from, index );
	}
	if ( datetmp < date ) {
		if ( index == from )
			return index;
		return getDateIndex2( date, index, to );
	}

	return null;
}

var req;

function loadXMLDoc(url)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange()
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			doc  = req.responseXML.documentElement;
			var notWhitespace	= /\S/;
			var childNode		= null;

			for ( var x = 0; x < doc.childNodes.length; x++ ) {
				childNode = doc.childNodes[ x ];
				if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
					// that is, if it's a whitespace text node
					doc.removeChild( doc.childNodes[ x ] );
					x--
				}
			}

			onXMLLoadDone();
		}
		else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
    }
	
}

function onPrint() {
	var html = '<!DO'+'CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
		+'<ht'+'ml xmlns="http://www.w3.org/1999/xhtml">'
		+'<he'+'ad>'
		+'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />'
		+'<title>Citi Handlowy - Kurs akcji Banku Handlowego</title>'
		+'<link rel="STYLESHEET" type="text/css" href="/poland/homepage/polish/main.css" />'
		+'<style media="all" type="text/css">'
		+"\n"+'.r1 { border-left:1px solid #99CDE5; border-top:1px solid #99CDE5; background: none;}'
		+"\n"+'.r2 { border-right:1px solid #99CDE5; border-top:1px solid #99CDE5;  background: none;}'
		+"\n"+'.r3 { border-left:1px solid #99CDE5; border-bottom:1px solid #99CDE5;  background: none;}'
		+"\n"+'.r4 { border-right:1px solid #99CDE5; border-bottom:1px solid #99CDE5;  background: none;}'
		+'</style>'
		+'</he'+'ad>'
		+'<bo'+'dy onload="window.print();"><div style="width: 569px;">';

	html +='<strong>Kurs akcji Banku Handlowego w Warszawie S.A., stan na: ' + date_from + ' - ' +date_to + '</strong><br/><br/>';

	html += document.getElementById( 'table').innerHTML;

	html += '</div></bo'+'dy>'
		+'</ht'+'ml>';


	var win2 = window.open('', 'Printing');

	if ( navigator.userAgent.toString().toLowerCase().search( "msie" ) != -1 ) {
		win2.document.open();
		win2.document.write( html );
		win2.document.close();
	}
	else {
		win2.document.open();
		win2.document.write( html );
		win2.document.close();
		win2.focus();
	}
}

function sh(divek) {
	txt =document.getElementById(divek+"_txt");
	div=document.getElementById(divek);
	if(div.style.display=='block') {
		div.style.display='none';
		if(txt) {
			txt.innerHTML="rozwiń";
			txt.className="karty_rozwin_txt";
		}
	}
	else {
		div.style.display='block';
		if(txt)	{
			txt.innerHTML="zwiń";
			txt.className="karty_zwin_txt";
		}
	}
}

