

function exceptionData(name, message) {
	this.name = name;
	this.message = message;
}

function checkNum(value, name) {
	var e = new exceptionData('nan', name + ' - niewłaściwy format.');
	if (!value || value == "") throw(e);
	value = value.replace(/,/,".");
	if (isNaN(value)) throw e;
	return parseFloat(value);
}

function checkRange(value, minimum, maximum, name) {
	if (value < minimum || value > maximum) {
		var e = new exceptionData('range', name + ' poza zakresem: ' + minimum +  ' - ' + maximum + '.');
		throw e;
	}
}

sizeCalc = {

	hideErrMsg : function() {

		var rightCol = document.getElementById('rightCol');
		var msgDiv = document.getElementById('sizeCalcErrMsg');

		if (msgDiv) rightCol.removeChild(msgDiv);
	},

	exceptionHandler : function(e) {

		sizeCalc.hideErrMsg();

		var fullMessage = '';

		if (e.name == 'nan') fullMessage = e.message + ' Popraw dane i przelicz ponownie.';
		else if (e.name == 'range') fullMessage = e.message + ' ';
		else fullMessage = e.message;

		var rightCol = document.getElementById('rightCol');
		var innerDiv = document.createElement('div');
		var outerDiv = document.createElement('div');
		var text = document.createTextNode(fullMessage);

		innerDiv.appendChild(text);
		outerDiv.className = 'errMsg';
		outerDiv.setAttribute('id', 'sizeCalcErrMsg');
		outerDiv.appendChild(innerDiv);
		rightCol.appendChild(outerDiv);
	},

	braSize : function(overBreast, underBreast) {

		checkRange(overBreast = checkNum(overBreast, 'Liczba "obwód klatki piersiowej w biuście  (cm)"'), 73, 154, 'Liczba "obwód klatki piersiowej w biuście  (cm)"');
		checkRange(underBreast = checkNum(underBreast, 'Liczba "obwód klatki piersiowej pod biustem  (cm)"'), 63, 127, 'Liczba "obwód klatki piersiowej pod biustem  (cm)"');

		var size = Math.round(underBreast / 5) * 5;

		var difference = Math.round(overBreast - size);
		checkRange(difference, 6, 22, 'Różnica w rozmiarze');

		if (difference <= 13) cup = "A";
		if (difference >= 14) cup = "B";
		if (difference >= 16) cup = "C";
		if (difference >= 18) cup = "D";
		if (difference >= 20) cup = "E";

		return size + ' ' + cup;
	},

	pantiesSize : function(waist) {

		checkRange(waist = checkNum(waist, 'Liczba "obwód w biodrach"'), 84, 150, 'Liczba "obwód w biodrach"');

		waist = Math.round(waist);
   
		if (waist >= 84 && waist <= 87) size = "32 / XS";
		if (waist >= 88 && waist <= 91) size = "34 / XS";
		if (waist >= 92 && waist <= 95) size = "36 / S";
		if (waist >= 96 && waist <= 98) size = "38 / S";
		if (waist >= 99 && waist <= 101) size = "40 / M";
		if (waist >= 102 && waist <= 104) size = "42 / M";
		if (waist >= 105 && waist <= 108) size = "44 / L";
		if (waist >= 109 && waist <= 112) size = "46 / L";
		if (waist >= 113 && waist <= 116) size = "48 / XL";
		if (waist >= 117 && waist <= 121) size = "50 / XL";
		if (waist >= 122 && waist <= 126) size = "52 / XXL";
		if (waist >= 127 && waist <= 132) size = "54 / XXL";
		if (waist >= 133 && waist <= 138) size = "56 / XXXL";            
		if (waist >= 139 && waist <= 144) size = "58 / XXXL";
		if (waist >= 145 && waist <= 150) size = "60 / XXXL";

		return size;
	},

	socksSize : function(footLength) {
 
		checkRange(footLength = checkNum(footLength, 'Liczba "długość stopy (cm)"'), 10.4, 32.3, 'Liczba "długość stopy (cm)"');

		if (footLength <= 11) size = "18";
		if (footLength >= 11.1) size = "19";  
		if (footLength >= 11.7) size = "20"; 
		if (footLength >= 12.4) size = "21"; 
		if (footLength >= 13.1) size = "22"; 
		if (footLength >= 13.8) size = "23"; 
		if (footLength >= 14.4) size = "24"; 
		if (footLength >= 15.0) size = "25"; 
		if (footLength >= 15.6) size = "26"; 
		if (footLength >= 16.3) size = "27"; 
		if (footLength >= 16.9) size = "28"; 
		if (footLength >= 17.5) size = "29"; 
		if (footLength >= 18.2) size = "30"; 
		if (footLength >= 18.8) size = "31"; 
		if (footLength >= 19.5) size = "32"; 
		if (footLength >= 20.2) size = "33"; 
		if (footLength >= 20.8) size = "34"; 
		if (footLength >= 21.5) size = "35"; 
		if (footLength >= 22.4) size = "36"; 
		if (footLength >= 23.1) size = "37"; 
		if (footLength >= 23.7) size = "38"; 
		if (footLength >= 24.4) size = "39"; 
		if (footLength >= 25.1) size = "40";
		if (footLength >= 25.8) size = "41"; 
		if (footLength >= 26.5) size = "42"; 
		if (footLength >= 27.1) size = "43"; 
		if (footLength >= 27.8) size = "44"; 
		if (footLength >= 28.4) size = "45"; 
		if (footLength >= 29.1) size = "46"; 
		if (footLength >= 29.8) size = "47"; 
		if (footLength >= 30.4) size = "48"; 
		if (footLength >= 31.1) size = "49";
		if (footLength >= 31.7) size = "50";  

		return size;
	},

	hosierySize : function(height, weight) {

		checkRange(height = checkNum(height, 'Liczba "wzrost"'), 130, 190, 'Liczba "wzrost"');
		checkRange(weight = checkNum(weight, 'Liczba "waga"'), 50, 99, 'Liczba "waga"');
  
		var size = sizeCalc.undefSign;

		var e = new exceptionData('range', 'Podana wartość nie mieści się w tabeli rozmiarów.');
		
		if (weight <= 55) {
			if (height >= 156 && height <= 170) size = "S";
			else if (height >= 171 && height <= 180) size = "M";
			else throw e;
		} else if (weight >= 56 && weight <= 60) {
			if (height <= 170) size = "S";
			else if (height >= 171 && height <= 180) size = "M";
			else throw e;
		} else if (weight >= 61 && weight <= 65) {
			if (height <= 165) size = "S";
			else if (height >= 166 && height <= 175) size = "M";
			else if (height >= 176 && height <= 185) size = "L";
			else throw e;
		} else if (weight >= 66 && weight <= 70) {
			if (height <= 155) size = "S";
			else if (height >= 156 && height <= 175) size = "M";
			else if (height >= 176 && height <= 185) size = "L";
			else throw e;
		} else if (weight >= 71 && weight <= 75) {
			if (height >= 151 && height <= 170) size = "M";
			else if (height >= 171 && height <= 180) size = "L";
			else if (height >= 181) size = "XL";
			else throw e;
		} else if (weight >= 76 && weight <= 80) {
			if (height >= 151 && height <= 155) size = "M";
			else if (height >= 156 && height <= 180) size = "L";
			else if (height >= 181) size = "XL";
			else throw e;
		} else if (weight >= 81 && weight <= 90) {
			if (height >= 156 && height <= 170) size = "L";
			else if (height >= 171) size = "XL";
			else throw e;
		} else if (weight >= 91){
			if (height >= 161) size = "XL";
			else throw e;
		} else throw e;

		return size;
	},

	underwearExec : function(e) {
		e = evt.fix(e);
		sizeCalc.hideErrMsg();
		try {
			document.getElementById('braSize').value = sizeCalc.braSize(
				document.getElementById('overBreast').value,
				document.getElementById('underBreast').value);
			document.getElementById('pantiesSize').value = sizeCalc.pantiesSize(
				document.getElementById('waist').value);
		}
		catch (exc) {
			sizeCalc.exceptionHandler(exc);
		}
		e.preventDefault();
	},

	socksExec : function(e) {
		e = evt.fix(e);
		sizeCalc.hideErrMsg();
		try {
			document.getElementById('socksSize').value = sizeCalc.socksSize(
				document.getElementById('footLength').value);
		}
		catch (exc) {
			sizeCalc.exceptionHandler(exc);
		}
		e.preventDefault();
	},

	hosieryExec : function(e) {
		e = evt.fix(e);
		sizeCalc.hideErrMsg();
		try {
			document.getElementById('hosierySize').value = sizeCalc.hosierySize(
				document.getElementById('height').value,
				document.getElementById('weight').value);
		}
		catch (exc) {
			sizeCalc.exceptionHandler(exc);
		}
		e.preventDefault();
	},

	init : function() {
		evt.add(document.getElementById('underwearTrigger'), 'click', sizeCalc.underwearExec);
		evt.add(document.getElementById('socksTrigger'), 'click', sizeCalc.socksExec);
		evt.add(document.getElementById('hosieryTrigger'), 'click', sizeCalc.hosieryExec);
	}
}

evt.add(window, 'load', sizeCalc.init);


