// Declare our global variables.
var browser = BrowserDetect.browser;								  								  						
var sku_hash = new Object();							
var color_size_array = new Array();														
var size = new Array();
var avail2 = new Array();


// This function parses the data retrived from Hyper, and formats appropriately.
function rt_inventory() {
	// Split the information gathered from the .asp script.
	//document.write(avail);	
	var avail_array = avail.split(";");
	var sorted_sizes = new Array();	
	var temp = new Array();
	
	
	// JavaScript doesn't support multidimensional arrays, so we have to fake it.
	// We start on 1, because the split above produces an empty string for element 0.							
	for ( i = 1 ; i<avail_array.length ; i++ )
	{
		color_size_array[i] = new Array();
		color_size_array[i] = avail_array[i].split(",");									
	}	
	
	// This loop populates the two-dimensional array color_size_array, and then digests the
	// information - populating size and avail2 with availability information that is mapped to
	// color using the index i.
	for (var i = 1 ; i<color_size_array.length ; i++)
	{		
		// Now we digest the sku information, so we can use it to display images. This maps
		// color to sku number.  We chop off the last two elements of the array, because they
		// are not used in the image path.
		temp = color_size_array[i][0].split("|");		
		sku_hash[temp[0]] = temp[1].substring(0,temp[1].length - 2);				
		
		// Now that we have mapped color to sku number, we need to chop off the sku so we can have
		// a direct relationship between color_size_array[], size[], and avail2[] (e.g. the first
		// element of color_size_array is the color, and the first elements of size[] and avail2[]
		// are lists of sizes and availability, which also correspond).
		color_size_array[i][0] = temp[0];
		
		// This is so we can fake a multidimensional array for size, and avail2.
		size[i] = new Array();
		avail2[i] = new Array();
		
		var tmp = new Array();
				
		// This loop starts on 1 because we want to skip the color name.
		for (var j = 1 ; j<color_size_array[i].length - 1 ; j++) {		
			color_size_array[i][j] = trimString(color_size_array[i][j]);
				
			// Since the information coming from Hyper is sorted by itemnumber, and not size - some of
			// the sizes returned may be out of order.  We must sort them.
			tmp.push(color_size_array[i][j]);
			//temp = color_size_array[i][j].split("@");
			//size[i][j] = temp[0];
			//avail2[i][j] = temp[1];
			//document.write(color_size_array[i][0] + ": " + size[i][j] + " " + avail2[i][j] + " ");									
		}
				
		// Sort using our proprietary size sorting algorithm ;)		
		sorted_sizes = tmp.sort(sort_sizes);
		
		// Run through the sorted array, and make sure the array indices line up with
		// color_size_array (that's what the x+1 is for).
		for (var x = 0; x<sorted_sizes.length; x++) {
			temp = sorted_sizes[x].split("@");
			size[i][x+1] = temp[0];
			avail2[i][x+1] = temp[1];
		}
		
		//document.write("<br />");
		//document.write(sku_hash[color_size_array[i][0]] + " ");
	}	
}



// This function tests a, and if it is not a number - it sorts against predetermined sort orderd,
// otherwise it sorts alpha, and then numerically - modified by James
// note: a&b are not 'Sizes' they are "SIZE@QTY" in a string
function sort_sizes(a, b) {
        var sizes = ['XS' ,'S', 'M','L','XL','XXL','XXXL' ];
        var a_val = sizes.length + 1;
        var b_val = sizes.length + 1;
        for(var i = 0; i < sizes.length; i ++) {
                if (sizes[i] == a.split('@')[0]) { a_val = i; }
                if (sizes[i] == b.split('@')[0]) { b_val = i; }
        }

        if ((a_val < (sizes.length + 1)) || (b_val < (sizes.length + 1)))
        {
                return a_val - b_val;
        }

        if ( isNaN(parseInt(a, 10)) || isNaN(parseInt(b, 10)) ) {
                return (b < a) - (a < b);
        }
        else {
                return parseInt(a, 10)-parseInt(b, 10);
        }
}

// This function tests a, and if it is not a number - it sorts alphabetically, 
// otherwise it sorts numerically.
//function sort_sizes(a, b) {
//	if ( isNaN(parseInt(a, 10)) || isNaN(parseInt(b, 10)) ) {
//		return (b < a) - (a < b);	
//	}
//	else {
//		return parseInt(a, 10)-parseInt(b, 10);
//	}
//}

// This function draws the swatches, based on the item name, image path, and initial display values.
function draw_swatches(itemName, imgPath, display) {
	// We need an array in order to keep track of all the references to table rows.
	temprow = new Array();	
	
	// Generate the table that will contain the size tags.
	document.getElementById('container').innerHTML = document.getElementById('container').innerHTML + '<table id="size_table" class="rt_sizediv">';
	
	for (i = 1; i<color_size_array.length; i++)
	{									
		// Since avail2 is a two-dimensional array, we need to sum all the elements to make sure that at least something
		// is in stock, otherwise we gray out the whole color swatch.									
		var temp = 0;
		for ( j = 1; j<avail2[i].length; j++) 
		{
			temp += parseInt(avail2[i][j], 10);										
		}									
		
		// Color - in stock.
		if ( temp > 0)
		{									
			// Generate the swatch tags.
			document.getElementById('swatchdiv').innerHTML = document.getElementById('swatchdiv').innerHTML + '<div class="rt_swatch"><a onmouseout="swapcolor(\'\')" onmouseover="swapcolor(\'' + color_size_array[i][0] + '\')" onClick="updateSize(temprow[' + i + ']); swapNormalImg(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-p.jpg\'); updateLargeImgSrc(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '.jpg\'); updateNormalImgSrc(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-p.jpg\'); swapmaincolor(\'' + color_size_array[i][0] + '\'); makeborder(\'theswatch' + i + '\'); swapoptcolor(\'' + color_size_array[i][0] + '\');"><img src="' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-s.jpg" alt="' + color_size_array[i][0] + '" width="25" height="25" style="border: 2px solid #000000;" id="theswatch' + i + '"></a></div>';
			
			// Generate the respective size tags.												
			
			// We have to use the methods insertRow, and insertCell.
			// Using an index of -1 appends the row.											
			temprow[i] = document.getElementById('size_table').insertRow(-1);											
			
			// We have to use the methods insertRow, and insertCell.											
			for ( j = 1; j<size[i].length; j++)
			{												
				var tempcell = new Array();
				
				// This color and size is in stock.
				if ( parseInt(avail2[i][j], 10) > 0 )
				{
					// Using an index of -1 appends the cell.													
					tempcell[i] = temprow[i].insertCell(-1);
					
					// Use different styles for IE, and Mozilla.
					if ( BrowserDetect.browser == 'Explorer' ) {
						
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\');selectSize()" class="rt_size_text_IE" id="thesize' + i + j + '" style="padding: 0px; margin: 2px;"><span style="padding: 0px; margin: 0px; width: 23px; height: 25px;">' + size[i][j] + '</span></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\');selectSize()" class="rt_size_IE" id="thesize' + i + j + '" style="margin: 0 2px 0 2px;"><span style="padding: 0px; margin: 0px; width: 23px; height: 25px;">' + size[i][j] + '</span></div>';													
						}
					
					}
					else {
						
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\');selectSize()" class="rt_size_text" id="thesize' + i + j + '" style="margin: 2px;"><span style="padding: 0px; margin: 0px; width: 23px; height: 25px;">' + size[i][j] + '</span></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\');selectSize()" class="rt_size" id="thesize' + i + j + '" style="margin: 2px;"><span style="padding: 0px; margin: 0px; width: 23px; height: 25px;">' + size[i][j] + '</span></div>';													
						}
						
					}
				}
				
				// This color and size is out of stock.
				else
				{
					// Using an index of -1 appends the cell.													
					tempcell[i] = temprow[i].insertCell(-1);
					
					// Use different styles for IE, and Mozilla.
					if ( BrowserDetect.browser == 'Explorer' ) {
					
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text_out_IE" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_out_IE" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';													
						}					
					}
					else {
						
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text_out" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_out" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';													
						}						
					}
				}
				
				if ( BrowserDetect.browser != 'Explorer' ) {
					// Keep things organized by limiting the number of sizes per row.
					if (j % 10 == 0)
					{ 
						temprow[i].innerHTML = temprow[i].innerHTML + "<br />";												
					}
				}
			}

			// Set the initially selected swatch, based on the 'display variable'.			
			if ( sku_hash[color_size_array[i][0]].substring(sku_hash[color_size_array[i][0]].length - 2, sku_hash[color_size_array[i][0]].length) == display ) {				
				makeborder("theswatch" + i);
				updateSize(temprow[i]);
				swapmaincolor(color_size_array[i][0]);
				document.getElementById('optsize').value = "notanoption";
			}
			
		}
		
		// Color - out of stock.
		else
		{										
			// Generate the swatch tags.
			document.getElementById('swatchdiv').innerHTML = document.getElementById('swatchdiv').innerHTML + '<div class="rt_swatch_out" onMouseOver="swapcolor(\'' + color_size_array[i][0] + '\');" onmouseout="textOff(); swapcolor(\'\')"><img onclick="updateLargeImgSrc(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '.jpg\'); updateNormalImgSrc(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-p.jpg\'); updateSize(temprow[' + i + ']); swapcolor(\'Out of Stock\'); textOver(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + ' is currently <br /><b>Out of Stock</b>\'); swapNormalImg(\'' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-p.jpg\')" class="rt_swatch_img_out" src="' + imgPath + '/' + sku_hash[color_size_array[i][0]] + '-s.jpg" alt="' + color_size_array[i][0] + '" id="theswatch' + i + '"></div>';
			
			// Generate the respective size tags.												
			
			// We have to use the methods insertRow, and insertCell.
			// Using an index of -1 appends the row.											
			temprow[i] = document.getElementById('size_table').insertRow(-1);											
			
			// We have to use the methods insertRow, and insertCell.											
			for ( j = 1; j<size[i].length; j++)
			{												
				var tempcell = new Array();
				
				// This color and size is in stock.
				// THIS SHOULD NEVER BE TRUE - This is here just to be consistent.
				if ( parseInt(avail2[i][j], 10) > 0 )
				{
					// Using an index of -1 appends the cell.													
					tempcell[i] = temprow[i].insertCell(-1);
					
					// Use different styles for IE, and Mozilla.
					if ( BrowserDetect.browser == 'Explorer' ) {
					
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text_IE" id="thesize' + i + j + '" onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\')>' + size[i][j] + '</div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_IE" id="thesize' + i + j + '" onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\')">' + size[i][j] + '</div>';													
						}						
					}
					else {
						
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.													
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text" id="thesize' + i + j + '" onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\')">' + size[i][j] + '</div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size" id="thesize' + i + j + '" onmouseout="swapsize(\'\')" onMouseOver="swapsize(\'' + size[i][j] + '\');" onClick="swapmainsize(\', ' + size[i][j] + '\'); makesizeborder(\'thesize' + i + j + '\')">' + size[i][j] + '</div>';													
						}						
					}
				}
				
				// This color and size is out of stock.
				else
				{
					// Using an index of -1 appends the cell.													
					tempcell[i] = temprow[i].insertCell(-1);
					
					// Use different styles for IE, and Mozilla.
					if ( BrowserDetect.browser == 'Explorer' ) {					
					
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text_out_IE" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_out_IE" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';													
						}					
					}
					else {
						
						// Test if the size contains words (such as "One Size Fits All), and change the style 
						// if it does.
						if ( isNaN(parseInt(size[i][j], 10)) )
						{
							// The size contains words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_text_out" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';
						}
						else
						{
							// The size does not contain words.
							tempcell[i].innerHTML = tempcell[i].innerHTML + '<div class="rt_size_out" id="thesize' + i + j + '" onclick="swapsize(\'Out of Stock\');" onMouseOver="swapsize(\'' + size[i][j] + '\'); textOverSize(\'<b>' + color_size_array[i][0] + '</b> ' + itemName + '\', \'size <b>' + size[i][j] + '</b> is currently<br /><b>Out of Stock</b>\')" onmouseout="textOff(); swapsize(\'\')"><font style="cursor: default; opacity: 0.5; color: #736F6E; filter:alpha(opacity=10)">' + size[i][j] + '</font></div>';													
						}						
					}
				}
				
				if ( BrowserDetect.browser != 'Explorer' ) {
					// Keep things organized by limiting the number of sizes per row.
					if (j % 10 == 0)
					{ 
						temprow[i].innerHTML = temprow[i].innerHTML + "<br />";													
					}
				}
			}										
		}								
	}
	
	document.getElementById('swatchdiv').innerHTML = document.getElementById('swatchdiv').innerHTML + "</tr></table>";
}

// This function swaps the given element with the one in sizediv.
function updateSize(element)
{
	// In order to prevent the border from disappearing, we need to uniquely
	// name the divs that are being copied, so we are going to replace all
	// occurences of 'thesize' with 'sizegrid'.
	var oldHTML = element.innerHTML;	
	var newHTML = oldHTML.replace(/thesize/g,"sizegrid");	
	document.getElementById('sizediv').innerHTML = newHTML;									
}

// This function ensures that a color and size are selected before proceeding to the cart.
// It also updates the vwitem value with the proper sku, of what is selected.
function check_options(form) {	
	if (form.optcolor.value != 'notanoption' && form.optsize.value != 'notanoption') {
		// They can pass.
		// Update vwitem.		
		document.getElementById('vwitem').value = 
		  loader_sku_hash[trimString(document.getElementById('maincolorname').innerHTML)][trimString(document.getElementById('mainsizename').innerHTML.replace(/,/, ''))]		
		return true;		
	}
	else {
		// They may not pass.
		if (form.optcolor.value == 'notanoption') {
			flash_color_flag = true;			
		}
		if (form.optsize.value == 'notanoption') {
			flash_size_flag = true;			
		}
		return false;
		
	}		
}

// This function flashes the size text when a user tries to proceed to the cart
// without selecting a size.
// UPDATED: Allison Miller, 9-7-2010
//		This function now pops up div layers that 
//		make it obvious that the user needs to choose a size.
var flash_size_count = 0;
var flash_color_count = 0;
var flash_size_flag = false;
var flash_color_flag = false;

function flashText() {	
	if (flash_size_flag) {	
		// Only make div visible once, otherwise it may popup again 
		// after add to bag was clicked
		if (flash_size_count < 1) {	
			document.getElementById('selectSize').style.display = "block"; 
			document.getElementById('selectSizeMore').style.display = "block"; 
		}
		
		// Test our counter to make sure we should even flash the text.
		if (flash_size_count < 8) {		
			if (document.getElementById('flash_size').style.color == "rgb(0, 153, 204)") {			
				document.getElementById('flash_size').style.color = "rgb(0, 0, 0)"	
			}
			else {			
				document.getElementById('flash_size').style.color = "rgb(0, 153, 204)"
			}
			
			// After changing the text, increment the count - so we don't loop forever.
			flash_size_count++;
		} else { // Otherwise, we reset the flag and counter.
			document.getElementById('flash_size').style.color = "rgb(0, 0, 0)"
			flash_size_flag = false;
			flash_size_count = 0;
		}
	}
	
	if (flash_color_flag) {
		// Test our counter to make sure we should even flash the text.
		if (flash_color_count < 8) {		
			if (document.getElementById('flash_color').style.color == "rgb(0, 153, 204)") {			
				document.getElementById('flash_color').style.color = "rgb(0, 0, 0)"	
			}
			else {			
				document.getElementById('flash_color').style.color = "rgb(0, 153, 204)"
			}
			
			// After changing the text, increment the count - so we don't loop forever.
			flash_color_count++;
		}
		
		// Otherwise, we reset the flag and counter.
		else {
			document.getElementById('flash_color').style.color = "rgb(0, 0, 0)"
			flash_color_flag = false;
			flash_color_count = 0;
		}	
	}
	
	var sret = setTimeout("flashText()", 300);		
}

// This function changes the form submit image
// from gray to pink to go along with previous function
// in order to make selecting a size more apparent.
function selectSize() {
	document.all.addToBagImage.src = "/images/add-to-bag.gif";
	document.getElementById('selectSize').style.display = "none";
	document.getElementById('selectSizeMore').style.display = "none";
}
