function viewLabelDetail(itemCode) {
  if (getEl("detaildiv") == null)
    return;
    
	sendDetailAJAX(itemCode);
}

function validateDetailCallback(responseStr) {
  //alert(responseStr);
  
  var toolbar = document.getElementById("labellist");
  var detaildiv = getDetailDIV();
  detaildiv.innerHTML = responseStr;
  
  updatePopupPos();
  detaildiv.style.left = (popupXpos + 265) + "px";
  detaildiv.style.top = (popupYpos + getElementY(toolbar)) + "px";
    
  var visible = detaildiv.style.visibility;
  if (visible == "hidden") {  
	
		if (bNotesVisible()) showNotes(false);
    if (bPriceListDivVisible()) showPrice(false);
		if (bPreferenceVisible()) showPreference(false);
		if (bLabelTypeVisible()) showLabelType(false);
		if (bImageTypeVisible()) showImageType(false);
		if (bFavouriteDivVisible) showFavourite(false);
		if (bBackorderDivVisible()) showBackorderDetail(false);
    showDetail(true);
  }
  getEl("btnclosedetail").focus();
}

function bDetailDivVisible() {
  var detaildiv = getDetailDIV();
  if (detaildiv != null) {
    var visible = detaildiv.style.visibility;
    return (visible == "visible") ? true : false;
  }
  else {
    return false;
  }
}

function showDetail(flag)
{
	var detaildiv = getDetailDIV();
	if (detaildiv != null)
	{
		
		if (flag == true)	{
			detaildiv.style.visibility = "visible";
			captureOldValues();
		}
		else {
			if (getEl("bCurrentLabel") == null) {
				detaildiv.style.visibility = "hidden";
			}
			else {
				nowCurrentLabel = getEl("bCurrentLabel").value;
				nowEncoding = getEl("encoding").value;
				nowBarcode = getEl("barcode").value;
				nowSticker = getEl("label_cust_none").value;
				
				if (nowCurrentLabel != oldCurrentLabel ||
					nowEncoding != oldEncoding ||
					nowBarcode != oldBarcode ||
					nowSticker != oldSticker) {
					var ret=confirm("Do you want to save the changes?");
					if (ret) {
						saveDetails();
					}
					else {
						detaildiv.style.visibility = "hidden";
					}
				}
				else {
					detaildiv.style.visibility = "hidden";
				}
			}
		}
	}
	
	if (! flag) focusOnGenus();
}

function getDetailDIV() {
  return document.getElementById("detaildiv");
}

function changeCurrent() {
  getEl("currentLabel").innerText = "\n\n";
  if (getEl("bCurrentLabel").value == "N") {
    getEl("currentLabel").innerText = 
        'This item will be accessible from the Non-Current category';
  }
}

function submitPreviewForm() {
	validated = validateBarcodeChkDigit(getEl("encoding").value, getEl("barcode").value);
	if (!validated) {
		alert("The barcode is invalid. Please re-enter.");
		document.getElementById("barcode").focus();
		return;
	}
	
	document.getElementById('stickerForm').submit();
}

function saveDetails() {
	validated = validateBarcodeChkDigit(getEl("encoding").value, getEl("barcode").value);
	if (!validated) {
		alert("The barcode is invalid. \r\nPlease try again.");
		document.getElementById("barcode").focus();
		return;
	}
	
  getEl("prompt").innerText = "Please wait while processing...";
  getEl("savebtn").disabled = true;
  
  var url = "saveLabelDetail.php?customer=" + getEl("customer_id").value + 
            "&item=" + getEl("label_code").value + 
            "&current=" + getEl("bCurrentLabel").value +
						"&internalBarcodeType=" + getEl("encoding").value +
						"&internalBarcode=" + getEl("barcode").value +
						"&stickerType=" + getEl("label_cust_none").value;          
  var ajax = new AJAXAction(url, validateSaveDetailCallback); 
  ajax.doGet();  
}

function validateSaveDetailCallback(responseStr) {
  var retarry = responseStr.split("/");
  if (retarry[0] == "waiting") {
    alert("Cannot get the response from the server. Please try again later.");
  }
  else {
    alert(retarry[1]);
  }
  getEl("savebtn").disabled = false;
  
  if (retarry[0] == "success") {
		captureOldValues();
    stayInCurrentPage();
		showDetail(false);
  }
}

var oldCurrentLabel;
var oldEncoding;
var oldBarcode;
var oldSticker;

function captureOldValues() {
	oldCurrentLabel = getEl("bCurrentLabel").value;
	oldEncoding = getEl("encoding").value;
	oldBarcode = getEl("barcode").value;
	oldSticker = getEl("label_cust_none").value;
}

function validateBarcodeChkDigit(type, code) {
	if (code.length == 0) return true;
	
	if (type == "ean13") return validateEANBarcodeChkDigit(code);
	else if (type == "upc") return validateUPCBarcodeChkDigit(code);
	else return true;
}

function validateEANBarcodeChkDigit(UserEAN) {
	if (UserEAN.length != 13) return false;
	
	barcode = UserEAN;
	checkDigit = parseInt(UserEAN[12]);
	
	//2 parse EAN and converted to num except m (var m is check digit supplied by user)
	var a = parseInt(barcode[0]); //digit1
	var b = parseInt(barcode[1]); //digit2
	var c = parseInt(barcode[2]); //digit3
	var d = parseInt(barcode[3]); //digit4
	var e = parseInt(barcode[4]);  //digit5
	var f = parseInt(barcode[5]);  //digit6
	var g = parseInt(barcode[6]);  //digit7
	var h = parseInt(barcode[7]);  //digit8
	var i = parseInt(barcode[8]);  //digit9
	var j = parseInt(barcode[9]);  //digit10
	var k = parseInt(barcode[10]);  //digit11
	var l = parseInt(barcode[11]);  //digit12

	//3 multiply each digit against weighting factors to get extended value
	var a1 =(a * 1);
	var b1 =(b * 3);
	var c1 =(c * 1);
	var d1 =(d * 3);
	var e1 =(e * 1);
	var f1 =(f * 3);
	var g1 =(g * 1);
	var h1 =(h * 3);
	var i1 =(i * 1);
	var j1 =(j * 3);
	var k1 =(k * 1);
	var l1 =(l * 3);

	//4 sum extended values
	var iSum=(a1+b1+c1+d1+e1+f1+g1+h1+i1+j1+k1+l1);

	//5&6 divide sum by 10, returning the remainder
	var iDiv=(iSum % 10);

	if (iDiv == 0) check_digit = 0;
	else {
		check_digit = (iSum - iDiv) + 10 - iSum;
	}
	
	//"check_digit" is the calculated check digit to compare to user input.
	if (checkDigit != "" && check_digit != checkDigit) {
		return false
	}
	return true;
}

function validateUPCBarcodeChkDigit(UserUPC) {
	if (UserUPC.length != 12) return false;
	
	barcode = UserUPC;
	checkDigit = parseInt(UserUPC[11]);
	
	odd_total  = 0;
	even_total = 0;

	for(i=0; i<11; i++) {
		if(((i+1)%2) == 0) {
			/* Sum even digits */
			even_total += parseInt(barcode[i]);
		} else {
			/* Sum odd digits */
			odd_total += parseInt(barcode[i]);
		}
	}

	sum = (3 * odd_total) + even_total;

	/* Get the remainder MOD 10*/
	check_digit = sum % 10;

	/* If the result is not zero, subtract the result from ten. */
	check_digit = (check_digit > 0) ? 10 - check_digit : check_digit;
	
	if (checkDigit != "" && check_digit != checkDigit) {
		return false;
	}
	return true;
}

