var screenWidth = screen.width;
var screenHeight = screen.height;
var browser = navigator.appName;
var b_version = navigator.appVersion;

var LOGIN_HELP_VISIBLE = 0;
function removeHelpContainer() {
	document.body.removeChild(document.getElementById('LOGINHELP_CONTAINER'));
}
function showLoginHelp(type) {


	if(LOGIN_HELP_VISIBLE == 1) {
		fadeOutDiv(10,document.getElementById('LOGINHELP_CONTAINER'),removeHelpContainer);
		LOGIN_HELP_VISIBLE = 0;
		return 1;
	}
	LOGIN_HELP_VISIBLE = 1;
	var div = document.createElement("div");
	div.setAttribute('id','LOGINHELP_CONTAINER');
	setStyles(div,'visibility=hidden,padding=10px,position=absolute,border=5px solid #2c3b58,background=white,width=400px');
	var placer = document.getElementById('LOGHELPPLACER');
	var px = findPosX(placer);
	var py = findPosY(placer);


	var tbl = document.createElement("table");
	var tbod = document.createElement("tbody");
	tbod.setAttribute("id","FPTABLE");	
	var tr,td,txt;

	tr = document.createElement("tr");
	td = document.createElement("td");
	td.setAttribute('align','right');
	td.setAttribute("width","500px");
	td.setAttribute('class','normTxtS');
	td.setAttribute('className','normTxtS');
	td.setAttribute("colspan","2");
	td.setAttribute("colSpan","2");
	try { var span = document.createElement("<span>"); }
	catch(e) { var span = document.createElement("span"); }
	span.style.cursor = "pointer";
	span.onclick = function() { showLoginHelp(); }
	
	txt = document.createTextNode("Close");
	span.appendChild(txt);
	td.appendChild(span);
	tr.appendChild(td);
	tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
	td.setAttribute("align","center");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtL"); td.setAttribute("className","nomrTxtL");
        td.style.borderTop = "0px solid black";
	if(type == 1) { txt = document.createTextNode("Did you forget your login info?"); }
	else { txt = document.createTextNode("Retrieve your login information"); }
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

///////////////////////////////////////////////////////////////////////////////
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        txt = document.createTextNode("\u00a0");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);
///////////////////////////////////////////////////////////////////////////////


	tr = document.createElement("tr");
	td = document.createElement("td");
	td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
	td.setAttribute("class","normTxtL"); td.setAttribute("className","nomrTxtL");
	td.style.borderTop = "0px solid black";
	txt = document.createTextNode("Retrieve by User ID");
	td.appendChild(txt);
	tr.appendChild(td);
	tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtNobold"); td.setAttribute("className","normTxtNobold");
        td.style.borderBottom = "1px solid black";
        txt = document.createTextNode("If you know your user ID, enter it below.  Your log in information will be sent to the e-mail address that you have on file at TheraBill.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

	tr = document.createElement("tr");
	td = document.createElement("td");
	td.setAttribute("width","70px");
	td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
	txt = document.createTextNode("User ID:");
	td.appendChild(txt);
	tr.appendChild(td);

        td = document.createElement("td");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
	try { var txtbox = document.createElement("<input type='text' id='FP_USERID'>"); }
	catch(e) {
		var txtbox = document.createElement("input");
		txtbox.setAttribute("type","text");
		txtbox.setAttribute("id","FP_USERID");
	}
	txtbox.style.width = "150px";
        td.appendChild(txtbox);
	try { var btn = document.createElement("<input type='button' value='Go'>"); }
	catch(e) {
		var btn = document.createElement("input");
		btn.setAttribute("type","button");
		btn.setAttribute("value","Go");
	}
	btn.style.cursor = "pointer";
	btn.onclick = function() { retreivePassByUserID(); }
	td.appendChild(btn);
        tr.appendChild(td);
	tbod.appendChild(tr);

///////////////////////////////////////////////////////////////////////////////
	tr = document.createElement("tr");
	td = document.createElement("td");
	td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
	txt = document.createTextNode("\u00a0");
	td.appendChild(txt);
	tr.appendChild(td);
	tbod.appendChild(tr);
///////////////////////////////////////////////////////////////////////////////

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtL"); td.setAttribute("className","nomrTxtL");
        td.style.borderTop = "0px solid black";
        txt = document.createTextNode("Retrieve by E-mail");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtNobold"); td.setAttribute("className","normTxtNobold");
        td.style.borderBottom = "1px solid black";
        txt = document.createTextNode("Enter the e-mail address that you have on file with TheraBill and we will send your login information to that e-mail.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("width","70px");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        txt = document.createTextNode("E-mail:");
        td.appendChild(txt);
        tr.appendChild(td);

        td = document.createElement("td");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        try { var txtbox = document.createElement("<input type='text' id='FP_EMAIL'>"); }
        catch(e) {
                var txtbox = document.createElement("input");
                txtbox.setAttribute("type","text");
                txtbox.setAttribute("id","FP_EMAIL");
        }
        txtbox.style.width = "200px";
        td.appendChild(txtbox);
        try { var btn = document.createElement("<input type='button' value='Go'>"); }
        catch(e) {
                var btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","Go");
        }
        btn.style.cursor = "pointer";
        btn.onclick = function() { retreivePassByEmail(); }
        td.appendChild(btn);
        tr.appendChild(td);
        tbod.appendChild(tr);

///////////////////////////////////////////////////////////////////////////////
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        txt = document.createTextNode("\u00a0");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);
///////////////////////////////////////////////////////////////////////////////
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtL"); td.setAttribute("className","nomrTxtL");
        td.style.borderTop = "0px solid black";
        txt = document.createTextNode("Retrieve by NPI");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtNobold"); td.setAttribute("className","normTxtNobold");
        td.style.borderBottom = "1px solid black";
        txt = document.createTextNode("Enter your NPI and TheraBill will e-mail the userID and password to the e-mail address that you have on file.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("width","70px");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        txt = document.createTextNode("NPI:");
        td.appendChild(txt);
        tr.appendChild(td);

        td = document.createElement("td");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        try { var txtbox = document.createElement("<input type='text' id='FP_NPI'>"); }
        catch(e) {
                var txtbox = document.createElement("input");
                txtbox.setAttribute("type","text");
                txtbox.setAttribute("id","FP_NPI");
        }
	txtbox.onkeyup = function() { removeNonNumeric(this); }
        txtbox.style.width = "200px";
        td.appendChild(txtbox);
        try { var btn = document.createElement("<input type='button' value='Go'>"); }
        catch(e) {
                var btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","Go");
        }
        btn.style.cursor = "pointer";
        btn.onclick = function() { retreivePassByNPI(); }
        td.appendChild(btn);
        tr.appendChild(td);
        tbod.appendChild(tr);


///////////////////////////////////////////////////////////////////////////////
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        txt = document.createTextNode("\u00a0");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);
///////////////////////////////////////////////////////////////////////////////

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtL"); td.setAttribute("className","nomrTxtL");
        td.style.borderTop = "0px solid black";
        txt = document.createTextNode("Retrieve by Tax ID");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        td.setAttribute("class","normTxtNobold"); td.setAttribute("className","normTxtNobold");
        td.style.borderBottom = "1px solid black";
        txt = document.createTextNode("Enter your tax ID and TheraBill will send your login information to the email that you have on file.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("width","70px");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        txt = document.createTextNode("Tax ID:");
        td.appendChild(txt);
        tr.appendChild(td);

        td = document.createElement("td");
        td.setAttribute("class","normTxt"); td.setAttribute("className","normTxt");
        try { var txtbox = document.createElement("<input type='text' id='FP_TAXID'>"); }
        catch(e) {
                var txtbox = document.createElement("input");
                txtbox.setAttribute("type","text");
                txtbox.setAttribute("id","FP_TAXID");
        }
        txtbox.onkeyup = function() { removeNonNumeric(this); }
        txtbox.style.width = "200px";
        td.appendChild(txtbox);
        try { var btn = document.createElement("<input type='button' value='Go'>"); }
        catch(e) {
                var btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","Go");
        }
        btn.style.cursor = "pointer";
        btn.onclick = function() { retreivePassByTaxID(); }
        td.appendChild(btn);
        tr.appendChild(td);
        tbod.appendChild(tr);

///////////////////////////////////////////////////////////////////////////////
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("colspan","2"); td.setAttribute("colSpan","2");
        txt = document.createTextNode("\u00a0");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);
///////////////////////////////////////////////////////////////////////////////


	tbl.appendChild(tbod);
	div.appendChild(tbl);
	document.body.appendChild(div);
        div.style.left = (px - div.offsetWidth + placer.offsetWidth) + "px";
        div.style.top = (py) + "px";
	FADEINDIV = 'LOGINHELP_CONTAINER';
	fadeInDiv(0); 
}
function removeNonNumeric(obj) {
        var caretPos = doGetCaretPosition(obj);
        var val = trim(obj.value);
        var newval = "";
        for(var j=0; j<val.length; j++) {
                var alphaa = val.charAt(j);
                var hh = alphaa.charCodeAt(0);
                if(hh > 47 && hh<58) { newval += alphaa; }
                else { caretPos--; }
        }
        obj.value = newval;
        doSetCaretPosition(obj,caretPos);
}
   function doGetCaretPosition (oField) {

     // Initialize
     var iCaretPos = 0;

     // IE Support
     if (document.selection) {

       // Set focus on the element
       oField.focus ();
 
       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange ();
 
       // Move selection start to 0 position
       oSel.moveStart ('character', -oField.value.length);
 
       // The caret position is selection length
       iCaretPos = oSel.text.length;
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0')
       iCaretPos = oField.selectionStart;

     // Return results
     return (iCaretPos);
   }
   function doSetCaretPosition (oField, iCaretPos) {
     // IE Support
     if (document.selection) {

       // Set focus on the element
       oField.focus ();
 
       // Create empty selection range
       var oSel = document.selection.createRange ();
 
       // Move selection start and end to 0 position
       oSel.moveStart ('character', -oField.value.length);
 
       // Move selection start and end to desired position
       oSel.moveStart ('character', iCaretPos);
       oSel.moveEnd ('character', 0);
       oSel.select ();
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0') {
       oField.selectionStart = iCaretPos;
       oField.selectionEnd = iCaretPos;
       oField.focus ();
     }
   }

function retreivePassByTaxID() {
                var tid = trim(document.getElementById('FP_TAXID').value);
                if(tid == '') { alert("You did not enter a tax ID"); return 1; }
                var xmlHttp;
                try { xmlHttp = new XMLHttpRequest(); }
                catch (e) {
                        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                        catch(e) {
                                try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                                catch(e) {
                                        alert("You must enable javascript");
                                        return false;
                                }
                        }
                }
                var url = "/login/retreivePasswordByTaxID.php?taxid=" + tid;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                finishRetrievePassword(MRES);

}
function retreivePassByNPI() {
                var npi = trim(document.getElementById('FP_NPI').value);
		if(npi == '') { alert("You did not enter a NPI"); return 1; }
                var xmlHttp;
                try { xmlHttp = new XMLHttpRequest(); }
                catch (e) {
                        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                        catch(e) {
                                try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                                catch(e) {
                                        alert("You must enable javascript");
                                        return false;
                                }
                        }
                }
                var url = "/login/retreivePasswordByNPI.php?npi=" + npi;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                finishRetrievePassword(MRES);
}
function retreivePassByUserID() {
		var uid = trim(document.getElementById('FP_USERID').value);
                var xmlHttp;
                try { xmlHttp = new XMLHttpRequest(); }
                catch (e) {
                        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                        catch(e) {
                                try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                                catch(e) {
                                        alert("You must enable javascript");
                                        return false;
                                }
                        }
                }
                var url = "/login/retreivePasswordByUserID.php?userID=" + uid;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
		finishRetrievePassword(MRES);
}
function retreivePassByEmail() {
                var email = trim(document.getElementById('FP_EMAIL').value);
                var xmlHttp;
                try { xmlHttp = new XMLHttpRequest(); }
                catch (e) {
                        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                        catch(e) {
                                try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                                catch(e) {
                                        alert("You must enable javascript");
                                        return false;
                                }
                        }
                }
                var url = "/login/retreivePasswordByEmail.php?email=" + email;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                finishRetrievePassword(MRES);
}
function removeFinishPassword() {
	document.body.removeChild(document.getElementById('FINISH'));
}
function finishRetrievePassword(AGS) {
	showLoginHelp();
	var R = trim(AGS);
	var div = document.createElement("div");
	div.setAttribute('id','FINISH');
	div.style.border = "5px solid red";
	div.style.position = "absolute";
	div.style.background = "white";
	div.style.padding = "20px";
	div.style.visibility = "visible";
	var t;
	if(R == 0) { t = "Could not find your log in information based on the information you provided"; }
	if(R == "MAIL SENT") { t = "An e-mail has been sent to the e-mail address that you have on file with TheraBill"; }
	if(R == "MAIL ERROR") { t = "Contact support@therabill.com to retreive your login information"; }
	
	var tbl = document.createElement("table");
	var tbod = document.createElement("tbody");
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.setAttribute('class','normTxtL');
	td.setAttribute('className','normTxtL');
	td.setAttribute('align','center');
	td.setAttribute('width','300px');
	var txt = document.createTextNode(t);
	td.appendChild(txt);
	tr.appendChild(td);
	tbod.appendChild(tr);	

	tr = document.createElement('tr');
	td = document.createElement('td');
	td.setAttribute('align','center');
	try { var btn = document.createElement("<input type='button' value='OK'>"); }
	catch(e) {
		var btn = document.createElement("input");
		btn.setAttribute('type','button');
		btn.setAttribute('value','OK');
	}
	btn.onclick = function() { removeFinishPassword(); }
	td.appendChild(btn);
	tr.appendChild(td);
	tbod.appendChild(tr);
	tbl.appendChild(tbod);
	div.appendChild(tbl);

        var placer = document.getElementById('LOGINHELP_CONTAINER');
        var px = findPosX(placer);
        var py = findPosY(placer);
	document.body.appendChild(div);	
	div.style.left = (px - div.offsetWidth/2 + placer.offsetWidth/2) + "px";
	div.style.top = (py - placer.offsetHeight/2 - div.offsetHeight/2) + "px";
	
	


}
function demoLogIn() {
	document.getElementById('LIN_USERID').value = 'demo';
	document.getElementById('LIN_USERPASS').value = 'demo';
	verifyLogin();
}
function verifyLogin() {
	var uid = document.getElementById('LIN_USERID').value;
	var ups = u_prep(document.getElementById('LIN_USERPASS').value);
	var rememberMe = 0;
	if(document.getElementById('REMEMBERME').checked) { rememberMe = 1; }
	
        if(uid.length == 0 || ups.length == 0) { alert("Please enter a login name and password"); return 1; }

                var xmlHttp;
                try { xmlHttp = new XMLHttpRequest(); }
                catch (e) {
                        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                        catch(e) {
                                try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                                catch(e) {
                                        alert("You must enable javascript");
                                        return false;
                                }
                        }
                }
                var url = "/login/verifyLogin.php?uid=" + uid + "&upass=" + ups + "&width=" + screenWidth + "&height=" + screenHeight + "&browser=" + browser + "&b_version=" + b_version + "&setCookie=" + rememberMe;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                parseLoginResults(MRES,uid,ups);


}
function parseLoginResults(MRES,uid,ups) {
	MRES = trim(MRES);
        if(MRES != '0') {
		var VLS = MRES.split(":");
		if(VLS[0] == "PAYMENT_REQUIRED") {
			window.location = "https://www.therabill.com/makePayment.php?userID=" + VLS[1];
		}
		else if(VLS[0] == "ACCOUNT_LOCKED") {
			alert("This account is not active at this time.");
		}
		else if(VLS[3] == '1') { // MAIN MEMBER LOGIN
                	document.getElementById('LOGIN_USERID').value = VLS[1];
                	document.getElementById('LOGIN_SUBID').value = VLS[2];
			document.getElementById('LOGIN_SESSID').value = VLS[0];
                	document.getElementById('LOGIN_SUBMIT').submit();
		}
		else if(VLS[3] == '2') { // PROVIDER LOGIN
                        document.getElementById('PROV_LOGIN_USERID').value = VLS[1];
                        document.getElementById('PROV_LOGIN_SUBID').value = VLS[2];
                        document.getElementById('PROV_LOGIN_SESSID').value = VLS[0];
                        document.getElementById('PROV_LOGIN_SUBMIT').submit();

		}
		else if(VLS[3] == '3') { // CLIENT LOGIN
                        document.getElementById('CLIENT_LOGIN_USERID').value = VLS[1];
                        document.getElementById('CLIENT_LOGIN_SUBID').value = VLS[2];
                        document.getElementById('CLIENT_LOGIN_SESSID').value = VLS[0];
                        document.getElementById('CLIENT_LOGIN_SUBMIT').submit();
		}
		else if(VLS[0] == "CONFIRMEMAIL") {
			CREATE_EMAIL = VLS[3];
			CREATE_ACCOUNT_ID = VLS[1];
			EMAIL_CONF_CODE = VLS[2];
			gotoConfirmEmail();
			
		}
		else if(VLS[0] == "AGREETERMS") {
			CREATE_ACCOUNT_ID = VLS[1];
			gotoTerms();
		}
		
        }
        else {
		if(LOGIN_HELP_VISIBLE == 0) { showLoginHelp(1); }
                showBadLogIn(uid,ups);
        }
}

function showBadLogIn(uid,ups) {
        alert("Incorrect userID/Password");
}

