
var goodsAjax = new AjaxWrapper();

function extractGoodsIndex(name) {
	var regExp = new RegExp("[0-9]");
	var m = regExp.exec(name);
	if (m == null) {
		return null;
	} else {
		return name.substr(m.index, m.length);
	}
	
}

function validateGoodsLimits(obj) {
	var index = extractGoodsIndex(obj.name);
	if (index == null) {
		return;
	}
	
	var widthObj = document.getElementsByName("itemSelections(" + index + ").quantityDetails.width")[0];
	var heightObj = document.getElementsByName("itemSelections(" + index + ").quantityDetails.height")[0];
	var depthObj = document.getElementsByName("itemSelections(" + index + ").quantityDetails.depth")[0];
	var weightObj = document.getElementsByName("itemSelections(" + index + ").quantityDetails.weight")[0];
	var ddObj = document.getElementsByName("itemSelections(" + index + ").selectedItemType")[0];
	var buttonGroup = document.BookingForm["courierTypeSelections.type"];
	var courierType = getCheckedRadioValue(buttonGroup);
	var includeDims = false;
	if( courierType == "freight") {
		includeDims = true;
	}
	
	var urlParams = "?width=" + widthObj.value;
	urlParams += "&height=" + heightObj.value;
	urlParams += "&depth=" + depthObj.value;
	urlParams += "&weight=" + weightObj.value;
	urlParams += "&item=" + ddObj[ddObj.selectedIndex].value;
	urlParams += "&includedim=" + includeDims;
	var url = "validate_dimensions" + urlParams;
	goodsAjax.get(url, processValidation);	
}

function processValidation() {
	if( goodsAjax.isProcessed()) {
		var xmlDoc = goodsAjax.getResponseXml();
		var errors = xmlDoc.getElementsByTagName("error");
		var errorMsgArea = document.getElementById("item_error_block");	
		var msgArray = new Array();
		var srcArray = new Array();
		
		var widthLabelObj = document.getElementById("width_label");
		var heightLabelObj = document.getElementById("height_label");
		var depthLabelObj = document.getElementById("depth_label");
		var weightLabelObj = document.getElementById("weight_label");
		
		if(errors && errors.length > 0) {
			for(var idx=0; idx<errors.length; idx++) {
				msgArray.push(errors[idx].firstChild.nodeValue);
			}
			var msgHtml = convertArray(msgArray);
			errorMsgArea.innerHTML = msgHtml;
			for(var idx=0; idx<errors.length; idx++) {
				srcArray.push(errors[idx].getAttribute("source"));
			}
			
			if(containsLabel(srcArray, "itemSelections.quantityDetails.width")) {
				widthLabelObj.className = "error";
			}
			else {
				widthLabelObj.className = "";
			}
			
			if(containsLabel(srcArray, "itemSelections.quantityDetails.height")) {	
				heightLabelObj.className = "error";
			}
			
			else {
				heightLabelObj.className = "";
			}
			
			if(containsLabel(srcArray, "itemSelections.quantityDetails.depth")) {
				depthLabelObj.className = "error";
			}
			else {
				depthLabelObj.className = "";
			}
			
			if(containsLabel(srcArray, "itemSelections.quantityDetails.weight")) {
				weightLabelObj.className = "error";
			}
			else {
				weightLabelObj.className = "";
			}
		}
		else {
			errorMsgArea.innerHTML = "";
			widthLabelObj.className = "";
			heightLabelObj.className = "";
			depthLabelObj.className = "";
			weightLabelObj.className = "";
		}
	}	
}

function convertArray(array) {
	var msgHtml = ""; 
	for(var idx=0; idx<array.length; idx++) {
		var msg = array[idx];
		if(msgHtml.indexOf(msg) < 0 ) {
			msgHtml += msg;
			msgHtml += "<br/>";
		}
	}
	return msgHtml;
}

function containsLabel(array, lbl) {
	for(var idx=0; idx<array.length; idx++) {
		if(array[idx] == lbl) {
			return true;
		}
	}
	return false;
}

function isSuburbSearchValid(courierType, value) {
	if ((courierType == "freight" || courierType == "courier") && value.length < 3) {
		alert('You must enter at least 3 characters for a search (e.g. "Syd" or "200")!');
		return false;
	}
	return true;
}
	