var itemCode;
var customerID;
var requestType;

function downloadimg(code, custId) {
  itemCode = code;
  customerID = custId;
  
  var url = "getImageType.php?custId=" + custId + "&itemCode=" + itemCode + "&type=checkagreement";
  var ajax = new AJAXAction(url, validateCheckAgreementSignedCallback);
  ajax.doGet();
}

function validateCheckAgreementSignedCallback(responseXML) {
  if (responseXML == "notagree") {
    var message = "Please sign and return the Norwood Digital Image Licence Agreement.\r\n";
    message += "You may proceed with your selection of digital images and submit your order, however these will not be available for download until the signed agreement is received by us.\r\n\r\n";
    message += "Would you like to download the Digital Image Licence Agreement?";
    
    if (newConfirm("Digital Image Licence Agreement",message,1,0,0) == true) {
      window.open("mdil_agree.pdf");
    }
  }
  
  var item = document.getElementById("downloadimg[" + itemCode + "]");
  requestType = "remove";
  if (item.src.indexOf("nodownloadimg") > 0)
    requestType = "add";
	
  item.src = "n_images/halfdownloadimg.gif";	  
  
  if (requestType == "remove") {
    sendAddImageToShoppingCartAJAX(itemCode, 0);
  } else {
    sendAddImageToShoppingCartAJAX(itemCode, 1);
  }
}

function validateImgDownloadCallback(responseXML) {
  if (responseXML != "null") {     
    var img;  
    var item = document.getElementById("downloadimg[" + itemCode + "]");
   
    if (requestType == "add") {
      img = "downloadimg.gif";
      alt = "Click to remove the item image from the shopping cart.";
    }
    else {
      img = "nodownloadimg.gif";
      alt = "Click to add the item image into the shopping cart.";
    }

    if (item != null) {
      item.src = "n_images/" + img;
      item.alt = alt;
    }    
  }
  
  if (document.getElementById("gotopage").value == "14" ||
      document.getElementById("gotopage").value == "15") {
    constructParameters();
  }
} 

function checkImgChargeKeyValue(oriObject, e) {
  var oriValue = oriObject.value;
  var keyCode = getKeyCode(e);

  var wrongFormat = false;
  if ((keyCode >= NUM_0 && keyCode <= NUM_9) ||
	  (keyCode >= NUMLOCK_0 && keyCode <= NUMLOCK_9)) { //press a number key
	  if (keyCode * 1 > 95) {
      keyCode = keyCode - 48;
    }
	} else if (keyCode >= CHAR_A && keyCode <= CHAR_Z) {
    wrongFormat = true;
	} else if (keyCode == CHAR_DOT || keyCode == NUM_DOT) {
    if (oriValue.indexOf(".") >=0)
      wrongFormat = true;
    else
      return;
	} else if (keyCode != BAK && keyCode != DEL && keyCode != UP && keyCode != DOWN &&
    keyCode != LEFT && keyCode != RIGHT && keyCode != ENTER && keyCode != TAB) {
    wrongFormat = true;
	}
	
	if (wrongFormat) {
    alert("Wrong Number Format");
    oriObject.value = oriValue;
	}
}

function getOtherCharges() {
  var charge_code = "001";
  var charge_amount = 2000;
  var chargeselector = document.getElementById("other_charges");
  if (chargeselector != null) {
    var values = chargeselector.value.split("*");
    charge_code = values[1];
    charge_amount = values[2];
    var imgHandling = document.getElementById("imghandling");
    imgHandling.value = complete2decimal(charge_amount/100);
  }
  calculateImgCharges();
}

function calculateImgCharges(gstpercent) {
  var sectionTotal = 0;
  var gstValue = 0;
	var totalValue = 0;

	var sortseqs = document.getElementsByName("sortseqid");
	var qtys = document.getElementsByName("sortseqqty");

  if (sortseqs == null || sortseqs.length < 1) {
    return;
  }
  
	if (gstpercent == null) {
		gstpercent = 0.1;
	}
	
  for (j = 0; j < sortseqs.length; j++) {
    var qty = qtys[j].value;
    var unitPrice = document.getElementById("sort"+sortseqs[j].value).value;
    
    sectionTotal = round2decimal(unitPrice * 1 * qty);
    totalValue += sectionTotal;
    gstValue += round2decimal(unitPrice * gstpercent) * qty;
    
    var objNetPrice = document.getElementById("sortNetPrice"+sortseqs[j].value);
    objNetPrice.value = complete2decimal(sectionTotal);
  }
  
  var imgHandling = document.getElementById("imghandling");
  totalValue += round2decimal(imgHandling.value * 1);
  gstValue += round2decimal(imgHandling.value * gstpercent);
  
  var GSTValue = document.getElementById("GSTValue");
  if (GSTValue != null) {
    GSTValue.value = complete2decimal(gstValue);
    document.getElementById("digitalImgTotal").value =
      complete2decimal(totalValue + gstValue);
  } else {
    document.getElementById("digitalImgTotal").value =
      complete2decimal(totalValue);
  }
}
