var rfq_wiz = {};

rfq_wiz.globalObjName = 'rfq_wiz';
//alert("zzz Inside rfq_wiz js file loaded successfully!")
console.log("Inside rfq_wiz js file loaded successfully!");

function wfValData(hdrFormXml, currFormXml, objContext, action, objName, allFormXmlData) {
	//alert('zzz inside wfValData function');
	console.log('inside wfValData function in js file');
	return true;
}

function valData(objContext, action, objName, currentFormData, objFormName) {
	//alert('zzz inside valData function');
	console.log('inside valData function in js file');
	return true;
}

//called in offline mode
function localWfValData(hdrFormXml, currFormXml, objContext, action, objName, allFormXmlData) {
	//alert('zzz inside localWfValData function');
	var isValidWfData = true;
	console.log('inside localWfValData function in js file **************************');
	return isValidWfData;
}

function localValData(objContext, action, objName, currentFormData, objFormName) {
	//alert('zzz inside localValData function');
	var isValidData = true;
	console.log('objName is' + objName);
	console.log('objFormName is' + objFormName);
	console.log('objContext is' + objContext);
	console.log('action is' + action);
	console.log('inside localValData function in js file **************************' + currentFormData);
	var currentFormData = currentFormData;
	var action = action;
	var xmlVersion = currentFormData.indexOf("<?xml version='1.0' encoding='UTF-8'?>");
	if (currentFormData != null && currentFormData.trim().length > 0) {
		console.log("cureentform data is not null" + currentFormData);

		if (xmlVersion == 0) {
			//console.log("inside if");
			currentFormData = currentFormData;
		}
		else {
			//console.log("inside else");
			currentFormData = "<Root>" + currentFormData + "</Root>";
		}

	}
	else {
		console.log("null currentformdata");
	}

	if (window.DOMParser) {
		//alert("inside window.DOMParser");
		parser = new DOMParser();
		try {
			if (currentFormData != null && currentFormData.trim().length > 0) {
				xmlDoc = parser.parseFromString(currentFormData, "text/xml");
				//console.log("xmlDoc =["+xmlDoc+"]");

			}

		}
		catch (ex) {
			alert("Exception occurs while parsing xml data");
		}
	}
	else // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.loadXML(txt);
	}
	if (xmlDoc != null) {
		if (objName === 'rfq_wiz') {
			if (objContext === '2') {

				console.log("=== RFQ_WIZ VALIDATION START ===");
				console.log("Form Name :: [" + objName + "]");
				console.log("Object Context :: [" + objContext + "]");

				var detailElement = xmlDoc.getElementsByTagName("Detail" + objContext);
				console.log("Total Detail Elements Found :: [" + detailElement.length + "]");

				for (var i = 0; i < detailElement.length; i++) {

					console.log("---- Processing Row Index :: [" + i + "] ----");

					var detail = detailElement[i];

					var attributeVal = detail.getElementsByTagName("attribute")[0].getAttribute("updateFlag");
					console.log("updateFlag :: [" + attributeVal + "]");

					if (attributeVal != 'D') {

						var rateQuot = "";
						var competitorRate = "";
						var competitorName = "";
						var competitorItem = "";
						var ptrFlag = "";


						// rate_quot
						if (detail.getElementsByTagName("rate_quot")[0] != null &&
							detail.getElementsByTagName("rate_quot")[0].childNodes[0] != null) {

							rateQuot = detail.getElementsByTagName("rate_quot")[0].childNodes[0].nodeValue;
						}

						// competitor_rate
						if (detail.getElementsByTagName("competitor_rate")[0] != null &&
							detail.getElementsByTagName("competitor_rate")[0].childNodes[0] != null) {

							competitorRate = detail.getElementsByTagName("competitor_rate")[0].childNodes[0].nodeValue;
						}

						// competitor_name
						if (detail.getElementsByTagName("competitor_name")[0] != null &&
							detail.getElementsByTagName("competitor_name")[0].childNodes[0] != null) {

							competitorName = detail.getElementsByTagName("competitor_name")[0].childNodes[0].nodeValue;
						}

						// competitor_item
						if (detail.getElementsByTagName("competitor_item")[0] != null &&
							detail.getElementsByTagName("competitor_item")[0].childNodes[0] != null) {

							competitorItem = detail.getElementsByTagName("competitor_item")[0].childNodes[0].nodeValue;
						}

						if (detail.getElementsByTagName("ptr_flag")[0] != null &&
							detail.getElementsByTagName("ptr_flag")[0].childNodes[0] != null) {

							ptrFlag = detail.getElementsByTagName("ptr_flag")[0].childNodes[0].nodeValue;
						}

						console.log("rate_quot :: [" + rateQuot + "]");
						console.log("competitor_rate :: [" + competitorRate + "]");
						console.log("competitor_name :: [" + competitorName + "]");
						console.log("competitor_item :: [" + competitorItem + "]");
						console.log("ptrFlag :: [" + ptrFlag + "]");

						// ===== Existing rate validation =====
						if (rateQuot == null) {
							console.log("Validation Failed :: rate_quot is NULL");
						} else if (rateQuot.trim().length == 0) {
							console.log("Validation Failed :: rate_quot is EMPTY");
						} else if (isNaN(rateQuot)) {
							console.log("Validation Failed :: rate_quot is NOT A NUMBER");
						} else if (Number(rateQuot) <= 0) {
							console.log("Validation Failed :: rate_quot <= 0");
						}

						if (rateQuot == null || rateQuot.trim().length == 0 || isNaN(rateQuot) || Number(rateQuot) <= 0) {
							console.log("L Invalid rate_quot detected at row :: [" + i + "]");
							alert("Invalid Rate Quot! Value should be greater than 0");
							isValidData = false;
							return isValidData;
						}

						// ===== NEW VALIDATION =====
						if (Number(rateQuot) > 0 && ptrFlag == "N" || ptrFlag.trim().length == 0) {

							if (
								competitorRate == null || competitorRate.trim().length == 0 ||
								competitorName == null || competitorName.trim().length == 0 ||
								competitorItem == null || competitorItem.trim().length == 0
							) {
								console.log("L Competitor details missing at row :: [" + i + "]");
								alert("Competitor details cannot be empty for manually entering special rate");
								isValidData = false;
								return isValidData;
							}
						}

						console.log(" Valid row :: [" + i + "]");
					} else {
						console.log("Skipping row [" + i + "] as updateFlag is D (Deleted)");
					}
				}

				console.log("=== RFQ_WIZ VALIDATION END ===");
			}
		}

	}

	return isValidData;
}
function localAutoNumberChange(editorObjName, currentFormNo, offlineRefVal, freeFormDataMap, fldName, fldValue, maxDomValue, isDoneSucess) {
	//alert('zzz inside localAutoNumberChange function');
	console.log('inside localAutoNumberChange function in js file **************************');
	var checkitemchangeData = 1;
	console.log("Testing editorObjName [" + editorObjName + "] currentFormNo [" + currentFormNo + "] offlineRefVal [" + offlineRefVal + "] fldName [" + fldName + "] freeFormDataMap [" + freeFormDataMap + "] fldValue [" + fldValue + "] maxDomValue [" + maxDomValue + "] isDoneSucess [" + isDoneSucess + "]");
	if (maxDomValue != "" && maxDomValue.length > 0) {
		console.log("Testing maxDomValue [" + maxDomValue + "]");
		if (isDoneSucess) {
			checkitemchangeData = parseInt(maxDomValue) + 1;
		}
		else {
			checkitemchangeData = parseInt(maxDomValue) - 1;
		}
	}
	/*if( fldValue != "" && fldValue.length > 0 )
	{
		console.log("Testing fldValue ["+fldValue+"]");
		if( isDoneSucess )
		{
			checkitemchangeData = parseInt(fldValue)+1;
		}
		else
		{
			checkitemchangeData = parseInt(fldValue)-1;
		}
	}*/
	console.log("Testing final fldValue [" + fldValue + "] checkitemchangeData [" + checkitemchangeData + "]");
	return checkitemchangeData;
}

function localItemChange(objContext, action, objName, currentFormData, fieldName, domId, objFormName, fieldValue, parentFormNo, parentDomID, buttonId, prevValue, itemCode, allFormDetaildata, dataModelJson) {
	//alert('zzz inside localItemChange function');
	console.log("inside localItemChange" + objFormName + "action::" + action + "domId:::" + domId + "fieldName:::" + fieldName);
	var isItemchangeData = "";
	var retString = "";
	//	var orderMap = new Map(); 
	var domArr = [];
	var orderArr = [];
	console.log("dataModelJson::" + dataModelJson);
	console.log('inside localItemChange function in js file **************************');

	var xmlVersion = currentFormData.indexOf("<?xml version='1.0' encoding='UTF-8'?>");
	//console.log("xmlVersion is : "+xmlVersion)
	if (currentFormData != null && currentFormData.trim().length > 0) {
		console.log("cureentform data is not null" + currentFormData);

		if (xmlVersion == 0) {
			//console.log("inside if");
			currentFormData = currentFormData;
		}
		else {
			//console.log("inside else");
			currentFormData = "<Root>" + currentFormData + "</Root>";
		}

	}
	else {
		console.log("null currentformdata");
	}
	if (window.DOMParser) {
		//alert("inside window.DOMParser");
		parser = new DOMParser();
		try {
			if (currentFormData != null && currentFormData.trim().length > 0) {
				xmlDoc = parser.parseFromString(currentFormData, "text/xml");
				//console.log("xmlDoc =["+xmlDoc+"]");

			}

		}
		catch (ex) {
			alert("Exception occurs while parsing xml data");
		}
	}
	else // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.loadXML(txt);
	}

	if (xmlDoc != null) {

		if (objContext == 2) {
			if (fieldName === 'quot_qty') {
						console.log("In quot_qty condition" );

				var quantity = 0;
				var rateQuot = 0;
				var value = 0;

				var detailElement = xmlDoc.getElementsByTagName("Detail" + objContext);

				for (var i = 0; i < detailElement.length; i++) {

					var detail = detailElement[i];

					// Get quantity
					if (detail.getElementsByTagName("quot_qty")[0] != null &&
						detail.getElementsByTagName("quot_qty")[0].childNodes[0] != null) {

						quantity = detail.getElementsByTagName("quot_qty")[0].childNodes[0].nodeValue;
						console.log("quantity is: " + quantity);
					}

					// Get rate
					if (detail.getElementsByTagName("rate_quot")[0] != null &&
						detail.getElementsByTagName("rate_quot")[0].childNodes[0] != null) {

						rateQuot = detail.getElementsByTagName("rate_quot")[0].childNodes[0].nodeValue;
						console.log("rateQuot is: " + rateQuot);
					}

					// Convert and multiply
					var qtyNum = Number(quantity);
					var rateNum = Number(rateQuot);

					if (!isNaN(qtyNum) && !isNaN(rateNum)) {
						value = qtyNum * rateNum;
					} else {
						value = 0;
					}

					console.log("Calculated value :: " + value);

					// Pass value into XML
					itemChangeStr = "<Detail2><value protect='1' visible='1'>" + value + "</value></Detail2>";
				}
			}
		}
	}

	return itemChangeStr;
}

