function moHide(id) {
	hide('o'+ id);
	//document.getElementById('lo' + id).style.display = 'inline';
}

function moShow(id) {
	show('o' + id);
	//document.getElementById('lo' + id).style.display = 'none';
}

function moReset() {
	rOptions();
	var options = document.getElementById('uOptions').getElementsByTagName('LI');
	for(var i = 0; i < options.length; i++) {
		if(options[i].id)
			hide(options[i].id.substring(1));
	}

	/*
	spans = document.getElementById('uOptions').getElementsByTagName('SPAN');
	for(i = 0; i < spans.length; i++)
		spans[i].style.display = 'inline';
	*/
}

function mo(id) {
	switch(document.getElementById('no' + id).className) {
		case 'active':
			moHide(id);
			break;
		case 'inactive':
			moReset();
			moShow(id);
			break;
	}
}

function moRadio(f) {
	radio(f);
	switch(f.name) {
		case 'SPIMoldClass':
			var steel = new Steel();
			steel.UpdateClass();
			get('CavityDetailInserted').checked = true;
			moRadio(get('CavityDetailInserted'));
			get('CoreDetailInserted').checked = true;
			moRadio(get('CoreDetailInserted'));
			get('CavityDetailCutFromSolid').disabled = false;
			get('CoreDetailCutFromSolid').disabled = false;
				
			switch(f.value) {
				case 'SPI 104':
					get('fPartingLineInterlocks').checked = false;
					get('CavityDetailCutFromSolid').checked = true;
					moRadio(get('CavityDetailCutFromSolid'));
					get('CoreDetailCutFromSolid').checked = true;
					moRadio(get('CoreDetailCutFromSolid'));
					break;
				case 'SPI 103':
					get('fPartingLineInterlocks').checked = false;
					get('GatingRequirementsSubGateIntoPart').checked = true;
					moRadio(get('GatingRequirementsSubGateIntoPart'));
					get('EngravingPartNumber').checked = false;
					get('EngravingRevision').checked = false;
					get('EngravingCavityID').checked = false;
					get('EngravingDatePlugs').checked = false;
					get('EngravingRecycleInsert').checked = false;
					break;
				case 'SPI 102':
					get('fPartingLineInterlocks').checked = true;
					get('CavityDetailCutFromSolid').disabled = true;
					get('CoreDetailCutFromSolid').disabled = true;
					break;
				case 'SPI 101':
					get('fPartingLineInterlocks').checked = true;
					get('CavityDetailCutFromSolid').disabled = true;
					get('CoreDetailCutFromSolid').disabled = true;
					break;
			}
			break;
		case 'MoldConstructionFrame':
			switch(f.value) {
				case 'MUD frame':
					get('MoldConstructionPlates2Plate').disabled = true;
					get('MoldConstructionPlates2PlateAX').disabled = true;
					get('MoldConstructionPlates2PlateBX').disabled = true;
					get('fMoldConstructionPlates').value = '';
					get('MoldConstructionTempColdRunner').checked = true;
					moRadio(get('MoldConstructionTempColdRunner'));
					get('MoldConstructionTempHotManifold').disabled = true;
					break;
				default:
					get('MoldConstructionPlates2Plate').disabled = false;
					get('MoldConstructionPlates2PlateAX').disabled = false;
					get('MoldConstructionPlates2PlateBX').disabled = false;
					get('MoldConstructionPlates2Plate').checked = true;
					moRadio(get('MoldConstructionPlates2Plate'));
					get('MoldConstructionTempHotManifold').disabled = false;
			}
			break;
		case 'MoldConstructionTemp':
			switch(f.value) {
				case 'Hot Manifold':
					get('MoldConstructionHotManifoldBrand').style.display = 'inline';
					moSelect(get('MoldConstructionHotManifoldBrand'));
					get('GatingRequirementsPinPoint').checked = true;
					moRadio(get('GatingRequirementsPinPoint'));
					get('GatingRequirementsCenterSprue').disabled = true;
					get('fGatingHotSprue').checked = false;
					get('fGatingHotSprue').disabled = true;
					break;
				case 'Cold Runner':
					get('MoldConstructionHotManifoldBrand').style.display = 'none';
					get('fMoldConstructionHotManifoldBrand').value = '';
					get('GatingRequirementsSubGateIntoPart').checked = true;
					moRadio(get('GatingRequirementsSubGateIntoPart'));
					get('GatingRequirementsCenterSprue').disabled = false;
					get('fGatingHotSprue').disabled = false;
					break;
			}
			break;
		case 'CavityDetail':
			var detail = new Detail('Cavity');
		case 'CoreDetail':
			if(!detail) var detail = new Detail('Core');
			switch(f.value) {
				case 'Cut from Solid':
					detail.HideMaterial();
					detail.HideHRC();
					break;
				case 'Inserted':
					detail.ShowMaterial();
					detail.ShowHRC();
					break;
			}
			break;
		case 'GatingRequirements':
			switch(f.value) {
				case 'Valve Gate':
				case 'Pin Point':
				case 'Center Sprue':
					get('fGatingRunnerShutoff').checked = false;
					get('fGatingRunnerShutoff').disabled = true;
					break;
				default:
					get('fGatingRunnerShutoff').disabled = false;
			}
			break;
	}
	rOptions();
}

function moSelect(f) {
	switch(f.name) {
		case 'MoldConstructionHotManifoldBrand':
			get('fMoldConstructionHotManifoldBrand').value = f.options[f.selectedIndex].text;
			break;
		case 'CavityMaterial':
			var detail = new Detail('Cavity');
			detail.UpdateMaterial();
			break;
		case 'CoreMaterial':
			var detail = new Detail('Core');
			detail.UpdateMaterial();
			break;
		case 'LifterMaterial':
			var detail = new Detail('Lifter');
			detail.UpdateMaterial();
			break;
		case 'CavityHRC':
			get('fCavityHRC').value = f.options[f.selectedIndex].text;
			break;
		case 'CoreHRC':
			get('fCoreHRC').value = f.options[f.selectedIndex].text;
			break;
		case 'LifterHRC':
			get('fLifterHRC').value = f.options[f.selectedIndex].text;
			break;
	}
	rOptions();
}

function moCheck(f) {
	switch(f.name) {
		case 'fEjectionPins':
		case 'fEjectionBlade':
		case 'fEjectionSleeve':
		case 'fEjectionStripper':
			if(
				!get('fEjectionPins').checked &&
				!get('fEjectionBlade').checked &&
				!get('fEjectionSleeve').checked &&
				!get('fEjectionStripper').checked
			) {
				alert('You must select at least one form of ejection.');
				get('fEjectionPins').checked = true;
			}
			break;
		case 'fPartingLineInterlocks':
			if(get('fSPIMoldClass').value == 'SPI 101') {
				alert('SPI 101 class molds require Parting Line Interlocks.');
				get('fPartingLineInterlocks').checked = true;
			}
	}
	rOptions();
}

function rOptions() {
	var lER = l('loEjectionRequirements');
	var lERguided = document.mainform.fEjectionRequirementsGuided;
	var lERspring = document.mainform.fEjectionRequirementsSpring;
	var lEnR = l('loEngravingRequirements');
	var lEnRpn = document.mainform.fEngravingPartNumber;
	var lEnRr = document.mainform.fEngravingRevision;
	var lEnRcid = document.mainform.fEngravingCavityID;
	var lEnRdp = document.mainform.fEngravingDatePlugs;
	var lEnRri = document.mainform.fEngravingRecycleInsert;
	var lOO = l('loOtherOptions');
	var lOOpli = document.mainform.fPartingLineInterlocks;
	var lOOcc = document.mainform.fCycleCounter;
	
	l('loSPIMoldClass').nodeValue = ': ' + value('fSPIMoldClass');
	l('loMoldConstruction').nodeValue = ': ' + value('fMoldConstructionFrame');
	if(value('fMoldConstructionPlates') != '')
		l('loMoldConstruction').nodeValue += ', ' + value('fMoldConstructionPlates');
	l('loMoldConstruction').nodeValue += ', ' + value('fMoldConstructionTemp');
	if(value('fMoldConstructionHotManifoldBrand') != '')
		l('loMoldConstruction').nodeValue += ' (' + value('fMoldConstructionHotManifoldBrand') + ')';
	l('loCavityDetail').nodeValue = ': ' + value('fCavityDetail');
	if(value('fCavityMaterial') != '')
		l('loCavityDetail').nodeValue += ', ' + value('fCavityMaterial');
	if(value('fCavityHRC') != '')
		l('loCavityDetail').nodeValue += ', ' + value('fCavityHRC');
	if($S('fCavityTexture').text != 'None')
		l('loCavityDetail').nodeValue += ', ' + $S('fCavityTexture').text;
	if($S('fCavityPolish').text != 'None')
		l('loCavityDetail').nodeValue += ', ' + $S('fCavityPolish').text;
	l('loCoreDetail').nodeValue = ': ' + value('fCoreDetail');
	if(value('fCoreMaterial') != '')
		l('loCoreDetail').nodeValue += ', ' + value('fCoreMaterial');
	if(value('fCoreHRC') != '')
		l('loCoreDetail').nodeValue += ', ' + value('fCoreHRC');
	if($S('fCoreTexture').text != 'None')
		l('loCoreDetail').nodeValue += ', ' + $S('fCoreTexture').text;
	if($S('fCorePolish').text != 'None')
		l('loCoreDetail').nodeValue += ', ' + $S('fCorePolish').text;
	l('loLifterDetail').nodeValue = ': ' + value('fLifterMaterial');
	if(value('fLifterHRC') != '')
		l('loLifterDetail').nodeValue += ', ' + value('fLifterHRC');
	l('loGatingRequirements').nodeValue = ': ' + value('fGatingRequirements');
	if(get('fGatingRunnerShutoff').checked)
		l('loGatingRequirements').nodeValue += ' + ' + value('fGatingRunnerShutoff');
	if(get('fGatingHotSprue').checked)
		l('loGatingRequirements').nodeValue += ' + ' + value('fGatingHotSprue');
	
	var lERoptions = new Array('Pins', 'Guided', 'Spring', 'Blade', 'Sleeve', 'Stripper');
	lER.nodeValue = ': ' + rOptionsList('fEjection', lERoptions, ', ', true);
	lEnR.nodeValue = ': ';
	if(lEnRpn.checked)
		lEnR.nodeValue += lEnRpn.value + ' ';
	if(lEnRr.checked)
		lEnR.nodeValue += lEnRr.value + ' ';
	if(lEnRcid.checked)
		lEnR.nodeValue += lEnRcid.value + ' ';
	if(lEnRdp.checked)
		lEnR.nodeValue += lEnRdp.value + ' ';
	if(lEnRri.checked)
		lEnR.nodeValue += lEnRri.value;
	lOO.nodeValue = ': ';
	if(lOOpli.checked)
		lOO.nodeValue += lOOpli.value + ' ';
	if(lOOcc.checked)
		lOO.nodeValue += lOOcc.value;
	
}

function rOptionsList(prefix, list, sep, sepnofirst) {
	if(!sep) sep = ', ';
	var ret = ' ';
	for(var i in list) {
		if(get(prefix + list[i]).checked)
			ret += sep + get(prefix + list[i]).value;
	}
	if(sepnofirst)
		ret = ret.substring(sep.length);
	return ret;
}


//
//
// CLASSES
//

function Detail(name) {
	
	this.Name = name;
	this.Steel = new Steel();
	this.loMaterial = document.getElementById('lo' + name + 'Material');
	this.fMaterial = document.getElementById('f' + name + 'Material');
	this.sMaterial = document.getElementById(name + 'Material');
	this.loHRC = document.getElementById('lo' + name + 'HRC');
	this.fHRC = document.getElementById('f' + name + 'HRC');
	this.sHRC = document.getElementById(name + 'HRC');
	
	this.ShowMaterial = function() {
		this.loMaterial.style.display = 'inline';
		this.sMaterial.style.display = 'inline';
		moSelect(this.sMaterial);
	}
	
	this.HideMaterial = function() {
		this.loMaterial.style.display = 'none';
		this.sMaterial.style.display = 'none';
		this.fMaterial.value = '';
		rOptions();
	}
	
	this.UpdateMaterial = function() {
		this.fMaterial.value = this.sMaterial.options[this.sMaterial.selectedIndex].text;
		this.Steel.UpdateHRC(this.Name);
	}
	
	this.ShowHRC = function() {
		this.loHRC.style.display = 'inline';
		this.sHRC.style.display = 'inline';
		moSelect(this.sHRC);
	}
	
	this.HideHRC = function() {
		this.loHRC.style.display = 'none';
		this.sHRC.style.display = 'none';
		this.fHRC.value = '';
		rOptions();
	}
	
	this.UpdateHRC = function() {
		this.fHRC.value = this.sHRC.options[this.sHRC.selectedIndex].text;
	}
	
}

function Steel() {

	this.Class = document.getElementById('fSPIMoldClass');
	
	this.SPI103 = new Array(
		'P20',
		'P20 LQ',
		'H13',
		'S7',
		'420 SS',
		'NAK 80',
		'NAK 55',
		'PX5',
		'AMPCOLOY 940',
		'MOLDMAX XL'
	);
	
	this.SPI102 = new Array(
		'H13',
		'S7',
		'420 SS',
		'NAK 80',
		'NAK 55',
		'AMPCOLOY 940'
	);
	
	this.SPI101 = new Array(
		'H13',
		'S7',
		'420 SS'
	);
	
	this.HRCP20 = new Array(
		'HRC 30-35',
		'HRC 34-38'
	);
	
	this.HRCH13102 = new Array(
		'HRC 40-45',
		'HRC 45-48',
		'HRC 48-52',
		'HRC 52-56'
	);
	
	this.HRCH13101 = new Array(
		'HRC 48-52',
		'HRC 52-56'
	);
	
	this.GetClass = function() {
		switch(this.Class.value) {
			case 'SPI 101':
				var options = this.SPI101;
				break;
			case 'SPI 102':
				var options = this.SPI102;
				break;
			default:
				var options = this.SPI103;
		}
		return options;
	}
	
	this.Optionize = function(select, options) {
		select.options.length = 0;
		for(var i = 0; i< options.length; i++)
			select.options[i] = new Option(options[i]);
	}
	
	this.UpdateClass = function() {
		var options = this.GetClass();
		
		//var Material = this.Optionize(this.GetClass());
		
		var cavity = new Detail('Cavity');
		this.Optionize(cavity.sMaterial, this.GetClass());
		moSelect(cavity.sMaterial);
		this.UpdateHRC('Cavity');
		
		var core = new Detail('Core');
		this.Optionize(core.sMaterial, this.GetClass());
		moSelect(core.sMaterial);
		this.UpdateHRC('Core');
		
		var lifter = new Detail('Lifter');
		this.Optionize(lifter.sMaterial, this.GetClass());
		moSelect(lifter.sMaterial);
		this.UpdateHRC('Lifter');
	}
	
	this.UpdateHRC = function(section) {
		var Material = document.getElementById('f' + section + 'Material');
		var select = document.getElementById(section + 'HRC');
		var options = false;
		
		switch(Material.value) {
			case 'P20':
				options = this.HRCP20;
				break;
			case 'H13':
			case 'S7':
				switch(this.Class.value) {
					case 'SPI 101':
						options = this.HRCH13101;
						break;
					default:
						options = this.HRCH13102;
				}
				break;
			default:
				options = false;
		}
	
		var detail = new Detail(section);
	
		if(!options) {
			detail.HideHRC();
		} else {
			this.Optionize(select, options);
			detail.ShowHRC();
		}

	}
	
}