var lastclick = null;
var ahinventory = { };
	
function validateCallBack(v, m, f)
{
	if(v)
		lastclick.submit();
}

function removeColor(obj, colorobj)
{
	var colorname = obj.substring(4, obj.length);
	// Locate all TD tags. Find the one with the color we want, and make it hidden.
	var internalobjects = document.getElementsByTagName("td");
	for(var j=0; j < internalobjects.length; j++)
	{
		if(internalobjects[j].innerHTML.indexOf(colorname) >= 0 && (internalobjects[j].innerHTML.indexOf("/font") >= 0 || internalobjects[j].innerHTML.indexOf("/FONT") >= 0))
		{
			if(internalobjects[j].innerHTML.length < 500)
				internalobjects[j].style.display = 'none';
		}
	}
	
	for(var c=0; c < colorobj.options.length; c++)
	{
		if(colorobj.options[c].value == obj)
			colorobj.remove(c);
	}
}

function validateInventory(currentform)
{
	var internalobjects = currentform.getElementsByTagName("select");
	var hiddenobjects = currentform.getElementsByTagName("input");
	var colorobj = null;
	var sizeobj = null;
	var widthobj = null;
	var skuname = "";
	var stylename = "";
	var colorname = "";
	var sizename = "";
	var widthname = "";
	var personalization = 0;
	var personalizationcomponent = 0;
	
	for(var j=0; j < internalobjects.length; j++)
	{
		if(internalobjects[j].name == "1.Color" || internalobjects[j].name == "1.Print" || internalobjects[j].name == "1.Team")
			colorobj = internalobjects[j];
		if(internalobjects[j].name == "2.Size")
			sizeobj = internalobjects[j];
		if(internalobjects[j].name == "3.Width")
			widthobj = internalobjects[j];
			
		if(internalobjects[j].name.indexOf("4a.Left Chest") >= 0 && internalobjects[j].selectedIndex == 0)
			personalizationcomponent++;
		if(internalobjects[j].name.indexOf("5a.Right Chest") >= 0 && internalobjects[j].selectedIndex == 0)
			personalizationcomponent++;
	}
	
	if(colorobj && colorobj.value == "-1") // Check for invalid selection value
	{
		$.prompt("Sorry, you have not chosen a color for this product. Please select a color and then try again.", {buttons: { 'OK, Return to Shopping': false } } );
		return false;
	}
	
	if(sizeobj)
		sizeobj.disabled = false;
		
	for(var j=0; j < hiddenobjects.length; j++)
	{
		if(hiddenobjects[j].title.indexOf("7.Personalization") >= 0 && hiddenobjects[j].value == "")
			personalization = 1;
			
		if(hiddenobjects[j].name == "x")
		{
			stylename = hiddenobjects[j].value;
			skuname = hiddenobjects[j].value;
		}
	}
	
	if(personalizationcomponent > 1)
		personalization = 1;
		
	// Get names
	if(colorobj)
		colorname = colorobj.value.substring(0, 3);
	if(sizeobj)
		sizename = sizeobj.value.substring(0, 3);
	if(widthobj)
		widthname = widthobj.value.substring(0, 3);
	
	// Replace tall and petite
	stylename = stylename.replace("Tall", "");
	stylename = stylename.replace("Petite", "");
	stylename = stylename.replace("TALL", "");
	stylename = stylename.replace("PETITE", "");
	
	stylename = stylename.substring(0, 10);
	
	for(var k = 0; k < ahinventory.product.length; k++)
	{
	
		if(ahinventory.product[k].u.toLowerCase() == skuname.toLowerCase())
		{
			stylename = ahinventory.product[k].yid;
			colorname = ahinventory.product[k].c;
			sizename = ahinventory.product[k].s;
			widthname = ahinventory.product[k].w;
		}
	
		if(ahinventory.product[k].yid.toLowerCase() == stylename.toLowerCase())
			if(ahinventory.product[k].c.toLowerCase() == colorname.toLowerCase() || ahinventory.product[k].c.toLowerCase() == colorname.toLowerCase().replace(' ', ''))
				if(ahinventory.product[k].s.toLowerCase() == sizename.toLowerCase())
					if(ahinventory.product[k].w.toLowerCase() == widthname.toLowerCase())
					{
						// Match
						if(ahinventory.product[k].d == 0 && ahinventory.product[k].av < 1) // Not disc
						{
							lastclick = currentform;
							$.prompt("The product you have selected is currently on backorder and is expected to be available by <nobr>" + ahinventory.product[k].dd + "</nobr>.<br /><br />Click OK to add this item to your shopping cart and the product will be delivered to you as soon as it is available. Your credit card will not be charged until we ship your order. If you do not want to wait for this item, click Cancel to continue shopping.", { buttons: { 'Ok, Add to Cart': true, 'Return to Shopping': false }, focus: 1, callback: validateCallBack });
							return false;
						}
						if(ahinventory.product[k].d == 1 && ahinventory.product[k].av < 1) // Disc
						{
							$.prompt("Sorry, the product you have selected has sold out and is no longer available. Please click OK to continue shopping for a different product from this or another page.", {buttons: { 'OK, Return to Shopping': false } } );
							return false;
						}
						
					}
	}
	
	if(personalization == 1)
	{
		lastclick = currentform;
		$.prompt("Did you know that personalization is available for the product you have selected? Would you like to add personalization, or place the product in your cart now without personalization?", { buttons: { 'No thank you, Add to Cart': true, 'Add Personalization': false }, focus: 1, callback: validateCallBack });
		return false;
	}
	
	return true;
}

function changeToSoldOut(obj)
{
	for(var i = 0; i < obj.options.length; i++)
	{
		obj.options[i].text = obj.options[i].text + " - SOLD OUT";	
		obj.options[i].text = obj.options[i].text.replace(" - SOLD OUT", "");
	}	
}

function isSoldOut(stylename, skuname, colorname, sizename, widthname)
{		
	for(var k = 0; k < ahinventory.product.length; k++)
	{
		if(ahinventory.product[k].u.toLowerCase() == skuname.toLowerCase())
		{
			stylename = ahinventory.product[k].yid;
			colorname = ahinventory.product[k].c;
			sizename = ahinventory.product[k].s;
			widthname = ahinventory.product[k].w;
		}
		
		if(ahinventory.product[k].yid.toLowerCase() == stylename.toLowerCase())
			if(ahinventory.product[k].c.toLowerCase() == colorname.toLowerCase())
				if(ahinventory.product[k].s.toLowerCase() == sizename.toLowerCase())
					if(ahinventory.product[k].w.toLowerCase() == widthname.toLowerCase())
					{
						// Match
						if(ahinventory.product[k].d == 1 && ahinventory.product[k].av < 1) // Disc
						{
							return true;
						}
					}
	}
	return false;
}

function isDelayed(stylename, skuname, colorname, sizename, widthname)
{		
	for(var k = 0; k < ahinventory.product.length; k++)
	{
		if(ahinventory.product[k].u.toLowerCase() == skuname.toLowerCase())
		{
			stylename = ahinventory.product[k].yid;
			colorname = ahinventory.product[k].c;
			sizename = ahinventory.product[k].s;
			widthname = ahinventory.product[k].w;
		}
		
		if(ahinventory.product[k].yid.toLowerCase() == stylename.toLowerCase())
			if(ahinventory.product[k].c.toLowerCase() == colorname.toLowerCase())
				if(ahinventory.product[k].s.toLowerCase() == sizename.toLowerCase())
					if(ahinventory.product[k].w.toLowerCase() == widthname.toLowerCase())
					{
						// Match
						if(ahinventory.product[k].rrp == 1 && ahinventory.product[k].av > 0) // RRP Delay
						{
							return true;
						}
					}
	}
	return false;
}

function inventoryCheck(obj)
{
	var currentform = null;
	var internalobjects = null;
	var colorobj = null;
	var sizeobj = null;
	var widthobj = null;
	var hiddenobjects = null;
	var stylename = null;
	var skuname =  null;
	var colorname = "";
	var fullcolorname = "";
	var sizename = "";
	var widthname = "";
	var found = 0;
	var allsoldout = 1;
	
	// Find the object
	var allformelements = document.getElementsByName("master-form");
	for(var i=0; i < allformelements.length; i++)
	{
		currentform = allformelements[i];
		internalobjects = currentform.getElementsByTagName("select");
		hiddenobjects = currentform.getElementsByTagName("input");
		colorobj = null;
		sizeobj = null;
		widthobj = null;
		found = 0;
		
		for(var j=0; j < internalobjects.length; j++)
		{
			if(internalobjects[j].name == "1.Color" || internalobjects[j].name == "1.Print" || internalobjects[j].name == "1.Team")
				colorobj = internalobjects[j];
			if(internalobjects[j].name == "2.Size")
				sizeobj = internalobjects[j];
			if(internalobjects[j].name == "3.Width")
				widthobj = internalobjects[j];
			// Check if this is the size. If so, make a note of it
			if(internalobjects[j] == obj)
			{
				found = 1;
			}
		}
		
		for(var j=0; j < hiddenobjects.length; j++)
		{
			if(hiddenobjects[j].name == "x")
			{
				stylename = hiddenobjects[j].value;
				skuname = hiddenobjects[j].value;
				
				stylename = stylename.replace("Tall", "");
				stylename = stylename.replace("Petite", "");
				stylename = stylename.substring(0, 10); // xxz
			}
		}
	
		if(found == 1)
			break;
	}
	
	if(found == 1)
	{
		// Now do the processing logic
		// Form is known, and all objects in the form are now defined
			// Get names
		if(!colorobj || !sizeobj)
			return;
			
		if(colorobj.value == "-1")
			sizeobj.disabled = true;
		else
			sizeobj.disabled =  false;
			
		if(colorobj)
		{
			colorname = colorobj.value.substring(0, 3);
			fullcolorname = colorobj.value;
		}
		if(sizeobj)
			sizename = sizeobj.value.substring(0, 3);
		if(widthobj)
			widthname = widthobj.value.substring(0, 3);
		
		// Reset the existing sizes
		for(var z=0; z < sizeobj.options.length; z++)
			sizeobj.options[z].text = sizeobj.options[z].value;
		
		// Loop through all size options for sold out status
		for(var z=0; z < sizeobj.options.length; z++)
		{
			if(isDelayed(stylename, skuname, colorname, sizeobj.options[z].value.substring(0, 3), widthname))
			{
				if(stylename.substring(0, 2) == "LN")
					sizeobj.options[z].text = sizeobj.options[z].value + " - Add 3 days to order delivery";
				else
					sizeobj.options[z].text = sizeobj.options[z].value + " - Add 3 days to order delivery";
			}
				
			if(isSoldOut(stylename, skuname, colorname, sizeobj.options[z].value.substring(0, 3), widthname))
			{
				sizeobj.options[z].text = sizeobj.options[z].value + " - SOLD OUT";
			}
			else // new 2009-11-08
			{
				allsoldout = 0;
			}
		}
		
		if(colorobj)
			if(allsoldout == 1)
				removeColor(fullcolorname, colorobj);
	}
}

function ahinit()
{
	var allinputelements = document.getElementsByName("x");
	var stylelist = "";
	for(var i=0; i < allinputelements.length; i++)
	{
		if(allinputelements[i].name == "x")
			stylelist += allinputelements[i].value.substring(0, 10) + "|";
	}
	
	var ahinvscript = document.createElement('script');
	ahinvscript.type = 'text/javascript';
	ahinvscript.src = 'http://manage.allheart.com/inventory/i.php?y=' + stylelist;
	document.getElementsByTagName('head')[0].appendChild(ahinvscript);
	attachCode();
}

function attachCode()
{
	if (!ahinventory.product)
	{
        setTimeout(attachCode, 100);
        return;
    }
	else
	{
	   ahscriptload();
    }	
}

function ahscriptload()
{
	var allformelements = document.getElementsByName("master-form");
	
	// Parse the color and size querystring parameters
    var parms = window.location.search.substring(1).split('&');
		
    var queryStringSize;
	var queryStringColor;
  
    if(parms != '')
	{
		for (var z1 = 0; z1 < parms.length; z1++) 
		{
			pos = parms[z1].indexOf('=');
			if (pos > 0 && parms[z1].substring(0,pos) == 's') 
			{
				  queryStringSize = parms[z1].substring(pos+1);
			}
			if (pos > 0 && parms[z1].substring(0,pos) == 'c') 
			{
				  queryStringColor = parms[z1].substring(pos+1);
			}
		}
	}
	
	for(var i=0; i < allformelements.length; i++)
	{
		var currentform = allformelements[i];
		currentform.onsubmit = function() { return validateInventory(this); };
		
		internalobjects = currentform.getElementsByTagName("select");
		var sizeobj = null;
		var colorobj = null;
		
		for(var j=0; j < internalobjects.length; j++)
		{
			if(internalobjects[j].name == "1.Color" || internalobjects[j].name == "1.Print" || internalobjects[j].name == "1.Team")
			{	// Add handlers for ddrops
				internalobjects[j].onchange = function() { inventoryCheck(this); };
				colorobj = internalobjects[j];
			}
			if(internalobjects[j].name == "2.Size")
			{
				sizeobj = internalobjects[j];
			}
			if(internalobjects[j].name == "3.Width") // Add handlers for ddrops
				internalobjects[j].onchange = function() { inventoryCheck(this); };
		}
		
		if(colorobj && sizeobj)
		{
			for(var xx = 0; xx < colorobj.options.length; xx++)
			{
				colorobj.selectedIndex = xx;
				//alert(colorobj.options[colorobj.selectedIndex].value);
				inventoryCheck(colorobj);
			}
			
			sizeobj.disabled = true;
			var newElem = document.createElement("option");
			newElem.text = "Choose a color...";
			newElem.value = "-1";
			colorobj.options.add(newElem, 0);
			colorobj.selectedIndex = 0;
		}
		
		if(sizeobj && queryStringSize)
		{ 
	    	var menu = sizeobj.options;
			for(var h=0; h < menu.length; h++)
				if(menu[h].value.substring(0, 3) == unescape(queryStringSize).substring(0, 3))
					menu.selectedIndex = h;
		}
		
	    if(colorobj && queryStringColor)
		{ 
	    	var menu = colorobj.options;
			for(var h1=0; h1 < menu.length; h1++)
				if(menu[h1].value.substring(0, 3) == unescape(queryStringColor).substring(0, 3))
				{
					menu.selectedIndex = h1;
					if(sizeobj)
						sizeobj.disabled = false;
				}
		}
	}
}