/* MODAL DIALOG */
/* USAGE: win=window.dialogArguments; */
function showModal(url, rtnParam, winWidth, winHeight) {
	showModalDialog('/meds/common/modal/index.jsp?url='+ url, rtnParam, 'status:no; dialogWidth:'+ winWidth+ 'px; dialogHeight:'+ winHeight+ 'px');
}

function showModalScroll(url, rtnParam, winWidth, winHeight) {
	showModalDialog('/meds/common/modal/index_scroll.jsp?url='+ url, rtnParam, 'scroll:yes; status:no; dialogWidth:'+ winWidth+ 'px; dialogHeight:'+ winHeight+ 'px');
}

/* STRING UTILITY */
function trim(TRIM_VALUE) {
	if (TRIM_VALUE.length<1) {
		return "";
	}
	TRIM_VALUE= RTrim(TRIM_VALUE);
	TRIM_VALUE= LTrim(TRIM_VALUE);
	if (TRIM_VALUE=="") {
		return "";
	}
	else {
		return TRIM_VALUE;
	}
}
function RTrim(VALUE) {
	var w_space= String.fromCharCode(32);
	var v_length= VALUE.length;
	var strTemp= "";
	if (v_length<0) {
		return"";
	}
	var iTemp= v_length -1;
	while(iTemp>-1) {
		if (VALUE.charAt(iTemp)==w_space) {
		}
		else {
			strTemp= VALUE.substring(0, iTemp+1);
			break;
		}
		iTemp= iTemp- 1;
	}
	return strTemp;
}
function LTrim(VALUE) {
	var w_space= String.fromCharCode(32);
	if (v_length<1) {
		return "";
	}
	var v_length= VALUE.length;
	var strTemp= "";
	var iTemp= 0;
	while(iTemp<v_length) {
		if (VALUE.charAt(iTemp)==w_space) {
		}
		else {
			strTemp= VALUE.substring(iTemp, v_length);
			break;
		}
		iTemp= iTemp+ 1;
	}
	return strTemp;
}

/* CREATE SYNC/ASYNC REQUEST */
function createRequest() {
	var request= null;
	try {
		request= new XMLHttpRequest();
	}
	catch (e1) {
		try {
			request= new ActiveXObject("Msxm12.XMLHTTP");
		}
		catch (e2) {
			try {
				request= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e3) {
				request= null;
			}
		}
	}
	if (request==null) {
		alert('Error creating request object!');
	}
	else {
		return request;
	}
}

/* SELECT UTILITY */
function twoMultipleSelect(from, to, flag, obj) {
	if (from.selectedIndex<0) {
		if (flag==0) {
			alert('Please select at least One '+ obj+ ' from the List of Available '+ obj+ 's.');
		}
		else if (flag==1) {
			alert('Please select at least One '+ obj+ ' from the List of Added '+ obj+ 's.');
		}
	}
	else {
		while (from.selectedIndex>=0) {
			selected= from.options[from.selectedIndex];
			to.options[to.length]= new Option(selected.text, selected.value);
			from.options[from.selectedIndex]= null;
		}
	}
}
function sendMultipleSelect(to, temp) {
	temp.value= '';
	for(i=0; i<to.length; i++) {
		temp.value+= to.options[i].value;
		if (i!=(to.length-1)) {
			temp.value+= ',';
		}
	}
}
function moveUpList(listField) {
	if ( listField.length == -1) {
		// If the list is empty
		alert("Please Add Items to this Group first.");
	}
	else {
		var selected = listField.selectedIndex;
		if (selected == -1) {
			alert("Please select an Item that you want to change its Sequence.");
		}
		// Something is selected
		else { 
			// If there's only one in the list
			if ( listField.length == 0 ) {
				alert("There is only one entry!\nThe one entry will remain in place.");
			}
			// There's more than one in the list, rearrange the list order
			else {
				if ( selected == 0 ) {
					alert("The first entry in the list cannot be moved up.");
				}
				else {
					// Get the text/value of the one directly above the hightlighted entry as
					// well as the highlighted entry; then flip them
					var moveText1 = listField[selected-1].text;
					var moveText2 = listField[selected].text;
					var moveValue1 = listField[selected-1].value;
					var moveValue2 = listField[selected].value;
					listField[selected].text = moveText1;
					listField[selected].value = moveValue1;
					listField[selected-1].text = moveText2;
					listField[selected-1].value = moveValue2;
					// Select the one that was selected before
					listField.selectedIndex = selected-1;
				}
			}
		}
	}
}
function moveDownList(listField) {
	if ( listField.length == -1) {
		// If the list is empty
		alert("Please Add Items to this Group first.");
	}
	else {
		var selected = listField.selectedIndex;
		if (selected == -1) {
			alert("Please select an Item that you want to change its Sequence.");
		}
		else {
			// Something is selected 
			if ( listField.length == 0 ) {
				// If there's only one in the list
				alert("There is only one entry!\nThe one entry will remain in place.");
			}
			else {
				// There's more than one in the list, rearrange the list order
				if ( selected == listField.length-1 ) {
					alert("The last entry in the list cannot be moved down.");
				}
				else {
					// Get the text/value of the one directly below the hightlighted entry as
					// well as the highlighted entry; then flip them
					var moveText1 = listField[selected+1].text;
					var moveText2 = listField[selected].text;
					var moveValue1 = listField[selected+1].value;
					var moveValue2 = listField[selected].value;
					listField[selected].text = moveText1;
					listField[selected].value = moveValue1;
					listField[selected+1].text = moveText2;
					listField[selected+1].value = moveValue2;
					// Select the one that was selected before
					listField.selectedIndex = selected+1;
				}
			}
		}
	}
}

/* MODULE UTILITY */
function hdrTitle(funcName) {
	if ((window.parent !== undefined) && (window.parent.header !== undefined) && (window.parent.header.functionname !== undefined)) {
		window.parent.header.functionname.value= funcName;
		return true;
	}
	return false;
}

/* FORM TEXT FIELD VALIDATION */
function isValidCharSet(str, charset) {
	for (var i=0; i<str.length; i++) {
		if (charset.indexOf(str.substr(i,1))<0) {
			return false;
		}
	}
	return true;
}
function txtIsEmpty(txtObj) {
	if (trim(txtObj.value)=='') {
		return true;
	}
	else {
		return false;
	}
}
function txtIsNum(txtObj) {
	if (!isValidCharSet(txtObj.value, "0123456789")) {
		return false;
	}
	else {
		return true;
	}
}
function txtIsNumAll(txtObj) {
	format=/^0+$|(^-?(\d*)\.?(\d*)$)/gi;	
	return format.test(txtObj.value);
}
function txtIsDbl(txtObj) {
	if (!isValidCharSet(txtObj.value, ".0123456789")) {
		return false;
	}
	else {
		format=/^0+$|(^-?(\d*)\.?(\d*)$)/gi;	
		return format.test(txtObj.value);
	}
}
function txtIsNegDbl(txtObj) {
	if (!isValidCharSet(txtObj.value, "-.0123456789")) {
		return false;
	}
	else {
		return true;
	}
}
function txtIsDate(txtObj) {
	if (txtIsEmpty(txtObj)) {
		return false;
	}
	if (txtObj.value.length!=10) {
		return false;
	}
	if (!isValidCharSet(txtObj.value, "/0123456789")) {
		return false;
	}
	if ((txtObj.value.substring(2,3)!='/') || (txtObj.value.substring(5,6)!='/')) {
		return false
	}
	return true;
}
function txtIsMoney(txtObj) {
	//format=/^([0-9])+(\.)?([0-9])?([0-9])?$/gi;
	format=/^([0-9])*(\.)?([0-9])?([0-9])?$/gi;
	return format.test(txtObj.value);
}
function txtIsMoneyAll(txtObj) {
	format=/^(-)?([0-9])*(\.)?([0-9])?([0-9])?$/gi;
	return format.test(txtObj.value);
}
function txtIsNPI(txtObj) {
	if (!txtIsEmpty(txtObj)) {
		if ((txtObj.value.length==10) && ((txtObj.value.substring(0,1)=='1') || (txtObj.value.substring(0,1)=='2') || (txtObj.value.substring(0,1)=='3') || (txtObj.value.substring(0,1)=='4'))) {
			return true;
		}
	}
	return false;
}
function charMask(txtObj) {
	format= /^([a-z]|[A-Z])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^a-zA-Z]/g, "");
	}
}
function percentMask(txtObj) {
	if (!txtIsEmpty(txtObj)) {
		if (txtObj.value.substring(0,1)=='%') {
			txtObj.value= txtObj.value.replace(/[%]/g, "");
		}
	}
}
function skuMask(txtObj) {
	format= /^(_|[a-z]|[A-Z]|[0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^_a-zA-Z0-9]/g, "");
	}
}
function skuQtyMask(txtObj) {
	format= /^(\.|_|[a-z]|[A-Z]|[0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^\._a-zA-Z0-9]/g, "");
	}
}
function dblMask(txtObj) {
	format= /^(\.|[0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^\.0-9]/g, "");
	}
}
function negDblMask(txtObj) {
	format= /^(\.|-|[0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^\.-0-9]/g, "");
	}
}
function positiveIntMask(txtObj) {
	format= /^([0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^0-9]/g, "");
	}
}
function positiveIntCommaMask(txtObj) {
	format= /^(,|[0-9])+$/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^,0-9]/g, "");
	}
}
function dateMask() {
	var sMask= "0123456789/";
	var KeyTyped= String.fromCharCode(window.event.keyCode);
	var sSourceValue= window.event.srcElement.value;

	if ((sMask.indexOf(KeyTyped)==-1) || (sSourceValue.length>9)) {
		window.event.keyCode= 0;
		_re = false;
	}
	sSourceValue= sSourceValue.replace(/[//]/gi,"");
	var sTmp= '';

	for (i=0; i<sSourceValue.length; i++) {
		if (sSourceValue.length>3) {
			sTmp= sSourceValue.substring(0,2)+ '/'+ sSourceValue.substring(2,4)+ '/'+ sSourceValue.substring(4);
		}
		else if (sSourceValue.length>2) {
			sTmp= sSourceValue.substring(0,2)+ '/'+ sSourceValue.substring(2);
		}
		else if (sSourceValue.length>1) {
			sTmp= sSourceValue.substring(0,2)+ '/';
		}
		else {
			sTmp= sSourceValue;
		}
	}
    window.event.srcElement.value= sTmp;
}
function alphaNumericCap() {
	txtObj= event.srcElement;
	format= /^[A-Z][0-9]/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^A-Z0-9]/g, "");
	}
}
function alphaNumeric() {
	txtObj= event.srcElement;
	format= /^[A-Z][a-z][0-9]/gi;
	if (!format.test(txtObj.value)) {
		txtObj.value= txtObj.value.replace(/[^A-Za-z0-9]/g, "");
	}
}

/* STRING UTILITY */
function dateRestore(txtObj) {
	if (txtIsDate(txtObj)) {
		return txtObj.value.substring(6)+ txtObj.value.substring(0,2)+ txtObj.value.substring(3,5);
	}
	else {
		return '';
	}
}

/* DATA SELECTION UTILITY */
function pkDiag(frmObj, seq) {
	showModalDialog('/meds/common/modal/pick.jsp?util=diag&seq='+ seq, frmObj, 'status:no; dialogWidth:500px; dialogHeight:500px');
}
function pkHcpcs(frmObj) {
	showModalDialog('/meds/common/modal/pick.jsp?util=hcpcs', frmObj, 'status:no; dialogWidth:500px; dialogHeight:500px');
}
function pkPhy(frmObj) {
	showModalDialog('/meds/common/modal/pick.jsp?util=phy', frmObj, 'status:no; dialogWidth:650px; dialogHeight:500px');
}
function pkSku(frmObj) {
	showModalDialog('/meds/common/modal/pick.jsp?util=sku', frmObj, 'status:no; dialogWidth:700px; dialogHeight:500px');
}
function pkVnd(frmObj) {
	showModalDialog('/meds/common/modal/pick.jsp?util=vnd', frmObj, 'status:no; dialogWidth:550px; dialogHeight:600px');
}

/* DOCUMENT SCANNING UTILITY */
function scanDoc_Modal(param) {
	//showModalDialog('/meds/servlet/com.meds.scan.servlet.ScanCtrlMain?'+ param, window, 'status:no; scroll:no; dialogWidth:320px; dialogHeight:100px');
	window.open('/meds/servlet/com.meds.scan.servlet.ScanCtrlMain?'+ param, 'win', 'width=400; height=100;');
}
function scanDoc_Inventory(office, sku, lotno) {
	scanDoc_Modal('act=inventory&office='+ escape(office)+ '&sku='+ escape(sku)+ '&lotno='+ escape(lotno));
}
function scanDoc_Customer(customerid) {
	scanDoc_Modal('act=customer&customerid='+ escape(customerid));
	//window.open('/meds/servlet/com.meds.scan.servlet.ScanCtrlMain?act=customer&customerid='+ customerid, 'win', 'status');
}
function scanDoc_Order(orderno) {
	scanDoc_Modal('act=order&orderno='+ orderno);
}
function scanDoc_Delivery(trackno) {
	scanDoc_Modal('act=delivery&trackno='+ escape(trackno));
}
function scanDoc_Claim(claimno) {
	scanDoc_Modal('act=claim&claimno='+ escape(claimno));
}
function scanDoc_Title(make, partno) {
	scanDoc_Modal('act=title&make='+ escape(make) + '&partno='+ escape(partno));
}
function scanDoc_Insurance(insurance) {
	scanDoc_Modal('act=insurance&insurance='+ escape(insurance));
}
function scanDoc_Vendor(vendor) {
	scanDoc_Modal('act=vendor&vendor='+ escape(vendor));
}
function scanDoc_PurchaseOrder(pono) {
	scanDoc_Modal('act=po&pono='+ escape(pono));
}
function scanDoc_Payment(paymentno) {
	scanDoc_Modal('act=payment&paymentno='+ paymentno);
}

/* CUSTOMER NOTES */
function popupCusNotes(cusid) {
	window.open('/meds/notes/cus/index.jsp?from=X&cusid='+ cusid, '', 'left=180 top=50 width=800 height=500 scrollbars=yes resizable=yes');
}

/* ONLINE HELP */
function launchTrainingGuide(id) {
	var Train= window.open('/meds/servlet/com.meds.train.servlet.TrainMng?id='+ id, 'Train', 'width=670, height=550');
}

/* CALL INVENTORY ITEM INQUIRY PAGE AS POPUP FROM EXTERNAL PROGRAM */
function launchInvItemInq(sku, office, lotno) {
	window.open('/meds/servlet/com.meds.inv.servlet.InvItem?act=inq&position=X&sku='+ sku+ '&office='+ office+ '&lotno='+ lotno, 'invWin', 'width=850, height=640, left='+ (screen.availWidth-850)/2+ ', top='+ (screen.availHeight-640)/2+ ', scrollbars=yes');
}
function launchInvItemInqFrmOrdSch(sku, office, lotno) {
	window.open('/meds/servlet/com.meds.inv.servlet.InvItem?act=inq&position=X&ext=ordsch&sku='+ sku+ '&office='+ office+ '&lotno='+ lotno, 'invWin', 'width=850, height=640, left='+ (screen.availWidth-850)/2+ ', top='+ (screen.availHeight-640)/2+ ', scrollbars=yes');
}

/* ELEMENT UTILITY */
function clearText(el) {
	if (el!=null) {
		if (el.childNodes) {
			for (var i=0; i<el.childNodes.length; i++) {
				var childNode= el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}
function replaceText(el, txt) {
	if (el!=null) {
		clearText(el);
		var newNode= document.createTextNode(txt);
		el.appendChild(newNode);
	}
}
function getText(el) {
	var txt= '';
	if (el!=null) {
		if (el.childNodes) {
			for (var i=0; i<el.childNodes.length; i++) {
				var childNode= el.childNodes[i];
				if (childNode.nodeValue!=null) {
					txt+= childNode.nodeValue;
				}
			}
		}
	}
	return txt;
}
function verifyNPI(s)
{
	window.open('/meds/claim/npi.jsp?npi='+s,'','');	
}

function chkDbl(e)
{
	o = e.srcElement;
	if(o.value!='' && !txtIsDbl(o))
	{
		alert('Invalid Format');
		o.select();
		return false;
	}
	return true;
}

function chkInt(e)
{
	o = e.srcElement;
	if(o.value!='' && !txtIsNum(o))
	{
		alert('Invalid Format');
		o.select();
		return false;
	}
	return true;
}

function ot2()
{
	window.open('/meds/ot2/Online_Training.jsp', 'Train', 'width=870, height=600, resizable');
}

function nppes()
{
	window.open('https://nppes.cms.hhs.gov/NPPES/Welcome.do', 'NPPES', 'width=870, height=600, resizable, scrollbars, status');
}

function removeRow()
{
	var row = event.srcElement.parentNode.parentNode;
	var tbody = row.parentNode;
	tbody.removeChild(row);
}

function jedi(color)
{
	if(color == "1")
		color = "#F2F0CC";
	else if(color == "0")
		color = "white";
	var row = event.srcElement;
	while(row.tagName != 'TR')
		row = row.parentNode;
	for(i=0;i<row.childNodes.length;i++)
		row.childNodes[i].style.backgroundColor = color;
}

function getModalDiv(topId)
{
	modalId = "_divModalOverlay";
	modalDiv = document.getElementById(modalId);
	if(!modalDiv)
	{
		var newdiv = document.createElement("div");
		newdiv.className = "modalLayer";
		newdiv.style.zIndex=document.getElementById(topId).style.zIndex-1;
		newdiv.id = modalId;

		document.getElementById(topId).parentNode.appendChild(newdiv);

		modalDiv = newdiv;
		
		window.onscroll = function ()
		{
			modalDiv.style.height = screen.availHeight;
			modalDiv.style.width = screen.availWidth-20;
		};
	}
	
	return modalDiv;
}

function searchBlock(id)
{
	oriDiv = document.getElementById(id);
	modalId = "_divModalSearchBlock";
	modalDiv = document.getElementById(modalId);
	if(!modalDiv)
	{
		var newdiv = document.createElement("div");
		newdiv.id = modalId;
		document.body.appendChild(newdiv);
		
		modalDiv = newdiv;
		modalDiv.className="modalLayer";
		modalDiv.style.zIndex=oriDiv.style.zIndex+10;
		//modalDiv.style.background="#DAB2B2";
		modalDiv.style.fontSize="10em";
		modalDiv.style.textAlign="center";
		//modalDiv.innerHTML="Searching......";
		modalDiv.style.backgroundImage="url(/meds/images/enterprise.jpg)";
		modalDiv.style.backgroundRepeat="no-repeat";
		modalDiv.style.backgroundPosition="center center";
		modalDiv.style.display="block";
	}
	else
	{
		document.body.removeChild(modalDiv);		
	}
}

function btnPrint(id)
{
	document.getElementById(id).style.display="none";
	print();
}

function validateFocus(o, msg)
{
	if(trim(o.value) == ""){
		alert(msg);
		o.focus();
		return false;
	}
	
	return true;
}

function validateSelect(o, msg)
{
	if(trim(o.value) == ""){
		alert(msg);
		o.select();
		return false;
	}
	
	return true;
}

function flipDiv(id)
{
	if (document.getElementById(id).style.display == "none")
	{
		document.getElementById(id).style.display="block";
	}
	else
	{
		document.getElementById(id).style.display="none";
	}
}

function __getInvoiceFunc(pdf) {
	return ((pdf) ? (__doInvoicePdf) : (__doInvoiceJsp));
}
function __doInvoicePdf(invoices, seq) {
	window.open('/meds/servlet/com.meds.form.servlet.Invoice?invoiceno=' + invoices, 'invPdfWin' + seq, 'left=40, top=40, width=800, height=600, scrollbars=yes, resizable=yes');
}
function __doInvoiceJsp(invoices, seq) {
	window.open('/meds/rep/invoice/invoicerpt_print.jsp?invoiceno=' + invoices, 'invJspWin' + seq, 'left=40, top=40, width=800, height=600, scrollbars=yes, resizable=yes');
}
function doInvoice(pdf, invoices, seq) {
	var limit = 1900;
	var func = __getInvoiceFunc(pdf);
	if ((invoices != null) && (invoices.length > 0)) {
		if (invoices.length > limit) {
			var temp = invoices.substring(0, limit);
			while (temp.substring(temp.length - 1) != ',') {
				temp = temp.substring(0, temp.length - 1);
			}
			func(temp.substring(0, temp.length - 1), seq);
			doInvoice(pdf, invoices.substring(temp.length), ++seq);
		}
		else {
			func(invoices, seq);
		}
	}
}

function fOpenOrderChecklist(orderNumber, timing) {
	window.open('/meds/order/checklist/index.jsp?orderNumber=' + orderNumber + '&timing=' + timing, 'oclWin', 'width=750, height=600, status=yes, scrollbars=yes, resizable=yes');
}

function isCreditCardValid(number) {
	if (number !== undefined) {
		number = String(number);
		if (number.length > 0) {
			var total = 0;
			for (var idx = (number.length - 1), ctr = 0; idx >= 0; idx--, ctr++) {
				var digit = Number(number.substring(idx, (idx + 1)));
				if (!isNaN(digit)) {
					if ((digit >= 0) && ( digit <= 9)) {
						total += (((ctr % 2) == 0) ? (digit) : ((parseInt((2 * digit) / 10)) + ((2 * digit) % 10)));
					}
				}
			}
			if (total > 0) {
				if ((total % 10) == 0) {
					return true;
				}
			}
		}
	}
	return false;
}
