/*##################################################*/
/*######## LOAD PROFILES FROM XML DATA FILE ########*/
/*##################################################*/
var xmlDoc;

function AJAX_load(path)      
{       
	try        
	{ // MSIE                
	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");        
	}       
	catch(e)       
	{               
		try               
		{ // Firefox, Opera, etc.                        
		var xmlDoc=document.implementation.createDocument("","",null);               
		}               
		catch(e)              
		{                      
		alert(e.message)             
		}        
	}       
	try       
	{                
		xmlDoc.async = false;                
		xmlDoc.load(path);       
	}
	catch(e)        
	{ // unable to load                
		try               
		{   // for Safari                        
			xmlDoc = new XMLHttpRequest();                        
			xmlDoc.open("GET", path, false);                       
			xmlDoc.send();                       
			xmlDoc=xmlDoc.responseXML;                                       
		}                
		catch(e)                
		{                        
			alert(e.message)                       
			return; // return with nothing                
		}        
	}        
return xmlDoc;
}

xmlDoc = AJAX_load("xml/profiles.xml");

var x = xmlDoc.getElementsByTagName("PROFILE");

function shuffle(o)
{
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function displayRandomProfiles(pType)
{
	profileIndex = new Array();
	
	if(pType == "ep") //EXPERIENCED PROFESSIONALS
	{	profileIndex = shuffle([2, 3, 6, 7, 8, 10, 18, 19]); }
	
	if(pType == "up") //UNIVERSITY PROFESSIONALS
	{	profileIndex = shuffle([0, 1, 4, 9, 12, 14, 15, 17, 20]); }
	
	var profileName, profileImage, profileLocation, profileLinkId;
	var tbl = "";
	tbl += "<table border='0' cellpadding='0' cellspacing='2'><tr>";
	for(var i=0; i<4; i++)
	{
		try
		{
		profileName = x[profileIndex[i]].getElementsByTagName("NAME")[0].childNodes[0].nodeValue;
		profileImage = x[profileIndex[i]].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
		profileLocation = x[profileIndex[i]].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue;
		profileLinkId = x[profileIndex[i]].getElementsByTagName("LINKID")[0].childNodes[0].nodeValue;
				
		tbl += "<td valign='top'>";
		tbl += "<a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'><img src='images/"+profileImage+"' border='0' width='63' height='63'></a><br>";
		tbl += "<a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'>"+ profileName +"</a><br>"+profileLocation+"<br>";
		tbl += "<a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'><img src='images/icon_video.gif' border='0' width='16' height='13'></a>";
		tbl += "</td>";	
		}
		catch(e)
		{}
	}
		tbl += "</tr></table>";
	document.write(tbl);
}

var counter = 0;
var flag = 0;
var pRegion, pCity = null;
var page = 0;

function next(data)
{
	flag = 0;
	if(counter < data)
	{ displayProfileGrid(); }
}

function previous()
{
	flag = 1;
	if (counter > 0)
	{ displayProfileGrid(); }
}

function displayProfileGrid()
{
	var profileName, profileImage, profileDesignation, profileDepartment, profileLocation, profileLinkId, profileR, profileVideo;
	var tbl = "";
	
	if(pRegion == "all")
	{ profileR = x;	}
	else
	{ profileR = getRegionProfile(pRegion);	}
		
	if(flag == 1)
	{
		counter = counter - 6; page = page - 6;
		if(counter < 0) { counter = 0;}
		if(page < 0) { page = 0; }
	}
	
	tbl += "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='profileTable'><tr>"
	tbl += "<td width='100%' valign='middle' class='hp_gridHeader'>MEET OUR EMPLOYEES</td></tr>"
	tbl += "<tr><td class='hp_gridBody' valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='0'>"
	
	for (var i = 0; i<3; i++)
	{
		try{
		profileName = profileR[counter].getElementsByTagName("NAME")[0].childNodes[0].nodeValue;
		profileImage = profileR[counter].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
		profileDesignation = profileR[counter].getElementsByTagName("DESIGNATION")[0].childNodes[0].nodeValue;
		profileDepartment = profileR[counter].getElementsByTagName("DEPARTMENT")[0].childNodes[0].nodeValue;
		profileLocation = profileR[counter].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue;
		profileLinkId = profileR[counter].getElementsByTagName("LINKID")[0].childNodes[0].nodeValue;
		
		tbl += "<tr><td width='23%' valign='top' class='profile_thumbnail'><a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'><img src='images/"+profileImage+"' width='63' height='63' border='0'></a></td>"
		tbl += "<td width='77%' valign='top' class='profile_detail'>"
		
		try	{ profileVideo = profileR[counter].getElementsByTagName("VIDEO")[0].childNodes[0].nodeValue;
			tbl += "<a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'><img src='images/icon_video.gif' width='16' height='13' border='0' align='right'></a>" }
		catch(e){}
		
		tbl += "<strong><a href='/about/careers/culture/profiles/index.html?id="+profileLinkId+"'>"+profileName+"</strong></a><br>"+profileDesignation+"<br>"+profileDepartment+", "+profileLocation+"</td>"		
		tbl += "</tr>"
		
		}
		catch(e)
		{}
		page++;
		counter++;
	}
	tbl += "</table></td></tr><tr>"
	tbl += "<td class='hp_gridFooter' valign='middle'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr>"
	tbl += "<td width='3%'><strong><img src='images/bullet_red.gif' width='9' height='8' border='0'></strong></td>"
	tbl += "<td width='68%'><strong><a href='/about/careers/culture/profiles/index.html'>View all our people</a></strong> </td>"
	
	if(page > profileR.length)
	tbl += "<td width='17%' class='grey' align='right'><strong>"+profileR.length+"/"+profileR.length+"</strong>&nbsp;</td>"
	else
	tbl += "<td width='17%' class='grey' align='right'><strong>"+page+"/"+profileR.length+"</strong>&nbsp;</td>"
	
	tbl += "<td width='12%' align='center' nowrap><input type='image' src='images/pagination_left.gif' width='7' height='13' border='0' id='leftArrow' onClick='previous();'> <input type='image' src='images/pagination_right.gif' width='7' height='13' border='0' id='rightArrow' onClick='next(\""+profileR.length+"\");'>"
	tbl += "&nbsp;</td></tr></table></td></tr></table>"
	
	document.getElementById("profileholder").innerHTML = tbl;
	
	if(counter <= 3)
	{
		document.getElementById("leftArrow").disabled = true;
		document.getElementById("leftArrow").src = "images/pagination_left_disable.gif";
	}
	if(counter >= profileR.length)
	{
		document.getElementById("rightArrow").disabled = true;
		document.getElementById("rightArrow").src = "images/pagination_right_disable.gif";
	}
}

function getRegionProfile(region)
{
	var profileRegion;
	profileArray = new Array();
	var j = 0;
	if(pCity != null)
	{
		for (var i = 0; i<x.length; i++)
		{ 
			profileRegion = x[i].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue;
			if(pCity == profileRegion)
			{
				profileArray[j] = x[i];
				j++;
			}
		}		
	}
	else
	{
		for (var i = 0; i<x.length; i++)
		{
			profileRegion = x[i].getElementsByTagName("REGION")[0].childNodes[0].nodeValue;
			if(region == profileRegion)
			{
				profileArray[j] = x[i];
				j++;
			}
		}
	}
	return profileArray; 
}
