﻿
/*
    Function which makes a header ajax call - used to check whether a file exists
    
    Creating an object:
        var VAR_NAME = new HeaderLoader(FILE_PATH,FUNCTION_TO_CALL);
            
            FILE_PATH - path to file
            FUNCTION_TO_CALL - reference to function to call if file exists
            
            P.S: The FUNCTION_TO_CALL is optional
    
    Vars used to test the call
    
        VAR_NAME.exist - true if file exist
        VAR_NAME.error - true if there was an error

*/

    
function HeaderLoader(imgPath,funCall)
{
    if(typeof HeaderLoader._initialized == 'undefined')
    {
        HeaderLoader.prototype.load = function()//Make Ajax call to retrieve the header
        {
            this.oXHR = this.createXHR();//reference to ajax object
            
            if(this.oXHR)
            {
                this.oXHR.open('HEAD',this.path,false);
                this.oXHR.send(null); 
                if(this.oXHR.status == 304 || this.oXHR.status == 200)
                {
                    this.exist = true;
                    if(this.funCall)this.funCall();
                }
            } else {
                        this.error = true;
                   }
        };
        HeaderLoader.prototype.createXHR = function()//Create Ajax request object
        {
            if (typeof XMLHttpRequest != "undefined") 
            {
                return  new XMLHttpRequest();
            } else if (window.ActiveXObject) 
                   {
                var aVersions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
                for (var i = 0; i < aVersions.length; i++) 
                {
                    try {
                        return new ActiveXObject(aVersions[i]);
                    } catch (oError) {
                        //Do nothing
                    }
                }
            }
            return;       
        };
    }
    HeaderLoader._initialized = true;
    
    if(funCall)this.funCall = funCall;
    this.path = imgPath;//path to image file 
    this.load();   
}