/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
(function($){
    voteup = function($ideaid) {
    {
        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmIdeaVote.php',
            async: true,
            data:
            {
                vote: $ideaid,
                votevalue: 1

            },
            success: function(data)
            {
                var $returned = jQuery.parseJSON(data);
                if($returned.resultvalue == '1')
                {
                    $('#ui-dialog-ideavote-status').html($returned.message);
                    $('#score_'+$ideaid).html($returned.score);
                    $('#votes_'+$ideaid).html($returned.votes);

                    $('#voteup_'+$ideaid).effect('pulsate', {
                        times:3
                    }, 500);

                    $('#idea'+$ideaid).css('border-color','#999');
                }
                else
                {
                    $('#ui-dialog-ideavote-status').html($returned.message);
                    $('#ui-dialog-ideavote-status').dialog(
                    {
                        modal: true,
                        show: 'scale',
                        hide: 'clip',
                        cache: false,
                        title: 'Vote!',
                        closeOnEscape: false,
                        buttons:
                        {
                            Ok: function()
                            {
                                $( this ).dialog('close');
                                $(this).dialog('destroy');
                            }
                        }
                    });
                }
                
            },
            error: function()
            {
                $('#ui-dialog-ideavote-status').html('Error 2.');
                $('#ui-dialog-ideavote-status').dialog(
                {
                    modal: true,
                    show: 'scale',
                    hide: 'clip',
                    cache: false,
                    title: 'Vote!',
                    closeOnEscape: false,
                    buttons:
                    {
                        Ok: function()
                        {
                            $( this ).dialog('close');
                            $(this).dialog('destroy');
                        }
                    }
                });
            }
        });
                
        
        return false;
    }
    };
})(jQuery);

(function($){
    votedown = function($ideaid) {
    {
        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmIdeaVote.php',
            async: true,
            data:
            {
                vote: $ideaid,
                votevalue: 0

            },
            success: function(data)
            {
                var $returned = jQuery.parseJSON(data);
                if($returned.resultvalue == '1')
                {
                    $('#ui-dialog-ideavote-status').html($returned.message);
                    $('#score_'+$ideaid).html($returned.score);
                    $('#votes_'+$ideaid).html($returned.votes);

                    $('#votedown_'+$ideaid).effect('pulsate', {
                        times:3
                    }, 500);

                    $('#idea'+$ideaid).css('border-color','#999');

                }
                else
                {
                    $('#ui-dialog-ideavote-status').html($returned.message);
                    $('#ui-dialog-ideavote-status').dialog(
                    {
                        modal: true,
                        show: 'scale',
                        hide: 'clip',
                        cache: false,
                        closeOnEscape: false,
                        buttons:
                        {
                            Ok: function()
                            {
                                $( this ).dialog('close');
                            }
                        }
                    });
                }
            },
            error: function()
            {
                $('#ui-dialog-ideavote-status').html('Error 2.');
                $('#ui-dialog-ideavote-status').dialog(
                {
                    modal: true,
                    show: 'scale',
                    hide: 'clip',
                    cache: false,
                    closeOnEscape: false,
                    buttons:
                    {
                        Ok: function()
                        {
                            $( this ).dialog('close');
                        }
                    }
                });
            }
        });

        
        return false;
    }
    };
})(jQuery);

/*
 * comments
 */

(function($){
    votecomup = function($comid) {
    {
        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmCommentVote.php',
            async: true,
            data:
            {
                vote: $comid,
                votevalue: 1
            },
            success: function(data)
            {
                var $returnedcom = jQuery.parseJSON(data);
                if($returnedcom.resultvalue == '1')
                {
                    $('#ivotecomupvalue_'+$comid).html($returnedcom.votesup);
                    $('#ivotecomdownvalue_'+$comid).html($returnedcom.votesdown);

                    $('#ivotecomupvalue_'+$comid).effect('pulsate', {
                        times:3
                    }, 500);
                    $('#ivotecomdownvalue_'+$comid).effect('pulsate', {
                        times:3
                    }, 500);

                }
                else
                {
                    $('#ui-dialog-ideacomvote-status').html('You have to login');
                    $('#ui-dialog-ideacomvote-status').dialog(
                    {
                        modal: true,
                        show: 'scale',
                        hide: 'clip',
                        cache: false,
                        closeOnEscape: false,
                        buttons:
                        {
                            Ok: function()
                            {
                                $( this ).dialog('close');
                            }
                        }
                    });
                }

            },
            error: function()
            {
                $('#ui-dialog-ideacomvote-status').html('Error 2633');
            }
        });
        return false;
    }
    };
})(jQuery);

(function($){
    votecomdown = function($comid) {
    {
        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmCommentVote.php',
            async: true,
            data:
            {
                vote: $comid,
                votevalue: 0
            },
            success: function(data)
            {
                var $returnedcom = jQuery.parseJSON(data);
                if($returnedcom.resultvalue == '1')
                {
                    $('#ivotecomupvalue_'+$comid).html($returnedcom.votesup);
                    $('#ivotecomdownvalue_'+$comid).html($returnedcom.votesdown);

                    $('#ivotecomupvalue_'+$comid).effect('pulsate', {
                        times:3
                    }, 500);
                    $('#ivotecomdownvalue_'+$comid).effect('pulsate', {
                        times:3
                    }, 500);
                }
                else
                {
                    $('#ui-dialog-ideacomvote-status').html('You have to login.');
                    $('#ui-dialog-ideacomvote-status').dialog(
                    {
                        modal: true,
                        show: 'scale',
                        hide: 'clip',
                        cache: false,
                        closeOnEscape: false,
                        buttons:
                        {
                            Ok: function()
                            {
                                $( this ).dialog('close');
                            }
                        }
                    });
                }
            },
            error: function()
            {
                $('#ui-dialog-ideacomvote-status').html('Error 2356');
                
            }
        });
        return false;
    }
    };
})(jQuery);

(function($){
    editidea = function($ideaid) {
    {
        $('#ui-dialog-ideaedit').html("Loading...");

        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmIdeaEdit.php',
            async: true,
            data:
            {
                ideaid: $ideaid
            },
            success: function(data)
            {
                $('#ui-dialog-ideaedit').html(data);
                
            },
            error: function()
            {
                $('#ui-dialog-ideaedit').html('Error 2.');
            }
        });

        $('#ui-dialog-ideaedit').dialog(
        {
            modal: true,
            cache: false,
            height: 450,
            width: 700,
            closeOnEscape: true,
            title: 'Update',
            buttons:
            {
                Cancel: function()
                {
                    $( this ).dialog('close');
                },
                Update:function()
                {
                    validateFrm('#IdeaUpdateForm');
                    var form_data = $('#IdeaUpdateForm').serialize().replace('%5B%5D', '[]');
                    $.ajax(
                    {
                        type: 'POST',
                        url: 'http://localhost/toadext/FrmIdeaEdit.php',
                        async: false,
                        data: form_data,
                        success: function(data)
                        {
                            if(data != '1')
                            {
                                $('#ui-dialog-edit-idea-status').html(data);
                                $('#ui-dialog-edit-idea-status').dialog(
                                {
                                    height: 150,
                                    width: 200,
                                    show: 'scale',
                                    title:'Update result',
                                    buttons:{
                                        OK:function()
                                        {
                                            $(this).dialog('close');
                                        }
                                    }
                                });
                            }
                            $('#idea'+$ideaid).load('http://localhost/toadext/FrmIdeaSummary.php?ideaid='+$ideaid+'&lnk=1');
                            $('#idea'+$ideaid).effect('pulsate', {
                                times:3
                            }, 500);
                            
                        },
                        error: function()
                        {
                            alert('error');
                            $('#ui-dialog-edit-idea-status').html('Error loading data.');
                            $('#ui-dialog-edit-idea-status').dialog(
                            {
                                height: 150,
                                width: 200,
                                show: 'scale',
                                title:'Error message',
                                buttons:
                                {
                                    OK:function()
                                    {
                                        $(this).dialog('close');
                                    }
                                }
                            });
                        }
                    });
                    $( this ).dialog('close');
                }
            }
        })
    }
    return false;
    }
}
)(jQuery);

(function($){
    delidea = function($ideaid,$navprodid) {
    {
        $('#ui-dialog-ideadel').html('Do you wish to delete this idea?')
        $('#ui-dialog-ideadel').dialog(
        {
            modal: true,
            cache: false,
            height: 150,
            width: 200,
            closeOnEscape: true,
            title: 'Delete',
            buttons:
            {
                Cancel: function()
                {
                    $( this ).dialog('close');
                },
                Delete:function()
                {
                    $.ajax(
                    {
                        type: 'POST',
                        url: 'http://localhost/toadext/FrmIdeaDel.php',
                        async: false,
                        data: {
                            id: $ideaid,
                            prodid: $navprodid
                        },
                        success: function(data)
                        {
                            if(data != '1')
                            {
                                $('#ui-dialog-edit-idea-status').html(data);
                                $('#ui-dialog-edit-idea-status').dialog(
                                {
                                    height: 150,
                                    width: 200,
                                    show: 'scale',
                                    title:'Result',
                                    buttons:{
                                        OK:function()
                                        {
                                            $(this).dialog('close');
                                        }
                                    }
                                });
                            }
                            else
                            {
                                $('#idea'+$ideaid).effect('pulsate', {
                                    times:1
                                }, 500);
                                $('#idea'+$ideaid).hide(500);
                            }

                        },
                        error: function()
                        {
                            $('#ui-dialog-edit-idea-status').html('Error loading data.');
                            $('#ui-dialog-edit-idea-status').dialog(
                            {
                                height: 150,
                                width: 200,
                                show: 'scale',
                                title:'Error message',
                                buttons:
                                {
                                    OK:function()
                                    {
                                        $(this).dialog('close');
                                    }
                                }
                            });
                        }
                    });
                    $( this ).dialog('close');
                }
            }
        })
    }
    return false;
    }
}
)(jQuery);


(function($){
    editkom = function($komid, $prodid) {
    {
        $('#ui-dialog-ideaedit').html("Loading...");

        $.ajax(
        {
            type: 'POST',
            url: 'http://localhost/toadext/FrmKomEdit.php',
            async: true,
            data:
            {
                komid: $komid
            },
            success: function(data)
            {
                $('#ui-dialog-ideaedit').html(data);
                $('#navprod_id').val($prodid);
            },
            error: function()
            {
                $('#ui-dialog-ideaedit').html('Error 2.');
            }
        });
       
        
        $('#ui-dialog-ideaedit').dialog(
        {
            modal: true,
            cache: false,
            show: 'scale',
            hide: 'clip',
            height: 300,
            width: 460,
            closeOnEscape: true,
            title: 'Update comment',
            buttons:
            {
                Cancel: function()
                {
                    $( this ).dialog('close');
                },
                Update:function()
                {
                    validateFrm('#KomUpdateForm');
                    var form_data = $('#KomUpdateForm').serialize().replace('%5B%5D', '[]');
                    $.ajax(
                    {
                        type: 'POST',
                        url: 'http://localhost/toadext/FrmKomEdit.php',
                        async: false,
                        data: form_data,
                        success: function(data)
                        {
                            if(data != '1')
                            {
                                $('#ui-dialog-edit-idea-status').html(data);
                                $('#ui-dialog-edit-idea-status').dialog(
                                {
                                    height: 150,
                                    width: 200,
                                    show: 'scale',
                                    title:'Update result',
                                    buttons:{
                                        OK:function()
                                        {
                                            $(this).dialog('close');
                                        }
                                    }
                                });
                            }
                            $('#kom'+$komid).load('http://localhost/toadext/FrmKomSummary.php?komid='+$komid+'&prodid='+$prodid);
                            $('#kom'+$komid).effect('pulsate', {
                                times:3
                            }, 500);

                        },
                        error: function()
                        {
                            alert('error');
                            $('#ui-dialog-edit-idea-status').html('Error loading data.');
                            $('#ui-dialog-edit-idea-status').dialog(
                            {
                                height: 150,
                                width: 200,
                                show: 'scale',
                                title:'Error message',
                                buttons:
                                {
                                    OK:function()
                                    {
                                        $(this).dialog('close');
                                    }
                                }
                            });
                        }
                    });
                    $( this ).dialog('close');
                    $( this ).dialog('destroy');
                }
            }
        })
    }
    return false;
    }
}
)(jQuery);

(function($){
    delkom = function($komid,$navprodid) {
    {
        $('#ui-dialog-ideadel').html('Do you wish to delete this comment?')
        $('#ui-dialog-ideadel').dialog(
        {
            modal: true,
            cache: false,
            height: 150,
            width: 200,
            closeOnEscape: true,
            title: 'Delete',
            buttons:
            {
                Cancel: function()
                {
                    $( this ).dialog('close');
                },
                Delete:function()
                {
                    $.ajax(
                    {
                        type: 'POST',
                        url: 'http://localhost/toadext/FrmKomDel.php',
                        async: false,
                        data: {
                            komid: $komid,
                            prodid: $navprodid
                        },
                        success: function(data)
                        {
                            if(data != '1')
                            {
                                $('#ui-dialog-edit-idea-status').html(data);
                                $('#ui-dialog-edit-idea-status').dialog(
                                {
                                    height: 150,
                                    width: 200,
                                    show: 'scale',
                                    title:'Result',
                                    buttons:{
                                        OK:function()
                                        {
                                            $(this).dialog('close');
                                        }
                                    }
                                });
                            }
                            else
                            {
                                $('#kom'+$komid).effect('pulsate', {
                                    times:1
                                }, 500);
                                $('#kom'+$komid).hide(500);
                            }

                        },
                        error: function()
                        {
                            alert('Error');
                            $('#ui-dialog-edit-idea-status').html('Error loading data.');
                            $('#ui-dialog-edit-idea-status').dialog(
                            {
                                height: 150,
                                width: 200,
                                show: 'scale',
                                title:'Error message',
                                buttons:
                                {
                                    OK:function()
                                    {
                                        $(this).dialog('close');
                                    }
                                }
                            });
                        }
                    });
                    $( this ).dialog('close');
                }
            }
        })
    }
    return false;
    }
}
)(jQuery);


(function($){
    showLoginForm = function()
    {
        $(document).ready(function()
        {
            if($('#ui-dialog-ideavote-status').is( ":ui-dialog" ) == true)
            {
                $('#ui-dialog-ideavote-status').dialog('close');
                $('#ui-dialog-ideavote-status').dialog('destroy');
            }

            if($('#dialogbox').is( ":ui-dialog" ) == true)
            {
                $('#dialogbox').dialog('close');
                $('#dialogbox').dialog('destroy');
            }

            $('#ui-dialog-login').dialog(
            {
                show: 'scale',
                hide: 'clip',
                height: 200,
                width: 300,
                cache: false,
                closeOnEscape: false,
                title:'Login',
                modal: true,

                buttons:
                {
                    Close: function()
                    {
                        $(this).dialog('close');
                    },
                    Login:function()
                    {
                        var form_data = $('#loginform').serialize();
                        $.ajax(
                        {
                            type: 'POST',
                            url: 'http://localhost/toadext/FrmLogin.php',
                            data: form_data,
                            success: function(data)
                            {
                                if(data == '1')
                                {
                                    document.loginform.submit();
                                }
                                else
                                {
                                    $('#ui-dialog-login-status').html(data);
                                    $('#ui-dialog-login-status').dialog(
                                    {
                                        height: 150,
                                        width: 200,
                                        title:'Error message',
                                        buttons:{
                                            OK:function()
                                            {
                                                $(this).dialog('close')
                                            }
                                        }
                                    });
                                }
                            },
                            error: function()
                            {
                                $('#ui-dialog-login-status').html('Error loading data.');
                            }
                        })
                    }
                },

                overlay:
                {
                    backgroundColor: '#000',
                    opacity: 0.5
                }
            });
            return false;
           
        });
    }
}
)(jQuery);


(function($){
    showRegForm = function(){
        $(document).ready(function()
        {
            if($('#ui-dialog-ideavote-status').is( ":ui-dialog" ) == true)
            {
                $('#ui-dialog-ideavote-status').dialog('close');
                $('#ui-dialog-ideavote-status').dialog('destroy');
            }
            
            if($('#dialogbox').is( ":ui-dialog" ) == true)
            {
                $('#dialogbox').dialog('close');
                $('#dialogbox').dialog('destroy');
            }



            $('#ui-dialog-register').dialog(
            {
                show: 'scale',
                hide: 'clip',
                height: 600,
                width: 400,
                cache: false,
                closeOnEscape: false,
                title:'Register',
                modal: true,
                buttons:
                {
                    Close: function()
                    {
                        $(this).dialog('close');
                    },
                    Register:function()
                    {
                        var form_data = $('#registrationform').serialize();
                        $.ajax(
                        {
                            type: 'POST',
                            url: 'http://localhost/toadext/FrmRegister.php',
                            data: form_data,
                            success: function(data)
                            {
                                if(data == '1')
                                {
                                    document.registrationform.submit();
                                }
                                else
                                {
                                    $('#ui-dialog-reg-status').html(data);
                                    $('#ui-dialog-reg-status').dialog({
                                        height: 150,
                                        width: 200,
                                        title:'Error message',
                                        buttons:
                                        {
                                            OK:function()
                                            {
                                                $(this).dialog('close')
                                            }
                                        }
                                    });
                                }
                            },
                            error: function()
                            {
                                $('#ui-dialog-login-status').html('Error loading data.');
                            }
                        })
                    }
                },
                overlay:
                {
                    backgroundColor: '#000',
                    opacity: 0.5
                }
            });
            return false;
        });
    }
}
)(jQuery);

(function($){
    validateFrm = function($form)
    {
        var $isOk = true;
        
        $($form).find(".req").each(function()
        {
            if ($(this).val().length == 0)
            {
                $(this).addClass('error');
                $isOk = false;
                $(this).effect('pulsate',
                {
                    times:2
                }, 500);
            }
            else
            {
                $(this).removeClass('error');
            }
        });
        if($isOk == false)
        {
            exit();
        }
    }
}
)(jQuery);



