﻿var activeCheck;var hasProducts=false;var isRefused=false;var dialogOpen = false;jQuery(function(){var dlg=jQuery('#productdialog').dialog({bgiframe:true,autoOpen:false,modal:true,resizable:false,width:803});dlg.parent().appendTo(jQuery("form:last"));setupchecks();setupcontent();});function nothanks(){jQuery('#productdialog input:checkbox:checked').attr('checked',false);jQuery('#productdialog td span').html('');jQuery('#productdialog').dialog('close');isRefused=true;jQuery(callingButton).click();}
function tidy(){jQuery('#productdialog input:checkbox:checked').attr('checked',false);jQuery('#productdialog td span').html('');return true;}
function setupchecks(){var chks=jQuery('#productdialog input[type="checkbox"]');jQuery(chks).each(function(i){jQuery(this).click(function(){if(jQuery(this).attr('checked')==true){activeCheck=this;enableselect();}else{activeCheck=this;disableselect()};});});}
function enableselect(){jQuery(activeCheck).parent().siblings('.AccessoriesContainer-PriceBox').children('select').removeAttr("disabled");}
function disableselect(){jQuery(activeCheck).parent().siblings('.AccessoriesContainer-PriceBox').children('select').attr("disabled","disabled");}
function setupcontent(){jQuery('#products').show();jQuery('#accessories').hide();}
function togglecontent(){jQuery('#products').slideToggle();jQuery('#accessories').slideToggle(function(){if(jQuery(this).is(':visible')){LoadOptions(activeCheck)};});}
function LoadOptions(){var opts=eval('('+jQuery(activeCheck).siblings('input[type="hidden"]').val()+')');var items='';for(var i=0;i<opts.options.length;i++){items+='<option value="'+opts.options[i].Value+'">'+opts.options[i].Name+'</option>';}
jQuery("#optionlist").html(items);}
function opendialog(){
    
    if(validateproductpage()){
        
        jQuery('#globalNotifier').hide();
        if(hasProducts||isRefused)
            return true;

        if(!hasProducts||!isRefused){
            jQuery.ajax({
                type: "POST",
                url: "addtocart.ashx",
                data: jStr + "&a=" + jQuery('.AddToCartContainerSmall-AttDDL').val() + "&q=" + jQuery(qtb).val(),
                success: function(msg){openthedialog();},
                error: function(msg){jQuery(hiddenBtn).click();}
             });
            return false;
            
        }else{
            jQuery('#globalNotifier').show();
            return false;
        }
    }else{
        jQuery('#globalNotifier').show();
        return false;
    }
}


function openthedialog()
{
    dialogOpen = true;
    jQuery('#productdialog').dialog('open');
    jQuery('#NoThanksDialog input').blur();
    setupcontent();
}

function closedialog(){jQuery('#productdialog input:checkbox:checked').attr('checked',false);jQuery('#productdialog td span').html('');jQuery('#productdialog').dialog('close');return true;}
function addproducts(){hasProducts=true;jQuery('#productdialog').dialog('close');}
function updatepage(products){jQuery(productsField).val(products);jQuery('#SelectedProducts').html(products);}
function addaccessory(optionname,optionval){updateproduct(optionname,optionval);togglecontent();}
function updateproduct(optionname,optionval){updatespanoption(optionname,optionval,"block")}
function updatespanoption(optionname,optionval,display){var td=jQuery(activeCheck).parent().parent().parent();var span=jQuery(td).children('span');jQuery(span).attr("title",optionval).html(optionname).css("display",display);}
function validatelists(){var chks=jQuery('#productdialog input[type="checkbox"]:checked');var isSubmitReady=true;jQuery(chks).each(function(i){var container=jQuery(this).parent().parent();var span=jQuery(container).children('span').children('span');var select=jQuery(span).children('select');var selectCheck=jQuery(this);if(jQuery(select).attr('selectedIndex')==0){jQuery(span).css("border","2px solid red");isSubmitReady=false;jQuery(select).change(function(){if(jQuery(select).attr('selectedIndex')==0&&jQuery(selectCheck).attr('checked')==true){jQuery(span).css("border","2px solid red");}else{jQuery(span).css("border","none");}});}
if(!isSubmitReady){jQuery('#ErrorBox').show();}});return isSubmitReady;}
function validateproductpage()
{
    var select=jQuery('.AddToCartContainerSmall-AttDDL');
    var span=jQuery(select).parent();
    if(jQuery(select).attr('selectedIndex')==0)
    {
        jQuery(span).css("border","2px solid red");
        return false;
    }else{
        jQuery(span).css("border","none");
        return true;
    }
    return true;
}
