var _ir_js_loaded = 1;
var IRS_isConnected = 0;
var IRS_xmlobj = null;
var IRS_gotConnection = 0;
var IRS_channel = "all";
var IRS_pollInterval = 10000;
var IRS_mode;
var IRS_connCheckId;
var IRS_href = escape(location.href);
var IRS_port = location.port;
var IRS_keepAliveIterations = 1;
var IRS_keepAliveMsecs = 1500000; // 25 minutes
var showStaticBidButton = false;

function IRS_debug(msg) {
	var now = new Date();
	msg = now.toLocaleTimeString() + " " + msg;

	var c = unescape(getCookie("db"));
	if (c && c != "null")
		c = msg + "\n" + c;
	else
		c = msg;
	document.cookie = "db=" + escape(c) + ";expires=Thu, 01-Jan-10 00:00:01 GMT;path=/";
}

function ir_init(channel) {
	IRS_mode = getCookie("irx");
	if (IRS_mode == null || isNaN(IRS_mode)) IRS_mode = 1;
	if (channel)
		IRS_channel = channel;
	IRS_makeIframe();
	try {
		iframe_init_page();
	} catch (e) { }
}

function IRS_saveMode() {
	var expiry = new Date();
	expiry.setTime(expiry.getTime()+2592000000); // 30 days
	document.cookie = "irx=" + IRS_mode + "; path=/; expires=" + expiry.toGMTString();

	if (IRS_mode == 0) {
		if (parent.DisableIR) {
			parent.DisableIR();
		}
		else {
			alert(JS_irserver_Yourbro_8715);
			parent.location.reload();
		}
	}
}

function IRS_setEnabled() {
	var hadConnection = IRS_gotConnection;
	IRS_isConnected = 1;
	IRS_gotConnection = 1;
	IRS_saveMode();
	if (! hadConnection) {
		parent.KNS_ok();
	}
	window.clearTimeout(IRS_connCheckId);
}

function IRS_doIRMessage(code) {
	try {
		eval(code);
	}
	catch (e) {  }
}

// Something has happened on the XML object (new data, loss of connection)
var IRS_lastLength = 0;
function IRS_newData() {
	if (! self.IRS_xmlobj) {
		// Happens on page reload
		return;
	}
    try
	{
	if (IRS_xmlobj.readyState == 3 || IRS_xmlobj.readyState == 4) {
		var remain = IRS_xmlobj.responseText.substring(IRS_lastLength);
		var endPos;
		while ((endPos = remain.indexOf("</script>")) > -1)
		{
			IRS_lastLength += endPos + 9;
			// Skip past the opening <script>
			var payload = remain.substring(remain.indexOf('t">') + 3,
							endPos);
			remain = remain.substring(endPos + 9);
			try {
				eval(payload);
			} catch (e) { }
		}
	}
	if (IRS_xmlobj.readyState == 4) {
		IRS_setDisconnected();
	}
	}
	catch(e)
	{
	    // self.IRS_xmlobj is not null but IRS_xmlobj is null on firefox, ignore.
	}
}

function IRS_makeIframe() {
	var url = "http://" + document.domain;

	if (IRS_mode == 2)
		url += ":7633";
	else if (IRS_port && IRS_port != 80)
		url += ":" + IRS_port;
	url += "/ir/" + IRS_channel +
		"?t=" + (new Date().getTime() % 50301);

	if (IRS_mode == 3)
		url += "&d=1";

	try {
		url += "&s=" + (sTime / 1000 - 30);
	} catch (e) { }

	url += "&r=" + IRS_href;

	if (IRS_xmlobj != null) {
		try {
			IRS_xmlobj.open("GET", url, true);
			IRS_xmlobj.onreadystatechange = IRS_newData;
			IRS_xmlobj.send(null);
			IRS_lastLength = 0;
			IRS_connCheckId = window.setTimeout('IRS_checkConnection()', IRS_mode == 3 ? 60000 : 8000);
		}
		catch(e) {
			try {
				IRS_xmlobj.abort();
			}
			catch (e2) { }
			ir_retry();
		}
	}
	else {
		document.getElementById("irs_iframe_span").innerHTML = '<iframe id="irs_iframe" border="0" width="0" height="0" frameborder="0" src="' + url + '"></iframe>';
		IRS_connCheckId = window.setTimeout('IRS_checkConnection()', IRS_mode == 3 ? 60000 : 8000);
	}
}

// Checks to see if we've successfully connected yet.
function IRS_checkConnection() {
	if (IRS_gotConnection)
		IRS_gotConnection = 0;
	else
		ir_retry();
}

function IRS_doReconnect() {
	IRS_isConnected = 0;
	if (IRS_xmlobj != null) {
		var oldObj = IRS_xmlobj;
		IRS_xmlobj = null;
		if (oldObj.readyState != 4) {
			oldObj.abort();
		}
		IRS_xmlobj = new XMLHttpRequest();
	}

	window.setTimeout('IRS_makeIframe()', 50);
}

function IRS_setDisconnected() {
	var wasConnected = IRS_isConnected;
	IRS_isConnected = 0;

	if (wasConnected) {
		if (IRS_mode == 3) {
			window.setTimeout('IRS_doReconnect()', IRS_pollInterval);
		}
		else {
			try {
				KNS_onTunnelStop("");
			} catch (e) { }
			window.setTimeout('IRS_doReconnect()', 1);
		}
	}
}

function ir_retry() {
	if (IRS_mode == 3)
		IRS_mode = 1;
	else
		IRS_mode++;
	IRS_saveMode();
	IRS_isConnected = 0;
	IRS_doReconnect();
	try {
		window.clearTimeout(IRS_connCheckId);
	} catch (e) { }
}

function ir_setChannel(chan) {
	IRS_channel = chan;
}


function isIE7() {
// Returns true if browser is IE and version is 7.0 or greater

  var ie_ver = -1; 
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      ie_ver = parseFloat( RegExp.$1 );
  }
  return (ie_ver >= 7.0) ;
}


function keepMeLoggedIn() { 
    var maxKeepAliveIters = 28; // 12 hours / 25 mins

    var o = document.getElementById("_keepalive"); 
    if (!o) return; 
    o.src = location.protocol + '//' + location.host + 
               '/jsp/acct/keepalive.jsp?page=' + (pageType==undefined ? "" : pageType) + '&time=' + IRS_keepAliveMsecs / 60000 * IRS_keepAliveIterations++;

    if (IRS_keepAliveIterations < maxKeepAliveIters) {
        setTimeout('keepMeLoggedIn()', IRS_keepAliveMsecs);
    }
}

function keepalive_html() {
    if (!document.getElementById) return; 

    var imgsrc = location.protocol + '//' + location.host + '/images/space.gif' ;
    document.write('<img id="_keepalive" src="' + imgsrc + '" width=1 height=1>');

    setTimeout('keepMeLoggedIn()', IRS_keepAliveMsecs);

}

function ir_html() {

    /* IE7 (as of version 7.0.5700.6, the Oct 2006 Release Candidate, at least) does not 
       fully conform to the W3C spec for XHR. Specifically, it does not allow access to 
       response contents as they are being streamed back (readyState 3). 
       We depend on being able to do that.  Therefore, we are going to fallback and 
       use the same IR technique in IE7 as we do in earlier versions of IE.
     */

    if (typeof XMLHttpRequest != "undefined" && !isIE7() ) {
        // Use XMLHttpRequest object when it's available (unless this is IE7, see above)
		IRS_xmlobj = new XMLHttpRequest();
		self.doIRMessage = IRS_doIRMessage;
	}
	else {
        // If not XMLHttpRequest, open our connection with a hidden iFrame
		document.write('<iframe border="0" frameborder="0" id="irs_stopper" width="0" height="0"></iframe>');
		document.write('<span id="irs_iframe_span"></span>');
	}
	document.write('<span id="backfill"></span>');

    // Possibly keep session alive if auction is open or about to open (within the next 4 hours)
    if ( ((typeof loggedIn != "undefined" && loggedIn) || (typeof pageType != "undefined" && pageType == "Bid" ))
        && typeof aucOpenTime != "undefined" && sTime > aucOpenTime - 14400000) {  
        // But only do this for bidders
        var isBidder = getCookie("roles");
        if (isBidder != null && isBidder.indexOf('b') > -1 && isBidder.indexOf('a') == -1) {
            keepalive_html();
        }
    }

	showStaticBidButton = isSafari();

	return 1;
}

if (! document.getElementById) {
	IRS_mode = 0;
	IRS_saveMode();
}

function isSafari() {
	var re  = new RegExp("Safari");
	return (re.exec(navigator.userAgent) != null);
}

