
function getElement(id)
{
	if(document.getElementById)
	{
        return document.getElementById(id);
    }
    else if(document.all)
    {
        return document.all[id];
    }
    else if(document.layers)
    {
        return document.layers[id];
    }
    else
    {
        return null;
    }
}


var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
} 

function calculatePrice()
{
	
	var product_group_id = getElement('hid_product_group_id').value;
	var thread_size = '';
	var material = '';
	var head_type = '';
	var body_length = '';
	var flute = '';
	var chamfer = '';
	var member_set = '';
	var go_nogo = '';
	var quantity = getElement('txt_quantity').value;
	
	var run_request = true;
	
	if(getElement('sel_thread_size'))
	{
		thread_size = getElement('sel_thread_size').value;
		if(thread_size == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_material'))
	{
		material = getElement('sel_material').value;
		if(material == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_head_type'))
	{
		head_type = getElement('sel_head_type').value;
		if(head_type == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_body_length'))
	{
		body_length = getElement('sel_body_length').value;
		if(body_length == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_flute'))
	{
		flute = getElement('sel_flute').value;
		if(flute == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_chamfer'))
	{
		chamfer = getElement('sel_chamfer').value;
		if(chamfer == '')
		{
			run_request = false;
		}
	}
	if(getElement('sel_type'))
	{
		member_set = getElement('sel_type').value;
		if(member_set == '')
		{
			run_request = false;
		}
		if(member_set == 3 || member_set == 4)
		{
			getElement('sel_go').selectedIndex = 0;
			getElement('sel_go').disabled=true;
		}
		else
		{
			getElement('sel_go').disabled=false;
		}
	}
	if(getElement('sel_go'))
	{
		if(member_set == 3 || member_set == 4)
		{
			go_nogo = '0';
		}
		else
		{
			go_nogo = getElement('sel_go').value;
		}
	}
	if(quantity == '')
	{
		run_request = false;
	}
	
	if(run_request)
	{
		createXMLHttpRequest();    
		var url = "/index/calculate-price";
		var queryString = "product_group_id=" + product_group_id;
		queryString += "&thread_size=" + thread_size;
		queryString += "&material=" + material;
		queryString += "&head_type=" + head_type;
		queryString += "&body_length=" + body_length;
		queryString += "&flute=" + flute;
		queryString += "&chamfer=" + chamfer;
		queryString += "&member_set=" + member_set;
		queryString += "&go_nogo=" + go_nogo;
		queryString += "&quantity=" + quantity;
		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange = setProductPrice;
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
		xmlHttp.send(queryString);
	}
	
}

function setProductPrice()
{
	if(xmlHttp.readyState == 4)
	{
	  	if(xmlHttp.status == 200)
	  	{
	  		var price = 'Price: $0.00';
	  		if(xmlHttp.responseText != "")
	  		{
	  			if(xmlHttp.responseText == "gross error")
	  			{
	  				getElement('price-calc-error').innerHTML = "Quantities under 1000 must be purchased in gross (144). Please enter a multiple of 144.";
	  				getElement('price-calc-error').style.display = 'block';
	  				getElement('add-to-cart-button').disabled = true;
	  			}
	  			else if(xmlHttp.responseText == "stock error")
	  			{
	  				getElement('price-calc-error').innerHTML = "This item is currently out of stock, please call 508-764-4394 for ship date.";
	  				getElement('price-calc-error').style.display = 'block';
	  				getElement('add-to-cart-button').disabled = true;
	  			}
	  			else 
	  			{
		  			price = 'Price: $' + xmlHttp.responseText;
		  			getElement('price-calc-error').style.display = 'none';
		  			getElement('add-to-cart-button').disabled = false;
	  			}
		    } 
		    getElement('product-price').innerHTML = price;
		    retrieveProductSpecs();
    	}
  	}
}

function retrieveProductSpecs()
{
	
	var product_group_id = getElement('hid_product_group_id').value;
	var thread_size = '';
	var material = '';
	var head_type = '';
	var body_length = '';
	var flute = '';
	var chamfer = '';
	var member_set = '';
	var go_nogo = '';
	var quantity = getElement('txt_quantity').value;
	
	if(getElement('sel_thread_size'))
	{
		thread_size = getElement('sel_thread_size').value;
	}
	if(getElement('sel_material'))
	{
		material = getElement('sel_material').value;
	}
	if(getElement('sel_head_type'))
	{
		head_type = getElement('sel_head_type').value;
	}
	if(getElement('sel_body_length'))
	{
		body_length = getElement('sel_body_length').value;
	}
	if(getElement('sel_flute'))
	{
		flute = getElement('sel_flute').value;
	}
	if(getElement('sel_chamfer'))
	{
		chamfer = getElement('sel_chamfer').value;
	}
	if(getElement('sel_type'))
	{
		member_set = getElement('sel_type').value;
	}
	if(getElement('sel_go'))
	{
		if(member_set == 3 || member_set == 4)
		{
			go_nogo = '0';
		}
		else
		{
			go_nogo = getElement('sel_go').value;
		}
	}
		
	createXMLHttpRequest();    
	var url = "/index/product-specs";
	var queryString = "product_group_id=" + product_group_id;
	queryString += "&thread_size=" + thread_size;
	queryString += "&material=" + material;
	queryString += "&head_type=" + head_type;
	queryString += "&body_length=" + body_length;
	queryString += "&flute=" + flute;
	queryString += "&chamfer=" + chamfer;
	queryString += "&member_set=" + member_set;
	queryString += "&go_nogo=" + go_nogo;
	queryString += "&quantity=" + quantity;
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = setProductSpecs;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	xmlHttp.send(queryString);

}

function setProductSpecs()
{
	if(xmlHttp.readyState == 4)
	{
	  	if(xmlHttp.status == 200)
	  	{
	  		if(xmlHttp.responseText != "")
	  		{
		  		getElement('product-information').innerHTML = xmlHttp.responseText;
		    } 
		    calculatePrice();
    	}
  	}
}

function submitProductChoice(pProductGroupId,pThreadSize,pBodyLength,pHeadType,pFlutes,pChamfers)
{
	
	var data = '';
	var frm_action = '';
	data = pThreadSize + "|" + pBodyLength + "|" + pHeadType + "|" + pFlutes + "|" + pChamfers;
	
	getElement('hid_data').value = data;

	frm_action = getElement('frm_product').action;
	
	frm_action = frm_action + "id/" + pProductGroupId;
	
	getElement('frm_product').action = frm_action;

	getElement('frm_product').submit();
	
}

function setCartShippingRate(pValue)
{
	window.open('/cart/cart/step/shipping/rate/' + pValue, '_top');
}

function changeSearchResults(val)
{
	getElement('hid_limit_start').value = val;
	getElement('frm_search').submit();
}

function cascadeAttributesUpdate()
{
	var product_group_id = getElement('hid_product_group_id').value;
	var thread_size = '';
	var quantity = getElement('txt_quantity').value;
	
	if(getElement('sel_thread_size'))
	{
		thread_size = getElement('sel_thread_size').value;
	}
		
	createXMLHttpRequest();    
	var url = "/index/update-attributes";
	var queryString = "product_group_id=" + product_group_id;
	queryString += "&thread_size=" + thread_size;
	queryString += "&quantity=" + quantity;
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = setAttributes;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	xmlHttp.send(queryString);
}

function setAttributes()
{
	if(xmlHttp.readyState == 4)
	{
	  	if(xmlHttp.status == 200)
	  	{
	  		if(xmlHttp.responseText != "")
	  		{
	  			var data = xmlHttp.responseText;
	  			var thread_size = '';
				var material = '';
				var head_type = '';
				var body_length = '';
				var flute = '';
				var chamfer = '';
				var temp = '';
				var product_id = '';
				var start;

				data = data.split("|");
				
				if(getElement('sel_material'))
				{
					material = getElement('sel_material').value;
					material_options = getElement('sel_material');
					
					while (material_options.options.length > 0) {
						material_options.options[(material_options.options.length - 1)] = null;
					}
					
					temp = data[0].split(",");
					start=0;
					if(temp.length > 1)
					{
						start = 1;
						eval("material_options.options[0]=" + "new Option('--','')");
					}
					for (var i=0; i < temp.length; i++) {
						eval("material_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
					}
					
					getElement('sel_material').value = material;
				}
				if(getElement('sel_head_type'))
				{
					head_type = getElement('sel_head_type').value;
					head_type_options = getElement('sel_head_type');
					
					while (head_type_options.options.length > 0) {
						head_type_options.options[(head_type_options.options.length - 1)] = null;
					}
					
					temp = data[1].split(",");
					start=0;
					if(temp.length > 1)
					{
						start = 1;
						eval("head_type_options.options[0]=" + "new Option('--','')");
					}
					for (var i=0; i < temp.length; i++) {
						eval("head_type_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
					}
					
					getElement('sel_head_type').value = head_type;
				}
				if(getElement('sel_body_length'))
				{
					body_length = getElement('sel_body_length').value;
					body_length_options = getElement('sel_body_length');
					
					while (body_length_options.options.length > 0) {
						body_length_options.options[(body_length_options.options.length - 1)] = null;
					}
					
					temp = data[2].split(",");
					start=0;
					if(temp.length > 1)
					{
						start = 1;
						eval("body_length_options.options[0]=" + "new Option('--','')");
					}
					for (var i=0; i < temp.length; i++) {
						eval("body_length_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
					}
					
					getElement('sel_body_length').value = body_length;
				}
				if(getElement('sel_flute'))
				{
					flute = getElement('sel_flute').value;
					flute_options = getElement('sel_flute');
					
					while (flute_options.options.length > 0) {
						flute_options.options[(flute_options.options.length - 1)] = null;
					}
					
					temp = data[3].split(",");
					start=0;
					if(temp.length > 1)
					{
						start = 1;
						eval("flute_options.options[0]=" + "new Option('--','')");
					}
					for (var i=0; i < temp.length; i++) {
						eval("flute_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
					}
					
					getElement('sel_flute').value = flute;
				}
				if(getElement('sel_chamfer'))
				{
					chamfer = getElement('sel_chamfer').value;
					chamfer_options = getElement('sel_chamfer');
					
					while (chamfer_options.options.length > 0) {
						chamfer_options.options[(chamfer_options.options.length - 1)] = null;
					}
					
					temp = data[4].split(",");
					start=0;
					if(temp.length > 1)
					{
						start = 1;
						eval("chamfer_options.options[0]=" + "new Option('--','')");
					}
					for (var i=0; i < temp.length; i++) {
						eval("chamfer_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
					}
					
					getElement('sel_chamfer').value = chamfer;
				}
				retrieveProductSpecs();
		    } 
    	}
  	}
}

function toggleCheckoutPassword()
{
	if(getElement('chk_save_info').checked)
	{
		getElement('checkout_password').style.display = "block";
	}
	else
	{
		getElement('checkout_password').style.display = "none";
	}
}

function viewAllExact(radioObj)
{
	radioObj[0].checked = true;
	getElement('frm_search').submit();
}

function printProductSpecs()
{
	var productId = '';
//	var props = 'width=350,height=600,top=0,left=0,scrollbars=1';	
	
	if(getElement('hid_product_id'))
	{
		productId = getElement('hid_product_id').value;
	}
	
	if(productId != '')
	{
//		window.open('/index/product-specs-print/id/' + productId,'_blank',props);		
		window.open('/index/product-specs-print/id/' + productId,'_blank');		
	}
}

function getSearchAttributes()
{
	var category_id = getElement('sel_category_id').value;
	var material = getElement('sel_material').value;
	var thread_size = getElement('sel_thread_size').value;
	var head_type = getElement('sel_head_type').value;
	
	if(getElement('sel_category_id').value != 'nosearch')
	{
		createXMLHttpRequest();    
		var url = "/index/get-search-attributes";
		var queryString = "category_id=" + category_id;
		queryString += "&material=" + material;
		queryString += "&thread_size=" + thread_size;
		queryString += "&head_type=" + head_type;
		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange = setSearchAttributes;
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
		xmlHttp.send(queryString);
	}
}

function setSearchAttributes()
{
	if(xmlHttp.readyState == 4)
	{
	  	if(xmlHttp.status == 200)
	  	{
	  		if(xmlHttp.responseText != "")
	  		{
	  			var data = xmlHttp.responseText;
	  			var thread_size = '';
				var start;

				data = data.split("|");
				
				//material
				material = getElement('sel_material').value;
				material_options = getElement('sel_material');
				
				while (material_options.options.length > 0) 
				{
					material_options.options[(material_options.options.length - 1)] = null;
				}
				
				temp = data[0].split(",");
				start=0;
				if(temp.length > 1)
				{
					start = 1;
					eval("material_options.options[0]=" + "new Option('--','')");
				}
				for (var i=0; i < temp.length; i++) {
					eval("material_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
				}
				
				getElement('sel_material').value = material;
				
				//thread size				
				thread_size = getElement('sel_thread_size').value;
				thread_options = getElement('sel_thread_size');
				
				while (thread_options.options.length > 0) 
				{
					thread_options.options[(thread_options.options.length - 1)] = null;
				}
				
				temp = data[1].split(",");
				start=0;
				if(temp.length > 1)
				{
					start = 1;
					eval("thread_options.options[0]=" + "new Option('--','')");
				}
				for (var i=0; i < temp.length; i++) {
					eval("thread_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
				}
				
				getElement('sel_thread_size').value = thread_size;
				
				//head type
				head_type = getElement('sel_head_type').value;
				head_type_options = getElement('sel_head_type');
				
				while (head_type_options.options.length > 0) 
				{
					head_type_options.options[(head_type_options.options.length - 1)] = null;
				}
				
				temp = data[2].split(",");
				start=0;
				if(temp.length > 1)
				{
					start = 1;
					eval("head_type_options.options[0]=" + "new Option('--','')");
				}
				for (var i=0; i < temp.length; i++) {
					eval("head_type_options.options[i+start]=" + "new Option('" + temp[i] + "','" + temp[i] + "')");
				}
				
				getElement('sel_head_type').value = head_type;
				
		    } 
    	}
  	}
}

function checkSearchCategory()
{
	if(getElement('sel_category_id').value != 'nosearch')
	{
		return true;
	}
	else
	{
		alert('Please select a sub category to return accurate search results');
		return false;
	}
}