// Get years
function getshapes_SF() {
    buildList('action=shape');
}

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[2].od;
	 var minor_dropdown = document.forms[2].minor;
	 //var major_dropdown = document.forms[0].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 getsmall(m) {
	var thickness = m.options[m.selectedIndex].value;
	$('small').disabled = false;
	$('thickness').disabled = true;
}

// Get original diamaters
function getlarge(m) {


	var small = m.options[m.selectedIndex].value;
   	$('small').disabled = true;
  
  	//enable the od dropdown
  	$('large').disabled = false;
 	
 	var thickness = $('thickness').value;
      
    buildList('thickness='+thickness+'&small='+small);
	 //containerDiv.innerHTML = '<h2>'+year+'</h2>';
	//showCars('inventory='+inventory+'&year='+year);
	
	
}

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

function getmajors_SF(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('shape='+shape+'&od='+od+'&minor='+minor);
}

function calculatePrice_SF() {
	var containerDiv = $('price_per_foot');
	containerDiv.innerHTML = '';
	
	var feet = $('feet').value;
	var size_array = $('large').value.split("|");
	var size = size_array[0];
	var price = size_array[1];
	price = parseFloat(price);
	price = price.toFixed(2);
	
	total_price = price * feet;
	total_price = total_price.toFixed(2);	
	
	var html = '<p><strong>Number of Feet: </strong> ' + feet + '<br />';
	html+= '<strong>New Dimensions in Inches:</strong> ' + $('small').value + ' x ' + size + '<br /><br />';
	html+= '<strong>Price:</strong> $' + total_price + '</p>';
	
	containerDiv.innerHTML = html;
	
}

// Ajax Request
function buildList(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/sf_create_xml.php', 
        {method: 'post', parameters: p, onComplete: pushoptions_SF}
        );
    
}// end reqXML()

function pushoptions_SF(originalRequest) {
	
    var size_options = new Array(); // size options
    var size_vals = new Array();
    var xmlResponse = originalRequest.responseXML;
   	var sizes = xmlResponse.getElementsByTagName('sizes');
    
    if (sizes.length > 0) {
    
		var sizes_array = '';
        sizes_array = xmlResponse.getElementsByTagName('size');
        var size_val_array = xmlResponse.getElementsByTagName('size_value');		
		size_options[0] = "Select Large Side";
		size_vals[0] = "";
				
        for (i=0;i<sizes_array.length;i++) {
            size_options[i+1] = sizes_array[i].firstChild.data;
        }
               
		for (b=0;b<size_val_array.length;b++) {
            size_vals[b+1] = size_val_array[b].firstChild.data;
        }
        
        var size_dropdown = document.forms[2].large;

    }
     
	if (size_options.length > 1) {
        for (i=0;i<size_options.length;i++) {
            size_dropdown.options[i] = new Option(size_options[i],size_vals[i]);
        }
    }	
}

function getfeet_SF() {
	$('large').disabled = true;
	$('feet').disabled = false;
}

function activateCalculate_SF() {
	$('calculate').disabled = false;
	calculatePrice_SF();
}

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

function resetForm_SF() {
	$('thickness').disabled = false;
	$('small').disabled = true;
	$('large').disabled = true;
	$('calculate').disabled = true;
	$('feet').disabled = true;
	$('price_per_foot').innerHTML = '';
	$('price').value = '';
}

function checkForm_SF() {

	if ( $('price').value == '') {
		alert('Please wait until your price is displayed.');
		activateCalculate_SF();
		return false;
	}
	
	var error = false;
	if ( $('thickness').value == "" || $('small').value == "" || $('large').value == "" || $('feet').value == "") {
		error = true;
	}
	
	if (error == true) {
		alert("You are required to input a type, shape, size, and feet.");
		return false;
	}
	
	$('thickness').disabled = false;
	$('small').disabled = false;
	$('large').disabled = false;
	$('feet').disabled = false;
	
	var feet = $('feet').value;
	var large = $('large').value;
	var small = $('small').value;
	
	var size_array = large.split("|");
	var size = size_array[0];
	var price = size_array[1];
		
	var total_price = price * feet;
	$('price').value = total_price;
	$('real_size').value = small + ' X ' + size;		
	
	return true;	
}