var CREATING_ACCOUNT = 0;
var FORM_ERRORS = "";
var ID_ERRORS = new Array();
var CREATE_EMAIL = "";
var EMAIL_CONF_CODE = "";
var CREATE_ACCOUNT_ID = "";
var VIEWED_TERMS = 0;
var FINAL_CREATING = 0;
var CURRENT_FOCUS = "";
var errSep = "\r\n";

function lookupNPI() {
        var npi = trim(document.getElementById('NEW_NPI').value);
        if(npi.length != 10) { 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;
                        }
                }
        }
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState == 4) {
                        RESULTS = xmlHttp.responseText;
                        loadNPIResults(RESULTS);
                }
        }
        var url = "./ajax/lookUPNPI.php?npi=" + npi;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);

}

function loadNPIResults(RESULTS) {
        if(trim(RESULTS) == '0') { return 1; }
        var VALS = RESULTS.split("<++||++>");
        document.getElementById('NEW_BUSINESSNAME').value = VALS[1];
        document.getElementById('NEW_FNAME').value = VALS[2];
        document.getElementById('NEW_LNAME').value = VALS[3];
        document.getElementById('NEW_PHONE').value = VALS[4];
        document.getElementById('NEW_ADDRESS').value = VALS[5];
        document.getElementById('NEW_ADDRESS2').value = VALS[6];
        document.getElementById('NEW_CITY').value = VALS[7];
        document.getElementById('NEW_ZIP').value = VALS[9];

        var st = document.getElementById('NEW_STATE');
        for(var i=0; i<st.options.length; i++) {
                if(st.options[i].value == VALS[8]) { st.options.selectedIndex = i; }
        }

}

function page_initiate() {

        var mn = document.getElementById('TRY');
        mn.setAttribute("class","active");
        mn.setAttribute("className","active");


        resetAccountCreateButton();
        resetAccountCreateForm();
        var email = document.getElementById('NEW_EMAIL');
        var fname = document.getElementById('NEW_FNAME');
        var lname = document.getElementById('NEW_LNAME');
        var orgName = document.getElementById('NEW_BUSINESSNAME');
        var taxID = document.getElementById('NEW_TAXID');
        var tx1 = document.getElementById('TX1');
        var tx2 = document.getElementById('TX2');
        var address = document.getElementById('NEW_ADDRESS');
        var address2 = document.getElementById('NEW_ADDRESS2');
        var city = document.getElementById('NEW_CITY');
        var state = document.getElementById('NEW_STATE');
        var zip = document.getElementById('NEW_ZIP');
        var phone = document.getElementById('NEW_PHONE');
        var pss1 = document.getElementById('NEW_PASSWORD');
        var pss2 = document.getElementById('NEW_PASSWORD2');
        var uid = document.getElementById('NEW_USERID');
        var refer = document.getElementById("NEW_REFERRER");
        var npi = document.getElementById('NEW_NPI');
/*
        refer.value = "Cynthia Dundas";
        npi.value = "1234567890";
        email.value = "joe@dundashome.com";
        fname.value = "Joe";
        lname.value = "Dundas";
        orgName.value = "My business name";
        taxID.value = "353745868";
        address.value = "1198 Blackburn Drive";
        address2.value = "Suite 200";
        city.value = "Grayslake";
        zip.value = "60030";
        phone.value = "8475488126";
        pss1.value = "7383jmd2";
        pss2.value = "7383jmd2";
        uid.value = "joe";
        tx1.checked = "true";
        for(var i=0; i<state.options.length; i++) {
                if(state.options[i].value == "IL") { state.selectedIndex = i; }
        }
*/

}

function beginCreateAccount() {
        resetAccountCreateForm();
        if(CREATING_ACCOUNT == 1) { return 1; }
        var but = document.getElementById('CREATEACCOUNTBUTTON');
        but.value = "Please wait...";
        CREATING_ACCOUNT = 1;

        userIDcheck();

}

function userIDcheck() {
        var uid = trim(document.getElementById('NEW_USERID').value);
        if(uid.length >= 3) {
                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 = "ajax/checkUIDexists.php?uid=" + uid;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                newAccountErrorCheck(MRES);
        }
        else { formChecker(); }

}
function newAccountErrorCheck(MRES) {
        var RES = trim(MRES);
        if(RES == '0') {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "That login name is already in use.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_USERID";
        }
        formChecker();
}
function formChecker() {
        var email = document.getElementById('NEW_EMAIL');
        var fname = document.getElementById('NEW_FNAME');
        var lname = document.getElementById('NEW_LNAME');
        var orgName = document.getElementById('NEW_BUSINESSNAME');
        var taxID = document.getElementById('NEW_TAXID');
        var tx1 = document.getElementById('TX1');
        var tx2 = document.getElementById('TX2');
        var address = document.getElementById('NEW_ADDRESS');
        var address2 = document.getElementById('NEW_ADDRESS2');
        var city = document.getElementById('NEW_CITY');
        var state = document.getElementById('NEW_STATE');
        var zip = document.getElementById('NEW_ZIP');
        var phone = document.getElementById('NEW_PHONE');
        var pss1 = document.getElementById('NEW_PASSWORD');
        var pss2 = document.getElementById('NEW_PASSWORD2');
        var uid = document.getElementById('NEW_USERID');

	var how_hear = document.getElementById('NEW_HOWHEAR');


        email.value = trim(email.value);
        fname.value = trim(fname.value);
        lname.value = trim(lname.value);
        orgName.value = trim(orgName.value);
        taxID.value = trim(taxID.value);
        address.value = trim(address.value);
        address2.value = trim(address2.value);
        city.value = trim(city.value);
        zip.value = trim(zip.value);
        phone.value = trim(phone.value);
        pss1.value = trim(pss1.value);
        pss2.value = trim(pss2.value);
	if(how_hear.selectedIndex == 0) {
        	if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please let us know how you heard about Therabill";
                ID_ERRORS[ID_ERRORS.length] = "NEW_HOWHEAR";
	}
        if(taxID.value.length > 0) {
                if(! (tx1.checked || tx2.checked) ) {
                        if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                        FORM_ERRORS += "Please choose the tax ID type that you entered";
                        ID_ERRORS[ID_ERRORS.length] = "NEW_TAXID";
                }
        }
        if(!validEmail(email.value)) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter a valid e-mail address.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_EMAIL";
        }
        if(fname.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter your first name.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_FNAME";
        }
        if(lname.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter your last name.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_LNAME";
        }
        if(address.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter your billing address.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_ADDRESS";
        }
        if(city.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter your billing city.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_CITY";
        }
        if(state.options[state.selectedIndex].value == '99') {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please select your billing state.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_STATE";
        }
        if(zip.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter your billing zip code.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_ZIP";
        }
	else {
		if(zip.value.length != 9) {
			
                	if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                	FORM_ERRORS += "ZIP code must be 9 digits. Click the search ZIP+4 link to find your 9 digit ZIP code.";
                	ID_ERRORS[ID_ERRORS.length] = "NEW_ZIP";
		}
	}
        if(uid.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter a desired login name.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_USERID";
        }
        else if(uid.value.length < 3) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Login name must be 3 or more characters.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_USERID";
        }

        if(pss1.value.length == 0) {
                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                FORM_ERRORS += "Please enter a secure password.";
                ID_ERRORS[ID_ERRORS.length] = "NEW_PASSWORD";
        }
        else {
                if(pss1.value.length < 5) {
                        if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                        FORM_ERRORS += "Your password should be at least 5 characters in length.";
                        ID_ERRORS[ID_ERRORS.length] = "NEW_PASSWORD";
                }
                else {
                        if(pss2.value.length == 0) {
                                if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                                FORM_ERRORS += "Please verify your password.";
                                ID_ERRORS[ID_ERRORS.length] = "NEW_PASSWORD2";
                        }
                        else {
                                if(pss2.value != pss1.value) {
                                        if(FORM_ERRORS.length > 0) { FORM_ERRORS += errSep; }
                                        FORM_ERRORS += "Your passwords do not match.";
                                        ID_ERRORS[ID_ERRORS.length] = "NEW_PASSWORD";
                                        ID_ERRORS[ID_ERRORS.length] = "NEW_PASSWORD2";
                                }
                        }

                }
        }


        if(FORM_ERRORS.length > 0) { showFormErrors(); return 1; }
        createTempAccount();
}


function createTempAccount() {
        var npi = document.getElementById('NEW_NPI');
        var email = document.getElementById('NEW_EMAIL');
        var fname = document.getElementById('NEW_FNAME');
        var lname = document.getElementById('NEW_LNAME');
        var orgName = document.getElementById('NEW_BUSINESSNAME');
        var taxID = document.getElementById('NEW_TAXID');
        var tx1 = document.getElementById('TX1');
        var tx2 = document.getElementById('TX2');
        var address = document.getElementById('NEW_ADDRESS');
        var address2 = document.getElementById('NEW_ADDRESS2');
        var city = document.getElementById('NEW_CITY');
        var state = document.getElementById('NEW_STATE');
        var zip = document.getElementById('NEW_ZIP');
        var phone = document.getElementById('NEW_PHONE');
        var pss1 = document.getElementById('NEW_PASSWORD');
        var pss2 = document.getElementById('NEW_PASSWORD2');
        var uid = document.getElementById('NEW_USERID');
        var refer = document.getElementById('NEW_REFERRER');


	var how_hear = document.getElementById('NEW_HOWHEAR');
	how_hear = how_hear.options[how_hear.selectedIndex].value;

        var taxIDtype = "";
        if(tx1.checked) { taxIDtype = "SSN"; }
        else if(tx2.checked) { taxIDtype = "EIN"; }

        refer.value = u_prep(trim(refer.value));
        npi.value = u_prep(trim(npi.value));
        email.value = u_prep(trim(email.value));
        fname.value = u_prep(trim(fname.value));
        lname.value = u_prep(trim(lname.value));
        orgName.value = u_prep(trim(orgName.value));
        taxID.value = u_prep(trim(taxID.value));
        address.value = u_prep(trim(address.value));
        address2.value = u_prep(trim(address2.value));
        city.value = u_prep(trim(city.value));
        zip.value = u_prep(trim(zip.value));
        phone.value = u_prep(trim(phone.value));
        pss1.value = u_prep(trim(pss1.value));
        pss2.value = u_prep(trim(pss2.value));
        uid.value = u_prep(trim(uid.value));

        var eiprovider = 0;
        if(document.getElementById('EIPROV_Y').checked) { eiprovider = 1; }


        CREATE_EMAIL = 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 = "ajax/createTemporaryAccount.php?uid=" + uid.value + "&refer=" + refer.value + "&npi=" + npi.value + "&email=" +email.value + "&fname=" + fname.value + "&lname=" + lname.value + "&orgName=" + orgName.value + "&taxID=" + taxID.value + "&taxIDtype=" + taxIDtype + "&address=" + address.value + "&address2=" + address2.value + "&city=" + city.value + "&state=" + state.options[state.selectedIndex].value + "&zip=" + zip.value + "&phone=" + phone.value + "&pass=" + pss1.value + "&eiprovider=" + eiprovider + "&howhear=" + how_hear;
        xmlHttp.open("GET",url,false);
        xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
        xmlHttp.send(null);
        MRES = xmlHttp.responseText;
        setConfCodeLocally(MRES);


}

function setConfCodeLocally(MRES) {
        var RES  = trim(MRES).split("|");
        if(RES[0] == "SUCCESSFUL") {
                MAIL_SENT = RES[3];
                EMAIL_CONF_CODE = RES[2];
                CREATE_ACCOUNT_ID = RES[1];
                if(CREATE_ACCOUNT_ID == '0') {
                        alert("A FATAL ERROR HAS OCCURRED.  SUPPORT HAS BEEN NOTIFIED OF THIS ERROR.  SOMEONE FROM SUPPORT WILL BECONTACTING YOU WITHIN THE NEXT 24 HOURS AT THE E-MAIL ADDRESS: " + CREATE_EMAIL + ".  FEEL FREE TO CONTACT OUR SUPPORT STAFF AT SUPPORT@THERABILL.COM");
                        sendErrorReport(CREATE_EMAIL,"ajax/createTemporaryAccount.php" + MRES);
                }
                else if(MAIL_SENT == '0') {
                        gotoTerms();
                }
                else {
                        gotoConfirmEmail();
                }
        }
        else {
                alert("A FATAL ERROR HAS OCCURRED.  SUPPORT HAS BEEN NOTIFIED OF THIS ERROR.  SOMEONE FROM SUPPORT WILL BE CONTACTING YOU WITHIN THE NEXT 24 HOURS AT THE E-MAIL ADDRESS: " + CREATE_EMAIL + ".  FEEL FREE TO CONTACT OUR SUPPORT STAFF AT SUPPORT@THERABILL.COM");
                sendErrorReport(CREATE_EMAIL,MRES);

        }      
}

function sendErrorReport(email,errMsg) {
        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;
                        }
                }
        }
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState == 4) {
                        RESULTS = xmlHttp.responseText;
                        loadMessageSendResults(RESULTS);
                }
        }
        var url = "./ajax/sendErrorMessageToSupport.php?email=" + email + "&message=" + errMsg;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);

}

function loadMessageSendResults(RES) {
        resetAccountCreateButton();
}

function gotoConfirmEmail() {
	self.scrollTo(0,0);
        var bg = document.createElement("div");
        bg.setAttribute("class","opaqBG");
        bg.setAttribute("className","opaqBG");
        bg.setAttribute("id","OPQBG");
        bg.style.visibility = "visible";
        bg.style.width = "550px";
        bg.style.height = "400px";
        bg.style.left = "50%";
        bg.style.top = "50%";
        bg.style.backgroundColor = "#003a68";
        bg.style.marginLeft = "-275px";
        bg.style.marginTop = "-200px";

        document.body.appendChild(bg);


        var box = document.createElement("div");
        box.setAttribute("id","CONFEMDIV");
        box.style.width = "500px";
        box.style.height = "350px";
        box.style.position = "absolute";
        box.style.backgroundColor = "white";
        box.style.top = "50%";
        box.style.left = "50%";
        box.style.marginLeft = "-250px";
        box.style.marginTop = "-175px";
        box.style.zIndex = "901";

        var otb = document.createElement("table");
        var otbod = document.createElement("tbody");
        otb.setAttribute('width','100%');
        otb.setAttribute('height','100%');
        var otr = document.createElement('tr');
        var otd = document.createElement('td');
        otd.setAttribute('width','100%');
        otd.setAttribute('height','100%');
        otd.setAttribute('align','center');
        otd.setAttribute('valign','middle');

        var tb = document.createElement('table');
        var tbod = document.createElement('tbody');
        var tr,td,txt,txtbox,btn,hr,span;

        tb.setAttribute("align","center");
        tb.setAttribute("valign","middle");
        tb.setAttribute("border","0");
        tb.style.verticalAlign = "middle";
        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        txt = document.createTextNode("Confirm your e-mail address");
        td.appendChild(txt);
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","normTxtNobold");
        td.setAttribute("className","normTxtNobold");
        try { span = document.createElement("<span>"); }
        catch(e) { span = document.createElement("span"); }
        span.setAttribute("class","normTxt");
        span.setAttribute("className","normTxt");
        txt = document.createTextNode("Why do we need to confirm your e-mail? ");
        span.appendChild(txt);
        td.appendChild(span);
        txt = document.createTextNode("TheraBill uses your e-mail address to notify you of various system alerts and error messages as well as for support related issues.  Your e-mail is not used for any marketing purposes. By confirming your e-mail address we can gaurantee that you will indeed receive these vital messages.");
        td.appendChild(txt);
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement('tr');
        td = document.createElement('td');
        txt = document.createTextNode("\u00a0");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","normTxt");
        td.setAttribute("className","normTxt");
        td.setAttribute("align","justify");
        txt = document.createTextNode("An e-mail has been sent to " + CREATE_EMAIL + " with a confirmation code in it.  Please confirm that you received this e-mail and enter the confirmation code below.  If the e-mail ended up in your spam folder, please make sure to set your spam filter settings to allow e-mails from therabill.com.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontM");
        td.setAttribute("className","loginFontM");
        td.setAttribute("align","center");
        txt = document.createTextNode("Confirmation Code: ");
        td.appendChild(txt);

        try {
                txtbox = document.createElement("<input type='text' id='ENTER_CONF_CODE_BOX' size=10>");
        }
        catch(e) {
                txtbox = document.createElement("input");
                txtbox.setAttribute("type","text");
                txtbox.setAttribute("id","ENTER_CONF_CODE_BOX");
                txtbox.setAttribute("size","10");
        }
        td.appendChild(txtbox);
        try {
                btn = document.createElement("<input type='button' value='Confirm'>");
        }
        catch(e) {
                btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","confirm");
        }
        btn.onclick = function() { confirmConfirmationCode(); }

        td.appendChild(btn);

        tr.appendChild(td);
        tbod.appendChild(tr);
        tr = document.createElement('tr');
        td = document.createElement('td');
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontM");
        td.setAttribute("className","loginFontM");
        td.setAttribute("align","center");
        try { span = document.createElement("<span>"); }
        catch(e) { span = document.createElement("span"); }
        span.style.textDecoration = "underline";
        span.style.cursor = "pointer";
        span.onclick = function() { resendConfirmationCode(); }
        txt = document.createTextNode("Resend Code");
        span.appendChild(txt);
        td.appendChild(span);

        txt = document.createTextNode("\u00a0\u00a0\u2022\u00a0\u00a0");
        td.appendChild(txt);

        try { span = document.createElement("<span>"); }
        catch(e) { span = document.createElement("span"); }
        span.style.textDecoration = "underline";
        span.style.cursor = "pointer";
        span.onclick = function() { backToForm(); }
        txt = document.createTextNode("Edit e-mail");
        span.appendChild(txt);
        td.appendChild(span);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("align","center");

        var mdiv = document.createElement("div");
        mdiv.style.height = "30px";
        mdiv.style.width = "100%";


        var itb = document.createElement("table");
        var itbod = document.createElement("tbody");
        itbod.setAttribute("id","CONF_MESSAGE_TABLE");
        itb.appendChild(itbod);
        mdiv.appendChild(itb);
        td.appendChild(mdiv);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tb.appendChild(tbod);
        otd.appendChild(tb);
        otr.appendChild(otd);
        otbod.appendChild(otr);
        otb.appendChild(otbod);

        box.appendChild(otb);
        document.body.appendChild(box);


}
function resendConfirmationCode() {
        var tmpID = CREATE_ACCOUNT_ID;

        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;
                        }
                }
        }
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState == 4) {
                        RESULTS = xmlHttp.responseText;
                        loadResendResults(RESULTS);
                }
        }
        var url = "./ajax/resendConfirmationCode.php?tmpID=" + tmpID;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);

}

function loadResendResults(RESULTS) {
        var R = trim(RESULTS);
        if(R == '1') {
                giveConfCodeMessage("E-mail has been resent");
        }
        else {
                giveConfCodeMessage("There was a problem sending e-mail");
        }
}

function backToForm() {
        var bg = document.getElementById('OPQBG');
        var box = document.getElementById('CONFEMDIV');
        document.body.removeChild(bg);
        document.body.removeChild(box);
        resetAccountCreateButton();
}
function confirmConfirmationCode() {
        var codeBox = document.getElementById('ENTER_CONF_CODE_BOX');
        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;
                        }
                }
        }
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState == 4) {
                        AGS = xmlHttp.responseText;
                }
        }
        var url = "ajax/markEmailConfirmed.php?tempID=" + CREATE_ACCOUNT_ID;
      xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
//HACK
        if(trim(codeBox.value) == EMAIL_CONF_CODE) { gotoTerms(); }
        else {
                codeBox.style.backgroundColor = "red";
                giveConfCodeMessage("Incorrect onfirmation code");
        }
}
function gotoTerms() {
        var bg = document.getElementById('OPQBG');
        var box = document.getElementById('CONFEMDIV');

        if(bg) { document.body.removeChild(bg); }
        if(box) { document.body.removeChild(box); }

        var bg = document.createElement("div");
        bg.setAttribute("class","opaqBG");
        bg.setAttribute("className","opaqBG");
        bg.setAttribute("id","OPQBG");
        bg.style.visibility = "visible";
        bg.style.width = "550px";
        bg.style.height = "300px";
        bg.style.left = "50%";
        bg.style.top = "50%";
        bg.style.backgroundColor = "#003a68";
        bg.style.marginLeft = "-275px";
        bg.style.marginTop = "-150px";

        document.body.appendChild(bg);


        var box = document.createElement("div");
        box.setAttribute("id","CONFEMDIV");
        box.style.width = "500px";
        box.style.height = "250px";
        box.style.position = "absolute";
        box.style.visiblity = "visible";
        box.style.backgroundColor = "white";
        box.style.top = "50%";
        box.style.left = "50%";
        box.style.marginLeft = "-250px";
        box.style.marginTop = "-125px";
        box.style.zIndex = "901";

        var otb = document.createElement("table");
        var otbod = document.createElement("tbody");
        otb.setAttribute('width','100%');
        otb.setAttribute('height','100%');
        var otr = document.createElement('tr');
        var otd = document.createElement('td');
        otd.setAttribute('width','100%');
        otd.setAttribute('height','100%');
        otd.setAttribute('align','center');
        otd.setAttribute('valign','middle');

        var tb = document.createElement('table');
        var tbod = document.createElement('tbody');
        var tr,td,txt,txtbox,btn,hr,span;

        tb.setAttribute("align","center");
        tb.setAttribute("valign","middle");
        tb.setAttribute("border","0");
        tb.style.verticalAlign = "middle";
        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        td.setAttribute("colspan","2");
        txt = document.createTextNode("Terms & Conditions of Use");
        td.appendChild(txt);
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("width","100px");

	try {
		var href = document.createElement("<a href='./therabill_agreement.pdf' target='_blank'>");
	}
	catch(e) {
		var href = document.createElement("a");
		href.setAttribute("href","./therabill_agreement.pdf");
		href.setAttribute("target","_blank");
	}
	href.onclick = function() { openTerms(); }

        try {
                var img = document.createElement("<img border=0 src='../images/documents/PDFicon.jpg' width='80'>");
        }
        catch(e) {
                var img = document.createElement("img");
                img.setAttribute("src","../images/documents/PDFicon.jpg");
                img.setAttribute("width","80");
		img.setAttribute("border","0");
        }
        img.style.cursor = "pointer";
	href.appendChild(img);
        td.appendChild(href);
        tr.appendChild(td);

        td = document.createElement("td");
        td.setAttribute("class","normTxt");
        td.setAttribute("className","normTxt");
        td.setAttribute("align","justify");
        txt = document.createTextNode("Click the PDF icon to view the terms & conditions of use as well as the TheraBill privacy statement (a.k.a HIPAA statement).  You must click the PDF icon before you can continue.  If you agree to the terms and conditions then check this box ");
        td.appendChild(txt);

        try {
                var chkbox = document.createElement("<input type='checkbox' id='AGREECHKBOX'>");
        }
        catch(e) {
                var chkbox = document.createElement("input");
                chkbox.setAttribute("type","checkbox");
                chkbox.setAttribute("id","AGREECHKBOX");
        }
        td.appendChild(chkbox);

        txt = document.createTextNode(". You must agree to the terms before your account can be created.");
        td.appendChild(txt);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        td.setAttribute("align","right");
        td.setAttribute("colspan","2");
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);

        try {
                btn = document.createElement("<input type='button' value='Create my account' id='AGREEBUTTON'>");
        }
        catch(e) {
                btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","Create my account");
                btn.setAttribute("id","AGREEBUTTON");
        }
        btn.style.cursor = "pointer";
        btn.onclick = function() { finalizeAccountCreation('1'); }
        td.appendChild(btn);

        tr.appendChild(td);
        tbod.appendChild(tr);


        tbod.appendChild(tr);
        tb.appendChild(tbod);
        otd.appendChild(tb);
        otr.appendChild(otd);
        otbod.appendChild(otr);
        otb.appendChild(otbod);
        box.appendChild(otb);
        document.body.appendChild(box);
}

function openTerms() {
	VIEWED_TERMS = 1;
}
function finalizeAccountCreation(termsCheck) {
        var tmpID = CREATE_ACCOUNT_ID;
        if(termsCheck == '1') {
                var errMsg = "";
                if(VIEWED_TERMS == 0) {
                        if(errMsg.length > 0) { errMsg += "\r\n"; }
                        errMsg += "You must click the PDF icon to view the terms and conditions";
                }
                if(! document.getElementById('AGREECHKBOX').checked ) {
                        if(errMsg.length > 0) { errMsg += "\r\n"; }
                        errMsg += "You must agree to the terms and conditions by putting a check mark in the check box";
                }

                if(errMsg.length > 0) {
                        alert(errMsg); return 1;
                }


                if(FINAL_CREATING == 1) { return 1; }
                FINAL_CREATING = 1;
                document.getElementById('AGREEBUTTON').value = "Please wait... creating account";
        }



        var SxmlHttp;
        try { SxmlHttp = new XMLHttpRequest(); }
        catch (e) {
                try { SxmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                catch(e) {
                        try { SxmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                        catch(e) {
                                alert("You must enable javascript");
                                return false;
                        }
                }
        }
        SxmlHttp.onreadystatechange=function() {
                if(SxmlHttp.readyState == 4) {
                        AGS = SxmlHttp.responseText;
			tieItUp(AGS);
                }
        }
        var url = "ajax/finalizeAccountCreation.php?tmpID=" + tmpID;
        SxmlHttp.open("GET",url,true);
//        SxmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
        SxmlHttp.send(null);
//        MRES = SxmlHttp.responseText;
//        tieItUp(MRES);



}

function chooseDifferentUserID() {
        var bg = document.getElementById('OPQBG');
        var box = document.getElementById('CONFEMDIV');

        if(bg) { document.body.removeChild(bg); }
        if(box) { document.body.removeChild(box); }

        var bg = document.createElement("div");
        bg.setAttribute("class","opaqBG");
        bg.setAttribute("className","opaqBG");
        bg.setAttribute("id","OPQBG");
        bg.style.visibility = "visible";
        bg.style.width = "600px";
        bg.style.height = "300px";
        bg.style.left = "50%";
        bg.style.top = "50%";
        bg.style.backgroundColor = "#003a68";
        bg.style.marginLeft = "-300px";
        bg.style.marginTop = "-150px";

        document.body.appendChild(bg);


        var box = document.createElement("div");
        box.setAttribute("id","CONFEMDIV");
        box.style.width = "550px";
        box.style.height = "250px";
        box.style.position = "absolute";
        box.style.visiblity = "visible";
        box.style.backgroundColor = "white";
        box.style.top = "50%";
        box.style.left = "50%";
        box.style.marginLeft = "-275px";
        box.style.marginTop = "-125px";
        box.style.zIndex = "901";

        var otb = document.createElement("table");
        var otbod = document.createElement("tbody");
        otb.setAttribute('width','100%');
        otb.setAttribute('height','100%');
        var otr = document.createElement('tr');
        var otd = document.createElement('td');
        otd.setAttribute('width','100%');
        otd.setAttribute('height','100%');
        otd.setAttribute('align','center');
        otd.setAttribute('valign','middle');
        var tb = document.createElement('table');
        var tbod = document.createElement('tbody');
        var tr,td,txt,txtbox,btn,hr,span;

        tb.setAttribute("align","center");
        tb.setAttribute("valign","middle");
        tb.setAttribute("border","0");
        tb.style.verticalAlign = "middle";
        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        td.setAttribute("colspan","2");
        txt = document.createTextNode("Login name just taken.");
        td.appendChild(txt);
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","normTxt");
        td.setAttribute("className","normTxt");
        td.setAttribute("width","250px");
        td.setAttribute("align","justify");
        txt = document.createTextNode("When you started to create your account, the login name you chose was available.  However, from the time you began the account creation up until now, someone has taken that login name.  Since login names are given out on afirst come first serve basis, you will need to choose a different login name.");
        td.appendChild(txt);
        tr.appendChild(td);
        td = document.createElement('td');
        td.setAttribute("class","normTxt");
        td.setAttribute("className","normTxt");
        td.setAttribute("width","250px");
        td.setAttribute("align","center");
        txt = document.createTextNode("Enter desired login name");
        td.appendChild(txt);
        try { var br = document.createElement("<br>"); }
        catch(e) { var br = document.createElement("br"); }
        td.appendChild(br);
        try {
                txtbox = document.createElement("<input type='text' id='SEL_NEW_USERNAME'>");
        }
        catch(e) {
                txtbox = document.createElement("input");
                txtbox.setAttribute("type","text");
                txtbox.setAttribute("id","SEL_NEW_USERNAME");
        }
        txtbox.style.width = "100px";
        td.appendChild(txtbox);

        try {
                btn = document.createElement("<input type='button' value='Go'>");
        }
        catch(e) {
                btn = document.createElement("input");
                btn.setAttribute("type","button");
                btn.setAttribute("value","Go");
        }
        btn.style.cursor = "pointer";
        btn.onclick = function() { userIDTryAgain(); }
        td.appendChild(btn);
        tr.appendChild(td);
        tbod.appendChild(tr);


        tr = document.createElement('tr');
        td = document.createElement('td');
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        td.setAttribute("colspan","2");
        try { hr = document.createElement("<hr>"); }
        catch(e) { hr = document.createElement("hr"); }
        td.appendChild(hr);
        tr.appendChild(td);
        tbod.appendChild(tr);

        tb.appendChild(tbod);

        otd.appendChild(tb);
        otr.appendChild(otd);
        otbod.appendChild(otr);
        otb.appendChild(otbod);
        box.appendChild(otb);
        document.body.appendChild(box);

}
function userIDTryAgain() {

        var uid = trim(document.getElementById('SEL_NEW_USERNAME').value);
        if(uid.length >= 3) {
                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 = "ajax/checkUIDexists.php?uid=" + uid;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                recheckOfUID(MRES,uid);
        }
        else { alert("Login name must be at least 3 characters in length."); }
}

function resetLoginNameTo(tmpid,uid) {

                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 = "ajax/changeTemporaryUserID.php?tmpid=" + tmpid + "&uid=" + uid;
                xmlHttp.open("GET",url,false);
                xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
                xmlHttp.send(null);
                MRES = xmlHttp.responseText;
                finalizeAccountCreation('0')

}
function recheckOfUID(MRES,uid) {
        var R = trim(MRES);
        if(R == '1') {
                var tmpID = CREATE_ACCOUNT_ID;
                resetLoginNameTo(tmpID,uid);
        }
        else {
                alert("That login name is in use.");
        }
}
function tieItUp(MRES) {
        var R = trim(MRES).split("|");
        if(R[0] == "UID_EXISTS") { chooseDifferentUserID(); return 1; }

        if(R[0] == "SUCCESSFUL") {
		window.location = "http://www.therabill.com/INITIAL_LOGIN.php?uid=" + R[3] + "&upass=" + R[4];
        }
        else {
                alert("A FATAL ERROR HAS OCCURRED.  SUPPORT HAS BEEN NOTIFIED OF THIS ERROR.  SOMEONE FROM SUPPORT WILL BE CONTACTING YOU WITHIN THE NEXT 24 HOURS AT THE E-MAIL ADDRESS: " + CREATE_EMAIL + ".  FEEL FREE TO CONTACT OUR SUPPORT STAFF AT SUPPORT@THERABILL.COM");
                sendErrorReport(CREATE_EMAIL,MRES);
        }

}
function giveConfCodeMessage(msg) {
        var hohum = document.getElementById('CONF_MESSAGE_TABLE');
        while(hohum.childNodes.length > 0) { hohum.removeChild(hohum.firstChild); }
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.setAttribute("class","loginFontL");
        td.setAttribute("className","loginFontL");
        td.style.color = "red";
        txt = document.createTextNode(msg);
        td.appendChild(txt);
        tr.appendChild(td);
        hohum.appendChild(tr);
        var t = setTimeout('clearConfCodeMessage()',1300);
}

function clearConfCodeMessage() {
        var hohum = document.getElementById('CONF_MESSAGE_TABLE');
        while(hohum.childNodes.length > 0) { hohum.removeChild(hohum.firstChild); }
}
function createConfCode(email) {
        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 = "ajax/createEmailConfirmationCode.php?email=" + email;
        xmlHttp.open("GET",url,false);
        xmlHttp.setRequestHeader('Accept','message/x-jl-formresult');
        xmlHttp.send(null);
        MRES = xmlHttp.responseText;
        newAccountErrorCheck(MRES);

}

function showFormErrors() {
        for(var i=0; i<ID_ERRORS.length; i++) {
                var t = document.getElementById(ID_ERRORS[i]);
                t.style.backgroundColor = "#FF4540";
        }
        resetAccountCreateButton();
        alert(FORM_ERRORS);

}

function resetAccountCreateForm() {
        for(var i=0; i<ID_ERRORS.length; i++) {
                var t = document.getElementById(ID_ERRORS[i]);
                t.style.backgroundColor = "white";
        }
        FORM_ERRORS = "";
        ID_ERRORS = [];
        ID_ERRORS.length = 0;

}

function resetAccountCreateButton() {
        var but = document.getElementById('CREATEACCOUNTBUTTON');
        but.value = "Create Account";
        CREATING_ACCOUNT = 0;
}

function alphaChecker() {
        var uid = trim(document.getElementById('NEW_USERID').value);
        var newuid = "";
        var foundNoAN = 0;
        for(var j=0; j<uid.length; j++) {
                var alphaa = uid.charAt(j);
                var hh = alphaa.charCodeAt(0);
                if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) { newuid += alphaa; }
                else { foundNoAN = 1; }
        }
        document.getElementById('NEW_USERID').value = newuid;

        if(foundNoAN) {
                var warn = document.getElementById('UIDWARN');
                warn.style.color = "red";
                warn.style.fontSize = "15px";
                var t = setTimeout('returnWarning()',300);

        }
}
function numericChecker(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; }
        }
        obj.value = newval;
}

function returnWarning() {
        var warn = document.getElementById('UIDWARN');
        warn.style.fontSize = "12px";
        warn.style.color = "black";
}

function setHelpFocus(obj) {
        var objid = obj.getAttribute("id");
        if(objid == "NEW_ADDRESS2" || objid == "NEW_CITY" || objid == "NEW_STATE" || objid == "NEW_ZIP") { objid = "NEW_ADDRESS"; }
        var helpObj = document.getElementById("EXP_" + objid);
        helpObj.style.color = "#FF4540";
        CURRENT_FOCUS = "EXP_" + objid;
}
function unsetHelpFocus(obj) {
        var objid = obj.getAttribute("id");
        if(objid == "NEW_ADDRESS2" || objid == "NEW_CITY" || objid == "NEW_STATE" || objid == "NEW_ZIP") { objid = "NEW_ADDRESS"; }
        var helpObj = document.getElementById("EXP_" + objid);
        helpObj.style.color = "black";
        CURRENT_FOCUS = "";
}
function highlightHelpFocus(obj) {
        var objid = obj.getAttribute("id");
        if(objid == "NEW_ADDRESS2" || objid == "NEW_CITY" || objid == "NEW_STATE" || objid == "NEW_ZIP") { objid = "NEW_ADDRESS"; }
        var objID = "EXP_" + objid;
        if(CURRENT_FOCUS == objID) { return 1; }
        var helpObj = document.getElementById(objID);
        helpObj.style.color = "blue";
}
function unhighlightHelpFocus(obj) {
        var objid = obj.getAttribute("id");
        if(objid == "NEW_ADDRESS2" || objid == "NEW_CITY" || objid == "NEW_STATE" || objid == "NEW_ZIP") { objid = "NEW_ADDRESS"; }
        var objID = "EXP_" + objid;
        if(CURRENT_FOCUS == objID) { return 1; }
        var helpObj = document.getElementById(objID);
        helpObj.style.color = "black";
}


