// JavaScript Document

// These functions power the quote generation section of the quote tool

function roundUp(number_to_round,type) {
	var rounded = Math.round(number_to_round / 100) * 100; // create a variable containing the number rounded DOWN
	var rounded_plus_ten = rounded + 10; //create a variable containing the  rounded down figure adding 10
	if (number_to_round <= rounded_plus_ten) {
		return rounded;
	}
	if (number_to_round > rounded) {
		rounded = rounded + 100;
		return rounded;
	}
}

function getPrice(the_width, the_drop) {
	width_array_position = the_width - 500;
	width_array_position = width_array_position / 100;
	drop_array_position = the_drop - 500;
	drop_array_position = drop_array_position / 100;
	price_array = window['blind_price_table_' + blind_type];
	//alert('blind_price_table_' + blind_type);
	blind_price = price_array[drop_array_position][width_array_position];
	// PREVIOUSLY ADDED ADDITIONAL UPGRADE PRICE HERE BUT THIS INTERFERRED WITH THE CHECKING OF VALID SIZINGS
	//FEATURE MOVED TO WRITE QUOTE.JS
}

function getStackedHeight(drop) {
	drop_array_position = drop - 500;
	drop_array_position = drop_array_position / 100;
	if (blind_type == 'aluminium_25mm' || blind_type == 'aluminium_50mm' || blind_type == 'roller_ot_blockout' || blind_type == 'roller_ot_sunfilter_classic' || blind_type == 'roller_ot_sunfilter_luxury' || blind_type == 'roller_ot_sunscreen' || blind_type == 'roller_reg_blockout' || blind_type == 'roller_reg_sunfilter_classic' || blind_type == 'roller_reg_sunfilter_luxury' || blind_type == 'roller_reg_sunscreen' || blind_type == 'roller_reg_blockout_designer' || blind_type == 'roller_reg_sunfilter_designer') { // Handling blinds where a stacking height is not applicable such as aluminium
		stacked_height = 'n/a';
	} else {
		stacked_height = blind_stacked_height[drop_array_position];
	}
}

function getQuote(form)
{
	blind_width = testForNumber(form.width.value);
	blind_drop = testForNumber(form.drop.value);
	var blind_quote_error;
	for (var i = 0; i < 5; i++) {
		if (i == 0) {
			if (!blind_width || !blind_drop){
				blind_quote_error = blind_quote_error + "Please enter a numerical blind width and drop in millimetres. ";
				break;
				}
		}
		if (i == 1) {
			if (blind_width < 500) {
				blind_quote_error = blind_quote_error + "Blinds less wide than 500mm are a specialist size, please contact our sales team on 0800 483 638, who will be able to offer you professional advice. ";
			}
		}
		if (i == 2){
			if (blind_width > 3000) {
				blind_quote_error = blind_quote_error + "Blinds wider than 3000mm are a specialist size, please contact our sales team on 0800 483 638, who will be able to offer you professional advice. ";
				}
		}
		if (i == 3) {
			if (blind_drop < 500) {
				blind_quote_error = blind_quote_error + "Blinds with a drop less than 500mm are a specialist size, please contact our sales team on 0800 483 638, who will be able to offer you professional advice. ";
			}
		}
		if (i == 4){
			if (blind_drop > 3000) {
				blind_quote_error = blind_quote_error + "Blinds with a drop longer than 3000mm are a specialist size, please contact our sales team on 0800 483 638, who will be able to offer you professional advice. ";
				}
		}
	}

	if (!blind_quote_error) {
		killError(4);
		round_width = roundUp(blind_width,'width');
		round_drop = roundUp(blind_drop,'drop');
		the_quote = getPrice(round_width,round_drop);
		getStackedHeight(round_drop);
		//alert(blind_price);
		if (the_quote != false && blind_price != 0) {
			// create an array of the users selections
			blind_array = finalBlindRowValidation();
			if (blind_array != false) {
				// Add the current array to the stack
				addToBlindStack(blind_array);
		
				if (blind_stack.length < 0) {
					writeQuoteTableRow(0);
				} else {
					writeQuoteTableRow(blind_stack.length -1);
				}
				initialiseWorkAreas(); initialiseSwatches(); reInitialiseGlobals(); // CLEAR EVERYTHING TO START THE NEXT BLIND
				nextStep(4);
			}
		} else {
			errorCheck(100,"This type of blind is not manufactured at this size. Please contact our sales team on 0800 483 638, who will be able to offer you professional advice.");
		}
		
	} else {
		blind_quote_error = removeUndefined(blind_quote_error);
		errorCheck(100,blind_quote_error);
	}
}