<?php
	//If user is not signed in redirect
	if(!$user->signed) redirect("?page=login");	
	
		$fin_to_cp = array(
					'1' => 'Copper',
					'2' => 'Copper',
					'3' => 'Copper',
					'4' => 'Aluminium',
					'5' => 'Aluminium'
					);

    $therminol_d12_access_email_domains = ['mattreeves@gmail.com','@boeing.com', '@wlv.com', '@wieland-microcool.com', '@microcooling.com'];

    function hasAccess($arr,$keyword) {
        foreach ($arr as $index => $string) {
            if (strpos($keyword, $string) !== FALSE)
                return true;
        }
        return false;
    }

?>
<script>
$(document).ready(function() {
	
	//form validation rules
	//add a custom method to check for zeros
	$.validator.addMethod("notEqual", function(value, element, param) {
  		return this.optional(element) || value != param;
	  }, "Please enter a number >0");
	// we need to have the validation plugin still validate hidden fields which we dynamically set with display:none propert
	 $.validator.setDefaults({
        ignore: []
    });
    
	
	var validator = $( "#mat" ).validate({
  		rules: {
			material_cold_plate: {
     	 		required: true
    		},	
			material_tim: {
     	 		required: true
    		},	
			material_tim_conductivity: {
     	 		required: true,
				number: true,
				range: [0.1, 450]
    		},	
			material_heater: {
     	 		required: true
    		},
			material_fluid: {
     	 		required: true
    		},	
			mixture_ratio: {
     	 		required: true,
				range: [0, 100]
    		}		
			
		},
		messages: {
			//we will turn some messages off (the hidden fields)
			material_heater: ""
		}
	});	
	
	// read only TIM if TIM thickness is zero
	
	
	<?php if ( @$_SESSION['sim_data']['geom']['tim_thickness'] == 0 ) {?>
		
		//$('[name=material_tim] option').attr('disabled','disabled');
		//$('[name=material_tim] option[value="other"]').prop('selected', true);
		
			 //disable all options of the TIM material
			$('[name=material_tim] option').attr('disabled','disabled');
			//select and enable the 'other' TIM plate material
			$('[name=material_tim] option').filter(function() { 
    			return ($(this).val().toLowerCase().indexOf("other") >= 0); 
			}).prop('selected', true).removeAttr('disabled', 'disabled');
			//set the tim conductivity to a set value within operating range but make read-only
			$("#material_tim").attr('readonly','readonly');
			$('#material_tim_conductivity').val('0.1');	

		//$('#material_tim_conductivity').attr('readonly','readonly');
	<?php } else { ?>
		$("#material_tim").removeAttr('readonly');
		$('#material_tim_conductivity').removeAttr('readonly');
		$('[name=material_tim] option').removeAttr('disabled','disabled');

	<?php } ?>

	
	// read-only coldplate material if fin type != 0
	<?php 
		//associative array mapping fin options to cold plate materials
		$fin_to_cp = array(
					'1' => 'Copper',
					'2' => 'Copper',
					'3' => 'Copper',
					'4' => 'Aluminium',
					'5' => 'Aluminium'
					);

	
		switch (@$_SESSION['sim_data']['geom']['fin_type']) {
    		case "0":
  			//cold plate material should be selectable
	?>
			$("#material_cold_plate").removeAttr('readonly');
	<?php 
        	break;
    		default :
			//  cold-plate material is set based on fin_options
	?>
			//disable all options of the cold plater material
			$('[name=material_cold_plate] option').attr('disabled','disabled');
			
			//select and enable the cold plate material based on the fin type
			$('[name=material_cold_plate] option').filter(function() { 
    			return ($(this).val().toLowerCase().indexOf("<?php echo strtolower($fin_to_cp[$_SESSION['sim_data']['geom']['fin_options']]);?>") >= 0); 
			}).prop('selected', true).removeAttr('disabled', 'disabled');
			
			
	<?php 	
		}
	?>
	
	
	
	//show material_mixture input when page load if not water selected on working fluid 
	var option_mix =  $("#material_fluid option:selected").val();
	
	var tim_conductivity_obj = $('#material_tim_conductivity');
	var option_tim = $("#material_tim option:selected").val();
		
    if( typeof option_tim == 'undefined' ) {
		tim_conductivity_obj.val("");
		tim_conductivity_obj.attr('readonly','readonly');
	} else if ( option_tim === "other" ) {
		<?php if ( @$_SESSION['sim_data']['geom']['tim_thickness'] !== 0 ) {?>
			tim_conductivity_obj.removeAttr('readonly');
			
		<?php } ?>
		<?php if ( @$_SESSION['sim_data']['geom']['tim_thickness'] == 0 ) {?>
				tim_conductivity_obj.attr('readonly','readonly');
				tim_conductivity_obj.val("0.1");
				
		<?php } ?>
		
		} 
	else {
				if(option_tim === "siliconPaste") {tim_conductivity_obj.val("0.3");}
				if(option_tim === "liquidMetal") {tim_conductivity_obj.val("23.7");}
				tim_conductivity_obj.attr('readonly','readonly');
				

	}
	
    if( typeof option_mix == 'undefined' ) {
         $('#mixtureRatio').toggle(false);
         //$('#mixture_ratio').val(100);
         $( "#mixture_ratio" ).slider( "value", 100);
         //$('#mixture_ratio').slider('refresh');
    } else if (option_mix !== "eg" && option_mix !== "pg" ) {
        $('#mixtureRatio').toggle(false);
        $( "#mixture_ratio" ).slider( "value", 100);
    } else {
		$('#mixtureRatio').toggle(true);
	}
   
	//change tim thermal conductivity input when tim changes
	$("#material_tim").change(function() {
		
		option_tim = $("option:selected", this).val();
		
		if ( option_tim === "other" ) {
			
			<?php if ( @$_SESSION['sim_data']['geom']['tim_thickness'] !== 0 ) {?>
				tim_conductivity_obj.removeAttr('readonly');
				tim_conductivity_obj.val("");
				$( "#mat" ).validate().resetForm();
				
			<?php } ?>
			<?php if ( @$_SESSION['sim_data']['geom']['tim_thickness'] == 0 ) {?>
				tim_conductivity_obj.attr('readonly','readonly');
				tim_conductivity_obj.val("0.1");
				
				
			<?php } ?>
			
	
		} else {
			
			if(option_tim === "siliconPaste") {tim_conductivity_obj.val("0.3");}
			if(option_tim === "liquidMetal") {tim_conductivity_obj.val("23.7");}
			tim_conductivity_obj.attr('readonly','readonly');
		
			
		}
		
			$( "#mat" ).validate().resetForm();
		
	});


	//show material_mixture input when not water selected on working fluid 
	$("#material_fluid").change(function() {
		
		option_mix = $("option:selected", this).val();
		console.log(option_mix);
		
        if ( option_mix !== "eg" && option_mix !== "pg" ) {
            
            $('#mixtureRatio').toggle(false);
            //$('#mixture_ratio').val(100);
            //$('#mixture_ratio').slider('refresh');
            $( "#mixture_ratio" ).slider( "value", 100);

        } else {
             $('#mixtureRatio').toggle(true);
             $( "#mixture_ratio" ).slider( "value", 0);
        }
			$( "#mat" ).validate().resetForm();

	});
	
	<?php if($_SESSION['sim_data']['mat']['hasinput']) { 
	?> $( "#mat" ).valid(); <?php } else { $_SESSION['sim_data']['mat']['hasinput'] = true; }?>
	

});
</script>
<form id="mat" action="#" enctype="application/x-www-form-urlencoded" class="sim-form"> 
<div class="subcontent" id="mat">
    			<h3>MATERIALS</h3>
    			<hr  class="breaklightblue" />
                <div class="leftcol" style="width:40%;">
                		<p><label class="formLabel" style="vertical-align:top;">Coldplate Material</label>
                	   		<select name="material_cold_plate" id="material_cold_plate" size="2" style="display:inline-block; height:auto; padding-top:5px; padding-bottom:5px; width:150px;">
                    			<option selected value="aluminium">Aluminium 1100</option>
                    			<option value="copper">Copper 110</option>
                			</select>
                            <label for="material_cold_plate" class="error" generated="true"></label>
                       </p>
                       <p><label class="formLabel" style="vertical-align:top;">TIM Material***</label>
                	   		<select name="material_tim" id="material_tim" size="3" style="display:inline-block; height:auto; padding-top:5px; padding-bottom:5px; width:150px;">
                    			<option value="siliconPaste">Silicon Paste</option>
                    			<option value="liquidMetal">Liquid Metal</option>
                                <option selected value="other">Other</option>
                			</select>
                            <label for="material_tim" class="error" generated="true"></label>
                       </p>
                       <p>  <label for="material_tim_conductivity" class="formLabel">TIM Thermal Conductivy* </label>
                       		<input type="text" id="material_tim_conductivity" name="material_tim_conductivity" size="5"/> W m<sup>-1</sup>K<sup>-1</sup>
                             <label for="material_tim_conductivity" class="error" generated="true"></label>
                       </p>
                        <p>
                           <input type="hidden" id="material_heater" name="material_heater" size="5"/>
                           <label for="material_heater" class="error" generated="true"></label>
                        </p>
 						<p><label class="formLabel" style="vertical-align:top;">Working Fluid**</label>
                	   		<select name="material_fluid" id="material_fluid" size="5" style="display:inline-block; height:auto; padding-top:5px; padding-bottom:5px; width:100px;">
                    			<option selected value="water_corr">Water</option>
                    			<option value="eg">Water/EG</option>
                                <option value="pg">Water/PG</option>
                                <option value="Dexron">Dexron VI ATF</option>
                                <option value="OptiCoolPH5">OptiCool PH5</option>
                                <option value="KeroseneD60">Kerosene D60</option>
                                <option value="Turbo_Oil_2380">Turbo Oil 2380</option>
                                <option value="Brayco_Micronic_889">Brayco Micronic 889</option>

                                <?php if( hasAccess($therminol_d12_access_email_domains,$user->data['email']) ) : ?>
                                <option value="TherminolD12">Therminol D-12</option>
                                <?php endif; ?>


  
                			</select>
                             <label for="material_fluid" class="error" generated="true"></label>
                       </p>
                       <div id="mixtureRatio" style="display:inline-block; width:220px;">
                			<label class="formLabel">Water (% Mass)</label>
      						<div class="jslider-layout-slider" style="width: 100px; display:inline-block;">
      							<input id="mixture_ratio" type="slider" name="mixture_ratio" value="0" size="15"/>
    						</div>
    						<script type="text/javascript" charset="utf-8">
     			 		$("#mixture_ratio").slider({ from: 0, to: 100, step: 5, round: 1, limits: false, format: { format: '#', locale: 'us' }, skin: "blue" ,scale: [0, '|', 25, '|' , 50, '|', 75, '|', 100]});
    						</script>
                             <label for="mixture_ratio" class="error" generated="true"></label>
                	   </div>
                </div><!-- End .leftcol -->
                <div class = "rightcol" style="width:55%; padding: 0 5px 0 0;">
                	<table class="gridtable"><caption>Fluid Property Comparison at 30 &deg;C</caption>
						<tr>
						  <th width="57"></th>
						  <th width="55">Density <br />[kg m<sup>-3</sup>]</th>
						  <th width="68">Specific heat <br />[J kg<sup>-1</sup> K<sup>-1</sup>]</th>
                          <th width="79">Dynamic viscosity <br />[&micro;Pa s]</th>
                          <th width="99">Thermal conductivity <br />[mW m<sup>-1</sup> K<sup>-1</sup>]</th>
						</tr>
						<tr>
                        	<td>Water</td>
                        	<td>995.7</td>
                            <td>4179.8</td>
                            <td>797.4</td>
                            <td>615.5</td>
                       </tr>
					    <tr>
                        	<td>Water/EG (50/50)</td>
                        	<td>1069.5</td>
                            <td>3408.1</td>
                            <td>2785.1</td>
                            <td>395.7</td>
                       </tr>
					    <tr>
                        	<td>Water/EG (85/15)</td>
                        	<td>1016.4</td>
                            <td>3948.4</td>
                            <td>1128.7</td>
                            <td>540.5</td>
                       </tr>
					    <tr>
                        	<td>Water/PG (50/50)</td>
                        	<td>1030.3</td>
                            <td>3651.8</td>
                            <td>3954.2</td>
                            <td>382.1</td>
                       </tr>
					    <tr>
                        	<td>Water/PG (85/15)</td>
                        	<td>1005.7</td>
                            <td>4050.2</td>
                            <td>1301.8</td>
                            <td>545.6</td>
                       </tr>
                       <tr>
                        	<td>Dexron VI ATF</td>
                        	<td>836.1</td>
                            <td>1942.2</td>
                            <td>37122.8</td>
                            <td>136.2</td>
                       </tr>   
                       <tr>
                        	<td>OptiCool PH5</td>
                        	<td>823.5</td>
                            <td>2166.3</td>
                            <td>6250.4</td>
                            <td>137.1</td>
                       </tr>
                       <tr>
                        	<td>BP TURBO</td>
                        	<td>974</td>
                            <td>1858</td>
                            <td>4845</td>
                            <td>151</td>
                       </tr>                                                                              
						</table>
                    <p class="notes"><b>Special Notes:</b></p>
                    <ul class="notes">
                         <li><b>*Thermal conductivities for Silicon Paste and Liquid Metal are 0.3 W m<sup>-1</sup>K<sup>-1</sup> and 23.7 W m<sup>-1</sup>K<sup>-1</sup>respectively.</b></li>
                    	 <li><b>*Thermal conductivity must be between 0.1-450 W m<sup>-1</sup>K<sup>-1</sup>.</b></li>
                         <li><b>**EG = Ethylene Glycol</b></li>
                         <li><b>**PG = Propylene Glycol</b></li>
                         <li><b>***leave defaults if no TIM (choose 0 for TIM thickness on materials page)</b></li>
                    </ul>
                    <p style="float:right;">
                    	<input onclick="window.location.href='?page=bdry';" type="button"  value="BACK" style="width: 
                        	120px; height: 70px; -webkit-appearance: none; background-color:#00457c; font-weight:bold; 
                            font-size:18px; color:#adafb2; margin-top:20px; margin-bottom:20px; margin-right:10px; display
                            : inline;" /> 
                    	<input onclick="window.location.href='?page=simulate';" type="button"  value="NEXT" style="width: 
                    		120px; height: 70px; -webkit-appearance: none; background-color:#00457c; font-weight:bold; 
                            font-size:18px; color:#adafb2; margin-top:20px; margin-bottom:20px; display:inline;" />
                    </p> 
                </div><!-- End .rightcol -->
            </div><!-- end .subcontent-->    
</form>            