
// Validation to prevent "--" options being added to basket /
// and warn about invalid option combinations  /
function validateOptions() {
	var units = getObById('units');
	var eyes = units.options[units.options.selectedIndex].value;
	var opts = document.getElementsByTagName('select');
	var error = new Array;
	var skua = '';
	var skub = '';
	var incA = false;
	var incB = false;
	var emptyerror = false;
	var rightempty = false;
	var leftempty = false;
	var comboerror = false;
	var rightcombo = false;
	var leftcombo = false;
	var prefix = '';
	var eye = 0;
	ready = true;
	
	//process each option /
	for(var i=0;i<opts.length;i++) {
		var opt = opts[i];
		
		//check to ensure select is a product option / 
		if(opt.id && opt.id.indexOf('option') != -1) {
			opt.style.borderColor = '#dddddd';
			if(opt.className != 'disabled') {
				opt.style.backgroundColor = '#ffffff';
			}
			
			//set the eye prefix /
			if(eyes == 2) {
				if(opt.id.indexOf('optionB') == -1) {
					prefix = '<strong>Right Eye: </strong>';
					eye = 1;
					skua += buildAbbr(opt);
					// Get the power for right eye
					if(oh[opt.title].abbr == 'p') {
						var power1_value = opt.options[opt.options.selectedIndex].text;
						var power1 = opt;
					}
					// Get the basecurve for right eye
					if(oh[opt.title].abbr == 'bc') {
						var basecurve1_value = opt.options[opt.options.selectedIndex].text;
						var basecurve1 = opt;
					}
				} else {
					prefix = '<strong>Left Eye: </strong>';
					eye = 2;
					skub += buildAbbr(opt);
					// Get the power for left eye
					if(oh[opt.title].abbr == 'p') {
						var power2_value = opt.options[opt.options.selectedIndex].text;
						var power2 = opt;
					}
					// Get the basecurve for right eye
					if(oh[opt.title].abbr == 'bc') {
						var basecurve2_value = opt.options[opt.options.selectedIndex].text;
						var basecurve2 = opt;
					}
				}
			} else if(opt.id.indexOf('optionB') == -1) {
				prefix = '';
				eye = -1;
				skua += buildAbbr(opt);
			} else {
				eye = 0;
			}
			
			//check for blank option "--" /
			if(opt.options[opt.options.selectedIndex].text == "--") {
				colorError(opt.id,r,g,b);
				if((eye == -1 && emptyerror == false) || (eye == 1 && rightempty == false) || (eye == 2 && leftempty == false)) {
					error[error.length + 1] = (prefix + emptymsg + '<br />');
					emptyerror = true;
					if(eye == -1 || eye == 1) {
						rightempty = true;
					} else if(eye == 2) {
						leftempty = true;
					}
					ready = false;
				}
			}
		}
	}
	//end option loop /
	
	//If we don't use SKUs we can stop now /
	if(skulist == undefined || skulist.length == 0 || skulist == "no skus") {
		return feedback(ready,error);
	}
	
	//check for valid sku on first product /
	if(skulist.indexOf(skua) == -1 && rightempty == false) {
		//Color the active options /
		for(var i=0;i<opts.length;i++) {
			var opt = opts[i];
			if(opt.id && opt.id.indexOf('option') != -1 && opt.id.indexOf('optionB') == -1 && opt.className != 'disabled') {
				colorError(opt.id,r,g,b);
			}
		}
		//set the eye prefix /
		if(eyes == 2) {
			prefix = '<strong>Right Eye: </strong>';
		} else {
			prefix = '';
		}
		error[error.length + 1] = (prefix + combomsg + '<br />');
		ready = false;
	}
	
	//check for valid sku on second /
	if(skub != '' && skulist.indexOf(skub) == -1 && leftempty == false) {
		//Color the active options /
		for(var i=0;i<opts.length;i++) {
			var opt = opts[i];
			if(opt.id && opt.id.indexOf('optionB') != -1 && opt.className != 'disabled') {
				colorError(opt.id,r,g,b);
			}
		}
		//set the prefix /
		prefix = '<strong>Left Eye: </strong>';
		error[error.length + 1] = (prefix + combomsg + '<br />');
		ready = false;
	}
	//done /
	
	// If there is not already an error check the two powers and warn the customer that they are different
	if(ready != false && power1_value && power2_value && power1_value != '0.00' && power2_value != '0.00') {
		if(power1_value.substr(0, 1) != power2_value.substr(0, 1)) {
			if(confirm('Are you sure you want to order lenses with positive and negative powers?\nClick OK to add these lenses to your basket\nClick CANCEL to amend the powers you have selected')) {
				ready = true;
			}
			else {
				colorError(power1.id,r,g,b);
				colorError(power2.id,r,g,b);
				ready = false;
			}
		}	
	}
	// If there is not already an error check the two basecurves and warn the customer that they are the same
	if(ready != false && basecurve1_value && basecurve2_value) {
		if(basecurve1_value != basecurve2_value) {
			if(confirm('Are you sure you want to order lenses with different base curves?\nClick OK to add these lenses to your basket\nClick CANCEL to amend the base curves you have selected')) {
				ready = true;
			}
			else {
				colorError(basecurve1.id,r,g,b);
				colorError(basecurve2.id,r,g,b);
				ready = false;
			}
		}	
	}
	
	error = errorString(error);
	
	return feedback(ready,error);
	
}

//Handle output of errors / submit form /
function feedback(ready,error) {
	if(ready == true) {
		return true;
	} else {
		var errp = getObById('jsErrors');
		errp.innerHTML = error;
		errp.style.display = 'block';
		errp.style.width = '400px';
		return false;
	}
}

//Helper for building option name->abbr lookup hash object /
function okv(name,abbr) {
	this.name = name;
	this.abbr = abbr;
}

//Build option _abbreviate-value string
function buildAbbr(el) {
	if(el.title == 'Colour') {
		var osv = colourCodes[el.options[el.options.selectedIndex].text];
	} else {
		var osv = el.options[el.options.selectedIndex].text;
	}
	if(osv == '--') {
		osv = '';
	}
	var abval = "_" + oh[el.title].abbr + osv;
	return abval;
}

//Helper to sort errors and convert to string / 
function errorString(msg) {
	msg = msg.sort();
	msg = msg.reverse();
	msg = msg.join(" ");
	return msg;
}




