
function ReturnFormEventHandler(WebsiteID,ContextHandler)
{
	this.clicked 		= 0;	
	this.ContextHandler = ContextHandler;
	this.AjaxRoot		= "/shared/php/ajax/";
	this.WebsiteID		= WebsiteID;
	this.ProductFinderWindow = null;
	/**
	 * Since we need to search for an order and validate it as such, we won't have the OrderID when this object gets
	 * instantiated. That's why we farm this out to a diff function.
	 * @param {Object} OrderID
	 */
	this.SetOrderID	= function(OrderID)
	{
		this.OrderID		= OrderID;
	}
	this.set_mode = function(mode)
	{
	    document.MASTER.mode.value = mode;
	    document.MASTER.submit();	    
	}
	
	this.radio_select = function()
	{
	    this.clicked = 1;	    
	}
	
	this.check_form=function()
	{
	    if(this.clicked == 0)
	        alert('Please select an order.');
	    else
	        this.set_mode('show_order');
	}
	
	this.policycheck = function()
	{
	    if(!document.MASTER.policy.checked)
	    {
	        alert('Please check that you agree to our Return Policy, in order to proceed.');
	        return;
	    }
	    this.submit_return_request();
	}
	
	this.submit_return_request = function()
	{
		// get the order_detail_id for the selected item
	    var order_detail = document.getElementsByName("0DB8ACB4-8E34-4040-9137-72AAE86819C7");
	    var order_detail_id = "";
	    for(var i=0; i < order_detail.length; i++)
	    {
	    	if(order_detail[i].checked == true)
	    	{
	    		order_detail_id = order_detail[i].value;
	    		break;
			}
	    }	    
		// submit the form
	    this.submit_new_return_request();	    
	}
	
	/**
	 * hits the ajax landing page, validates and on success inserts.
	 */	
	this.submit_new_return_request=function()
	{
		var id=Array("return_error_text");		
		var objAjaxv2 	= new Ajaxv2(this.AjaxRoot+"return_form_helper.php",this,this.InsertNewReturnCallback,"POST","",this.ContextHandler);
		var container 	= document.getElementById('form_container');		
		objAjaxv2.Parameters=objAjaxv2.GetFormValues(container)+"&mode=ActionInsertNewReturn&OrderID="+this.OrderID;
		objAjaxv2.EventData	=id;
		objAjaxv2.GetPost(true);
		//this.ShowRefreshingIcon(id);		
	}
	
	this.InsertNewReturnCallback = function(Success, ResponseObject, EventData)
	{
		if(Success)
		{
			var element;
			var event_array = Array();
			try
			{
				eval("var obj="+ResponseObject.responseText);
				//we only show text on errors. otherwise we'll refresh page to success.
				if(obj.RetVal <= 0)
				{
					if(EventData.constructor!=Array)
						event_array.push(EventData);
					else
						event_array=EventData;
						
					for(var c=0;c<event_array.length;c++)
					{
						element=document.getElementById(event_array[c]);			
						element.innerHTML=obj.Response;
					}				
					
					if(obj.Javascript != null)			
						window.eval(obj.Javascript);
				}
				else
				{
					//on success push to thanks
					if(this.WebsiteID !=6)
						document.location="/forms/merchandise_thanks.php";
					else
                    {
						document.location="/systems/returns/a_returncreate.php?mode=complete&ReturnID="+obj.Response.ReturnID;
                    }
				}
			}
			catch(ex)
			{
				alert('Exception caught in Return Submission '+ex.toString());	
			}				
		}
	}
	
	
	this.select_item = function()
	{
		// show the return form
		this.show_hidden_object("return_form_div", "block");
		// show the acknowledgement message
		if(document.getElementById("acknowledgement_message_div") != undefined)
			this.show_hidden_object("acknowledgement_message_div", "block");
		// show the admin options, if in admin mode
		if(document.getElementById("admin_options_div") != undefined)
			this.show_hidden_object("admin_options_div", "block");		
		// show the submit button
		this.show_hidden_object("submit_return_div", "block");
	}
	
	this.ChangeMainReason = function(reason)
	{
		// get the selected reason
		var reason_id 		= reason.options[reason.selectedIndex].value;						
		
		if(reason_id == -1)
		{
			// hide the dropdown if they select the default option
			this.hide_object("return_sub_reason_div");
			// hide the sub-reason options
			this.hide_object("reason_ship_damage_div");	
			this.hide_object("sub_reason_other_div");
		} 
		else
		{			
			var Parameters	= "mode=get_sub_reason&return_reason_id="+reason_id;
			var objAjaxv2 	= new Ajaxv2(this.AjaxRoot+"/return_form_helper.php",this,this.OnChangeMainReason,"POST",Parameters,this.ContextHandler);
			objAjaxv2.EventData=reason_id;		
			objAjaxv2.GetPost(true);	
		}
	}
	
	this.OnChangeMainReason = function(Success,ResponseObject,EventData)
	{
		if(!Success)
        {
            //display some kind of warning
            alert('The system was unable to contact the server. Please try again.');
            return;
        }                
        try
        {                
            //did we pass validation?
            eval("var obj="+ResponseObject.responseText);
            
            if(obj.RetVal < 0)
            {
                //gah, some kind of error php side. Lets setup the error on the form.
               	alert(obj.Response);
                return false;
            }
			
			var reason_id		= EventData; //we passed the reason through via EventData
			var show_other		= 0;
			var show_ship_damage= 0;
			var objHtml = document.getElementById('return_sub_reason_div');			
			
			if(reason_id==113 || reason_id==115) //top level OTHER option, let sub-options still load, will be hidden input.		
				show_other=1;			
		
			objHtml.innerHTML=obj.Response;
			
			// show the dropdown
			this.show_hidden_object("return_sub_reason_div", "block");
	
			if(!show_other)
				this.hide_object("sub_reason_other_div");
			else
				this.show_hidden_object("sub_reason_other_div", "block");
				
			if(!show_ship_damage)
				this.hide_object("reason_ship_damage_div");
			else
				this.show_hidden_object("reason_ship_damage_div", "block");		
		            
			return true;            
        }
        catch(exception)
        {
            alert('A system error occured.('+exception+')');
            return false;
        }	
	}
	
	this.ChangeSubReason = function(sub_reason)
	{		
		var sub_reason_id 	= sub_reason.options[sub_reason.selectedIndex].value;
		
		// if the default is selected, then hide anything that might be open
		if(sub_reason_id == -1)
		{
			this.hide_object("sub_reason_other_div");
			this.hide_object("reason_ship_damage_div");
		}
		else 
		{	
			var show_other		= 0;
			var show_ship_damage= 0;
			
			if(sub_reason_id==104 || 
				sub_reason_id==109 ||
				sub_reason_id==110 ||
				sub_reason_id==111 ||
				sub_reason_id==112)
			{
				show_other=1;
			}
			
			if(sub_reason_id==111)
				show_ship_damage=1;				
			
			if(show_other)
				this.show_hidden_object("sub_reason_other_div", "block");
			else
				this.hide_object("sub_reason_other_div");
				
			if(show_ship_damage)
				this.show_hidden_object("reason_ship_damage_div", "block");
			else
				this.hide_object("reason_ship_damage_div");		
		}
	}
	
	
	this.change_customer_choice=function(customer_choice)
	{
		var customer_choice_id = customer_choice.options[customer_choice.selectedIndex].value;
		if(customer_choice_id == 2)
			this.show_hidden_object("vehicle_form", "block");
		else
			this.hide_object("vehicle_form");
	}
	
	this.open_product_finder=function(product_finder_url)
	{
		this.ProductFinderWindow=window.open(product_finder_url, 'product_finder', 'width=800,height=600,toolbar=no,location=no,scrollbars=yes,resizable=yes');
	}
	
	this.select_product_for_exchange=function(application_id)
	{
		var Parameters	= "mode=get_application_product_code&application_id="+application_id;
		var objAjaxv2 	= new Ajaxv2(this.AjaxRoot+"return_form_helper.php",this,this.OnPickExchangeProduct,"POST",Parameters,this.ContextHandler);			
		objAjaxv2.GetPost(true);
	}
	
	this.OnPickExchangeProduct=function(Success,ResponseObject,EventData)
	{		
		if(!Success)
        {
            //display some kind of warning
            alert('The system was unable to contact the server. Please try again.');
            return;
        }                
        try
        {                
            //did we pass validation?
            eval("var obj="+ResponseObject.responseText);
            
            if(obj.RetVal < 0)
            {
                //gah, some kind of error php side. Lets setup the error on the form.
               	alert(obj.Response);
                return false;
            }
			var myArray				= obj.Response;
			var code_response		= myArray[0];
			var description_response= myArray[1];
	
			document.getElementById("SubstitutionProductCode").value 	= code_response;
			document.getElementById("product_code").innerHTML 			= code_response;
			document.getElementById("SubstitutionProductDescription").value 	= description_response;
			document.getElementById("product_description").innerHTML 			= description_response;			
			
			//close our popup window. since we're firing the ajax call via the window, we can't simply fire ajax and close..that != good
			if(this.ProductFinderWindow != null)
				if(!this.ProductFinderWindow.closed)
					this.ProductFinderWindow.close();
			
			return true;            
        }
        catch(exception)
        {
            alert('A system error occured.('+exception+')');
            return false;
        }	
	}
	
	this.hide_object=function(object_id)
	{
		document.getElementById(object_id).style.display = "none";
	}
	
	this.show_hidden_object=function(object_id,block_mode)
	{
		if (block_mode == undefined)		
			block_mode = 'inline'		
	    document.getElementById(object_id).style.display = block_mode;
	}
}