// JavaScript Document

var xmlhttp1_products = false;
var xmlhttp2_products = false;

//Check if we are using IE.
try
{
	//If the Javascript version is greater than 5.
	xmlhttp1_products = new ActiveXObject("Msxml2.XMLHTTP");
	xmlhttp2_products = new ActiveXObject("Msxml2.XMLHTTP");


} 
catch (e) 
{
	//If not, then use the older active x object.
	try 
	{
		//If we are using Internet Explorer.
		xmlhttp1_products = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp2_products = new ActiveXObject("Microsoft.XMLHTTP");

	} 
	catch (E) 
	{
		//Else we must be using a non-IE browser.
		xmlhttp1_products = false;
		xmlhttp2_products = false;

	}
}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp1_products && typeof XMLHttpRequest != 'undefined') 
{
	xmlhttp1_products = new XMLHttpRequest();
}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp2_products && typeof XMLHttpRequest != 'undefined') 
{
	xmlhttp2_products = new XMLHttpRequest();
}


function getNewSwatch( id, imageName ){ //imageName is not used anymore
	// var obj = document.getElementById(objID);
	var serverPage = '/scripts/products.php?action=getSwatch&swatchId=' + id;
	// Can put a loading image on thetop
	// obj.innerHTML = '<img src="/images/adding_to_project.gif" alt="adding to project" />';
	
	xmlhttp1_products.open("GET", serverPage);
	xmlhttp1_products.onreadystatechange = function() {
		if (xmlhttp1_products.readyState == 4 && xmlhttp1_products.status == 200) {
			var args = pairstr2Arr(xmlhttp1_products.responseText, '=', '||');
			
			document.getElementById( 'order_sample_link' ).onclick = function(){ addSwatch(id); return false; }
			document.getElementById( 'product_id' ).innerHTML = args['color'];
			document.getElementById( 'product_swatch' ).src = '/' + args['image'];//imageName;
			var hiddenElem = document.getElementById( 'hidden_swatch_id' );
			document.getElementById( 'swatch_' + hiddenElem.innerHTML).className = "unhighlight";
			document.getElementById( 'swatch_' + id).className = "highlight";
			hiddenElem.innerHTML = id;
			
			//now set the previous and next buttons
			var prev = document.getElementById( 'previous_product' );
			if(args['previous_id'] == ""){
				prev.style.display = "none";
			}
			else{
				prev.href = location.pathname + "?swatchId=" + args['previous_id'];
				prev.onclick = function(){ getNewSwatch(args['previous_id'], args['image']); return false;}
				prev.style.display = "inline";
			}
			var next = document.getElementById( 'next_product' );
			if(args['next_id'] == ""){
				next.style.display = "none";
			}
			else{
				next.href = location.pathname + "?swatchId=" + args['next_id'];
				next.onclick = function(){ getNewSwatch(args['next_id'], args['image']); return false;}
				next.style.display = "inline";
			}
		}
		else
		{
			// do nothing
		}
	}
	
	xmlhttp1_products.send(null);	
}

function pairstr2Arr (str, separator, delim) {
	var arr = new Array();
    var elems = str.split(delim);
    for(var i=0; i < elems.length; i++) {
        var val = elems[i];
        var nameVal = val.split(separator);
        arr[nameVal[0]] = nameVal[1];
    }
        return arr;
}

function showProdDetail( id, arrowId ) {
	ai = document.getElementById( arrowId );
	elem = document.getElementById( id );
	
	if( ai.src.match(/product_arrow.gif/) ) {
		ai.src = '/images/product_arrow_down.gif';
	}
	else {
		ai.src = '/images/product_arrow.gif';
	}
	if( elem.style.display == 'none' || elem.style.display == "" ) {
		elem.style.display = 'block';
	}
	else {
		elem.style.display = 'none';
	}
}