var currentStep = 1;

function handleSubmit(f) {
    var c = $F('ContactForm');
    var friend = $('sendToFriend');
    var maxSteps = (friend) ? 2 : 3;
    
    if(currentStep == maxSteps) {
        
        $(f).request({
            method: 'post',
            parameters: '_validate=1',
            onFailure: function(transport) {
                $('errors').update('<h3>The Following Error(s) Occurred:</h3>' + transport.responseText).show()
		        $('container').scrollTo();
            },
            onSuccess: function() {
                f.submit();
            }
        });
        
        return false;
    }

    listSelectedItems();
    listContactInfo();

    toggleCurrentStep();
    
    currentStep += 1;
    
    toggleNextStep();
    toggleActualSteps();
    
    return false;
}

function goBack(howMany) {
    togglePrevStep();
    
    currentStep = (howMany || 1);
    
    toggleCurrentStepOff();
    toggleActualSteps();
    
    return false;
}

//toggle current nav off
function toggleCurrentStep() {
    var thisNavStep = $('steps').select('li.navstep' + currentStep).first();
    thisNavStep.addClassName('active_back').removeClassName('active');
}

//toggle current nav off
function toggleCurrentStepOff() {
    var thisNavStep = $('steps').select('li.navstep' + currentStep).first();
    thisNavStep.addClassName('active').removeClassName('active_back');
}

//toggle previous nav off
function togglePrevStep() {
    var thisNavStep = $('steps').select('li.navstep' + currentStep).first();
    thisNavStep.addClassName('inactive').removeClassName('active');
}

//toggle next nav on
function toggleNextStep() {
    var nextNavStep = $('steps').select('li.navstep' + currentStep).first();
    nextNavStep.addClassName('active').removeClassName('inactive');
}

//hide all steps and show next step
function toggleActualSteps() {
    $('step1', 'step2', 'step3').invoke('hide');
    $('step' + currentStep).show();
    $('verbiage').show();
    
    $('sidebar-product', 'sidebar-contact').invoke('hide');
    $$('.buttons').first().setStyle({ width: 'auto' });
    $('btn').src = 'images/btn_continue.gif';
    
    if(currentStep > 1) {
        $('sidebar-product').show();
        $$('.buttons').first().setStyle({ width: '590px' });
        $('verbiage').hide();
    }
    
    if(currentStep > 2 || ($('sendToFriend') && currentStep == 2)) {
        $('sidebar-contact').show();
        $('btn').src = 'images/btn_request.gif';
    }

    $('errors').hide();

    //BridgeTrack ads
    if(currentStep == 2) BridgeTrackAds(4662); //contact info "page"
    if(currentStep == 3) BridgeTrackAds(4663); //additional info "page"
}

//build list of selected items
function listSelectedItems() {
    var items = $('items');
    items.select('li').invoke('remove');
    
    $('step1').select('thead input[type="checkbox"]').each(function(chk){
        if(!chk.checked) return;
        
        items.insert(new Element('li').update(chk.value));
    });
}

//build list of contact info
function listContactInfo() {
    var c = $F('ContactForm');
    
    var content = '<div>' + $F('shipNameFirst' + c) + ' ' + $F('shipNameMiddle' + c) + ' ' + $F('shipNameLast' + c) + '</div>'
        + '<div>' + $F('shipAddress1' + c) + '</div>';

    var address2 = $F('shipAddress2' + c);
    if(address2 && address2 != '') content += '<div>' + address2 + '</div>';
    
    content += '<div>' + $F('shipCity' + c) + ', ';
    
    countryID = $('shipCountryID' + c);
    if(countryID.getValue() == '0') {
        var st = $('shipStateID' + c);
        content += st.options[st.selectedIndex].text;
    }
    else {
        content += $F('shipProvince' + c);
    }
    
    content += ' ' + $F('shipPostalCode' + c) + '</div>'
        + '<div>' + countryID.options[countryID.selectedIndex].text + '</div>'
        + '<br />'
        + '<div>' + $F('shipPhoneHome' + c) + '</div>'
        + '<div>' + $F('shipEmail' + c) + '</div>';

    $('mailing-address').update(content);
}

function BridgeTrackAds(id) {
    var szProtocol = window.location.protocol;
    var szRandom = Math.random() * 1000000;

    $('container').insert('<img src="' + szProtocol + '//ads.bridgetrack.com/track/?id=' + id + '&r=' + szRandom + '" width="1" height="1" border="0" />');
}