function NewsSection(sectionTitle){

 this.title = sectionTitle;
 this.itemsCount = 0;
 this.hasMore = false;
 this.items = new Array();
 this.moreLink = "";
 this.moreTitle = "";


}

NewsSection.prototype.addItems = function addI(itemsArray){
  this.items = itemsArray;
  this.itemsCount = itemsArray.length;
}
NewsSection.prototype.addMoreLink = function addM(moreUrl,moreLinkTitle){
  this.moreLink = moreUrl;
  this.moreTitle = moreLinkTitle;
  this.hasMore = true;
}

NewsSection.prototype.toHtml = function toH(){
    sectHead = "<div id=\"ecaNewsSection\">";
	sectTail = "</div>";
	sectStart = "<div id=\"ecaNewsLinks\"><table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">";

    sectionItemsText = "";
    sectionTitleDisplayText="<div id=\"ecaNewsSectionTitle\">" + this.title + "</div>";
    sectionEndWithMoreLink = "<tr><td id=\"moreRow\" colspan=2 ><span id=\"ecaNewsMore\" ><a href=\""+this.moreLink+"\" >"+this.moreTitle+"</a></span></td></tr></table></div>";
    sectionEndWithoutMoreLink = "<tr><td id=\"moreRow\" colspan=2 >&nbsp;</td></tr></table></div>";
	for(var i=0; i<this.itemsCount; i++){
           currentItem = this.items[i];
       	   sectionItemsText = sectionItemsText + "<tr><td id=\"newscol1\" vAlign= \"top\"   >"+currentItem['date']+"</td><td id=\"newscol2\" vAlign= \"top\"><a href=\""+currentItem['url']+"\">" + currentItem['title'] +"</a></td></tr>";
	}
	sectionListEnd = (this.hasMore) ? sectionEndWithMoreLink  : sectionEndWithoutMoreLink;
    sectionItemsText = sectHead + sectionTitleDisplayText + sectStart + sectionItemsText + sectionListEnd + sectTail;

    //if(this.hasMore)alert(this.moreTitle+"\n"+this.moreLink);
    //alert(this.title + "\nNumber of Items:" +  this.itemsCount);
    //alert(sectionItemsText);
    return sectionItemsText;


}


function ParseNews(htmlText){
var NewsPulls = new Array();
var MATCH_SUBGROUP_SECTIONTITLE = 1;
var MATCH_SUBGROUP_ITEMDATE = 1;
var MATCH_SUBGROUP_ITEMTITLE = 3;
var MATCH_SUBGROUP_ITEMURL = 2;
var MATCH_SUBGROUP_MOREURL = 1;
var MATCH_SUBGROUP_MORETITLE = 2;
sectionTitlePattern = "<table[^>]+><tr>[^<]*<td[^>]+>[^<]*<h1>([^<]+)</h1>[^<]*</td>[^<]*</tr>[^<]*(?:<tr>[^<]*<td[^>]+>[\\S\\s]*?</td>[^<]*</tr>)+";
sectionItemsPattern = "<table[^>]+>([^<]*<tr><td[^<]+>[^~]+?</td><td[^<]+><a[^>]+>[^<]+</a></td>[^<]*</tr>[^<]*)+</table>";
sectionMorePattern = "</td></tr></table><table[^>]+>[^<]*(?:<tr>[^<]*<td>[^<]*</td>[^<]*<td>[^<]*</td>[^<]*</tr>)*[^<]*<tr>[^<]*<td width=\"90%\">[^<]*</td>[^<]*<td[^>]+><a[^>]+?href=\"([^\"]+)\">([^<]+)[\\s]*</a></td>[^<]*</tr>[^<]*</table>";
sectionCombinedPattern = sectionItemsPattern + "(?:"+ sectionMorePattern + ")*";
itemPattern = "<td(?!\\swidth=\"90%\")[^<]+>(?:<acronym[^>]+>)*([^~]+?)(?:</acronym>)*</td><td[^<]+><a[^>]+?href=\"([^\"]+)\">([^<]+)</a></td>[^<]*";
sectionMorePattern2 = "<tr>[^<]*<td width=\"90%\">[^<]*</td>[^<]*<td[^>]+><a[^>]+?href=\"([^\"]+)\">([^<]+)[\\s]*</a></td>[^<]*</tr>";
var sectionRegex = new RegExp(sectionCombinedPattern, "gim");
var titleRegex = new RegExp(sectionTitlePattern, "gim");
var itemRegex = new RegExp(itemPattern,"gim");
var moreRegex = new RegExp(sectionMorePattern2,"gim");
var titlesMatch = titleRegex.exec(htmlText);
var numberOfSections;
while(titlesMatch != null){
     numberOfSections = titlesMatch.length
     sTitle = titlesMatch[MATCH_SUBGROUP_SECTIONTITLE];
     if(sTitle != null){
	    var currentItem,itemMatch,newsItems;
	    nSection = new NewsSection(sTitle);
	    NewsPulls.push(nSection);
     }
     titlesMatch = titleRegex.exec(htmlText);
}

for(var n=0; n<numberOfSections; n++ ){

  //news items

  var sectionsFound = htmlText.match(sectionRegex);
  if(sectionsFound!=null){
    var currentItem, newsItems, itemMatch,moreLinkMatch,s;
    s="";
    //alert("sections count: "+sectionsFound.length);
    for(var j=0; j<sectionsFound.length; j++){
       sectionText = sectionsFound[j];
       itemMatch = itemRegex.exec(sectionText);
       newsItems = new Array();
       while(itemMatch != null){
          currentItem = new Array();
          currentItem['date'] = itemMatch[MATCH_SUBGROUP_ITEMDATE];
          currentItem['title'] = itemMatch[MATCH_SUBGROUP_ITEMTITLE];
          currentItem['url'] = itemMatch[MATCH_SUBGROUP_ITEMURL];
          newsItems.push(currentItem);
          //alert( currentItem['date'] + "\n" + currentItem['title'] + "\n" + currentItem['url']);
          itemMatch = itemRegex.exec(sectionText);
       }  //while
       nSection = NewsPulls[j];
       nSection.addItems(newsItems);
       moreLinkMatch = moreRegex.exec(sectionText);
       if(moreLinkMatch!=null){
          nSection.addMoreLink(moreLinkMatch[MATCH_SUBGROUP_MOREURL],moreLinkMatch[MATCH_SUBGROUP_MORETITLE]);
          
       }

    }  //for (j=0...
    //alert(s);
  } //if sectionsFound


}  //if(titlesFound!=null)

return NewsPulls;

}

var xmlhttp;
function loadNews(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
      htmlText = xmlhttp.responseText
      var newsSections = ParseNews(htmlText);
      //alert(newsSections.length);
	  var newsHtml = "";
      for(k=0;k<newsSections.length;k++){
         currentSection = newsSections[k];
         newsHtml += currentSection.toHtml();
      }
      targetElem = document.getElementById("ecaNewsList");

	  if(targetElem!=null){
	    targetElem.innerHTML = newsHtml;
	    closeDefaultNewsWidget();
      }


    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}
function getParentElem(targetElem, numberOfLevels){
   var newTarget=targetElem;
   for(var i=0; i<numberOfLevels; i++){
      newTarget = newTarget.parentNode;
   }
   return newTarget;
}
function getNearestContainer(targetElem, targetName){
   var newTarget=targetElem;
   maxLevels = 4;
   for(var i=0; i<maxLevels; i++){
      newTarget = newTarget.parentNode;
      newTargetTagName = newTarget.tagName;
      if(newTargetTagName.toLowerCase() == targetName.toLowerCase())break;
   }
   return newTarget;
}
function closeDefaultNewsWidget(){
  var sectionMarker = document.getElementsByTagName("td");
  for (var i = 0; i < sectionMarker.length; i++) {
    targetElem = sectionMarker[i];
    if(targetElem.className == "pagetitleUnderLine"){
        elemToClose = getNearestContainer(targetElem,"table");
        elemToClose.style.display = "none";
        //alert("elemtoclose:"+elemToHide.tagName)
        //alert("found td=pagetitleUnderline");
    }else if(targetElem.className=="pagetitleLineBlue"){
        elemToClose = getNearestContainer(targetElem,"table");
        elemToClose.style.display = "none";

    }else if(targetElem.getAttribute("width") == "90%"){
        elemToClose = getNearestContainer(targetElem,"table");
        if(elemToClose!=null)elemToClose.style.display = "none";
    }
  }
}
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
	    window.onload = func; 
	} else { 
	    window.onload = function() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    } 
	  } 
}
try{
    tgt = "http://web.worldbank.org/WBSITE/EXTERNAL/COUNTRIES/ECAEXT/POLANDEXTN/0,,menuPK:304800~pagePK:141159~piPK:141110~theSitePK:304795,00.html";
    loadNews(tgt);
    throw "NEWSLOADED";
 
}catch(err){
    if(err == "NEWSLOADED"){
       addLoadEvent(closeDefaultNewsWidget);
    }
        
}

