﻿        $(document).ready(function() {
            // lightbox
            $('a.gallery').lightBox({ fixedNavigation: true });
        
            $('#divTellAFriendThanks').hide();
            $('#'+_PnlTellAFriend).hide();
            $('#'+_PnlInquireForm).hide();
            
            $('#aRequestInfo').click(function(){
                $('#divInquiryConfirmation').hide();
                $('#divInquiryForm').show();
                
                $(document).Modal({
                    ModalObject:$('#'+_PnlInquireForm)[0],
                    CssName:'modal-bg',
                    zIndex: 99999
                });
            });
            
            
            $('#btnInquiryCancel,#btnInquiryThankYouClose').click(function(){
                $(document).Modal({
                    ModalObject:$('#'+_PnlInquireForm)[0]
                });
            });
            
            $('#btnOpenCommentForm').click(function(){
                $(document).Modal({
                    ModalObject:$('#product-detail-comment-form')[0],
                    CssName:'modal-bg',
                    zIndex: 99999
                });
            });
            
            $('#btnCloseCommentForm').click(function(){
                $(document).Modal({
                    ModalObject:$('#product-detail-comment-form')[0]
                });
            });
            
            $('#aTellAFriend').click(function(){
                $('#divFriendForm').show();
                $('#divTellAFriendThanks').hide();
                
                $(document).Modal({
                    ModalObject:$('#'+_PnlTellAFriend)[0],
                    CssName:'modal-bg',
                    zIndex: 99999
                });
                
            });
            
            $('#btnFriendCancel,#btnFriendCloseThanks').click(function(){
                $(document).Modal({
                    ModalObject:$('#'+_PnlTellAFriend)[0]
                });
                
                $('#divTellAFriendThanks').hide();
            });
            
            $('[ProductOptions]').change(function(){
                var intIndex = 0;
                var blnAllChosen = false;
                var strValue = '';
                var arrValues = new Array();
                var intInventory = 0;
                var intVariationId = 0;
                var blnUsesInventory = false;
                
                for(intIndex=0;intIndex <= _arrOptionId.length-1;intIndex++){
                    strValue = $('#'+_arrOptionId[intIndex]).attr('value');
                    
                    if(IsDefined(strValue)){
                        if(strValue.trim().length == 0){
                            blnAllChosen = true;
                            break;                        
                        }
                    }else{
                        blnAllChosen = true;
                        break;
                    }

                }
                
                if(! blnAllChosen){
                    intVariationId = $(this).attr('VariationId');
                    
                    if(IsDefined(intVariationId) && _blnUsesInventory){
                        arrValues = strValue.split(';');
                        if(arrValues.length == 3){
                            intInventory = arrValues[2];
                            if(intInventory <= 0){
                                $('#div_outofstock_'+intVariationId).show(); 
                                blnAllChosen = true;      
                            }else{
                                $('#div_outofstock_'+intVariationId).hide();
                            }
                        }
                    }
                }
                
                $('#'+_strAddButtonId)[0].disabled = blnAllChosen;
                $('#'+_strQtyTxtId)[0].disabled = blnAllChosen;
            });
            
            //Submit send to friend
            $('#btnFriendsSubmit').click(function(){
                var blnValid = true;
                var arrErr = new Array();
                var strFriend,strFriendEmail,strMyEmail,strName,strComments;
                var objValid = $(document).Validation;
                
                strFriend = $('#'+_FriendNameId).attr('value');
                strFriendEmail = $('#'+_FriendEmail).attr('value');
                strMyEmail = $('#'+_MyEmail).attr('value');
                strName = $('#'+_MyName).attr('value');
                strComments = $('#'+_Comments).attr('value');
                
                if(! objValid.IsValidText(strName,true)){
                    arrErr[arrErr.length] = 'Your Name is required.';            
                }
        
                if(! objValid.IsValidEmail(strMyEmail)){
                    arrErr[arrErr.length] = 'Your Email is invalid.';
                }
                
                if(! objValid.IsValidText(strFriend,true)){
                    arrErr[arrErr.length] = 'Please enter your Friend\'s Name';
                }
                
                if(! objValid.IsValidEmail(strFriendEmail,true)){
                    arrErr[arrErr.length] = 'Please enter your Friend\'s Email';
                }
                
                if(! objValid.IsValidText(strComments,true)){
                    arrErr[arrErr.length] = 'Please enter your comments.';
                }
        
                $('#divTellAFriendErrs p').html('');
        
                if(arrErr.length > 0){
                    //display in the error list
                    $('#divTellAFriendErrs p').DataBind({
                        Data:arrErr,
                        BindItem:'{}<br />',
                        ClearContent:true
                    });
                }else{
                    //do ajax stuff
                    Store_ProductDetail.SendEmailToFriend(strFriend,strFriendEmail,strMyEmail,strName,strComments,_Link,function(objAjaxItem){
                        if(objAjaxItem.IsError){
                            $('#divTellAFriendErrs p').DataBind({
                                Data:objAjaxItem.Errors,
                                BindItem:'{}<br />',
                                ClearContent:true
                            });
                        }else{
                            $('#divFriendForm').hide();
                            $('#divTellAFriendThanks').show();
                            $('#'+_FriendNameId).attr('value','');
                            $('#'+_FriendEmail).attr('value','');
                        }
                    });
                }
            });
            //End submit send to friend
            
            
            //Request Information
            $('#btnInquirySubmit').click(function(){
                var strName = $('#txtInquiryName').attr('value');
                var strEmail = $('#txtInquiryEmail').attr('value');
                var strComments = $('#txtAreaInquiryComments').attr('value');
                var objValid = $(document).Validation;
                var arrErr = new Array();
                
                if(! objValid.IsValidText(strName,true)){
                    arrErr[arrErr.length] = 'Please enter your name.';
                }
                
                if(! objValid.IsValidEmail(strEmail)){
                    arrErr[arrErr.length] = 'Invalid email address.';
                }
                
                if(! objValid.IsValidText(strComments,true)){
                    arrErr[arrErr.length] = 'Please enter your comments.';
                }
                
                $('#divInquiryErr p').html('');
                
                if(arrErr.length > 0){
                    $('#divInquiryErr p').DataBind({
                        Data:arrErr,
                        BindItem:'{}<br />',
                        ClearContent:true
                    });
                }else{
                    Store_ProductDetail.SaveInquiryFields(_intProductid,_intSiteId,strName,strEmail,strComments,function(objAjaxItem){
                        if(objAjaxItem.IsError){
                            $('#divInquiryErr p').DataBind({
                                Data:objAjaxItem.Errors,
                                BindItem:'{}<br />',
                                ClearContent:true
                            });
                        }else{
                            $('#divInquiryConfirmation').show();
                            $('#divInquiryForm').hide();
                        }
                    });
                }
            });
            //End Request Information
        });