/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    le.net.js - core network connectivity (AJAX, etc.)
     
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

/*==========================================================================

    Builds the first part of a URL for Ajax calls.  Prevents security errors
    in strict browsers.
    
===========================================================================*/
function getHostname() {
    var hrefPort = "";
    
    if (window.location.port != null && window.location.port != "" && window.location.port != undefined) {
        hrefPort = ":" + window.location.port;
    }
    
    hostname = window.location.protocol + "//" + window.location.hostname + hrefPort;
    //hostname = "http://wwwqa.landsend.com";
    
    ppPath = hostname + window.location.pathname;
        
    doDebug("[getHostname] Hostname: " + hostname);
    doDebug("[getHostname] Product Page Path: " + ppPath);
}

/*==========================================================================

    An asynchronous content loader.  It can handle just about any text-based
    content, but it's best used for XML and HTML.

    url - Required - The URL of the resource to load
    
    onload - Required - Callback function to be called upon successful
    loading of the resource.
    
    onerror - Optional - Callback function to be called if there is an
    error loading the resource.
    
    method - Optional - GET or POST
    
    params - Optional - Any HTTP parameters to send in the request
    
    contentType - Optional - ContentType of the resource
==========================================================================*/
function contentLoader(url, onload, onerror, method, params, contentType) {
    var thisCL = this;

    var READY_STATE_UNINITIALIZED   = 0;
    var READY_STATE_LOADING         = 1;
    var READY_STATE_LOADED          = 2;
    var READY_STATE_INTERACTIVE     = 3;
    var READY_STATE_COMPLETE        = 4;
    
    var request                     = null;
    
    this.url						= url;
    this.request                    = request;
    this.onload                     = onload;
    this.onerror                    = onerror;
    
    this.loadXMLDoc = function(url, method, params, contentType) {

    	if (method === undefined) {
    	    method = "GET";
    	}

        if (contentType === undefined) {
            contentType = "application/x-www-form-urlencoded";
        }
        
        request = new XMLHttpRequest();
        try {
            var loader = thisCL;
                            
            request.onreadystatechange = function() {
                thisCL.onReadyState.call(loader);
            };
			doDebug("[contentLoader.open(\""+method+"\", \""+ url + "\", true)");
            request.open(method, url, true);
            
            if (contentType !== null) {
                request.setRequestHeader("Content-Type", contentType);  
                doDebug("setting contenttype: " + contentType);                  
            }
            doDebug("send: " + params);
            request.send(params);
        } catch (err) {
        	doDebug("[contentLoader.loadXMLDoc]error sending data!\n");
        	window.onerror(err);
            onerror(request);
        }
        
    };
    
    this.onReadyState = function() {
        doDebug("[contentLoader.onReadyState] readyState changed");
        try {
            doDebug("[contentLoader.onReadyState]request.readyState: " + request.readyState);
	        if (request.readyState == READY_STATE_COMPLETE) {
	        	doDebug("[contentLoader.onReadyState]request.status: "+request.status);
	            if (request.status == 200) {   		
	                onload(request);
	            } else {
	            	doDebug("[contentLoader.onReadyState]error fetching data!\n\nreadyState:" + request.readyState + "\nstatus: " + request.status + "\nheaders: " + request.getAllResponseHeaders());
	            	throw new Error("bad http response status: " + request.status+" "+url); 
	            }
	        }
	    } catch(err) {
	    	doDebug("[contentLoader.onReadyState catch] There was an AJAX error...check  " + err);
	    	window.onerror(err);
	    	onerror(request);
	    }
    };
    
    this.defaultError = function() {
        doDebug("[contentLoader.defaultError]error fetching data!\n\nreadyState:" + request.readyState + "\nstatus: " + request.status + "\nheaders: " + request.getAllResponseHeaders());
    };
    
    this.loadXMLDoc(url, method, params, contentType);
}

function URLExists(url) {
 	var request = new XMLHttpRequest();

 		request.open("HEAD", url, false );
    	request.send(null);
		if (request.status == 200) {
			return true;
		}
	return false;
}
