//*********************************************************************************************
//DO NOT MODIFY THE CODE BELOW
//*********************************************************************************************
// Rod - slider script

var thisURL = location.toString();
var thisLan="en";
if (thisURL.indexOf('french') != -1){
	thisLan="fr";
}

$(document).ready(function(){

	$(".calc").hover(function () {
			
        	var imgSuffix = $(this).attr("src");
			$(this).attr("src", imgSuffix.replace(/_off.jpg/,"_on.jpg") );
      	}, 
     	 function () {
        	var imgSuffix = $(this).attr("src");
			$(this).attr("src", imgSuffix.replace(/_on.jpg/,"_off.jpg") );
      	});

		
	
	//user clicked calculate button
	$(".calc").click(function (){
		
		monthly = (0.03 * ($('#grocSlider').slider( "value", 0 ) + $('#travSlider').slider( "value", 0 ) + $('#clothSlider').slider( "value", 0 ) + $('#eduSlider').slider( "value", 0 ) + $('#entSlider').slider( "value", 0 ) + $('#miscSlider').slider( "value", 0 )));
		
		if(monthly != 0){
		
		monthly = monthly.toFixed(2);
		yearly = (monthly * 12).toFixed(2);
		yearlyOut = addComma (yearly, this, 6);
		monthlyOut = addComma (monthly, this, 6);
		
		if (thisURL.indexOf('french') != -1){
			monthlyOut = monthlyOut.replace(".",",");
			yearlyOut = yearlyOut.replace(".",",");
		
			$("#estEarnPerMoVal").html( monthlyOut + '$');
			$("#estEarnPerYrVal").html( yearlyOut  + '$' )
		}
		else{
			$("#estEarnPerMoVal").html( '$' + monthlyOut );
			$("#estEarnPerYrVal").html( '$' + yearlyOut );
		}
		}
	});
	
	
		$(".minMax").blur(function (){
		//alert("anything");
		newValue = $("#"+this.id).val();
		thisID=String(this.id).replace(/Min/,"");
		//alert(newValue);
		//first remove comma/space and preceding zeros
		newValue = newValue.replace(/ /,"");
		newValue = newValue.replace(/,/,"");
		newValue = newValue.replace(/^[0]+/,"0");
		
		//if new value isn't a number then use default value
		if(isNaN(newValue) || String(newValue).length == 0){
			//$("#"+this.id).val(addComma(window[this.id],thisID));
			newValue = window[this.id];
			$('#'+thisID+'Slider').slider("moveTo",counterValue,0);
			//alert("use default value");
		}
		//check against acceptable min value
		else if((parseInt(newValue) < (window[thisID+"Min"] / window[thisID+"Steps"])) && (parseInt(newValue) > 0)){
			//new value is greater than 0 but smaller than min
			//alert(window[thisID+"Max"]);
			newValue = window[thisID+"Min"] / window[thisID+"Steps"]  ;
			//alert(newValue);
			//alert("new value"+newValue+" is outside the range\n min: "+window[thisID+"Min"]+"\n"+ window[thisID+"Max"]);
		}
		//check against acceptable max value
		else if(parseInt(newValue) > window[thisID+"Min"]){
			//new value greater than max
			newValue = window[thisID+"Min"];
			//alert(newValue);
			//alert("new value"+newValue+" is outside the range\n min: "+window[thisID+"Min"]+"\n"+ window[thisID+"Max"]);
		}
		
			//get the counterpart's value
			counterValue = $("#"+thisID+"Min").val();
			counterValue = counterValue.replace(/ /,"");
			counterValue = parseInt(counterValue.replace(/,/,""));
			
			//update the slider
			$('#'+thisID+'Slider').slider("moveTo",newValue,0);
			
			//$('#'+thisID+'Slider').slider("moveTo",counterValue,0);
			newCounterValue = $("#"+thisID+"Min").val();
			newCounterValue = newCounterValue.replace(/ /,"");
			newCounterValue = parseInt(newCounterValue.replace(/,/,""));
			
			$('#'+thisID+'MinYr').val(addComma (newCounterValue * 12, this, 3));
			$('#'+thisID+'MinYr').val(addComma (newCounterValue * 12, this, 3));
			
	});
	
	$(".minMaxYr").blur(function (){
		newValue = $("#"+this.id).val();
		thisID=String(this.id).replace(/MinYr/,"");
		
		//first remove comma/space and preceding zeros
		newValue = newValue.replace(/ /,"");
		newValue = newValue.replace(/,/,"");
		newValue = newValue.replace(/^[0]+/,"0");
		//if new value isn't a number then use default value
		if(isNaN(newValue) || String(newValue).length == 0){
			newValue = window[this.id];
			$('#'+thisID+'Slider').slider("moveTo",counterValue,0);
		}
		//check against acceptable min and max values
		else if(parseInt(newValue) < window[thisID+"MinYr"] || parseInt(newValue) > window[thisID+"Max"]){
			//new value is outside the range
			newValue = window[this.id];
		}
		
			//get the counterpart's value
			counterValue = $("#"+thisID+"MinYr").val();
			counterValue = counterValue.replace(/ /,"");
			counterValue = parseInt(counterValue.replace(/,/,""));
			
			//update the slider
			$('#'+thisID+'Slider').slider("moveTo",(counterValue / 12),0);
			
			newCounterValue = $("#"+thisID+"MinYr").val();
			newCounterValue = newCounterValue.replace(/ /,"");
			newCounterValue = parseInt(newCounterValue.replace(/,/,""));
			
			$('#'+thisID+'Min').val(addComma (newCounterValue / 12, this, 3));
			
	});
	
});


//check new values and update text boxes. 
function updateBoxes(thisID,leftValue){
		yrValue = (leftValue * 12);
		
		minValue = addComma(leftValue, thisID, 3);
		yrFinValue = addComma(yrValue, thisID, 3);
		
		$("#"+thisID+"Min").val(minValue);
		$("#"+thisID+"MinYr").val(yrFinValue);
 	}

//add comma or space if needed
function addComma(thisValue, thisID, thisLengh){
	
	

	if(thisValue > 999){
		leftString=Left(thisValue,String(thisValue).length-thisLengh);
		rightString=Right(thisValue,String(thisValue).length-leftString.length);
		if(thisLan == "en"){
			thisValue = leftString + "," + rightString;
		}
		else{
			thisValue = leftString + " " + rightString;
		}
	}
	return thisValue;
}

//add left and right functions
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
} 
//*********************************************************************************************
////DO NOT MODIFY THE CODE ABOVE
//*********************************************************************************************
