//Call on the forward button click 
function forward()
{
     //Get Index value.
     var index=parseInt(document.getElementById("index").value);
     
     //Get Max Value
     var count=parseInt(document.getElementById("maxcount").value);
    
     //Increase the value by 1, if reach at Max, index value set to 0.
     if (index == count - 1)
     {
         index = 0;
     }
     else
     {
          index = index + 1;
     }  
     
     //To call the Callserver.
     GetPageContents(index);            
}

//Call on the Back button click
function backward()
{
    //Get Index value.
    var index=parseInt(document.getElementById("index").value);
    
    //Get Max Value
    var count=parseInt(document.getElementById("maxcount").value);

    //Decrease the value by 1, if reach at 0, index value set to max.
    if (index == 0)
    {
         index  = count - 1;
    }
    else
    {
         index  = index - 1;
    }           
     
    //To call the Callserver. 
    GetPageContents(index);            
}

//To Call the Callserver method
function GetPageContents(index)
{   
    CallServer(index, "");
}

//Get the value from sever side method in the form of XML
function ReceiveServerData(rValue)
{        
     var doc;
     // code for IE
     if (window.ActiveXObject)
     {
           doc=new ActiveXObject("Microsoft.XMLDOM");
           doc.async="false";
           doc.loadXML(rValue);
     }
     // code for Mozilla, Firefox, Opera, etc.
     else
     {
          var parser=new DOMParser();
          doc=parser.parseFromString(rValue,"text/xml");
     }    
    
     //Write the Data in Divs.
     // documentElement always represents the root node 
     WriteData(doc.documentElement);    
}

//Write the data in divs
function WriteData(xmldoc)
{    
    if(null != xmldoc)
    {
        //If error occur, return from function
        if(CheckError(xmldoc))
        {
            return;
        }
    
        //Set the Name of Person
        var name = GetValue(xmldoc, 0);
        document.getElementById("divName").innerHTML = name;
        document.getElementById("divMoreName").innerHTML = "More About " + name + " &raquo;";
         
        //Set the Desg of Person
        document.getElementById("divPosition").innerHTML = GetValue(xmldoc, 1);
         
        //Dispaly the detail of person 
        DisplayDescription(xmldoc);
        
        //Set the Href for Image and hyperlink 
        var pageUrl = GetValue(xmldoc, 3);
        document.getElementById("aDetailPageUrl").setAttribute("Href",pageUrl, false);
        document.getElementById("aPageUrl").setAttribute("Href",pageUrl, false);
         
        //Set the Image path of person
        document.getElementById("imgPath").setAttribute("src",GetValue(xmldoc, 4));
        
        //Set the current index to hidden variable.
        document.getElementById("index").value = GetValue(xmldoc, 5);        
     }
}

//Get the value for every node according to index
function GetValue(xmldoc, index)
{
    var value = "";
    
    if(null != xmldoc.childNodes[index] && null != xmldoc.childNodes[index].childNodes[0])
    {
        value = xmldoc.childNodes[index].childNodes[0].nodeValue;
    }
    
    return value;
}

//Display the character according to displaychar length.
function DisplayDescription(xmldoc)
{  
    var displayCharacters = GetValue(xmldoc, 7);
    var div = document.getElementById("divDescription");
    div.innerHTML = GetValue(xmldoc, 2);
    
    //Remove any markup in the div HTML by getting its inner text
    //NOTE: In IE browsers use the innerText property; others may use textContent property
    var innertext;
    if(div.innerText != undefined)
    {
        innertext = div.innerText;
    }
    else if(div.textContent != undefined)
    {
        innertext = div.textContent;
    }
    
    //Shorten the text if necessary
    if(innertext.length > displayCharacters)
    {
        innertext = innertext.substr(0,displayCharacters) + "...";
    }
    
    //Wrap the text in a <p> and assign the result to the div HTML 
    div.innerHTML = "<p>" + innertext + "</p>";
    
}

//Chcek the error to read the data from library
function CheckError(xmldoc)
{
    var isErr = false;
    var error = GetValue(xmldoc, 6);
            
    //if there is error, shows the error div and hide the main div
    if(error == "True" || error == "true")
    { 
        var errorMsg = "There has been some error ouccurred, Check the event log to look the error."; 
        document.getElementById("divError").innerHTML = errorMsg;
        document.getElementById("divOurPeople").style.display="none";
        isErr = true;
    }
    return isErr;
}