/**
 * Set values for a specific item and button on the my account page
 */
function setValues(sName, sButtonContent)
{
    // set the cell content
    document.getElementById(sName + '_cell').innerHTML = xmlHttp.responseText;
    
    // set the button content
    document.getElementById(sName + '_button').innerHTML = sButtonContent;
}

/**
 * Get the field to edit the name
 */
function get_edit_name()
{
    // if ready to accept
    if (ready_to_accept())
    {
        // open the command
        xmlHttp.open('GET', 'ajax_commands/get_edit_name.php');
        
        // when changing status
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the components
                setValues('name', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready yet
    else
    {
        // repeat every 250 ms 
        setTimeout('get_edit_name()', 250);
    }
}

/**
 * Gets the controls for editing the address
 */
function get_edit_address()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the call
        xmlHttp.open('GET', 'ajax_commands/get_edit_address.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('address', '');
            }
        };
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250 ms until read
        setTimeout('get_edit_address()', 250);
    }
}

/**
 * Sets the controls for editing address 2
 */
function get_edit_address2()
{
    // if ready to accept
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_address2.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('address2', '');
            }
        };
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250 ms until ready
        setTimeout('get_edit_address2()', 250);
    }
}

/**
 * Gets the controls for editing a city
 */
function get_edit_city()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_city.php');
        
        // when the status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('city', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {   
        // repeat every 250ms until ready
        setTimeout('get_edit_city()', 250);
    }
}

/**
 * Get the controls for editing the state
 */
function get_edit_state()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_state.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('state', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('get_edit_state()', 250);
    }
}

/**
 * Gets the controls for editing zip code
 */
function get_edit_zip()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the XML call
        xmlHttp.open('GET', 'ajax_commands/get_edit_zip.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('zip', '');
            }
        };
        
        // sends the xml call
        xmlHttp.send(null); 
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('get_edit_zip()', 250);
    }
}

/**
 * Gets the controls for editing phone number
 */
function get_edit_phone()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_phone.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('phone', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('get_edit_phone()', 250);
    }
}

/**
 * Gets the controls for editing the email
 */
function get_edit_email()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_email.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set controls
                setValues('email', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250 ms until ready
        setTimeout('get_edit_email()', 250);
    }
}

/**
 * Gets the controls for editing payment information
 */
function get_edit_payment()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // empty the payment information table
        document.getElementById('payment_information_section').innerHTML = '';
        
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_edit_payment.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('payment', '');
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('get_edit_payment()', 250);
    }
}

/**
 * Show the payment information table when selecting payment information
 */
function show_payment_information(sValue)
{
    // switch values
	switch (sValue)
    {
        // if direct deposit
		case 'Direct Deposit':
		    // show direct deposit table and hide the rest
			document.getElementById('direct_deposit_table').style.display = '';
			document.getElementById('paypal_table').style.display = 'none';
			document.getElementById('mailed_check_table').style.display = 'none';
			break;
		
		// if paypal	
		case 'Paypal':
		    // show paypal table and hide the rest
		    document.getElementById('paypal_table').style.display = '';
		    document.getElementById('direct_deposit_table').style.display = 'none';
		    document.getElementById('mailed_check_table').style.display = 'none';
		    break;
		    
		// if mailed check
		case 'Mailed Check':
		    // hide everything
		    document.getElementById('paypal_table').style.display = 'none';
		    document.getElementById('direct_deposit_table').style.display = 'none';
		    document.getElementById('mailed_check_table').style.display = '';
		    break;
	}
}

/**
 * Edit the payment information
 */
function edit_payment()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // if direct deposit
        if (document.payment_form.payment_method[0].checked)
        {  
            // build the string for direct deposit
            sPayment = 'payment=' + document.payment_form.payment_method[0].value;
            sPayment += '&account_holder=' + document.payment_form.account_holder.value;
            sPayment += '&account_number=' + document.payment_form.account_number.value;
            sPayment += '&routing_number=' + document.payment_form.routing_number.value;
            sPayment += '&bank_type=' + document.payment_form.bank_type.options[document.payment_form.bank_type.selectedIndex].value;
            sPayment += '&bank_name=' + document.payment_form.bank_name.value;
        }
        // if paypal
        else if (document.payment_form.payment_method[1].checked)
        {
            // build string for paypal
            sPayment = 'payment=' + document.payment_form.payment_method[1].value;
            sPayment += '&paypal_address=' + document.payment_form.paypal_address.value;
        }
        // if direct mail
        else if (document.payment_form.payment_method[2].checked)
        {
            // build the string for direct mail
            sPayment = 'payment=' + document.payment_form.payment_method[2].value;
            sPayment += '&address=' + document.payment_form.address.value;
            sPayment += '&address2=' + document.payment_form.address2.value;
            sPayment += '&city=' + document.payment_form.city.value;
            sPayment += '&state=' + document.payment_form.state.value;
            sPayment += '&zip=' + document.payment_form.zip.value;
        }
        
        // start the xml call
        xmlHttp.open('GET', 'ajax_commands/edit_payment.php?' + sPayment);
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // get the payment information shown
                get_payment();
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('edit_payment()', 250);
    }
}

/**
 * Gets the payment informtation being set
 */
function get_payment()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/get_payment.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // set the controls
                setValues('payment', '<a href="#" onclick="get_edit_payment()">Edit</a>');
                
                // display payment information table
                display_payment_information();
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250 ms until ready
        setTimeout('get_payment()', 250);
    }
}

/**
 * Display Payment Information Table
 */
function display_payment_information()
{
    // if ready to accept commands
    if (ready_to_accept())
    {
        // open the xml call
        xmlHttp.open('GET', 'ajax_commands/display_payment_information.php');
        
        // when status changes
        xmlHttp.onreadystatechange = function myFunction()
        {
            // if ready to process
            if (ready_to_process())
            {
                // display the table
                document.getElementById('payment_information_section').innerHTML = xmlHttp.responseText;
            }
        };
        
        // send the xml call
        xmlHttp.send(null);
    }
    // if not ready
    else
    {
        // repeat every 250ms until ready
        setTimeout('display_payment_information()', 250);
    }
}



function check_username(sUsername)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/check_username.php?username=' + sUsername);
        xmlHttp.onreadystatechange = process_check_username;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('check_username("' + sUsername + '")', 250);
    }
}

function check_email(sEmail)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/check_email.php?email=' + sEmail);
        xmlHttp.onreadystatechange = process_check_email;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('check_email("' + sEmail + '")', 250);
    }
}

function process_check_username()
{
    if (ready_to_process())
    {
        if (xmlHttp.responseText == 'error_present')
        {
            alert("We're sorry, but that username already exists in our database. Please try a different username.");
            document.signup_step1.username.focus();
            document.signup_step1.username.value = '';
            document.signup_step1.user_error.value = 'true';
        }
        else if (xmlHttp.responseText == 'error')
        {
            alert("There was illegal character on your username. Please try a different username.");
            document.signup_step1.username.focus();
            document.signup_step1.username.value = '';
            document.signup_step1.user_error.value = 'true';
        }
        else
        {
            document.signup_step1.user_error.value = '';
        }        
    }
}

function process_check_email()
{
    if (ready_to_process())
    {
        if (xmlHttp.responseText == 'error')
        {
            alert("We're sorry, but that e-mail address already exists in our database.  Please enter a different e-mail address.");
            document.signup_step1.email.focus();
            document.signup_step1.email.value = '';
            document.signup_step1.email_error.value = 'true';
        }
        else
        {
            document.signup_step1.email_error.value = '';
        }
    }
}

function edit_user(sUsername)
{
    if (ready_to_accept())
    {
        var sSrecTransaction = document.getElementById(sUsername + '_srec_transaction').options[document.getElementById(sUsername + '_srec_transaction').selectedIndex].value;
        var iSrecsWon = document.getElementById(sUsername + '_srecs_won').value;
        var iSrecSold = document.getElementById(sUsername + '_srecs_sold').value;
        var sMoneyTransaction = document.getElementById(sUsername + '_money_transaction').options[document.getElementById(sUsername + '_money_transaction').selectedIndex].value;
        var sAmountOwed = document.getElementById(sUsername + '_amount_owed').value;
        var sAmountDue = document.getElementById(sUsername + '_amount_due').value;
        
        xmlHttp.open('GET', '/ajax_commands/edit_user.php?username=' + sUsername + '&srec_transaction=' + sSrecTransaction + '&srecs_won=' + iSrecsWon + '&srecs_sold=' + iSrecSold + '&money_transaction=' + sMoneyTransaction + '&amount_owed=' + sAmountOwed + '&amount_due=' + sAmountDue);
        
        xmlHttp.onreadystatechange = function myFunction() {
            process_edit_user(sUsername);
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_user(' + sUsername + ')', 250);
    }
}

function process_edit_user(sUsername)
{
    document.getElementById(sUsername + '_update').innerHTML = xmlHttp.responseText;
}












function edit_name()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_name.php?first_name=' + document.name_form.first_name.value + '&last_name=' + document.name_form.last_name.value);
        xmlHttp.onreadystatechange = process_edit_name;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_name()', 250);
    }
}

function edit_njep_username()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_njep_username.php?srec_username=' + document.njep_form.srec_username.value);
        xmlHttp.onreadystatechange = process_edit_njep_username;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_njep_username()', 250);
    }
}

function edit_address()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_address.php?address=' + document.address_form.address.value);
        xmlHttp.onreadystatechange = process_edit_address;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_address()', 250);
    }
}

function edit_address2()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_address2.php?address2=' + document.address_form2.address2.value);
        xmlHttp.onreadystatechange = process_edit_address2;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_address2()', 250);
    }
}

function edit_city()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_city.php?city=' + document.city_form.city.value);
        xmlHttp.onreadystatechange = process_edit_city;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_city()', 250);
    }
}

function edit_state()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_state.php?state=' + document.state_form.state.value);
        xmlHttp.onreadystatechange = process_edit_state;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_state()', 250);
    }
}

function edit_zip()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_zip.php?zip=' + document.zip_form.zip.value);
        xmlHttp.onreadystatechange = process_edit_zip;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_zip()', 250);
    }
}

function edit_phone()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_phone.php?phone=' + document.phone_form.phone.value);
        xmlHttp.onreadystatechange = process_edit_phone;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_phone()', 250);
    }
}

function edit_email()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/edit_email.php?email=' + document.email_form.email.value);
        xmlHttp.onreadystatechange = process_edit_email;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('edit_email()', 250);
    }
}



function process_edit_name()
{
    if (ready_to_process())
    {
        get_name();
    }
}

function process_edit_njep_username()
{
    if (ready_to_process())
    {
        get_njep_username();
    }
}

function process_edit_address()
{
    if (ready_to_process())
    {
        get_address();
    }
}

function process_edit_address2()
{
    if (ready_to_process())
    {
        get_address2();
    }
}

function process_edit_city()
{
    if (ready_to_process())
    {
        get_city();
    }
}

function process_edit_state()
{
    if (ready_to_process())
    {
        get_state();
    }
}

function process_edit_zip()
{
    if (ready_to_process())
    {
        get_zip();
    }
}

function process_edit_phone()
{
    if (ready_to_process())
    {
        get_phone();
    }
}

function process_edit_email()
{
    if (ready_to_process())
    {
        get_email();
    }
}


function get_name()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_name.php');
        xmlHttp.onreadystatechange = process_get_name;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_name()', 250);
    }
}

function get_njep_username()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_njep_username.php');
        xmlHttp.onreadystatechange = process_get_njep_username;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_njep_username()', 250);
    }
}

function get_address()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_address.php');
        xmlHttp.onreadystatechange = process_get_address;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_address()', 250);
    }
}

function get_address2()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_address2.php');
        xmlHttp.onreadystatechange = process_get_address2;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_address2()', 250);
    }
}

function get_city()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_city.php');
        xmlHttp.onreadystatechange = process_get_city;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_city()', 250);
    }
}

function get_state()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_state.php');
        xmlHttp.onreadystatechange = process_get_state;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_state()', 250);
    }
}

function get_zip()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_zip.php');
        xmlHttp.onreadystatechange = process_get_zip;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_zip()', 250);
    }
}

function get_phone()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_phone.php');
        xmlHttp.onreadystatechange = process_get_phone;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_phone()', 250);
    }
}

function get_email()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', 'ajax_commands/get_email.php');
        xmlHttp.onreadystatechange = process_get_email;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_email()', 250);
    }
}



function get_edit_order(iOrderId)
{
    if (ready_to_accept()) 
    {
        xmlHttp.open('GET', '/ajax_commands/edit_order_book.php?srec_orders_id=' + iOrderId)
        xmlHttp.onreadystatechange = function ()
        {
            if (ready_to_process())
            {
                document.getElementById('order_' + iOrderId).innerHTML = xmlHttp.responseText;
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_edit_order(' + iOrderId + ')', 250);
    }
}


function process_get_name()
{
    if (ready_to_process())
    {
        setValues('name', '<a href="#" onclick="get_edit_name()">Edit</a>');
    }
}

function process_get_address()
{
    if (ready_to_process())
    {
        setValues('address', '<a href="#" onclick="get_edit_address()">Edit</a>');
    }
}

function process_get_address2()
{
    if (ready_to_process())
    {
        setValues('address2', '<a href="#" onclick="get_edit_address2()">Edit</a>');
    }
}

function process_get_city()
{
    if (ready_to_process())
    {
        setValues('city', '<a href="#" onclick="get_edit_city()">Edit</a>');
    }
}

function process_get_state()
{
    if (ready_to_process())
    {
        setValues('state', '<a href="#" onclick="get_edit_state()">Edit</a>');
    }
}

function process_get_zip()
{
    if (ready_to_process())
    {
        setValues('zip', '<a href="#" onclick="get_edit_zip()">Edit</a>');
    }
}

function process_get_phone()
{
    if (ready_to_process())
    {
        setValues('phone', '<a href="#" onclick="get_edit_phone()">Edit</a>');
    }
}

function process_get_email()
{
    if (ready_to_process())
    {
        setValues('email', '<a href="#" onclick="get_edit_email()">Edit</a>');
    }
}




