var xmlHttp

function checkCal(str)
{
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="/cgi/getcal.py";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    var spaceLen = str.split('%20').length;
    var lineLen = str.split('%0A').length;
    //str = str.replace( "%0a", "\\n");
    var strLen = 0;
    (spaceLen > lineLen) ? strLen = spaceLen : strLen = lineLen;
    var clientDate = new Date();
    url=url+"&clientDate="+clientDate;
    //debug
    //document.getElementById("testid").innerHTML=strLen + clientDate
    if (strLen > 2) {
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
        document.getElementById("disabledInput").value="Thinking...";
    }
} 

function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
            //document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
        var responseLen = xmlHttp.responseText.length;
        if (responseLen > 15) {
            // document.getElementById("disabledInput").disabled=0;
            document.getElementById("disabledInput").value="Calendar Ready!";
            //document.getElementById("testid3").innerHTML='<form><input type="submit" name="iCal" value="Outlook Calendar / iCal" onclick=popICal()></form>'
            var fileNameIdx = xmlHttp.responseText.indexOf("TMPFILE:");
            var fileName = xmlHttp.responseText.slice(fileNameIdx+8,fileNameIdx+23);
            document.getElementById("testid3").innerHTML='<a href="http://www.poseeq.com/tmp/'+fileName+'"><img name="Outlook" src="outlookical.gif" width=90 alt="Outlook or ICal"></a>'
            //document.getElementById("testid3").innerHTML=fileName;
            var rruleIdx = (xmlHttp.responseText.indexOf("RRULE:"));
            if (rruleIdx == -1) {
                //document.getElementById("testid2").innerHTML='<form><input type="button" name="gcal" value="Google Calendar" onclick=popGoogleCal()></form>'
                document.getElementById("testid2").innerHTML='<form><input type="image" name="gcal" width="90" src="GoogleCalendar.gif" value="Google Calendar" onclick=popGoogleCal()></form>'
                //document.getElementById("testid4").innerHTML='<form><input type="button" name="ycal" value="Yahoo! Calendar" onclick=popYahooCal()></form>'
                document.getElementById("testid4").innerHTML='<form><input type="image" name="ycal" width="90" src="YahooCalendar.gif" value="Yahoo! Calendar" onclick=popYahooCal()></form>'
            }
        } else if ((responseLen > 1) || (responseLen == 0)) {
            document.getElementById("disabledInput").value="Could not parse";
        }
        // For debug
        //document.getElementById("testid").innerHTML=xmlHttp.responseText + responseLen;
    }
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function stateChangedHelp() 
{ 
    if (xmlHttp.readyState==4)
    { 
        //document.getElementById("testid").innerHTML=xmlHttp.responseText;
        var disInp = document.getElementById("disabledInput");
        //working document.getElementById("disabledInput").disabled=False;
        disInp.disabled=False;
        //document.getElementById("parsedCal").value=xmlHttp.responseText;
    }
}

function getCal()
{
    //alert("Content-type: text/calendar\n");
    //alert(xmlHttp.responseText);
    document.getElementById("parsedCal").value=xmlHttp.responseText;
    var url="/cgi/poseeq.py";
    url=url+"?q="+xmlHttp.responseText+"&type=calendar";
    url=url+"&sid="+Math.random();
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function popICal()
{
    //alert(xmlHttp.responseText);
    //var pop1 = window.open("", "win1");
    //var doc = pop1.document.open("text/html", "replace");
    //doc.writeln(xmlHttp.responseText);
    //doc.close();
    //pop1.focus();
    //xmlHttp2=GetXmlHttpObject();
    var url="http://www.poseeq.com/cgi/poseeq.py";
    url=url+"?parsedCal="+xmlHttp.responseText;
    url=url+"&type=calendar";
    url=url+"&sid="+Math.random();
    window.open(url, 'ical');
    //xmlHttp.open("GET",url,true);
    //xmlHttp.send(null);
}
function popGoogleCalNew()
{
    //alert(xmlHttp.responseText);
    //var pop1 = window.open("", "win1");
    //var doc = pop1.document.open("text/html", "replace");
    //doc.writeln(xmlHttp.responseText);
    //doc.close();
    //pop1.focus();
    //xmlHttp2=GetXmlHttpObject();
    var url="https://www.google.com/calendar/upload_event";
    url=url+'?Content-Type: multipart/form-data; boundary=---------------------------327572003712859 Content-Length:' + (xmlHttp.responseText).length + '-----------------------------327572003712859 Content-Disposition: form-data; name="filename"; filename="poseeq.ics" Content-Type: text/calendar' + '-----------------------------327572003712859 Content-Disposition: form-data; name="hl" en -----------------------------327572003712859 Content-Disposition: form-data; name="src" ZHIudW5uYXRpQGdtYWlsLmNvbQ -----------------------------327572003712859 Content-Disposition: form-data; name="ctz" America/Los_Angeles -----------------------------327572003712859 Content-Disposition: form-data; name="lef" OTAyMWI1c25oaXByaDM5NXRpNzRiaWVibW52ajQ3ZTdAaW1wb3J0LmNhbGVuZGFyLmdvb2dsZS5jb20 -----------------------------327572003712859 Content-Disposition: form-data; name="lef" ZHIudW5uYXRpQGdtYWlsLmNvbQ -----------------------------327572003712859 Content-Disposition: form-data; name="droi" 20100118T000000/20100705T000000 -----------------------------327572003712859 Content-Disposition: form-data; name="secid" IQYcA9MZYsT8paGc_fIr2RtCjPw -----------------------------327572003712859-- ';
    window.open(url, 'ical');
    //xmlHttp.open("GET",url,true);
    //xmlHttp.send(null);
}
function popGoogleCal()
{
    var unEscXml = unescape(xmlHttp.responseText)
    var dtStartIdx= (unEscXml.indexOf("DTSTART:"));
    var dtEndIdx= (unEscXml.indexOf("DTEND:"));
    var descIdx= (unEscXml.indexOf("DESCRIPTION:"));
    var summIdx= (unEscXml.indexOf("SUMMARY:"));
    var UIDIdx= (unEscXml.indexOf("UID:"));
    var rruleIdx= (unEscXml.indexOf("RRULE:"));
    var dtStart = (unEscXml.slice(dtStartIdx+8,dtStartIdx+23));
    var dtEnd = (unEscXml.slice(dtEndIdx+6,dtEndIdx+21));
    var desc = (unEscXml.slice(descIdx+12,dtEndIdx-1)).replace("\\n", "<br>");
    var summ = (unEscXml.slice(summIdx+8,UIDIdx-1));
    var recurrence = (unEscXml.slice(rruleIdx+6,summIdx-1));
    desc = summ + "%0A___________________________________%0Awww.poseeq.com - Itinerary to Calendar.";
    //var gcalURI = "http://www.google.com/calendar/event?action=TEMPLATE&text=" + summ + "%21&dates=" + dtStart + "Z/" + dtEnd + "Z&details=" + desc + "&location=None&trp=false&sprop=&sprop=name:";
    var gcalURI = "http://www.google.com/calendar/event?action=TEMPLATE&text=" + summ + "&dates=" + dtStart + "/" + dtEnd + "&details=" + desc + "&location=None&trp=false&sprop=&sprop=name:";
    window.open(gcalURI, 'gcal');
}
function popYahooCal()
{
    var unEscXml = unescape(xmlHttp.responseText)
    /*
    var dtStartIdx= (xmlHttp.responseText.indexOf("DTSTART:"));
    var dtEndIdx= (xmlHttp.responseText.indexOf("DTEND:"));
    var descIdx= (xmlHttp.responseText.indexOf("DESCRIPTION:"));
    var summIdx= (xmlHttp.responseText.indexOf("SUMMARY:"));
    var UIDIdx= (xmlHttp.responseText.indexOf("UID:"));
    var dtStart = (xmlHttp.responseText.slice(dtStartIdx+8,dtStartIdx+23));
    var dtEnd = (xmlHttp.responseText.slice(dtEndIdx+6,dtEndIdx+21));
    var olddesc = (xmlHttp.responseText.slice(descIdx+12,dtEndIdx));
    var desc = olddesc.replace("\\n", "%0a");
    var summ = (xmlHttp.responseText.slice(summIdx+8,UIDIdx-1));
    summ = summ.replace("\\n", "%0a");
    */
    var dtStartIdx= (unEscXml.indexOf("DTSTART:"));
    var dtEndIdx= (unEscXml.indexOf("DTEND:"));
    var descIdx= (unEscXml.indexOf("DESCRIPTION:"));
    var summIdx= (unEscXml.indexOf("SUMMARY:"));
    var UIDIdx= (unEscXml.indexOf("UID:"));
    var dtStart = (unEscXml.slice(dtStartIdx+8,dtStartIdx+23));
    var dtEnd = (unEscXml.slice(dtEndIdx+6,dtEndIdx+21));
    var summ = (unEscXml.slice(summIdx+8,UIDIdx-1));
    summ = summ.replace("\\n", "%0a");
    summ = summ.replace("\n", "%0a");
    var desc = (unEscXml.slice(descIdx+12,dtEndIdx));
    desc = summ + "%0A___________________________________%0Awww.poseeq.com - Itinerary to Calendar.";
    var ycalURI = "http://calendar.yahoo.com/?v=60&view=d&type=10&title=" + summ + "&dur=0100&st=" + dtStart + "&desc=" + desc;
    window.open(ycalURI, 'ycal');
}

