// Doesn't appear to exist in vanilla Javascript:
// Iterates through an array and returns the element with the value matching
// val.
function findByValue(ar, val) {
	var i;
	for(i = 0; i < ar.length; i++) {
		if(ar[i].value == val) {
			return ar[i];
		}
	}

	return null;
}

// This function shows an element if visible_value == current_value.  It's
// mostly used in the onchange of of the drop-downs containing an 'other'
// field to show the 'Please Specify' part.
function otherField(visible_value, current_value, element) {
	element = $(element);
	// '' == 0, unfortunately, and eval() is what passes for atoi() from what I
	// can tell.  :(
	if(visible_value === eval(current_value)) {
		element.style.display = '';
	} else {
		element.style.display = 'none';
	}
}

// This function copies the fields from one set into the other.  It's fairly
// hard-coded to our current purpose.
function addressSameify(issame) {
	var orig = 'estimate_address';
	var clone = 'mailing_address';
	var fields = [ '_line1', '_line2', '_city', '_state', '_zip' ];

	var i;

	if(issame) {
		for(i = 0; i < fields.length; i++) {
			var of = $(orig + fields[i]);
			var cf = $(clone + fields[i]);

			// We don't strictly need to copy all of these values,
			// but it does make the user feel better.
			cf.value = of.value;
			cf.disabled = true;
		}
	} else {
		for(i = 0; i < fields.length; i++) {
			var cf = $(clone + fields[i]);
			cf.disabled = false;
		}
	}
}

// This function calculates total price on estimate form page
// hard-coded to our current purpose.
function updateTotal() {
	
	$('estimate_company_total_value').innerHTML = formatCurrency(get_total_project_price());
	
	$('estimate_company_discount_value').innerHTML = formatCurrency(get_project_discount());
	
	
	ttl = formatCurrency(get_total_project_price() - get_project_discount());
	
	$('estimate_company_total_after_discount').innerHTML = ttl;
	

	
	full_price = formatCurrency(get_total_project_price() - get_project_discount() + get_commision_discount());
	
	$('total').innerHTML = full_price;
	updateCommision();
}



function get_total_project_price(){
	var orig = 'estimate';
	
	var cost_fields = [ '_access_cost', '_travel_cost', '_tearoff_cost', '_plywood_cost', '_additional_material_cost', '_material_cost', '_other_charges_cost' ];

	var i;


	var total_price = 0.0;	
	for(i = 0; i < cost_fields.length; i++) {
		var f = $(orig + cost_fields[i]);
		num = parseFloat(f.value);
		if (num >0 )
		total_price += num;
	}
	return total_price
}

function get_project_discount(){
	var orig = 'estimate';
	discount = $(orig +'_company_discount').value
	discount_value_p = 0.0;
	total_price = get_total_project_price();
	if (parseFloat(discount) > 0){
		discount_value_p = total_price * (discount/100);
		
	}
	return discount_value_p;
}

// This function calculates commision on estimate form page
// hard-coded to our current purpose.
function updateCommision() {

	
	
	c_d = get_commision_discount();
	$('commision_total_after_discount').innerHTML = formatCurrency(c_d);
	
	full_price = formatCurrency(get_total_project_price() - get_project_discount() + c_d);
	
	$('total').innerHTML = full_price;
	
}
function get_commision_discount(){
		var orig = 'estimate';

		var total_price = 0.0;	
	
		var f = $(orig + '_commision');
		num = parseFloat(f.value);
		
		total_price  = get_total_project_price() - get_project_discount();
		
	
	discount = $(orig +'_commision').value
	discount_value = 0.0;
	
	num = parseFloat(discount);
	if (num >0 ){
		discount_value = total_price * (discount/100);
	}
	return discount_value;
}

function get_commision_discount_discount_value(){
	var orig = 'estimate';

	var total_price = 0.0;
	
	total_price = get_commision_discount();
		
	discount = $(orig +'_commision_discount').value
	discount_value = 0.0;
	num = parseFloat(discount);
	if (num >0 ){
		discount_value = total_price * (discount/100);
	}
	return discount_value;
}


function get_commision_discount_value(){
		var orig = 'estimate';

		var total_price = 0.0;	
	
		var f = $(orig + '_commision');
		num = parseFloat(f.value);
		
		total_price = get_commision_discount();
	
	discount = $(orig +'_commision_discount').value
	discount_value = 0.0;
	num = parseFloat(discount);
	if (num >0 ){
		discount_value = total_price * (discount/100);
	}
	commision = eval(total_price) - eval(discount_value)
	return commision;
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function prepare_series(){
	brand_id = $('material_roof_brand_id').value != '' ? parseFloat($('material_roof_brand_id').value)  : 0;
	
	series = $('material_brand_serie_id');
	if (brand_id > 0){
		series.options.length = 0;
		series.options[0] = new Option('Updating...','0');
		new Ajax.Request('/job/update_series/'+brand_id, {asynchronous:true, evalScripts:true});
	}else{
		series.options.length = 0;
		series.options[0] = new Option('<Select Brand...>','0');
	}
}


function add_material(){
	$('_material_brand_id').value = $('material_roof_brand_id').value;
	$('_material_brand_serie_id').value = $('material_brand_serie_id').value;
	$('_material_color').value = $('material_color').value;
	
	form_to_submit = document.forms[1];
	
	new Ajax.Request('/job/add_material', {asynchronous:true, evalScripts:true, parameters:Form.serialize(form_to_submit)});
	
	$('material_roof_brand_id').value = '';
	$('material_color').value = ''
	$('material_brand_serie_id').options.length = 0;
	
}

