var xmlHttp=null;
var selectedDivId;

var theme = "/webpages";

function GetDocuments(fid, source, layerid, divId)
{
	var formData = "CMD=GetDocuments&featureid=" + fid + "&source=" + source + "&securedlayerid=" + layerid;
	
	var docViewerHandler = getApplicationPath() + theme + "/DocumentViewer/DocumentViewer.aspx";	
	RetrieveDocuments(docViewerHandler, formData, divId);
}
function RetrieveDocuments(url, formDataString, divId)
{
	selectedDivId=divId;
	if(xmlHttp==null || xmlHttp.readyState==4)
	{
		if(xmlHttp==null)
		{
			if(window.ActiveXObject)
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			else if(window.XMLHttpRequest)
				xmlHttp =new XMLHttpRequest(); 
		}
    	
		if(xmlHttp!=null)
		{
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlHttp.onreadystatechange=ProcessDocuments;
			xmlHttp.send(formDataString);
		}	
	}
	else if(xmlHttp.readyState!=4)
		alert("Please wait. Processing the previous request");
}
function ProcessDocuments()
{
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		    document.getElementById(selectedDivId).innerHTML = xmlHttp.responseText;
		else
			alert("Server not responding, Please try later");
	}
	else
	{
		//Error from server
	}
	xmlHTTP=null; 	
}

function ToggleVisibility(divId)
{
	var obj = document.getElementById(divId);
	if(obj)
		obj.style.display = obj.style.display=="inline"? "none" : "inline";
} 
function ToggleImage(image1, image2, objId)
{
	var obj = document.getElementById(objId);
	if(obj){
		if(obj.src.indexOf(image1) > 0 )
			setImageSource(image2,objId);    //obj.src=image2;
		else
			setImageSource(image1,objId);  //obj.src=image1;
	}
}
function setImageSource(src, objId)
{
	document.getElementById(objId).src= src;
}


function openDocLink(linkUrl)
{
    if(linkUrl)
    {
        window.open(linkUrl, "documentlink");
    }
}

function getApplicationPath()
{
	var theHost   = window.location.hostname;
	var path      = window.location.pathname;
	var protocol  = window.location.protocol;
	var port	    = window.location.port;

	if(protocol.indexOf(":") < 0)
		protocol+=":";
	
	if(port !=null && port.length>0)
	{
		if(theHost.indexOf(":") < 0)
			theHost+=":" + port;		
	}		
	path = path.substring(0,path.indexOf("/",1));
	path = protocol + "//" + theHost + path;

	return path;
}