
function markOn(fieldName) {
	var field = document.getElementById(fieldName+'1');
	field.style.backgroundColor='#990000';
	field.style.backgroundImage='none';
	field = document.getElementById(fieldName+'2');
	field.style.backgroundColor='#990000';
	field.style.backgroundImage='none';
}
function markOff(fieldName) {
	var field = document.getElementById(fieldName+'1');
	field.style.backgroundColor='';
	field.style.backgroundImage='url(/rxpath/images/lightbluebg.gif)';
	field = document.getElementById(fieldName+'2');
	field.style.backgroundColor='';
	field.style.backgroundImage='none';
}

function selectNoteType(){
	var newval = document.task.notetype[document.task.notetype.selectedIndex].value;
	var oldval = document.task.note.value;
	document.task.note.value=newval;
	
}
function checkWeight(){
	var td = document.getElementById('td_scanning');
	var weight = document.getElementById('weight');
	var warning = document.getElementById('field_warning');
	if(document.cc.weight.value>1.0){
		td.style.backgroundColor='#FF6666';
		weight.style.backgroundColor='yellow';
		warning.style.textDecoration='blink';
	} else {
		td.style.backgroundColor='white';		
		weight.style.backgroundColor='white';
		warning.style.textDecoration='none';
	}
}
function quickCaculate(){
	if(document.drug.qq.value.length>0 && isNum(document.drug.qq.value)){

	} else {
		alert("You must enter a valid number before you can calculate the GHMC % upcharge.");
		return;	
	}
	if(isNum(document.drug.trueacq.value)){

	} else {
		alert("The true aquisition cost does not exist yet, you need to save this drug first.");
		return;
	}
	var x = ((document.drug.qq.value-document.drug.trueacq.value)/document.drug.trueacq.value)*100;	
	x = round(x,2);
	document.drug.uppercent.value = x;
	document.drug.upfee.value = 0;
}

function quickCaculateDispensingPercent(){
	if(document.drug.qs.value.length>0 && isNum(document.drug.qs.value)){

	} else {
		alert("You must enter a valid number before you can calculate the raw cost.");
		return;	
	}
	if(isNum(document.drug.dispuppercent.value)){

	} else {
		alert("The dispensing percent field has no value, you need to enter a dispensing percent first.");
		return;
	}	
//	var x = ((document.drug.qs.value-(document.drug.cost.value)))/(document.drug.cost.value)*100;	
	var x = document.drug.qs.value/(document.drug.dispuppercent.value/100+1);	
	x = round(x,2);
	document.drug.cost.value = x;
	document.drug.dispupfee.value = 0;
}

function freeDrug(fieldNames){
	var fields = fieldNames.split('~');
	for(var i=0;i<fields.length-1;i++){
		document.getElementById(fields[i]).value='0.00';			
	}	
}

// UTILTIY FUNCTIONS -------------------------------------------------------------------------------------------
function checkEmail(emailAddr) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr)){
		return true;
	}
	return false;
}

function isCreditCardValid(ccnum) {
	var sum = 0;
  	for (var i = ccnum.length - 1; i >= 0; i -= 2){
    		sum += ccnum.charAt(i) * 1 + ccnum.charAt(i - 1) * 2 - (ccnum.charAt(i - 1) >= 5 ? 9 : 0);
	}
  	return ( sum % 10 == 0 );
}

function highlightField(fieldId){
//	if (document.getElementById(fieldId).style.getAttribute("backgroundColor")=='#ffcc99'){
	if(document.getElementById(fieldId).disabled){
		document.getElementById(fieldId).disabled=false;
		document.getElementById(fieldId).style.backgroundColor='#ffcc99';
		document.getElementById(fieldId).value='0';
	} else {
		document.getElementById(fieldId).style.backgroundColor='#ffffff';
		document.getElementById(fieldId).value=' - ';
		document.getElementById(fieldId).disabled=true;		
	}
}

function openSSLWindow(){
	var w = window.open('/rxpath/include/ssl.jsp','printwin','toolbar=no,scrollbars=no,status=no,menubar=no,width=360,height=329', true);
	w.focus();
}

function openShowCCWindow(){
	var w = window.open('/rxpath/include/cc_password.jsp','printwin','toolbar=no,scrollbars=no,status=no,menubar=no,width=300,height=200', true);
	w.focus();
}

function openPrintWindow(newurl){
	var w = window.open('/rxpath/include/print.jsp?print_url='+newurl,'printwin','toolbar=no,scrollbars=no,status=yes,menubar=no,width=800,height=600', true);
	w.focus();
}
function openWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=640,height=480', true);
	w.focus();
}
function openAnotherWindow(newurl){
	var w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=640,height=480', true);
	w.focus();
}
function openWideWindow(newurl){
	var w = window.open(newurl,'winwide','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=680,height=480', true);
	w.focus();
}
function openAnotherSmallWindow(newurl){
	var w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=500,height=400', true);
	w.focus();
}
function openDrugNoteWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=400,height=300', true);
	w.focus();
}
function openPinPayWindow(newurl){
	var w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=1024,height=800', true);
	w.focus();
}
function openOldImagesWindow(newurl){
	var w = window.open(newurl,'winimg','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=800,height=600', true);
	w.focus();
}
function openLongWindow(newurl){
	window.open(newurl,'win','toolbar=no,scrollbars=auto,status=yes,menubar=no,width=640,height=700', true);
}

function openFormsWindow(newurl){
	window.open(newurl,'win','toolbar=yes,scrollbars=yes,status=yes,menubar=yes,width=705,height=770', true);
}
function openHTMLPrintWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=auto,status=yes,menubar=yes,width=675,height=700', true);
	w.print();
}
function openAnotherHTMLPrintWindow(newurl){
	var w = window.open(newurl,'win2','toolbar=yes,scrollbars=auto,status=yes,menubar=yes,width=675,height=700', true);
	w.print();
}
function openWindowToolbar(newurl){
	window.open(newurl,'win','toolbar=yes,scrollbars=auto,status=yes,menubar=no,width=640,height=480', true);
}

function openLargeWindow(newurl){
	var w = window.open("/rxpath/blankwindow.htm",'win','toolbar=no,resizable=yes,scrollbars=yes,menubar=no,status=yes,width=800,height=600', true);
	w = window.open(newurl,'win','toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=yes,width=800,height=600', true);
	w.focus();
}

function openEditOrderWindow(newurl){
	w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=yes,width=900,height=700', true);
	w.focus();
}

function openRxHistoryWindow(newurl){
	w = window.open(newurl,'winrxs','toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=yes,width=800,height=700', true);
	w.focus();
}

function openEditRxWindow(newurl){
	w = window.open(newurl,'winrx','toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=yes,width=680,height=480', true);
	w.focus();
}

function openLargerWindow(newurl){
	var w = window.open("/rxpath/blankwindow.htm",'win','toolbar=no,scrollbars=yes,menubar=no,status=yes,width=1000,height=800', true);
	w = window.open(newurl,'win','toolbar=no,scrollbars=yes,menubar=no,status=yes,width=1000,height=800', true);
	w.focus();
}


function copyImageSplit(){
	var answer = confirm("Are you sure you want to make a copy of this image?");
	if(answer){
		openSmallWindow('execute.do?action=copyimage')
	} 
	
}

function saveImageSplit(){
	if(document.cc.patientcode.value.length==0){
		alert("The patient ID was not entered.");
		return;
	} 
	
	if(document.cc.desc.value.length==0){
		alert("No image description was entered.");
		return;
	}
	
	document.cc.action.value = 'saveimagerimage'; 
	document.cc.submit();		
	
}

function deleteImageSplit(){
	var answer = confirm("Are you sure you want to delete all pages in this image?");
	if(answer){
		document.cc.action.value = 'deleteimagerimage'; 
		document.cc.submit();
	} 	
}

function deleteDrug(drugPid){
	if(drugPid==0){
		alert('Drug is not yet created');
	} else {
		var answer = confirm("Are you sure you want to delete this drug?");
		if(answer){
			location.href='/rxpath/execute.do?action=drugadmin&title=S4DA&drug_pid='+drugPid;	
		} 		
	}
}

function saveDrug(){
	document.drug.title.value = 'S5DA'; 
	document.drug.submit();
}

function copyDrug(){
	document.drug.title.value = 'S6DA'; 
	document.drug.submit();
}

function deleteThirdParty(tpartyPid){
	if(tpartyPid==0){
		alert('Affiliate is not yet created');
		
	} else {
		var answer = confirm("Are you sure you want to delete this affiliate?");
		if(answer){
			location.href='/rxpath/execute.do?action=tpartyadmin&title=S4TA&tparty_pid='+tpartyPid;	
		} 		
	}
}

function skipImageSplit(){
	var answer = confirm("Are you sure you want to skip this image?\nThis will place the image back at the end of the queue.");
	if(answer){
		document.cc.action.value = 'skipimagerimage'; 
		document.cc.submit();
	} 
	
}

function selectPatientOrder(patientPid, orderPid, isPatientOnly){
	top.cc.patientcode.value = patientPid;
	top.cc.ordercode.value = orderPid;
	if(isPatientOnly==1){
		top.cc.imagetype[0].checked=true;
	} else {
		top.cc.imagetype[1].checked =true;
	}
	
	top.cc.desc.focus();
	
}

function removeDrugFormulary(tpartyPid, drugPid){
	var answer = confirm("This will remove the drug from the formulary.");
	if(answer){
		location.href='../execute.do?action=formulary&title=S3FORMULARY&tparty_pid='+tpartyPid+'&drug_pid='+drugPid;	
	} 
}

function removeDrugCopayFormulary(tpartyPid, drugPid){
	var answer = confirm("This will remove the drug from the formulary. Note, any unsaved changes will be lost.");
	if(answer){
		location.href='../execute.do?action=formulary&title=S3FORMULARY&tparty_pid='+tpartyPid+'&drug_pid='+drugPid;	
	} 
}

function saveDrugFormulary(tpartyPid, drugPid, qty, copay){
	if(qty.length==0 || copay.length==0){
		alert('You must enter a package quantity and copay before saving.');	
	} else {
		location.href='../execute.do?action=formulary&title=S4FORMULARY&tparty_pid='+tpartyPid+'&drug_pid='+drugPid+'&copay='+copay+'&qty='+qty;		
	}
}

function openLargeResizableWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,resizable=yes,scrollbars=yes,menubar=no,status=yes,width=800,height=600', true);
	w.focus();
}

function openAnotherLargeWindow(newurl){
	var w = window.open("/rxpath/blankwindow.htm",'win2','toolbar=no,scrollbars=yes,menubar=no,status=yes,width=800,height=600', true);
	w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,menubar=no,status=yes,width=800,height=600', true);
	
	if(newurl.indexOf('title=S3PO')>-1){
		var orderPid = newurl.substring(newurl.indexOf('&order_pid=')+11);
		orderPid = orderPid.substring(0,orderPid.indexOf('&'));
		var timedCode = "window.opener.location.href='/rxpath/execute.do?action=updateorder&title=S2RO&order_pid="+orderPid+"';window.close();";
		window.setTimeout(timedCode, 1500);				
	} else {
		w.focus();	
	}
	
}

function openTechCheckWindow(newurl){
	var w = window.open("/rxpath/blankwindow.htm",'win2','toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=yes,width=1000,height=700', true);
	w = window.open(newurl,'win2','toolbar=no,scrollbars=yes,menubar=no,status=yes,resizable=yes,width=1000,height=700', true);
	w.focus();	
	
}

function searchOrder(orderCode, orderPid){
	var w = window.open('searchorder.jsp?action=S1SOLO&order_pid='+orderPid+'&ordercode='+orderCode,'win2','toolbar=no,scrollbars=auto,status=no,menubar=no,width=400,height=200', true);
	w.focus();	
}


function openReportWindow(action, title, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10){
	if(v1.length>0){
		v1 = '&'+v1
	}
	if(v2.length>0){
		v2 = '&'+v2
	}
	if(v3.length>0){
		v3 = '&'+v3
	}
	if(v4.length>0){
		v4 = '&'+v4
	}
	if(v5.length>0){
		v5 = '&'+v5
	}
	if(v6.length>0){
		v6 = '&'+v6
	}
	if(v7.length>0){
		v7 = '&'+v7
	}
	if(v8.length>0){
		v8 = '&'+v8
	}
	if(v9.length>0){
		v9 = '&'+v9
	}
	if(v10.length>0){
		v10 = '&'+v10
	}	
	var newurl = '/rxpath/execute.do?action='+action+'&title='+title+v1+v2+v3+v4+v5+v6+v7+v8+v9+v10;
	window.open(newurl,'report','toolbar=yes,scrollbars=yes,menubar=yes,status=yes,width=1024,height=800', true);
}

function openReportScheduleWindow(name, action, title, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10){
		if(v1.length>0){
			v1 = '&'+v1
		}
		if(v2.length>0){
			v2 = '&'+v2
		}
		if(v3.length>0){
			v3 = '&'+v3
		}
		if(v4.length>0){
			v4 = '&'+v4
		}
		if(v5.length>0){
			v5 = '&'+v5
		}
		if(v6.length>0){
			v6 = '&'+v6
		}
		if(v7.length>0){
			v7 = '&'+v7
		}
		if(v8.length>0){
			v8 = '&'+v8
		}
		if(v9.length>0){
			v9 = '&'+v9
		}
		if(v10.length>0){
			v10 = '&'+v10
		}	
		var newurl = '/rxpath/include/sched_report.jsp?name='+name+'&action='+action+'&title='+title+v1+v2+v3+v4+v5+v6+v7+v8+v9+v10;
		window.open(newurl,'report','toolbar=yes,scrollbars=yes,menubar=yes,status=yes,width=800,height=600', true);
}

function openOrderInFullViewer(orderPid){
	top.window.location.href="/rxpath/execute.do?action=updateorder&title=S2RO&gotopharmacyview=true&order_pid="+orderPid;	
}

function openImageWindow(newurl){
	var w = window.open('include/imageviewer.jsp?image_url='+newurl,'winimg','toolbar=no,resizable=yes,scrollbars=no,menubar=no,status=yes,width=600,height=800', true);
	w.focus();
}

function openImageUploadWindow(newurl){
	var w = window.open(newurl,'winimg','toolbar=no,scrollbars=auto,menubar=no,status=yes,width=600,height=200', true);
	w.focus();
}

function openSmallWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=auto,status=no,menubar=no,width=400,height=200', true);
	w.focus();
}

function openDrugQuoteWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=auto,status=no,menubar=no,width=550,height=300', true);
	w.focus();
}

function openTinyWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=no,status=no,menubar=no,width=1,height=1', true);
	w.focus();
}

function openCalendar(){
	var w = window.open('/rxpath/include/calendar.jsp','win','toolbar=no,scrollbars=no,status=no,menubar=no,width=220,height=175', true);
	w.focus();
}

function openTallWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=yes,status=no,menubar=no,width=400,height=600', true);
	w.focus();
}

function openTaskWindow(newurl){
	var w = window.open(newurl,'win','toolbar=no,scrollbars=yes,status=no,menubar=no,width=680,height=800', true);
	w.focus();
}

function openCpostWindow(newurl){
	if(newurl.indexOf('S11SO')>-1){
		w = window.open(newurl,'cpost','toolbar=no,scrollbars=auto,status=no,menubar=no,width=400,height=738', true);
		w.focus();	
		return w;
	} else {
		var w = window.open("/rxpath/blankwindow.htm",'cpost','toolbar=no,scrollbars=auto,menubar=no,status=no,width=400,height=738', true);
		w.close();
		var timedCode = "var w = window.open('"+newurl+"','cpost','toolbar=no,scrollbars=auto,status=no,menubar=no,width=400,height=738', true); w.focus();";				
		window.setTimeout(timedCode, 1000);								
		return w;	
	}

}

function openPaypalWindow(newurl){
	var w = window.open(newurl,'win','toolbar=yes,scrollbars=yes,menubar=no,status=yes,width=800,height=700', true);
	w.focus();
}

function billOrder(){  
	var answer = confirm("Are you sure you want to bill this order?");
	if(answer){ 
		document.task.submit();
	} 
}

function markTxVoid(patientPid, rxPid, txPid){
	var answer = confirm("VOID Tx: Are you sure you want to continue...");
	if(answer){ 
		var url = "../execute.do?action=updatepatient&title=voidtx&rx_pid=" + rxPid +"&tx_pid=" + txPid;
		location.href=url;
	} 
}

function downloadWinRxBatch(){
	var answer = confirm("Are you sure you want to download WinRx batch file, this remove the file from the server.");
	if(answer){ 
		var newurl = "batch.text?action=downloadwinrx";
		var w = window.open(newurl,'winimg','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=400,height=300', true);
		w.focus();	
//		location.href=url;
	} 
}

function emailWinRxBatch(){
	var answer = confirm("Are you sure you want to bundle up and email the WinRx refills.");
	if(answer){ 
		var newurl = "execute.do?action=EMAILWINRXBATCHREFILL";
		var w = window.open(newurl,'winimg','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=400,height=300', true);
		w.focus();	
	} 
}

function pushPatientToWinRxBatch(patientPid){
	var answer = confirm("Are you sure you want to push this patient profile into the WinRx batch file.");
	if(answer){ 
		var newurl = "execute.do?action=ppwinrx&patientpid="+patientPid;
		var w = window.open(newurl,'winimg','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=400,height=300', true);
		w.focus();	
	} 
}

function adjustTx(patientPid, rxPid, txPid){
	var answer = confirm("VOID Tx: Are you sure you want to continue...");
	if(answer){ 
		var url = "../execute.do?action=updatepatient&title=voidtx&rx_pid=" + rxPid +"&tx_pid=" + txPid;
		location.href=url;
	} 
}

function markRxStop(patientPid, rxPid){
	var answer = confirm("STOP Rx: Are you sure you want to continue...");
	if(answer){ 
		var url = "../execute.do?action=updatepatient&title=stoprx&rx_pid=" + rxPid;
		location.href=url;
	} 
}


function markRxVoid(patientPid, rxPid){
	var answer = confirm("VOID Rx: Are you sure you want to continue...");
	if(answer){ 
		var url = "../execute.do?action=updatepatient&title=voidrx&rx_pid=" + rxPid;
		location.href=url;
	} 
}

function markRxVoidFromOrder(rxPid){
	if(rxPid==0){
		alert('Error: No Rx has been created');
		return;
	}
	var answer = confirm("VOID Rx: Are you sure you want to continue...");
	if(answer){ 
		var url = "../execute.do?action=updatepatient&title=voidrx&navigate=order&rx_pid=" + rxPid;
		location.href=url;
	} 
}

function cancelRxEdit(){
//	var answer = confirm("Are you sure you want to cancel");
//	if(answer){ 
		history.back(-1);
//	} 

}

function saveRxRefill(rxRefillField, rxSigField, rxDaysSupplyField, rxDoctor, rxQtyField, rxRemainingQtyField, drugPid, orderPid, defer, rxPharmacy) {
	var rxdayssupply = document.getElementById(rxDaysSupplyField).value;
	var rxsig = document.getElementById(rxSigField).value;
	var rxremainqty = document.getElementById(rxRemainingQtyField).value;
		
	if(rxsig.length==0 || rxremainqty.length==0 || rxdayssupply.length==0){
		alert('You must fill in the Total Auth, Days Supply and SIG for the Rx.');	
		return;
	}
	
	var rxqty = document.getElementById(rxQtyField).value;
	
	var rxrefill = document.getElementsByName(rxRefillField);
	var refillValue = "";
	for(var i = 0 ; i< rxrefill.length;i++){
		if (rxrefill[i].checked == true){
	        	refillValue = rxrefill[i].value;
		}
	}
	rxDoctor = document.getElementById(rxDoctor).value;
	rxPharmacy = document.getElementById(rxPharmacy).value;
	var url = "execute.do?action=showtask&title=S9RX&drug_pid=" + drugPid + "&physician="+rxDoctor+"&qty="+rxqty+"&refill="+refillValue+"&remain="+rxremainqty+"&rxsig="+rxsig+"&RXDAYSSUPPLY"+drugPid+"="+rxdayssupply+"&order_pid="+orderPid+"&defer="+defer+"&pharmacy="+rxPharmacy;
	location.href=url;
	

}


function addDrugOutOfStock(action, drugPid, jsessionid){
	var answer = confirm("This drug is currently out of stock.\nIf you proceed with this drug you will be required to wait for supply to come in before this order can be processed.\nAre you sure you want to continue adding this drug to your order?");
	if(answer){
		var url = jsessionid+"?action=" + action + "&shopping=true&drug_pid=" + drugPid+ "&qty=1";
		location.href=url;
	}

}

function help(topic){
	var w = window.open('/rxpath/help.jsp?topic='+topic,'helpwin','toolbar=no,scrollbars=yes,status=yes,menubar=no,width=400,height=600', true);					
}

function refundOrder(){
	if(document.task.note.value.length==0){
		alert('You must enter a reason for this refund.');	
	} else {
		var answer = confirm("Are you sure you want to refund this order?");
		if(answer){
			document.task.submit();
		}	
	}

}

function voidManifestTrackingNumber(rawTrackingNumber, codedTrackingNumber, manifestNumber){
	var answer = confirm("Are you sure you want to VOID  "+codedTrackingNumber+" tracking number and line item?");
	if(answer){
		location.href='execute.do?action=manifestadmin&title=S5MA&trackingnumber='+rawTrackingNumber+'&codedtrackingnumber='+codedTrackingNumber+'&manifestnumber='+manifestNumber;
	}
}

function printDoctorScripts(){  
	if(rxselected.length==0){
		alert('You have not selected any orders for printing.');	
	} else {
//		var answer = confirm("This will print the authorized prescriptions for each order you have selected.");
//		if(answer){ 
			var w = window.open('/rxpath/include/printdocauth.jsp?rxselected='+rxselected,'printwin','toolbar=no,scrollbars=no,status=yes,menubar=no,width=800,height=600', true);					
			w.focus();					
//		} 
	}
	
}

function printDoctorScriptsHtml(){  
	if(rxselected.length==0){
		alert('You have not selected any orders for printing.');	
	} else {
		var w = window.open('/rxpath/prescription.pdf?action=physicianprintrx&rxselected='+rxselected,'printwin','toolbar=yes,scrollbars=no,status=yes,menubar=no,width=800,height=600', true);					
		w.focus();					
	}
	
}


function holdOrder(){                              
	if(document.task.holddate.value.length==0 || document.task.notetype.selectedIndex==0){
		alert('You must enter a HOLD DATE and/or select a HOLD TYPE before you can place this order on hold.');
		return;
	} else {
		var answer = confirm("This action will place this order on hold for the time period you requested.");
		if(answer){ 
			document.task.submit();
		} 	
	}
}

function clearDUR(){                              
	if(document.task.clearnote.value.length==0){
		alert('You must enter a REASON before you can clear this DUR.');
		return;
	} else {
		var answer = confirm("This action will clear this DUR allowing the order to continue.");
		if(answer){ 
			document.task.submit();
		} 	
	}
}

function pushBackOrder(previousTaskName){                              
	if(document.task.holddate.value.length==0 || document.task.notetype.selectedIndex==0){
		alert('You must select a REASON before you can push this order back to '+previousTaskName+'.');
		return;
	} else {
		var answer = confirm("This action will push this order back to "+previousTaskName+".");
		if(answer){ 
			document.task.submit();
		} 	
	}
}

function pushBackOrderCanaRx(){                              
	if(document.task.holddate.value.length==0 || document.task.notetype.selectedIndex==0){
		alert('You must select a REASON before you can push this order back to CanaRx.');
		return;
	} else {
		var answer = confirm("This action will push this order back to CanaRx.");
		if(answer){ 
			document.task.submit();
		} 	
	}
}

function removeHoldOrder(){  
	var answer = confirm("This action will remove any hold off this task.");
	if(answer){ 
		document.task.holddate.value='1900-01-01';		
		document.task.submit();
	} 		
}

function searchPatient(){
	if(document.cc.firstname.value.length==0 && document.cc.lastname.value.length==0 && document.cc.phonearea.value.length==0 && document.cc.phone.value.length==0){
		alert("A partial name is required.");
		return false;
	} else {
		return true;
	}
}

function searchBar(){
	if(document.search.tabsearchorder.value.length==0 && document.search.tabsearchpatient.value.length==0){
		alert("An order ID or patient name is required.");
		return false;
	} else {
		return true;
	}
}

function openPatientNotes(patientPid, navigate){
	openLargeResizableWindow('patientnotes.jsp?patient_pid='+patientPid+'&nav='+navigate);
}

function openCanadianPatientAgreement(patientPid){
	openLargeResizableWindow('/rxpath/legal/global_v3.jsp?patient_pid='+patientPid);
}

function openPatientPodimNotes(patientPid){
	openLargeResizableWindow('patientnotes.jsp?patient_pid='+patientPid+'&podim=true');
}

function openPatientOrders(patientPid){
	var newurl  ='/rxpath/include/orderhistory.jsp?patient_pid='+patientPid;
	openLargeResizableWindow(newurl);
}

function openPatientMedical(){
	var newurl  ='/rxpath/include/medical.jsp';
	openAnotherSmallWindow(newurl);
}

function openTpartyNotes(tpartyPid, navigate){
	openLargeResizableWindow('tpartynotes.jsp?tparty_pid='+tpartyPid+'&nav='+navigate);
}

function openPatientImages(patientPid, navigate){
	openLargeWindow('patientimages.jsp?patient_pid='+patientPid+'&nav='+navigate);
}

function createPatientNote(patientPid){
	openWindow('patientnotes.jsp?new=true&patient_pid='+patientPid);
}

function modifyBank(patientPid){
	openWindow('bank.jsp?patient_pid='+patientPid);
}

function createTpartyNote(tpartyPid){
	openWindow('tpartynotes.jsp?new=true&tparty_pid='+tpartyPid);
}

function createPatientNoteFromPatientNotes(patientPid){
	openAnotherSmallWindow('patientnotes.jsp?new=true&refresh=true&patient_pid='+patientPid);
}

function createTpartyNoteFromTpartyNotes(tpartyPid){
	openAnotherSmallWindow('tpartynotes.jsp?new=true&refresh=true&tparty_pid='+tpartyPid);
}

function saveQuote(){
//	if(document.cc2.patientname.value.length==0){
//		alert('You must enter the patient\'s name.');
//	}else{
		document.cc2.submit();
//	}
}

function cancelOrder(){
	if(document.task.note.value.length==0){
		alert('You must enter a reason for cancelling this order.');	
	} else {
		var answer = confirm("Are you sure you want to cancel this entire order?");
		if(answer){
			document.task.submit();
		}	
	}
}


function undoTask(){
	var answer = confirm("Are you sure you want to UNDO this task? By proceeding you will set this task to NOT STARTED?");
	if(answer){
		document.task.submit();
	}
}

function clearCanarxOrderId(){
	var answer = confirm("Are you sure you want to clear the CanaRx Order ID? By proceeding you will break this order's synchronization with the CanaRx system.");
	if(answer){
		document.task.submit();
	}
}

function deleteImage(action, title, imagePid){
	var answer = confirm("Are you sure you want to delete this image?");
	if(answer){
		document.location.href='/rxpath/execute.do?action='+action+'&title='+title+'&image_pid='+imagePid;
	}
}

function deleteDrugImage(action, title, drugPid, imagePid){
	var answer = confirm("Are you sure you want to delete this image?");
	if(answer){
		document.location.href='/rxpath/execute.do?action='+action+'&title='+title+'&image_pid='+imagePid+'&drug_pid='+drugPid;
	}
}

function doctorAuthorize(formName){  
//	var answer = confirm("Any authorizations or notes you've entered will be submitted.");
//	if(answer){ 
		document.RX.submit();
//	} 
}

function chooseRxSendMethod(methodName){

	if(methodName.indexOf('Upload')>-1  ){
		openImageUploadWindow('include/image.jsp');								
	} else if(methodName.indexOf('doctor')>-1){
		openWindow('include/docfaxrequest.jsp');									
	} else if(methodName.indexOf('transfer')>-1){
		openWindow('include/pharmacytransfer.jsp');									
	}
}


function checkField(field, errmsg, formErrorMsg) {
	if (isEmpty(field)){
		if (formErrorMsg.length==0){
			field.focus();
		}	
		field.style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		field.style.backgroundColor='#FFFFFF';
		return formErrorMsg;
	}
}

function checkCCField(field, errmsg, formErrorMsg) {
	if (isEmpty(field)){
		if (formErrorMsg.length==0){
			field.focus();
		}	
		field.style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		if(!isCreditCardValid(field.value)){				
			field.style.backgroundColor='#FFCC99';
			formErrorMsg += "The credit card number you entered is invalid, please check the number.\n";
			field.focus();
		} else {
			field.style.backgroundColor='#FFFFFF';
		}
		return formErrorMsg;		
	}
}

function checkEmailField(field, errmsg, formErrorMsg) {
	if (isEmpty(field) || !checkEmail(field.value)){
		if (formErrorMsg.length==0){
			field.focus();
		}	
		field.style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		field.style.backgroundColor='#FFFFFF';
		return formErrorMsg;
	}
}

function checkNumField(field, errmsg, formErrorMsg) {
	var originalMsg = formErrorMsg;
	formErrorMsg = checkField(field, errmsg, formErrorMsg);
	if(originalMsg==formErrorMsg){
		if(!isNum(field.value)){
			if (formErrorMsg.length==0){
				field.focus();
			}	
			field.style.backgroundColor='#FFCC99';
			formErrorMsg += errmsg;
		}
	}
	return formErrorMsg;

}

function checkRadioField(field, sectionId, errmsg, formErrorMsg) {
	var valid = false;
	for(var i = 0 ; i< field.length;i++){
		if (field[i].checked == true){
	        	valid = true;
		}
	}
	if (!valid){
		if (formErrorMsg.length==0){
			field[0].focus();
		}	
		document.getElementById(sectionId).style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		document.getElementById(sectionId).style.backgroundColor='#FFFFFF';
		return formErrorMsg;
	}
}

function checkSelectField(field, errmsg, formErrorMsg) {
	var valid = false;
	// skip the element at position zero, assumes a blnak in the first position
	for(var i = 1 ; i< field.length;i++){
		if (field[i].selected == true){
	        	valid = true;
		}
	}
	if (!valid){
		if (formErrorMsg.length==0){
			field[0].focus();
		}	
		field.style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
	} else {
		field.style.backgroundColor='#FFFFFF';
	}

	return formErrorMsg;

}

function checkPasswordField(field, field2, errmsg, formErrorMsg) {
	if (isEmpty(field) || isEmpty(field2) || (field.value!=field2.value) || field.value.length<=4){
		if (formErrorMsg.length==0){
			field.focus();
		}	
		field.style.backgroundColor='#FFCC99';
		field2.style.backgroundColor='#FFCC99';
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		field.style.backgroundColor='#FFFFFF';
		field2.style.backgroundColor='#FFFFFF';
		return formErrorMsg;
	}
}

function markField(fieldName) {
	var field = document.getElementById(fieldName);
	field.style.color='#FFFFFF';
	field.style.backgroundColor='#FF0000';
}

function checkProceduresField(field, errmsg, formErrorMsg) {
	if (isEmpty(field)){
		if (formErrorMsg.length==0){
			
		}	
		formErrorMsg += errmsg;
		return formErrorMsg;		
	} else {
		return formErrorMsg;
	}
}



function copyFieldToField(fieldFrom, fieldTo){
	document.getElementById(fieldTo).value = document.getElementById(fieldFrom).value;
}

function cleanData(data){
	for (var i = 0; i < 250; i++){
		data = data.replace("?"," ");
		data = data.replace("\n",", ");
		data = data.replace("\r",", ");
		data = data.replace("'","`");
		data = data.replace('"',"`");
	}
	return data;
}

function cleanAndReplaceData(data){
	for (var i = 0; i < 250; i++){
		data = data.replace("?"," ");
		data = data.replace("\n"," ");
		data = data.replace("\r"," ");
		data = data.replace("'","`");
		data = data.replace('"',"`");
	}
	return data;
}

function getSelectValues(field){
	var values = '';
	// skip the element at position zero, assumes a blnak in the first position
	for(var i=1; i < field.length; i++){
		if(field[i].selected==true){
			values += field[i].value +', ';		
		}
	}
	if (values.length>0){
		values = values.substring(0,values.length-2);
	}
	return values;
}

function isEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}	


function isNum(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function round(number,X) {
// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}



// AJAX UTILTIY FUNCTIONS -------------------------------------------------------------------------------------------
function newXMLHttpRequest() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
	// unable to create 
      }
    }
  }
  return xmlreq;
}

function getReadyStateHandler(req, responseXmlHandler) {
  return function () {
    if (req.readyState == 4) {       
      if (req.status == 200) {
        responseXmlHandler(req.responseXML);
      } else {
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function resizeWindow(){
   resizeBy(5,1);
}

// AJAX scripts			
function selectSigValue(value){
	if(value.length==0 || value=='' || value.indexOf('n/a')>-1){	
		return;
	}
	var paras = value.split("~");
	var sigcode = 'RXSIG'+paras[2];
	var sigdesc = 'RXSIGDESC'+paras[2];
	window.document.getElementById(sigcode).value=paras[0];
	window.document.getElementById(sigdesc).value=paras[1];
	document.getElementById("display_sig").style.visibility="hidden";		
}
	
function getSigList(chars, drugPid) {
	if(chars.length>0){
		var req = newXMLHttpRequest();
		var handlerFunction = getReadyStateHandler(req, updateSigList);
		req.onreadystatechange = handlerFunction;  
		req.open("POST", "/rxpath/execute.do", true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send("action=ajaxsig&drug_pid="+drugPid+"&sig="+chars);
	}
}

function updateSigList(sigXML) {
	var lastUpdate = 0;
	document.getElementById("display_sig").style.visibility="visible";
	var dataenv = sigXML.getElementsByTagName("dataenv")[0];
	// Check that a more recent document hasn't been processed already
	var generated = dataenv.getAttribute("generated");
	var searchStr = dataenv.getAttribute("display");
	if (generated > lastUpdate) {
		lastUpdate = generated;
		var contents = document.getElementById("list_contents");
		contents.innerHTML = "";
		// Loop over the items
		var items = dataenv.getElementsByTagName("item");
		for (var i = 0 ; i < items.length ; i++) {
			var item = items[i];
			var value1 = item.getElementsByTagName("value1")[0].firstChild.nodeValue;                                               
			var value2 = item.getElementsByTagName("value2")[0].firstChild.nodeValue;                                               
			var value3 = item.getElementsByTagName("value3")[0].firstChild.nodeValue;                                               
			if (value1=="."){
				value1 = "";
			}
			if (value2=="."){
				value2 = " ";
			}
			if (value3=="."){
				value3 = " ";
			}
			var li = document.createElement("option");
			li.value = value1+"~"+value2+"~"+value3;
			li.innerText=value1 + " " + value2;
			contents.appendChild(li);
		}
		if(items.length==0){
			var li = document.createElement("option");
			li.value = "n/a~n/a";
			li.innerText='No SIG found';
			contents.appendChild(li);			
		}
	}	
}
	
function closeTypeAhead(){
	document.getElementById("display_sig").style.visibility="hidden";
}

function mergePatientIntoSession(){
	var answer = confirm("Are you sure you want to merge these two patients into one.");
	if(answer){ 
		var sourcePid = document.getElementById("patientsourcepid").value;
		location.href='execute.do?action=mergepatientintosession&source_pid='+sourcePid;
	} 	
}

function mergePatientOutOfSession(){
	var answer = confirm("Are you sure you want to merge these two patients into one.");
	if(answer){ 
		var sourcePid = document.getElementById("patientsourcepid").value;
		location.href='execute.do?action=mergepatientoutofsession&source_pid='+sourcePid;
	}
}

function viewAffiliateDeductions(){
	var tparty = document.getElementById("tparty").value;
	if(tparty.length==0){
		alert("You must select an affiliate first.");
		return;
	}
	location.href='start.jsp?action=tpartyadmin&title=S23TA&tparty_code='+tparty;
}

function addDrugToInventoryOrder(drugPid, qty){
	location.href='execute.do?action=inventorymgmt&title=S10IOR&drug_pid='+drugPid+'&qty='+qty;
	
}


