// Adds quotes to the search query if a space is found, to change search operator from OR to AND
function changeSearch() {
    var q = document.getElementsByName("q");
    for (var i = 0; i < q.length; i++) {
        if (trim(q[i].value).indexOf(' ') != -1 && q[i].value.indexOf('"') == -1)
            q[i].value = '"' + trim(q[i].value) + '"';
    }
}

function trim(value) {
    value = value.replace(/^\s+/, '');
    value = value.replace(/\s+$/, '');
    return value;
}

function printItem() {
	if (location.href.indexOf('?') < 0)
	{
		location.href = location.href + "?PrinterFriendly=1";
	} else {
		location.href = location.href + "&PrinterFriendly=1";
	}
}

function sendItem() {
	window.open('/Admin/Public/SendToFriend.aspx?ID=2&M=SendFriend&url=' + escape(location.href) ,'sendfriend','width=400,height=400,toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=1');
}

function isValidEan(eanCode, blnAllowZeroEan) {
		//0000000000000 is used as an valid EAN number
		if(blnAllowZeroEan && eanCode == '0000000000000'){
			return true;
		}
		
		//Check if EAN starts with 2608
		if(eanCode.substring(0, 4) != '2608'){
			return false;
		}
	
		// Check if only digits
		var ValidChars = "0123456789";
		for (i = 0; i < eanCode.length; i++) {
			digit = eanCode.charAt(i);
			if (ValidChars.indexOf(digit) == -1) {
				return false;
			}
		}

		// Add five 0 if the code has only 8 digits
		//if (eanCode.length == 8 ) {
				//eanCode = "00000" + eanCode;
		//}
		
		// Check for 13 digits otherwise
		if (eanCode.length != 13) {
			return false;
		}

		// Get the check number
		originalCheck = eanCode.substring(eanCode.length - 1);
		eanCode = eanCode.substring(0, eanCode.length - 1);

		// Add even numbers together
		even = Number(eanCode.charAt(1)) +
			   Number(eanCode.charAt(3)) +
			   Number(eanCode.charAt(5)) +
			   Number(eanCode.charAt(7)) +
			   Number(eanCode.charAt(9)) +
			   Number(eanCode.charAt(11));
		// Multiply this result by 3
		even *= 3;

		// Add odd numbers together
		odd = Number(eanCode.charAt(0)) +
			  Number(eanCode.charAt(2)) +
			  Number(eanCode.charAt(4)) +
			  Number(eanCode.charAt(6)) +
			  Number(eanCode.charAt(8)) +
			  Number(eanCode.charAt(10));

		// Add two totals together
		total = even + odd;

		// Calculate the checksum
		// Divide total by 10 and store the remainder
		checksum = total % 10;
		// If result is not 0 then take away 10
		if (checksum != 0) {
			checksum = 10 - checksum;
		}

		// Return the result
		if (checksum != originalCheck) {
			return false;
		}

		return true;
	}
