$(document).ready(function() {  

    var max = 100;  

    

     var pcLabels = $("div[rel^='pcelements'] label"); 

    

     pcLabels.each(function(){  

         if ($(this).width() > max)  

             max = $(this).width();  

     });  

     pcLabels.width(max);  

     

       

     var max2 = 250;  

     
     var pcSelects = $("div[rel^='pcelements'] select");
     var pcSelectsQty = $("div[rel^='pcelements'] input.elQty");
     

     pcSelects.each(function(){  

         if ($(this).width() > max2)  

             max2 = $(this).width();  

     });  

     pcSelects.width(max2);       

     pcSelects.change(UpTemplate);

     pcSelectsQty.bind('change keyup', UpTemplate);
//     pcSelectsQty.change(UpTemplate);

     $(".error_box").text('').hide();

     

     

     $("div[rel^='pcelements']").find("a.infoimg").bind("click", function(e){

          url = $(this).attr('href');

          myname = $(this).attr('title');

          popw = 650;

          poph = 650;

          popscroll = 'yes';

          

          var winl = (screen.width - popw) / 2;

          var wint = (screen.height - poph) / 2;

          winprops = 'height='+poph+',width='+popw+',top='+wint+',left='+winl+',scrollbars='+popscroll+',resizable=1';

          win = window.open(url, myname, winprops);

          if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }         

         

          return false;

     });

          

    

     var selID = false;

     

     function UpTemplate() {



         selID = $(this);

         var oldSelBorder = selID.css("border");

         if ($(this).attr('class') == 'elQty') {
           if ($(this).val() <= 0) return false;
         }

//         alert($(this).attr('name'));
         

         $.ajax({

           type: "POST",

           url: resURL,

           cache: false,

           dataType: 'json',
           data: pcSelects.serialize() +'&'+ pcSelectsQty.serialize() + '&selel='+ selID.attr("id") + '&productID=' + productID + '&confID=' + confID + sessID,
           

           beforeSend: function(){

             //dasable all selects 

             pcSelects.attr("disabled","disabled");
             pcSelectsQty.attr("disabled","disabled");
             $(".error_box").text('').hide();

//             selID.css("border","1px solid #f00");

           }, 

           

           error: function(XMLHttpRequest, textStatus, errorThrown){

             show_error( "AJAX Error: " + textStatus + '[' + errorThrown + ']' );

           },

           

           success: function(result){

            

                // update selects

                if (result['error'] == true) {
                    show_error(result['error_message']);
                    pcSelects.removeAttr("disabled");
                    pcSelectsQty.removeAttr("disabled");

                    selID.focus();

                    return false;
                }

                

                $.each(result['products'], function(pIndex, pData) {

                    $("option[value="+pIndex+"]").text(pData);

                }); 

                

                $.each(result['elements'], function(eIndex, eData) {

                    $("select[id="+eIndex+"]").val(eData);



                    if (result['productsIds'][eData] > 0) { 

                      elhref = $('div[rel="pcelements_'+eIndex+'"]').find("a.infoimg");

                      elhref.attr('href',elhref.attr('href').replace(/#.*/, '') + '#' + result['productsIds'][eData])

//                      elhref.attr('title',result['products'][eData]);

                    }  

                  

                });                

                 

                $("#pTopPrice").text(result['shownPrice']);
                $("#pTopListPrice").text(result['shownListPrice']);
                $("#productNEWprice").text('Subtotal Price with Options: ' + result['shownPrice']);

                   

                   

//                alert( pIndex + '<>' + eIndex );     

                   

                pcSelects.removeAttr("disabled");
                pcSelectsQty.removeAttr("disabled");
                
                selID.focus();
             

             

           }

         });

         return true;
     }



     

     function show_error(msg) {

         $(".error_box").html(msg).show();

         return true;

     }

     

    

     

});





function NewWindow(url, myname, w, h, scroll, id)

{

   var winl = (screen.width - w) / 2;

   var wint = (screen.height - h) / 2;

   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=1';

   if(id != null)

        url = url + id.selectedIndex;

   win = window.open(url, myname, winprops);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }

}   


