function swapCalculationPresets()
{
	var type_cm = document.getElementById('type_cm');
	var type_inc = document.getElementById('type_inc');

	var calcType = "cm";

	var neck = document.getElementById('neck');
	var chest = document.getElementById('chest');
	var length = document.getElementById('length');
	var recomended = document.getElementById('recomended');

	var typeLabel = document.getElementById('measurementtype');

	if(type_cm.checked == true)
	{
		typeLabel.innerHTML = "<b>Cm</b>";
		calculationType = "cm";
		returnRecomendedSize(neck.value,chest.value,length.value,"cm");
	}
	if(type_inc.checked == true)
	{
		typeLabel.innerHTML = "<b>Inches</b>";
		calculationType = "inc";
		returnRecomendedSize(neck.value,chest.value,length.value,"inc");
	}
	//document.getElementById('neck').focus();
}


function toggleDisplay(divId)
{
	if(divId == "overallsInfo")
	{
		$('#otherItems').fadeOut('fast',function()
		{
			$('#overallsInfo').fadeIn('fast');
		});
	}
	if(divId == "otherItems")
	{
		$('#overallsInfo').fadeOut('fast',function()
		{
			$('#otherItems').fadeIn('fast');
		});
	}

	generateTabsMenu(divId);
}

function generateTabsMenu(selected)
{
	var tabs = ["otherItems","overallsInfo"];
	var titles = ["Other items","Overalls" ];

	document.getElementById('MenuContainer').innerHTML = "";
	for(t = 0; t < tabs.length; t++)
	{
		var tab = document.createElement("div");
		tab.setAttribute("id",tabs[t]+"_tab");
		tab.setAttribute("title",titles[t]);

		if(selected == tabs[t])
		{
			tab.setAttribute("class","ItemSelected");
			tab.setAttribute("onMouseOver","this.className='ItemSelectedOver';");
			tab.setAttribute("onMouseOut","this.className='ItemSelected';");
		}else
		{
			tab.setAttribute("class","Item");
			tab.setAttribute("onMouseOver","this.className='ItemOver';");
			tab.setAttribute("onMouseOut","this.className='Item';");
		}
		tab.setAttribute("onClick","toggleDisplay('"+tabs[t]+"');");
		tab.innerHTML = titles[t];
		document.getElementById("MenuContainer").appendChild(tab);
	}

	document.getElementById('clothingtype').value = selected;
}

/*
	@author		Helgi P - helgi@nethonnun.is
	@date		22.01.10
	@desc		Fall sem skilar niðurstöðu um stærð eftir neck, length eða chesc.  Finnur út hvaða mælingu á að taka mark á og skilar stærð eftir því.
	@param		INT neckValue
	@param		INT chestValue
	@param		INT lengthValue
	@param		STR type (inc eða cm)
*/
function returnRecomendedSize(neckValue, chestValue, lengthValue,type)
{
	var whoIsBiggest = "";
	var rSize = "";

	var clothingtype = document.getElementById('clothingtype').value;

	neckValue = parseFloat(neckValue.replace(",","."));
	chestValue = parseFloat(chestValue.replace(",","."));
	lengthValue = parseFloat(lengthValue.replace(",","."));

	if(isNaN(neckValue)) {
		neckValue = 0;
	}
	if(isNaN(chestValue)) {
		chestValue = 0;
	}
	if(isNaN(lengthValue)) {
		lengthValue = 0;
	}
	//alert(neckValue+' '+chestValue+' '+lengthValue);
	/*if(neckValue > lengthValue && neckValue > chestValue)
	{
		whoIsBiggest = "neck";
	}
	if(chestValue >= neckValue && chestValue > lengthValue)
	{
		whoIsBiggest = "chest";
	}
	if(lengthValue >= chestValue && lengthValue > neckValue)
	{
		whoIsBiggest = "length";
	}*/
	var sizeValue = new Array();
	sizeValue['XS'] = 1;
	sizeValue['S'] = 2;
	sizeValue['M'] = 3;
	sizeValue['L'] = 4;
	sizeValue['XL'] = 5;
	sizeValue['2XL'] = 6;
	sizeValue['3XL'] = 7;
	sizeValue['4XL'] = 8;
	sizeValue['5XL'] = 9;
	//
	//alert(whoIsBiggest);
	if(type == "cm")
	{
		//
//		if(whoIsBiggest == "neck")
//		{
			if(neckValue <= 22)
			{
				nSize = "XS";
			}
			if(neckValue >= 23 && neckValue <= 26)
			{
				nSize = "S";
			}
			if(neckValue >= 27 && neckValue <= 30)
			{
				nSize = "M";
			}
			if(neckValue >= 31 && neckValue <= 34)
			{
				nSize = "L";
			}
			if(neckValue >= 35 && neckValue <= 38)
			{
				nSize = "XL";
			}

			if(neckValue >= 39 && neckValue <= 42)
			{
				nSize = "2XL";
			}

			if(neckValue >= 43 && neckValue <= 46)
			{
				nSize = "3XL";
			}

			if(neckValue >= 47 && neckValue <= 50)
			{
				nSize = "4XL";
			}
			if(neckValue >= 51)
			{
				nSize = "5XL";
			}
//			document.getElementById('recomended').value = rSize;
//		}
//		//
//		if(whoIsBiggest == "chest")
//		{
			if(chestValue <= 28 )
			{
				cSize = "XS";
			}
			if(chestValue >= 29 && chestValue <= 34)
			{
				cSize = "S";
			}
			if(chestValue >= 35 && chestValue <= 40)
			{
				cSize = "M";
			}
			if(chestValue >= 41 && chestValue <= 48)
			{
				cSize = "L";
			}
			if(chestValue >= 49 && chestValue <= 58)
			{
				cSize = "XL";
			}
			if(chestValue >= 59 && chestValue <= 68)
			{
				cSize = "2XL";
			}
			if(chestValue >= 69 && chestValue <= 78)
			{
				cSize = "3XL";
			}
			if(chestValue >= 79 && chestValue <= 88)
			{
				cSize = "4XL";
			}
			if(chestValue >= 89)
			{
				cSize = "5XL";
			}
//			document.getElementById('recomended').value = rSize;
//		}
//		//
//		if(whoIsBiggest == "length")
//		{
			if(lengthValue <= 20)
			{
				lSize = "XS";
			}
			if(lengthValue >= 21 && lengthValue <= 25)
			{
				lSize = "S";
			}
			if(lengthValue >= 26 && lengthValue <= 30)
			{
				lSize = "M";
			}
			if(lengthValue >= 31 && lengthValue <= 35)
			{
				lSize = "L";
			}
			if(clothingtype == "overallsInfo")
			{
				if(lengthValue >= 36 && lengthValue <= 41)
				{
					lSize = "XL";
				}
				if(lengthValue >= 42 && lengthValue <= 47)
				{
					lSize = "2XL";
				}
				if(lengthValue >= 48 && lengthValue <= 52)
				{
					lSize = "3XL";
				}
				if(lengthValue >= 53 && lengthValue <= 57)
				{
					lSize = "4XL";
				}
				if(lengthValue >= 58)
				{
					lSize = "5XL";
				}
			}
			if(clothingtype == "otherItems")
			{
				if(lengthValue >= 36 && lengthValue <= 41)
				{
					lSize = "XL";
				}
				if(lengthValue >= 42 && lengthValue <= 48)
				{
					lSize = "2XL";
				}
				if(lengthValue >= 49 && lengthValue <= 56)
				{
					lSize = "3XL";
				}
				if(lengthValue >= 57 && lengthValue <= 66)
				{
					lSize = "4XL";
				}
				if(lengthValue >= 67)
				{
					lSize = "5XL";
				}
			}
//			document.getElementById('recomended').value = rSize;
		}
//	}
	//
	if(type == "inc")
	{
		//
//		if(whoIsBiggest == "neck")
//		{
			if(neckValue <= 8.9)
			{
				nSize = "XS";
			}
			if(neckValue >= 9 && neckValue <= 10.4)
			{
				nSize = "S";
			}
			if(neckValue >= 10.5 && neckValue <= 12)
			{
				nSize = "M";
			}
			if(neckValue >= 12.1 && neckValue <= 13.6)
			{
				nSize = "L";
			}

			if(neckValue >= 13.7 && neckValue <= 15.2)
			{
				nSize = "XL";
			}
			if(neckValue >= 15.3 && neckValue <= 16.7)
			{
				nSize = "2XL";
			}
			if(neckValue >= 16.8 && neckValue <= 18.3)
			{
				nSize = "3XL";
			}
			if(neckValue >= 18.4 && neckValue <= 19.8)
			{
				nSize = "4XL";
			}
			if(neckValue >= 19.9)
			{
				nSize = "5XL";
			}
//			document.getElementById('recomended').value = rSize;
//		}
//		//
//		if(whoIsBiggest == "chest")
//		{
			if(chestValue <= 11.2)
			{
				cSize = "XS";
			}
			if(chestValue >= 11.3 && chestValue <= 13.6)
			{
				cSize = "S";
			}
			if(chestValue >= 13.7 && chestValue <= 15.9)
			{
				cSize = "M";
			}
			if(chestValue >= 16 && chestValue <= 19.1)
			{
				cSize = "L";
			}
			if(chestValue >= 19.2 && chestValue <= 23)
			{
				cSize = "XL";
			}
			if(chestValue >= 23.1 && chestValue <= 27)
			{
				cSize = "2XL";
			}
			if(chestValue >= 27.1 && chestValue <= 30.9)
			{
				cSize = "3XL";
			}
			if(chestValue >= 31 && chestValue <= 34.8)
			{
				cSize = "4XL";
			}
			if(chestValue >= 34.9)
			{
				cSize = "5XL";
			}
//			document.getElementById('recomended').value = rSize;
//		}
//		//
//		if(whoIsBiggest == "length")
//		{
			if(lengthValue <= 8)
			{
				lSize = "XS";
			}
			if(lengthValue >= 8.1 && lengthValue <= 10)
			{
				lSize = "S";
			}
			if(lengthValue >= 10.1 && lengthValue <= 12)
			{
				lSize = "M";
			}
			if(lengthValue >= 12.1 && lengthValue <= 14)
			{
				lSize = "L";
			}
			if(clothingtype == "overallsInfo")
			{
				if(lengthValue >= 14.1 && lengthValue <= 16.3)
				{
					lSize = "XL";
				}
				if(lengthValue >= 16.4 && lengthValue <= 18.7)
				{
					lSize = "2XL";
				}
				if(lengthValue >= 18.8 && lengthValue <= 20.6)
				{
					lSize = "3XL";
				}
				if(lengthValue >= 20.7 && lengthValue <= 22.6)
				{
					lSize = "4XL";
				}
				if(lengthValue >= 22.7)
				{
					lSize = "5XL";
				}
			}
			if(clothingtype == "otherItems")
			{
				if(lengthValue >= 14.1 && lengthValue <= 16.3)
				{
					lSize = "XL";
				}
				if(lengthValue >= 16.4 && lengthValue <= 19.1)
				{
					lSize = "2XL";
				}
				if(lengthValue >= 19.2 && lengthValue <= 22.2)
				{
					lSize = "3XL";
				}
				if(lengthValue >= 22.3 && lengthValue <= 26.2)
				{
					lSize = "4XL";
				}
				if(lengthValue >= 26.3)
				{
					lSize = "5XL";
				}
			}
//			document.getElementById('recomended').value = rSize;
//		}
	}
	rSize = '';
	//alert(lSize+' '+nSize+' '+cSize);
	if(sizeValue[nSize] >= sizeValue[lSize] && sizeValue[nSize] >= sizeValue[cSize]) {
		rSize = nSize;
	}
	if(sizeValue[lSize] >= sizeValue[nSize] && sizeValue[lSize] >= sizeValue[cSize]) {
		rSize = lSize;
	}
	if(sizeValue[cSize] >= sizeValue[nSize] && sizeValue[cSize] >= sizeValue[lSize]) {
		rSize = cSize;
	}
	/*
if(lSize != nSize && lSize != cSize && nSize != cSize) {
		$('#needHelp').css('display','block');
	}
	else {
		$('#needHelp').css('display','none');
	}
*/
	if((Math.abs(sizeValue[lSize] - sizeValue[nSize]) >= 2) || (Math.abs(sizeValue[lSize] - sizeValue[cSize]) >= 2) || (Math.abs(sizeValue[nSize] - sizeValue[cSize]) >= 2)) {
		$('#needHelp').css('display','block');
	}
	else {
		$('#needHelp').css('display','none');
	}
	if(rSize)
	{
		document.getElementById('recomended').value = rSize;
		$('.white').css({'background-color':'#fff', 'font-weight':'normal'});
		$('.colored').css({'background-color':'#eee', 'font-weight':'normal'});
		if(lengthValue == 0 && neckValue == 0 && chestValue == 0) {

		}
		else {
			document.getElementById(clothingtype+'_row_'+rSize).style.backgroundColor = '#e19bb3';
			document.getElementById(clothingtype+'_row_'+rSize).style.fontWeight = 'bold';
		}
	}
}

function takeStep(stepValue)
{
	var currentStep = document.getElementById('currentstep');
	var step = currentStep.value;
	var displayImg = document.getElementById('StepByStepImg');
	var stepNr = document.getElementById('StepByStepNr');

	if(stepValue == 0)
	{
		step--;
	}

	if(stepValue == 1)
	{
		step++;
	}

	if(step > 3)
	{
		step = 1;
	}
	if(step < 1)
	{
		step = 3;
	}

	switch(step)
	{
		case 1 :
			displayImg.style.backgroundPosition = '-140px 30px';
			currentStep.value = step;
			document.getElementById('length').focus();
			stepNr.innerHTML = "Step 1";
		break;

		case 2 :
			displayImg.style.backgroundPosition = '10px 10px';
			currentStep.value = step;
			document.getElementById('neck').focus();
			stepNr.innerHTML = "Step 2";
		break;

		case 3 :
			displayImg.style.backgroundPosition = '-120px -75px';
			currentStep.value = step;
			stepNr.innerHTML = "Step 3";
			document.getElementById('chest').focus();
		break;
	}
}

function gotoStep(step)
{
	var displayImg = document.getElementById('StepByStepImg');
	var stepNr = document.getElementById('StepByStepNr');

	switch(step)
	{
		case 1 :
			displayImg.style.backgroundPosition = '-140px 30px';
			document.getElementById('currentstep').value = step;
			stepNr.innerHTML = "Step 1";
		break;
		case 2 :
			displayImg.style.backgroundPosition = '10px 10px';
			document.getElementById('currentstep').value = step;
			stepNr.innerHTML = "Step 2";
		break;
		case 3 :
			displayImg.style.backgroundPosition = '-120px -75px';
			document.getElementById('currentstep').value = step;
			stepNr.innerHTML = "Step 3";
		break;
	}
}
$(function(){
	$('.prod_cat').click(function(event) {
		var catid = $(this).attr('rel');
		$('#prods_'+catid).slideToggle(400);
		if ($(this).hasClass('pc_closed')) {
			$(this).removeClass('pc_closed');
			$(this).addClass('pc_open');
			$.ajax({
				type: 'POST',
				data: {cat_id: catid, cat_status: 'open'},
				url: 'ajax.php'
			});
		}
		else {
			$(this).removeClass('pc_open');
			$(this).addClass('pc_closed');
			$.ajax({
				type: 'POST',
				data: {cat_id: catid, cat_status: 'closed'},
				url: 'ajax.php'
			});
		}
		event.preventDefault();
	});
	$('#length').focus(function(){
		var displayImg = document.getElementById('StepByStepImg');
		var stepNr = document.getElementById('StepByStepNr');
		var step = $(this).attr('rel');
		displayImg.style.backgroundPosition = '-140px 30px';
		document.getElementById('currentstep').value = step;
		stepNr.innerHTML = "Step 1";
		$('#textLength').css('display', 'block');
		$('#textNeck').css('display', 'none');
		$('#textChest').css('display', 'none');
	});
	$('#length').keydown(function(event){
		if(event.keyCode == 13) {
			$(this).parent().next().children('input').focus();
			return false;
		}
	})
	$('#neck').focus(function(){
		var displayImg = document.getElementById('StepByStepImg');
		var stepNr = document.getElementById('StepByStepNr');
		var step = $(this).attr('rel');
		displayImg.style.backgroundPosition = '10px 10px';
		document.getElementById('currentstep').value = step;
		stepNr.innerHTML = "Step 2";
		$('#textLength').css('display', 'none');
		$('#textNeck').css('display', 'block');
		$('#textChest').css('display', 'none');
	});
	$('#neck').keydown(function(event){
		if(event.keyCode == 13) {
			$(this).parent().next().children('input').focus();
			return false;
		}
	})
	$('#chest').focus(function(){
		var displayImg = document.getElementById('StepByStepImg');
		var stepNr = document.getElementById('StepByStepNr');
		var step = $(this).attr('rel');
		displayImg.style.backgroundPosition = '-120px -75px';
		document.getElementById('currentstep').value = step;
		stepNr.innerHTML = "Step 3";
		$('#textLength').css('display', 'none');
		$('#textNeck').css('display', 'none');
		$('#textChest').css('display', 'block');
	});
	$('#chest').keydown(function(event){
		if(event.keyCode == 13) {
			$(this).parent().next().children('input').focus();
			return false;
		}
	})
	$('#recomended').focus(function(){
		$('#textLength').css('display', 'none');
		$('#textNeck').css('display', 'none');
		$('#textChest').css('display', 'none');
	})
});

function ajaxMaillist()
{
	var email_name = $('#maillist_name').val();
	var email_email = $('#maillist_email').val();

	var error = "";

	if(email_name == "")
	{
		error += "Name must be set<br>\n";
	}
	if(email_email == "")
	{
		error += "Email must be set<br>\n";
	}

	if(error != "")
	{
		$("#MaillistError").html(error);
		$("#MaillistError").slideDown('fast');
	}else
	{
		$("#MaillistError").slideUp('fast');
		$.ajax({
			url : "ajaxmaillist.php",
			data : "action=register&name="+email_name+"&email="+email_email,
			success :function(data)
				{
					//alert(data);
				switch(data)
				{
					case "1" :
						$("#FormElements").fadeOut('fast');
						$("#MaillistSuccess").slideDown('fast');
					break;
					case "2" :
						$("#MaillistError").html("Email already registerd");
						$("#MaillistError").slideDown('fast');
					break;
					case "0" :
						$("#MaillistError").html("System Error, contact webmaster");
						$("#MaillistError").slideDown('fast');
					break;
				}
			}
		});
	}

	return false;
}

