// Binds a callback function to an element's event handler dynamically, passing the callback function a parameter.
function bind_function(func, this_object) 
{
    var args = new Array(arguments[1]);
    return function() 
    { 
        return func.apply(this_object, args);
    };
}

$(document).ready(onload_event_handler);
function onload_event_handler()
{
    // Bind a callback function to the onfocus event of all the elements in the form.
    var form = document.getElementById('wizard-form');
    for(i = 0; i < document.forms.length; ++i)
    {
        for(j = 0; j < document.forms[i].elements.length; ++j)
        {
            // FF, Safari, etc..
            if(document.forms[i].elements[j].addEventListener)
            {
                document.forms[i].elements[j].addEventListener('focus', bind_function(update_instructions, document.forms[i].elements[j]), false);
            }
            // IE
            else
            {
                document.forms[i].elements[j].attachEvent('onfocus', bind_function(update_instructions, document.forms[i].elements[j]));
            }
        }
    }

    // Check if there's any fields the user didn't fill to highlight.
    highlight_missing_fields(missing_fields);

    // On the first wizard page, some static instructions should be displayed in the sidebar.
    if(wizard_page == 1)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>\
            Answer the questions by choosing from the dropdown menus. <br /><br />\
            When you click on a question's menu item, an explanation of that question or data request will display here.\
        </div>"
        );        
    }
    
    // If we're on the second page of the wizard, update the instructions pane with the state / project type / contractor role instrictions.
    else if(wizard_page == 2)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div> " + second_page_instructions + "</div>"
        );
    }
    
    else if(wizard_page == 100)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>\
            Create a free account with zLien to use our industry-leading Lien Pilot web application.<br /><br />\
            With an zLien account, you can manage project data, preliminary notices and lien documents,\
            and even keep track of lien and notice deadlines.\
            Then, filing a construction lien or notice takes just a click of the button.\
        </div>"
        );        
    }
    
    else if(wizard_page == 3)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>Add here your personal data</div>"
        );
        // for "About You" wizard page
        $("#company_name").trigger("focus");
    }
    
    else if(wizard_page == 4)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>Add here Project data or select an existent one</div>"
        );
        // for "About You" wizard page
        $("#company_name").trigger("focus");
    }
    
    else if(wizard_page == 5)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>Add here personal data of Project contacts</div>"
        );
    }
    
    else if(wizard_page == 6)
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div>Add here order information</div>"
        );
    }
}

// The function responsible for updating the instructions.
function update_instructions(field) 
{
    if(instructions[field.name])
    {
        $("#sidebar").html("\
        <h2>Instructions</h2>\
        <div> " + instructions[field.name] + "</div>"
            );
    }
}

// Compare the form field id's to the names of missing fields stored in the session to highlight the paragraphs enclosing the missing fields.
function highlight_missing_fields(fields)
{
    for(var i = 0; i < fields.length; ++i)
    {
        var field = document.getElementById(fields[i]);        
        var parent = field.parentNode;
        $(parent).addClass("error-highlight");
    }
}

function select_price(obj, spanId)
{
    
    var priceId = $(obj).val();
    var discount_str = '';
    if(discount > 0)
    {
        discount_str = '<font color="grey">(<font color="green">-' + discount + '%</font>)</font>&nbsp;';
    }
    var html  = '<span style="color: #969696;" onmouseover="Tip(\'This is the price of the selected product. The <b>discount<\/b> (if any) indicated in green.<br><br>This price <b>includes<\/b>:<br><br><ul><li>Filing fees<\/li><li>Delivery and Service Charges<\/li><li>Preparation and Filing<\/li><li>Research of Legal Property Description<\/li><li>Verification of Property Owner with Public Records<\/li><li>Safe, Online Storage of Documents<\/li><\/ul>\', BALLOON, true, ABOVE, true, OFFSETX, -17, PADDING, 8, BALLOONIMGPATH, \'/admin/images/tip_balloon/\')" onmouseout="UnTip()">';
        html += productPrices['id-'+priceId] + "&nbsp;"+ discount_str + "<span style=\"cursor:pointer;\" id=\"help-price\"><img style=\"margin-bottom:-3px;\" src=\""+baseUrl+"/images/help.png\" /></span>";
        html += '</span>';
    
    // system enabled and there is a price and the product is not stopped
    if(justform_enabled && justformPrices['id-'+priceId] && parseInt(justformEnable['id-'+priceId]) == 1)
    {
        // remembering input select from session variable
        if(justform_selected == 1)
        {
            var sel_form = ' checked="checked"';
            var sel_norm = '';
        }
        else
        {
            var sel_form = '';
            var sel_norm = ' checked="checked"';
        }
        var justform_html = '<div id="justform-div" class="choose-order-type"><input onclick="$(\'#justform_selected\').val(\'\');" type="radio" name="order_type" value="ordinary"'+ sel_norm +' />&nbsp;Do All The Work For Me';
        justform_html += '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #969696;" onmouseover="Tip(\'This is the price of the selected product. The <b>discount<\/b> (if any) indicated in green.<br><br>This price <b>includes<\/b>:<br><br><ul><li>Filing fees<\/li><li>Delivery and Service Charges<\/li><li>Preparation and Filing<\/li><li>Research of Legal Property Description<\/li><li>Verification of Property Owner with Public Records<\/li><li>Safe, Online Storage of Documents<\/li><\/ul>\', BALLOON, true, ABOVE, true, OFFSETX, -17, PADDING, 8, BALLOONIMGPATH, \'/admin/images/tip_balloon/\')" onmouseout="UnTip()">';
        justform_html += html;
        justform_html += '</span>';
        justform_html += '<br /><br /><input onclick="$(\'#justform_selected\').val(\'1\');" type="radio" name="order_type" value="justform"'+ sel_form +' />&nbsp;Just Give Me The Form';
        justform_html += '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #969696;" onmouseover="Tip(\'This is the price of the selected product. The <b>discount<\/b> (if any) indicated in green.<br><br>This price <b>includes<\/b> sending you document text.\', BALLOON, true, ABOVE, true, OFFSETX, -17, PADDING, 8, BALLOONIMGPATH, \'/admin/images/tip_balloon/\')" onmouseout="UnTip()">';
        justform_html += '$' + justformPrices['id-'+priceId] + "&nbsp;"+ discount_str + "<span style=\"cursor:pointer;\" id=\"help-price-form\"><img style=\"margin-bottom:-3px;\" src=\""+baseUrl+"/images/help.png\" /></span>";
        justform_html += '</span>';
        justform_html += '';
        justform_html += '</div>';
        
        var pos = $(obj).position();
        var newTop = parseInt(pos.top) - 50;
        var newLeft = parseInt(pos.left) + $(obj).width() + 15;
        $("#" + spanId).hide().html(justform_html).fadeIn();
        $("#justform-div").css({"top": newTop + 'px', "left": newLeft + 'px'});
    }
    else
    {
        $("#" + spanId).hide().html(html).fadeIn();
    }    
    
}

