// JavaScript Document
// 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.
Pending Modification: Submition of Files via AJAX.
Developer : Rakesh kumar
Developed On : 05-02-2008
Update On : 03-04-2008



*/

//Procedure to implement SSRSJS MultiuseAjaxer
/*
	//Declare and object as below
	var myRequest=new sendAjaxRequest(trgtFormOrVar,trgtPage,trgtForm,trgtVarVal,trgtMethod,trgtObj,ignoreObj,diminishObj);
	
	Here the arguments are as below specifications:
		trgtFormOrVar -> Decides whether you are submitting a Form or just sending a GET Request
		 trgtPage -> URL of page to recieve the request
		trgtForm -> DOM ID of the Form, if you are submittin a Form. Otherwise a null ('')
		 trgtVarVal -> An Array containing Variable preceeding Value element, if send a GET Request. E.g. Array('rType','A','job','save')
		trgtMethod -> POST or GET in Single quotes. E.g.- ('POST')
		 trgtObj -> DOM ID of DIV,TD or any other Object to display response recieved for request made
		ignoreObj -> 0 or 1 depeding on either you want to point updated 'trgtObj' or not
		 diminishObj -> 1 or 0 depending on either you want to Diminish (to low opacity) the target object during updation or not
*/

function sendAjaxRequest(trgtFormOrVar,trgtPage,trgtForm,trgtVarVal,trgtMethod,trgtObj,ignoreObj,diminishObj)
{
	var ajaxRequest=null;
	var getStr="";
	var postStr="";
	var currWaitId='cover' + Math.floor(Math.random()*101);
	
	this.askToWait=function(currWaitId)
		{
			var currWait=document.createElement("div");
			currWait.setAttribute('id',currWaitId);
			currWait.setAttribute('style','position:absolute; top:35%; left:35%; display:none; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:18px; text-align:center; vertical-align:middle; border: 4px double #BBBBBB; color:#444444; background-color:#EEEEEE;');
			document.body.appendChild(currWait);
			
			document.getElementById(currWaitId).innerHTML='';
		};
	
	//Function diminishing the target DOM Object
	this.dimIt=function(trgtId)
		{
			document.getElementById(trgtId).style.opacity='0.2';
			document.getElementById(trgtId).style.filter='alpha(opacity=20)';
			document.getElementById(currWaitId).style.display='block';
		};
	
	//Definition of function to encode and escape form field values
	this.makeParameters=function(trgtFormFields)
		{
			var lmt=trgtFormFields.length;
			for(i=0;i<lmt;i++)
			{
				if(i!=0)
				{	postStr+="&";	}
				
				if(trgtFormFields[i].type=='checkbox')
				{
					if(trgtFormFields[i].checked==true)
					{	postStr+=trgtFormFields[i].name + "=" + escape(encodeURI(trgtFormFields[i].value));	}
					else
					{	postStr+=trgtFormFields[i].name + "=" + '';	}
				}
				if(trgtFormFields[i].type=='radio')
				{
					if(trgtFormFields[i].checked==true)
					{	postStr+=trgtFormFields[i].name + "=" + escape(encodeURI(trgtFormFields[i].value));	}
				}
				else
				{	postStr+=trgtFormFields[i].name + "=" + escape(encodeURI(trgtFormFields[i].value));	}		
			}
		};
	
	if(trgtVarVal.length>0)
	{
		var lmt=trgtVarVal.length;
		for(i=0;i<lmt;i=i+2)
		{
			if(i!=0)
			{	getStr+="&";	}
			getStr+=trgtVarVal[i] + "=" + trgtVarVal[i+1];
		}
	}
	
	//Initialiazing the AjaxRequest Object
	if(window.XMLHttpRequest)
	{
		ajaxRequest=new window.XMLHttpRequest();
	}
	else
	{
		if(window.ActiveXObject)
		{
			ajaxRequest=new window.ActiveXObject("Microsoft.XMLHTTP");
			if(!ajaxRequest)
			{
				ajaxRequest=new window.ActiveXObject("Msxml2.XMLHTTP");
			}
			if(!ajaxRequest)
			{
				if($trgtFormOrVar==0)
				{
					document.getElementById(trgtForm).action=trgtPage + "?" + getStr;
					document.getElementById(trgtForm).method=trgtMethod;
					document.getElementById(trgtForm).submit();
				}
				else
				{	top.location.href=trgtPage + "?" + getStr;	}
			}
		}
		else
		{
			if(trgtFormOrVar==0)
			{
				document.getElementById(trgtForm).action=trgtPage + "?" + getStr;
				document.getElementById(trgtForm).method=trgtMethod;
				document.getElementById(trgtForm).submit();	
			}
			else
			{	top.location.href=trgtPage + "?" + getStr;	}
		}
	}
	//Definition of On ReadyStateChange function
	ajaxRequest.onreadystatechange=function()
		{
			if(ajaxRequest.readyState==4)
			{
				
				if(ajaxRequest.status==200)
				{
					document.getElementById(trgtObj).innerHTML=ajaxRequest.responseText;
					
					if(ignoreObj==0)
					{	top.location.href='#'+trgtObj;	}
					
					/*To Execute JavaScript sent via AJAX Response - You have to put your JavaScript in <script>....</script> tags
					
					var toEval=currRqst.responseText.split('script>');
					var evalLmt=toEval.length;
					for(i=0;i<evalLmt;i++)
					{
						if((i%2)!=0)
						{
							toEval[i]=toEval[i].substr(0,toEval[i].length-2);
							eval(toEval[i]);
						}
					}
					*/
					ajaxRequest=null;
				}
				else
				{
					if(ajaxRequest.status==404)
					{	alert("Sorry! Page not found");	}
					else if(ajaxRequest.status==12030)
					{	}
					else
					{
						alert("Sorry! communication with server failed. Please try again! (" + ajaxRequest.status + ")");
					}
					
					ajaxRequest=null;
				}
				
				if(diminishObj==1)
				{
					document.getElementById(trgtObj).style.opacity='1';
					document.getElementById(trgtObj).style.filter='alpha(opacity=100)';
					document.body.removeChild(document.getElementById(currWaitId));
				}
				delObj(this);
			}
		};
		
	if(trgtFormOrVar==0)
	{
		ajaxRequest.open(trgtMethod,trgtPage,true);
		this.makeParameters(document.getElementById(trgtForm).elements);
		var combinedString=getStr + "&" + postStr;
		
		ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length",combinedString.length);
		ajaxRequest.setRequestHeader("Connection","close");
		ajaxRequest.send(combinedString);
	}
	else
	{
		ajaxRequest.open(trgtMethod,trgtPage + "?" + getStr,true);
		ajaxRequest.send(null);
	}
	
	if(diminishObj==0)
	{
		document.getElementById(trgtObj).innerHTML="<div align='center' style='background-image:url(http://192.168.2.195/fantasy_ifc/images/loading.gif); width:100px;background-repeat:no-repeat;    height:30px; border:1px;'></div>";
	}
	else
	{	this.askToWait(currWaitId);	this.dimIt(trgtObj);	}
}

function delObj(trgt)
{
	delete trgt;
}