﻿/// <reference path="jquery/jquery-1.4.4.js" />
$(function() {
	$('a[rel*=facebox]').facebox();
});
String.format = function() {
	var s = arguments[0];
	for (var i = 0; i < arguments.length - 1; i++) {
		var reg = new RegExp("\\{" + i + "\\}", "gm");
		s = s.replace(reg, arguments[i + 1]);
	}

	return s;
};
String.prototype.escapeForAjax = function() {
	if (this == null) return null;
	return this.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
};
Communifire = function() {

	//private variables
	var virtualDirectory = VD;
	var locale = LOCALE;
	var modalContainerClass = 'generic-dialog';
	var loadingImage = '<img src="' + virtualDirectory + '/assets/images/icons/spinner.gif">';

	//private methods
	var buildAjaxUrl = function(path, reqPathMsg, basePath) {
		if (!path) {
			//The mandatory argument pageName is not present
			alert(reqPathMsg);
			return;
		}

		if (locale) {
			if (path.indexOf('?') > -1) {
				return String.format('{0}{1}{2}&locale={3}',
					VD, basePath, path, locale);
			}
			else {
				return String.format('{0}{1}{2}?locale={3}',
					VD, basePath, path, locale);
			}

		}
		else {
			return String.format('{0}{1}{2}',
					VD, basePath, path);
		}
	};
	//public variables and methods
	var my = {
		virtualDirectory: virtualDirectory,
		modalContainerClass: modalContainerClass,
		loadingImage: loadingImage,
		locale: locale,
		buildAjaxTemplatesUrl: function(pageName) {
			///	<summary>
			///		Builds the url for a particular ajax template page.
			///     If locale param is available via routing, that param will added as querystring
			///     to url. For example, if the local is 'en-US' and the page name is 'LatestArticlesList.aspx'
			///     this method will return '/AjaxTemplates/LatestArticlesList.aspx?locale=en-US'
			///	</summary>
			///	<param name="pageName" type="String">The name of the page</param>
			///	<returns type="String">The page url</returns>

			return buildAjaxUrl(pageName, 'Please provide a page name to call', '/AjaxTemplates/');
		},
		buildCommonWSUrl: function(methodName) {
			///	<summary>
			///		Builds the url by appending the method name to common webservice url.
			///     If locale param is available via routing, that param will added as querystring
			///     to url. For example, if the local is 'en-US' and the methodName is 'FindCountries'
			///     this method will return '/CommonWebService.asmx/FindCountries?locale=en-US'
			///	</summary>
			///	<param name="methodName" type="String">The name of the method of common webservice</param>
			///	<returns type="String">The common webservice url</returns>

			return buildAjaxUrl(methodName, 'Please provide a method name to call', '/webServices/CommonWebService.asmx/');
		},
		buildHTTPHandlersUrl: function(pageName) {
			///	<summary>
			///		Builds the url by appending the page name to HTTPHandlers directory url.
			///     If locale param is available via routing, that param will added as querystring
			///     to url. For example, if the local is 'en-US' and the methodName is 'AttachmentUploader.ashx'
			///     this method will return '/HTTPHandlers/AttachmentUploader.ashx?locale=en-US'
			///	</summary>
			///	<param name="pageName" type="String">The name of thepage</param>
			///	<returns type="String">The page url</returns>

			return buildAjaxUrl(pageName, 'Please provide a page name to call', '/HTTPHandlers/');
		},
		buildJsonResourcesUrl: function(pageName) {
			///	<summary>
			///		Builds the url for a particular json resource.
			///     If locale param is available via routing, that param will added as querystring
			///     to url. For example, if the local is 'en-US' and the page name is 'GetArticles.aspx'
			///     this method will return '/JsonResources/GetArticles.aspx?locale=en-US'
			///	</summary>
			///	<param name="pageName" type="String">The name of the page</param>
			///	<returns type="String">The page url</returns>
		return buildAjaxUrl(pageName, 'Please provide a page name to call', '/JsonResources/');
		},
		buildModalDialogsUrl: function(pageName) {
			///	<summary>
			///		Builds the url for a particular modal dialog.
			///     If locale param is available via routing, that param will added as querystring
			///     to url. For example, if the local is 'en-US' and the page name is 'GetArticles.aspx'
			///     this method will return '/ModalDialogs/FriendsModal.aspx?locale=en-US'
			///	</summary>
			///	<param name="pageName" type="String">The name of the page</param>
			///	<returns type="String">The page url</returns>
			return buildAjaxUrl(pageName, 'Please provide a page name to call', '/ModalDialogs/');
		}
	};
	return my;
} ();

Communifire.GlobalSetting = function() {
	var isFileControlEnabled;
	return {
		isFileControlEnabled: isFileControlEnabled
	};
};
Communifire.Utilities = function() {

	var _yaxisOffset = 100;
	var my =
	{
		checkAll: function(checkAllBox) {
			var frm = document.aspnetForm;
			var chkState = checkAllBox.checked;
			for (i = 0; i < frm.length; i++) {
				e = frm.elements[i];
				if (e.type == 'checkbox' && e.name.indexOf('Id') != -1) {
					e.checked = chkState;
				}
			}
		},
		checkChanged: function() {
			var frm = document.aspnetForm;
			var boolAllChecked;
			boolAllChecked = true;
			for (i = 0; i < frm.length; i++) {
				e = frm.elements[i];
				if (e.type == 'checkbox' && e.name.indexOf('Id') != -1) {
					if (e.checked == false) {
						boolAllChecked = false;
						break;
					}
				}
			}
			for (i = 0; i < frm.length; i++) {
				e = frm.elements[i];
				if (e.type == 'checkbox' && e.name.indexOf('checkAll') != -1) {
					if (boolAllChecked == false) {
						e.checked = false;
					} else {
						e.checked = true;
					} break;
				}
			}
		},
		fadeInfadeOutAnimation: function(obj) {
			if (obj instanceof jQuery) {
				obj.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
			}
			else {
				if (obj === null || obj === undefined) {
					$(obj).fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
				}
			}
		},
		getCenterXPosition: function(object) {
			var wi = $(window);
			var width = (wi.width() - object.width()) / 2 + wi.scrollLeft() + 'px';
			return width;
		},
		getCenterYPosition: function(object) {
			var wi = $(window);
			var height = (wi.height() - object.height()) / 2 + wi.scrollTop() - _yaxisOffset + 'px';
			return height;
		},
		getQuerystringParamValue: function(key, default_) {
			if (default_ == null) default_ = "";
			var keyInLower = key.toLowerCase();
			var url = window.location.href;
			var urlInLower = url.toLowerCase();
			key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
			var regex = new RegExp("[\\?&]" + keyInLower + "=([^&#]*)");
			var qs = regex.exec(urlInLower);
			if (qs == null) {
				return default_;
			}
			else {
				return qs[1];
			}
		},
		isValidEmail: function(email) {
			var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			return filter.test(email);
		},
		jumpPage: function(basepage, pageCount, suffix) {
			if (!document.getElementById('txtPager').value > pageCount) {
				window.location = basepage + document.getElementById('txtPager').value + suffix;
			}
		},
		print: function() {
			var disp_setting = "toolbar=no,location=yes,directories=no,menubar=no,";
			disp_setting += "scrollbars=yes,width=680, height=600, left=100, top=25, resizable=yes";
			var content_vlue = document.getElementById("print_content").innerHTML;
			var docprint = window.open("", "", disp_setting); docprint.document.open();
			docprint.document.write('<html><head><title>Print</title>');
			docprint.document.write(String.format('<link rel="stylesheet" href="{0}assets/css/styles.css" type="text/css" />', Communifire.virtualDirectory));
			docprint.document.write('</head><body style="padding:20px;" onLoad="self.print()">');
			docprint.document.write(content_vlue);
			docprint.document.write('</body></html>');
			docprint.document.close(); docprint.focus();
		},
		htmlEncode: function(value) {
			return $('<div/>').text(value).html();
		},
		htmlDecode: function(value) {
			return $('<div/>').html(value).text();
		}
	};
	return my;
} ();
//Usage
//Communifire.virtualDirectory
//Communifire.loadingImage
//Communifire.modalContainerClass
//Communifire.Utilities.getCenterXPosition
//Communifire.Utilities.getCenterYPosition
//Communifire.Utilities.getQuerystringParamValue
//Communifire.Utilities.jumpPage
//Communifire.Utilities.fadeInfadeOutAnimation
//Communifire.Utilities.print
//Communifire.Utilities.htmlEncode()
//Communifire.Utilities.htmlDecode()
//Communifire.buildCommonWSUrl('FindCountries');
//Communifire.buildAjaxTemplatesUrl('LatestArticlesList.aspx');
//Communifire.buildJsonResourcesUrl('GetArticles.aspx');
//Communifire.buildHTTPHandlersUrl('AttachmentUploader.ashx');
//Communifire.buildModalDialogsUrl('FriendsModal.aspx');
