/*
	This function has used in different TPL files to make the logic generic and to remove the duplicacy of code	
*/

var login_page_location = '';
var dir_name = 'mlsrealestatevideos';
var host_name = window.location.host;
host_name = host_name.toLowerCase();

if(host_name == 'localhost')
{
	login_page_location = window.location.protocol + '//' + window.location.host + '/' + dir_name + '/web/home/signin';
}
else
{
	login_page_location = window.location.protocol + '//' + window.location.host + '/' + 'web/home/signin';
}

function AjaxRequest(MethodName, ScriptName, QueryString, AjaxLoaderHTML, TargetDiv)
{
	jQuery.ajax(
	{
		type: MethodName,
		url: ScriptName,
		data: QueryString,
		beforeSend: function()
		{
			jQuery("#"+TargetDiv).html(AjaxLoaderHTML);
		},
		complete: function()
		{
			//jQuery("#"+TargetDiv).html('');
		},
		success: function(htmlData)
		{
			RedirectAfterSessionExpired(htmlData, TargetDiv, false);
		}
	});		
}

//Do not show AjaxLoader
function AjaxRequestWithoutLoader(MethodName, ScriptName, QueryString, TargetDiv)
{
	jQuery.ajax(
	{
		type: MethodName,
		url: ScriptName,
		data: QueryString,
		success: function(htmlData)
		{
			RedirectAfterSessionExpired(htmlData, TargetDiv, false);			
		}
	});			
}

function AjaxRequestWithShowHideLoader(MethodName, ScriptName, QueryString, AjaxLoaderId, TargetDiv)
{
	jQuery.ajax(
	{
		type: MethodName,
		url: ScriptName,
		data: QueryString,
		beforeSend: function()
		{
			jQuery("#"+AjaxLoaderId).show();
		},
		complete: function()
		{
			jQuery("#"+AjaxLoaderId).hide();
		},
		success: function(htmlData)
		{
			RedirectAfterSessionExpired(htmlData, TargetDiv, false);
		}
	});		
}

function AjaxRequestForHtmlAppend(MethodName, ScriptName, QueryString, AjaxLoaderHTML, AjaxLoaderDivId, TargetDiv)
{
	jQuery.ajax(
	{
		type: MethodName,
		url: ScriptName,
		data: QueryString,
		beforeSend: function()
		{
			jQuery("#"+TargetDiv).append(AjaxLoaderHTML);
		},
		complete: function()
		{
			jQuery("#"+AjaxLoaderDivId).remove();
		},
		success: function(htmlData)
		{
			RedirectAfterSessionExpired(htmlData, TargetDiv, true);
		}
	});		
}

//To redirect after session expire
function RedirectAfterSessionExpired(htmlData, TargetDiv, HtmlAppend)
{
	if(htmlData == "SESSION EXPIRED")
	{
		alert("Your session has expired.\nPlease login again.");
		window.location.href = login_page_location;
	}
	else
	{
		if(HtmlAppend == true)
		{
			jQuery("#"+TargetDiv).append(htmlData);
		}
		else
		{
			jQuery("#"+TargetDiv).html(htmlData);
		}
	};
}
