/*
*   grab.js
*   Misc. functions for GRAB integration
*/

var strGrabUrl = null;      // variable set by server-side code
var strSignupText = null;   // variable set by server-side code
var strSigninText = null;   // variable set by server-side code
var strSignoutText = null;  // variable set by server-side code
var strGrabUsername = null; // 
var logregdiv;

// handleResponse - event handler for the XMLHttpRequest object
// inserts GRAB username into the header
function handleResponse()
{
    var userName;
    if(xmlhttp.readyState == 4) {
        if(xmlhttp.status == 200) {
            userName = xmlhttp.responseText;
            if(userName.length > 0 && userName.toLowerCase() != "not logged in") {
                strGrabUsername = userName;
                // user is logged in, display username
                if(logregdiv)
                    logregdiv.innerHTML = "Welcome, " + strGrabUsername + "!&#160;&#160;" + strSignoutText;
            }
        if(strGrabUsername == null && logregdiv)
            logregdiv.innerHTML = strSigninText + "&#160;&#8226;&#160;" + strSignupText;
        }
    }
    return true;
}

// init_GIAB - event handler for window.onload
function init_GIAB()
{
    logregdiv = document.getElementById("logreg");
    var sUrl

    // Initialize the xml parser object
    xmlhttp=getXmlParser();
    // send the http request only if all required objects are present and initialized
    if(strGrabUrl != null && strGrabUrl.length > 0 && typeof xmlhttp != "undefined" && xmlhttp != null) {
        // attach a timestamp to the url to make it unique
        sUrl = strGrabUrl+"?"+(new Date()).getTime();
        fetch(sUrl,handleResponse);
    } else {
        if(logregdiv) {
            logregdiv.innerHTML = strSigninText + "&#160;&#8226;&#160;" + strSignupText;
        }
    }
}

// popup photo upload window
function popupPhotoUpload(pBaseUrl,pstrTags)
{
    var width = 420;
    var height = 420;
    var strUrl = pBaseUrl + "/profiles/signin_popup.aspx?return_url=" + escape("/photos/AddPhotosFrame.aspx?tags=") + pstrTags;
    winopen(strUrl, null, width, height);
}

// open a popup window
function winopen(url, winname, width, height) {
	var hWin = window.open(url, winname, "width="+width+",height="+height+",menubar=no,status=no,location=no,toolbar=no,scrollbars=no");
	hWin.focus();
}

// append init_GIAB to the window.onload event handlers
AddEventHandler(window,"load","init_GIAB");


