function icalDateParser(iCalDate){
	var d = new Date();
	d.setUTCFullYear(iCalDate.value.substr(0, 4), parseInt(iCalDate.value.substr(4, 2), 10) - 1, iCalDate.value.substr(6, 2));
	d.setUTCHours(iCalDate.value.substr(9,2), iCalDate.value.substr(11, 2) , iCalDate.value.substr(13, 2));
	return d;
}
var icalParser={
	ical:{
		version:'',
		prodid:'',
		events:[],
		todos:[],
		journals:[],
		freebusys:[]
	},
	parseIcal: function(icsString){
		this.ical.version=this.getValue('VERSION',icsString);
		this.ical.prodid=this.getValue('PRODID',icsString);
		
		var reg=/BEGIN:VEVENT(\r?\n[^B].*)+/g;
		var matches=icsString.match(reg);
		if(matches){
			for(i=0;i<matches.length;i++){
				//console.log(matches[i]);
				this.parseVevent(matches[i]);
			}
		}
		console.log('parsed');
		//alert(this.ical.events[0].start.params.valeurs);
	},
	parseVevent: function(veventString){
		////PROCHAINE VERSION: Générer seul les propriétés trouvées : + rapide
		var event={
			id:this.getValue('UID',veventString).value.substr(0, 5), //This property defines the persistent, globally unique identifier for the calendar component.
			title:this.getValue('SUMMARY',veventString).value.substr(0, this.getValue('SUMMARY',veventString).value.length - 1), //This property defines a short summary or subject for the calendar component.
			start:icalDateParser(this.getValue('DTSTART',veventString)), //This property specifies when the calendar component begins.
			end:icalDateParser(this.getValue('DTEND',veventString)) //This property specifies the date and time that a calendar component ends.
		};
		this.ical.events[this.ical.events.length]=event;
	},
	getValue: function(propName,txt,multiple){
		if(multiple){
			eval('var matches=txt.match(/\\n'+propName+'[^:]*/g)');
			var props=[];
			if(matches){
				for(l=0;l<matches.length;l++){
					//on enleve les parametres 
					matches[l]=matches[l].replace(/;.*/,'');
					props[props.length]=this.getValue(matches[l],txt);
				}
				return props;
			}
		}else{
			var reg=new RegExp('('+propName+')(;[^=]*=[^;:\n]*)*:([^\n]*)','g');
			var matches=reg.exec(txt);
			if(matches){ //on a trouvé la propriété cherchée
				var valeur=RegExp.$3;
				var tab_params;
				if(RegExp.$2.length>0){ //il y a des paramètres associés
					var params=RegExp.$2.substr(1).split(';');
					var pair;var code='';
					for(k=0;k<params.length;k++){
						pair=params[k].split('=');
						if(!pair[1]) pair[1]=pair[0];
						code+=pair[0].replace(/-/,'')+' : "'+pair[1]+'", '; 
					}
					eval('tab_params=( { '+code.substr(0,code.length-2)+' } );');
				}
				//console.log(propName+' '+valeur+'\n'+toJsonString(tab_params));
				return {
					value:valeur,
					params:tab_params
				};
			}else{
				return null;
			}
		}
	}
}
