// JavaScript Document
$(document).ready(function(){
				   
	xmlProductTwo_clickables();
	xmlProductTwo_loadUI();

});
function xmlProductTwo_loadUI(){
		// Make the Initial Choices Red to Signify that a response is needed.
		$('select option:selected').each(function(){if($(this).val() == 0){$(this).css('color','#CF0275')}});
		// Move stockhint up inline with input box.		
		//$('.StockHint').css({'display':'inline-block','margin-left':'3px'}).parent().css('display','inline-block');

		$('.cdSelect').first().closest('table').after("To order a separate top or bottom, simply enter a zero for the Quantity of the item you don't wish to order.");
	}
function xmlProductTwo_clickables(){

		// Main action function. Abstracts the input boxes and uses element relations to find the desired elements.
		$('.cdSelect').change(function(event){
			var a = $(this);
			var b;
			var c = "";
			var inv = "";
			var colorOpt = "";
			var sizeOpt = a.children(':selected').first().html(); //Gets the user selection
			var sizeVal = a.children(':selected').first().val();
			var qty = 0;
			var hintClass = "";
			if(a.parent().attr('class') == 'cdSizeSelect'){	b = a.parent();}else{ b = a;} // Checks to see what type of element it is
			qty = b.siblings('.cdQty').first().val();				// Gets the Qty from the input box			
			b.siblings('span.cdInventoryTable').first().children().each(function(){
				if($(this).hasClass('val_'+colorOpt+'_'+sizeOpt)){inv = $(this).html();} // Gets the inventory value for specified size
			});
			
			// The following condition sets the message and color of the Inventory Hint.
			if(qty == ''){b.siblings('.cdQty').first().val('0'); qty = 0;}
			if(qty == 0){
				$(this).hide().val(1); sizeVal = 1;
				$(this).parent().siblings('#cdPadding').hide().val(1);
			} else {
				if($(this).is(':hidden')) $(this).show().val(0);
				if($(this).parent().siblings('#cdPadding').is(':hidden')) $(this).parent().siblings('#cdPadding').show().val(0);
			}

			if(sizeVal == 0 && qty > 0){ 
				c = "Choose "+document.getElementById(a.attr('id')).options[0].text+" Please!";
				hintClass = '#CF0275';
			}else if(qty > 0){
				if(Number(inv) == 0){
					c = "Sorry, "+sizeOpt+" Out of Stock";
					if(qty != 0){c+=' <span class="StockHintHelper">(Remove Qty)</span>'}
					//b.siblings('.cdQty').first().val(inv);
					hintClass = '#CF0275';
				}else{
					if(Number(inv) > 0){
						c = sizeOpt+" In Stock";hintClass = 'black';
						if(qty == 0){c+=' <span class="StockHintHelper">(Add Qty)</span>'}
						//if(qty == 0){b.siblings('.cdQty').first().val('0');}
					}
					if(Number(inv) < Number(qty)){
						//c = sizeOpt+" In Stock - "+inv+" is the maximum you may order at this time."; // Don't show customer the Inventory Value
						c = sizeOpt+" In Stock - Please reduce your order qty.";
						//b.siblings('.cdQty').first().val(inv); // Don't change to inventory value.
						hintClass='#CF0275';
					}
				}
			} else if(qty == 0) {
				c = '<span class="StockHintHelper">(Add Qty)</span>'
			}
			b.siblings('div').children('.StockHint').first().html(c).css('color',hintClass); // Sets the Text and the Color of the Inventory Hint
		})/*.blur(function(){$(this).trigger('change')})*/; // Calls this function on blur as well as on change.
		
		$('.cdQty').keyup(function(){
			if($(this).val() != '' && event.keyCode != 8){
				$(this).siblings('.cdSizeSelect').first().children('.cdSelect').trigger('change');
			}
		}).blur(function(){
			$(this).siblings('.cdSizeSelect').first().children('.cdSelect').trigger('change');
		}); 
			// Calls the dropdown change function on keyup and blur for the qty input box. 
		
		$('.StockHintHelper').live('click',function(){
				var qty = $(this).parent().parent().siblings('.cdQty').first().val(); var val = 0;
				if(qty > 0){val=0;}else{val+=1;}
				$(this).parent().parent().siblings('.cdQty').first().val(val).siblings('.cdSizeSelect').first().children('.cdSelect').trigger('change');
			});
	}
function TableOrderForm_Validator(theForm){                    
	return (true);
}
function cdValidatePForm(){
	var errString = "";
	var flagObjSelect = new Boolean(false);
	var sizeOneObj = document.getElementById('cdSizeOne');
	var sizeTwoObj = document.getElementById('cdSizeTwo');
	var colorOneObj = document.getElementById('cdColorOne');
	var colorTwoObj = document.getElementById('cdColorTwo');
	var paddingObj = document.getElementById('cdPadding');
	
	var sizeOneText = "";
	var colorOneText = "";
	var sizeTwoText = "";
	var colorTwoText = "";
	var qtyError = "The Quantity entered is larger than stock on hand for:\n";
	var flagQty = new Boolean(false);
				
	
	flagObjSelect = false;          
	
	if (paddingObj != null)
	{
	  if (paddingObj.selectedIndex == 0 && parseInt(document.getElementById('cdQtyOne').value))
	  {
		errString += "Please select a " + paddingObj.options[paddingObj.selectedIndex].text + ". \n";  
		flagObjSelect = true;
	  }
	}
	if (colorOneObj != null)
	{
	  colorOneText = colorOneObj.options[colorOneObj.selectedIndex].text;
	  if (colorOneObj.selectedIndex == 0 && parseInt(document.getElementById('cdQtyOne').value))
	  {
		errString += "Please select a " + colorOneObj.options[colorOneObj.selectedIndex].text + ". \n";  
		flagObjSelect = true;
	  }
	}
	if (sizeOneObj != null)
	{
	  sizeOneText = sizeOneObj.options[sizeOneObj.selectedIndex].text;
	  if (sizeOneObj.selectedIndex == 0 && parseInt(document.getElementById('cdQtyOne').value))
	  {
		errString += "Please select a " + sizeOneObj.options[sizeOneObj.selectedIndex].text + ". \n";
		flagObjSelect = true;
	  }
	} 
	if (colorTwoObj != null)
	{
	  colorTwoText = colorTwoObj.options[colorTwoObj.selectedIndex].text;
	  if (colorTwoObj.selectedIndex == 0 && parseInt(document.getElementById('cdQtyTwo').value))
	  { 
		errString += "Please select a " + colorTwoObj.options[colorTwoObj.selectedIndex].text + ". \n"; 
		flagObjSelect = true;
	  }
	}           
	if (sizeTwoObj != null)
	{
	  sizeTwoText = sizeTwoObj.options[sizeTwoObj.selectedIndex].text;
	  if (sizeTwoObj.selectedIndex == 0 && parseInt(document.getElementById('cdQtyTwo').value))
	  {
		errString += "Please select a " + sizeTwoObj.options[sizeTwoObj.selectedIndex].text + ". \n";
		flagObjSelect = true;
	  }
	}
	
	if (flagObjSelect)
	{ 
	  alert (errString);
	  return false;
	}
	else 
	{ 
	  if(parseInt(document.getElementById('cdQtyOne').value) > parseInt(document.getElementById('Inv_1_'+colorOneText+'_'+sizeOneText).innerHTML))
	  {
	  qtyError = qtyError +' Top '+colorOneText+' '+sizeOneText+'\n';
	  flagQty = true;
	  } 
	  if(parseInt(document.getElementById('cdQtyTwo').value) > parseInt(document.getElementById('Inv_2_'+colorTwoText+'_'+sizeTwoText).innerHTML))
	  {
	  qtyError = qtyError +' Bot '+colorTwoText+' '+sizeTwoText+'\n';
	  flagQty = true;
	  }
	  
	  if (flagQty == true)
	  { 
		alert (qtyError);
		return false;
	  }
	}
	return true;  
	
}
