function UNICEFFootnote() {
	this.media = 'screen';
	this.addClass = function (DOMObj, classname) {
		if ( DOMObj.className != '' ) {
			DOMElementObj.className += ' ' + classname;
			return;
		}
		DOMObj.className = classname;
	}
	this.inArray = function (haystack, needle) {
  		for ( var i = 0; i < haystack.length; i++ ) {
    		if ( haystack === needle ) {
      		return i;
    		}
		}
  		return false;
	}
	this.forPrint = function (candidate) {
    	if ( candidate.getAttribute('href') || candidate.getAttribute('cite') ) {
			if ( candidate.getAttribute('href') ) {
				if ( candidate.getAttribute('href').indexOf("mailto") != -1 ) {
					return false;
				}
				if ( candidate.getAttribute('href').indexOf(".ram") != -1 ) {
					return false;
				}
				if ( candidate.getAttribute('href').indexOf("javascript:void(0)") != -1 ) {
					return false;
				}
				if ( candidate.getAttribute('href').indexOf("q=printme") != -1 ) {
					return false;
				}
				if ( candidate.getAttribute('href') == '#' ) {
					return false;
				}
				if ( candidate.getAttribute('href') == 'http://www.unicef.org' ) {
					return false;
				}
				if ( candidate.getAttribute('href') == 'http://www.unicef.org/' ) {
					return false;
				}
				if ( candidate.getAttribute('href') == '/' ) {
					return false;
				}
			}
			if ( candidate.className == '' || candidate.className == 'mailto' || candidate.className.indexOf('ignore') == -1 ) {
				return true;
			}
		}
		return false;
	}
	this.init = function () {
		this.media = 'print';
	}
	this.write = function (containerID, targetID) {
		if ( ! (this.media == 'print') ) {
			return false;
		}

		var selected = new Array;
		var containerObj = document.getElementById(containerID);
		var targetObj = document.getElementById(targetID);

  		if ( ! containerObj || ! targetObj ) {
			return false;
		}
  	
		var h2Obj = document.createElement('h2');
		var h2TxtObj = document.createTextNode('Links');
		this.addClass(h2Obj, 'printOnly');
		h2Obj.appendChild(h2TxtObj);

		var aCandidates = containerObj.getElementsByTagName('A');
		var olObj = document.createElement('ol');
		this.addClass(olObj, 'printOnly');
	
		for ( var i = 0; i < aCandidates.length; i++ ) {
			if ( this.forPrint(aCandidates[i]) ) {
      		var citeNum = '1';
				var linkTxt = aCandidates[i].getAttribute('href') ? aCandidates[i].href : aCandidates[i].cite;
				var arrIndex = this.inArray(selected, linkTxt);

				if ( arrIndex || arrIndex === 0 ) {
					citeNum = document.createTextNode(arrayIndex + 1);
				}
				else {
					selected[selected.length] = linkTxt;
					citeNum = document.createTextNode(selected.length);
        			var liObj = document.createElement('li');
        			var liTxt = document.createTextNode(linkTxt);
        			liObj.appendChild(liTxt);
        			olObj.appendChild(liObj);
				}
      		
				var noteObj = document.createElement('sup');
				this.addClass(noteObj, 'printOnly');
      		noteObj.appendChild(citeNum);
      
				if ( aCandidates[i].tagName.toLowerCase() == 'blockquote' ) {
					var lastChild = this.lastChildContainingText(aCandidates[i]);
        			lastChild.appendChild(noteObj);
				}
				else {
					aCandidates[i].parentNode.insertBefore(noteObj, aCandidates[i].nextSibling);
				}
			}	
		}
  		
		targetObj.appendChild(h2Obj);
		targetObj.appendChild(olObj);
		this.addClass(document.getElementsByTagName('html')[0], 'noted');
  		return true;
	}
	this.lastChildContainingText = function (DOMObj) {
		var lastChild = DOMObj.lastChild;
  		var contentCntnr = ['p','li','dd'];
  		while ( lastChild.nodeType != 1 ) {
			lastChild = lastChild.previousSibling;
		} 
  		
		var tag = lastChild.tagName.toLowerCase();
  		var tagInArr = this.inArray(contentCntnr, tag);
  		if ( ! tagInArr && tagInArr !== 0 ) {
    		lastChild = lastChildContainingText(lastChild);
  		}
  		return lastChild;
	}
	this.popUp = function (strURL, strType, strWidth, strHeight) {  
		var newWin = null;
		if ( newWin != null && ! newWin.closed ) newWin.close();  
 		
		var strOptions="";  
 		if ( strType=="fixednooptions" ) strOptions="height="+strHeight+",width="+strWidth; 
 		if ( strType=="resizablenooptions" ) strOptions="resizable,height="+strHeight+",width="+strWidth;
		if ( strType=="fixedscroll" ) strOptions="scrollbars,"+"height="+strHeight+",width="+strWidth;  
		if ( strType=="fixedmenuscroll" ) strOptions="menubar,scrollbars,"+"height="+strHeight+",width="+strWidth;  
		if (strType=="resizablemenuscroll") strOptions="menubar,scrollbars,"+"resizable,height="+strHeight+",width="+strWidth;
		if (strType=="fixedtoolsmenuscroll") strOptions="toolbar,menubar,scrollbars,"+"height="+strHeight+",width="+strWidth;  
		if (strType=="resizabletoolsmenuscroll") strOptions="toolbar,menubar,scrollbars,"+"resizable,height="+strHeight+",width="+strWidth;
		if (strType=="fixedtoolslocationmenuscroll") strOptions="toolbar,menubar,scrollbars,"+"location,height="+strHeight+",width="+strWidth;  
		if (strType=="resizabletoolslocationmenuscroll") strOptions="toolbar,menubar,scrollbars,"+"resizable,location,height="+strHeight+",width="+strWidth;
		if (strType=="resizablelocationmenuscroll") strOptions="menubar,scrollbars,"+"resizable,location,height="+strHeight+",width="+strWidth;
		if ( strType=="printerfriendlypage" ) strOptions="toolbar,menubar,scrollbars,"+"resizable,location,height="+strHeight+",width="+strWidth;
 
		newWin = window.open(strURL, 'newWin', strOptions);  
		newWin.focus();
	}
}
var UNICEFFOOTNOTE = new UNICEFFootnote();

