function createCookie(name,value,days){if(days){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();}else var expires = "";document.cookie = name+"="+value+expires+"; path=/";}
function readCookie(name) {var nameEQ = name + "=";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++){var c = ca[i];while (c.charAt(0)==' ') c = c.substring(1,c.length);if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);}return null;}
function eraseCookie(name){createCookie(name,"",-1);}

/*Debug Functions*/
function try_clear(obj){if(typeof(console)==='object'){if(typeof(console.clear)==='function'){console.clear();}}}

function try_debug(obj){if(document.getElementById('_debug_console')){document.getElementById('_debug_console').innerHTML += obj + '<br/>';document.getElementById('_debug_console').scrollTop = document.getElementById('_debug_console').scrollHeight;}else{if(typeof(console)==='object'){if(typeof(console.info)==='function'){console.info(obj);}}}}
function try_info(obj){if(typeof(console)==='object'){if(typeof(console.info)==='function'){console.info(obj);}}}
function try_warning(obj){if(typeof(console)==='object'){if(typeof(console.warn)==='function'){console.warn(obj);}}}
function try_error(obj){if(typeof(console)==='object'){if(typeof(console.error)==='function'){console.error(obj);}}}
function try_group(name){if(typeof(console)==='object'){if(typeof(console.group)==='function'){console.group(name);}}}
function try_group_end(){if(typeof(console)==='object'){if(typeof(console.groupEnd)==='function'){console.groupEnd();}}}
function try_time(name){
	if(document.getElementById('_debug_console')){
		if(!this.timers){this.timers = {};}
		this.timers[name] = new Date(); 
		this.timers[name].getTime();
	}else{
		if(typeof(console)==='object'){
			if(typeof(console.time)==='function'){
				console.time(name);
			}
		}		
	}
}

function try_time_end(name){
	if(document.getElementById('_debug_console')){
		var now = new Date(); 
		now = now.getTime();
		var time = now - this.timers[name];
		try_debug(name+': ' + time + ' ms');
	}else{
		if(typeof(console)==='object'){
			if(typeof(console.timeEnd)==='function'){
				console.timeEnd(name);
			}
		}
	}
}

/*Reads querystring from window.location*/
function queryString(ji) {hu = window.location.search.substring(1);gy = hu.split("&");for (i=0;i<gy.length;i++) {ft = gy[i].split("=");if (ft[0] == ji) {return ft[1];}}}

/*Returns XmlHttpRequest (cross browser)*/
function StandardXMLHttpRequest(){if(window.XMLHttpRequest){try{return new XMLHttpRequest();}catch(e){return false;}} else if(window.ActiveXObject){try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){return false;}}}else{return false;}}

/*Validate multilevel JSON input(root, 'child1.child2.child3(etc)')*/
function object_test(obj,children){
	if(typeof(obj)==='undefined'){
		return false;
	}else if(!obj){
		return false;
	}
	children = (children)?children.split('.'):[];
	for(var i in children){
		if(typeof(obj[children[i]])==='undefined'){
			obj = 0
		}else{
			obj = obj[children[i]];
		}
		if(!obj){
			return false;
		}
	}
	return true;
}

/*Check if val is numeric*/
function isNumeric(val){
	//return !isNaN(val*1);
	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; 
	return String(val).match(RegExp);
}

/*Remove duplicates from array*/
function array_unique(a){
	var r = new Array();
	o:for(var i = 0, n = a.length; i < n; i++){
		for(var x = 0, y = r.length; x < y; x++){
			if(r[x]==a[i]) continue o;
		}
		r[r.length] = a[i];
	}return r;
}

function array_filter(array, key, value){
	var tmp = [];
	for(var i in array){
		if(array[i][key]){
			value = value+',';
			if(value.indexOf(array[i][key].toString()) > -1){
				tmp.push(array[i]);
			}
		}
	}
	return tmp;
}

array_diff = function(arr1,arr2){
	var a=[], diff = [];
	for(var i=0; i<arr1.length;i++)
		a[arr1[i]]=true;
	for(var i=0;i<arr2.length;i++)
    	if(a[arr2[i]]) delete a[arr2[i]];
		else a[arr2[i]]=true;
	for(var k in a)
    	diff.push(k);
  		return diff;
}


/*Get Current Server Time (Uses local time if session.time does not exist)*/
function get_server_time(){
	try{
		this.server_time = (this.server_time) ? this.server_time : new Date(session.time.year, session.time.month-1, session.time.day, session.time.hour, session.time.minute, session.time.second);
	}catch(e){
		this.server_time = (this.server_time) ? this.server_time : new Date();
	}
	this.client_time = (this.client_time) ? this.client_time : new Date();
	var current_client_time = new Date();
	var diff = Date.parse(current_client_time) - Date.parse(this.client_time);
	return new Date(Date.parse(this.server_time) + diff);
}

/*jTemplate System*/
function jTemplate(o){
	
	if(o.text){
		template = o.text;
	}else if(o.dom){
		if(!this.cache){this.cache = {};}
		if(this.cache[o.dom]){
			template = this.cache[o.dom];
		}else{
			var template = document.getElementById(o.dom);
			if(template){
				template = this.cache[o.dom] = template.innerHTML;
			}else{
				template = '<span class="error template_not_found">Template Not Found: '+o.dom+'</span>';
			}
		}
	}else{
		try_info(o);
		throw('jTemplate Error: You must define either template text or DOM!');
	}
	
	
	/*Process Conditional Vars*/
	var fks = template.match(/\{{2}(.*?)\}{2}/igm);
	if(fks === null){fks = [];}else{fks = array_unique(fks);}
	for(var i in fks){
		
		var fks_sub = fks[i].match(/\[(.*?)\]/igm);
		if(fks_sub === null){
			template = template.replace(fks[i], '');
		}else{
			var varname_sub = fks_sub[0].replace(/\[(.*?)\]/igm,'$1');
			if(o.data[varname_sub]){
				fks[i].replace(fks_sub[0], o.data[varname_sub]);
			}else{
				template = template.replace(fks[i], '');
			}
		}
	}
	
	template = template.replace(/\{{2}/igm, '');
	template = template.replace(/\}{2}/igm, '');
	
	/*Process Vars*/
	var fks = template.match(/\[(.*?)\]/igm);
	if(fks === null){fks = [];}else{fks = array_unique(fks);}
	for(var i in fks){
		var val = fks[i];
		var varname = val.replace(/\[(.*?)\]/igm,'$1');
		var pattern = new RegExp('\\['+varname+'\\]', 'igm');
		template = template.replace(pattern, (o.data[varname])?o.data[varname]:'');
	}
	
	if(o.sec_data){
		/*Process Functions*/
		var fks = template.match(/\{(.*?)\}/igm);
		if(fks === null){fks = [];}else{fks = array_unique(fks);}
		for(var i in fks){
			var val = fks[i];
			var fks_sub = val.match(/\(.*?\)/igm)[0];
			var func = val.replace(fks_sub,'').replace(/[\{|\}]/g,'');
			if(typeof(window[func])==='function'){
				template = template.replace(val, window[func](o.data[fks_sub.replace(/[\(|\)]/g,'')], o.sec_data));
			}
		}
	}
	
	return template.replace(/xsrc=/igm, 'src=');
}



