//  +-----------------------------------------------------------------------------------+
//  |  Copyright (c) Vantage Media Corporation 2006
//  +-----------------------------------------------------------------------------------+
//  |  This source file is bound by United States copyright law.
//  +-----------------------------------------------------------------------------------+
//  |  Author: fred
//  +-----------------------------------------------------------------------------------+
//  |  JavaScript common functions
//  +-----------------------------------------------------------------------------------+


Array.prototype.indexOf=function(n){for(var i=0;i<this.length;i++){if(this[i]===n){return i;}}return -1;}
Array.prototype.lastIndexOf=function(n){var i=this.length;while(i--){if(this[i]===n){return i;}}return -1;}
Array.prototype.forEach=function(f){var i=this.length,j,l=this.length;for(i=0;i<l;i++){if((j=this[i])){f(j);}}};
Array.prototype.insert=function(i,v){if(i>=0){var a=this.slice(),b=a.splice(i);a[i]=value;return a.concat(b);}}
Array.prototype.shuffle=function(){var i=this.length,j,t;while(i--){j=Math.floor((i+1)*Math.random());t=arr[i];arr[i]=arr[j];arr[j]=t;}}
Array.prototype.unique=function(){var a=[],i;this.sort();for(i=0;i<this.length;i++){if(this[i]!==this[i+1]){a[a.length]=this[i];}}return a;}
if(typeof Array.prototype.concat==='undefined'){Array.prototype.concat=function(a){for(var i=0,b=this.copy();i<a.length;i++){b[b.length]=a[i];}return b;};}
if(typeof Array.prototype.copy==='undefined'){Array.prototype.copy=function(a){var a=[],i=this.length;while(i--){a[i]=(typeof this[i].copy!=='undefined')?this[i].copy():this[i];}return a;};}
if(typeof Array.prototype.pop==='undefined'){Array.prototype.pop=function(){var b=this[this.length-1];this.length--;return b;};}
if(typeof Array.prototype.push==='undefined'){Array.prototype.push=function(){for(var i=0,b=this.length,a=arguments;i<a.length;i++){this[b+i]=a[i];}return this.length;};}
if(typeof Array.prototype.shift==='undefined'){Array.prototype.shift=function(){for(var i=0,b=this[0];i<this.length-1;i++){this[i]=this[i+1];}this.length--;return b;};}
if(typeof Array.prototype.slice==='undefined'){Array.prototype.slice=function(a,c){var i=0,b,d=[];if(!c){c=this.length;}if(c<0){c=this.length+c;}if(a<0){a=this.length-a;}if(c<a){b=a;a=c;c=b;}for(i;i<c-a;i++){d[i]=this[a+i];}return d;};}
if(typeof Array.prototype.splice==='undefined'){Array.prototype.splice=function(a,c){var i=0,e=arguments,d=this.copy(),f=a;if(!c){c=this.length-a;}for(i;i<e.length-2;i++){this[a+i]=e[i+2];}for(a;a<this.length-c;a++){this[a+e.length-2]=d[a-c];}this.length-=c-e.length+2;return d.slice(f,f+c);};}
if(typeof Array.prototype.unshift==='undefined'){Array.prototype.unshift=function(a){this.reverse();var b=this.push(a);this.reverse();return b;};}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
	 return true;
}

function time(){
	var d = new Date();
	var time = d.getTime();
	return time;
} 

function associate(object) {
	var keys = new Array;
	for(desc in object) {	  
	  keys.push(desc+'.'+object[desc]);
	}	
	return keys.toString();
}

//usage : getAge(year, month, day, unit, decimals, rounding)
//year should be a valid full year (ie: 1978)
//month should an integer from 1 to 12
//day should be a valid day of the month
//unit specifies the unit to display the age in. Valid values are "years", "months", or "days"
//decimals specifies the number of trailing decimals. Valid values are 0 and above
//rounding specifies whether to round up or down the age. Valid values are "roundup" or "rounddown"

function getAge(yr, mon, day, unit, decimal, round) {
	var one_day = 1000*60*60*24;
	var one_month = 1000*60*60*24*30;
	var one_year = 1000*60*60*24*30*12;

	today = new Date();
	var pastdate = new Date(yr, mon-1, day);

	var countunit = unit;
	var decimals = decimal;
	var rounding = round;

	finalunit = (countunit == "days")? one_day : (countunit=="months")? one_month : one_year;
	decimals = (decimals <= 0)? 1 : decimals*10;

	if(unit != "years") {
		if(rounding == "rounddown") {
			return Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals; //+" "+countunit;
		} else {
			return Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals; //+" "+countunit;
		}
	} else {
		yearspast = today.getFullYear()-yr-1;
		tail = (today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0;
		pastdate.setFullYear(today.getFullYear());
		pastdate2 = new Date(today.getFullYear()-1, mon-1, day);
		tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals;
		return yearspast+tail; //+" "+countunit;
	}
}
