var G_config = new Array();
G_config['products'] = new Array();
G_config['finishing_batten'] = new Array();
G_config['finishing_braids'] = new Array();
G_config['finishing_pulls'] = new Array();
G_config['finishing_roman_braids_bottom'] = new Array();
G_config['finishing_roman_braids_bottom3'] = new Array();
G_config['costs'] = new Array();
G_config['fabric_weights'] = new Array();
G_config['cord_weights'] = new Array();

function displayRequestFields() {
	var div = document.getElementById('calcSendRequestDiv');
	var butt = document.getElementById('displayRequestFieldsButt');
	
	
	if(div.style.display != 'block') {
		butt.value = 'Cancel request';
		div.style.display = 'block';
	} else {
		butt.value = 'Send request';
		div.style.display = 'none';
	}
}

function addProduct(id) {
	document.getElementById('refId').value = id;
	document.getElementById('ref').value = G_config['products'][id]['name'];
}

function addBatten(id) {
	window.opener.document.getElementById('battenId').value = id;
	window.opener.document.getElementById('batten').value = G_config['finishing_batten'][id]['name'];
}

function addBraid(id) {
	window.opener.document.getElementById('braidId').value = id;
	window.opener.document.getElementById('braid').value = G_config['finishing_braids'][id]['name'];
}

function addPull(id) {
	window.opener.document.getElementById('pullId').value = id;
	window.opener.document.getElementById('pull').value = G_config['finishing_pulls'][id]['name'];
}

function addFabWeight(id) {
	window.opener.document.getElementById('fabweightId').value = id;
	window.opener.document.getElementById('fabweight').value = G_config['fabric_weights'][id]['name'];
}

function addCordWeight(id) {
	window.opener.document.getElementById('cordweightId').value = id;
	window.opener.document.getElementById('cordweight').value = G_config['cord_weights'][id]['name'];
}

function calculatePrice() {
	
	if(document.getElementById('refId')) {
		var productId = document.getElementById('refId').value;
		var productCat = G_config['products'][productId]['cat'];
	} else {
		var productCat = document.getElementById('productCat').value;
	}

	if(productCat != 'roman' & productCat != 'roller_blinds_laminated') {
		var fields = {'ref':	'Fabric style,mandatory,fabric_exists',
					  'width':	'Width,mandatory,numeric',
					  'depth':	'Depth,mandatory,numeric'};
	} else if(productCat == 'roller_blinds_laminated') {
		var fields = {'line':		'Laminate type,lineType',
					  'width':		'Width,mandatory,numeric',
					  'depth':		'Depth,mandatory,numeric'};
	} else {
		var fields = {'type':		'Type,romanType',
					  'line':		'Line type,lineType',
					  'width':		'Width,mandatory,numeric',
					  'depth':		'Depth,mandatory,numeric'};
	}
	var validFields =  validateFields(fields);
	
	if(!validFields) {
		return;
	} else {
		if(productCat != 'roman' && productCat != 'roller_blinds_laminated') {
			var productBand = G_config['products'][productId]['band'];
		} else if(productCat != 'roller_blinds_laminated') {
			for(var i = 0; i < document.getElementById('calc').type.length; i++) {
				if(document.getElementById('calc').type[i].checked) {
					var productType = document.getElementById('calc').type[i].value;
					break;
				}
			}
			for(var i = 0; i < document.getElementById('calc').line.length; i++) {
				if(document.getElementById('calc').line[i].checked) {
					var lineType = document.getElementById('calc').line[i].value;
					break;
				}
			}
			for(var i = 0; i < document.getElementById('calc').braidType.length; i++) {
				if(document.getElementById('calc').braidType[i].checked) {
					var braidType = document.getElementById('calc').braidType[i].value;
					break;
				}
			}
		}

		if(productCat == 'roller_blinds_laminated') {
			for(var i = 0; i < document.getElementById('calc').laminate.length; i++) {
				if(document.getElementById('calc').laminate[i].checked) {
					var productLamType = document.getElementById('calc').laminate[i].value;
					break;
				}
			}
		}

		var blindWidth = document.getElementById('width').value;
		var blindDepth = document.getElementById('depth').value;

		var batten = document.getElementById('battenId');
		var battenName = document.getElementById('batten');

		var braid = document.getElementById('braidId');
		var braidName = document.getElementById('braid');

		var pull = document.getElementById('pullId');
		var pullName = document.getElementById('pull');

		var contrastBorder = document.getElementById('cbb');
		var contrastBorder3 = document.getElementById('cbo3s');

		var extraFabWeight = document.getElementById('fabweightId');
		var extraFabWeightName = document.getElementById('fabweight');

		var extraCordWeight = document.getElementById('cordweightId');
		var extraCordWeightName = document.getElementById('cordweight');

		var childSafteyCheckbox = document.getElementById('childSafety');

		var totalCostField = document.getElementById('total_cost');
		var costField = document.getElementById('cost');
	
		var newBlindWidth, newBlindDepth, productCost, battenCost, braidCost, PullCost, conrastBorderCost, contrastBorder3Cost, extraFabWeightCost, extraCordWeightCost, childSafteyCost, totalCost;
	}
	
	if(productCat == 'roller_blinds') {
		if(blindWidth <= 635) {
			newBlindWidth = 635;
		} else if(blindWidth <= 762) {
			newBlindWidth = 762;
		} else if(blindWidth <= 889) {
			newBlindWidth = 889;
		} else if(blindWidth <= 1016) {
			newBlindWidth = 1016;
		} else if(blindWidth <= 1143) {
			newBlindWidth = 1143;
		} else if(blindWidth <= 1270) {
			newBlindWidth = 1270;
		} else if(blindWidth <= 1397) {
			newBlindWidth = 1397;
		} else if(blindWidth <= 1524) {
			newBlindWidth = 1524;
		} else if(blindWidth <= 1651) {
			newBlindWidth = 1651;
		} else if(blindWidth <= 1778) {
			newBlindWidth = 1778;
		} else if(blindWidth <= 1905) {
			newBlindWidth = 1905;
		} else if(blindWidth <= 2032) {
			newBlindWidth = 2032;
		} else if(blindWidth <= 2286) {
			newBlindWidth = 2286;
		} else {
			displayMaxWidthMessage('2286');
			return;
		}
		
		if(blindDepth <= 1016) {
			newBlindDepth = 1016;
		} else if(blindDepth <= 1270) {
			newBlindDepth = 1270;
		} else if(blindDepth <= 1600) {
			newBlindDepth = 1600;
		} else if(blindDepth <= 2032) {
			newBlindDepth = 2032;
		} else {
			displayMaxDepthMessage('2032');
			return;
		}
	} else if(productCat == 'vertical_blinds') {
		if(blindWidth <= 762) {
			newBlindWidth = 762;
		} else if(blindWidth <= 1016) {
			newBlindWidth = 1016;
		} else if(blindWidth <= 1270) {
			newBlindWidth = 1270;
		} else if(blindWidth <= 1524) {
			newBlindWidth = 1524;
		} else if(blindWidth <= 1778) {
			newBlindWidth = 1778;
		} else if(blindWidth <= 2032) {
			newBlindWidth = 2032;
		} else if(blindWidth <= 2286) {
			newBlindWidth = 2286;
		} else if(blindWidth <= 2540) {
			newBlindWidth = 2540;
		} else if(blindWidth <= 2794) {
			newBlindWidth = 2794;
		} else if(blindWidth <= 3048) {
			newBlindWidth = 3048;
		} else {
			displayMaxWidthMessage('3048');
			return;
		}
		
		if(blindDepth <= 1016) {
			newBlindDepth = 1016;
		} else if(blindDepth <= 1270) {
			newBlindDepth = 1270;
		} else if(blindDepth <= 1524) {
			newBlindDepth = 1524;
		} else if(blindDepth <= 1778) {
			newBlindDepth = 1778;
		} else if(blindDepth <= 2032) {
			newBlindDepth = 2032;
		} else if(blindDepth <= 2286) {
			newBlindDepth = 2286;
		} else if(blindDepth <= 2540) {
			newBlindDepth = 2540;
		} else if(blindWidth <= 3048) {
			newBlindDepth = 3048;
		} else {
			displayMaxDepthMessage('3048');
			return;
		}
	} else if(productCat == 'woven_blinds') {
		if(blindWidth <= 600) {
			newBlindWidth = 600;
		} else if(blindWidth <= 800) {
			newBlindWidth = 800;
		} else if(blindWidth <= 1000) {
			newBlindWidth = 1000;
		} else if(blindWidth <= 1200) {
			newBlindWidth = 1200;
		} else if(blindWidth <= 1400) {
			newBlindWidth = 1400;
		} else if(blindWidth <= 1600) {
			newBlindWidth = 1600;
		} else if(blindWidth <= 1800) {
			newBlindWidth = 1800;
		} else if(blindWidth <= 2000) {
			newBlindWidth = 2000;
		} else {
			displayMaxWidthMessage('2000');
			return;
		}

		if(blindDepth <= 600) {
			newBlindDepth = 600;
		} else if(blindDepth <= 800) {
			newBlindDepth = 800;
		} else if(blindDepth <= 1000) {
			newBlindDepth = 1000;
		} else if(blindDepth <= 1200) {
			newBlindDepth = 1200;
		} else if(blindDepth <= 1400) {
			newBlindDepth = 1400;
		} else if(blindDepth <= 1600) {
			newBlindDepth = 1600;
		} else if(blindDepth <= 1800) {
			newBlindDepth = 1800;
		} else if(blindDepth <= 2000) {
			newBlindDepth = 2000;
		} else if(blindDepth <= 2200) {
			newBlindDepth = 2200;
		} else if(blindDepth <= 2400) {
			newBlindDepth = 2400;
		} else {
			displayMaxDepthMessage('2400');
			return;
		}
	} else if(productCat.indexOf('venetion') != -1) {
		if(blindWidth <= 600) {
			newBlindWidth = 600;
		} else if(blindWidth <= 800) {
			newBlindWidth = 800;
		} else if(blindWidth <= 1000) {
			newBlindWidth = 1000;
		} else if(blindWidth <= 1200) {
			newBlindWidth = 1200;
		} else if(blindWidth <= 1400) {
			newBlindWidth = 1400;
		} else if(blindWidth <= 1600) {
			newBlindWidth = 1600;
		} else if(blindWidth <= 1800) {
			newBlindWidth = 1800;
		} else if(blindWidth <= 2000) {
			newBlindWidth = 2000;
		} else if(blindWidth <= 2200) {
			newBlindWidth = 2200;
		} else if(blindWidth <= 2400) {
			newBlindWidth = 2400;
		} else {
			displayMaxWidthMessage('2400');
			return;
		}

		if(blindDepth <= 500) {
			newBlindDepth = 500;
		} else if(blindDepth <= 750) {
			newBlindDepth = 750;
		} else if(blindDepth <= 1000) {
			newBlindDepth = 1000;
		} else if(blindDepth <= 1250) {
			newBlindDepth = 1250;
		} else if(blindDepth <= 1500) {
			newBlindDepth = 1500;
		} else if(blindDepth <= 1750) {
			newBlindDepth = 1750;
		} else if(blindDepth <= 2000) {
			newBlindDepth = 2000;
		} else if(blindDepth <= 2250) {
			newBlindDepth = 2250;
		} else if(blindDepth <= 2500) {
			newBlindDepth = 2500;
		} else {
			displayMaxDepthMessage('2500');
			return;
		}
	} else if(productCat == 'roller_blinds_laminated') {
		if(blindWidth <= 635) {
			newBlindWidth = 635;
		} else if(blindWidth <= 762) {
			newBlindWidth = 762;
		} else if(blindWidth <= 889) {
			newBlindWidth = 889;
		} else if(blindWidth <= 1016) {
			newBlindWidth = 1016;
		} else if(blindWidth <= 1270) {
			newBlindWidth = 1270;
		} else if(blindWidth <= 1397) {
			newBlindWidth = 1397;
		} else if(blindWidth <= 1524) {
			newBlindWidth = 1524;
		} else if(blindWidth <= 1651) {
			newBlindWidth = 1651;
		} else if(blindWidth <= 1778) {
			newBlindWidth = 1778;
		} else if(blindWidth <= 1905) {
			newBlindWidth = 1905;
		} else if(blindWidth <= 12032) {
			newBlindWidth = 2032;
		} else if(blindWidth <= 2286) {
			newBlindWidth = 2286;
		} else {
			displayMaxWidthMessage('2286');
			return;
		}

		if(blindDepth <= 1016) {
			newBlindDepth = 1016;
		} else if(blindDepth <= 1270) {
			newBlindDepth = 1270;
		} else if(blindDepth <= 1600) {
			newBlindDepth = 1600;
		} else if(blindDepth <= 2032) {
			newBlindDepth = 2032;
		} else {
			displayMaxDepthMessage('2032');
			return;
		}
	} else if(productCat == 'aw') {
		if(blindWidth <= 1000) {
			newBlindWidth = 1000;
		} else if(blindWidth <= 1200) {
			newBlindWidth = 1200;
		} else if(blindWidth <= 1400) {
			newBlindWidth = 1400;
		} else if(blindWidth <= 1600) {
			newBlindWidth = 1600;
		} else if(blindWidth <= 1800) {
			newBlindWidth = 1800;
		} else {
			displayMaxWidthMessage('1800');
			return;
		}

		if(blindDepth <= 1000) {
			newBlindDepth = 1000;
		} else if(blindDepth <= 1200) {
			newBlindDepth = 1200;
		} else if(blindDepth <= 1400) {
			newBlindDepth = 1400;
		} else if(blindDepth <= 1600) {
			newBlindDepth = 1600;
		} else if(blindDepth <= 1800) {
			newBlindDepth = 1800;
		} else if(blindDepth <= 2000) {
			newBlindDepth = 2000;
		} else if(blindDepth <= 2400) {
			newBlindDepth = 2400;
		} else if(blindDepth <= 2600) {
			newBlindDepth = 2600;
		} else if(blindDepth <= 3000) {
			newBlindDepth = 3000;
		} else {
			displayMaxDepthMessage('3000');
			return;
		}
	} else { // Roman blinds as no category
		if(blindWidth <= 610) {
			newBlindWidth = 610;
		} else if(blindWidth <= 914) {
			newBlindWidth = 914;
		} else if(blindWidth <= 1219) {
			newBlindWidth = 1219;
		} else if(blindWidth <= 1524) {
			newBlindWidth = 1524;
		} else if(blindWidth <= 1829) {
			newBlindWidth = 1829;
		} else if(blindWidth <= 2134) {
			newBlindWidth = 2134;
		} else {
			displayMaxWidthMessage('2134');
			return;
		}

		if(blindDepth <= 915) {
			newBlindDepth = 915;
		} else if(blindDepth <= 1219) {
			newBlindDepth = 1219;
		} else if(blindDepth <= 1524) {
			newBlindDepth = 1524;
		} else if(blindDepth <= 1829) {
			newBlindDepth = 1829;
		} else {
			displayMaxDepthMessage('1829');
			return;
		}
	}
	
	if(productCat != 'roman' && productCat != 'roller_blinds_laminated') {
		productCost = G_config['costs'][productCat]['bands'][productBand][newBlindWidth][newBlindDepth];
	} else if(productCat == 'roller_blinds_laminated') {
		productCost = G_config['costs'][productCat][productLamType][newBlindWidth][newBlindDepth];
	} else {
		productCost = G_config['costs'][productCat]['bands'][productType+'_'+lineType][newBlindDepth][newBlindWidth];
	}

	if(contrastBorder) {
		conrastBorderCost = (contrastBorder.checked) ? G_config['costs'][productCat]['contrast_border'][newBlindDepth][newBlindWidth] : 0;
	} else {
		conrastBorderCost = 0;
	}

	if(contrastBorder3) {
		conrastBorder3Cost = (contrastBorder3.checked) ? G_config['costs'][productCat]['contrast_border_3'][newBlindDepth][newBlindWidth] : 0;
	} else {
		conrastBorder3Cost = 0;
	}
		
	if(batten) {
		battenCost = (batten.value && battenName.value != '') ? G_config['finishing_batten'][batten.value]['cost'][newBlindWidth] : 0;
	} else {
		battenCost = 0;
	}

	if(braid) {
		if(productCat == 'roman') {
			if(braidType == 'bottom') {
				braidCost = (braid.value && braidName.value != '') ? G_config['finishing_roman_braids_bottom'][braid.value]['cost'][newBlindWidth] : 0;
			} else {
				braidCost = (braid.value && braidName.value != '') ? G_config['finishing_roman_braids_bottom3'][braid.value]['cost'][newBlindDepth][newBlindWidth] : 0;
			}
		} else {
			braidCost = (braid.value && braidName.value != '') ? G_config['finishing_braids'][braid.value]['cost'][newBlindWidth] : 0;
		}
	} else {
		braidCost = 0;
	}

	if(pull) {
		pullCost = (pull.value && pullName.value != '') ? G_config['finishing_pulls'][pull.value]['cost'] : 0;
	} else {
		pullCost = 0;
	}
	
	if(extraFabWeight) {
		extraFabWeightCost = (extraFabWeight.value && extraFabWeightName.value != '') ? G_config['fabric_weights'][extraFabWeight.value]['cost'][newBlindWidth] : 0;
	} else {
		extraFabWeightCost = 0;
	}

	if(extraCordWeight) {
		extraCordWeightCost = (extraCordWeight.value && extraCordWeightName.value != '') ? G_config['cord_weights'][extraCordWeight.value]['cost'] : 0;
	} else {
		extraCordWeightCost = 0;
	}

	if(childSafteyCheckbox) {
		childSafteyCost = (childSafteyCheckbox.checked) ? '4.00' : 0;
	} else {
		childSafteyCost = 0;
	}

	
	totalCost = parseFloat(productCost)
			  + parseFloat(battenCost)
			  + parseFloat(braidCost)
			  + parseFloat(pullCost)
			  + parseFloat(extraFabWeightCost)
			  + parseFloat(extraCordWeightCost)
			  + parseFloat(childSafteyCost)
			  + parseFloat(conrastBorderCost)
			  + parseFloat(conrastBorder3Cost);
	
	totalCostField.value = totalCost.toFixed(2);
	costField.value = totalCost.toFixed(2)+' GBP';
}

function sendRequest() {
	if(document.getElementById('refId')) {
		var productCat = G_config['products'][document.getElementById('refId').value]['cat'];
	} else {
		var productCat = document.getElementById('productCat').value;
	}

	if(productCat != 'roman' && productCat != 'roller_blinds_laminated') {
		var fields = {'ref'			: 'Fabric style,mandatory,fabric_exists',
					  'width'		: 'Width,mandatory,numeric',
					  'depth'		: 'Depth,mandatory,numeric', 
					  'name'		: 'Name,mandatory',
					  'email'		: 'Email address,mandatory,email',
					  'telephone'	: 'Telephone number,mandatory'};
	} else if(productCat == 'roller_blinds_laminated') {
		var fields = {'line'		: 'Laminate type,lineType',
					  'width'		: 'Width,mandatory,numeric',
					  'depth'		: 'Depth,mandatory,numeric', 
					  'name'		: 'Name,mandatory',
					  'email'		: 'Email address,mandatory,email',
					  'telephone'	: 'Telephone number,mandatory'};
	} else {
		var fields = {'type':		'Type,romanType',
					  'line':		'Line type,lineType',
					  'width':		'Width,mandatory,numeric',
					  'depth':		'Depth,mandatory,numeric',
					  'name': 		'Name,mandatory',
					  'email': 		'Email address,mandatory,email',
					  'telephone': 	'Telephone number,mandatory'};
	}

	var valid = validateFields(fields);
	
	if(valid) {
		document.getElementById('calc').submit();
	}
}

function validateFields(fields) {
	var errorMsg = 'The following error(s) have occurred:'+"\n\n";
	var foptions, fn, fvalidation, error;
	var errors = '';
				  
	for(fid in fields) {
		foptions = fields[fid].split(',');
		fn = foptions.shift();
		fvalidation = foptions;
		fvalue = document.getElementById(fid).value;
		
		for(var i = 0; i < fvalidation.length; i++) {
			switch(fvalidation[i]) {
				case 'mandatory':
					error = v_mandatory(fn, fvalue);
					break
				case 'numeric':
					regex = /[0-9]+(\.[0-9]+)?/;
					error = v_regexp(regex, fn, fvalue);
					break;
				case 'email':
					regex = /^([-A-Z0-9_]+(\.[-A-Z0-9_]+)?)\@([-A-Z0-9\.]+\.[A-Z]+(\.[A-Z]+)?)$/i;
					error = v_regexp(regex, fn, fvalue);
					break;
				case 'fabric_exists':
					error = v_fabric_exists(fn, fvalue);
					break;
				case 'romanType':
					error = v_romanType(fn, fvalue);
					break;
				case 'lineType':
					error = v_lineType(fn, fvalue);
					break;
				case 'romanBraidType':
					error = v_romanBraidType(fn, fvalue);
					break;
			}
			
			if(error) {
				errors += ' - '+error+"\n";
				error = '';
				break;
			}
		}
	}
	
	if(errors) {
		alert(errorMsg+errors);
		return false;
	}
	
	return true;
}

function v_mandatory(fn, fvalue) {
	if(fvalue == '' || fvalue == '0') {
		return fn+' is required';
	}
	
	return;
}

function v_regexp(regexp, fn, fvalue) {
	if(!fvalue.match(regexp)) {
		return fn+' is invalid';
	}
	
	return;
}

function v_fabric_exists(fn, fvalue) {
	var refId = document.getElementById('refId').value;

	if(!G_config['products'][refId]) {
		return fn+' does not exists (select from list)';
	}
	
	return;
}

function v_romanType(fn, fvalue) {
	var form = document.getElementById('calc');

	for(var i = 0; i < form.type.length; i++) {
		if(form.type[i].checked) {
			return;
		}
	}

	return fn+' is required';
}

function v_lineType(fn, fvalue) {
	var form = document.getElementById('calc');

	for(var i = 0; i < form.line.length; i++) {
		if(form.line[i].checked) {
			return;
		}
	}

	return fn+' is required';
}

function v_romanBraidType(fn, fvalue) {
	var form = document.getElementById('calc');

	for(var i = 0; i < form.braidType.length; i++) {
		if(form.braidType[i].checked) {
			return;
		}
	}

	return fn+' is required';
}

function showHideExtra(extraId) {
	extraDiv = document.getElementById(extraId);

	if(extraDiv.style.display == 'block') {
		document.getElementById(extraId).style.display = 'none';
	} else {
		document.getElementById(extraId).style.display = 'block';
	}
}

function showHideFabric(div) {
	if(div == 'fbyDiv') {
		document.getElementById('fbyDiv').style.display = 'block';
		document.getElementById('fbuDiv').style.display = 'none';
	} else if(div == 'fbuDiv') {
		document.getElementById('fbyDiv').style.display = 'none';
		document.getElementById('fbuDiv').style.display = 'block';
	}
}

function showFabricType() {
	document.getElementById('fbuDiv').style.display = 'block';
}

function hideFabricType() {
	document.getElementById('fbuDiv').style.display = 'none';
}

function displayMaxWidthMessage(max_value) {
	alert('Your width extends the maximum of '+max_value+' allowed for this product.');
}

function displayMaxDepthMessage(max_value) {
	alert('Your depth extends the maximum of '+max_value+' allowed for this product.');
}