// JavaScript Document

/*
Developed by : Shah Pratik 
Date : 23-3-2006
Function Name : ajaxprocess
Purpose  : 	Ajax processing class file - used to send request to any particular file and get response in request/response manner 
			without refreshing page.
Modified and Commented by : Shah Pratik 
Date : 02-5-2006, 04-5-2006, 29-06-2006

*/
/*
create object of ajaxprocess() function from where u want to generate request and get response in your script
eg. say var exmpleobj = new ajaxprocess();
*/
function ajaxprocess()
{
/*
http is request object of browser
*/
	var http = createRequestObject(); 
	//document.getElementById('ajaxloader').style.display='block';
	function createRequestObject()
	{
		var request_o;
/*
get name of the browser
*/
		var browser = navigator.appName;
		
		request_o = false;
		if(browser == "Microsoft Internet Explorer")
		{
			request_o = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			request_o = new XMLHttpRequest();
		}
		return request_o;
	}
/*
Developed by : Shah Pratik 
Date : 04-5-2006
call ajaxclass function using created object of ajaxprocess function in your script with proper arguments as suggested below 

var methodname = 'get' or 'post';

var filename = filepath = 'path of webroot dir\application dir name\folder path\filname *.* ';

var formid =  if method is passed 'get' keep value of this argument ''  
			  if method is passed 'post' you have to pass value to this variable as per suggested below	
				you can pass the form object directly by  'this.form'  or 
				you can	pass form id or 
				you can pass the string of variables with their values concatenated by '&' eg. in format formid="var1=value1&var2=value2"

var resfun = this argument should be defined in form of function with one argument it has in your script from where request is generated
			 the argument of this function is directly assigned by the browser object created for this request in this script
			 so value of responseText is available to you from where the request is generated in your script by object.responseText   
					
			eg. from where the request is generated in your script 
					var result = function(req_obj) { var response = req_obj.responseText; alert(response); };
					pass variable 'result' as last argument of function ajaxclass 
*/
	this.ajaxclass=function(method,filename,formid,resfun)
	{
		
		function handleDivTag(divtag) 
		{ 
		   var divtag; 
		   return divtag; 
		} 
		var divhandler = new handleDivTag(null);
/*
Developed by : Shah Pratik 
Date : 23-3-2006
Function Name : getFormElements
Purpose  : 	To get all the form elements name and their value in single string format where each variable & value pair is concatenated by '&'
*/		
		function getFormElements(frmObject)
		{
			if(typeof(frmObject) == 'object')
			{	
				var str_FormVars = '';
				var elementsLength = frmObject.elements.length;
				for(var i=0 ; i < parseInt(elementsLength) ; i++)
				{
						str_FormVars += frmObject.elements[i].name + "=" + frmObject.elements[i].value + "&" ;
				}
				return str_FormVars;
			}
		}
/*
Developed by : Shah Pratik 
Date : 23-3-2006
Function Name : handleData
Purpose  : it checks weather the browser has reached or not to readyState = 4 if has reached then return browser object  	
*/		
		function handleData()
		{	
			if(http.readyState == 4)
			{ 
				var AJAX_Response = http.responseText;
				//alert(AJAX_Response);
				//$e("body_div").innerHTML = AJAX_Response;
				//$e("mytesttextarea_RESP").value = "";
				//$e("mytesttextarea_RESP").value = AJAX_Response;
				
				getScriptText(AJAX_Response);
				//window.lastAppendedScript = window.scriptID ;
				window.scriptID ='';
				resfun(http);
			}
		}
		
		if(method == "get"  )
		{
			//alert("calling open");
			//alert(filename);
			http.open(method,filename);
			http.onreadystatechange = handleData; 
			http.send(null);
		}
		else if(method == "post"  )
		{
			var formvars;
			
			if((typeof(formid) == 'string' && formid.search('=') != -1) || (formid == ""))
			{
				formvars = formid;
			}
			else 
			{
				if(typeof(formid) == 'object')
				{
					formvars = getFormElements(formid);
				}
				else
				{
					frmObj = $e(formid);
					formvars = getFormElements(frmObj);
				}
			}
			http.open(method,filename); 					
			http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			http.onreadystatechange = handleData; 
			http.send(formvars);
		}
	}
}

/*
RH for returnHTML
Developed By : Shah Pratik
Date : 07-06-2006
Function Name : returnHTML
Purpose  : Extracts all html part from the response and return it
*/
function returnHTML(RH_Response)
{	
	//alert("RHTML : " + RH_Response);	
	var REGEX_rmvScript = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
	var RH_HTML = RH_Response.replace(REGEX_rmvScript,'');
	//document.getElementById('ajaxloader').style.display='none';
	return trim(RH_HTML) ;
}



/*
GST for getScriptText
Developed By : Shah Pratik
Date : 19-06-2006
Function Name : getScriptText 
Purpose  : Extracts all part between different script tags from the response and merge all the part and pass it to runScript function  
*/

function getScriptText(GST_Response)
{	
	//alert(scID + "==>"+GST_Response);
	var REGEX_rmvScript = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
	var REGEX_matchScript = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;
	var GST_SCRIPT = GST_Response.match(REGEX_rmvScript);
	GST_jScriptText = null;
	var GST_jScriptText = "";	
	if(GST_SCRIPT)
	{
		for(var GST_i = 0; GST_i < GST_SCRIPT.length; GST_i++)
		{
			var GST_jScript = GST_SCRIPT[GST_i].match(REGEX_matchScript);
			GST_jScriptText = GST_jScriptText + GST_jScript[2];
    	}
	}
	
	if(GST_jScriptText && GST_jScriptText != null && GST_jScriptText != "")
	{
		//alert(scID+'ddd');	
		//alert(scID +'after');	
		//window.myvar = 'dd';
		//window.myvar=scID;		
		runScript(GST_jScriptText);		
	}
	
}

/*
RS for runScript
Developed By :  Shah Pratik
Date : 06-06-2006
Function Name : runScript 
Purpose  : attach all the script text to head dynamically
*/
function runScript(RS_scriptText) 
{	
scripteID = window.scriptID;
//if(window.firefox)console.log('Script Added ===>' + RS_scriptText);
if (scripteID != undefined || scripteID != 'undefined'){
	//alert(RS_scriptText);
	//alert(scripteID);
	var RS_scriptContainer = document.createElement('script');
	RS_scriptContainer.id = window.scriptID;
    RS_scriptContainer.language = 'javascript';
	RS_scriptContainer.type = 'text/javascript';
	RS_scriptContainer.text = RS_scriptText;
	if(!($e('RS_dynamicAddedScript')))
	{
		document.getElementsByTagName('head')[0].appendChild(RS_scriptContainer);
	}
	if($e('RS_dynamicAddedScript') && $e('RS_dynamicAddedScript') != null){
		var dynjspid = $e('RS_dynamicAddedScript').parentNode;
		dynjspid.removeChild($e('RS_dynamicAddedScript'));
		dynjspid.appendChild(RS_scriptContainer);		
	}
	//$e("mytesttextarea_JS").value = "";
	//$e("mytesttextarea_JS").value = $e('RS_dynamicAddedScript').text;
}
}


