// Whole-script strict mode syntax
//"use strict";
if (!window.console) {
window.console = {
log : function(s) {
// alert(s);
}
};
}
var app = {
ajax : {},
util : {}
};
(function($) {
jQuery.fn.visible = function() {
return this.css('visibility', 'visible');
};
jQuery.fn.invisible = function() {
return this.css('visibility', 'hidden');
};
jQuery.fn.visibilityToggle = function() {
return this.css('visibility', function(i, visibility) {
return (visibility == 'visible') ? 'hidden' : 'visible';
});
};
$(document).on('click', 'table.list_table tr', function(){
app.util.toggleCssClass(this, 'highlightSelectRow');
});
app.ajax.doAjaxCall = function(callURL, dataString, responsDivId, isToShowLoading) {
var showLoading = true;
if(null != isToShowLoading){
showLoading = isToShowLoading;
}
var startTime = +new Date();
$.ajax({
type : 'POST',
url : callURL,
data : dataString,
divId : responsDivId,
startTime : startTime,
beforeSend : function(request) {
if(showLoading){
$('#loading_div_wrapper').show();
}
},
complete : function(request) {
if(showLoading){
$('#loading_div_wrapper').hide();
}
},
success : function(html) {
var endTime = +new Date();
console.log('time required for ajax call =' + (endTime - this.startTime) / 1000.0 + 'sec');
app.util.handleResponse.call(this, html);
},
error : function(xhr, ajaxOptions, thrownError) {
app.util.showErrorMessage('Network is slow !! Please check with IT Support.');
console.log('Network is slow, Pleach check with IT Support !!! \n' + thrownError);
$('#loading_div_wrapper').hide();
}
});
return false;
};
app.util.handleResponse = function(html) {
html = app.ajax.clearResponse(html);
var id = this.divId;
var isMessagePresent = false;
var isBodyPresent = false;
if (html.indexOf('<div id=\"main_message_div_id\">') !== -1) {
isMessagePresent = true;
}
if (html.indexOf('<div class=\"center_content\" id=\"center_content\">') !== -1) {
isBodyPresent = true;
}
if (isMessagePresent && !isBodyPresent) {
$('#dummy_div').empty();
$('#dummy_div').html(html);
}
else {
$('#' + id).slideDown();
if (id == 'light_box_popup_content_div_iframe_wrapper') {
$('#light_box_popup_content_div').hide();
var element = document.getElementById('light_box_popup_content_div_iframe');
var doc = element.contentDocument;
if (doc == undefined || doc == null){
doc = element.contentWindow.document;
}
doc.body.innerHTML = html;
$('#light_box_popup_div').slideDown();
}else if (id == 'light_box_popup_content_div'){
$('#' + id).empty();
$('#' + id).html(html);
$('#light_box_popup_content_div_iframe_wrapper').hide();
$('#light_box_popup_div').slideDown();
}else{
$('#' + id).empty();
$('#' + id).html(html);
}
}
if (isMessagePresent) {
$('#main_message_div_id_wrapper').show();
setTimeout("$('#dummy_div').empty();$('#main_message_div_id_wrapper').slideUp()", 4000);
}
};
app.util.showHideDiv = function(id, effect) {
var obj = document.getElementById(id);
if (obj) {
if (effect == 'TOGGLE')
$(obj).toggle();
else if (obj.style.display == 'none') {
if (effect == 'FADE') {
$(obj).fadeIn();
}
if (effect == 'SLIDE')
$(obj).slideDown();
}
else {
if (effect == 'FADE')
$(obj).fadeOut();
if (effect == 'SLIDE')
$(obj).slideUp();
}
}
};
// Removes leading whitespaces
app.util.LTrim = function(value) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, '$1');
};
// Removes ending whitespaces
app.util.RTrim = function(value) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, '$1');
};
// Removes leading and ending whitespaces
app.util.trim = function(value) {
return app.util.LTrim(app.util.RTrim(value));
};
app.util.isEmpty = function(inputStr) {
if (null == inputStr || '' == app.util.trim(inputStr)) {
return true;
}
return false;
};
app.util.isNumber = function(inputStr) {
return !isNaN(parseFloat(inputStr)) && isFinite(inputStr);
};
app.util.replaceNonAlphaNum = function(inputStr) {
alert(inputStr);
if(!isEmpty(inputStr)){
return inputStr.replace(/[^a-zA-Z0-9-]/g,'');
}
alert(inputStr);
return inputStr;
};
app.ajax.clearResponse = function(html) {
if (html.indexOf('.dtd') != -1)
html = html.substr(html.indexOf('.dtd') + 6, html.length);
return html;
};
// Replaces commonly-used Windows 1252 encoded chars that do not exist in ASCII or ISO-8859-1 with ISO-8859-1 cognates.
app.util.replaceWordChars = function(text) {
var s = text;
// smart single quotes and apostrophe
s = s.replace(/[\u2018|\u2019|\u201A]/g, "\'");
// smart double quotes
s = s.replace(/[\u201C|\u201D|\u201E]/g, "\"");
// ellipsis
s = s.replace(/\u2026/g, "...");
// dashes
s = s.replace(/[\u2013|\u2014]/g, "-");
// circumflex
s = s.replace(/\u02C6/g, "^");
// open angle bracket
s = s.replace(/\u2039/g, "<");
// close angle bracket
s = s.replace(/\u203A/g, ">");
// spaces
s = s.replace(/[\u02DC|\u00A0]/g, " ");
return s;
};
app.ajax.doAjaxCallWithForm = function(formId, responsDivId) {
var str = "";
var arrElements = [];
var arrElementsValue = [];
var fobj = document.getElementById(formId);
for ( var i = 0; i < fobj.elements.length; i++) {
if (null != fobj.elements[i].type && undefined != fobj.elements[i].type && 'undefined' != fobj.elements[i].type && !$(fobj.elements[i]).hasClass('hidden')) {
if (fobj.elements[i].type.toLowerCase() == "text") {
str += fobj.elements[i].name + "=" + encodeURIComponent(app.util.replaceWordChars(fobj.elements[i].value)) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
else if (fobj.elements[i].type.toLowerCase() == "checkbox") {
if (fobj.elements[i].checked)
str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
else if (fobj.elements[i].type.toLowerCase() == "radio") {
if (fobj.elements[i].checked == true)
str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
else if (fobj.elements[i].type.toLowerCase() == "hidden") {
str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
else if (fobj.elements[i].type.toLowerCase() == "password") {
str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
else if (fobj.elements[i].tagName.toLowerCase() == 'select') {
var value = '';
if (fobj.elements[i].type.toLowerCase() == 'select-one') {
str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
value = fobj.elements[i].options[fobj.elements[i].selectedIndex].value;
}
else {
for ( var index = 0; index < fobj.elements[i].options.length; index++) {
if (fobj.elements[i].options[index].selected == true) {
str = str + fobj.elements[i].name + "=" + fobj.elements[i].options[index].value + "&";
value = value + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + ',';
}
}
}
arrElements.push(fobj.elements[i]);
arrElementsValue.push(value);
}
else if (fobj.elements[i].tagName.toLowerCase() == 'textarea') {
str += fobj.elements[i].name + "=" + encodeURIComponent(app.util.replaceWordChars(fobj.elements[i].value)) + "&";
arrElements.push(fobj.elements[i]);
arrElementsValue.push(fobj.elements[i].value);
}
}
}
str = str.substr(0, (str.length - 1));
if (!app.util.validateFields(arrElements, arrElementsValue)) {
return false;
}
app.ajax.doAjaxCall(fobj.action, str, responsDivId);
};
app.util.toTitle = function(str) {
str = typeof str === 'string' ? str : "";
return str.toLowerCase().replace(/\b\w/g, function(a, b) {
return a.toUpperCase();
});
};
app.util.clearForm = function(form) {
form.find('input:text, input:password, input:file, select, textarea').val('');
form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
};
app.util.clearFormWithHidden = function(form) {
app.util.clearForm(form);
form.find('input:hidden').val('');
};
app.util.validateFields = function(arrElements, arrElementsValue) {
var isValidationPassed = true;
for ( var i = 0; i < arrElements.length; i++) {
var elementObj = arrElements[i];
if ($(elementObj).attr('validation_type')) {
errorMessage = app.util.doValidation($(elementObj).attr('validation_type'), arrElementsValue[i]);
if (errorMessage) {
$(elementObj).addClass('mandatory_error');
isValidationPassed = false;
}
else {
$(elementObj).removeClass('mandatory_error');
}
}
}
if (!isValidationPassed) {
errorMessage = 'Please check form and data !!!!';
app.util.showErrorMessage(errorMessage);
}
return isValidationPassed;
};
app.util.showErrorMessage = function(errorMessage) {
$('#main_message_div_id2').empty();
$('#main_message_div_id2').html(errorMessage);
$('#main_message_div_id_wrapper2').slideDown();
setTimeout("$('#main_message_div_id2').empty();$('#main_message_div_id_wrapper2').slideUp()", 4000);
};
app.util.doValidation = function(validationType, value) {
var message = null;
switch (validationType) {
case 'string':
if (app.util.isEmpty(value)) {
message = 'field cant be empty';
}
break;
case 'number':
if (!app.util.isNumber(value)) {
message = 'field cant be empty and have numeric value';
}
break;
default:
message = 'check the validation type';
}
return message;
};
app.util.closeLightBoxDiv = function() {
$('#light_box_popup_div').hide('slow');
};
app.util.applyAutocomplete_dummy = function(xmlDataDivId, xmlProperty, inputElementId) {
app.util.applyAutocomplete($('#'+xmlDataDivId).html(), xmlProperty, inputElementId);
};
app.util.applyAutocomplete = function(xmlData, xmlProperty, inputElement) {
var authorArr = [];
xmlDoc = $.parseXML(xmlData),
$xml = $(xmlDoc),
$property = $xml.find(xmlProperty);
$property.each(function() {
authorArr.push($(this).text());
});
$(inputElement).autocomplete({
source : authorArr,
minLength : 1,
select : function(event, ui) {
$(inputElement).val(ui.item.value);
}
});
};
app.util.trackFormChanges = function(formId) {
$("#" + formId).data("formChanged", false);
if (formId == null) {
return;
}
var selectId = "#" + formId + " select";
var inputId = "#" + formId + " input";
var textAreaId = "#" + formId + " textarea";
$(document).on('change', selectId, function(){
$("#" + formId).data("formChanged", true);
});
$(document).on('change keypress', inputId, function(){
$("#" + formId).data("formChanged", true);
});
$(document).on('change keypress', textAreaId, function(){
$("#" + formId).data("formChanged", true);
});
};
app.util.toggleCssClass = function (elemObj, cssClass) {
$('.' + cssClass).removeClass(cssClass);
$(elemObj).addClass(cssClass);
};
app.util.doesExist = function(ref) {
return $(ref).length > 0;
};
app.util.isFormChanged = function(formId, tinyMceIdArray) {
if (formId == null) {
return false;
}
if (tinyMceIdArray != null) {
var i = 0;
for (i=0; i < tinyMceIdArray.length; i++) {
if (tinyMCE.get(tinyMceIdArray[i]).isDirty()) {
return true;
}
}
}
if ($("#" + formId).data("formChanged")) {
return true;
}
return false;
};
app.util.autofillTitle = function (element){
var urlToCall = $(element).val();
if(!app.util.isEmpty(urlToCall)) {
var titleElem = $('#'+$(element).attr('id')+'_title');
$(titleElem).attr('validation_type', 'string');
$(titleElem).addClass('mandatory');
$.ajax({
type : 'GET',
url : 'urlTitleAjaxCall.action',
data : 'url='+urlToCall,
beforeSend : function(request) {
$('#loading_div_wrapper').show();
},
success : function(html) {
$('#loading_div_wrapper').hide();
if(!app.util.isEmpty(html)){
$(titleElem).val(html);
}
},
error : function(xhr, ajaxOptions, thrownError) {
$('#loading_div_wrapper').hide();
alert('Server not responding !!! \n' + thrownError);
}
});
}else{
$(element).removeClass('mandatory');
$(element).removeClass('mandatory_error');
$(element).attr('validation_type', '');
$(element).val('');
var titleElem = $('#'+$(element).attr('id')+'_title');
$(titleElem).removeClass('mandatory');
$(titleElem).removeClass('mandatory_error');
$(titleElem).attr('validation_type', '');
$(titleElem).val('');
}
};
app.util.trackInputLength = function (selector) {
// set default content with some empty space,
// so that page does not flicker later
var defaultContent = ' ';
// function which actually does the work
showInputLength = function(e) {
var length = this.value.length;
if (length == 0) {
length = defaultContent;
} else if (length < 10) {
length = '0' + length;
}
$(this).siblings().first().html(length);
};
// register handler
$(selector).keyup(showInputLength);
// show the current input length
$(selector).each(function() {
$(this).after('<span>' + defaultContent + '<span>');
showInputLength.call(this);
});
};
app.util.updateCurrentSelected = function (className, elementToBeSelected) {
$('.'+className).removeClass(className);
$(elementToBeSelected).addClass(className);
};
app.util.showConfirmationBox = function (message, yesCallback, noCallback){
$('#customized_confirmation_box_wrapper').html(message);
$('#customized_confirmation_box_wrapper').dialog({
resizable: false,
height:160,
width:400,
modal: true,
closeOnEscape: true,
title:'Editor\'s Confirmation !',
buttons: {
'No': function() {
$( this ).dialog( "close" );
noCallback();
},
'Yes': function() {
$( this ).dialog( "close" );
yesCallback();
}
}
});
};
app.util.showAlertBox = function (message){
$('#customized_confirmation_box_wrapper').html(message);
$('#customized_confirmation_box_wrapper').dialog({
resizable: false,
height:160,
width:400,
modal: true,
closeOnEscape: true,
title:'System Alert !',
buttons: {
'Okey': function() {
$( this ).dialog( "close" );
}
}
});
};
app.util.alertConfirm = function (formId, tinyMceEditorId, fallbackFx){
var tinyId = null;
if(null != tinyMceEditorId){
tinyId = [tinyMceEditorId];
}
if (app.util.isFormChanged(formId, tinyId)) {
app.util.showConfirmationBox('Do you want to save your changes ?', function () {
$('#is_data_got_changed').val(true);
$('#'+formId).trigger('submit');
}, fallbackFx);
}else{
fallbackFx();
}
};
app.util.showHide = function (ele, key){
if($(ele).attr('id')=='show'){
$.cookie(key, true);
$(ele).hide();
$('#hide').show();
$('#search_form_wrapper').slideDown();
}else{
$.cookie(key, false);
$(ele).hide();
$('#show').show();
$('#search_form_wrapper').slideUp();
}
};
app.util.checkShowHide = function (key){
if($.cookie(key)=='false'){
$('#hide').hide();
$('#search_form_wrapper').hide();
}else{
$('#show').hide();
$('#search_form_wrapper').show();
}
};
/** For TinyMCE and ShowMore related link **/
app.util.backyardAppsTriggerSave = function () {
tinyMCE.triggerSave(false, true);
};
app.util.previewImage = function (imageSrc) {
$('#light_box_popup_content_div').empty();
$('#light_box_popup_content_div').append('<div style="text-align:center;"><img style="width:400px;" src="'+imageSrc+'"/></>');
$('#light_box_popup_div').show();
};
app.util.browser = function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
};
$(document).keyup(function(e) {
// close preview light box on ESCAPE key
if(e.which == 27){
$('#light_box_popup_div').hide('slow');
}
});
})(jQuery);