﻿function getSwatches(pfid)
{
	var imageFile = pfid;
	if(product_image[0].image != "" && product_image[0].image.indexOf("t.jpg")==-1 ){
		imageFile = product_image[0].image;				
	}

	var extra = "";
	if (enable_manual_quote==1) { extra = "_is";}
			
	loadxmlDoc(imgURLpre + imageFile +extra,'req=imageset,XML,UTF-8');
}


/*GETS SWATCH INFORMATION FROM XML*/
var xmlDoc = null;
var swatch_images = new Array();
var imgIndex = 0;
var txtLoadDone = false;
function loadxmlDoc(url,args)
{	
	
 	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}	
	
	xmlDoc.open("GET","CrossDomainProxy.asp?UUurl="+url+"&"+args,true);

	xmlDoc.onreadystatechange = 
			function(){
				try{ 
					if(xmlDoc.readyState == 4){	// 4 = complete
						//if ERROR is NOT contained in the returned message...
						if(xmlDoc.responseText.search(/ERROR/i)==-1){
							
							//alert(xmlDoc.responseText);
							
                            var is_data = xmlDoc.responseText.substring(xmlDoc.responseText.indexOf('value="')+7,xmlDoc.responseText.lastIndexOf('"'));

                            if(is_data && is_data!=""){
                                //replace any commas with semicolons
                                is_data = is_data.replace(/,/g,';');
                                is_data = is_data.replace(/GolfGalaxy\//ig,'');
                            	
                                var is_images = is_data.split(';');	
                            	
                                for(var aSwatch=0;aSwatch<is_images.length;aSwatch+=2){
                                    swatch_images.push(new swatchElement(is_images[aSwatch],""));				
                                }
                            	
                                //get the swatch data/labels
                                getSwatchData();
                            	
                            	//display swatches
                            	//displaySwatchImages();
                            }

						}//end if
					}//end if readyState == 4
					else {
						// So IE won't throw an error
						return;
					}
				}catch(e){
					// improperly formed json
				}			};
				
		xmlDoc.send(null);
}


function getSwatchData()
{
	
	var swatchList = "";
	//go through swatch_images and build query to get data
	for(var aSwatch=0;aSwatch<swatch_images.length;aSwatch++){
		swatchList += swatch_images[aSwatch].imgName + "|";
	}
	
	
	//do call to get data
	if(swatchList!="") {
		if (document.implementation && document.implementation.createDocument)
		{
			textDoc = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			textDoc = new ActiveXObject("Microsoft.XMLHTTP");
	 	}
		else
		{
			alert('Your browser can\'t handle this script');
			return;
		}
		//take out last |
		swatchList = swatchList.substring(0,swatchList.length-1);
		
		textDoc.open('GET', 'CrossDomainProxy.asp?UUurl='+imgURLpre+'|UUarg|&req=userdata&UUargs='+swatchList, true);
		
		textDoc.onreadystatechange = 
			function(){
				try{
					if(textDoc.readyState == 4){	// 4 = complete
						//if ERROR is NOT contained in the returned message...
						if(textDoc.responseText.search(/ERROR/i)==-1){
							//alert(textDoc.responseText);
							
							//split response on |
							var arSwatchData = textDoc.responseText.split('|');
							
							//add each data to array (looping through swatch_images array and using arSwatchData array
							for(var aSwatch=0;aSwatch<swatch_images.length;aSwatch++){
								//make sure no error in data
								if(arSwatchData[aSwatch] && arSwatchData[aSwatch].search(/ERROR/i)==-1)
								{
									//remove label= from data to get actual value
									var displayText = arSwatchData[aSwatch].replace(/label=/ig,'');
									//if the data is not empty...
									if(arSwatchData[aSwatch].replace(/\s/ig,'') != "" && displayText != "") {
										//add data to swatch_images
										swatch_images[aSwatch].label = displayText;
									}
								}
							}//end for
							
							//display swatches
                            displaySwatchImages();

						}//end if no ERROR
					}
					else {
						// So IE won't throw an error
						return;
					}
				}catch(e){
					// improperly formed json
				}			
			};
		textDoc.send(null);
	}//end if
}


function displaySwatchImages() {
 	
	for(aSwatch=0;aSwatch<swatch_images.length;aSwatch++){
		document.getElementById('lyr1').innerHTML += get_S7_image(swatch_images[aSwatch].imgName,"s7swatch", swatch_images[aSwatch].label, swatch_images[aSwatch].imgName, "");
	}
	
	document.getElementById('swatchHolder').style.display = "block";
	document.getElementById('swatch_up').style.display = "none";
		document.getElementById('swatch_down').style.display = "none";
	
	
	//if there are more than 4 images to show, display scroll arrows
	if (swatch_images.length > 4) {
		document.getElementById('swatch_up').style.display = "block";
		document.getElementById('swatch_down').style.display = "block";
	}
	
}


function swatchElement(imgName,label)
{
	this.imgName = imgName;
	this.label = label;
}