﻿
/// <summary>
/// Declare InlinePopup as javascript class
/// <summary>
InlinePopup = Class.create();

/// <summary>
/// Define a InlinePopup javascript class
/// <summary>
///<notes>
/// ids of html elements for controling display properties.
/// -body: id of the main form body
/// -success: id of the html that is shown upon a successfull transaction
/// -failure: id of the html that is shown upon a failed transaction
/// -loader: the id of the html that is shown whilst the ajax call is made.
/// </notes>
InlinePopup.prototype = 
{
	/// <summary>
	/// Creates a new instance of InlinePopup
	/// <summary>
	/// <param name="hs">highslide: Global highslide object</param>
	/// <param name="ajaxSuccessText">string: If successful form submit; the text you expect to recieve back</param>
	/// <param name="httpMethod">string: 'get' or 'post'</param>
	/// <param name="objectType">string: 'ajax' or 'iframe'</param>
	initialize: function(hs, httpMethod, objectType)
	{												
		this.hs = hs;
		this.objectType = objectType;
		this.httpMethod = httpMethod;
	},
	
	ajaxResponseText: function()
	{
		return this.responseText;	
	},
	
	/// <summary>
	/// Opens a new highslide window
	/// <summary>
	/// <param name="objAnchor">element: anchor tag with onclick opening link</param>
	/// <param name="windowId">string: id of highslide popup window content</param>
	/// <param name="displayData">Associative array: all display object (id)/value pairs</param>
	/// <param name="postData">Associative array: key/value pairs of form submit data
	///	note: the value is either a string or an element object</param>
	openWindow: function(objAnchor, windowId, displayData, postData, requiredFields)
	{	
		this.displayData = displayData;
		this.windowId = windowId;
		this.postData = postData;
		this.requiredFields = requiredFields;
		
		for(var key in this.displayData)
		{
			//check element exists
			if($(key))
			{
				//check element is a form element or html element
				if($(key).type == 'text')
				{
					$(key).value = this.displayData[key];
				}
				else if($(key).type == 'textarea')
				{
					$(key).innerHTML= this.displayData[key];
				}
				else
				{
					$(key).innerHTML= this.displayData[key];
				}								
			}							
		}
		
		var isExpanded = this.hs.htmlExpand(objAnchor, {contentId: this.windowId, objectType: this.objectType})
			
		return isExpanded;
									
	},
	
	/// <summary>
	/// Closes an existing highslide window
	/// <summary>
	/// <param name="objAnchor">element: anchor tag with onclick closing link</param>
	closeWindow: function(objAnchor)
	{	
		var close =  this.hs.close(objAnchor);		
		
		return close;
	},						
	
	/// <summary>
	/// Submits data to a specified URL using the prototype library AJAX request.
	/// <summary>
	/// <param name="url">string: full URL to submit form to</param>
	/// <param name="errorAlertInline">string: 'alert' or 'inline' or 'ignore'</param>
	/// <notes>When errorAlertInline = 'inline' declare a html tag
	/// with an id of the windowId preceded by '-errMessage'</notes>	
	submitForm: function(url, errorAlertInline, onSuccessCallback, onFailureCallback)
	{
		var returnText = '#return';							
		var postParams = '';		
		this.url = url;
		var errorMessage = '';
		var isSuccess = false;
		var isFailure = false;
		var windowId = this.windowId;
		this.errorAlertInline = errorAlertInline;
		var responseText = null;
		
		
		//build standard '& =' query string
		for(var key in this.postData)
		{			
			postParams += key + '=';
			
			//if string then write normally otherwise it's an 
			//element therefore determine type and act accordingly
			if(typeof(this.postData[key]) == 'string')
			{
				postParams += encodeURIComponent(this.postData[key]) + '&';	
			}
			else
			{
				var element = this.postData[key];
				var paramValue = '';
				
				if(element)
				{				
					if(element.type == 'text' || element.type == 'textarea')
					{
						paramValue = element.value;
					}
					else if(element.type == 'select-one')
					{
						paramValue = element.options[element.selectedIndex].text;
					}				
					else if(element.type == 'checkbox')
					{
						paramValue = element.checked;
					}
					else
					{
						paramValue = element.innerHTML;
					}
				}
				
				postParams += encodeURIComponent(paramValue) + '&';
			}		
			
		}
		
		//build error message text
		for(var key in this.requiredFields)		
		{
			var isEmail = false;
			var emailPrefix = "Email#";
			
			if(key.substring(0, 6) == emailPrefix)
			{
				key = key.substring(6, key.length);
				isEmail = true;
			}
			
			if($(key))
			{
				if($(key).type == 'text' || $(key).type == 'textarea')
				{
					if(($(key).value == "" && this.requiredFields[key] != "") && !isEmail)
					{
						errorMessage += this.requiredFields[key] + '\n';
						
					}
					else if(isEmail)
					{
						if(!checkEmail($(key).value))
						{
							errorMessage += this.requiredFields[emailPrefix + key] + '\n';
						}
					}
				}
				else if($(key).type == 'select-one')
				{
					if($(key).options[$(key).selectedIndex].value == "" && this.requiredFields[key] != "")
					{
						errorMessage += this.requiredFields[key] + '\n';
					}
				}
			}
		
		}
		
		//if form is correct then continue with posting of data else 
		//disply error message.
		if(this.errorAlertInline == 'ignore' || errorMessage.length == 0)
		{
			//show loader
			var loaderId = this.windowId + '-loader';
			var loader = $(loaderId);
			if(loader) {loader.toggle();}
			
			//hide body
			var bodyId = windowId + '-body';
			var element = $(bodyId);
			if(element) {element.toggle();}
		
			new Ajax.Request(
			this.url,
			{
				method: this.httpMethod,
				parameters: postParams,
				onSuccess: function(transport)
				{
					var loaderId = windowId + '-loader';
					var loader = $(loaderId);
					if(loader) {loader.toggle();}
					
					var successId = windowId + '-success';
					var success = $(successId);
					if(success) {success.toggle();}
					
					if (onSuccessCallback) { onSuccessCallback(transport); }
				},
				onFailure: function()
				{
					var loaderId = windowId + '-loader';
					var loader = $(loaderId);
					if(loader) {loader.toggle();}
					
					var failureId = windowId + '-failure';
					var failure = $(failureId);
					if(failure) {failure.toggle();}
					
					if (onFailureCallback) { onFailureCallback(); }
				}
			});
					
			
			 
		}
		else
		{
			// * popup alert with specified text
			if(this.errorAlertInline == 'alert')
			{
				alert(errorMessage);
			}	
			
			// * Add specified text to empty html tag in popup
			if(this.errorAlertInline == 'inline')
			{			
				var errId = this.windowId + '-errMessage';
				var element = $(errId);
				if(element)
				{
					element.innerHTML = errorMessage;
				}
			}	
			
		}
		
	}
}

function showInfoTypeHelp(divId)
{
    var divElement = $(divId);
    var helpTextElement = $('helpText');
    if (helpTextElement && divElement)
        helpTextElement.innerHTML = divElement.innerHTML;
    return false;
}


 