$(document).ready(function(){
	
	$('select[name="chassis"]').livequery('change', function(){
		
		var temp = $(this).val();
		
		
		// BLURBS
		callCBlurb(temp)
		callRCBlurb(1);
		callHDBlurb(1);
		callMBlurb(temp);
		callMaxDrives(1,temp);
        
        
        //Get Memory Quant
        var uri = "_selects.php?et=phonehome&do=getmem&chassid=" + temp;
		$.getJSON(
			uri, 
			function(data){
        		jQuery.each(data, function(i, val){
			    	$('#memlb').html(val);
			    	$('#mem').val(i);
			    });
        	}
        );
        
        
        
        //Get Memory Quant
        var ur = "_selects.php?et=phonehome&do=getmemq&chassid=" + temp; 
		$.getJSON(
			ur, 
			function(data){
        		$('select[name="memc"]').empty();
        		$('select[name="memc"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="memc"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
        
        
        //SET BANDWIDTH 
        $('select[name="bandw"]').empty();
		$('select[name="bandw"]').removeAttr("disabled");
		
		for($i=1; $i<=10; $i++)
		{
			$('select[name="bandw"]').append('<option value="' + $i + '">' + $i + ' MBit</option>');
		}
		        
		        
		
        //Set Processor count
		$('select[name="procc"]').empty();
		$('select[name="procc"]').removeAttr("disabled");
	   	$('select[name="procc"]').append('<option value="1">Single</option>');
	   	$('select[name="procc"]').append('<option value="2">Double</option>');
        
        
        
        
        //Get Processors
        var ur = "_selects.php?et=phonehome&do=getproc&chassid=" + temp; 
        $.getJSON(
			ur, 
			function(data){
        		$('select[name="proc"]').empty();
        		$('select[name="proc"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="proc"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
        
        
        
        
        //Get HDTypes        
        var ur = "_selects.php?et=phonehome&do=gethdt&chassid=" + temp;         
        $.getJSON(
			ur, 
			function(data){
        		$('select[name="hdt"]').empty();
        		$('select[name="hdt"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="hdt"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
        
        
        //Get RAID        
        var ur = "_selects.php?et=phonehome&do=getraid&chassid=" + temp;         
        $.getJSON(
			ur, 
			function(data){
        		$('select[name="raidc"]').empty();
        		$('select[name="raidc"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="raidc"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
        
        
        
        
        //set inital Raids
        var ur = "_selects.php?et=phonehome&do=getavailraid&drivequant=1"; 
		$.getJSON(
			ur, 
			function(data){
        		$('select[name="raids"]').empty();
        		$('select[name="raids"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="raids"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
		
		//Get HD's
        var ur = "_selects.php?et=phonehome&do=gethds&chassid=" + temp; 
        $.getJSON(
			ur, 
			function(data){
        		$('select[name="hd"]').empty();
        		$('select[name="hd"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="hd"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );

	});
	
	
	// this is the hd type changes
	$('select[name="hdt"]').livequery('change', function(){
		
		var temp = $(this).val();
		var ur = "_selects.php?et=phonehome&do=gethds&hdt=" + temp; 
		
		callHDTB(temp);
		
		//Get HardDrives
		$.getJSON(
			ur, 
			function(data){
        		$('select[name="hd"]').empty();
        		$('select[name="hd"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="hd"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
	});
	
	
	// this is the hd quant changes
	$('select[name="hdq"]').livequery('change', function(){
		
		var temp = $(this).val();
		var ur = "_selects.php?et=phonehome&do=getavailraid&drivequant=" + temp; 
		
		//Get HardDrives
		$.getJSON(
			ur, 
			function(data){
        		$('select[name="raids"]').empty();
        		$('select[name="raids"]').removeAttr("disabled");
        		jQuery.each(data, function(i, val){
			    	$('select[name="raids"]').append('<option value="' + i + '">' + val + '</option>');
			    });
        	}
        );
        
	});
	
	
	
	
	// this is the hd changes	
	$('select[name="hd"]').livequery('change', function(){
		
		var temp = $(this).val();
		callHDB(temp);
        
	});
	
	
	
	// this sets max drives depening on controller (raid)	
	$('select[name="raidc"]').livequery('change', function(){
		var chassid = $('select[name="chassis"]').val();
		callRCBlurb($(this).val()); //set the raid blurb
		callMaxDrives($(this).val(),chassid);
	});


	
	
	// the submit calculator	
	$('input[name="btSRV"]').livequery('click', function(){
	
	
		//get chassis id
		var temp = $('select[name="chassis"]').val();
		var valChassis = temp;
		
		//get processor id
		var temp = $('select[name="proc"]').val();
		var valProcessor = temp;
		
		//get processor count
		var temp = $('select[name="procc"]').val();
		var valProcessorC = temp;
		
		//get memory id
		var temp = $('#mem').val();
		var valMem = temp;
		
		//get memory count
		var temp = $('select[name="memc"]').val();
		var valMemC = temp;
		
		//get raidcontroller id
		var temp = $('select[name="raidc"]').val();
		var valRaidC = temp;
		
		//get hd id
		var temp = $('select[name="hd"]').val();
		var valHD = temp;
		
		//get hdCount
		var temp = $('select[name="hdq"]').val();
		var valHDQuant = temp;
		
		//get bandwidth
		
		var temp = $('select[name="bandw"]').val();
		var vBandw = temp;
		
		//Chassis Blurb
		//var ur = "_selects.php?et=phonehome&do=calc&c=" + valChassis + "&p=" + valProcessor + "&pc=" + valProcessorC + "&m=" + valMem + "&mc=" + valMemC + "&r=" + valRaidC + "&h=" + valHD + "&hc=" + valHDQuant + "&bandw=" + vBandw;
		//$('div#srvT').load(ur);
		
		//Chassis Blurb
		var ur = "_selects.php?et=phonehome&do=calc&c=" + valChassis + "&p=" + valProcessor + "&pc=" + valProcessorC + "&m=" + valMem + "&mc=" + valMemC + "&r=" + valRaidC + "&h=" + valHD + "&hc=" + valHDQuant + "&bandw=" + vBandw;
		$('div#srvT').load(ur, {}, function(){
			$('#submitcont').livequery('click', function(){
				
				var arr = $('input#myarr').val();
				var nam = $('input#nam').val();
				var tel = $('input#tel').val();
				var em = $('input#em').val();
				
				//alert(anam);
				
				$('span#final').html('Thanks ' + nam + ', we will be in touch shortly');
				
				var ur = "_selects.php?et=phonehome&do=contactme&myarr=" + arr + "&nam=" + nam + "&em=" + em + "&tel=" + tel;
				
				//alert(ur);
				
				$('div#contm').load(ur);
				
			})
		})
        
	});

	
	
});

//submitcont

function callMaxDrives(raidtype,chassid){
	
	var ur = "_selects.php?et=phonehome&do=getmaxdrive&raidc=" + raidtype + "&cid=" + chassid;
	
	//alert(ur);
	
	$.getJSON(
		ur, 
		function(data){
    		$('select[name="hdq"]').empty();
    		$('select[name="hdq"]').removeAttr("disabled");
    		jQuery.each(data, function(i, val){
		    	$('select[name="hdq"]').append('<option value="' + i + '">' + val + '</option>');
		    });
    	}
    );
}

function callHDBlurb(hdid){
	//HD Blurb
	var ur = "_selects.php?et=phonehome&do=gethdb&id=" + hdid; 
	$('div#srvHD').load(ur);
}

function callHDTB(hdid){
	//HD Blurb
	var ur = "_selects.php?et=phonehome&do=gethdtb&id=" + hdid; 
	$('div#srvHD').load(ur);
}

function callHDB(hdid){
	//HD Blurb
	var ur = "_selects.php?et=phonehome&do=gethdb1&id=" + hdid; 
	$('div#srvHD').load(ur);
}

function callRCBlurb(rcid){
	//RAID Controller Blurb
	var ur = "_selects.php?et=phonehome&do=getrcb&id=" + rcid; 
	$('div#srvRC').load(ur);
}

function callMBlurb(chassid){
	var ur = "_selects.php?et=phonehome&do=getmb&id=" + chassid; 	
	//Set Server Blurb 	
	$('div#srvM').load(ur);
}

function callCBlurb(chassid){
	var ur = "_selects.php?et=phonehome&do=getsb&id=" + chassid; 	
	//Set Server Blurb 	
	$('div#srvB').load(ur);
}