// JavaScript Document
/*
Module Name : SSRSJS_muajaxer
Description : This module is to implement AJAX on any page. You just have to mention a few parameters to send either a variables or a form via AJAX. This single module works for all.
Developer : Tajinder Singh Namdhari
Developed On : 05-02-2008
License : GPL
Company : SSRSJS Organisation
Website : http://www.ssrsjs.org
*/

var newRequest=null;
var requestObj="";
var getString="";
var postString="";
var accessObject=0;
var dimStatus=0;
function doDim()
{
	document.body.style.opacity='0.9';
	document.body.style.filter='alpha(opacity=80)';
	document.getElementById('cover').style.display='block';
}

function doUndim()
{
	document.body.style.opacity='1';
	document.body.style.filter='alpha(opacity=100)';
	document.getElementById('cover').style.display='none';
}

function doParameters(trgtFormFields)
{
	postString="";
	lmt=trgtFormFields.length;
	for(i=0;i<lmt;i++)
	{
		if(i!=0)
		{	postString+="&";	}
		postString+=trgtFormFields[i].name + "=" + escape(encodeURI(trgtFormFields[i].value));
	}
}

function sendRequest(trgtFormOrVar,trgtPage,trgtForm,trgtVarVal,trgtMethod,trgtObj,impliDim)
{
	getString="";
	
	
	if(impliDim==null)
	{	impliDim=0;	}
	
	dimStatus=impliDim;
	requestObj=trgtObj;
	if(trgtFormOrVar==0)
	{	accessObject=1;	}
	else
	{	accessObject=0;	}
	
	if(trgtVarVal.length>0)
	{
		var lmt=trgtVarVal.length;
		for(i=0;i<lmt;i=i+2)
		{
			if(i!=0)
			{	getString+="&";	}
			getString+=trgtVarVal[i] + "=" + trgtVarVal[i+1];
		}
	}
	
	if(window.XMLHttpRequest)
	{
		newRequest=new window.XMLHttpRequest();
		
	}
	else
	{
		if(window.ActiveXObject)
		{
			newRequest=new window.ActiveXObject("Microsoft.XMLHTTP");
			if(!newRequest)
			{
				newRequest=new window.ActiveXObject("Msxml2.XMLHTTP");
			}
			if(!newRequest)
			{
				if($trgtFormOrVar==0)
				{
					document.getElementById(trgtForm).action=trgtPage + "?" + getString;
					document.getElementById(trgtForm).method=trgtMethod;
					document.getElementById(trgtForm).submit();
				}
				else
				{	top.location.href=trgtPage + "?" + getString;	}
			}
		}
		else
		{
			if(trgtFormOrVar==0)
			{
				document.getElementById(trgtForm).action=trgtPage + "?" + getString;
				document.getElementById(trgtForm).method=trgtMethod;
				document.getElementById(trgtForm).submit();	
			}
			else
			{	top.location.href=trgtPage + "?" + getString;	}
		}
	}
	newRequest.onreadystatechange=handleRequest;
	if(trgtFormOrVar==0)
	{
		newRequest.open(trgtMethod,trgtPage,true);
		doParameters(document.getElementById(trgtForm).elements);
		var combinedString=getString+"&"+postString;
		
		newRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		newRequest.setRequestHeader("Content-length",combinedString.length);
		newRequest.setRequestHeader("Connection","close");
		newRequest.send(combinedString);
	}
	else
	{
		newRequest.open(trgtMethod,trgtPage + "?" + getString,true);
		newRequest.send(null);
	}
	if(impliDim==0)
	{
		document.getElementById(trgtObj).innerHTML="<img src='http://james.promaticstechnologies.com/images/loder.gif' border='0' vspace='0' hspace='0' style='background-repeat:no-repeat; width:145;'/>";
	}
	else if(impliDim==1)
	{
		doDim();
		}
}

function handleRequest()
{
	if(newRequest.readyState==4)
	{
		
		if(newRequest.status==200)
		{
				
				
			//alert(newRequest.responseText);
			//alert(document.getElementById(requestObj).innerHTML);
			document.getElementById(requestObj).innerHTML=newRequest.responseText;
			//alert(document.getElementById(requestObj).innerHTML);
			if(accessObject==1)
			{	
			
			//top.location.href='#'+requestObj;
			}
			if(dimStatus==1)
			{
					doUndim();
			}
			
		}
		else
		{
			alert("Press OK to continue (msg.3)");
		}
	}
}