function Utils(consts)
{
	if (consts)
	{
		this.m_oConsts = consts;
	}
	else
	{
		this.m_oConsts = new PMP4Consts();
	}

	this.oErrorCodes = new PMP4ResultCodes();
	this.m_strRouterUrl = this.m_oConsts.ROUTING_SERVLET;

	this.setPageLabels = Utils_setPageLabels;
	this.m_oXmlUtils = new XmlUtilsIe();
	this.getXmlUtils = Utils_getXmlUtils;
	this.logout = Utils_logout;
	this.setPageParamsRequest = Utils_setPageParamsRequest;
	this.getTodayString = Utils_getTodayString;
	this.dateToString = Utils_dateToString;
	this.getDateFromNow = Utils_getDateFromNow;
	this.isModal = Utils_isModal;
	this.handleSessionExpired = Utils_handleSessionExpired;
	this.openDialog = Utils_openDialog;
	this.showSubscriberDetailsDialog = Utils_showSubscriberDetailsDialog;
	this.getTestResultString = Utils_getTestResultString;
	this.SendSMSByMail = Utils_SendSMSByMail;
	this.cutText = Utils_cutText;
	this.cutWord = Utils_cutWord;
	this.getPageParamsRequest = Utils_getPageParamsRequest;

	this.getRegex = Utils_getRegex;
	this.convertEscapeChars = Utils_convertEscapeChars;
	this.sleep = Utils_sleep;
	this.m_oPageParams;

	this.m_attTestResultCaptions;
	this.getLoginRequiredConst = Utils_getLoginRequiredConst;

	this.setValuesRange = Utils_setValuesRange;
	this.getIsValuesOffRange = Utils_getIsValuesOffRange;

	this.setWebAccessWS = Utils_setWebAccessWS;
	this.getElementsByClassName = Utils_getElementsByClassName;

	this.openLotReport = Utils_openLotReport;
	this.checkForInt = Utils_checkForInt;
	this.checkForFloat = Utils_checkForFloat;
	this.isFirstDateEarlier = Utils_isFirstDateEarlier;
	this.isOptionVisible = Utils_isOptionVisible;

	this.convertMeasure = Utils_convertMeasure;
}

function
Utils_getRegex(
	regexType
) {
	var reg;
	switch (regexType) {
		case 1:
			// White space, -, $, !, ?, ., ,, \, %, *, and alphabet of Greek, Dutch, French, Polish and English
			// ONE character of the above
			reg = /^[0-9\s\-\$\!\?\.\,\(\)\%\*\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u01ba\u01bc-\u01bf\u01c4-\u02ad\u0386\u0388-\u0481\u048c-\u0556\u0561-\u0587\u10a0-\u10c5\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2131\u2133\u2134\u2139\ufb00-\ufb17\uff21-\uff3a\uff41-\uff5a]/;
			break;
		case 2:
			// Spaces and alpha
			reg = /^[\s\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u01ba\u01bc-\u01bf\u01c4-\u02ad\u0386\u0388-\u0481\u048c-\u0556\u0561-\u0587\u10a0-\u10c5\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2131\u2133\u2134\u2139\ufb00-\ufb17\uff21-\uff3a\uff41-\uff5a]*$/;
			break;
		case 3:
			// Alpha numeric
			reg = /^[0-9\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u01ba\u01bc-\u01bf\u01c4-\u02ad\u0386\u0388-\u0481\u048c-\u0556\u0561-\u0587\u10a0-\u10c5\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2131\u2133\u2134\u2139\ufb00-\ufb17\uff21-\uff3a\uff41-\uff5a]+$/;
			break;
		case 4:
			// Alpha numeric and spaces
			reg = /^[0-9\s\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u01ba\u01bc-\u01bf\u01c4-\u02ad\u0386\u0388-\u0481\u048c-\u0556\u0561-\u0587\u10a0-\u10c5\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2131\u2133\u2134\u2139\ufb00-\ufb17\uff21-\uff3a\uff41-\uff5a]+$/;
			break;
		case 5:
			// Alpha
			reg = /^[\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u01ba\u01bc-\u01bf\u01c4-\u02ad\u0386\u0388-\u0481\u048c-\u0556\u0561-\u0587\u10a0-\u10c5\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2131\u2133\u2134\u2139\ufb00-\ufb17\uff21-\uff3a\uff41-\uff5a]*$/;
			break;
		case 6:
			// Integer
			reg = /^[0-9]+$/;
			break;
		case 7:
			// Positive Float
			reg = /^(([0]+[\.][0-9]+)|([0-9]*[1-9][0-9]*([0-9]*|[\.][0-9]+)))$/;
			break;
		default:
			reg = null;
	}
	return reg;
}

function Utils_isOptionVisible(optionId,lsOptionStateConfig)
{
	var res = true;
	var nLength = lsOptionStateConfig.length;
	if (nLength > 0)
	{
		for (var i = 0 ; i < nLength ; i++)
		{
			var option = lsOptionStateConfig.item(i);
			if(option.getAttribute("id") == optionId)
			{
				if(option.getAttribute("state") == "hidden")
				{
					res = false;
					break;
				}
			}
		}
	}

	return res;
}

function
Utils_logout(
) {
	window.parent.location = getCookieValue("login.page");
}

function
Utils_getXmlUtils() {
	return this.m_oXmlUtils;
}

function
Utils_setPageParamsRequest(
	oRpcRequests,
	nPageId
) {
	oRpcRequests.addRequest("GetPageParams");
	oRpcRequests.addRequestParam(
		"GetPageParams",
		this.getXmlUtils().getXmlObject("<PAGEID>" + nPageId + "</PAGEID>").documentElement
	);
}


function
Utils_getLoginRequiredConst(
	oRpcRequests,
	nPageId

) {
	var oRpcMngr = new RpcMngr(this.m_strRouterUrl);
	oRpcRequests.addRequest("GetPageParams");
	oRpcRequests.addRequestParam(
		"GetPageParams",
		this.getXmlUtils().getXmlObject("<PAGEID>" + nPageId + "</PAGEID>").documentElement
	);
	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oRes = oRpcResponses.getResponse("GetPageParams");
	var oConsts = oRes.getResultContent().selectSingleNode("Constants");
	this.m_oPageParams = new X2WebConstsManager();
	if (oConsts) {
		this.m_oPageParams.loadXml(
			this.getXmlUtils().getXmlObject(oConsts.xml)
		);
	}
}



function
Utils_setPageLabels(
	oConsts
) {
	this.m_oPageParams = oConsts;
	try {
		this.m_attTestResultCaptions = new Array();
		this.m_attTestResultCaptions["PulseMinute"] = oConsts.PulseMinute;
		this.m_attTestResultCaptions["GlucoseMeasurement"] = oConsts.GlucoseMeasurement;
		this.m_attTestResultCaptions["InsulinUnits"] = oConsts.InsulinUnits;
		this.m_attTestResultCaptions["InsulinType"] = oConsts.InsulinType;
		this.m_attTestResultCaptions["Fvc"] = oConsts.Fvc;
		this.m_attTestResultCaptions["Pef"] = oConsts.Pef;
		this.m_attTestResultCaptions["Fev1Fvc"] = oConsts.Fev1Fvc;
		this.m_attTestResultCaptions["SPO2Average"] = oConsts.SPO2Average;
		this.m_attTestResultCaptions["SPO2Max"] = oConsts.SPO2Max;
		this.m_attTestResultCaptions["SPO2Min"] = oConsts.SPO2Min;
		this.m_attTestResultCaptions["HRAverage"] = oConsts.HRAverage;
		this.m_attTestResultCaptions["HRMax"] = oConsts.HRMax;
		this.m_attTestResultCaptions["HRMin"] = oConsts.HRMin;
		this.m_attTestResultCaptions["Fev1"] = oConsts.Fev1;
		this.m_attTestResultCaptions["BodyFat"] = oConsts.BodyFat;
		this.m_attTestResultCaptions["BMI"] = oConsts.BMI;
	} catch( oEx ) {
		this.m_attTestResultCaptions = null;
	}
	var oHtml = null;
	for (var name in oConsts)
	{
		if (typeof(oConsts[name]) != "function")
		{
			// This is a temp fix the problem of ' changing go " in french
			var constName = eval("oConsts." + name);
			constName = constName.replace("\"", "'");

			oHtml = document.getElementsByName(name);
			if (oHtml.length)
			{
				for (var i = 0; i < oHtml.length; i++)
				{
					var item = oHtml.item(i);
					try
					{
						item.innerText = constName;
					}
					catch (e)
					{
						if (item.tagName == "TITLE")
							document.title = constName;
						else
							item.value = constName;
					}
				}
			}
			else
			{
				oHtml = document.getElementById(name);
				if (oHtml != null)
				{
					try
					{
						oHtml.innerText = constName;
					}
					catch(e)
					{
						if (item.tagName == "TITLE")
							document.title = constName;
						else
							oHtml.value = constName;
					}
				}
			}
		}
	}
}

function
Utils_getTodayString() {
	return this.dateToString(new Date());
}

function
Utils_dateToString(
	oDate
) {
	var strDate = (
		String(oDate.getMonth() + 1) + "/" +
		String(oDate.getDate()) + "/" +
		oDate.getFullYear() + " " +
		oDate.getHours() + ":" + oDate.getMinutes()
	);
	return strDate;
}

function
Utils_getDateFromNow(
	nDays
) {
	var oDate = new Date();
	oDate.setDate(oDate.getDate() + nDays);
	return this.dateToString(oDate);
}

function
Utils_openDialog(
	strPath,
	oArgs,
	strFeatures
) {
	var dialogRes = window.showModalDialog(
		strPath,
		oArgs,
		strFeatures
	);

	if (
		typeof(dialogRes) != "object" &&
		dialogRes == this.oErrorCodes.ACCESS_DENIED
	) {
		this.handleSessionExpired();
	}
	else {
		return dialogRes;
	}
}

function
Utils_handleSessionExpired()
{
	var oRpcRequests = new RpcRequests("1001");
	this.getLoginRequiredConst(oRpcRequests,16);

	if (this.isModal())
	{
		window.returnValue = this.oErrorCodes.ACCESS_DENIED;
		window.close();
	}
	else
	{
		if (typeof this.webAccessWS != typeof undefined)
		{
			var parent = new String(window.parent.location);
			if (parent.indexOf("dialog") < 0)
			{
				this.webAccessWS.openAlertDialogByParam("LoginRequired");
			}
		}
		else
		{
			var m = new MessageBox(
				this.m_oPageParams.LoginRequired,
				"Warnning",
				"OK"
			);
			m.show();
		}

		this.logout();
	}
}

function
Utils_isModal() {
	return window.dialogWidth;
}

function
Utils_showSubscriberDetailsDialog(
	oArrArguments
) {
	var dialogResult = this.openDialog("/PMP4/Client/Admin/Subscribers/Details/details.htm", oArrArguments, "status:no; resizable:yes; dialogWidth:860px; dialogHeight:540px;help: no");
	return dialogResult;
}

function
Utils_getTestResultString(
	nMedicalTypeId,
	oTestXml,
	measureUnit
) {
	var strTestResults = "";
	var oXmlUtils = this.getXmlUtils();
	if (oTestXml != null && oTestXml.xml != "") {
		if (nMedicalTypeId == this.m_oConsts.BLOOD_PRESSURE_DB_ID) {


			strTestResults = (
				oXmlUtils.getNodeValue(oTestXml.selectSingleNode("SYSTOLICPRESSURE"), "", oTestXml) + "/" +
				oXmlUtils.getNodeValue(oTestXml.selectSingleNode("DIASTOLICPRESSURE"), "", oTestXml) + " " +
				" - " + this.m_attTestResultCaptions["PulseMinute"] + ":" +
				oXmlUtils.getNodeValue(oTestXml.selectSingleNode("HEARTRATE"), "", oTestXml)
			);
		}
		else if (nMedicalTypeId == this.m_oConsts.GLUCOSE_DB_ID)
		{
			var res = parseFloat(oTestXml.selectSingleNode("SUGARLEVEL").text);
			var result = "";
			var strMeasure = this.m_oPageParams.MmolL;

			if (this.m_oConsts.METRIC == 0) // american system
			{
				strMeasure = this.m_oPageParams.MgDl;
				if (measureUnit != 9) // it might be 8 or 0, 0 --> need to be converted to american
				{
					res = this.convertMeasure(3, res, true, 3);
				}
			}
			else	// non american system
			{
				if (measureUnit == 9) // convert to 8
				{
					res = this.convertMeasure(3, res, false, 3);
				}
			}

			result = (
						res + " " + strMeasure
					);

			strTestResults = (
				this.m_attTestResultCaptions["GlucoseMeasurement"] + ": " +
				result + "  " +
					this.m_attTestResultCaptions["InsulinUnits"] + ": " +
				oXmlUtils.getNodeValue(oTestXml.selectSingleNode("INSULINLEVEL"), "", oTestXml) + "  " +
					this.m_attTestResultCaptions["InsulinType"] + ": " +
				oXmlUtils.getNodeValue(oTestXml.selectSingleNode("INSULINTYPE"), "", oTestXml)
			);
		}
		else if (nMedicalTypeId == this.m_oConsts.TEMPERATURE_DB_ID)
		{
			var res = parseFloat(oTestXml.selectSingleNode("TEMPERATURE").text);
			var result = "";
			var strMeasure = this.m_oPageParams.Celsius;

			if (this.m_oConsts.METRIC == 0) // american system
			{
				strMeasure = this.m_oPageParams.Fahrenheit;
				if (measureUnit != 7)
				{
					res = this.convertMeasure(2, res, true, 3);
				}
			}
			else	// non american system
			{
				if (measureUnit == 7)
				{
					res = this.convertMeasure(2, res, false, 3);
				}
			}

			result = (
					res + " " + strMeasure
				);

			strTestResults = (
				result
			);
		}
		else if (nMedicalTypeId == this.m_oConsts.WEIGHT_DB_ID)
		{
			var res = parseFloat(oTestXml.selectSingleNode("WEIGHT").text);
			var strMeasure = this.m_oPageParams.kg;

			if ((this.m_oConsts.METRIC == 0)) // american system
			{
				strMeasure = this.m_oPageParams.Pound;
				if (measureUnit != 5)
				{
					res = this.convertMeasure(9, res, true, 3);
				}
			}
			else // non-american system
			{
				if (measureUnit == 5)
				{
					res = this.convertMeasure(9, res, false, 3);
				}
			}

			strTestResults = (
					res + " " + strMeasure
				);

		}
		else if (nMedicalTypeId == this.m_oConsts.HEARTRATE_DB_ID) {
			if (oTestXml.selectSingleNode("HRAVERAGE")) {
				strTestResults = (
					this.m_attTestResultCaptions["HRAverage"] + " : " +
					oTestXml.selectSingleNode("HRAVERAGE").text + " BPM  " +
					this.m_attTestResultCaptions["HRMax"] + " : " +
					oTestXml.selectSingleNode("HRMAX").text + " BPM  " +
					this.m_attTestResultCaptions["HRMin"] + " : " +
					oTestXml.selectSingleNode("HRMIN").text + " BPM"

				);
			}
		}
		else if (nMedicalTypeId == this.m_oConsts.BODYFAT_DB_ID) {
			if (oTestXml.selectSingleNode("BODYFAT")) {
				strTestResults = (
					this.m_attTestResultCaptions["BodyFat"] + " : " +
					oTestXml.selectSingleNode("BODYFAT").text + "%"
				);
			}
			if (oTestXml.selectSingleNode("BMI")) {
				strTestResults += (" " +
					this.m_attTestResultCaptions["BMI"] + " : " +
					oTestXml.selectSingleNode("BMI").text);
			}
		}
		else if (nMedicalTypeId == this.m_oConsts.PEAKFLOW_DB_ID) {
			if (oTestXml.selectSingleNode("FVC")) {
				strTestResults = (
					this.m_attTestResultCaptions["Fvc"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("FVC"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["Pef"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("PEF"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["Fev1"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("FEV1"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["Fev1Fvc"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("FEV1_FVC"), "", oTestXml)
				);
			}
		}
		else if (nMedicalTypeId == this.m_oConsts.SPO2_DB_ID) {
			if (oTestXml.selectSingleNode("SPO2AVERAGE")) {
				strTestResults = (
					this.m_attTestResultCaptions["SPO2Average"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("SPO2AVERAGE"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["SPO2Max"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("SPO2MAX"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["SPO2Min"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("SPO2MIN"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["HRAverage"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("HRAVERAGE"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["HRMax"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("HRMAX"), "", oTestXml) + " " +
					this.m_attTestResultCaptions["HRMin"] + " : " +
					oXmlUtils.getNodeValue(oTestXml.selectSingleNode("HRMIN"), "", oTestXml) + " "
				);
			}
		}
	}

	return strTestResults;
}


function
Utils_SendSMSByMail(
	strRouterUrl,
	strLotID,
	strPatientID,
	strSMSMessage,
	strSMSFormat,
	oXmlAdditionlReplacedValues,
	oConsts,
	expertId
) {
	var name;
	var strID;
	var type;
	if (typeof strLotID == typeof undefined)
	{
		name = "ID";
		strID = strPatientID;
		type = 0;
	}
	else if (typeof strPatientID == typeof undefined)
	{
		name = "LOTID";
		strID = strLotID;
		type = 12;
	}

	var smsName = "SMS" + name;

	var oRpcMngr = new RpcMngr(strRouterUrl);
	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("sendSMSEMail");
	oRpcRequests.addRequestParam(
		"sendSMSEMail",
		this.getXmlUtils().getXmlObject(
			"<FILTER>" +
				"<ITEM>" +
					"<NAME>" + name + "</NAME>" +
					"<VALUE>" + strID + "</VALUE>" +
				"</ITEM>" +
				"<ITEM>" +
					"<NAME>TYPE</NAME>" +
					"<VALUE>" + type + "</VALUE>" +
				"</ITEM>" +
			"</FILTER>"
		).documentElement
	);
	oRpcRequests.addRequestParam(
		"sendSMSEMail",
		this.getXmlUtils().getXmlObject(
			"<SMSMSGTEXT><![CDATA[" + strSMSMessage + "]]></SMSMSGTEXT>"
		).documentElement
	);

	oRpcRequests.addRequestParam(
		"sendSMSEMail",
		this.getXmlUtils().getXmlObject(
			"<EXPERTID><![CDATA[" + expertId + "]]></EXPERTID>"
		).documentElement
	);

	oRpcRequests.addRequestParam(
		"sendSMSEMail",
		this.getXmlUtils().getXmlObject(
			strSMSFormat
		).documentElement
	);
	oRpcRequests.addRequestParam(
		"sendSMSEMail",
		this.getXmlUtils().getXmlObject(
			"<" + smsName + ">" + strID + "</" + smsName + ">"
		).documentElement
	);
	if( oXmlAdditionlReplacedValues ) {
		oRpcRequests.addRequestParam(
			"sendSMSEMail",
			oXmlAdditionlReplacedValues
		);
	}

	//prompt("",oRpcRequests.getXml().xml);
	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	//prompt("",oRpcResponses.getResponse("sendSMSEMail").getResultContent().xml);

	var oRes = oRpcResponses.getResponse("sendSMSEMail");
	var errorMsgParam = (typeof strLotID == typeof undefined) ? oConsts.PatientsEmailFieldNotValid : oConsts.EmailFieldNotValid;
	if (oRes) {
		if (!oRes.isErrorResponse()) {
			var m = new MessageBox(
				oConsts.MessageSentSuccessfully,
				//oRes.getMessage(),
				"OK",
				oConsts.OK
			);
			m.show();
			return true;
		}
		else {
			var m = new MessageBox(
				errorMsgParam,
				"Error",
				oConsts.OK
			);
			m.show();
			return false;
		}
	}
	else {
		var m = new MessageBox(
				errorMsgParam,
				"Error",
				oConsts.OK
			);
			m.show();
			return false;
	}
}

function
Utils_cutWord(
	strWord,
	nMaxLength
) {
	var strReturnValue = strWord;
	var nLength = strWord.length;
	/*
	if( (nMaxLength > nLength) && ((nMaxLength-nLength) < 3) ) {
		nLength = strText.length;
	} else if( nMaxLength < nLength+3 ) {
		strReturnValue = strReturnValue.substr(0,nMaxLength);
		strReturnValue += "...";
	}
	*/
	if( nMaxLength < nLength ) {
		strReturnValue = strReturnValue.substr(0,(nMaxLength-3));
		strReturnValue += "...";
	}

	return strReturnValue;
}

function
Utils_cutText(
	strText,
	nMaxLength
) {
	var arrWords = strText.split(" ");
	var strReturnValue = "";
	var nLength = arrWords.length;

	for(var i=0; i<nLength; i++ ) {
		var strCutWord = this.cutWord(arrWords[i],nMaxLength);

		if( i == 0 )
			strReturnValue += strCutWord;
		else
			strReturnValue += " " + strCutWord;
	}

	return strReturnValue;
}

function
Utils_convertEscapeChars(
	strText
) {
	var ret = strText.replace(/&/g, "&amp;");
	ret = ret.replace(/</g, "&lt;");
	ret = ret.replace(/>/g, "&gt;");
	return ret;
}

function
Utils_sleep(
	millis
)
{
	var sleeping = true;

	var startTime = new Date().getTime();
	while (sleeping)
	{
		var alarmTime = new Date().getTime();
		if (alarmTime - startTime > millis)
		{
			sleeping = false;
		}
	}
}

function
Utils_getPageParamsRequest(
	nPageID,
	strRoutingServlet,
	strResponseName,
	bShowResponseErrorMessage
) {
	if (!strResponseName) {
		strResponseName = "GetPageParams";
	}

	if (bShowResponseErrorMessage == null) {
		bShowResponseErrorMessage = true;
	}

	var oPageConsts;
	var oRpcMngr = new RpcMngr(strRoutingServlet);
	var oRpcRequests = new RpcRequests("1001");

	this.setPageParamsRequest(
		oRpcRequests,
		nPageID
	);

	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oRes = oRpcResponses.getResponse(strResponseName);
		if (oRes) {
			if (!oRes.isErrorResponse()) {
				var oXmlConsts = oRes.getResultContent().selectSingleNode("Constants");
				if (oXmlConsts != null) {
					var oPageConsts = new X2WebConstsManager();
					oPageConsts.loadXml(this.getXmlUtils().getXmlObject(oXmlConsts.xml));
				}
			}
			else {
				if (bShowResponseErrorMessage) {
					var m = new MessageBox(
						oRes.getMessage(),
						"Error"
					);
					m.show();
				}
			}
		}

	return oPageConsts;
}

function Utils_setValuesRange(
	eventObject,
	medicalTypeId,
	patientId
)
{
	var isTemperature = false;
	var isGlucose = false;
	var isBloodPressure = false;
	var isWeight = false;
	var isHeartRate = false;

	switch (parseInt(medicalTypeId))
	{
		case 2:		// Temperature
			isTemperature = true;
			break;
		case 3:		// Glucose
			isGlucose = true;
			break;
		case 5:		// Blood pressure
			isBloodPressure = true;
			break;
		case 9:		// Weight
			isWeight = true;
			break;
		case 11:	// Heart rate
			isHeartRate = true;
	}

	var oRpcMngr = new RpcMngr(this.m_strRouterUrl, this);
	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("GetSubValuesRange");
	oRpcRequests.addRequestParam(
		"GetSubValuesRange",
		this.getXmlUtils().getXmlObject(
			"<SUBSCRIBERID>" +
				patientId +
			"</SUBSCRIBERID>"
		).documentElement
	);
	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oResultContent = oRpcResponses.getResponse("GetSubValuesRange").getResultContent();

	var oList = oResultContent.selectNodes("VALUESRANGE/ListItem");
	var nLength = oList.length;

	for (var i = 0; i < oList.length; i++)
	{
		var oItem = oList.item(i);
		var nMedicalTypeId = oItem.selectSingleNode("MEDICALTYPEID").text;
		if (nMedicalTypeId == medicalTypeId)
		{
			eventObject.m_nMinValue = parseFloat(oItem.selectSingleNode("MIN_VALUE").text);
			eventObject.m_nMaxValue = parseFloat(oItem.selectSingleNode("MAX_VALUE").text);

			if (isTemperature || isGlucose || isWeight)
			{
				eventObject.m_nMeasureId = parseInt(oItem.selectSingleNode("MEASUREID").text);
			}
		}
		else if (isBloodPressure && (nMedicalTypeId == 11))
		{
			eventObject.m_nSecondaryMinValue = parseFloat(oItem.selectSingleNode("MIN_VALUE").text);
			eventObject.m_nSecondaryMaxValue = parseFloat(oItem.selectSingleNode("MAX_VALUE").text);
		}
	}
}

function
Utils_getIsValuesOffRange(
	eventObject,
	medicalTypeId,
	oTestXml,
	measureUnit
)
{
	if ((oTestXml != null) && (oTestXml.xml != ""))
	{
		if (eventObject.m_nMinValue == eventObject.m_nMaxValue)
			return false;

		switch (parseInt(medicalTypeId))
		{
			case 2:		// Temperature
				var temperature = parseFloat(oTestXml.selectSingleNode("TEMPERATURE").text);
				if ((measureUnit != 7) && (eventObject.m_nMeasureId == 7))
				{
					temperature = this.convertMeasure(2, temperature, true, 3);
				}
				else if ((measureUnit == 7) && (eventObject.m_nMeasureId != 7))
				{
					temperature = this.convertMeasure(2, temperature, false, 3);
				}

				if ((temperature < eventObject.m_nMinValue) || (temperature > eventObject.m_nMaxValue))
					return true;

				break;
			case 3:		// Glucose
				var glucose = parseFloat(oTestXml.selectSingleNode("SUGARLEVEL").text);
				if ((measureUnit != 9) && (eventObject.m_nMeasureId == 9))
				{
					glucose = this.convertMeasure(3, glucose, true, 3);
				}
				else if ((measureUnit == 9) && (eventObject.m_nMeasureId != 9))
				{
					glucose = this.convertMeasure(3, glucose, false, 3);
				}

				if ((glucose < eventObject.m_nMinValue) || (glucose > eventObject.m_nMaxValue))
					return true;

				break;
			case 5:		// Blood pressure
				var diastolic = parseFloat(oTestXml.selectSingleNode("DIASTOLICPRESSURE").text);
				var systolic = parseFloat(oTestXml.selectSingleNode("SYSTOLICPRESSURE").text);
				if ((diastolic < eventObject.m_nMinValue) || (systolic > eventObject.m_nMaxValue))
					return true;

				var heartRate = parseFloat(oTestXml.selectSingleNode("HEARTRATE").text);
				if ((heartRate < eventObject.m_nSecondaryMinValue) || (heartRate > eventObject.m_nSecondaryMaxValue))
					return true;

				break;
			case 9:		// Weight
				var weight = parseFloat(oTestXml.selectSingleNode("WEIGHT").text);
				if ((measureUnit != 5) && (eventObject.m_nMeasureId == 5))
				{
					weight = this.convertMeasure(9, weight, true, 3);
				}
				if ((measureUnit == 5) && (eventObject.m_nMeasureId != 5))
				{
					weight = this.convertMeasure(9, weight, false, 3);
				}

				if ((weight < eventObject.m_nMinValue) || (weight > eventObject.m_nMaxValue))
					return true;

				break;
			case 11:	// Heart rate
				var hrMin = parseFloat(oTestXml.selectSingleNode("HRMIN").text);
				var hrMax = parseFloat(oTestXml.selectSingleNode("HRMAX").text);
				if ((hrMin < eventObject.m_nMinValue) || (hrMax > eventObject.m_nMaxValue))
					return true;
		}
	}

	return false;
}

function
Utils_setWebAccessWS(
	webAccessWS
)
{
	this.webAccessWS = webAccessWS;
}

function
Utils_getElementsByClassName(
	clsName
)
{
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function
Utils_openLotReport(
	lotId,
	readBySupervisor
)
{
	var readBySupervisorFilter = "NO";
	if (readBySupervisor)
	{
		readBySupervisorFilter = "YES";
	}

	var oRpcMngr = new RpcMngr(this.m_strRouterUrl);
	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("GetLotReport");
	oRpcRequests.addRequestParam(
		"GetLotReport",
		this.getXmlUtils().getXmlObject(
				"<FILTER>" +
					"<ITEM>" +
						"<NAME>LOTID</NAME>" +
						"<VALUE>" + lotId + "</VALUE>" +
						"<RESOLUTION>HIGH</RESOLUTION>" +
						"<FORMAT>PDF</FORMAT>" +
						"<READBYSUPERVISOR>" + readBySupervisorFilter + "</READBYSUPERVISOR>" +
					"</ITEM>" +
				"</FILTER>"
		).documentElement
	);

	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oResultContent = oRpcResponses.getResponse("GetLotReport");

	if ( oResultContent.isErrorResponse() ) {
		var m = new MessageBox(
			this.m_oPageParams.FailedToLoadReport,
			"Debug",
			this.m_oPageParams.OK
		);
		m.show();

		return -1;
	}

	this.m_ReportURL = oResultContent.getResultContent().selectSingleNode("REPORT").text;
	window.open(this.m_ReportURL,name,"width=1024,height=768,menubar=yes,scrollbars=yes,status=yes,toolbar=yes,resizable=yes");
}

function Utils_checkForInt(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return (charCode >= 48 && charCode <= 57);
}

function Utils_checkForFloat(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return ((charCode == 46) || (charCode >= 48 && charCode <= 57));
}

function Utils_isFirstDateEarlier(strFirstDate, strSecondDate, acceptEqual)
{
	if (acceptEqual && (strFirstDate == strSecondDate))
		return true;

	var pos;
	var nMonth;
	var nDay;
	var nYear;
	var oFirstDate = new Date();
	var oSecondDate = new Date();

	var strTempDate = strFirstDate;

	pos = strTempDate.indexOf("/");
	nMonth = strTempDate.substr(0, pos) - 1;
	strTempDate = strTempDate.substr(pos+1, strTempDate.length);

	pos = strTempDate.indexOf("/");
	nDay = strTempDate.substr(0, pos) - 1;
	strTempDate = strTempDate.substr(pos+1, strTempDate.length);

	nYear = strTempDate;

	oFirstDate.setFullYear( nYear );
	oFirstDate.setMonth( nMonth );
	oFirstDate.setDate( nDay );

	strTempDate = strSecondDate;

	pos = strTempDate.indexOf("/");
	nMonth = strTempDate.substr(0, pos) - 1;
	strTempDate = strTempDate.substr(pos+1, strTempDate.length);

	pos = strTempDate.indexOf("/");
	nDay = strTempDate.substr(0, pos) - 1;
	strTempDate = strTempDate.substr(pos+1, strTempDate.length);

	nYear = strTempDate;

	oSecondDate.setFullYear( nYear );
	oSecondDate.setMonth( nMonth );
	oSecondDate.setDate( nDay );

	return (oFirstDate < oSecondDate);
}

function Utils_convertMeasure(medicalTypeId, value, fromMetric, truncatePrecision)
{
	var ret = null;

	switch (medicalTypeId)
	{
		case 2:		// Temperature
			if (fromMetric)
			{
				ret = (((value * 9) / 5) + 32);
			}
			else
			{
				ret = (((value - 32) * 5) / 9);
			}

			break;
		case 3:		// Glucose
			if (fromMetric)
			{
				ret = (value * 18);
			}
			else
			{
				ret = (value / 18);
			}

			break;
		case 9:		// Weight
			if (fromMetric)
			{
				ret = (value * 2.20462262185);
			}
			else
			{
				ret = (value / 2.20462262185);
			}
	}

	if (truncatePrecision && (ret != null))
	{
		ret = "" + ret;
		var indexOfDot = ret.indexOf('.');
		if (indexOfDot > 0)
		{
			var length = ret.length;
			if (indexOfDot + truncatePrecision < length)
			{
				ret = parseFloat(ret.substr(0, indexOfDot + truncatePrecision));
			}
		}
	}

	return ret;
}
