document.observe('dom:loaded', function(){
    $$('dl.item-options').each(function(value,index){
        var enter_presciption_step =false;
        var prescription_step_html_content = '';
        var pr_tr5 = pd = '&nbsp;';
        var od_sph = od_cyl = od_axis = od_add = '&nbsp;';
        var os_sph = os_cyl = os_axis = os_add = '&nbsp;';
        var pd_left = pd_right = pd_far = pd_near = '';
        
        var dts = value.select('dt');
        
        dts.each(function(value,index){    
            var label = value.innerHTML;
            if(label.indexOf('OD SPH') == 0){
                od_sph = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }else if(label.indexOf('OD CYL') == 0){
                od_cyl = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            else if(label.indexOf('OD AXIS') == 0){
                od_axis = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            else if(label.indexOf('OD ADD') == 0){
                od_add = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            if(label.indexOf('OS SPH') == 0){
                os_sph = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }else if(label.indexOf('OS CYL') == 0){
                os_cyl = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            else if(label.indexOf('OS AXIS') == 0){
                os_axis = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            else if(label.indexOf('OS ADD') == 0){
                os_add = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();
            }
            else if(label.indexOf('PD FAR') == 0){
                pd_far = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();    
            }
            else if(label.indexOf('PD NEAR') == 0){
                pd_near = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();    
            }
            else if(label.indexOf('PD LEFT') == 0){
                pd_left = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();    
            }
            else if(label.indexOf('PD RIGHT') == 0){
                pd_right = value.next('dd').innerHTML;
                enter_presciption_step = true;
                value.next('dd').remove(); value.remove();    
            }
            else if(label.indexOf('comments') != -1){
                enter_presciption_step = true;
                pr_tr5 = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
            }
        });

            //create prescription step table html
        if(enter_presciption_step == true){
            prescription_step_html_content = '<table id="cart_prescription_table" cellspacing="4">'+
            '<tr class="head"><td class="label">&nbsp;</td><td>SPH</td><td>CYL</td><td>AXIS</td><td class="last">ADD</td></tr>'+
            '<tr class="b-top-bottom"><td class="label"><b>O.D.(right)</b></td><td>'+od_sph+'</td><td>'+od_cyl+'</td><td>'+od_axis+'</td><td class="last">'+od_add+'</td></tr>'+
            '<tr><td class="label"><b>O.S.(left)</b></td><td>'+os_sph+'</td><td>'+os_cyl+'</td><td>'+os_axis+'</td><td class="last">'+os_add+'</td></tr>'+
            '<tr class="head"><td class="label">&nbsp;</td><td>FAR</td><td>NEAR</td><td>RIGHT PD</td><td>LEFT PD</td></tr>'+
            '<tr class="b-top-bottom"><td class="label"><b>PD</b></td><td>'+pd_far+'</td><td>'+pd_near+'</td><td>'+pd_right+'</td><td class="last">'+pd_left+'</td></tr>';
            if(pr_tr5 != ''){
                prescription_step_html_content += '<tr><td><b>Additional<br />Comments:</b></td><td class="last" colspan=4>'+pr_tr5+'</td></tr>';
            }
            prescription_step_html_content += '</table>';
            
            value.update(value.innerHTML + '<dt><label>Prescription</label></dt>');
            value.update(value.innerHTML + '<dd>'+prescription_step_html_content+'</dd>');
                    
        }
    });
});

function trim(str){
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

/*Contacts options*/
document.observe('dom:loaded', function(){
    $$('.product-options dl').each(function(value,index){

        var os_power = od_power = '';
        var os_dia = od_dia = '';
        var os_bc = od_bc = '';
        var os_qty = od_qty = '';
        var os_price_per_box = od_price_per_box = '';
        var create_contacts_table = false;
        
        var dts = value.select('dt');
        
        dts.each(function(value,index){    
            var label = value.innerHTML;
            if(label.indexOf('OS POWER') != -1){
                os_power = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD POWER') != -1){
                od_power = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true; 
            }else if(label.indexOf('OS DIA') != -1){
                os_dia = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD DIA') != -1){
                od_dia = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OS BC') != -1){
                os_bc = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD BC') != -1){
                od_bc = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OS QTY') != -1){
                os_qty = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD QTY') != -1){
                od_qty = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }
            else if(label.indexOf('OS PRICE PER BOX') != -1){
                os_price_per_box = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD PRICE PER BOX') != -1){
                od_price_per_box = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }              
        });

        /*
        //create contacts table html
        if(create_contacts_table){
             var contacts_table_html ='<div id="contacts-options-table"><table>'+
             '<tbody><tr>'+
              '<td width="8%" valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Eye </td>'+
              '<td width="10%" valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Power </td>'+
              '<td width="10%" valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">BC </td>'+
              '<td width="10%" valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Dia. </td>'+
              '<td width="10%" valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Qty. </td>'+
            '</tr>'+
            '</tbody>'+
             '</table>';
            contacts_table_html += '<table width="100%" cellspacing="1" cellpadding="0" border="0">'+
                                '<tr>'+
                                  '<td width="8%" valign="middle" height="28" style="background-color:#fff;font-size:12px;font-weight:400" class="PrescriptionRLNewStyle">R (OD) </td>';
            contacts_table_html += '<td>'+od_power+'</td><td>'+od_bc+'</td><td>'+od_dia+'</td><td>'+od_qty+'</td></tr>'+
            '<tr><td width="8%" valign="middle" height="28" style="background-color:#fff;font-size:12px;font-weight:400" class="PrescriptionRLNewStyle">L (OS) </td>';
            contacts_table_html += '<td>'+os_power+'</td><td>'+os_bc+'</td><td>'+os_dia+'</td><td>'+os_qty+'</td></tr>';                      
            contacts_table_html + '</table></div>';
            $('product-options-wrapper').innerHTML = contacts_table_html;
        
            //remove 'please select'
             jQuery.each(jQuery('#contacts-options-table select'),function(index, value){
                jQuery(this).children(":first").html('');
            });
        
        }
        */
            
    });
});

/*For cart page*/
document.observe('dom:loaded', function(){
    $$('dl.item-options').each(function(value,index){

        var os_power = od_power = '';
        var os_dia = od_dia = '';
        var os_bc = od_bc = '';
        var os_qty = od_qty = '';
        var os_price_per_box = od_price_per_box = '';
        var create_contacts_table = false;
        
        var dts = value.select('dt');
        
        dts.each(function(value,index){    
            var label = value.innerHTML;
            if(label.indexOf('OS POWER') != -1){
                os_power = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD POWER') != -1){
                od_power = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true; 
            }else if(label.indexOf('OS DIA') != -1){
                os_dia = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD DIA') != -1){
                od_dia = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OS BC') != -1){
                os_bc = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD BC') != -1){
                od_bc = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OS QTY') != -1){
                os_qty = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD QTY') != -1){
                od_qty = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }
            else if(label.indexOf('OS PRICE PER BOX') != -1){
                os_price_per_box = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }else if(label.indexOf('OD PRICE PER BOX') != -1){
                od_price_per_box = value.next('dd').innerHTML;
                value.next('dd').remove(); value.remove();
                create_contacts_table = true;
            }              
        });

        //create contacts table html
        if(create_contacts_table){
             var contacts_table_html ='<div id="contacts-options-table"><table width="100%">'+
             '<tbody><tr>'+
              '<td  valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Eye </td>'+
              '<td  valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Power </td>'+
              '<td  valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">BC </td>'+
              '<td  valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Dia. </td>'+
              '<td  valign="middle" height="28" style="font-size:12px;font-weight:600" class="PrescriptionRowNewStyle">Qty. </td>'+
            '</tr>'+
            '</tbody>'+
             '</table>';
            contacts_table_html += '<table width="100%">'+
                                '<tr>'+
                                  '<td class="PrescriptionRLNewStyle">R (OD) </td>';
            contacts_table_html += '<td>'+od_power+'</td><td>'+od_bc+'</td><td>'+od_dia+'</td><td>'+od_qty+'</td></tr>'+
            '<tr><td  valign="middle" height="28" style="background-color:#fff;font-size:12px;font-weight:400" class="PrescriptionRLNewStyle">L (OS) </td>';
            contacts_table_html += '<td>'+os_power+'</td><td>'+os_bc+'</td><td>'+os_dia+'</td><td>'+os_qty+'</td></tr>';                      
            contacts_table_html + '</table></div>';
            $$('dl.item-options')[0].innerHTML = $$('dl.item-options')[0].innerHTML+contacts_table_html;
        
            //remove 'please select'
             jQuery.each(jQuery('#contacts-options-table select'),function(index, value){
                jQuery(this).children(":first").html('');
            });
        
        }
            
    });
});

document.observe('dom:loaded', function(){
    var product_price = jQuery('.price-box .price').text();
    product_price = parseFloat(product_price.substr(1));
    product_price = product_price.toFixed(2);

  /*Replace "--Please select--" with "Select" in contacts options*/  
    jQuery.each(jQuery('.contacts-options select'),function(index, value){
        jQuery(this).children(":first").html('Select');
    });
/*Hide qty - this is moved to view.phtml*/
/*     
$$('.qty-box').each(function(d){
    d.style.display = 'none';
});
*/     
/*Change the hidden fields value and price when changing os/od qty*/    
    jQuery('.os_qty select').change(function() {
        var selected_value = jQuery(".os_qty select option:selected").text().trim();
        jQuery('#contacts_os_qty').val(selected_value);
        var new_buy_price = parseInt(selected_value)*product_price; //left price
        new_buy_price = new_buy_price + parseInt(jQuery('#contacts_od_qty').val())*product_price;
        new_buy_price = new_buy_price.toFixed(2); 
        new_buy_price = '$'+new_buy_price;
        jQuery('.price-box .price').text(new_buy_price);
    });
    jQuery('.od_qty select').change(function() {
        var selected_value = jQuery(".od_qty select option:selected").text().trim();
        jQuery('#contacts_od_qty').val(selected_value);
        var new_buy_price = parseInt(selected_value)*product_price; //right price
        new_buy_price = new_buy_price + parseInt(jQuery('#contacts_os_qty').val())*product_price;
        new_buy_price = new_buy_price.toFixed(2); 
        new_buy_price = '$'+new_buy_price;
        jQuery('.price-box .price').text(new_buy_price);
    });
});

