function makeRequest() {
	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) {
					   try {
							httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
						   } 
						 catch (e) {}
					  }
								   }

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	else {
		return httpRequest;
	}
}

var http_request = makeRequest();
var array_tabs = new Array();
array_tabs[0] =  'description';
array_tabs[1] =  'application';
array_tabs[2] =  'ingredients';
array_tabs[3] =  'details';
array_tabs[4] =  'reviews';
array_tabs[5] =  'video';



function switch_pinfo( tab_title, pid ) {
	if (http_request.readyState == 4 || http_request.readyState == 0) {
		var str_tab_title = escape(tab_title);
		// set all the tabs back to the unselected state
		for ( var i = 0; i < array_tabs.length; i++ ) {
			tab_div_id = 'tab_div_' + array_tabs[i];
			var tab = document.getElementById( tab_div_id );
			if ( tab ){
				set_to_default( tab_div_id,pid );
			}
		}
		
		// set one tab to the current state
		set_current( str_tab_title, pid );
		
		// send request
		var str_pid = escape(pid);
		var rnd_no = Math.round(10000*Math.random());
		var href = 'aj/product_detail.php';
		var query_string = '?switch_pinfo=' + str_tab_title + '&pid=' + str_pid + '&rnd=' + rnd_no;
		http_request.open("GET", href + query_string, true);
		http_request.onreadystatechange = changeDescription; 
		http_request.send(null);
	}		
}

//Called when the AJAX response is returned.
function changeDescription() {
	if (http_request.readyState == 4) {
		var ss = document.getElementById('pd_desc')
		ss.innerHTML = http_request.responseText;
	}
}

/*
 * sets one of the tabs to the ordinary tab image, and the 
 * onclick onmouseover and onmouseout to the correct 
 * functions
*/
function set_to_default( tab_div_id, pid ){
	var tab_img_id = tab_div_id.replace( '_div_', '_img_' );
	var tab_img = document.getElementById( tab_img_id );
	// tab_img.src = tab_img.src.replace( 'hov', '' );
	tab_img.src = tab_img.src.replace( 'cur', '' );
	
	var tab_link_id = tab_div_id.replace( '_div_', '_link_' );
	var tab_link = document.getElementById( tab_link_id );
	
	tab_link.onclick = function(){ switch_pinfo( tab_div_id, pid); return false; };
	var tab_hov_src = tab_img.src.replace( 'tabs/tab', 'tabs/tabhov' );
	tab_link.onmouseover = function(){ MM_swapImage( tab_img_id, '', tab_hov_src); };
	tab_link.onmouseout = function(){ MM_swapImgRestore(); };
}

/*
 * sets one of the tabs to the current tab image, and the 
 * onclick onmouseover and onmouseout to do nothing 
*/
function set_current( tab_div_id, pid ){
	
	var tab_link_id = tab_div_id.replace( 'tab_div', 'tab_link' );
	var tab_link = document.getElementById( tab_link_id );
	
	tab_link.onclick = function(){ return false; };
	tab_link.onmouseover = function(){ return false; };
	tab_link.onmouseout = function(){ return false; };
	
	var tab_img_id = tab_div_id.replace( 'tab_div', 'tab_img' );
	var tab_img = document.getElementById( tab_img_id );
	tab_img.src = tab_img.src.replace( 'tabs/tabhov', 'tabs/tabcur' );
	
	
}
