// init function
function getXHRObject()
{
    var XHRObject;
    try { XHRObject = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch( e )
    {
        try     { XHRObject = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch( oc ) { XHRObject = null; }
    }

    if( !XHRObject && typeof XMLHttpRequest != "undefined" ) { 
        XHRObject = new XMLHttpRequest(); 
    }

    return XHRObject;
}   


function session_signup_handler(eventid, year, orderid, sessionid, sessionname, sessionstarttime, hardsessiontype)
{
    var absolute_src = document.getElementById(sessionid).src;
    
    var url = absolute_src.split("/");
    var img = url[url.length-1];
    
    if( img == 'register.gif' || img == 'add_to_my_agenda.gif' ) {
    
        session_signup( eventid, year, orderid, sessionid, sessionname, sessionstarttime, hardsessiontype );
        
    }
    else if( img == 'registered.gif' || img == 'added_to_my_agenda.gif' ) {
    
        session_remove( eventid, year, orderid, sessionid, hardsessiontype);
    }


}


function session_signup(eventid, year, orderid, sessionid, sessionname, sessionstarttime, hardsessiontype)
{

    var XHRObject;

    var url = '/' + year + '/myevent?rm=session_signup';

    url = url + "&eventid=" + escape( eventid );
    url = url + "&rsargs=" + escape( orderid );
    url = url + "&rsargs=" + escape( sessionid );
    url = url + "&rsargs=" + escape( sessionname );
    url = url + "&rsargs=" + escape( sessionstarttime );
    url = url + "&rsargs=" + escape( hardsessiontype );
    
   // alert(url);
    
    // load object
    XHRObject = getXHRObject();
    XHRObject.open("GET", url, true);

    XHRObject.onreadystatechange = function()
    {

        if( XHRObject.readyState == 4 ) { 

            if( XHRObject.status == 200 ) {

                var result = XHRObject.responseText;

                if( result == 'atmax' ) {

                    if( sessionname.match(/30 Min/i) ) {
                        alert( 'You have reached your maximum allotment of 30 Mins With.. sessions' );
                    }
                    else if( sessionname.match(/Master Class/) || sessionname.match(/Workshop/) ) {
                        alert( 'You have reached your maximum allotment of Master Class sessions' );
                    }
                    else if( sessionname.match(/Speed Pitching/) || hardsessiontype == 'Speed Pitching' ) {
                        alert( 'You have reached your maximum allotment of Speed Pitches for this event' );
                    }
                    else if( sessionname.match(/Speed Dating/) ) {
                        alert( 'You have reached your maximum allotment of Speed Dates for this event' );
                    }
                    else if( sessionname.match(/Roundtable/) || hardsessiontype == 'Roundtable' ) {
                        alert( 'You have reached your maximum allotment of Roundtable sessions for this event' );
                    }
                    else if( sessionname.match(/Legal Clinic/) ) {
                        alert( 'You have reached your maximum allotment of Legal Clinic sessions for this event' );
                    }
                    else if( sessionname.match(/Footage Dating/) ) {
                        alert( 'You have reached your maximum allotment of Footage Dating sessions for this event' );
                    }

                }
                // this case is a speed pitching conflict with another soft/hard session
                else if( result == 'timeslot_nsps' ) {
		    ShowModalDialogBox('speed_pitch_session_conflict_box', 350, 75);
                }	
                else if( result == 'timeslot' ) {

		    if( hardsessiontype == 'Speed Pitching' ) {
            	alert( 'You have already signed up for a speed pitch in this time slot' );
		    }
		    else if( hardsessiontype == 'Speed Dating' ) {
            	alert( 'You have already signed up for a speed date in this time slot' );
		    }
		    else {
                    	alert( 'You have already signed up for a session in this time slot' );
                    }
                }
                else if( result == 'full' ) {

                    alert( 'Unfortunately, this session is now full' );
                }
                else if( result == 'already' && hardsessiontype.match(/Speed Pitching/) ) {

                    alert( 'You have already signed up to make a pitch to this speaker' );
                }
                else if( result == 'already' && hardsessiontype.match(/Speed Dating/) ) {

                    alert( 'You have already signed up to date this sponsor' );
                }
                else if( result == 'already' && hardsessiontype.match(/Legal Clinic/) ) {

                    alert( 'You have already signed up for a session with this individual' );
                }
                else if( result == 'already' && hardsessiontype.match(/boardsu class/i) ) {

                    alert( 'You have already signed up for this session in another time slot' );
                }
                else if( result == 'already' && hardsessiontype.match(/Roundtable/i) ) {

                    alert( 'You have already signed up for a roundtable spot with this speaker' );
                }
                else if( result == 'already_in_time_slot' && hardsessiontype.match(/Roundtable/i) ) {

                    alert( 'You have already signed up for a roundtable spot in this time slot' );
                }
                else if( result == 0 ) {
                    alert( 'An error occurred, your request was not completed successfully.' );
                }
                else if( result == 1 ) {
                    ShowModalDialogBox('session_signup_box', 350, 75);
                    
                    if (hardsessiontype == 'soft') {
                        document.getElementById(sessionid).src = "/gimages/added_to_my_agenda.gif";
                    }else{
                        document.getElementById(sessionid).src = "/gimages/registered.gif";
                    	document.getElementById(sessionid).title = "You are currently registered for this session.  To un-register click the green checkmark.";
                    }
                }
            }
            else {
                alert('An error occurred, your request was not completed successfully.');
                return;
            }
        }
        else {          
            return;
        }
    }

    XHRObject.send( null );

}

function session_remove(eventid, year, orderid, sessionid, hardsessiontype)
{
    
    var answer = 1;
    if( hardsessiontype && hardsessiontype != 'soft' ) {
    	text = "Are you sure you wish to un-register from this session?  Session space fills up fast, you may not be able to re-register at a later date.";
    	answer = confirm(text);
    }
    
    if( answer ) {

        var url = '/' + year + '/myevent?rm=session_remove';
    	url = url + "&eventid=" + escape( eventid );

        var args = session_remove.arguments
        for( var i = 2; i < args.length; i++ ) { url = url + "&rsargs=" + escape( args[i] ); }

        // load object
        var XHRObject = getXHRObject();
        XHRObject.open("GET", url, true);

        XHRObject.onreadystatechange = function()
        {

            if( XHRObject.readyState != 4 ) { return; }

            var result = XHRObject.responseText;
            if( result == 0 ) {
                alert( 'An error occurred, your request was not completed successfully.' );

            }
            else {
            	//alert('hot dog');
		ShowModalDialogBox('session_removal_box', 350, 100);

                if( hardsessiontype == 'soft' ) {
                    document.getElementById(sessionid).src = "/gimages/add_to_my_agenda.gif";
                }
                else {
                    document.getElementById(sessionid).src = "/gimages/register.gif";
                    document.getElementById(sessionid).title = "Click to register for this session";
                }
            }

        }

        XHRObject.send( null );
    }

}