Last active
March 4, 2016 14:59
-
-
Save nkt217/d698b729d71aa4eda23a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************* | |
Get state codes from name and vice-versa | |
*********************************/ | |
function ConvertStateNameTo(name, to) { | |
var name = name.toUpperCase(); | |
var states = new Array( | |
{'name':'Alabama', 'abbrev':'AL'}, | |
{'name':'Alaska', 'abbrev':'AK'}, | |
{'name':'Arizona', 'abbrev':'AZ'}, | |
{'name':'Arkansas', 'abbrev':'AR'}, | |
{'name':'California', 'abbrev':'CA'}, | |
{'name':'Colorado', 'abbrev':'CO'}, | |
{'name':'Connecticut', 'abbrev':'CT'}, | |
{'name':'Delaware', 'abbrev':'DE'}, | |
{'name':'Florida', 'abbrev':'FL'}, | |
{'name':'Georgia', 'abbrev':'GA'}, | |
{'name':'Hawaii', 'abbrev':'HI'}, | |
{'name':'Idaho', 'abbrev':'ID'}, | |
{'name':'Illinois', 'abbrev':'IL'}, | |
{'name':'Indiana', 'abbrev':'IN'}, | |
{'name':'Iowa', 'abbrev':'IA'}, | |
{'name':'Kansas', 'abbrev':'KS'}, | |
{'name':'Kentucky', 'abbrev':'KY'}, | |
{'name':'Louisiana', 'abbrev':'LA'}, | |
{'name':'Maine', 'abbrev':'ME'}, | |
{'name':'Maryland', 'abbrev':'MD'}, | |
{'name':'Massachusetts', 'abbrev':'MA'}, | |
{'name':'Michigan', 'abbrev':'MI'}, | |
{'name':'Minnesota', 'abbrev':'MN'}, | |
{'name':'Mississippi', 'abbrev':'MS'}, | |
{'name':'Missouri', 'abbrev':'MO'}, | |
{'name':'Montana', 'abbrev':'MT'}, | |
{'name':'Nebraska', 'abbrev':'NE'}, | |
{'name':'Nevada', 'abbrev':'NV'}, | |
{'name':'New Hampshire', 'abbrev':'NH'}, | |
{'name':'New Jersey', 'abbrev':'NJ'}, | |
{'name':'New Mexico', 'abbrev':'NM'}, | |
{'name':'New York', 'abbrev':'NY'}, | |
{'name':'North Carolina', 'abbrev':'NC'}, | |
{'name':'North Dakota', 'abbrev':'ND'}, | |
{'name':'Ohio', 'abbrev':'OH'}, | |
{'name':'Oklahoma', 'abbrev':'OK'}, | |
{'name':'Oregon', 'abbrev':'OR'}, | |
{'name':'Pennsylvania', 'abbrev':'PA'}, | |
{'name':'Rhode Island', 'abbrev':'RI'}, | |
{'name':'South Carolina', 'abbrev':'SC'}, | |
{'name':'South Dakota', 'abbrev':'SD'}, | |
{'name':'Tennessee', 'abbrev':'TN'}, | |
{'name':'Texas', 'abbrev':'TX'}, | |
{'name':'Utah', 'abbrev':'UT'}, | |
{'name':'Vermont', 'abbrev':'VT'}, | |
{'name':'Virginia', 'abbrev':'VA'}, | |
{'name':'Washington', 'abbrev':'WA'}, | |
{'name':'West Virginia', 'abbrev':'WV'}, | |
{'name':'Wisconsin', 'abbrev':'WI'}, | |
{'name':'Wyoming', 'abbrev':'WY'} | |
); | |
var returnthis = false; | |
$.each(states, function(index, value){ | |
if (to == 'name') { | |
if (value.abbrev == name){ | |
returnthis = value.name; | |
return false; | |
} | |
} else if (to == 'abbrev') { | |
if (value.name.toUpperCase() == name){ | |
returnthis = value.abbrev; | |
return false; | |
} | |
} | |
}); | |
return returnthis; | |
} | |
/********************************* | |
convert CSV data to object | |
*********************************/ | |
function processCSVData(allText) { | |
var allTextLines = allText.split(/\r\n|\n/); | |
var headers = allTextLines[0].split(','); | |
var lines = []; | |
for (var i=1; i<allTextLines.length; i++) { | |
var data = allTextLines[i].split(','); | |
if (data.length == headers.length) { | |
var tarr = []; | |
for (var j=0; j<headers.length; j++) { | |
tarr.push(data[j]); | |
} | |
lines.push(tarr); | |
} | |
} | |
return lines; | |
} | |
/********************************* | |
Adding a loader to freeze the body when getting ajax request | |
*********************************/ | |
function disableBodyWhileLoading(_bool) | |
{ | |
if(_bool) | |
{ | |
$("#loading").remove(); | |
var loading = $('<div>').prop('id', 'loading'); | |
loading.html('<div id="loading_box"><div class="spinner"><div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div><div class="rect5"></div></div><div id="loading_txt">Searching...</div></div>'); | |
loading.appendTo('body'); | |
$("#loading").bind('click', function(){}) | |
} | |
else | |
{ | |
$("#loading").remove(); | |
} | |
} | |
function updateLoaderTexts(_val) | |
{ | |
$("#loading").find('#loading_txt').html(_val); | |
} | |
/********************************* | |
extract domian name from the url | |
*********************************/ | |
function extractDomainName(_val) | |
{ | |
if(_val.indexOf("http") == -1) | |
_val = "https://"+_val; | |
var url = document.createElement('a'); | |
url.href = _val; | |
var host = url.hostname; | |
host = host.split('.'); | |
var domain = host.pop(); | |
domain = host.pop() + '.' + domain; | |
//var domain = host.join('.'); | |
return domain.split('.')[0]; | |
} | |
/************************************* | |
DATE MANIPULATION UTILITIES | |
*************************************/ | |
var dates = { | |
convert:function(d) { | |
// Converts the date in d to a date-object. The input can be: | |
// a date object: returned without modification | |
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11. | |
// a number : Interpreted as number of milliseconds | |
// since 1 Jan 1970 (a timestamp) | |
// a string : Any format supported by the javascript engine, like | |
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. | |
// an object : Interpreted as an object with year, month and date | |
// attributes. **NOTE** month is 0-11. | |
return ( | |
d.constructor === Date ? d : | |
d.constructor === Array ? new Date(d[0],d[1],d[2]) : | |
d.constructor === Number ? new Date(d) : | |
d.constructor === String ? new Date(d) : | |
typeof d === "object" ? new Date(d.year,d.month,d.date) : | |
NaN | |
); | |
}, | |
compare:function(a,b) { | |
// Compare two dates (could be of any type supported by the convert | |
// function above) and returns: | |
// -1 : if a < b | |
// 0 : if a = b | |
// 1 : if a > b | |
// NaN : if a or b is an illegal date | |
// NOTE: The code inside isFinite does an assignment (=). | |
return ( | |
isFinite(a=this.convert(a).valueOf()) && | |
isFinite(b=this.convert(b).valueOf()) ? | |
(a>b)-(a<b) : | |
NaN | |
); | |
}, | |
compareDate:function(a,b) { | |
// Compare two dates (could be of any type supported by the convert | |
// function above) and returns: | |
// -1 : if a < b | |
// 0 : if a = b | |
// 1 : if a > b | |
// NaN : if a or b is an illegal date | |
// NOTE: The code inside isFinite does an assignment (=). | |
a=this.convert(a); | |
b=this.convert(b); | |
if(a.getDate() === b.getDate() && a.getMonth() === b.getMonth() && a.getFullYear() === b.getFullYear()) | |
return 0; | |
else | |
return -1; | |
}, | |
inRange:function(d,start,end) { | |
// Checks if date in d is between dates in start and end. | |
// Returns a boolean or NaN: | |
// true : if d is between start and end (inclusive) | |
// false : if d is before start or after end | |
// NaN : if one or more of the dates is illegal. | |
// NOTE: The code inside isFinite does an assignment (=). | |
return ( | |
isFinite(d=this.convert(d).valueOf()) && | |
isFinite(start=this.convert(start).valueOf()) && | |
isFinite(end=this.convert(end).valueOf()) ? | |
start <= d && d <= end : | |
NaN | |
); | |
} | |
} | |
function stringToDate(_date, _format, _delimiter) | |
{ | |
var formatLowerCase=_format.toLowerCase(); | |
var formatItems=formatLowerCase.split(_delimiter); | |
var dateItems=_date.split(_delimiter); | |
var monthIndex=formatItems.indexOf("mm"); | |
var dayIndex=formatItems.indexOf("dd"); | |
var yearIndex=formatItems.indexOf("yyyy"); | |
var month=parseInt(dateItems[monthIndex]); | |
month-=1; | |
var formatedDate = new Date(dateItems[yearIndex],month,dateItems[dayIndex]); | |
return formatedDate; | |
} | |
function getWeekRange(_val, _year) | |
{ | |
var _start = moment(_year).week(_val).startOf("week").format(); | |
var _end = moment(_year).week(_val).endOf("week").format(); | |
return {start: _start, end: _end}; | |
} | |
function getMonthRange(_val, _year) | |
{ | |
var _start = moment(_year).month(_val-1).startOf("month").format(); | |
var _end = moment(_year).month(_val-1).endOf("month").format(); | |
return {start: _start, end: _end}; | |
} | |
function getQuarterRange(_val) | |
{ | |
if(_val <= 4) | |
{ | |
var t1 =((_val-1)*3); | |
return ([t1, (t1+1), (t1+2)]); | |
} | |
else | |
return "invalid details"; | |
} | |
function setQuarter() { | |
startMonth = 'january'; | |
var obj = {}; | |
if(startMonth=='january'){ | |
obj.quarter1 = {start:moment().month(0).startOf('month'),end:moment().month(2).endOf('month')} | |
obj.quarter2 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')} | |
obj.quarter3 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')} | |
obj.quarter4 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')} | |
return obj; | |
} | |
else if(startMonth=='april'){ | |
obj.quarter1 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')} | |
obj.quarter2 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')} | |
obj.quarter3 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')} | |
obj.quarter4 = {start:moment().month(0).startOf('month').add('years',1),end:moment().month(2).endOf('month').add('years',1)} | |
return obj; | |
} | |
} | |
function getYearRange(_year) | |
{ | |
var _now = new Date(_year); | |
return new Date(new Date(_now).setMonth(_now.getYear() - 1)); | |
} | |
Date.prototype.addDays = function(days) { | |
var dat = new Date(this.valueOf()) | |
dat.setDate(dat.getDate() + days); | |
return dat; | |
} | |
Date.prototype.removeDays = function(days) { | |
var dat = new Date(this.valueOf()) | |
dat.setDate(dat.getDate() - days); | |
return dat; | |
} | |
function getMonthName(_val) | |
{ | |
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
var monthShortNames = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; | |
var returnData = ''; | |
if(typeof _val == "string") | |
{ | |
_val = _val.toLowerCase(); | |
$.each(monthShortNames, function(index, value){ | |
if(_val == value) { | |
returnData = index; | |
return false | |
} | |
}) | |
} | |
if(typeof _val == "number") | |
{ | |
$.each(monthShortNames, function(index, value){ | |
if(_val == index) { | |
returnData = value; | |
return false | |
} | |
}) | |
} | |
return returnData; | |
} | |
function isValidDate(str) { | |
var d = moment(str, "D M YYYY") || moment(str, "M D YYYY") || moment(str, "D-M-YYYY") || moment(str, "M-D-YYYY"); | |
if(d == null || !d.isValid()) return false; | |
/*return str.indexOf(d.format('D/M/YYYY')) >= 0 | |
|| str.indexOf(d.format('DD/MM/YYYY')) >= 0 | |
|| str.indexOf(d.format('D/M/YY')) >= 0 | |
|| str.indexOf(d.format('DD/MM/YY')) >= 0;*/ | |
return true; | |
} | |
function human_time (time) { | |
time = Math.round(time); | |
var minutes = Math.floor(time / 60); | |
var seconds = time % 60; | |
return minutes.toString() + " min, " + seconds.toString() + " sec"; | |
} | |
function human_time_min (time) { | |
time = Math.round(time); | |
var minutes = Math.floor(time / 60); | |
minutes = ( minutes < 10 ? "0" : "" ) + minutes.toString(); | |
var seconds = time % 60; | |
seconds = ( seconds < 10 ? "0" : "") + seconds.toString(); | |
return minutes.toString() + ":" + seconds.toString(); | |
} | |
function human_time_class (time) { | |
time = time * 100000; | |
return "time" + time.toString().substring(0,5); | |
} | |
/********************************** | |
Objects Sorting functions | |
**********************************/ | |
function arrangeAscending(a,b) { | |
if(typeof a == "object" && typeof b == "object") | |
{ | |
if (a.count < b.count) | |
return -1; | |
if (a.count > b.count) | |
return 1; | |
return 0; | |
} | |
else | |
{ | |
if (a < b) | |
return -1; | |
if (a > b) | |
return 1; | |
return 0; | |
} | |
} | |
function arrangeDescending(a,b) { | |
if(typeof a == "object" && typeof b == "object") | |
{ | |
if (a.count > b.count) | |
return -1; | |
if (a.count < b.count) | |
return 1; | |
return 0; | |
} | |
else | |
{ | |
if (a > b) | |
return -1; | |
if (a < b) | |
return 1; | |
return 0; | |
} | |
} | |
/********************************** | |
Array Prototype Functions | |
**********************************/ | |
Array.prototype.contains = function(v) { | |
for(var i = 0; i < this.length; i++) { | |
if(this[i] === v) return true; | |
} | |
return false; | |
}; | |
Array.prototype.unique = function() { | |
var arr = []; | |
for(var i = 0; i < this.length; i++) { | |
if(!arr.contains(this[i])) { | |
arr.push(this[i]); | |
} | |
} | |
return arr; | |
} | |
function getNumberOfDistinctElements(_arr) | |
{ | |
var unique = {}; | |
var distinct = []; | |
for( var i in _arr ){ | |
if( typeof(unique[_arr[i].count]) == "undefined"){ | |
if(_arr[i].count != undefined || _arr[i].count != "undefined") distinct.push(_arr[i].count); | |
} | |
unique[_arr[i].count] = 0; | |
} | |
return distinct.length - 1; | |
} | |
function getDistinctElements(_arr) | |
{ | |
var unique = {}; | |
var distinct = []; | |
for( var i in _arr ){ | |
if( typeof(unique[_arr[i].count]) == "undefined"){ | |
if(_arr[i].count != undefined || _arr[i].count != "undefined") distinct.push(_arr[i].count); | |
} | |
unique[_arr[i].count] = 0; | |
} | |
return distinct.splice(0, distinct.length-1); | |
} | |
function splitSlice2(str, len) { | |
var _size = Math.ceil(str.length/len), | |
_ret = new Array(_size); | |
for (var _i=0; _i<_size; _i++) { | |
_ret[_i] = str.substr(_i*len, len); | |
} | |
return _ret; | |
} | |
/********************************** | |
Detect if a div can be scrolled or not | |
**********************************/ | |
(function($) { | |
$.fn.hasScrollBar = function() { | |
var e = this.get(0); | |
return { | |
vertical: e.scrollHeight > e.clientHeight, | |
horizontal: e.scrollWidth > e.clientWidth | |
}; | |
} | |
})(jQuery); | |
/********************************** | |
Number manipulations | |
**********************************/ | |
function formatNumber(nStr){ | |
nStr += ''; | |
var x = nStr.split('.'); | |
var x1 = x[0]; | |
var x2 = x.length > 1 ? '.' + x[1] : ''; | |
var rgx = /(\d+)(\d{3})/; | |
while (rgx.test(x1)) { | |
x1 = x1.replace(rgx, '$1' + ',' + '$2'); | |
} | |
return x1 + x2; | |
} | |
function getRandomNumbers(max, count){ | |
var arr = [] | |
while(arr.length < count){ | |
var randomnumber=Math.floor(Math.random()*max) | |
var found=false; | |
for(var i=0;i<arr.length;i++){ | |
if(arr[i]==randomnumber){found=true;break} | |
} | |
if(!found) arr[arr.length]=randomnumber; | |
} | |
return arr; | |
} | |
/********************************** | |
Calculate Age from date of birth | |
**********************************/ | |
function calculateAge (birthDate, otherDate) { | |
birthDate = new Date(birthDate); | |
if(otherDate != null) otherDate = new Date(otherDate); | |
else otherDate = new Date(); | |
var years = (otherDate.getFullYear() - birthDate.getFullYear()); | |
if (otherDate.getMonth() < birthDate.getMonth() || | |
otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) { | |
years--; | |
} | |
return years; | |
} | |
/********************************** | |
String Manipulation function | |
**********************************/ | |
String.prototype.capitalizeFirstLetter = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1); | |
} | |
String.prototype.stripCommas = function(){ | |
var _text = $.trim(this); | |
return _text.replace(/,\s*$/, ""); | |
} | |
/********************************** | |
DOM Manipulation function | |
**********************************/ | |
function centerGroupElementInSVG(element) { | |
$svg = $(element).parent(); | |
$(element).attr("transform", "translate(" + $svg.width() / 2 + ", " + $svg.height() / 2 + ")"); | |
} | |
/********************************** | |
JSON Manipulation function | |
**********************************/ | |
function IsJsonString(str) { | |
try { | |
JSON.parse(str); | |
} catch (e) { | |
return false; | |
} | |
return true; | |
} | |
Two years late, but I have the solution you're looking for. Not intending to take credit form the original author, here's a plugin which I found works exceptionally well for what you need, but gets all possible styles in all browsers, even IE. | |
Warning: This code generates a lot of output, and should be used sparingly. It not only copies all standard CSS properties, but also all vendor CSS properties for that browser. | |
jquery.getStyleObject.js: | |
/* | |
* getStyleObject Plugin for jQuery JavaScript Library | |
* From: http://upshots.org/?p=112 | |
*/ | |
(function($){ | |
$.fn.getStyleObject = function(){ | |
var dom = this.get(0); | |
var style; | |
var returns = {}; | |
if(window.getComputedStyle){ | |
var camelize = function(a,b){ | |
return b.toUpperCase(); | |
}; | |
style = window.getComputedStyle(dom, null); | |
for(var i = 0, l = style.length; i < l; i++){ | |
var prop = style[i]; | |
var camel = prop.replace(/\-([a-z])/g, camelize); | |
var val = style.getPropertyValue(prop); | |
returns[camel] = val; | |
}; | |
return returns; | |
}; | |
if(style = dom.currentStyle){ | |
for(var prop in style){ | |
returns[prop] = style[prop]; | |
}; | |
return returns; | |
}; | |
return this.css(); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment