function SelectSingleNode(xmlDoc, elementPath)
{
    if(document.all != null)
    {
        return xmlDoc.selectSingleNode(elementPath);
    }
    else
    {
       var xpe = new XPathEvaluator();
       var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
       var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
       return results.singleNodeValue; 
    }
}

function GetXmlDocument(xmlhttp)
{
     if(document.all != null)
     {
        return xmlhttp.responseXml;

     }
     else
     {
        return (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); 
     }
}



$(document).ready(function GetRandomPhoto()
{

    var xmlhttp=null;
    //var url = sitePath + "/photo_upload/photoXML.aspx?d=" + (new Date());
    var url = "/photo_upload/photoXML.aspx?d=" + (new Date());

 
    try
    {
	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	    if (typeof(xmlhttp) != 'object' )
	    {	 	
	       // If IE7, Mozilla, Safari, etc: Use native object
	       xmlhttp = new XMLHttpRequest();
	    }
    }
    catch (e) 
    {
	    // fallback: use native object 
	    xmlhttp = new XMLHttpRequest();
    }
	

    if (xmlhttp!=null)
    {

	    xmlhttp.open("GET",url,false);
	    xmlhttp.send(null);

        if (xmlhttp.responseText != null)
        {
  
            var xmlDoc = GetXmlDocument(xmlhttp);           
            var xmlNodeLink = SelectSingleNode(xmlDoc, "//link");
           
            var aLink = document.getElementById('polaroidPhotoLink');
            aLink.href = xmlNodeLink.firstChild.nodeValue;

            //var arrLinkChilds = aLink.childNodes;

            if (aLink.childNodes != null && aLink.childNodes.length  > 0)
            {
                var xmlNodeImg =  SelectSingleNode(xmlDoc, "//img");

                if ( document.all != null)
                {
                    var imgPhoto  = aLink.childNodes[0];
                    //imgPhoto.src = sitePath + "/" + xmlNodeImg.getAttribute('src').replace("../../../","");
                    imgPhoto.src = xmlNodeImg.getAttribute('src');
                    imgPhoto.alt = xmlNodeImg.getAttribute('alt');
                }
                else
                {
                    var imgPhoto  = aLink.childNodes[1];
                    //imgPhoto.attributes.getNamedItem('src').nodeValue = sitePath + "/" + xmlNodeImg.getAttribute('src').replace("../../../","");
                    imgPhoto.attributes.getNamedItem('src').nodeValue = xmlNodeImg.getAttribute('src');
                    imgPhoto.attributes.getNamedItem('alt').nodeValue = xmlNodeImg.getAttribute('alt');
              }
          
           }
       }

    }	
});