﻿
	 $(document).ready(function(){
								
		// data validation, numeric values only
	 	$("input[name^='score'], input[name^='calc']").numeric();
    	
		//activates the popup window for help
		$('a[rel*=facebox]').facebox(); 
		
		// enables the main submit button 
		$("#submitMainForm").attr('disabled','');
		
		// radion buttons that open the calculator
		$("input[id^='calculator']").click(function() {
				$("#calcForm").show("slow");
		});
		
		// radion buttons that open the calculator
		$("input[id^='FertilizersCalc']").click(function() {
				$("#calcFormF").show("slow");
		});
		
		// radion buttons that open the calculator
		$("input[id^='GrowerCalc']").click(function() {
				$("#calcFormGrower").show("slow");
		});
		
		// radion buttons that open the calculator
		$("input[id^='IncomeCalc']").click(function() {
				$("#calcFormIncome").show("slow");
		});
		
		// radion buttons that open the calculator
		$("input[id^='YieldCalc']").click(function() {
				$("#calcFormYield").show("slow");
		});
		
		if($("#NoScorecard_ProjectFe").val().length == 0){				
			$("#NoScorecard_ProjectN, #NoScorecard_ProjectSp, #NoScorecard_ProjectCo, #NoScorecard_ProjectFe, #NoScorecard_ProjectHectares, #NoScorecard_ProjectBi, #NoScorecard_ProjectVe, #NoScorecard_ProjectLongitude, #NoScorecard_ProjectLatitude").defaultvalue(
				"[Insert name of Project]",	"[Insert Sponsor]", "[Insert total project cost in US$ millions]",	"[Insert kind of feedstock]", "[Insert hectares]",	"[Insert type of biofuel]",	"[Insert version #]", "[Insert longitude]", "[Insert latitude]"	);
		}
		
		
		$("select[name^='selectLH']").change(function(){ 
				
					$("#div1030").hide("fast");
					$("#div3060").hide("fast");
					$("#div60100").hide("fast");
					
									
					$("#selectH13").val(0);
					$("#selectH36WL").val(0);
					$("#selectH61WL").val(0);
				
					
					switch($("select[@name^='selectLH']").val())
					{
						case "1":
							$("#div1030").show("fast");
							break;
						case "2":
							$("#div3060").show("fast");
							break;
						case "3":
							$("#div60100").show("fast");
							break;					
					}
				
				});
		
		
		$("select[name^='selectLP']").change(function(){ 
				
					$("#divPOM").hide("fast");
					$("#divP36OM").hide("fast");
					$("#divP61OM").hide("fast");
					
									
					$("#selectPOM").val(0);
					$("#selectP36OM").val(0);
					$("#selectP61OM").val(0);
				
					
					switch($("select[@name^='selectLP']").val())
					{
						case "1":
							$("#divPOM").show("fast");
							break;
						case "2":
							$("#divP36OM").show("fast");
							break;
						case "3":
							$("#divP61OM").show("fast");
							break;					
					}
				
				});
				
		$("a[id^='MapGIS']").bind("click", function(){
				var valid = validateGIS();
				if(valid)
				{
					var x = $("input[@name='NoScorecard_ProjectLongitude']").val();
					var y = $("input[@name='NoScorecard_ProjectLatitude']").val();
					var area = $("input[@name='NoScorecard_ProjectHectares']").val();
					
				   
					var area= parseInt(area)*10000;
					
					area = Math.sqrt(area/Math.PI);
					
					window.open("map.cfm?notemplate&lat="+y+"&lon="+x+"&radius="+area , "ventana1" , "width=740,height=650,scrollbars=no");
				}
				
		 });
		

		
		$("a[id^='bioGIS']").bind("click", function(){
			
			 	dojo.require("dojo.io.script");
				var serviceResults;
			
            // Send the request to the geoprocessing service
				var valid = validateGIS();
            	if(valid)
				{

					
					$("#NoGISInfo").html("<hr><table width=100% style=font-size:12px><tr><td align=center><img valign=bottom src=/biofuelsscorecard/images/loading.gif><strong> Please wait while we calculate the Biodiversity score based on longitude, latitude and area <br /> Please click on Map to view the location of the project in relation to natural habitats and protected areas.</td></tr></table><hr>");
				$("#NoGISInfo").show("slow");

					var x = $("input[@name='NoScorecard_ProjectLongitude']").val();
					var y = $("input[@name='NoScorecard_ProjectLatitude']").val();
					var area = $("input[@name='NoScorecard_ProjectHectares']").val();
					
				   
					var area= parseInt(area)*10000;
					
					area = Math.sqrt(area/Math.PI);
	

	
					var jsonpArgs = {
						url: "http://174.129.230.157/arcgis/rest/services/IDB_Tool/GPServer/CVSelection/execute",
						callbackParamName: "callback",
						content: {
							AreaRadiusMeters: "{ 'distance' : " +area+", 'units' : 'esriMeters' }",
							CoordinatesWGS84: "{'geometryType' : 'esriGeometryPoint','spatialReference' : {'wkid' : 4326},'features'  : [{    'geometry' : {'x' :" + x +  ", 'y' :" +y+ "},    'attributes' : {'Id' : 1, 'Name' : 'Feature 1'}}]}",
							f: "json"
						},
						load: function(data){
	
							serviceResults = data;
							var Now = new Date();
							endTime = Now.getTime();
	
						},
						error: function(error){
							console.log("service results error: " + error);
						},
						timeout: 60000
					};
					
					var Now = new Date();
					startTime = Now.getTime();
					
					var response = dojo.io.script.get(jsonpArgs);

					
	
					response.addCallback(function() {
						parseServiceResults();
						$("#NoGISInfo").hide("slow");

					})
					

					
				}
				
			
			function parseServiceResults() {
				var type = 0;				
				if (serviceResults.hasOwnProperty("results")) {
										
					if (serviceResults.results[0].hasOwnProperty("value")) {
						if (serviceResults.results[0].value.hasOwnProperty("features")) {
							for (i=0; i<serviceResults.results[0].value.features.length; i++) {
								if (serviceResults.results[0].value.features[i].hasOwnProperty("attributes")) {
									if (serviceResults.results[0].value.features[i].attributes.hasOwnProperty("HabitatNam")) {
										
										if(serviceResults.results[0].value.features[i].attributes.HabitatNam == "Natural Habitat High Priority Ecosystems" || serviceResults.results[0].value.features[i].attributes.HabitatNam == "Natural Habitat Remainder Ecosystems")
										{
											if(type < 2)
											{	
												type =2;
											}
										}
										else
										{
											if(type < 3)
											{	
												type =3;
											}
										}
									}
								}
							}
						}
					}
				}

				switch(type)
				{
					case 0:
						$("input[@id=BiodiverCalc1]").attr('checked','true');
						break;
					case 2:
						$("input[@id=BiodiverCalc3]").attr('checked','true');
						break;
					case 3:
						$("input[@id=BiodiverCalc4]").attr('checked','true');
						break;
				}
				
				
			}
			
				
						
		});
		
		// ends score Biodiversity
		
		
		//Default values for inputs of users' information - Lanaguage-Sensitive//
		//jQuery(function($) {
			
			
		//});
	// This is for avoiding submit when the sums are not 100
	// 
		$("form").submit(function(){ 
				var myValidation = validateThis();
				if ((myValidation == 50) || (myValidation == 51)){
					
					return false;
				}
				else{
					$("#submitMainForm").attr('disabled','true');
					$("#errorMessages").hide("slow");
					return true;
				}
			}); 
			
		
		
		// check the valid field for the GIS service
		validateGIS = function(){
			var aux = false;
			if (($("input[@name='NoScorecard_ProjectLongitude']").val()=="[Insert longitude]")  ||  ($("input[@name='NoScorecard_ProjectLatitude']").val()=="[Insert latitude]") ||  ($("input[@name='NoScorecard_ProjectHectares']").val()=="[Insert hectares]")){
				aux=false;
				$("#NoGISInfo").html("<hr><table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td><strong>Check Latitude, Longitude and Area. </strong> <br/>" + "Latitude, Longitude and Area are required fields (located on top of the page).</td></tr></table><hr>");
				$("#NoGISInfo").show("slow");
			}
			else {
				$("#NoGISInfo").hide("slow");
				aux=true;
			}	
					

			return aux;
		};
		
		
		
		// check the valid field for entire Site
		
		
		validateThis = function(){
			var aux = 100;
			if (($("input[@name='NoScorecard_ProjectLongitude']").val()=="[Insert longitude]")  ||  ($("input[@name='NoScorecard_ProjectLatitude']").val()=="[Insert latitude]") ||  ($("input[@name='NoScorecard_ProjectFeedstock']").val()=="[Insert kind of feedstock]")  ||  ($("input[@name='NoScorecard_ProjectHectares']").val()=="[Insert hectares]")){
				aux=51;
				$("#NoProjectName").html("<hr><table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td><strong>Check Latitude, Longitude, Area and Feedstock.</strong> <br/>" + "Latitude, Longitude, Area and Feedstock are required fields (located on top of the page).</td></tr></table><hr>");
				$("#NoProjectName").show("slow");
			}
			else {
				$("#NoProjectName").hide("slow");
				aux=101;
			}
			$('.totalscore').each(function() {						
						if ($(this).val()==50){
							aux=50; 
							$("#errorMessages").html("<hr><table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td><strong>Check All Your Scores:</strong> <br/>" + "The total for all scores must be 100% exactly</td></tr></table><hr>");
							$("#errorMessages").show("slow");
						}
						if ((aux==101) || (aux==51)){
							$("#errorMessages").hide("slow");
						}
						return true;
					});	
					
					
			return aux;
		};
	// end of the sum=100 validation
	
	// starts score associated with calculator
	$("select[@name^='calc'],input[@name^='calc'],input[@id='closeCalc'],input[@id^='calculator']").bind("keyup change click", function(){
			var FirstStep= $("input[@name='calcdistanceFirstStep']").val()*$("select[@name='calcModeFirstStep']").val()*10;
			var SecondStep= $("input[@name='calcdistanceSecondStep']").val()*$("select[@name='calcModeSecondStep']").val();
			var ThirdStep= $("input[@name='calcdistanceThirdStep']").val()*$("select[@name='calcModeThirdStep']").val();
			var totalSteps = FirstStep + SecondStep + ThirdStep;
			if (totalSteps >= 170000){
				//$("input[@id=calculator2]").attr('disabled','true');
				//$("input[@id=calculator1]").attr('disabled','true');
				//$("input[@id=calculator3]").attr('disabled','');
				$("input[@id=calculator3]").attr('checked','true');
			}
			else {
				if ((totalSteps < 169999 ) && (totalSteps >= 50000 )){
					//$("input[@id=calculator3]").attr('disabled','true');
					//$("input[@id=calculator1]").attr('disabled','true');
					//$("input[@id=calculator2]").attr('disabled','');
					$("input[@id=calculator2]").attr('checked','true');
				}
				else {
					if (totalSteps > 0 ){
						//$("input[@id=calculator3]").attr('disabled','true');
						//$("input[@id=calculator2]").attr('disabled','true');
						//$("input[@id=calculator1]").attr('disabled','');
						$("input[@id=calculator1]").attr('checked','true');
					}
					else{
						//$("input[@id=calculator3]").attr('disabled','true');
						//$("input[@id=calculator1]").attr('disabled','true');
						//$("input[@id=calculator2]").attr('disabled','true');
						$("input[@id=calculator4]").attr('checked','true');
					}
				}
			}
					
	});
				// ends score associated with calculator
				
				
	// starts score associated with Fertilizers
	$("select[@name^='calcFHectares'],input[@name^='checkCRF'],input[@name^='checkUCC'],input[@name^='checkAEWC'],input[@name^='checkNI'],input[@name^='checkLim'],input[@name^='checkSA'],input[@id^='FertilizersCalc']").bind("keyup change click", function(){
			var FirstStep= $("input[@name='checkCRF']").is(':checked') * 1;
			var SecondStep= $("input[@name='checkNI']").is(':checked') * 1;
			var ThirdStep= $("input[@name='checkLim']").is(':checked') * 1;
			var FourthStep= $("input[@name='checkSA']").is(':checked') * 1;
			var FifthStep= $("input[@name='checkUCC']").is(':checked') * 1;
			var SixthStep= $("input[@name='checkAEWC']").is(':checked') * 1;
			var selectV = $("select[@name^='calcFHectares']").val()*1;
			var totalSteps = FirstStep + SecondStep + ThirdStep + FourthStep + FifthStep + SixthStep + selectV;			
			
			if(totalSteps >= 8)
			{
				$("input[@id=FertilizersCalc1]").attr('checked','true');
				
			}else if(totalSteps >= 5 && totalSteps <= 7)
			{
				$("input[@id=FertilizersCalc2]").attr('checked','true');
				
			}else if(totalSteps >= 3 && totalSteps <= 4)
			{
				$("input[@id=FertilizersCalc3]").attr('checked','true');
			}else
			{
				$("input[@id=FertilizersCalc4]").attr('checked','true');
			}
					
	});
	
	
	// starts score associated with Yield
	$("input[@name^='tonsH'],input[@name^='LitersE'],input[@name^='LitersB'],input[@name^='checkFeedcake'],input[@name^='checkGlycerol'],input[@name^='checkBagasse'],input[@name^='checkResidues'],input[@id^='YieldCalc']").bind("keyup change click", function(){
			var itonsH = $("input[@name='tonsH']").val()*1;
			var iLitersE = $("input[@name='LitersE']").val()*1;
			var iLitersB = $("input[@name='LitersB']").val()*1;
			
			var BiofuelYielHectare = ((iLitersE * 23.4) + (iLitersB * 34.5))/1000;
			
			$("input[@name='BiofuelY']").val(BiofuelYielHectare);
			
			
			var iFeedcake= $("input[@name='checkFeedcake']").is(':checked') * ((11.3*iLitersE)/1000);
			var iGlycerol= $("input[@name='checkGlycerol']").is(':checked') * ((3.51*iLitersB)/1000);
			var iBagasse= $("input[@name='checkBagasse']").is(':checked') * (4.7*itonsH);
			var iResidues= $("input[@name='checkResidues']").is(':checked') * (13.5*itonsH);
			var itotal = iFeedcake + iGlycerol + iBagasse + iResidues;
			
			$("input[@name='ProductY']").val(itotal);
			$("input[@name='BiofuelYP']").val(BiofuelYielHectare);
			
			var scoretotal = itotal + BiofuelYielHectare;
			
			
			if(scoretotal > 465)
			{
				$("input[@id=YieldCalc1]").attr('checked','true');
			}else if(scoretotal >= 131 && scoretotal <= 465)
			{
				$("input[@id=YieldCalc2]").attr('checked','true');
			}else if(scoretotal >= 31 && scoretotal <= 130)
			{
				$("input[@id=YieldCalc3]").attr('checked','true');
			}else
			{
				$("input[@id=YieldCalc4]").attr('checked','true');
			}
			
					
	});
	
	
	// starts score associated with Income
	$("select[@name^='selectH36'],select[@name^='selectLH'],select[@name^='selectH13'],select[@name^='selectH36WL'],select[@name^='selectH61WL'],select[@name^='selectLP'],select[@name^='selectPOM'],select[@name^='selectP36OM'],select[@name^='selectP61OM'],input[@id^='IncomeCalc']").bind("keyup change click", function(){
			var iselectH36 = $("select[@name^='selectH36']").val()*1;			
			var iselectH13 = $("select[@name^='selectH13']").val()*1;
			var iselectH36WL = $("select[@name^='selectH36WL']").val()*1;
			var iselectH61WL = $("select[@name^='selectH61WL']").val()*1;			
			var iselectPOM = $("select[@name^='selectPOM']").val()*1;
			var iselectP36OM = $("select[@name^='selectP36OM']").val()*1;
			var iselectP61OM = $("select[@name^='selectP61OM']").val()*1;
			
			
					
			var iTotal = iselectH36 + iselectH13 + iselectH36WL + iselectH61WL + iselectPOM + iselectP36OM + iselectP61OM;
			
			
			if(iTotal >= 35 )
			{
				$("input[@id=IncomeCalc1]").attr('checked','true');
			}else if(iTotal >= 21 && iTotal <= 34)
			{
				$("input[@id=IncomeCalc2]").attr('checked','true');
			}else if(iTotal >= 0 && iTotal <= 20)
			{
				$("input[@id=IncomeCalc3]").attr('checked','true');
			}
			
					
	});
	
	
	// starts score associated with Grower
	$("input[@name^='checkCAMLL_1'],input[@name^='checkAFTP_2'],input[@name^='checkTATT_3'],input[@name^='checkFOPA_4'],input[@name^='checkOAHQ_5'],input[@name^='checkADRM_6'],input[@name^='checkTOWR_7'],input[@name^='checkGARI_8'],input[@name^='checkRAEC_9'],input[@id^='GrowerCalc']").bind("keyup change click", function(){
			var icheckCAMLL_1= $("input[@name='checkCAMLL_1']").is(':checked') * 10;
			var icheckAFTP_2= $("input[@name='checkAFTP_2']").is(':checked') * 10;
			var icheckTATT_3= $("input[@name='checkTATT_3']").is(':checked') * 7;
			var icheckFOPA_4= $("input[@name='checkFOPA_4']").is(':checked') * 7;
			var icheckOAHQ_5= $("input[@name='checkOAHQ_5']").is(':checked') * 8;
			var icheckADRM_6= $("input[@name='checkADRM_6']").is(':checked') * 5;
			var icheckTOWR_7= $("input[@name='checkTOWR_7']").is(':checked') * 3;
			var icheckGARI_8= $("input[@name='checkGARI_8']").is(':checked') * 5;
			var icheckRAEC_9= $("input[@name='checkRAEC_9']").is(':checked') * 3;
			
			var itotal = icheckCAMLL_1 + icheckAFTP_2 + icheckTATT_3 + icheckFOPA_4 + icheckOAHQ_5 + icheckADRM_6 + icheckTOWR_7 + icheckGARI_8 + icheckRAEC_9;
			
			if(itotal >= 41 && itotal <= 58)
			{
				$("input[@id=GrowerCalc1]").attr('checked','true');
			}else if(itotal >= 21 && itotal <= 40)
			{
				$("input[@id=GrowerCalc2]").attr('checked','true');
			}else if(itotal >= 10 && itotal <= 20)
			{
				$("input[@id=GrowerCalc3]").attr('checked','true');
				
			}else if(itotal >= 1 && itotal <= 9)
			{
				$("input[@id=GrowerCalc4]").attr('checked','true');
				
			}else
			{
				$("input[@id=GrowerCalc5]").attr('checked','true');
			}
			
					
	});
				
				
				
				// starts score associated with calculator Water
	$("input[@name^='numberHectares'],input[@id='closeCalcW'],input[@name^='numberMonths'],input[@id^='WaterCalc']").bind("keyup change click", function(){
			
			var myFilterSpan="#remainsWaterCalc";
			var hectares = $("input[@name='numberHectares']").val();
			var Months = $("input[@name='numberMonths']").val();
			
			var FirstStep= ($("input[@name='numberHectares1']").val()*$("input[@name='numberMonths1']").val()) / ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var SecondStep= ($("input[@name='numberHectares2']").val()*$("input[@name='numberMonths2']").val())/ ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var ThirdStep= ($("input[@name='numberHectares3']").val()*$("input[@name='numberMonths3']").val()) / ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var FourthStep= ($("input[@name='numberHectares4']").val()*$("input[@name='numberMonths4']").val())/ ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var FifthStep= ($("input[@name='numberHectares5']").val()*$("input[@name='numberMonths5']").val()) / ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var SixthStep= ($("input[@name='numberHectares6']").val()*$("input[@name='numberMonths6']").val()) / ($("input[@name='numberHectares']").val()*$("input[@name='numberMonths']").val());
			var totalSteps = 0; 
			var myRemaining = 100;
			
			if(hectares > 0 && Months > 0)
			{
				$("input[@name^=stat1]").val(FirstStep*100);
				$("input[@name^=stat2]").val(SecondStep*100);
				$("input[@name^=stat3]").val(ThirdStep*100);
				$("input[@name^=stat4]").val(FourthStep*100);
				$("input[@name^=stat5]").val(FifthStep*100);
				$("input[@name^=stat6]").val(SixthStep*100);
				totalSteps = FirstStep*100 + SecondStep*100 + ThirdStep*100 + FourthStep*100 + FifthStep*100 + SixthStep*100;
				myRemaining = 100 - totalSteps;
			
			}else{
				$("input[@name^=stat1]").val(0);
				$("input[@name^=stat2]").val(0);
				$("input[@name^=stat3]").val(0);
				$("input[@name^=stat4]").val(0);
				$("input[@name^=stat5]").val(0);
				$("input[@name^=stat6]").val(0);
			}
			
			if(totalSteps == 0 && (hectares > 0 || Months > 0))
			{
				$(myFilterSpan).html("<table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td>Fix Problem. <br/>" + "The total must be 100% or all the fields must be blank</td></tr></table>");
			}else{
				if (totalSteps <= 100){
					if (totalSteps == 100){
						$(myFilterSpan).html("<table style=font-size:12px><tr ><td valign=middle>Percentage completed = <font color=#006CA8>100%</font>  </td><td>   <img valign=bottom src=/biofuelsscorecard/jQuery/ok.jpg></td><tr></table>");
					}
					else{
						$(myFilterSpan).html("<table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td>Percentage remaining: <font color=#006CA8>" + myRemaining + "%</font><br/>" + "The total must be 100% or all the fields must be blank</td></tr></table>");
					}
				}
				else{				
					$(myFilterSpan).html("<table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td>Percentage surplus: <font color=#006CA8>" + myRemaining + "%</font><br/>" + "The total must be 100% or all the fields must be blank</td></tr></table>");
				}
			}
			
			
			if($("input[@name^=stat1]").val() != 0)
			{
				$("input[@id^=WaterCalc1]").val($("input[@name^=stat1]").val());
			}
			
			if($("input[@name^=stat2]").val() != 0)
			{
				$("input[@id^=WaterCalc2]").val($("input[@name^=stat2]").val());
			}
			
			if($("input[@name^=stat3]").val() != 0)
			{
				$("input[@id^=WaterCalc3]").val($("input[@name^=stat3]").val());
			}
			
			if($("input[@name^=stat4]").val() != 0)
			{
				$("input[@id^=WaterCalc4]").val($("input[@name^=stat4]").val());
			}
			
			if($("input[@name^=stat5]").val() != 0)
			{
				$("input[@id^=WaterCalc5]").val($("input[@name^=stat5]").val());
			}
			
			if($("input[@name^=stat6]").val() != 0)
			{
				$("input[@id^=WaterCalc6]").val($("input[@name^=stat6]").val());
			}
			
					
	});
	// ends score associated with calculator
			
				/* this function is the percentage calculator for each of the sections */
				$("input[@name^='score'][@type=text]").bind("keyup", function putStatus(){
					var myFilter="input[@name^='" + $(this).attr("id") + "']"; 
					var myFilterSpan="#remain" + $(this).attr("id");
					var myFilterTotal="#total" + $(this).attr("id");
					var myRemaining = 100 - $(myFilter).sum();
						
						if (myRemaining >= 0){
							if (myRemaining > 0){
								$(myFilterTotal).val(50);
								$(myFilterSpan).html("<table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td>Percentage remaining: <font color=#006CA8>" + myRemaining + "%</font><br/>" + "The total must be 100% or all the fields must be blank</td></tr></table>");
							}
							else{
								$(myFilterTotal).val(100);
								$(myFilterSpan).html("<table style=font-size:12px><tr ><td valign=middle>Percentage completed = <font color=#006CA8>100%</font>  </td><td>   <img valign=bottom src=/biofuelsscorecard/jQuery/ok.jpg></td><tr></table>");
							}
						}
						else{
							$(this).val("");
							var myRemaining = 100 - $(myFilter).sum();
							$(myFilterSpan).html("<table style=font-size:12px><tr><td><img valign=bottom src=/biofuelsscorecard/jQuery/warn_sm.jpg></td><td>Percentage remaining: <font color=#006CA8>" + myRemaining + "%</font><br/>" + "The total must be 100% or all the fields must be blank</td></tr></table>");
							
							if (myRemaining == 0){
								$(myFilterTotal).val(50);
								$(myFilterSpan).html("<table style=font-size:12px><tr ><td valign=middle>Percentage completed = <font color=#006CA8>100%</font>  </td><td>  <img valign=middle src=/biofuelsscorecard/jQuery/ok.jpg></td><tr></table>");
							}
						}
						if ($(myFilter).sum()==0){
							$(myFilterSpan).html("<table style=font-size:12px><tr ><td valign=middle>Percentage remaining: <font color=#006CA8>100% </font></td><td> <img valign=middle src=/biofuelsscorecard/jQuery/ok.jpg></td><tr></table>");
							$(myFilterTotal).val(100);
						}
				});
				/* end of the function for each of the sections message validating the 100% needed*/

				// function to clear each item of the form
				$(".clearForm").click(function(){ 
						var myFilter="input[@name^='" + "score" +$(this).attr("id") + "']"; 
						var myFilterRadio="input[@id='" + "score" + $(this).attr("id") + "']"; 
						var myFilterSpan="#remain" + "score" +$(this).attr("id");
						//var myFilterSpan2="#radio" + "score" +$(this).attr("id");
						var myFilterHiddenInput="#totalscore" +$(this).attr("id");
						$(myFilterRadio).each(function(){ 
								$(this).attr('checked','false');
							return true;
						});	
					
						$(myFilterSpan).html("");
						$(myFilter).val("");
						$(myFilterSpan).html("");
						$(myFilterHiddenInput).val("100");
						
				});
				
				//This one is to clean the Calculator
				$(".clearCalc").click(function(){ 	
						$("#calcForm").hide("slow");
						$("input[@name^=calc]").val("0");
						//$("input[@id=calculator3]").attr('disabled','true');
						//$("input[@id=calculator1]").attr('disabled','true');
						//$("input[@id=calculator2]").attr('disabled','true');						   
						$("input[@id=calculator4]").attr('checked','true');
				});
				
				//THis one is to open the calculator
				$(".OpenCalc").click(function(){ 
					$("#calcForm").show("slow");
				
				});
				//THis one is to close the calculator
				$("#closeCalc").click(function(){ 
					$("#calcForm").hide("slow");
				
				});
				
				// Text open the calculator water
				$("input[@id^='WaterCalc']").click(function() {
						$("#calcFormW").show("slow");
				});
				
				//THis one is to open the calculator
				$(".OpenCalcW").click(function(){ 
					$("#calcFormW").show("slow");
				
				});
				
				//THis one is to close the calculator
				$("#closeCalcW").click(function(){ 
					$("#calcFormW").hide("slow");
				
				});
				
				//THis one is to open the login
				$(".openLogin").click(function(){ 
					$("#login").slideToggle("fast");
				
				});
				
				//THis one is to open the calculator Fertilizers
				$(".OpenCalcF").click(function(){ 
					$("#calcFormF").show("slow");
				
				});
				
				//THis one is to close the calculator Fertilizers
				$("#closeCalcFerti").click(function(){ 
					$("#calcFormF").hide("slow");
				
				});
				
				//THis one is to open the calculator Yield
				$(".OpenCalcYield").click(function(){ 
					$("#calcFormYield").show("slow");
				
				});
				
				//THis one is to close the calculator Yield
				$("#closeCalcYield").click(function(){ 
					$("#calcFormYield").hide("slow");
				
				});
				
				//THis one is to open the calculator Income
				$(".OpenCalcIncome").click(function(){ 
					$("#calcFormIncome").show("slow");
				
				});
				
				//THis one is to close the calculator Income
				$("#closeCalcIncome").click(function(){ 
					$("#calcFormIncome").hide("slow");
				
				});
				
				//THis one is to open the calculator Grower
				$(".OpenCalcGrower").click(function(){ 
					$("#calcFormGrower").show("slow");
				
				});
				
				//THis one is to close the calculator Grower
				$("#closeCalcGrower").click(function(){ 
					$("#calcFormGrower").hide("slow");
				
				});
				
				//$("#dialog").dialog();
				
				// to validate data in the item inputs, supports the ctrl+v
				$("input[@name^='score']").bind("keyup", function(){
					if(isNaN($(this).val())||$(this).val()>100 || $(this).val()<0){
						if (isNaN($(this).val())){
							$(this).val("");
						}
					
					$("#mytdeps").attr("disabled", "true"); 

					}					
					else {
					$(this).css({ backgroundColor:"white"});
					$("#mytdeps").attr("disabled", ""); 
					}
		  		
				});
				
				// to validate data in the calculator inputs
				$("input[@name^='calc']").bind("keyup", function(){
					
						if (isNaN($(this).val())){
							$(this).val("");
						}		  		
				});
				
				
					 
	 }); 
