// Web Splash screen manager 
// (c) 2001-2006 Netcraft, http://www.netcraft.co.il
// Altering this notice or redistributing this file is prohibited.
function httpRequest(url, callback) {
    var httpObj = false;
    if (typeof XMLHttpRequest != 'undefined') {
        httpObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try{
            httpObj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            try{
                httpObj = new ActiveXObject('iMicrosoft.XMLHTTP');
            } catch(e) {}
        }
    }
    if (!httpObj) return;    
    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4 && httpObj.status == 200) { // when request is complete
            callback(httpObj.responseText);
        }
    };
    try {
    		
		httpObj.open('GET', url, true);
		httpObj.send(null);
	} catch(e){
	}
}
function cookiesSupported()
{
	var cookieEnabled = (navigator.cookieEnabled)? true : false

	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	}
	
	return cookieEnabled;
}
function createCookie(name,value,minutes) {
	if (minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// web splash screen class
function webSplashScreen()
{
    var self = this;
    this.CookieKey          = "";
    this.CookieDuration     = 0; // in minutes
     
    this.init = function(sAdPageURL, sCookieKey, iCookieDuration)
    {
        self.CookieKey      = sCookieKey;
        self.CookieDuration = iCookieDuration
        //alert(sAdPageURL);

        // make sure we should actually show the splash
        if (cookiesSupported() && self.shouldBeShown())
        {
            // then we stop the document redering
            if (document.all)
            {
                document.execCommand("Stop"); //IE only
            } else { 
                window.stop(); //NS + Mozilla
            };

            // close current document            
            document.write("<body></body></html>");
            document.close();
            
            // fixes the use of layla.co.il without www - cross domain policy
			//if (window.location.toString().indexOf("www.") < 0)
			//{
				//sAdPageURL = sAdPageURL.replace("www.", "");
			//}
			try
			{			
				createCookie(self.CookieKey, "1", self.CookieDuration);
				var sURL = unescape(sAdPageURL);
				window.location.replace(sURL+"&target="+encodeURIComponent(window.location));
				//httpRequest(sAdPageURL, self.show);
			}
			catch (e)
			{
				createCookie(self.CookieKey, "1", self.CookieDuration);
				var sURL = unescape(sAdPageURL);
				window.location.replace(sURL+"&target="+encodeURIComponent(window.location));
			}
            
            //httpRequest(sAdPageURL, self.show);
        }
    }
    this.shouldBeShown = function()
    {
        // detemine if to show splash screen during page load

        //Google? - do not show
        if (navigator.userAgent.indexOf('Google') != -1) { return false; };

        //Cookie exists? - do not show
        if (readCookie(self.CookieKey) == "1")
        {
            //alert("cookie exists - splash will not show for " + self.CookieDuration + " minutes");
            return false;
        }
        //alert("cookie does not exist!");
        return true;
    }
    this.show = function(sResponseText)
    {
        // update the cookie
        createCookie(self.CookieKey, "1", self.CookieDuration);        
        // open a new document and write it's entire HTML content
        document.write(sResponseText);
        document.close();
    }
}