4 _App.Util = function ()
15 initialize: function ()
17 this.startupTime = this.unixTimestamp();
20 unixTimestamp: function ()
22 return Math.round((new Date().getTime() / 1000));
25 getUrlParam: function (name)
27 hashLoc = window.location.hash.split("?");
29 (RegExp(name + '=' + '(.+?)(&|$)').exec(hashLoc[1]) || [, null])[1]
37 div = document.createElement('div'),
38 all = div.getElementsByTagName('i');
40 div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
51 updateCheckboxStyle: function ($_checkbox)
53 if ($_checkbox.hasClass('no-mod'))
57 var label = $_checkbox.parent('label');
60 $(label).addClass('checkbox');
64 $_checkbox.wrap('<label class="checkbox"/>');
66 $('<span class="styled-checkbox" />').insertAfter($_checkbox);
67 if (!!this.ie && this.ie < 9)
69 $_checkbox.set_checked = function () {
72 form.find('input[name="' + this.name + '"] + span').removeClass('checked');
73 $(this).next('span').addClass('checked').blur();
76 $_checkbox.set_checked();
77 $_checkbox.bind('change', function () {
81 if ($_checkbox.value == 'on')
83 $_checkbox.value = true;
87 updateCheckboxStyles: function (form)
89 var $$_checkboxes = form.find('input[type=checkbox]');
90 $.each($$_checkboxes, function (i, checkbox) {
91 var $_checkbox = $(checkbox);
92 if ($_checkbox.hasClass('no-mod'))
96 var label = $_checkbox.parent('label');
99 $(label).addClass('checkbox');
103 $_checkbox.wrap('<label class="checkbox"/>');
105 $('<span class="styled-checkbox" />').insertAfter($_checkbox);
106 if (!!this.ie && this.ie < 9)
108 checkbox.set_checked = function () {
111 form.find('input[name="' + this.name + '"] + span').removeClass('checked');
112 $(this).next('span').addClass('checked').blur();
115 checkbox.set_checked();
116 $_checkbox.bind('change', function () {
120 if (checkbox.value == 'on')
122 checkbox.value = true;
127 updateTimeLeft: function ()
129 $('.time-left-dynamic').each(function()
131 var value = $(this).attr('data-value');
132 var numDays = $(this).attr('data-days');
133 var endDate = $(this).attr('data-date');
135 App.Util.calculateTimeLeft(
145 calculateTimeLeft: function (value, numDays, endDate, isUpdate)
147 var dateTime = value.split(' '),
150 dateParts = date.split('-').map(function (part) {
151 return parseInt(part, 10);
153 timeParts = time.split(':').map(function (part) {
154 return parseInt(part, 10);
156 var timeObject = new Date();
157 var currentDateTime = new Date(timeObject.getTime() - (1000 * App.timeDiff));
160 var dateTime = endDate.split(' '),
163 dateParts = date.split('-').map(function (part) {
164 return parseInt(part, 10);
166 timeParts = time.split(':').map(function (part) {
167 return parseInt(part, 10);
169 var expireDateTime = new Date(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]);
173 var dateTime = value.split(' '),
176 dateParts = date.split('-').map(function (part) {
177 return parseInt(part, 10);
179 timeParts = time.split(':').map(function (part) {
180 return parseInt(part, 10);
182 var expireDateTime = new Date(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]);
183 expireDateTime.setDate(expireDateTime.getDate() + parseInt(numDays));
185 var difference = expireDateTime.getTime() - currentDateTime.getTime();
186 var daysDifference, hoursDifference, minutesDifference;
188 daysDifference = Math.floor(difference / 1000 / 60 / 60 / 24);
189 difference -= daysDifference * 1000 * 60 * 60 * 24;
190 hoursDifference = Math.floor(difference / 1000 / 60 / 60);
191 difference -= hoursDifference * 1000 * 60 * 60;
192 minutesDifference = Math.floor(difference / 1000 / 60);
194 if (expireDateTime.getTime() < currentDateTime.getTime())
199 var result = daysDifference > 0
200 ? daysDifference + 'd, ' + hoursDifference + 'h, ' + minutesDifference + 'm'
201 : hoursDifference + 'h, ' + minutesDifference + 'm';
206 return '<span class="time-left-dynamic" '
207 + 'data-value="' + value + '" '
208 + 'data-days="' + numDays + '" '
209 + 'data-date="' + endDate + '">'
210 + result + '</span>';
213 calculateTimeLeftHours: function (value, numHours)
215 var dateTime = value.split(' '),
218 dateParts = date.split('-').map(function (part) {
219 return parseInt(part, 10);
221 timeParts = time.split(':').map(function (part) {
222 return parseInt(part, 10);
224 var timeObject = new Date();
225 var currentDateTime = new Date(timeObject.getTime() - (1000 * App.timeDiff));
226 var expireDateTime = new Date(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]);
227 var addSeconds = expireDateTime.setSeconds(expireDateTime.getSeconds() + numHours * 3600);
228 expireDateTime = new Date(expireDateTime.getSeconds() + addSeconds);
229 //console.log('expireDateTime2',expireDateTime2 ) ;
230 var difference = expireDateTime.getTime() - currentDateTime.getTime();
231 var daysDifference, hoursDifference, minutesDifference;
233 daysDifference = Math.floor(difference / 1000 / 60 / 60 / 24);
234 difference -= daysDifference * 1000 * 60 * 60 * 24;
235 hoursDifference = Math.floor(difference / 1000 / 60 / 60);
236 difference -= hoursDifference * 1000 * 60 * 60;
237 minutesDifference = Math.floor(difference / 1000 / 60);
238 difference -= minutesDifference * 1000 * 60;
240 if (expireDateTime.getTime() < currentDateTime.getTime())
245 return daysDifference > 0
246 ? ((daysDifference * 2) + hoursDifference) + 'h, ' + minutesDifference + 'm'
247 : hoursDifference + 'h, ' + minutesDifference + 'm';
249 /*? daysDifference + 'd, ' + hoursDifference + 'h, ' + minutesDifference + 'm'
250 : hoursDifference + 'h, ' + minutesDifference + 'm';*/
253 updateClock: function ()
255 var timeObject = new Date();
256 var currentTime = new Date(timeObject.getTime() - (1000 * App.timeDiff));
257 var currentHours = currentTime.getHours();
258 var currentMinutes = currentTime.getMinutes();
259 var currentSeconds = currentTime.getSeconds();
260 currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
261 currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
262 currentHours = ( currentHours == 0 ) ? 12 : currentHours;
263 var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
264 $("#clock").html(currentTimeString);
265 $("#clockmobi").html(currentTimeString);
266 if (parseInt(currentSeconds) < App.Util.theSecond)
268 App.Util.updateTimeLeft();
270 App.Util.theSecond = parseInt(currentSeconds);
273 setCookie: function (name, value, days)
277 var date = new Date();
278 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
279 var expires = "; expires=" + date.toGMTString();
285 document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
288 getCookie: function (name, defaultValue)
290 var nameEQ = escape(name) + "=";
291 var ca = document.cookie.split(';');
292 for (var i = 0; i < ca.length; i++)
295 while (c.charAt(0) == ' ')
297 c = c.substring(1, c.length);
299 if (c.indexOf(nameEQ) == 0)
301 return unescape(c.substring(nameEQ.length, c.length));
304 return undefined == defaultValue
309 formatDate: function (formatDate, formatString)
311 if (formatDate instanceof Date)
313 var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
314 var yyyy = formatDate.getFullYear();
315 var yy = yyyy.toString().substring(2);
316 var m = formatDate.getMonth() + 1;
317 var mm = m < 10 ? "0" + m : m;
319 var d = formatDate.getDate();
320 var dd = d < 10 ? "0" + d : d;
322 var h = formatDate.getHours();
323 var hh = h < 10 ? "0" + h : h;
324 var n = formatDate.getMinutes();
325 var nn = n < 10 ? "0" + n : n;
326 var s = formatDate.getSeconds();
327 var ss = s < 10 ? "0" + s : s;
329 formatString = formatString.replace(/yyyy/i, yyyy);
330 formatString = formatString.replace(/yy/i, yy);
331 formatString = formatString.replace(/mmm/i, mmm);
332 formatString = formatString.replace(/mm/i, mm);
333 formatString = formatString.replace(/m/i, m);
334 formatString = formatString.replace(/dd/i, dd);
335 formatString = formatString.replace(/d/i, d);
336 formatString = formatString.replace(/hh/i, hh);
337 formatString = formatString.replace(/h/i, h);
338 formatString = formatString.replace(/nn/i, nn);
339 formatString = formatString.replace(/n/i, n);
340 formatString = formatString.replace(/ss/i, ss);
341 formatString = formatString.replace(/s/i, s);
353 String.prototype.replaceAll = function (str1, str2, ignore)
355 return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
358 jQuery.validator.addMethod("notNull", function (value, element, params) {
359 return this.optional(element) || value != null;
360 }, "Please select a value."
363 jQuery.validator.addMethod("mobile", function (value, element, params) {
364 return this.optional(element) || value.match(/^[0-9]\d{8}$/) != null;
365 }, "Must be a valid mobile number."
368 jQuery.validator.addMethod("companyReg", function (value, element, params) {
369 return this.optional(element) || value.match(/^\d{4}\/\d{6}\/\d{2}$/) != null;
370 }, "Incorrect format."
373 $.validator.setDefaults({
375 errorPlacement: function (error, element) {
376 //error.prepend('<br/>');
377 if (element.hasClass('selectpicker'))
379 var elem = $('button[data-id=' + element.attr("name") + ']');
380 elem.addClass('error');
381 error.insertAfter(elem);
385 error.insertAfter(element);
388 unhighlight: function (element, errorClass, validClass) {
389 if ($(element).hasClass('selectpicker'))
391 $('button[data-id=' + $(element).attr("name") + ']').removeClass(errorClass).addClass(validClass);
395 $(element).removeClass(errorClass).addClass(validClass);
397 $(element.form).find("label[for=" + element.id + "]." + errorClass)
398 .remove();//.removeClass(errorClass);
405 $.fn.setCursorPosition = function (pos) {
406 if ($(this).get(0).setSelectionRange)
408 $(this).get(0).setSelectionRange(pos, pos);
410 else if ($(this).get(0).createTextRange)
412 var range = $(this).get(0).createTextRange();
413 range.collapse(true);
414 range.moveEnd('character', pos);
415 range.moveStart('character', pos);