// JavaScript Document

//----------------- global variables
var calculator_type = 'fc_posters';
var media_dropdown_list = [1,2];
var lamination_dropdown_list = [0,3,4,7];
var mounting_dropdown_list = [0,2,3,1,5,8];
var finishing_colors_dropdown_list = [0,1,2,3,4,5,6];
var design_price = p_design;
var piece_price = p_piece;
var	adhesive = 0.5;

var calculate_permission = false;
var error_isNaN = true;
var sqft = 0;
var cost_per_sqft = 0;
var media_price = 0;
var cost_per_sqft_lam = 0;
var lamination_price = 0;
var cost_per_sqft_mount = 0;
var mounting_price = 0;
var finishing_price = 0;
var pair_quantities_price = 0;
var finishing_total_prices = new Array(0,0,0,0,0,0,0,0);

// checking for isNaN
function check_isNaN(){
	var checksum = true;
	if (isNaN(document.getElementById('quantity').value) == true){checksum = false; field = 'Quantity'; }
	if (isNaN(document.getElementById('sides').value) == true){checksum = false; field = 'Sides'; }
	if (isNaN(document.getElementById('width').value) == true){checksum = false; field = 'Width'; }
	if (isNaN(document.getElementById('length').value) == true){checksum = false; field = 'Length'; }
	if (isNaN(document.getElementById('designs').value) == true){checksum = false; field = 'Designs'; }
	if (isNaN(document.getElementById('quantity_trimcap').value) == true){checksum = false; field = 'Quantity for Trim Cap mounting tabs (pair)'; }
	if (isNaN(document.getElementById('quantity_markbrick').value) == true){checksum = false; field = 'Quantity for Mark Brick wall mounting clips (pair)'; }
	if (checksum == false){
		error_isNaN = true;
		document.getElementById('error_message').innerHTML='Incorrect ' + field;
	} else {error_isNaN = false;}
}

// checking for any fields conditions
function check_conditions(check_isNaN_error){
	if (!check_isNaN_error){
		calculate_permission = true;
		document.getElementById('error_message').innerHTML='';
		
		//--------- CONDITIONS -----------
		if (document.getElementById('quantity').value > 0){
			if ((document.getElementById('sides').value < 1) || (document.getElementById('sides').value > 2)){
				calculate_permission = false;
				document.getElementById('error_message').innerHTML='SIDES must be 1 or 2.';
			}
			if ((document.getElementById('width').value < 12) || (document.getElementById('width').value > 120)){
				calculate_permission = false;
				document.getElementById('error_message').innerHTML='WIDTH must be between 12 and 120.';
			}
			if (document.getElementById('length').value < 18){
				calculate_permission = false;
				document.getElementById('error_message').innerHTML='LENGTH must be 18 or greater.';
			}
			if (document.getElementById('designs').value < 1){
				calculate_permission = false;
				document.getElementById('error_message').innerHTML='DESIGNS must be 1 or greater.';
			}
		}
		/*if (document.getElementById('quantity_trimcap').value < 1){
			calculate_permission = false;
			document.getElementById('error_message').innerHTML='Quantity for Trim Cap mounting tabs (pair) must be 1 or greater.';
		}
		if (document.getElementById('quantity_markbrick').value < 1){
			calculate_permission = false;
			document.getElementById('error_message').innerHTML='Quantity for Mark Brick wall mounting clips (pair) must be 1 or greater.';
		}*/
		//-------------------------------
	}
}

// if finishing needs a color
function needColorOption(num){
	option = document.getElementById('finishing_option['+num+']').value;
	if (option == 3 || option == 4 || option == 6){
		document.getElementById('finishing_color['+num+']').disabled = '';
	} else {
		document.getElementById('finishing_color['+num+']').selectedIndex = 0;
		//document.getElementById('finishing_color['+num+']').disabled = 'disabled';
	}
	if (option == 5){
		quantity_trimcap = true;
	}
	if(option == 7){
		quantity_markbrick = true;
	}

}
// calculate finisihing
function finishing(num){
	switch(document.getElementById('finishing_option['+num+']').value){
		case '1': finishing_total_prices[num] = (parseInt(document.getElementById('width['+num+']').value) + parseInt(document.getElementById('length['+num+']').value))*2*document.getElementById('quantity['+num+']').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]; break;
		case '2': finishing_total_prices[num] = (parseInt(document.getElementById('width['+num+']').value) + parseInt(document.getElementById('length['+num+']').value))*2*document.getElementById('quantity['+num+']').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]; break;
		case '3': finishing_total_prices[num] = parseFloat((parseInt(document.getElementById('width['+num+']').value) + parseInt(document.getElementById('length['+num+']').value))*2*document.getElementById('quantity['+num+']').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]+(document.getElementById('quantity['+num+']').value)*finishing_constant[document.getElementById('finishing_option['+num+']').value]); break;
		case '4': finishing_total_prices[num] = parseFloat((parseInt(document.getElementById('width['+num+']').value) + parseInt(document.getElementById('length['+num+']').value))*2*document.getElementById('quantity['+num+']').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]+(document.getElementById('quantity['+num+']').value)*finishing_constant[document.getElementById('finishing_option['+num+']').value]); break;
		case '6': finishing_total_prices[num] = parseFloat((parseInt(document.getElementById('width['+num+']').value) + parseInt(document.getElementById('length['+num+']').value))*2*document.getElementById('quantity['+num+']').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]+(document.getElementById('quantity['+num+']').value)*finishing_constant[document.getElementById('finishing_option['+num+']').value]); break;
		//case '5': finishing_total_prices[num] = document.getElementById('quantity_trimcap').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]; break;
		//case '7': finishing_total_prices[num] = document.getElementById('quantity_markbrick').value*finishing_prices[document.getElementById('finishing_option['+num+']').value]; break;
	}
	finishing_price += finishing_total_prices[num];
}
	
// calculation
function calculate(){
	calculate_permission = false;
	check_isNaN(); // checking for isNaN
	check_conditions(error_isNaN);// checking for any fields conditions
	finishing_price = 0;
	finishing_total_prices[0,0,0,0,0,0,0,0];
	
	if (calculate_permission == true){
		//quantity, sides, width, length, designs addition of all 7 items
		sqft = 0;
		designs = 0;
		quantity = 0;
		sides = 0;
		twosides = false;
		quantity_trimcap = false;
		quantity_markbrick = false;
		
		if (document.getElementById('quantity').value != ''){
		if (document.getElementById('quantity').value > 0){
			sqft += (document.getElementById('quantity').value * document.getElementById('sides').value * document.getElementById('width').value * document.getElementById('length').value)/144;
			designs += parseInt(document.getElementById('designs').value);
			sides += parseInt(document.getElementById('sides').value);
			if (parseInt(document.getElementById('sides').value) > 1){twosides = true;}
			if (document.getElementById('mounting').value != 0){
				//if (document.getElementById('finishing_option').value != 0){
					needColorOption(i);
					finishing(i);
				//}
			}
		quantity += parseInt(document.getElementById('quantity').value);
		}
		}
		
		//full color --> POSTERS&TRADE SHOWS <--
		//if (quantity_trimcap == true){document.getElementById('quantity_trimcap').disabled = '';} else {document.getElementById('quantity_trimcap').disabled = 'disabled';}
		//if (quantity_markbrick == true){document.getElementById('quantity_markbrick').disabled = '';} else {document.getElementById('quantity_markbrick').disabled = 'disabled';}
		if (quantity_trimcap == true){document.getElementById('quantity_trimcap').readonly = 'no';} else {document.getElementById('quantity_trimcap').readonly = 'yss';}
		if (quantity_markbrick == true){document.getElementById('quantity_markbrick').readonly = 'no';} else {document.getElementById('quantity_markbrick').readonly = 'yes';}
		
		//finding cost per sqft for media
		for (var i=1; i<=8; i++){
			if (bp_media[media_id[document.getElementById('material').value].group][i] > sqft){
				//cost_per_sqft = media[document.getElementById('material').value][i-1];
				cost_per_sqft = media[media_id[document.getElementById('material').value].group][i-1];
				break;
			} else if (bp_media[media_id[document.getElementById('material').value].group][i] < sqft){cost_per_sqft = media[media_id[document.getElementById('material').value].group][i];}
		}
		//calculating MIN price for media
		if ((cost_per_sqft*sqft+media[media_id[document.getElementById('material').value].group]['setup']) < media[media_id[document.getElementById('material').value].group]['min']){
			media_price = media[media_id[document.getElementById('material').value].group]['min'];
		} else{media_price = cost_per_sqft*sqft+media[media_id[document.getElementById('material').value].group]['setup'];}

		//finding cost per sqft for lamination
		//if (document.getElementById('lamination').value != -1){
		if (lamination_id[document.getElementById('lamination').value].group != -1){
			for (var i=1; i<=8; i++){
				if (bp_lamination[lamination_id[document.getElementById('lamination').value].group][i] > sqft){
					cost_per_sqft_lam = lamination[lamination_id[document.getElementById('lamination').value].group][i-1];
					break;
				} else if (bp_lamination[lamination_id[document.getElementById('lamination').value].group][i] < sqft){cost_per_sqft_lam = lamination[lamination_id[document.getElementById('lamination').value].group][i];}
			}
			//calculating MIN price for lamination
			if ((cost_per_sqft_lam*sqft+lamination[lamination_id[document.getElementById('lamination').value].group]['setup']) < lamination[lamination_id[document.getElementById('lamination').value].group]['min']){
				lamination_price = lamination[lamination_id[document.getElementById('lamination').value].group]['min'];
			} else{lamination_price = cost_per_sqft_lam*sqft+lamination[lamination_id[document.getElementById('lamination').value].group]['setup'];}
		} else {lamination_price = 0;}
		
		//finding cost per sqft for mounting
		//if (document.getElementById('mounting').value != -1){
		if (mounting_id[document.getElementById('mounting').value].group != -1){
			for (var i=1; i<=8; i++){
				if (bp_mounting[mounting_id[document.getElementById('mounting').value].group][i] > sqft){
					cost_per_sqft_mount = mounting[mounting_id[document.getElementById('mounting').value].group][i-1];
					break;
				} else if (bp_mounting[mounting_id[document.getElementById('mounting').value].group][i] < sqft){cost_per_sqft_mount = mounting[mounting_id[document.getElementById('mounting').value].group][i];}
			}
			if (twosides == true){
				cost_per_sqft_mount = (cost_per_sqft_mount/2)+0.5+0.25+adhesive;
			} else {cost_per_sqft_mount += adhesive;}
			//calculating MIN price for mounting
			if ((cost_per_sqft_mount*sqft+mounting[mounting_id[document.getElementById('mounting').value].group]['setup']) < mounting[mounting_id[document.getElementById('mounting').value].group]['min']){
				mounting_price = mounting[mounting_id[document.getElementById('mounting').value].group]['min'];
			} else{mounting_price = cost_per_sqft_mount*sqft+mounting[mounting_id[document.getElementById('mounting').value].group]['setup'];}
			// any item with two sides?
		} else {mounting_price = 0;}
		
		
		pair_quantities_price = document.getElementById('quantity_trimcap').value*finishing_prices[5] + document.getElementById('quantity_markbrick').value*finishing_prices[7];
		
		//calculating subtotal
		if (quantity != 0){
		material = Math.round( (media_price + lamination_price + mounting_price + piece_price*quantity + design_price*(designs-1) + pair_quantities_price) *100)/100;
		//output material
		document.calc_form.amount_subtotal.value = print_round(material);
		//output finishing
		document.calc_form.amount_finishing.value = print_round(Math.round(finishing_price*100)/100);
		//output total
		document.calc_form.amount_total.value = print_round(Math.round((finishing_price+material)*100)/100);
		} else {
			document.calc_form.amount_subtotal.value = 0;
			document.calc_form.amount_finishing.value = 0;
			document.calc_form.amount_total.value = 0;
			calculate_permission = false;
		}

		//alert if 2 sides item and no selected mounting
		if ((twosides == true) && (document.getElementById('mounting').value == -1)){
			document.getElementById('mounting').selectedIndex = 0;
			document.getElementById('error_message').innerHTML='There is a 2 sides item. Mounting is a must.';
			calculate_permission = false;
		}
	}

	//------- print values --------
	//window.status = pair_quantities_price;
return calculate_permission;
}
