//Using events_common.js to add eventhandler.
addEventHandler(window, "load", prepareToggleDescriptionShop);

//isToggleActivated - If toggle is activated or not.
var isToggleActivated = true;

/*
Name: showDescriptionShop
Purpose: Hides helptext for an product in Shop.
Arguments: elem - Element to hide.
*/
function showDescriptionShop(id) {
	var artikelId = id.substring(19, id.length - 4);
	document.getElementById('shopImgStampSmall_shopProductMoreInfo' + artikelId).style.display = 'none';
	document.getElementById('shopProductMoreInfo' + artikelId + 'Show').style.display = 'none';
	document.getElementById('shopImgStampLarge_shopProductMoreInfo' + artikelId).style.display = 'inline';
	document.getElementById('shopProductMoreInfo' + artikelId + 'Hide').style.display = 'inline';
	document.getElementById('shopProductForm' + artikelId).className = 'shopProductRowSmall';
	document.getElementById('shopProductMoreInfo' + artikelId).style.display = 'inline';
}
/*
Name: hideDescriptionShop
Purpose: Hides helptext for an element on page.
Arguments: elem - Element to hide.
*/
function hideDescriptionShop(id) {
	var artikelId = id.substring(19, id.length - 4);
	document.getElementById('shopImgStampSmall_shopProductMoreInfo' + artikelId).style.display = 'inline';
	document.getElementById('shopProductMoreInfo' + artikelId + 'Show').style.display = 'inline';
	document.getElementById('shopImgStampLarge_shopProductMoreInfo' + artikelId).style.display = 'none';
	document.getElementById('shopProductMoreInfo' + artikelId + 'Hide').style.display = 'none';
	document.getElementById('shopProductForm' + artikelId).className = 'shopProductRow';
	document.getElementById('shopProductMoreInfo' + artikelId).style.display = 'none';
}
/*
Name: prepareToggleDescriptionShop
Purpose: Adds eventhandlers for toggling descriptions (helptexts).
Arguments: None.
*/
function prepareToggleDescriptionShop() {
	var labels = document.getElementsByTagName("label");
	var divs = document.getElementsByTagName("div");
	//Add eventhandlers for div-elements with correct classname.
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className == "shopProductInfoShow") {
			divs[i].onclick = function () {
				//Toggle description onclick.
				showDescriptionShop(this.id);
			}
		}
		if (divs[i].className == "shopProductInfoHide") {
			divs[i].onclick = function () {
				//Toggle description onclick.
				hideDescriptionShop(this.id);
			}
		}
	}
}

// Funktionen kontrollerar att man fyller i siffror i antal-fältet i butikens detaljbild för en vara.

function FFCheckForm1(antal, sprak) {

	var errorAmount = "Ange antal!";
	var errorLarger = "Antal måste vara större än 0";
	var errorNumbers = "Antal måste anges med siffror!";

	if( "en" == sprak ){
		errorAmount = "Please specify quantity!";
		errorLarger = "Quantity must be more than 0";
		errorNumbers = "Quantity must be specified in numbers!";
	}

	if (antal.length == 0) {
		window.alert(errorAmount);
		return false;
	}

	if (antal.length > 0) {
		if (antal == 0) {
			alert(errorLarger);
			return false;
		}

		if (!isOnlyDigits(antal)) {
			window.alert(errorNumbers);
			return false;
		}
	}
	return true;
}

function FFCheckForm2(antal) {

	if (antal.length > 0) {
		if (antal == 0) {
			alert("Antal måste vara större än 0");
			return false;
		}

		if (!isOnlyDigits(antal)) {
			window.alert("Antal måste anges med siffror!");
			return false;
		}
	}
	return true;
}

function preValidateAbonnera(theForm) {
	var sum = 0;
	for (var i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name) {
			if (theForm.elements[i].name.length > 3) {
				if (theForm.elements[i].name.substr(0, 3) == 'art') {
					if (isOnlyDigits(theForm.elements[i].value)) {
						sum += parseInt(theForm.elements[i].value);
					}
				}
			}
		}
	}
	if (sum > 0) {
		return true;
	} else {
		alert('Du måste ange minst en artikel!');
		return false;
	}
}

function isOnlyDigits(number) {

	var numberregexp = /^\d+$/;

	if (number == '' || !numberregexp.test(number)) {
		return false;
	}
	return true;
}