Saving all occurences of Recurring events

I use the following procedure to add each separate event without deleting the master event.
The first problem was that in order to see the events correctly on screen I had to refresh the page myself. No big deal there.
The basic problem is that this procedure does not work all the time. Sometimes it will save all the separate events correctly. Sometimes it will stop before completing all events. It is driving me crazy since I can not trust it to work efficiently.
Use of firebug results in firefox crashing. I did catch once a message ‘this.getevent(a) is undefined’.
I could not think of a way to do this proccess on server side, mainly I could not link the getrecdates function or get the master id. Any suggestion there would also be welcomed.

This is the code

[code]

</code]
var dp = new dataProcessor(“php/events_rec.php”);
dp.init(scheduler);

	dp.attachEvent("onAfterUpdate", function(sid, action, tid, tag)
	{

		if (action == "inserted")
		{
			if(scheduler.getEvent(tid).rec_type!='' && scheduler.getEvent(tid).rec_type!='none')
			{
                    var eventText = scheduler.getEvent(tid).text;      
                    var repeatArray = new Array();
                    var repeatArray = scheduler.getRecDates(tid);
                    var repeatCount = repeatArray.length;
                    var new_id = scheduler.uid(); //added 
		var completed = scheduler.getEvent(tid).completed;
			
		for (var i = 0; i < repeatCount; i++)
		{
                      var start_date = repeatArray[i]['start_date'];
                      var end_date = repeatArray[i]['end_date'];
							  
                      scheduler.addEvent({
                            id:new_id, //added
                            start_date: start_date,
                            end_date: end_date,
                            text:eventText,		
                            event_length:start_date.valueOf()/1000,
                            rec_type:"",
			event_pid:tid,
			completed:completed
                        });
					  scheduler._add_rec_marker(scheduler.getEvent(new_id),start_date);
                      if (new_id!=tid){ //added
                            dp.setUpdated(new_id, true, "inserted"); //added
							}
                      new_id = scheduler.uid(); //added
                     }// render updated event
					alert(i);
					//scheduler.clearAll(); // delete all current event
					//scheduler.load("php/events_rec.php");
					 scheduler.render_view_data();
					 location.reload();
              }
              else return true;
		}    
	})

Thank you