var isIE = (navigator.appName == "Microsoft Internet Explorer")?true:false;
var isIE7 = (document.documentElement && typeof document.documentElement.style.maxHeight!="undefined")?true:false;
var isSafari = (document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false;


ie = (document.all) ? 1 : 0;
n = !ie;

function Go(){
	document.onkeypress = keyDown;
	if (n) {
    	document.captureEvents(Event.KEYPRESS);
	}
}

function keyDown(e) { /* Prevent non-numeric values for calc. change module */
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}
	if ((keycode < 45 || keycode > 57 ) && keycode != 0 && keycode != 8) {
		return false;
	}
	//else return true;
}


function CalcUpdate(elem) {	/* Objct for calculate-change module */
	var me = this;
	this.elem = elem;
	

	
	function parseOutCent(dec_val) {
		if(dec_val==0 || dec_val==00) {
			return 100;
		}
		if(dec_val.length < 2) {
			return dec_val;
		}
		if(dec_val.substring(0,1) == 0) {
			return dec_val.substring(1);
		} else {
			return dec_val;
		}
	}
	function formatKTCRes(ktc_val) {	// Logic to format displayed calculated total savings
		if(ktc_val.length>0) {
			if(ktc_val.length>2) {
				return (ktc_val.substring(0,ktc_val.length-2))+"."+(ktc_val.substring(ktc_val.length-2));
			} else if(ktc_val.length>1) {
				return "0."+ktc_val;
			} else {
				return "0.0"+ktc_val;
			}
		} else {
			return ktc_val;
		}
	}
	function calculateChange() {	// Main function - corrects for improper formating and accomodates/ignores accordingly
		//blockNonNums();
		var val_0 = document.getElementById('calc_input_0').value;
		var val_1 = document.getElementById('calc_input_1').value;
		var val_2 = document.getElementById('calc_input_2').value;
		if(val_0.length<1 || val_1.length<1 || val_2.length<1) {
			return;
		}
		/* begin regex for input validation and accomodation */
		var dec_regex = /a-zA-Z/g;
		w_res0 = val_0.match(dec_regex);
		if(w_res0 != null) {
			return;
		}
		w_res1 = val_1.match(dec_regex);
		if(w_res1 != null) {
			return;
		}
		w_res1 = val_1.match(dec_regex);
		if(w_res1 != null) {
			return;
		}
		dec_regex = /^\s*\.\d+$/g;
		var dres0 = val_0.match(dec_regex);
		if(dres0 != null) {
			val_0 = "0"+val_0;
		}
		var dres1 = val_1.match(dec_regex);
		if(dres1 != null) {
			val_1 = "0"+val_1;
		}
		var dres2 = val_2.match(dec_regex);
		if(dres2 != null) {
			val_2 = "0"+val_2;
		}
		dec_regex = /^\d+\.\d+$/g;
		var res0 = val_0.match(dec_regex);
		if(res0 == null) {
			return;
		}
		var res1 = val_1.match(dec_regex);
		if(res1 == null) {
			return;
		}
		var res2 = val_2.match(dec_regex);
		if(res2 == null) {
			return;
		}
		/* end regex */
		// Create arrays for calculating change
		var cc_val_0 = val_0.split(".");
		var cc_val_1 = val_1.split(".");
		var cc_val_2 = val_2.split(".");
		
		cc_val_0[1] = parseOutCent(cc_val_0[1]);
		cc_val_1[1] = parseOutCent(cc_val_1[1]);
		cc_val_2[1] = parseOutCent(cc_val_2[1]);
		
		var totalChange = ((100-cc_val_0[1])+(100-cc_val_1[1])+(100-cc_val_2[1]));
		
		document.getElementById('calc_res_block').innerHTML = formatKTCRes(""+totalChange);
	}
	
	elem.onkeyup = function(ev) {
		calculateChange();
	};
	
}
function bindEvents() {	// Bind calculate-change input elements to calc-change objects
	if($('m_calc_items') && $('m_calc_items').getElements('input[class=calc_mod_txt_in]')) {
		var tags = $('m_calc_items').getElements('input[class=calc_mod_txt_in]');
		var i=0;
		while (i<tags.length) {
			var tag = tags[i];
				new CalcUpdate(tag);
			i++;
		}
	}
}
window.onload = function() {
	bindEvents();	// call bind events fn for calc change module
	
	//new Effect.Tooltip('gtip_1', '<div class="gt_header">&nbsp;</div><div class="gt_body">Lorem ipsum, yo (1)</div><div class="gt_footer">&nbsp;</div>',{className: 'graph_tooltip',offset: {x:-20, y:-50}});
	//new Effect.Tooltip('gtip_2', '<div class="gt_header">&nbsp;</div><div class="gt_body">Lorem ipsum, yo (2)</div><div class="gt_footer">&nbsp;</div>',{className: 'graph_tooltip',offset: {x:-20, y:-50}});
	
}

function openMainCont(arg1) {
	if(arg1 == 1 && document.getElementById('mcb_btn').className == "mcb_btn_c") {
		document.getElementById('main_c_bucket').className = "mcb_open";
		document.getElementById('mcb_content').className = "mcb_displayed";
		document.getElementById('mcb_btn').className = "mcb_btn_o";
		//if (Effect.Queue.effects.length > 0) return;	// prevent multiple effects
		//new Effect.BlindDown('mcb_content');
		
	} else if(arg1 == 0 && document.getElementById('mcb_btn').className == "mcb_btn_o") {
		//if (Effect.Queue.effects.length > 0) return;	// prevent multiple effects
		//new Effect.BlindUp('mcb_content');
		document.getElementById('mcb_content').className = "mcb_hidden";
		document.getElementById('mcb_btn').className = "mcb_btn_c";
		document.getElementById('main_c_bucket').className = "mcb_closed";
		
	} else {
		return;
	}
}






