var str="";
var browser="";
function importXML(loc, type)
{
	file="news.xml";
	if (document.implementation && document.implementation.createDocument)
	{
		//alert("You are using W3 compatible browser");		
		browser="w3";
		xmlDoc = document.implementation.createDocument("", "", null);			
		xmlDoc.load(file);
		xmlDoc.onload=function () {
			if (type =="ProgChange" || type =="Other")			
			{
				getData("ProgChangeArea","ProgChange")
				getData("OtherArea","Other")
			}
			else
			{
				getData(loc, type);
			}
		}
	}
	else if (window.ActiveXObject)
	{
		//alert("You are using MSIE");
		browser="ie";
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");	
		xmlDoc.async=false;		
		xmlDoc.load(file);
		getData(loc, type);
 	}
	else
	{
		alert('Your browser can\'t handle this script\nPlease use either IE6.0 or Netscape 8.0 or above');
		return;
	}
}

function getData(loc, type)
{
	if (type=="All")
	{
		if (browser=="w3") clearXML();
		highlight();
	}
	else
	{
		if (browser=="w3") clearXML();
		createTable(type);
	}
	//alert(str);
	document.getElementById(loc).innerHTML=str;
	return str;
	
}
function clearXML()
{
//Clear Empty node (Netscape only)
	var x = xmlDoc.getElementsByTagName('NItem');
	//alert(x.length);	
	for (i=0;i<x.length;i++)
	{
		//alert(i + "Initial:" + x[i].childNodes.length);
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) x[i].removeChild(x[i].childNodes[j]);
		}
		//alert(i + "Final:" + x[i].childNodes.length);
	}
}

function highlight()
{
	var x = xmlDoc.getElementsByTagName('NItem');


	(x.length>3) ? Max=3:Max=x.length;
	for (i=0;i<Max;i++)
	{		
		str=str + "<a href=" + x[i].childNodes[1].getAttribute("link") + ">";
		str=str + "" + x[i].childNodes[1].firstChild.nodeValue;
		str=str + "</a>"; 
		str=str + "<br><br>"; 
	}

}
function createTable(itype)
{
	str="";	
	var x = xmlDoc.getElementsByTagName('NItem');
	str=str + ("<table width=100% border=0 cellpadding=5 cellspacing=1>");
	str=str + ("<tr valign=middle bgcolor=#C0E6EE height=27>");
	str=str + ("<th width=18% nowrap><font size=2>Date Posted</font></th>");
	str=str + ("<th width=38%><font size=2>Subject</font></th>");
	str=str + ("<th width=14%><font size=2>Applicants Targeted</font></th>");
	str=str + ("<th width=12%><font size=2>Mail Attached</font></th>");
	str=str + ("<th width=16%><font size=2>Remarks (if appropriate)</font></th>");
	str=str + ("</tr>");
	for (i=0;i<x.length;i++)
	{		
		//alert(x[i].getAttribute("type"))
		if ((x[i].getAttribute("type"))==itype)
		{
			str=str + ("<tr valign='top' bgcolor='#F9F3CE'>");
			//alert(x[i].childNodes.length);
			for (j=0;j<5;j++)	//Limit to 5 fields
			//for (j=0;j<x[i].childNodes.length;j++)
			{
				if (x[i].childNodes[j].nodeType != 1) continue;
				 if ((x[i].childNodes[j].nodeName)=="Subject") 
				 {
					str=str + "<td>";	 
				 }
				 else if ((x[i].childNodes[j].nodeName)=="Date")
				 {str=str + "<td align=left>";	 
				 }
				 else
				 {
					 str=str + ("<td align=center>");
				 }
				if (x[i].childNodes[j].hasChildNodes())						
				{
				//	if (x[i].childNodes[j].getAttribute("link")) str=str + ("<a href=" + x[i].childNodes[j].getAttribute("link") + ">");
					if (x[i].childNodes[j].getAttribute("link")) str=str + "<a href=" + x[i].childNodes[j].getAttribute("link") + " target=" + x[i].childNodes[j].getAttribute("target") +">";
					str=str + (x[i].childNodes[j].firstChild.nodeValue);				
					if (x[i].childNodes[j].getAttribute("link")) str=str + ("</a>");
				}
				else
				{
					str=str + ("-");
				}			
				str=str + ("</td>");
			}
			str=str + ("</tr>");
		}		
	}
	str=str + ("</table>");
}


//Group 1 Popup for applicant change programme
function importXMLPopUp(type, path)
{
	file=path + "news.xml";
	if (document.implementation && document.implementation.createDocument)
	{
		//alert("You are using W3 compatible browser");		
		browser="w3";
		xmlDoc = document.implementation.createDocument("", "", null);			
		xmlDoc.load(file);
		xmlDoc.onload=function () {
			if (type =="ProgChange")			
			{			   
				getPopData("ProgChange")
			}
			else
			{

			}
		}
	}
	else if (window.ActiveXObject)
	{
		//alert("You are using MSIE");
		browser="ie";
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");	
		xmlDoc.async=false;		
		xmlDoc.load(file);
		getPopData(type, path);
 	}
	else
	{
		alert('Your browser can\'t handle this script\nPlease use either IE6.0 or Netscape 8.0 or above');
		return;
	}
}

function getPopData(type, path)
{
  	str="";
  	pCode=new Array();
    var x = xmlDoc.getElementsByTagName('NItem');  
    for (i=0;i<x.length;i++)
    {		
      if (x[i].getAttribute("type")==type)  
      {
		    temp=new String(x[i].getAttribute("code"));		    
		    tempA=temp.split(",");	
        pCode=pCode.concat(tempA);	              
      }
		}
		pCode.sort();
		i1=new Array();
		i2=new Array();
		i3=new Array();
		i4=new Array();
		i5=new Array();
		i6=new Array();
		i7=new Array();
		i8=new Array();
		iJ=new Array();
		i9=new Array();
		
    for (i=0;i<pCode.length;i++)
    {
      (eval("i" + pCode[i].substr(0,1))).push(pCode[i]);
    } 
    
	for (var i = 0; i < i4.length; i++){
       if (i4[i] == 4276) {delete i4[i];
	   break;}
         
     }     


    str="";
            
    str=str + loopIAU("CityU", path + "chg_cityu.htm", i1);
    str=str + loopIAU("HKBU", path + "chg_hkbu.htm", i2);
    str=str + loopIAU("PolyU", path + "chg_polyu.htm", i3);
    str=str + loopIAU("CUHK", path + "chg_cuhk.htm", i4);
    str=str + loopIAU("HKUST", path + "chg_hkust.htm", i5);
    str=str + loopIAU("HKU", path + "chg_hku.htm", i6);
    str=str + loopIAU("LU", path + "chg_lu.htm", i7);
    str=str + loopIAU("HKIEd", path + "chg_hkied.htm", i8);
    str=str + loopIAU("Joint Programmes", path + "chg_lu.htm", iJ); 
    str=str + loopIAU("OUHK", path + "chg_ouhk.htm", i9);
        
    
    document.writeln(str);    
}

function loopIAU(iIAU, iFile, iA)
{
  rV="";

  if (iA.length > 0)
  {    
    rV=rV + "<tr><td>" + iIAU +"</td><td>:</td><td>";
    for (i=0;i<iA.length;i++)
    {
      //rV=rV + iA[i];
	    if(iA[i] != undefined)
      rV=rV + "<a href=" + iFile + "#" + iA[i] + " target=_blank>" + iA[i] + "</a>, ";
    }   
    rV=rV.substr(0,rV.length-2);    
    rV=rV + "</td></tr>\n"
    return rV;
  }
  return rV;
}

//Group 2 Popup for mail archive
function importXMLPopUp2(type, path)
{
	file=path + "news.xml";
	if (document.implementation && document.implementation.createDocument)
	{
		//alert("You are using W3 compatible browser");		
		browser="w3";
		xmlDoc = document.implementation.createDocument("", "", null);			
		xmlDoc.load(file);
		xmlDoc.onload=function () {
			if (type =="ProgChange")			
			{			   
				getPopData2("ProgChange")
			}
			else
			{

			}
		}
	}
	else if (window.ActiveXObject)
	{
		//alert("You are using MSIE");
		browser="ie";
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");	
		xmlDoc.async=false;		
		xmlDoc.load(file);
		getPopData2(type, path);
 	}
	else
	{
		alert('Your browser can\'t handle this script\nPlease use either IE6.0 or Netscape 8.0 or above');
		return;
	}
}

function getPopData2(type, path)
{
  	str="";
  	aCode=new Array();
  	pCode=new Array();
    var x = xmlDoc.getElementsByTagName('NItem');  
    for (i=0;i<x.length;i++)
    {		
      if (x[i].getAttribute("type")==type)  
      {
      /*
		    temp=new String(x[i].getAttribute("code"));		    
		    tempA=temp.split(",");	
        pCode=pCode.concat(tempA);
        temp=new String(x[i].getAttribute("schma"));		    	  
        pCode=pCode.concat(temp);
      */
        temp=new String(x[i].getAttribute("code")) + "," + new String(x[i].getAttribute("schma"));
        aCode=aCode.concat(temp);
      }
		}
		aCode.sort();
		for (i=0;i<aCode.length;i++)		
    {
      tempA=aCode[i].split(",");	
      pCode=pCode.concat(tempA);
    }
		i1=new Array();
		i2=new Array();
		i3=new Array();
		i4=new Array();
		i5=new Array();
		i6=new Array();
		i7=new Array();
		i8=new Array();
		iJ=new Array();
		i9=new Array();
		
    for (i=0;i<pCode.length;i++)
    {
      (eval("i" + pCode[i].substr(0,1))).push(pCode[i]);
    } 
    
    str="";
            
    str=str + loopIAU2("CityU", path + "chg_cityu.htm", i1);
    str=str + loopIAU2("HKBU", path + "chg_hkbu.htm", i2);
    str=str + loopIAU2("PolyU", path + "chg_polyu.htm", i3);
    str=str + loopIAU2("CUHK", path + "chg_cuhk.htm", i4);
    str=str + loopIAU2("HKUST", path + "chg_hkust.htm", i5);
    str=str + loopIAU2("HKU", path + "chg_hku.htm", i6);
    str=str + loopIAU2("LU", path + "chg_lu.htm", i7);
    str=str + loopIAU2("HKIEd", path + "chg_hkied.htm", i8);
    str=str + loopIAU2("Joint Programmes", path + "chg_lu.htm", iJ); 
    str=str + loopIAU2("OUHK", path + "chg_ouhk.htm", i9);
        
    
    document.writeln(str);    
}

function loopIAU2(iIAU, iFile, iA)
{
  rV="";
  
  if (iA.length > 0)
  {    
    rV=rV + "<tr bgcolor=\"#F9F3CE\"><td>" + iIAU +"</td><td>:</td><td>";
    for (i=0;i<iA.length;i++)
    {
      if ((iA[i].length)==4 || (iA[i].length)==6)
      {
        rV=rV + "<a href=" + iFile + "#" + iA[i] + " target=_blank>" + iA[i] + "</a>, ";
      }
      else
      {
        if (iA[i].length >6)
        {
          rV=rV.substr(0,rV.length-2);  
          rV=rV + " <a href=" + iA[i].substr(2) + " target=_blank>(Letter)</a>; ";
        }
      }
    }   
    rV=rV.substr(0,rV.length-2);    
    rV=rV + "</td></tr>\n"
    return rV;
  }
  return rV;
}
