var xmlhttp1_projects = false;
var xmlhttp2_projects = false;

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


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

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

	}
}

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

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


function removeSwatch( id ) {

	document.getElementById( 'thumb_' + id ).src="images/swatches/65x35.jpg";
	document.getElementById( 'side_bar_swatch' ).src="images/swatches/no_swatch.jpg";
	// var obj = document.getElementById(objID);
	var serverPage = '/scripts/projects.php?action=remove&swatchId=' + id + '&type=samples';
	
	// Can put a loading image on thetop
	// obj.innerHTML = '<img src="/images/adding_to_project.gif" alt="adding to project" />';
	
	xmlhttp1_projects.open("GET", serverPage);
	xmlhttp1_projects.onreadystatechange = function() {
		if (xmlhttp1_projects.readyState == 4 && xmlhttp1_projects.status == 200) {
			//document.getElementById( 'errors' ).innerHTML = xmlhttp1_projects.responseText;
			var args = pairstr2Arr(xmlhttp1_projects.responseText, '!', '||');
			
			document.getElementById( 'samples' ).innerHTML = args['htmlText'];
			var samplesCount = document.getElementById( 'samples_count' );
			samplesCount.innerHTML = parseInt(samplesCount.innerHTML) - 1;
			if(args['id'] != "undefined")
				changeImage( args['id'], args['image'] );
			else
				document.getElementById( 'side_bar_color' ).innerHTML = "";
		}
		else
		{
			// do nothing
		}
	}
	
	xmlhttp1_projects.send(null);
}


function addSwatch( id ) {
	
	if( document.getElementById( 'thumb_' + id ) && document.getElementById( 'thumb_' + id ) != "undefined" ) {
		document.getElementById( 'thumb_' + id ).src="images/swatches/65x35.jpg";
	}
	
	// var obj = document.getElementById(objID);
	var serverPage = '/scripts/projects.php?action=add&swatchId=' + id + '&type=samples';
	
	// Can put a loading image on thetop
	// obj.innerHTML = '<img src="/images/adding_to_project.gif" alt="adding to project" />';
	
	xmlhttp1_projects.open("GET", serverPage);
	xmlhttp1_projects.onreadystatechange = function() {
		if (xmlhttp1_projects.readyState == 4 && xmlhttp1_projects.status == 200) {
			//document.getElementById( 'errors' ).innerHTML = xmlhttp1_projects.responseText;
			
			var args = pairstr2Arr(xmlhttp1_projects.responseText, '!', '||');
			
			// popup open side panel if first swatch
			if( args['sampleCount'] == '1' ) {
				
				// check to see if panel is already open
				if( document.getElementById("side_bar").style.display == "none" ) {
					// set work root status
					var serverPage = 'scripts/projects.php?action=toggleState&makeOpen=false';
					
					xmlhttp2_projects.open("GET", serverPage);
					xmlhttp2_projects.send(null);
					
					// display work room panel
					document.getElementById("side_bar").style.display = "inline";

				}
			}
			if( args['full'] != 'true' ) { 
				document.getElementById( 'samples' ).innerHTML = args['htmlText'];
				
				var samplesCount = document.getElementById( 'samples_count' );
				
				samplesCount.innerHTML = args['sampleCount'];
				
				changeImage( id );
				//changeSideBarSwatchImage( id );
			}
			else {
				alert( 'You cannot add more than 10 swatches.');
			}
		}
		else
		{
			// do nothing
		}
	}
	
	xmlhttp1_projects.send(null);
}

function toggleProject() {
	
	var sideBar = document.getElementById("side_bar");

	var makeOpen;
	if(sideBar.style.display == "none")
		makeOpen = true;
	else
		makeOpen = false;
		
	// var obj = document.getElementById(objID);
	var serverPage = '/scripts/projects.php?action=toggleState&makeOpen=' + makeOpen;
	
	xmlhttp1_projects.open("GET", serverPage);
	xmlhttp1_projects.onreadystatechange = function() {
		if (xmlhttp1_projects.readyState == 4 && xmlhttp1_projects.status == 200) {
			if( makeOpen ){
				document.getElementById("side_bar").style.display = "inline";
			}
			else{
				document.getElementById("side_bar").style.display = "none";
			}
		}
		else
		{
			// do nothing loading animation could og here
		}
	}
	
	xmlhttp1_projects.send(null);
}

function changeImage( id, imageName ) {
	var serverPage = '/scripts/products.php?action=getSwatch&swatchId=' + id;
	
	xmlhttp1_projects.open("GET", serverPage);
	xmlhttp1_projects.onreadystatechange = function() {
	
		if (xmlhttp1_projects.readyState == 4 && xmlhttp1_projects.status == 200) {
			
			var args = pairstr2Arr(xmlhttp1_projects.responseText, '=', '||');
			
			document.getElementById( 'side_bar_swatch' ).src = '/' + args['imagePanelLrg'];
			//document.getElementById( 'side_bar_swatch' ).src = '/images/swatches/' + imageName;
			document.getElementById( 'side_bar_remove' ).onclick = function(){ removeSwatch( id );return false;	};
			if( document.getElementById( 'isDistributor' ) ) {
				document.getElementById( 'side_bar_go_to_group' ).attributes["href"].value = '/manufacturers/' + document.getElementById( 'distributorUrl' ).innerHTML  + '/pattern/market[1]=' + args['pattern_id'];
			}
			else {
				document.getElementById( 'side_bar_go_to_group' ).attributes["href"].value = document.getElementById( 'searchPage' ).innerHTML + '?market[1]=' + args['pattern_id'];
			}
			document.getElementById( 'side_bar_color' ).innerHTML = args['color'];
		}
	}
	
	xmlhttp1_projects.send(null);
}

function changeSideBarSwatchImage( id ) {
	var serverPage = '/scripts/products.php?action=getSwatch&swatchId=' + id;
	
	xmlhttp1_projects.open("GET", serverPage);
	xmlhttp1_projects.onreadystatechange = function() {
		if (xmlhttp1_projects.readyState == 4 && xmlhttp1_projects.status == 200) {
			var args = pairstr2Arr(xmlhttp1_projects.responseText, '=', '||');
			
			document.getElementById( 'side_bar_swatch' ).src = '/' + args['imagePanelLrg'];
		}
	}
	
	xmlhttp1_projects.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 clearSelect( set ) {
	switch( set ) {
		case "1":
			document.getElementById( 'collection').options[0].selected =true;
			document.getElementById( 'pattern').options[0].selected =true;
			break;
		case "2":
			document.getElementById( 'market').options[0].selected =true;
			document.getElementById( 'pattern_name').options[0].selected =true;
			document.getElementById( 'color').options[0].selected =true;
			break;
		default:
			document.getElementById( 'collection').options[0].selected =true;
			document.getElementById( 'pattern').options[0].selected =true;
			document.getElementById( 'market').options[0].selected =true;
			document.getElementById( 'pattern_name').options[0].selected =true;
			document.getElementById( 'color').options[0].selected =true;
			break;
	}
}