// JavaScript Document
	/*
	# eLuminous Technologies - Copyright (C)  http://eluminoustechnologies.com 
	# This code is written by eLuminous Technologies, Its a sole property of 
	# eLuminous Technologies and cant be used / modified without license.  
	# Any changes/ alterations, illegal uses, unlawful distribution, copying is strictly
	# prohibhited 
	# Name: ajax_request_handler.js
	# Usage: included the file to handle ajax request. ( WRITE USAGE DETAILS ) 
	# Created : Rupal Pinge & Sham Shriwastav (30-05-2007)
	# Update  : 30-05-2007 Sham Shriwastav 
	# Status  : open
	# Purpose : make javascript code seprate from other coding
	*/
	
var http; //golbal http object
var target_element_id; //if there is any target element then assing its id to this function
var processing_image_tag; //this will be the processing image path
var show_processing_image; //this will decide whether to show processing image or not
var actionName = ''; //global variable to know what action is going to be processed

target_element_id="";
var processing_image_path= 'http://www.cosplayfu.com' + "/images/ajax-loader.gif";
//processing_image_path="http://eluminoustechnologies.net/sham/vipdesk_new/images/ajax-loader.gif";
show_processing_image=1;

var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"];

/* The following function creates an XMLHttpRequest object... */
function createRequestObject()
{	
	
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer")
	{		
		for (var i = 0; i < aVersions.length; i++)
		{
			try
			{
				request_o = new ActiveXObject(aVersions[i]); //Create the object using MSIE's method 
			}
			catch(err)
			{
				
			}
		}
	}
	else
	{			
		try
		{
			request_o = new XMLHttpRequest();//Create the object using other browser's method 
		}
		catch(err)
		{
			
		}
	}
	return request_o; //return the object
}

/* The variable http will hold our new XMLHttpRequest object. */
function getData(method, target_file, parameters, target_element_id_param, myActionName, show_processing_image_param, process_image_url)
{
	//alert('hi, i am in getData');
	//alert('target_file = '+target_file);
	actionName = myActionName;
	
	try
	{
		http = createRequestObject();
	}
	catch(err)
	{
		txt="Unable create request object!\n\n";
		txt+="Error description: " + err.description + "\n\n";
		alert(txt);
	}
	target_element_id=target_element_id_param; //get the return element id
	//alert(target_element_id);
	show_processing_image=show_processing_image_param; //decided whether to show the processing image or not
	processing_image_path=process_image_url // set the process image path.
	if(method=="POST")//if the method is post
	{
		try
		{
			http.open("POST",target_file, true);  
		}
		catch(err)
		{
			txt="Unable to open the connection!\n\n";
			txt+="Error description: " + err.description + "\n\n";
			alert(txt);
		}
		http.onreadystatechange = handle_response; //use this callback function when the ready state changes
		try
		{
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   
			http.setRequestHeader("Content-length", parameters.length);   
			http.setRequestHeader("Connection", "close");	
		}
		catch(err)
		{
			txt="Unable to set the request header!\n\n";
			txt+="Error description: " + err.description + "\n\n";
			alert(txt);
		}
		try
		{
			http.send(parameters); 
			/* Send the data. We use something other than null when we are sending using the POST method. */	
		}
		catch(err)
		{
			txt="Unable to set the send the request\n\n";
			txt+="Error description: " + err.description + "\n\n";
			alert(txt);
		}
	}
		
	if(method=="GET")
	{
		target_url=target_file+"?"+parameters;		
		try
		{
			http.open('get',target_url);	
		}
		catch(err)
		{
			txt="Unable to open the connection!\n\n";
			txt+="Error description: " + err.description + "\n\n";
			alert(txt);
		}
		http.onreadystatechange = handle_response;
		try
		{
			http.send(null); /* Send the data. Use null to send data using get */
		}
		catch(err)
		{
			txt="Unable to set the send the request\n\n";
			txt+="Error description: " + err.description + "\n\n";
			alert(txt);
		}
	}
}


// This function handles the response
// Make sure that the transaction has finished. 
// The XMLHttpRequest object has a property called readyState with several states:
// 0: Uninitialized
// 1: Loading
// 2: Loaded
// 3: Interactive
// 4: Finished
function handle_response()
{
	try
	{
		var readystate=http.readyState;
	}
	catch(err)
	{
		txt="Unable to get the ready state !\n\n";
		txt+="Error description: " + err.description + "\n\n";
		alert(txt);
	}
	if(readystate<4)
	{
		if(processing_image_path!="" && show_processing_image==1)
		{
			var response = "<img src='"+processing_image_path+"' />";
			show_response(response);
		}
	}
	if(readystate == 4)
	{
		req_status=http.status;
		switch(req_status)
		{
			case 200:
				//alert(' case 200 response = '+actionName);
				switch(actionName)
				{
					case 'add_to_cart' :  
						//alert(response);
						var response = http.responseText;	
						
						if(response.substring(0,6)=='noroot')
						{
							alert("You can add related product after adding main product.");
							response=response.substring(6);
						}
						else if(response.substring(0,6)=='errpri')
						{
							alert("Sorry, Please try again.");
							response=response.substring(6);
						}
						else if(response.substring(0,6)=='errqty')
						{
							alert("Sorry, You can order maximum 10 products or avaliable instore Quantity.");
							response=response.substring(6);
						}
						else	
						{
							alert("Product is added successfully in the cart.");
						}
						show_response(response);
						//alert(response);
						if(typeof myCart != 'undefined')
						{
							if(confirm("Your cart has been modified. \nClick on 'OK' button to refresh your cart."))
							{
								document.location = 'http://www.cosplayfu.com/my_cart';
							}
						}
					break;
						
					case 'buy_it_now' : 
						var response = http.responseText;	
						//alert(response);
						if(response.substring(0,6)=='errqty')
						{
							alert("Sorry, You can order maximum 10 products or avaliable instore Quantity.");
							response=response.substring(6);
							show_response(response);
						}
						else	
						{
							document.location = 'http://www.cosplayfu.com/my_cart';
						}
						break;
				}
				break;
				
				/*var response = http.responseText;	
				show_response(response);
				//alert(response);
				alert("Product is added successfully in the cart.");
				
				if(myCart)
				{
					if(confirm("Your cart has been modified. \nClick on 'OK' button to refresh your cart."))
					{
						document.location = SITE_URL + '/my_cart';
					}
				}*/
				break;
			case 400:
				alert("Bad Request");
				break;
			case 401:
				alert("Unauthorised Request");
				break;
			case 403:
				alert("Forbidden Request");
				break;
			case 404:
				alert("File Not Found");
				break;
			case 500:
				alert("Internal Server Error");
				break;
			default:
				//alert("No valid response received !");
				break;
		}
	}
}

// This functions shows the response at the proper place
function show_response(response)
{
	//alert(response);
	if(document.getElementById(target_element_id))	
	{
		document.getElementById(target_element_id).innerHTML = response;
	}
}