if(window.location.hash) {
	if ("cat!" == self.document.location.hash.substring(1,5)) {
		window.location.href = self.document.location.hash.substring(5);
	}
}


function InputNumeric(inputs){
	$(inputs).keypress(function(e){
		if((e.which<48 || e.which > 57) && e.which != 8 ){
			e.cancel = true;
			return false;
		}
	});
}

function getUrlParams(url){//renvoie un tableau contenant arguments de l'url fournie par clef=nom => valeur=valeur
	var args 	= 	new Array();
	var temp 	= 	new Array();	
	var temp2 	= 	new Array();
	
	temp = url.split("?");
	temp = temp[1].split("&");
	for(var i = 0; i < temp.length; i++){
		temp2 = temp[i].split("=");
		args[temp2[0]] = temp2[1];
	}
	
	return args;
	
}

JSONstring={
	compactOutput:false, 		
	includeProtos:false, 	
	includeFunctions: false,
	detectCirculars:true,
	restoreCirculars:true,
	make:function(arg,restore) {
		this.restore=restore;
		this.mem=[];this.pathMem=[];
		return this.toJsonStringArray(arg).join('');
	},
	toObject:function(x){
		if(!this.cleaner){
			try{this.cleaner=new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')}
			catch(a){this.cleaner=/^(true|false|null|\[.*\]|\{.*\}|".*"|\d+|\d+\.\d+)$/}
		};
		if(!this.cleaner.test(x)){return {}};
		eval("this.myObj="+x);
		if(!this.restoreCirculars || !alert){return this.myObj};
		if(this.includeFunctions){
			var x=this.myObj;
			for(var i in x){if(typeof x[i]=="string" && !x[i].indexOf("JSONincludedFunc:")){
				x[i]=x[i].substring(17);
				eval("x[i]="+x[i])
			}}
		};
		this.restoreCode=[];
		this.make(this.myObj,true);
		var r=this.restoreCode.join(";")+";";
		eval('r=r.replace(/\\W([0-9]{1,})(\\W)/g,"[$1]$2").replace(/\\.\\;/g,";")');
		eval(r);
		return this.myObj
	},
	toJsonStringArray:function(arg, out) {
		if(!out){this.path=[]};
		out = out || [];
		var u; // undefined
		switch (typeof arg) {
		case 'object':
			this.lastObj=arg;
			if(this.detectCirculars){
				var m=this.mem; var n=this.pathMem;
				for(var i=0;i<m.length;i++){
					if(arg===m[i]){
						out.push('"JSONcircRef:'+n[i]+'"');return out
					}
				};
				m.push(arg); n.push(this.path.join("."));
			};
			if (arg) {
				if (arg.constructor == Array) {
					out.push('[');
					for (var i = 0; i < arg.length; ++i) {
						this.path.push(i);
						if (i > 0)
							out.push(',\n');
						this.toJsonStringArray(arg[i], out);
						this.path.pop();
					}
					out.push(']');
					return out;
				} else if (typeof arg.toString != 'undefined') {
					out.push('{');
					var first = true;
					for (var i in arg) {
						if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue};
						this.path.push(i);
						var curr = out.length; 
						if (!first)
							out.push(this.compactOutput?',':',\n');
						this.toJsonStringArray(i, out);
						out.push(':');                    
						this.toJsonStringArray(arg[i], out);
						if (out[out.length - 1] == u)
							out.splice(curr, out.length - curr);
						else
							first = false;
						this.path.pop();
					}
					out.push('}');
					return out;
				}
				return out;
			}
			out.push('null');
			return out;
		case 'unknown':
		case 'undefined':
		case 'function':
			if(!this.includeFunctions){out.push(u);return out};
			arg="JSONincludedFunc:"+arg;
			out.push('"');
			var a=['\n','\\n','\r','\\r','"','\\"'];
			arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
			out.push(arg);
			out.push('"');
			return out;
		case 'string':
			if(this.restore && arg.indexOf("JSONcircRef:")==0){
				this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj."));
			};
			out.push('"');
			var a=['\n','\\n','\r','\\r','"','\\"'];
			arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
			out.push(arg);
			out.push('"');
			return out;
		default:
			out.push(String(arg));
			return out;
		}
	}
};

function LimitToDecimal(field) {
	
	var val = field.value;
	val = val.replace(/,/g, '.');
	val = val.replace(/[^0-9\.]/g, '');
	field.value = val;
	
}


function comaField(champ) {
	var valeur = champ.value;
	pos = valeur.indexOf(",");
	if (pos >= 0) {
		valeur = valeur.substr(0, pos) + "." + valeur.substr(pos+1);
	}
	champ.value = valeur;
}

function DateFrToDate(date) {
	date = date.replace(/^0+/g,"");
	date = date.replace(/\/0+/g,"/");
	var s = date.split('/');
	return new Date(parseInt(s[2]), parseInt(s[1])-1, parseInt(s[0]));
}

function DateFrToSql(date) {
	var s = date.split('/');
	return s[2]+"-"+s[1]+"-"+s[0];
}

function TimeMinuteToSql(hm) {
	var hmpos = hm;
	if (hmpos < 0) hmpos = -hmpos;
	var h = Math.floor(hmpos/60);
	var m = hmpos%60;
	if (h < 10) h = '0' + h;
	if (m < 10) m = '0' + m;
	return (hm<0?'-':'') + h + ':' + m;
}

function DateToFr(date) {
	var day = (date.getDate()).toString();
	var month = (date.getMonth()+1).toString();
	var year = (date.getFullYear()).toString();
	if (day.length < 2) day = '0' + day;
	if (month.length < 2) month = '0' + month;
	return day + '/' + month + '/' + year;
}

function TimeSqlToMinute(t) {
	var s = t.split(':');
	return parseInt(s[0].replace(/^0/g,""))*60 + parseInt(s[1].replace(/^0/g,""));
}


function urlRemoveVariable(url, variable) {
	return url.replace(new RegExp(variable + '=[^&]*', 'g'), '').replace(new RegExp('&+', 'g'), '&').replace(new RegExp('&$', 'g'), '');
}
function urlRemoveVariables(url, variables) {
	for(var variable in variables)
		url = urlRemoveVariable(url, variable);
	
	return url;
}
function urlAddVariable(url, variable, value) {	
	url = urlRemoveVariable(url, variable);	
	if (url.indexOf('?',0) === false) return url + '?' + variable + '=' + value;
	else	return url + '&' + variable + '=' + value;
}
function Post(url, args) {
	var id = 'frmpostid' + Math.random();
	var html = '<form action="'+url+'" method="post" id="'+id+'">';
	for (key in args) {
		html += '<input type="hidden" name="'+key+'" value="'+args[key]+'" />';
	}
	html += '</form>';
	$('body').append(html);
	document.getElementById(id).submit();
}
function Get(url, args){
	url += "?";
	for (key in args)	url += "&" + key + "=" + args[key];
	document.location = url;
}


function connectPopupDisplayDriver(popup, splash){
	
	splash.height($(document).height());
	splash.width($(document).width());
	
	$(document).resize(function(){
		splash.height($(document).height());
		splash.width($(document).width());
	});
	
	var windowsHeight	= $(window).height();
	var windowsWidth	= $(window).width();
	
	var contentDivHeight	= popup.height(); 
	var contentDivWidth		= popup.width();
	
	var height	= windowsHeight	- contentDivHeight;		
	var width	= windowsWidth	- contentDivWidth;

	if($('body').height() < windowsHeight)	splash.height(windowsHeight);
	else 		splash.height($('body').height());
 
	//le test sur 0 blinde le cas ou height ou width est inferieur a 0
	popup.css('top',height > 0 ? height/2 : 0);
	popup.css('left',width > 0 ? width/2 : 0);						
 
	splash.show();
	popup.fadeIn();
 
	splash.click(function(){
		popup.hide();
		splash.fadeOut();
	});
 
	$(window).scroll(function(e){								
		bodyHeight		= $('body').height();
		bodyWidth		= $('body').width();
		height			= windowsHeight	- contentDivHeight;		
		width			= windowsWidth	- contentDivWidth;
		if(windowsHeight > contentDivHeight)
			popup.css('top',height/2 + $(this).scrollTop());
		
		if(windowsWidth > contentDivWidth)
			popup.css('left',width/2 + $(this).scrollLeft());				
		
	});
 
	$(window).resize(function(){
		windowsHeight	= $(this).height();
		windowsWidth	= $(this).width();					
		bodyHeight		= $('body').height();
		bodyWidth		= $('body').width();
		height			= windowsHeight	- contentDivHeight;		
		width			= windowsWidth	- contentDivWidth;		
 
		splash.height($(document).height());
		splash.width($(document).width());
 
		//le test sur 0 blinde le cas ou height ou width est inferieur a 0
		popup.css('top',height > 0 ? height/2 : 0);
		popup.css('left',width > 0 ? width/2 : 0);
	});
}

function currency(prix) {

	var str = (Math.round(prix*100)/100).toString().replace(/\./, ',');
	var nbDecimales = str.indexOf(',') == -1 ? 0 : str.length - str.indexOf(',') - 1;
	if (nbDecimales == 0) str += ',';
	for (nbDecimales; nbDecimales < 2; nbDecimales++)
		str += '0';

	return str;
	
}
