var _elementId;

/**
 * 
 */
function processAjaxResponse() {
 	 // only if req shows "loaded"
  	if (req.readyState == 4) {
    	// only if "OK"
    	if (req.status == 200) {	
    		if (document.getElementById(_elementId)) {
    			//alert(req.responseText)
      			document.getElementById(_elementId).innerHTML = req.responseText;			
    		}    			
    	} else {
      		alert("There was a problem retrieving the page:\n" +
      		req.statusText);
    	}
  	}
} 
	
/**
 * 
 */
function updatePage(url) {
  	var callback = processAjaxResponse;
  	refreshPage( callback, url );
}

function refreshPage(callback, url) {
	req = new initXMLHttpClient();
	req.onreadystatechange = callback;
	req.open("GET", url, true);
	req.send(null);
}

function initXMLHttpClient() {
	var xmlhttp;
    try {
    	// Mozilla / Safari / IE7
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        // IE
        var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
									'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP' );
        var success = false;

        for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
            try {
            	xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
            	
            	success = true;
            } catch (e) {}
        }

        if (!success) {
        	throw new Error('Unable to create XMLHttpRequest.');
        }
    }

    return xmlhttp;
}

function useHttpResponse() {
  	if (http.readyState == 4) {
    	var textout = http.responseText;
    	document.write.textout;
  	}
}

/**
 * this function reloads the page with the selected category
 */
function getLines(page)
{
	var resultInfo = document.getElementById('result_info');
	if ( resultInfo != null ) {
		resultInfo.innerHTML = "<img src='assets/ajax-loader.gif' />";
	}
	if ( getCategoryId() == '' ) {
		var listType = "category";
		var categoryId = null;
	}
	else {
		listType = "product";
		categoryId = getCategoryId();
	}
	
	if ( page == 'product' ) {
		_elementId = 'body_area_w_sidebar';
		url = 'update_product.php?menuChanged=true&listType='+listType+'&categoryId='+categoryId;
	}
	else {
		if ( getCategoryId() == '' && getDocTypeId() == '' ) {
			var docType = null;
			var categoryId = null;
		}
		else {
			docType = getDocTypeId();
			categoryId = getCategoryId();
		}
		
		_elementId = 'body_content';
		url = 'update_product_library.php?menuChanged=true&docTypeId='+docType+'&categoryId='+categoryId;
	}
	updatePage(url);
}

/**
 * this function reloads the page with teh selected category and line
 */
function getFamilies(page)
{
	var resultInfo = document.getElementById('result_info');
	if ( resultInfo != null ) {
		resultInfo.innerHTML = "<img src='assets/ajax-loader.gif' />";
	}
	if ( page == 'product' ) {
		_elementId = 'body_area_w_sidebar';
		url = 'update_product.php?menuChanged=true&listType=product&categoryId='+getCategoryId()+'&lineId='+getLineId();
	}
	else {
		_elementId = 'body_content';
		url = 'update_product_library.php?menuChanged=true&docTypeId='+getDocTypeId()+'&categoryId='+getCategoryId()+'&lineId='+getLineId()+'&familyId='+getFamilyId();
	}
	updatePage(url);
}

/**
 * this function reloads the page with the selected category, line, and family
 */
function getProducts(page)
{
	var resultInfo = document.getElementById('result_info');
	if ( resultInfo != null ) {
		resultInfo.innerHTML = "<img src='assets/ajax-loader.gif' style='padding: 0;' />";
	}
	if ( page == 'product' ) {
		_elementId = 'body_area_w_sidebar';
		url = 'update_product.php?menuChanged=true&listType=product&categoryId='+getCategoryId()+'&lineId='+getLineId()+'&familyId='+getFamilyId();
	}
	else {	
		_elementId = 'body_content';
		url = 'update_product_library.php?menuChanged=true&docTypeId='+getDocTypeId()+'&categoryId='+getCategoryId()+'&lineId='+getLineId()+'&familyId='+getFamilyId()+'&productId='+getProductId();
	}
	updatePage(url);
}

function getProduct(page) 
{
	if ( page == 'product_details' ) {
		window.location = 'product_details.php?menuChanged=true&listType=productDetail&categoryId='+getCategoryId()+'&lineId='+getLineId()+'&familyId='+getFamilyId()+'&productId='+getProductId();
	}
	else {
		var resultInfo = document.getElementById('result_info');
		if ( resultInfo != null ) {
			resultInfo.innerHTML = "<img src='assets/ajax-loader.gif' style='padding: 0;' />";
		}
		_elementId = 'body_content';
		url = 'update_product_library.php?menuChanged=true&docTypeId='+getDocTypeId()+'&categoryId='+getCategoryId()+'&lineId='+getLineId()+'&familyId='+getFamilyId()+'&productId='+getProductId();
		updatePage(url);
	}
}

function getProductList(url) {
	_elementId = 'product_listing';
	updatePage(url);	
}

function getProductDetailList(url) {
	_elementId = 'product_detail_listing';
	updatePage(url);
}

function getDocumentTypes(elementId) {
	if (getDocTypeId() == '') {
		var docType = null;
	}
	else {
		docType = getDocTypeId();
	}
	var resultInfo = document.getElementById('result_info');
	if (resultInfo != null) {
		resultInfo.innerHTML = "<img src='assets/ajax-loader.gif' style='padding: 0;' />";
	}
	_elementId = 'body_content';
	updatePage('update_product_library.php?menuChanged=true&docTypeId=' + docType);
}

function getDocuments(url) {
	_elementId = 'document_list';
	updatePage(url);	
}

function getLocations(url) {
	_elementId = 'location_list';
	updatePage(url);	
}

function getNewsEvents(url) {
	_elementId = 'newsevents_list';
	updatePage(url);	
}
/**
 * returns the categoryId from the category drop down
 */
function getCategoryId() {
	var elem = document.getElementById('categories');
	var index = elem.options.selectedIndex;
	// Setting the variable with the value of selected category
	return elem.options[index].value;	
}

/**
 * returns the lineId from the line drop down
 */
function getLineId() {
	var elem = document.getElementById('lines');
	index = elem.options.selectedIndex;
	// Setting the variable with the value of selected line
	return elem.options[index].value;
}

/** 
 * returns the familyId from the family drop down
 */
function getFamilyId() {
	var elem = document.getElementById('families');
	index = elem.options.selectedIndex;
	// Setting the variable with the value of selected line
	return elem.options[index].value;
}

function getProductId() {
	var elem = document.getElementById('products');
	index = elem.options.selectedIndex;
	// Setting the variable with the value of selected line
	return elem.options[index].value;
}

/** 
 * returns the familyId from the family drop down
 */
function getDocTypeId() {
	var elem = document.getElementById('documentTypes');
	index = elem.options.selectedIndex;
	// Setting the variable with the value of selected line
	return elem.options[index].value;

}

function checkKeyword() {
	var keyword = document.getElementById('keyword').value;
	keyword = keyword.toLowerCase();
	
	switch (keyword) {
		case "branches": 		location.href="/branches.php"; 				return false; break;
		case "locations": 		location.href="/page.php?p=108";			return false; break;
		case "catalog": 		location.href="/product_library.php";		return false; break;
		case "products": 		location.href="/product.php";				return false; break;
		case "cfee": 			location.href="/cfee/";						return false; break;
		case "c.f.e.e.": 		location.href="/cfee/";						return false; break;
		case "documents": 		location.href="/product_library.php";		return false; break;
		case "news": 			location.href="/page.php?p=110"; 			return false; break;
		case "events": 			location.href="/page.php?p=110";			return false; break;
		case "promotions": 		location.href="/page.php?p=111";			return false; break;
		case "tradeshows": 		location.href="/page.php?p=113";			return false; break;
		case "press releases": 	location.href="/page.php?p=112";		return false; break;
		case "forms": 			location.href="/product.php?productSelector=false&listType=line&categoryId=100005";		return false; break;
		case "enter keyword...": return false; break;
		default: return true; break;
	}
}