﻿function validate() {
try {   
    $(".options:visible").each(function(i,item) {
        $(".qrholder").fadeOut(500, function() {
            $("#result").html("");
            $(".options .customStyleSelectBox").remove();
            $(".options .customStyleSelectBoxInner").remove();
            $(".page .home").animate({ marginTop: "285px" }, 500);
        });
    });
            

    var data = $("#qrData").val();
    if(data.length>0) {
        var urlregex = /[-a-zA-Z0-9:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9:%_\+.~#?&//=]*)?/;
        var telregex = /^([+(\d]{1})(([\d+() -.]){5,16})([+(\d]{1})/;
        var emlregex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if (emlregex.test(data)) {
            $("#qrType").val("email");
        } else if (urlregex.test(data)) {
            $("#qrType").val("url");
        } else if (telregex.test(data)) {
            $("#qrType").val("phone");
        } else {
            $("#qrType").val("text");
        }
        $("#typeInfo").html($("#qrType option:selected").text());
        $(".options .customStyleSelectBoxInner").html($("#typeInfo").html());
        $("#typeInfo").show();
        $("#type-holder").show();
    }
}
catch(err) {}
}

function generateQR() {
try {
    if ($("#qrData").val().length > 0) {
        $(".qrholder").fadeOut(500, function() {
            $("#result").html("");
            $(".options .customStyleSelectBox").remove();
            $(".options .customStyleSelectBoxInner").remove();
        });

        var qr = {
            qrType: $("#qrType").val(),
            qrData: $("#qrData").val(),
            qrImgURL: "",
            qrSize: "310x310"
        };

        $.ajax({
            type: "POST",
            url: "/Home/CheckQR",
            data: JSON.stringify(qr),
            datatype: "JSON",
            contentType: "application/json; charset=utf-8",
            success: function (returndata) {
                if (returndata != null) {
                    var img = document.createElement('img');
                    img.src = returndata.qrImgURL;
                    
                    $(".page .home").animate({ marginTop: "100px" }, 500, function () {
                        $("#typeInfo").fadeOut(500, function() {
                            $("#result").html(img);
                            $(".qrholder").fadeIn(500);
                            $("#qrType").customStyle();
                        });
                        
                    });                                        

                    
                } else {
                    $("#result").html("Error");
                }
            }
        });
    } else {
        $("#typeInfo").html("Please enter the data to encode");
    }

    return false;
}
catch(err) { }
}

function saveQR() {
    var img = $("#result img").attr("src");
    var size = $("#qrSize").val();
    location.href = "/Home/SaveQR/?url="+escape(img.replace("chs=310x310", "chs="+size));

    return false;
}

/* Select Customise */
(function ($) {
    $.fn.extend({

        customStyle: function (options) {
            if (!$.browser.msie || ($.browser.msie && $.browser.version > 6)) {
                return this.each(function () {

                    var currentSelected = $(this).find(':selected');
                    $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">' + currentSelected.text() + '</span></span>').css({ position: 'absolute', opacity: 0, fontSize: $(this).next().css('font-size') });
                    var selectBoxSpan = $(this).next();
                    var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) - parseInt(selectBoxSpan.css('padding-right'));
                    var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                    selectBoxSpan.css({ display: 'inline-block' });
                    selectBoxSpanInner.css({ width: selectBoxWidth, display: 'inline-block' });
                    var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
                    $(this).height(selectBoxHeight).change(function () {
                        // selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
                        selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
                        // Thanks to Juarez Filho & PaddyMurphy
                    });

                });
            }
        }
    });
})(jQuery);
/* Select Customise */



