function make_uuid() {
    var uuid = "";
    for (var i = 0; i < 32; i++) {
        uuid += Math.floor(Math.random() * 16).toString(16)
    }
    return uuid;
}
function updateCaptcha(){
    var params = updateCaptcha.arguments;
    var paramsLength = updateCaptcha.arguments.length;
    var num = '';
    var extra = '';
    var width = '';
    var height = '';

    if(paramsLength >0){
        var num = (params[0] != '') ? params[0] : '';
        var extra = (params[1] != '') ? params[1] : '';
        var width = (params[2] != '') ? params[2] : '';
        var height = (params[4] != '') ? params[3] : '';
    }

    if ($('#captchaCode'+num).length) {
        $('#captchaCode'+num).attr('src', '');

        if(width == ''){
            $('#captchaCode'+num).css('width', '200px');
        }else{
            $('#captchaCode'+num).css('width', width+'px');
        }

        if(height == ''){
            $('#captchaCode'+num).css('height', '80px');
        }else{
            $('#captchaCode'+num).css('height', height+'px');
        }
    }

    if(num == ''){
        $('#captchaCode').attr('src', '/captcha.php?'+Math.random()+extra+'&width='+width+'&height='+height);
    }else{
        $('#captchaCode'+num).attr('src', '/captcha.php?'+Math.random()+'&num='+num+extra+'&width='+width+'&height='+height);
    }
}

/*
 *dialog title
 *msg to display
 *Function name to call
 *list of parameters in order the funciton call accept them
 **/
var $dialog;
function captchaDialog(){

    var params = captchaDialog.arguments;
    var paramsLength = captchaDialog.arguments.length;

    var functionCall;
    var functionCallParams = '';
    var uuid ;

    if (params['0'] == ''){
        captchaDialogTitle = 'Tnaflix Dialog Box';
    } else {
        captchaDialogTitle = params['0'];
    }

    if (params['1'] == ''){
        captchaDialogMsg = '&nbsp;';
    } else {
        captchaDialogMsg = params['1'];
    }

    if (captchaDialogMsg != '&nbsp;') {
        captchaDialogMsg = captchaDialogMsg + '<br><br>';
    }


    if(paramsLength > 0){
         $dialog = $('<div></div>')
                .dialog({
                autoOpen: false,
                title: captchaDialogTitle,
                width: 400,
                draggable: false,
                zIndex: 99999,
                modal: true,
                open: function(event, ui) { uuid = make_uuid(); },
                buttons: {
                        "Ok": function() {
                            captchaValCode = $('#captchaUserInputCode_'+uuid).val();
                            if(captchaValCode != '' ) {

                                for(i=4;i<paramsLength;i++){
                                   functionCallParams = functionCallParams + '"' +params[i]+'",';
                                }

                                functionCall = params[3]+'('+functionCallParams+'"'+captchaValCode+'",'+'"'+uuid+'");';
                                //alert(functionCall);
                                eval(functionCall);
                                $(this).dialog("close");
                            } else {
                                alert('Type the code in the image and click ok!');
                            }
                        },
                        "Cancel": function() {
                                $.ajax({
                                    type: "GET",
                                    url: '/ajax/captchaRemoved.php',
                                    data: ({
                                    uuid: uuid,
                                    action: 'delete'
                                }),
                                success : silentResponse
                                });
                                $(this).dialog("close");
                        }
                }
        });

        $dialog.dialog('open');
        $dialog.dialog("option","position",'top');

        if(params[3] != 'sndComment'){
            $dialog.html('<div style="margin-left: 5%; margin-right: 5%;"><div style="text-align:left">'+captchaDialogMsg+'<div style="font-size: .9em; font-style: italic;" id="captchaDialogBody">'+params[2]+'</div><br><br>Plese enter the code:</div><div style="float:left; margin-right: 10px;"><img id="captchaCode" src="/captcha.php?uuid='+uuid+'" class="captcha" width="115" height="45" /></div><div style="float:left; text-align:left;"><br><input type="text" name="captchaUserInputCode" id="captchaUserInputCode_'+uuid+'" autocomplete="off" /></div></div>');
        } else {
            $dialog.html('<div style="margin-left: 5%; margin-right: 5%;"><div style="text-align:left">'+captchaDialogMsg+'<div style="font-size: .9em; font-style: italic;" id="captchaDialogBody"></div><br><br>Plese enter the code:</div><div style="float:left; margin-right: 10px;"><img id="captchaCode" src="/captcha.php?uuid='+uuid+'" class="captcha" width="115" height="45" /></div><div style="float:left; text-align:left;"><br><input type="text" name="captchaUserInputCode" id="captchaUserInputCode_'+uuid+'" autocomplete="off" /></div></div>');
        }
    } else {
        alert('Invalid Options for Captcha');
    }
}


function silentResponse(xhr) {
    if(xhr != ""){}
}
