/*
 * Autodata JS For "calculator.jsp" Page.
 * version: 1.0.0
 * Author: Resen
 * 
 * (C) 2008 Autodata Solutions. All Rights Reserved. 
 * This source code is the confidential and proprietary information of 
 * Autodata. The user shall not, in whole or in part, modify, copy, 
 * publish, disclose or make any use of this source code unless 
 * specifically authorized in a written agreement with Autodata. 
 */


ASC.apply(Page, {	
	init: function()
	{
		var tt1 = new Ext.ToolTip({
		    target: 'tongueWeight',
		    title: 'Tongue Weight',
		    html:'<div class="calculator_toolTipDesc">The downward force exerted on the hitch ball by the trailer coupler.</div>',
		    floating: true,
		    dismissDelay: 0
		});
	
		var tt2 = new Ext.ToolTip({
		    target: 'grossCombinationWeight',
		    title: 'Gross Combination Weight (GCW)',
		    html:'<div class="calculator_toolTipDesc">Total weight of a fully equipped truck and trailer with cargo, driver and passengers, fuel, coolant, equipment, etc.</div>',
		    floating: true,
		    dismissDelay: 0
		});
		
		var tt3 = new Ext.ToolTip({
		    target: 'curbWeight',
		    title: 'Curb Weight',
		    html:'<div class="calculator_toolTipDesc">The weight of an unmanned vehicle without cargo, but including fuel, necessary fluids, and all standard equipment.</div>',
		    floating: true,
		    dismissDelay: 0
		});	
		
		ASC.getEl('vehicle_weight').on('change', this.checkValue, Page, {formName:'gcw_form'});
		ASC.getEl('trailer_weight').on('change', this.checkValue, Page, {formName:'gcw_form'});
		ASC.getEl('passengers').on('change', this.checkPullDown, Page, {formName:'gcw_form'});
		ASC.getEl('payload').on('change', this.checkValue, Page, {formName:'gcw_form'});
		ASC.getEl('item_weight').on('change', this.checkValue, Page, {formName:'ttw_form'});
		ASC.getEl('liquids').on('change', this.checkValue, Page, {formName:'ttw_form'});
		ASC.getEl('trailer').on('change', this.checkValue, Page, {formName:'ttw_form'});
		
		ASC.getEl('vehicle_weight').on('keydown', this.testForEnter, Page, {formName:'gcw_form'});
		ASC.getEl('trailer_weight').on('keydown', this.testForEnter, Page, {formName:'gcw_form'});
		ASC.getEl('payload').on('keydown', this.testForEnter, Page, {formName:'gcw_form'});
		ASC.getEl('item_weight').on('keydown', this.testForEnter, Page, {formName:'ttw_form'});
		ASC.getEl('liquids').on('keydown', this.testForEnter, Page, {formName:'ttw_form'});
		ASC.getEl('trailer').on('keydown', this.testForEnter, Page, {formName:'ttw_form'});
		
		ASC.getEl('clearForm').on('click', this.clearForms, Page);
		ASC.getEl('calculateTtw').on('click', this.calculateTTW, Page);
		ASC.getEl('calculateGcw').on('click', this.calculateGCU, Page);		
		
		Page.clearForms();
	},
	
	calculateGCU: function(){
		ASC.Chrysler.trackLinkMetrics('content', 'calculate_gross_combination_weight');
		this.addUpGCW();
	},
	calculateTTW: function(){
		ASC.Chrysler.trackLinkMetrics('content', 'calculate_total_towed_weight');
		this.addUpTTW();
	},
	testForEnter:function(event, el, options){
		if (event.keyCode == 13) {
			this.checkValue(event, el, options);
			event.cancelBubble = true;
			event.returnValue = false;
		}
	},
	clearForms: function()
	{
		document.forms.gcw_form.reset();
		Page.vehicle_weightValue = 0;
		Page.trailer_weightValue = 0;
		Page.passengersValue = 1;
		Page.payloadValue = 0;
		Page.addUpGCW();
		document.forms.ttw_form.reset();
		Page.item_weightValue = 0;
		Page.liquidsValue = 0;
		Page.trailerValue = 0;
		Page.addUpTTW();
	},
	
	vehicle_weightValue: 0,
	trailer_weightValue: 0,
	passengersValue: 1,
	payloadValue: 0,
	item_weightValue: 0,
	liquidsValue: 0,
	trailerValue: 0,
	
	checkPullDown: function(evt, el, options)//(argElement,argForm)
	{
		var argElement = el;
		var argForm = options.formName;
		
		var tempValue = Page.stripComma(argElement.options[argElement.selectedIndex].value);
		if (Page.isNumeric(tempValue)){
			//eval( argElement.name + "Value =" + tempValue );
			eval( "Page." + argElement.name + "Value =" + tempValue );
			if(argForm == "gcw_form"){
				Page.addUpGCW();
			}else{
				Page.addUpTTW();
			}
		}else{
			alert("Please enter a valid number.");
			argElement.options[argElement.selectedIndex].value = eval( argElement.name + "Value");
		}
	},
	
	checkValue: function(evt, el, options)
	{ 
		var argElement = el;
		var argForm = options.formName;
		
		var tempValue = Page.stripComma(argElement.value);
		
		if (Page.isNumeric(tempValue)){
			//eval( argElement.name + "Value =" + tempValue );
			eval( "Page." + argElement.name + "Value =" + (1*tempValue) );
			if(argForm == "gcw_form"){
				Page.addUpGCW();
			}else{
				Page.addUpTTW();
			}
		}else{
			alert("Please enter a valid number.");
			argElement.value = eval( "Page." + argElement.name + "Value");
		}
	},
	
	addUpGCW: function()
	{
		var total_gcw_weight = (1 * Page.vehicle_weightValue + 1 * Page.trailer_weightValue + 150 * Page.passengersValue + 1 * Page.payloadValue);
		Page.showWeight(total_gcw_weight,"gcwdiv");
	},
	
	addUpTTW: function()
	{
		var total_ttw_weight = (1*Page.item_weightValue + 7 * Page.liquidsValue + 1 * Page.trailerValue);
		Page.showWeight(total_ttw_weight,"ttwdiv");
	},
	
	showWeight: function(argWeight,argDiv)
	{
		var formatWeight = "<div class='towingBold'><nobr>";
		if (argDiv == "gcwdiv" ){
			formatWeight += "Gross Combination Weight:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}else{
			formatWeight += "Total Towed Weight:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		formatWeight += Page.addComma(argWeight);
		formatWeight += " total lbs</nobr></div>";
		
		Page.writeDivH(argDiv,formatWeight);
	},
	
	isNumeric: function(d)
	{
		if(d.length==0)return false;
		var c;
		for(var i=0; i<d.length; i++) {
			c = d.charAt(i);
			if(!(c >= "0" && c <= "9")) return false;
		}
		return true;
	},
	
	
	stripComma: function(string) 
	{
		removeComma = /,/g;
		var result = string.replace(removeComma, "");
		return result;
	},
	
	addComma: function(argValue)
	{
		var str = new String( argValue );
		if( str.length > 3 ) {
			if( str.length == 4 )
				insertComma = /(.{1})(.{3})/;
			if( str.length == 5 )
				insertComma = /(.{2})(.{3})/;
			if( str.length == 6 )
				insertComma = /(.{3})(.{3})/;
			if( str.length == 7 )
				insertComma = /(.{3})(.{3})/;
			result = str.replace(insertComma, "$1,$2");
		} else
			result = str;
		return result;
	},

	getE: function(e,f)
	{
		d=document;
		a=(d.all)?1:0;
		l=(d.layers)?1:0;
		
		if(l){
			f=(f)?f:self;
			V=f.document.layers;
			if(V[e])
				return V[e];
			for(W=0;W<V.length;)
				t=getE(e,V[W++]);
			return t;
		}
		if(a)
			return d.all[e];
		return d.getElementById(e);
	},
	
	writeDivH: function(e,h)
	{
		d=document;
		a=(d.all)?1:0;
		l=(d.layers)?1:0;
		
		e=Page.getE(e);
		if(l){
			Y=e.document;
			Y.write(h);
			Y.close();
		}else 
			e.innerHTML=h;
	}
});

ASC.onReady(Page.init, Page);
