$(document).ready(function(){ fAddTypes(); // Autofocus fix $(this).find('[autofocus]').focus(); $('.modal').on('shown.bs.modal', function() { $(this).find('[autofocus]').focus(); }); // popup hover (info) boxes $('[data-toggle="popover"]').popover(); // tooltips $('[data-toggle="tooltip"]').tooltip(); }); // Initialise a global variable for a File Reader var $oFileReader; // Initialise a global variable for any file inputs to test var $oFileInputToTest; function fAddTypes() { // If the data type is 'tabs', make this the container of jQuery Tabs if($('div[data-type="tabs"]').not('[data-type-initialised]').length) { $('div[data-type="tabs"]').not('[data-type-initialised]').tabs(); $('div[data-type="tabs"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } // If the data type is 'date', make this a datepicker input box if($('input[data-type="date"]').not('[data-type-initialised]').length) { $('input[data-type="date"]').not('[data-type-initialised]').datepicker(); $('input[data-type="date"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } // If the data type is 'validated-value', time to add validation! if($('input[data-type="validated-value"]').not('[data-type-initialised]').length) { // Add a function for these input boxes for whenever a button is pressed $('input[data-type="validated-value"]').not('[data-type-initialised]').on("keyup", function(){ // Make an error message var sMessage = ""; // Does this input only allow numeric values? if($(this)[0].hasAttribute("data-must-be-numeric") || $(this)[0].hasAttribute("data-cant-be-negative")) { // We only allow numerics, is it numeric and we're still looking for an issue? if(sMessage=="" && !$.isNumeric($(this).val())) { sMessage = "Must be a number"; } } // Does this input only allow numeric values greater than or equal to 0? if($(this)[0].hasAttribute("data-cant-be-negative")) { // We only allow numerics, is it numeric and we're still looking for an issue? if(sMessage=="" && (!$.isNumeric($(this).val()) || $(this).val() < 0)) { sMessage = "Must be greater than or equal to 0"; } } // Does this input need to be greater than another input? if($(this)[0].hasAttribute("data-greater-than-input")) { // We only allow numerics, is it numeric and we're still looking for an issue? console.log($(this).attr("data-greater-than-input")); if(sMessage=="" && (!$.isNumeric($(this).val()) || parseFloat($(this).val()) < parseFloat($("input[name='"+$(this).attr("data-greater-than-input")+"']").val()))) { sMessage = "Must be greater than "+$(this).attr("data-greater-than-input-label"); } } // Has this failed validation? If so set a data attribute and display the message if(sMessage != "") { if(!$("div[data-parent='"+$(this).attr("name")+"']").length) { $(this).attr("data-failed-validation", "1"); var oAlert = document.createElement("div"); oAlert.className = "input-validation-message"; oAlert.setAttribute("data-parent", $(this).attr("name")); oAlert.setAttribute("style", 'top:'+($(this).offset().top+$(this).outerHeight()+10)+'px;left:'+($(this).offset().left)+'px;'); $(this).after(oAlert); } $("div[data-parent='"+$(this).attr("name")+"']").html(sMessage); } else { $(this).removeAttr("data-failed-validation"); $("div[data-parent='"+$(this).attr("name")+"']").remove(); } }); $('input[data-type="validated-value"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } // If the data type is 'validated-image', add validation to its onchange event if($('input[data-type="validated-image"]').not('[data-type-initialised]').length) { // If the file reader object hasn't been initialised if($oFileReader == null) { // Initialise it $oFileReader = new FileReader; } // For each validated-image object, if it changes $("body").on("change", 'input[data-type="validated-image"]', function(){ // Set the currently being tested object to this file input $oFileInputToTest = this; // Declare flags and values for checking the min width and min height $bMinWidth = 0; $nMinWidth = 0; $bMinHeight = 0; $nMinHeight = 0; // If this file input has a min width if(typeof $(this).attr("data-minwidth") !== undefined && typeof $(this).attr("data-minwidth") !== "undefined") { // Set the min width flag to 1 $bMinWidth = 1; // Set the min width to be the minwidth data attribute $nMinWidth = $(this).attr("data-minwidth"); } // If this file input has a min height if(typeof $(this).attr("data-minheight") !== undefined && typeof $(this).attr("data-minheight") !== "undefined") { // Set the min height flag to 1 $bMinHeight = 1; // Set the min height to be the minheight data attribute $nMinHeight = $(this).attr("data-minheight"); } // When a file is loaded into the File Reader $oFileReader.onload = function() { // Create a new image object to read the image from the file reader var img = new Image; // When an image has been loaded img.onload = function() { // Initialise flags to check whether it has failed checks $bWidthFail = 0; $bHeightFail = 0; // If we're testing the width, and the width is too small if($bMinWidth && img.width<$nMinWidth) { // Set the width failure flag to 1 $bWidthFail = 1; } // If we're testing the height, and the height is too small if($bMinHeight && img.height<$nMinHeight) { // Set the height failure flag to 1 $bHeightFail = 1; } // If either of our flags have been set to 1 if($bWidthFail||$bHeightFail) { // Initialise a message telling them the image is too small var sMessage = "The image you selected is too small, it needs to be at least "; // Initialise a message telling them the dimensions it should be var sDimensions = ""; // If this image has a min width if($bMinWidth) { // Add the min width to the dimensions message sDimensions = $nMinWidth+"px wide"; } // If this image has a min height if($bMinHeight) { // If it also had a min width that we added a message for, stick the word 'by' between the two if($bMinWidth) sDimensions += " by "; // Add the min height to the dimensions message sDimensions += $nMinHeight+"px high"; } // Alert the dimensions message alert(sMessage+sDimensions); // Clear the field by wrapping it in a form and resetting the form // File input values are protected and can't be changed for obvious security reasons $($oFileInputToTest).wrap('
').closest('form').get(0).reset(); $($oFileInputToTest).unwrap(); } }; // Set the source of the image to be the result from the file reader reading the file img.src = $oFileReader.result; }; // Check the file is less than 10mb, else throw up an error if(this.files[0].size >= 10000000) { alert("Please choose an image that is less than 10MB."); $(this).wrap('').closest('form').get(0).reset(); $(this).unwrap(); } else { // Get the File Reader to read the file from the input box $oFileReader.readAsDataURL(this.files[0]); } }); $('input[data-type="validated-image"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } if($('input[data-type="ph9date"]').not('[data-type-initialised]').length) { $('input[data-type="ph9date"]').not('[data-type-initialised]').each(function(){ $(this).datepicker({ dateFormat: "d M yy", changeMonth: true, changeYear: true, altField: $(this).data("alt"), altFormat: "yymmdd", onClose: function( selectedDate ) { var $toDate = $(this).parent().next().children(); $toDate.datepicker( "option", "minDate", selectedDate ); setTimeout(function(){$toDate.datepicker('show');},0); // Puts text focus on next date range - made unneccesaary by setTimeout which // fixes problem of 2nd datepicker flashing when trying to change the month //$(this).parent().next().children().focus(); } }); }); $('input[data-type="ph9date"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } // If the data type is 'wysiwyg', make this a WYSIWYG box if($('textarea[data-type="wysiwyg"]').not('[data-type-initialised]').length) { $('textarea[data-type="wysiwyg"]').not('[data-type-initialised]').each(function(){ CKEDITOR.replace($(this).attr("name")); }); $('textarea[data-type="wysiwyg"]').not('[data-type-initialised]').attr("data-type-initialised", "1"); } /* If a field is marked as required=true, create an alert telling the user. * * This was created due to Safari not supporting the required attribute. */ $("form").submit(function(e) { var ref = $(this).find("[required]"); $(ref).each(function(){ if ( $(this).val() == '' ) { alert("Required field should not be blank. Please make sure all mandatory fields in the form are filled in."); $(this).focus(); e.preventDefault(); return false; } }); return true; }); }