// --------------------------------------
// Javascript code for real-time updates.
// --------------------------------------

// HTTP request generated from the client.
var xmlhttp=false;

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}


// --------------------------------------------------
// Load HTML code into a identifiable element.
// Display a progress bar until the load is complete.
// --------------------------------------------------
function loadFragmentInToElement(fragment_url, element_id) {
    var element = document.getElementById(element_id);
    progressBar();
    xmlhttp.open("GET", fragment_url);
    xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	    element.innerHTML = xmlhttp.responseText;
	    parent.frames.iFrameHeight();
	}
    }
    xmlhttp.send(null);
}


// ------------------------------------
// Get availability data for a product.
// ------------------------------------
function getAvail(product_id) {
    var host  = window.location.host;
    var path  = window.location.pathname;
    var npath = path.replace(/displayAvailInfo.php/, "");

    loadFragmentInToElement('http://' + host + npath +'checkavail_cust.php?product_id=' + product_id, 'displayAvailInfo');
}


// --------------------------------
// Create and run the progress bar. 
// --------------------------------
var i;
function progressBar()
{
    var element;
    if (document.getElementById)
	{
	    element = document.getElementById("pbar");
	}
    else if (document.all)
	{
	    element = document.all["pbar"];
	}
    
    if (element && element.style)
	{
	    element.style.width=0;
	}    
    
    i=0;
    timedProgressBar(); 
}


// ---------------------------
// Increment the progress bar.
// ---------------------------
function timedProgressBar()
{
    if (i<=(300))
	{
	    var element;
	    if (document.getElementById)
		{
		    element = document.getElementById("pbar");
		}
	    else if (document.all)
		{
		    element = document.all["pbar"];
		}

	    if (element && element.style)
		{
		    element.style.width=i+"px";
		}

	    // simulates some lengthy processing
	    var j=0;
	    while (j<=100)
		j++;  
	    setTimeout("timedProgressBar();", 20);
	    i++;  
	}
}

// --------------------------------------------------
// Resize the iframe to accomodate the data received.
// --------------------------------------------------
function iFrameHeight() {
	if(document.getElementById && !(document.all)) {
		h = document.getElementById('iframename').contentDocument.body.scrollHeight;
		document.getElementById('iframename').style.height = h;
	}
	else if(document.all) {
		h = document.frames('iframename').document.body.scrollHeight;
		document.all.iframename.style.height = h;
	}
}
