var xmlHttp; 
var xmlHttp2; 

//*************************************************************************************************
function GetXmlHttpObject(xmlHttp)
{
	try
	{	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{	//Internet Explorer
		try
		{	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}
		catch (e)
		{	try
			{	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}
			catch (failed)
				{	alert("This version of Internet Explorer does not support xmlHttp. Try another browser");	}
		}
	}	
	return xmlHttp;
}

//*************************************************************************************************
function GetXmlHttpObject2(xmlHttp2)
{
	try
	{	// Firefox, Opera 8.0+, Safari
		xmlHttp2=new XMLHttpRequest();
	}
	catch (e)
	{	//Internet Explorer
		try
		{	xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");	}
		catch (e)
		{	try
			{	xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");	}
			catch (failed)
				{	alert("This version of Internet Explorer does not support xmlHttp. Try another browser");	}
		}
	}	
	return xmlHttp2;
}


//*************************************************************************************************
function clearlistbox(lb)
{
	for (var i=lb.options.length-1; i>=0; i--)
	{
		lb.options[i] = null;
	}
	lb.selectedIndex = -1;
}

//*************************************************************************************************
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//*************************************************************************************************
function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//*************************************************************************************************
function clearlistbox(lb){
  for (var i=lb.options.length-1; i>=0; i--){
    lb.options[i] = null;
  }
  lb.selectedIndex = -1;
}

//*************************************************************************************************
function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

//*************************************************************************************************
function setSQL()
{
	// divSQL1 Start the SQL statement with a quote and select the client name
	document.getElementById("divSQL01").innerHTML = "\" Where CS.ClientName = '" + window.opener.document.getElementById("selClientList").value + "'\"";
	// set all the other SQL parms to quote space quote
	document.getElementById("divSQL02").innerHTML = "\" \"";
	document.getElementById("divSQL03").innerHTML = "\" \"";
	document.getElementById("divSQL04").innerHTML = "\" \"";
	document.getElementById("divSQL05").innerHTML = "\" \"";
	document.getElementById("divSQL06").innerHTML = "\" \"";
	document.getElementById("divSQL07").innerHTML = "\" \"";
	document.getElementById("divSQL08").innerHTML = "\" \"";
	document.getElementById("divSQL09").innerHTML = "\" \"";
	document.getElementById("divSQL10").innerHTML = "\" \"";
	document.getElementById("divSQL11").innerHTML = "\" \"";
	document.getElementById("divSQL12").innerHTML = "\" \"";
	document.getElementById("divSQL13").innerHTML = "\" \"";
	document.getElementById("divSQL14").innerHTML = "\" \"";
	document.getElementById("divSQL15").innerHTML = "\" \"";
	document.getElementById("divSQL16").innerHTML = "\" \"";
	document.getElementById("divSQL17").innerHTML = "\" \"";
	document.getElementById("divSQL18").innerHTML = "\" \"";
	document.getElementById("divSQL19").innerHTML = "\" \"";
	document.getElementById("divSQL20").innerHTML = "\" \"";

	// divSQL2 Get the chosen states
	element = document.getElementById("selClientStates"); 
	target = "";
	var x = 0;
	for (var i = 0; i < element.options.length; i++) 
	{ 		
		if(element.options[i].selected)
		{
			if (x==1)
				{ target += ","; }
			else
				{ target += "\" And CS.ProviderState In("; }
			target += "'" + element.options[i].value + "'";
			x = 1;
		}
	}
	if (x==1) 
	{	
		target += ")\""; 	
		document.getElementById("divSQL02").innerHTML = target;
	}
	else
	{
		document.getElementById("divSQL02").innerHTML = "\" \"";
	}

	document.getElementById("selCriteria").innerHTML = "";

	// divSQL3 Get the chosen state - counties
	element = document.getElementById("selClientStateCounties"); 
	target = "";
	var x = 0;
	for (var i = 0; i < element.options.length; i++) 
	{ 		
		if(element.options[i].selected)
		{
			if (x==1)
				{ target += ","; 
				  document.getElementById("selCriteria").innerHTML += ",";}
			else
				if (document.getElementById("chkExclCounty").checked)
				{ target += "\" And CS.ProviderCounty Not In("; 
				  document.getElementById("selCriteria").innerHTML += "<u>Not</u> ";
				}
				else
				{ target += "\" And CS.ProviderCounty In("; }
			county = element.options[i].value;
			target += "'" + county.substring(3,county.length) + "'";
			document.getElementById("selCriteria").innerHTML += county.substring(3,county.length);
			x = 1;
		}
	}
	if (x==1) 
	{	
		target += ")\""; 	
		document.getElementById("divSQL03").innerHTML = target;
	}
	else
	{
		document.getElementById("divSQL03").innerHTML = "\" \"";
	}


	// Modality check boxes
	var mod=0;
	target="\" ";
	if (document.getElementById("chkCTw").checked
		|| document.getElementById("chkCTwo").checked
		|| document.getElementById("chkCTwwo").checked
		|| document.getElementById("chkMRIw").checked
		|| document.getElementById("chkMRIwo").checked
		|| document.getElementById("chkMRIwwo").checked
		|| document.getElementById("chkPET").checked
		|| document.getElementById("chkMamm").checked
		|| document.getElementById("chkNucMed").checked
		|| document.getElementById("chkRadOnc").checked
		|| document.getElementById("chkScreen").checked
		|| document.getElementById("chkUltra").checked
		|| document.getElementById("chkXRay").checked
		|| document.getElementById("chkOther").checked)
	{
		target += "And C.Class In(";
		mod=1;
	}

	var comma="";
	
	// CT check boxes
	if (document.getElementById("chkCTw").checked)
	{
		target += "'CT w/ contrast'";
		comma=",";
	}
	if (document.getElementById("chkCTwo").checked)
	{
		target += comma;
		target += "'CT w/o contrast'";
		comma=",";
	}
	if (document.getElementById("chkCTwwo").checked)
	{
		target += comma;
		target += "'CT w/ w/o contrast'";
		comma=",";
	}

	target += "\"";
	document.getElementById("divSQL04").innerHTML = target;
	
	// MRI + PET check boxes
	target = "\" ";
	if (document.getElementById("chkMRIw").checked)
	{
		target += comma;
		target += "'MRI w/ contrast'";
		comma=",";
	}
	if (document.getElementById("chkMRIwo").checked)
	{
		target += comma;
		target += "'MRI w/o contrast'";
		comma=",";
	}
	if (document.getElementById("chkMRIwwo").checked)
	{
		target += comma;
		target += "'MRI w/ w/o contrast'";
		comma=",";
	}
	if (document.getElementById("chkPET").checked)
	{
		target += comma;
		target += "'PET'";
		comma=",";
	}

	target += "\"";
	document.getElementById("divSQL05").innerHTML = target;
	
	// Get the NON-AI modalities
	target = "\" ";
	if (document.getElementById("chkMamm").checked)
	{
		target += comma;
		target += "'Mammography'";
		comma=",";
	}
	if (document.getElementById("chkNucMed").checked)
	{
		target += comma;
		target += "'Nuclear Medicine'";
		comma=",";
	}
	if (document.getElementById("chkRadOnc").checked)
	{
		target += comma;
		target += "'Radiation Oncology'";
		comma=",";
	}
	if (document.getElementById("chkScreen").checked)
	{
		target += comma;
		target += "'Screening Orbit'";
		comma=",";
	}
	if (document.getElementById("chkUltra").checked)
	{
		target += comma;
		target += "'Ultrasound'";
		comma=",";
	}
	if (document.getElementById("chkXRay").checked)
	{
		target += comma;
		target += "'X-Ray'";
		comma=",";
	}
	if (document.getElementById("chkOther").checked)
	{
		target += comma;
		target += "'Other'";
		comma=",";
	}
	
	if(comma==",")
	{	target += ")\"";	}
	else
	{	target += "\"";	}

	document.getElementById("divSQL06").innerHTML = target;
	
	// divSQL09 Exclude the chosen modifiers
	target = "\" ";
	if (document.getElementById("chkExclModTC").checked)
	{
		target += " And (CS.Modifier1 Is Null Or Not(CS.Modifier1 = 'TC'))";
		target += " And (CS.Modifier2 Is Null Or Not(CS.Modifier2 = 'TC'))";
	}
	target += "\"";
	document.getElementById("divSQL08").innerHTML = target;

	target = "\" ";	
	if (document.getElementById("chkExclMod26").checked)
	{
		target += " And (CS.Modifier1 Is Null Or Not(CS.Modifier1 = '26'))";
		target += " And (CS.Modifier2 Is Null Or Not(CS.Modifier2 = '26'))";
	}
	target += "\"";
	document.getElementById("divSQL09").innerHTML = target;
	
	target = "\" ";
	var intMonth = parseInt(document.getElementById("DOSFrom_Month_ID").selectedIndex + 1);
	var intDay = parseInt(document.getElementById("DOSFrom_Day_ID").selectedIndex + 1);
	var strFromDate = intMonth.toString() + "/" + intDay.toString() + "/" + document.getElementById("DOSFrom_Year_ID").value;
	var intMonth2 = parseInt(document.getElementById("DOSThru_Month_ID").selectedIndex + 1);
	var intDay2 = parseInt(document.getElementById("DOSThru_Day_ID").selectedIndex + 1);
	var strThruDate = intMonth2.toString() + "/" + intDay2.toString() + "/" + document.getElementById("DOSThru_Year_ID").value;

	target += " And Convert(datetime,'" + strFromDate + "') %3C%3D CS.dateOfService";
	target += " and CS.dateOfService %3C%3D Convert(datetime,'" + strThruDate + "')";
	target += "\"";
	document.getElementById("divSQL10").innerHTML = target;

	document.getElementById("selCriteria").innerHTML += "<br>" + strFromDate + " - " + strThruDate;

	// divSQL11 Get the chosen Place Of Services
	element = document.getElementById("selClientPOS"); 
	target = "";
	var x = 0;
	for (var i = 0; i < element.options.length; i++) 
	{ 		
		if(element.options[i].selected)
		{
			if (x==1)
				{ target += ","; 
				  document.getElementById("selCriteria").innerHTML += ", ";}
			else
				{ target += "\" And CS.PlaceOfService In("; 
				  document.getElementById("selCriteria").innerHTML += "<br>POS = ";}
			target += "'" + element.options[i].value + "'";
			document.getElementById("selCriteria").innerHTML += element.options[i].value;
			x = 1;
		}
	}
	if (x==1) 
	{	
		target += ")\""; 
		document.getElementById("divSQL11").innerHTML = target;
		document.getElementById("selCriteria").innerHTML += "<br>";
	}
	else
	{
		document.getElementById("divSQL11").innerHTML = "\" \"";
		document.getElementById("selCriteria").innerHTML += "<br>All POS<br>";
	}

	// divSQL12 Exclude Allowed Amounts
	target = "\" ";
	target += " And CS.AmountAllowed %3E%3D " + document.getElementById("strAllowAmt").value;
	target += "\"";
	document.getElementById("divSQL12").innerHTML = target;

	//***************************************************
	//***************************************************
	//***************************************************

	// divSQL15, divSQL16, divSQL17 & divSQL18 Choose Upstate or DownState CA/NY rates

	target = "\" ";
	if (document.getElementById("chkNYUpDown").checked)
	{
		target = "\" ";
		target += "On (CS.ProviderState=SWA.USState And Not SWA.USState='NY')";
		target += " Or (SWA.USState='NYUpState' And CS.ProviderState='NY'";
		target += "\"";
		document.getElementById("divSQL15").innerHTML = target;

		target = "\" ";
		target += " And Not CS.ProviderCounty In('BRONX','KINGS','NASSAU','NEW YORK','QUEENS','RICHMOND','SUFFOLK','WESTCHESTER'))";
		target += "\"";
		document.getElementById("divSQL16").innerHTML = target;

		target = "\" ";
		target += " Or (SWA.USState='NYDownState' And CS.ProviderState='NY'";
		target += "\"";
		document.getElementById("divSQL17").innerHTML = target;

		target = "\" ";
		target += " And CS.ProviderCounty In('BRONX','KINGS','NASSAU','NEW YORK','QUEENS','RICHMOND','SUFFOLK','WESTCHESTER'))";
		target += "\"";
		document.getElementById("divSQL18").innerHTML = target;

	}
	else
	{
		target = "\" ";
		target += "On CS.ProviderState=SubString(SWA.USState,1,2)";
		target += " And Len(SWA.USState)=2";
		target += "\"";
		document.getElementById("divSQL15").innerHTML = target;
	}
	target += "\"";
	
	
	// divSQL19 Choose the Proposed Rate To Use
	target = "\"";
	target += document.getElementById("selProposedRate").value;
	target += "\"";
	document.getElementById("divSQL19").innerHTML = target;

	// divSQL20 Use Units ?
	target = "\"";
	if (document.getElementById("chkUnits").checked)
	{
		target += " Case When CS.Units = 0 Then 1 Else CS.Units End";
	}
	else
	{
		target += " 1";
	}
	target += "\"";
	document.getElementById("divSQL20").innerHTML = target;
}

//*************************************************************************************************
function jToggleAI()
{
	if (document.getElementById("chkCTw").checked)
	{
		document.getElementById("chkCTw").checked = 0;
		document.getElementById("chkCTwo").checked = 0;
		document.getElementById("chkCTwwo").checked = 0;
		document.getElementById("chkMRIw").checked = 0;
		document.getElementById("chkMRIwo").checked = 0;
		document.getElementById("chkMRIwwo").checked = 0;
		document.getElementById("chkPET").checked = 0;
	}
	else
	{
		document.getElementById("chkCTw").checked = 1;
		document.getElementById("chkCTwo").checked = 1;
		document.getElementById("chkCTwwo").checked = 1;
		document.getElementById("chkMRIw").checked = 1;
		document.getElementById("chkMRIwo").checked = 1;
		document.getElementById("chkMRIwwo").checked = 1;
		document.getElementById("chkPET").checked = 1;
	}
	setSQL();
}

//*************************************************************************************************
function jResetState()
{
	getClientStates(window.opener.document.getElementById("selClientList").value);
	clearlistbox(document.getElementById('selClientStateCounties'));
}

//*************************************************************************************************
function jResetCounty()
{
	getClientStateCounties();
}

//*************************************************************************************************
function jResetPOS()
{
	getClientPOS(window.opener.document.getElementById("selClientList").value);
}

//*************************************************************************************************
function jToggleMods()
{
	if (document.getElementById("chkExclModTC").checked)
	{
		document.getElementById("chkExclModTC").checked = 0;
		document.getElementById("chkExclMod26").checked = 0;
	}
	else
	{
		document.getElementById("chkExclModTC").checked = 1;
		document.getElementById("chkExclMod26").checked = 1;
	}
	setSQL();
}
//*************************************************************************************************
function jToggleNonAI()
{
	if (document.getElementById("chkMamm").checked)
	{
		document.getElementById("chkMamm").checked = 0;
		document.getElementById("chkNucMed").checked = 0;
		document.getElementById("chkRadOnc").checked = 0;
		document.getElementById("chkScreen").checked = 0;
		document.getElementById("chkUltra").checked = 0;
		document.getElementById("chkXRay").checked = 0;
		document.getElementById("chkOther").checked = 0;
	}
	else
	{
		document.getElementById("chkMamm").checked = 1;
		document.getElementById("chkNucMed").checked = 1;
		document.getElementById("chkRadOnc").checked = 1;
		document.getElementById("chkScreen").checked = 1;
		document.getElementById("chkUltra").checked = 1;
		document.getElementById("chkXRay").checked = 1;
		document.getElementById("chkOther").checked = 1;
	}
	setSQL();
}

//*************************************************************************************************
function getClientStates(str)
{ 
	clearlistbox(document.getElementById('selClientStates'));
	xmlHttp=GetXmlHttpObject(xmlHttp);
	if (xmlHttp==null)
	{
		alert ("Browser does not support this web page");
		return;
	}

	var url="php/getClientStates.php";
	url=url+"?p01="+str;
	url=url+"&sid="+Math.random();	
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

		var data = xmlHttp.responseText.split("\n");
		var clientStateList = document.getElementById('selClientStates');
		
		for (i = 0; i < data.length-1; i++)
		{
			var optionNew = document.createElement('option');
			optionNew.text = data[i];
			optionNew.value = data[i];
			try { clientStateList.add(optionNew, null); }
				catch(ex) { clientStateList.add(optionNew); }
		}	
	getClientStateCounties();
}

//*************************************************************************************************
function getClientStateCounties()
{
	clearlistbox(document.getElementById('selClientStateCounties'));

	xmlHttp=GetXmlHttpObject(xmlHttp);
	if (xmlHttp==null)
	{
		alert ("Browser does not support this web page");
		return;
	}

	var strParm1 = window.opener.document.getElementById("selClientList").value;

	// Get the chosen states
	element = document.getElementById("selClientStates"); 
	target = "";
	var x = 0;
	var strParm2 = " ";
	for (var i = 0; i < element.options.length; i++) 
	{ 		
		if(element.options[i].selected)
		{
			if (x==1)
				{ target += ","; }
			else
				{ target += " And ProviderState In("; }
			target += "'" + element.options[i].value + "'";
			x = 1;
		}
	}
	if (x==1) 
	{	
		target += ")"; 	
		strParm2 = target;
	}

	var url="php/getClientStateCounties.php";
	url=url+"?p01="+strParm1;
	url=url+"&p02="+strParm2;
	url=url+"&sid="+Math.random();	
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

		var data = xmlHttp.responseText.split("\n");

		for (i = 0; i < data.length; i++)
		{
			if (data[i] != null)
			{
				addOption(document.getElementById("selClientStateCounties"),data[i], data[i]); 
			}
		}
		getClientPOS(window.opener.document.getElementById("selClientList").value);

}

//*************************************************************************************************
function getClientPOS(str)
{
	xmlHttp=GetXmlHttpObject(xmlHttp);
	if (xmlHttp==null)
	{
		alert ("Browser does not support this web page");
		return;
	}

	var url="php/getClientPOS.php";
	url=url+"?p01="+str;
	url=url+"&sid="+Math.random();	
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

		clearlistbox(document.getElementById('selClientPOS'));

		var data = xmlHttp.responseText.split("\n");
		var clientPOSList = document.getElementById('selClientPOS');
		
		for (i = 0; i < data.length-1; i++)
		{
			var optionNew = document.createElement('option');
			optionNew.text = data[i];
			optionNew.value = data[i];
			try { clientPOSList.add(optionNew, null); }
				catch(ex) { clientPOSList.add(optionNew); }
		}	
		getRatesList()
}

//*************************************************************************************************
function getRatesList()
{ 
	clearlistbox(document.getElementById('selProposedRate'));
	
	xmlHttp=GetXmlHttpObject(xmlHttp);
	if (xmlHttp==null)
	{
		alert ("Browser does not support this web page");
		return;
	}
	var url="php/getRatesList.php";
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	var data = xmlHttp.responseText.split("\n");
	var rateList = document.getElementById('selProposedRate');

	for (i = 0; i < data.length-1; i++)
	{
		var optionNew = document.createElement('option');
		optionNew.text = data[i];
		optionNew.value = data[i];
		if (i==0)
		{
			optionNew.selected = true;
		}
		try { rateList.add(optionNew, null); }
			catch(ex) { rateList.add(optionNew); }
	}

	document.getElementById('selProposedRate').value=window.opener.document.getElementById("selClientList").value;
	setSQL();
	document.getElementById("divStatus").innerHTML = "";
	
}

function getAllFormElements() 
{
/*
var list = document.getElementsByTagName('*')
itot = list.length

var strDisp="";
for(i = 0;i < itot;i++)
    strDisp += list[i].tagName + "&nbsp;" + list[i].id + "<br>"

document.getElementById("divStatus").innerHTML = strDisp;
*/
	document.getElementById("divStatus").innerHTML += "<br>"+document.getElementById("DOSFrom_Month_ID").selectedIndex;
	document.getElementById("divStatus").innerHTML += "<br>"+document.getElementById("DOSFrom_Day_ID").selectedIndex;
	document.getElementById("divStatus").innerHTML += "<br>"+document.getElementById("DOSFrom_Year_ID").value;
}

//*************************************************************************************************
// Requires divUserInfo set up (preferrably inside divWrapper, after divFooter)
//*************************************************************************************************
function getUserInfo()
{ 
	xmlHttp=GetXmlHttpObject(xmlHttp);
	if (xmlHttp==null)
	{
		alert ("Browser does not support this web page");
		return;
	}
	var url="php/getUserInfo.php";
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=loadUserInfo;
	xmlHttp.send(null);
}

function loadUserInfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("divUserInfo").innerHTML = xmlHttp.responseText;
	}
}

function IsNumeric(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 jRoundNumber(rnum, rlength) 
{ // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; 
}

function jAddCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
