// Copyright (C) 2008 BlueGolf, LLC. All Rights Reserved
function Text(source)
{var index=0;var lines=String(source).split(/\r\n|\n|\r/);this.hasNext=function()
{return(index<lines.length);}
this.next=function()
{var result=lines[index++];while(index<lines.length&&lines[index].length>0)
{var n=lines[index].charCodeAt(0);if(n==9||n==32)
{result=result+lines[index++].substr(1);continue;}
break;}
return result;}
this.back=function()
{while(index>0)
{if(lines[--index].length>0)
{var n=lines[index].charCodeAt(0);if(n==9||n==32)
{continue;}}
break;}}}
function Parameter(name,value)
{this.name=String(name);this.value=String(value);}
Parameter.prototype.toString=function()
{return this.name+'='+this.value;}
function Property(line)
{this.parameters=[];line=String(line);var delim=line.search(/:|;/);if(delim<0)
{delim=line.length;}
this.name=line.substring(0,delim);line=line.substr(delim);while(line.length>0&&line.charAt(0)==';')
{line=line.substr(1);delim=line.search(/:|;|=/);if(delim<0)
{delim=line.length;}
var param=line.substring(0,delim);line=line.substr(delim+1);if(line.length>0&&line.charAt(0)=='"')
{line=line.substr(1);delim=line.indexOf('"');if(delim<=0)
{delim=line.length;}}
else
{delim=line.search(/:|;/);if(delim<0)
{delim=line.length;}}
this.parameters[this.parameters.length]=new Parameter(param,line.substring(0,delim));if(line.charAt(delim)=='"')
{delim++;}
line=line.substr(delim);}
this.parameters.sort(function(a,b)
{var aValue=a.name.toUpperCase();var bValue=b.name.toUpperCase();if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
var aValue=a.value;var bValue=b.value;if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
return 0;});this.value=line.substr(1);}
Property.asDate=function(value)
{var result=undefined;value=String(value);if(value.length==8)
{result=new Date(value.substr(0,4),value.substr(4,2)-1,value.substr(6,2));}
else if(value.length==15)
{result=new Date(value.substr(0,4),value.substr(4,2)-1,value.substr(6,2),value.substr(9,2),value.substr(11,2),value.substr(13,2));}
return result;}
Property.escape=function(value)
{var result='';for(var i=0;i<value.length;i++)
{var c=value.charAt(i);if(c=='\n'||c=='\r')
{c='\\n';}
else if(c=='\\')
{c='\\\\';}
else if(c==';')
{c='\\;';}
else if(c==',')
{c='\\,';}
result=result+c;}
return result;}
Property.date2text=function(d)
{var result=undefined;if(d&&d.getFullYear)
{var year=String(10000+d.getFullYear());year=year.substr(year.length-4);var month=String(101+d.getMonth());month=month.substr(month.length-2);var day=String(100+d.getDate());day=day.substr(day.length-2);result=year+month+day;}
return result;}
Property.formatDate=function(d)
{var result=undefined;if(d&&d.getDay)
{var space=String.fromCharCode(160);result=Property.formatDate.days[d.getDay()]+','+space+Property.formatDate.mons[d.getMonth()]+space+d.getDate()+','+space+d.getFullYear();}
return result;}
Property.formatDate.days=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];Property.formatDate.mons=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];Property.formatTime=function(d)
{var result=undefined;if(d&&d.getHours)
{var hour=+d.getHours();var minute=String(100+d.getMinutes()).substr(1);if(hour>=12)
{var suffix='PM';hour=hour-12;}
else
{suffix='AM';}
if(hour==0)
{hour=12;}
result=hour+':'+minute+String.fromCharCode(160)+suffix;}
return result;}
Property.unescape=function(value)
{var result=undefined;if(value)
{result=String(value);var offset=result.indexOf('\\');while(offset>=0&&(offset+1)<result.length)
{var c=result.charAt(offset+1);if(c=='N'||c=='n')
{c='\n';}
result=result.substr(0,offset)+c+result.substr(offset+2);offset=result.indexOf('\\',offset+1);}}
return result;}
Property.prototype.getParameterValue=function(re)
{var result=undefined;for(var i=0;i<this.parameters.length;i++)
{if((re.test&&re.test(this.parameters[i].name))||re==this.parameters[i].name)
{result=String(this.parameters[i].value);break;}}
return result;}
Property.prototype.toString=function()
{var text=this.name;for(var i=0;i<this.parameters.length;i++)
{text=text+';'+this.parameters[i];}
text=text+':'+this.value+'\r\n';return text;}
function Component(text)
{this.components=[];this.properties=[];if(text&&text.hasNext)
{while(text.hasNext())
{var prop=new Property(text.next());if((/^BEGIN$/i).test(prop.name))
{if(this.name)
{text.back();this.components[this.components.length]=new Component(text);}
else
{this.name=prop.value;}}
else if((/^END$/i).test(prop.name))
{break;}
else
{this.properties[this.properties.length]=prop;}}
this.components.sort(function(a,b)
{var aValue=a.name.toUpperCase();var bValue=b.name.toUpperCase();if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
var aValue=a.getPropertyValue(/^DTSTART$/i);var bValue=b.getPropertyValue(/^DTSTART$/i);if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
var aValue=a.getPropertyValue(/^SUMMARY$/i);var bValue=b.getPropertyValue(/^SUMMARY$/i);if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
var aValue=a.getPropertyValue(/^N$/i);var bValue=b.getPropertyValue(/^N$/i);if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
return 0;});this.properties.sort(function(a,b)
{var aValue=a.name.toUpperCase();var bValue=b.name.toUpperCase();if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
var aValue=Property.unescape(a.value);var bValue=Property.unescape(b.value);if(aValue<bValue)
{return-1;}
else if(aValue>bValue)
{return 1;}
return 0;});}
else
{this.name=text;}}
Component.inherit=function(){};Component.inherit.prototype=Component.prototype;Component.prototype.getComponent=function(re)
{var result=undefined;for(var i=0;i<this.components.length;i++)
{var comp=this.components[i];var uid=comp.getPropertyValue(/^UID$/i);if((re&&re.test&&re.test(uid))||re==uid)
{result=comp;break;}}
return result;}
Component.prototype.getProperties=function(re)
{var result=[];for(var i=0;i<this.properties.length;i++)
{if((re.test&&re.test(this.properties[i].name))||re==this.properties[i].name)
{result[result.length++]=this.properties[i];}}
return result;}
Component.prototype.getProperty=function(re)
{var result=undefined;for(var i=0;i<this.properties.length;i++)
{if((re.test&&re.test(this.properties[i].name))||re==this.properties[i].name)
{result=this.properties[i];break;}}
return result;}
Component.prototype.getPropertyValue=function(re)
{var result=undefined;var prop=this.getProperty(re);if(prop)
{result=Property.unescape(prop.value);}
return result;}
Component.prototype.toString=function()
{var text='BEGIN:'+this.name+'\n';for(var i=0;i<this.properties.length;i++)
{text=text+this.properties[i];}
for(var i=0;i<this.components.length;i++)
{text=text+this.components[i];}
text=text+'END:'+this.name+'\r\n';return text;}