﻿$(document).ready(function() {
    DrawCaptcha();

    $("#captchaBox").click(function() {
        DrawCaptcha();
    });

    $("#ctl00_ContentPlaceHolder1_btnSubmit").click(function() {
        if (!ValidCaptcha()) return false;        
    });
});


var captchaValue = '';

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1) + ' ';
    }
    return randomstring;
}


function DrawCaptcha() {
    var a = Math.ceil(Math.random() * 10) + '';
    var b = Math.ceil(Math.random() * 10) + '';
    var c = Math.ceil(Math.random() * 10) + '';
    var d = Math.ceil(Math.random() * 10) + '';
    var e = Math.ceil(Math.random() * 10) + '';
    var f = Math.ceil(Math.random() * 10) + '';
    var g = Math.ceil(Math.random() * 10) + '';
    //var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g;
    var code = randomString();


    $('#captchaCode').html(code);
    $('#captchaBox').css("background-image", "url('../../images/captcaha_bg.jpg')");
    $('#captchaBox').css("background-repeat", "no-repeat");
    $('#captchaBox').css("cursor", "pointer");
    $('#captchaBox').attr("title", "Click to generate new code");
    $('#captchaBox').css("width", "132px");
    $('#captchaBox').css("height", "46px");
    $('#captchaBox').css("border", "1px solid #000000");
    $('#captchaCode').addClass("captcha_font");
    captchaValue = code;

    var default_txt = "Enter Verification Code";

    $('#txtCaptchaCode').css("width", "130px");
    if (jQuery.trim($('#txtCaptchaCode').val()) === "") {
        $('#txtCaptchaCode').val(default_txt);
        $('#txtCaptchaCode').addClass("priSearchText");
    }

    if (jQuery.trim($('#txtCaptchaCode').val()) === default_txt) {
        $('#txtCaptchaCode').addClass("priSearchText");
    }



    $('#txtCaptchaCode').focus(function() {
        if (jQuery.trim($('#txtCaptchaCode').val()) === default_txt) {
            $(this).val('');
        }
    });

    $('#txtCaptchaCode').blur(function() {
        if (jQuery.trim($('#txtCaptchaCode').val()) === "") {
            $(this).val(default_txt);
            $(this).removeClass("secSearchText");
            $(this).addClass("priSearchText"); ;
        }
    });


    $('#txtCaptchaCode').keypress(function() {
        $(this).removeClass("priSearchText");
        $(this).addClass("secSearchText");
    });
}

function ValidCaptcha() {
    $('#captcha_error').text("");
    var str1 = removeSpaces(captchaValue);
    var str2 = removeSpaces($('#txtCaptchaCode').val());

    if (str1.toLowerCase() == str2.toLowerCase()) {
        $('#captcha_error').text("");
        $('#txtCaptchaCode').val('');
        return true;
    }
    else {
        $('#captcha_error').text("Invalid Verification Code. Click the code area to generate a new Code.");

    }
    return false;
}

function removeSpaces(string) {
    return string.split(' ').join('');
}
