﻿var Pliner = {
    Util: {
        /// Returns an XMLHttpRequest object in a browser-compatible way.
        GetHTTPObject: function() {
	        var xmlhttp;

	        /*@cc_on
	        @if (@_jscript_version >= 5) {
		        try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		        catch (e) {
			        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			        catch (E) { xmlhttp = false; }
		        }
	        }
	        @else
		        xmlhttp = false;
	        @end @*/

	        if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
		        try { xmlhttp = new XMLHttpRequest(); }
		        catch (e) { xmlhttp = false; }
	        }

	        return xmlhttp;
        }, 
        
        /// Gets AJAX content from the given URL asynchronously.  When the request is completed, calls the 
        /// given function and gives the request text or an error flag as the single parameter.  
        /// Takes an optional timeout. Returns the XMLHttpRequest object in use.
        ///
        /// Possible values returned to the request-completed handler function: 
        ///    * NOT_SUPPORTED: the user's browser will not allow an AJAX connection
        ///    * CANNOT_CONNECT: the request could not be opened
        ///    * TIMEOUT: the request timed out
        ///    * ERROR: the server returned a page with a status code other than 200, or an unhandled error occurred
        ///    * The actual response text from the URL.
        GetAjaxContent: function(whereItsAt, thingToDo, timeoutMs) {
	        var httpRequest;
	        
	        if (timeoutMs == null || timeoutMs == undefined)
	            timeoutMs = 10000;

	        try { httpRequest = Pliner.Util.GetHTTPObject(); }
	        catch (e) { return httpRequest; }

	        if (httpRequest == null) return httpRequest;

	        // -- Make sure the browser is supporting the XMLhttpRequest object
	        if (!httpRequest) {
		        thingToDo("NOT_SUPPORTED");
		        return httpRequest;
	        }

	        // -- Avoid domain permissions errors when a www.* domain and one without www.* refer to the same page, by standardizing to match the window url
	        if (window.location.href.indexOf("www.") != -1 && whereItsAt.indexOf("http://") != -1 && whereItsAt.indexOf("www.") == -1)    // url has www, and page address isn't relative so page address should too
		        whereItsAt = whereItsAt.replace("http://", "http://www.");
	        else if (window.location.href.indexOf("www.") == -1 && whereItsAt.indexOf("http://") != -1 && whereItsAt.indexOf("www.") != -1)    // url has no www, and page address isn't relative so page address shouldn't too
		        whereItsAt = whereItsAt.replace("http://www.", "http://");

	        whereItsAt = whereItsAt + (whereItsAt.indexOf("?") >= 0 ? "&" : "?") + "random=" + Math.floor(Math.random() * 100000000);

	        try {
	            var abortTimer = setTimeout(function() { Pliner.Util.AbortConnection(httpRequest, thingToDo); }, timeoutMs);
	            
		        httpRequest.open("GET", whereItsAt, true);
		        httpRequest.onreadystatechange = function() { Pliner.Util.__UpdateLoadStatus(httpRequest, thingToDo, abortTimer); };
		        httpRequest.send(null);
	        }
	        catch (e) {
	            thingToDo("CANNOT_CONNECT");
	        }
	        
	        return httpRequest;
        }, 
        
        /// Private: Follows the loading of the XMLHttpRequest object.
        __UpdateLoadStatus: function(httpRequest, thingToDo, abortTimer) { 
	        switch (httpRequest.readyState) {
		        case 1: break;
		        case 2: break;
		        case 3: break;
		        case 4:
		            clearTimeout(abortTimer);
			        if (httpRequest.status == 200) thingToDo(httpRequest.responseText);
			        else thingToDo("ERROR");
			        httpRequest = null;
			        thingToDo = null;
			        break;
		        default: break;
	        }
        }, 
        
        /// Aborts the given XMLHttpRequest object.
        AbortConnection: function(httpRequest, thingToDo) {
            httpRequest.abort();
            thingToDo("TIMEOUT");
        }
    }
};