// Get years
function getdiameter_CL(m) {
	$('thickness').disabled = true;
	$('diameter').disabled = false;
}


function getfeet_CL(m) {
	$('feet').disabled = false;
	$('diameter').disabled = true;
}

function get_available() {
	var query = window.location.search.substring(1);
	 var vars = query.split("&");
	 for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    return pair[1];
	}
}

function clear_options() {
	
	 var od_dropdown = document.forms[1].od;
	 var minor_dropdown = document.forms[1].minor;
	 //var major_dropdown = document.forms[1].major;
	
	od_dropdown.options[0] = new Option("Select an Original Diameter", "");
	minor_dropdown.options[0] = new Option("Select a Minor Outside Dimension", "");
	//major_dropdown.options[0] = new Option("Select a Major Outside Dimension", "");
	
}

function getshape(m) {
	var thickness = m.options[m.selectedIndex].value;
	$('shape').disabled = false;
	$('thickness').disabled = true;
}

// Get original diameters
function getods_CL(m) {
	//var containerDiv = $("cars");
	//containerDiv.innerHTML = 'Filtering car list by year...';
	
	clear_options();
	
	//var inventory = get_inventory();
  	var shape = m.options[m.selectedIndex].value;			
  	
  	var shape_dropdown = $('shape');
  	shape_dropdown.disabled = true;
  
  	//enable the od dropdown
  	var od = $('od');
  	od.disabled = false;
      
    buildList_CL('shape=' + shape);
	 //containerDiv.innerHTML = '<h2>'+year+'</h2>';
	//showCars('inventory='+inventory+'&year='+year);
	
	
}

function getminors_CL(m) {
	var od = m.options[m.selectedIndex].value;
	var shape = $('shape').value;
	$('od').disabled = true;
	var minor = $('minor');
	minor.disabled = false;
	buildList_CL('shape='+shape+'&od='+od)
}

function getmajors_CL(m) {
	var minor = m.options[m.selectedIndex].value;
	var shape = $('shape').value;
	var minor = $('minor').value;
	$('minor').disabled = true;
	$('feet').disabled = false;
	/*var major = $('major');
	major.disabled = false;*/
	buildList_CL('shape='+shape+'&od='+od+'&minor='+minor);
}

function calculatePrice() {
	var containerDiv = $('sized_pricing');
	containerDiv.innerHTML = '';
	
	var feet = $('feet').value;
	var shape = $('shape').value;
	
	if (shape == 'Oval') {
		var price = 1.50;
	}
	
	if (shape == 'Rectangle') {
		var price = 2.00;
	}
	
	if (shape == 'Square') {
		var price = 2.00;
	}
	
	total_price = price * feet;
	
	alert(total_price);
	
	total_price = total_price.toFixed(2);
	
	alert(total_price);
	
	var minor = $('minor').value;
	
	var html = '<p><strong>'+shape+ ' Liner</strong><br />';
	html+= '<strong>Number of Feet: </strong> ' + feet + '</br>';
	html+= '<strong>New Dimensions in Inches:</strong> ' + minor + '</p>';
	html+= '<p><strong>Price:</strong> $' + total_price + '</p>';
	
	containerDiv.innerHTML = html;
	
}

// Ajax Request
function buildList_CL(p) {
    
    // Parameters for request
    // p = parameters
    var loc = new String(window.parent.document.location);
	if (loc.indexOf("https://")!= -1)
	prefix = "https://";
	else
	prefix = "http://"; 
    
       
    var myAjax = new Ajax.Request(
        prefix+'www.olympiachimney.com/xml/cl_create_xml.php', 
        {method: 'post', parameters: p, onComplete: pushoptions_CL}
        );
    
}// end reqXML()

function pushoptions_CL(originalRequest) {
	
    var price_options = new Array();
    
    var xmlResponse = originalRequest.responseXML;

    var prices = xmlResponse.getElementsByTagName('prices');
    
    if (prices.length > 0) {
        
        
        var prices_array = '';
        var price = '';
        prices_array = xmlResponse.getElementsByTagName('price');
		price = prices_array[0].firstChild.data;
		feet = $('feet').value;
		
		
		total_price = price * feet;
		total_price = total_price.toFixed(2);
		
		
        $('price_per_foot').innerHTML = '<p><strong>Price per foot:</strong> $' + price + '<br /><strong>Estimated Total:</strong> $' + total_price + '</p>';
        
        
        $('price').value = total_price;
    
    } ///

	
}

function activateCalculate_CL(m) {
	var thickness;
	var diameter;
	var feet;
	$('diameter').disabled = false;
	diameter = $('diameter').value;	
	$('diameter').disabled = true;
	
	$('thickness').disabled = false;
	thickness = $('thickness').value;
	$('thickness').disabled = true;	
	
	feet = $('feet').value;

	buildList_CL('diameter='+diameter+'&feet='+feet);	
	
	$('calculate').disabled = false;
	$('calculate').focus();
}

function redirectTo(url) {
	window.location(url);
}

function resetForm_CL() {
	$('price_per_foot').innerHTML = '';
	$('thickness').disabled = false;
	$('diameter').disabled = true;
	$('feet').disabled = true;
}

function checkForm_CL() {
	
	var error = false;
	if ( $('thickness').value == "" || $('diameter').value == "" || $('feet').value == "" ) {
		error = true;
	}
	
	if (error == true) {
		alert("You are required to select a thickness, diameter, and feet.");
		return false;
	}
		
	$('thickness').disabled = false;
	$('diameter').disabled = false;
	$('feet').disabled = false;
	
	return true;	
}