﻿var EmailCheck = Class.create({
    animationclass: 'ajaxsaving',
    EmailURL: 'AjaxFunction.aspx',
    initialize: function(Container, Element) {
        this.Container = $(Container);
        this.Element = $(Element);

        this.Changed = false;
        Event.observe(this.Element, 'blur', this.fnBlur.bindAsEventListener(this), false);
        Event.observe(this.Element, 'keypress', this.fnCheckSearchKeyPress.bindAsEventListener(this), false);
        Event.observe($('password'), 'keyup', this.fnpasswordChanged.bindAsEventListener(this), false);        
    },
    fnValidateEmail: function(email) {
        //var email = document.getElementById(’emailaddress’);
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email)) {
        //alert('Please provide a valid email address');
        //email.focus
        return false;
        }
        return true;
    },
    fnCheckSearchKeyPress: function(e) {
        this.Changed = false;
        var code = e.keyCode;
        if (code == 13) {
            Event.stop(e);
        }
    },
    fnBlur: function(e) {
        if (this.fnValidateEmail($(this.Element).value)) {

            if (!this.Changed && $(this.Element).value.length > 8) {


                this.EmailLength = $(this.Element).value.length;
                this.Changed = true;
                this.fnLookUpEmail();
            }
        } else {
            //$(this.Element).focus;
        $(this.Container).innerHTML = 'Please enter a valid email address.';
        this.fnRemoveMessage.bind(this).delay(5);  
        }
    },
    fnLookUpEmail: function() {
        this.PleaseWait(this.Container, 'Searching...');
        //this.fnRemoveDelay.bind(this).delay(3);
        var params = { action: 'EMAIL', email: $(this.Element).value };
        var ajax = new Ajax.Request(this.EmailURL,
			{ onSuccess: function(request) {
			    this.StopWaiting();
			    var dataSet = request.responseText.evalJSON(false);
			    //alert(dataSet.EmailExists)
			    if (dataSet.EmailExists == '1') {
			        //$(this.Container).innerHTML = ' Pick a New Email Address.';
			        this.PleaseWait(this.Container, 'Pick a New Email Address, this one exists already.');
			    }
			    else {
			        this.PleaseWait(this.Container, 'Available.');
			        //$(this.Container).innerHTML = 'Available';
			    }

			    this.fnRemoveMessage.bind(this).delay(5);
			} .bind(this)
			, parameters: params
			});
    },
    PleaseWait: function(ContainerDivId, waitingtext) {
        var template_saving = '<span id="#{saving_wait}" class="#{saving_class}" style="display: none;">#{saving_text}</span>';
        var saving = new Template(template_saving);
        saving = saving.evaluate({
            saving_wait: 'saving_wait',
            saving_class: this.animationclass,
            saving_text: waitingtext
        });
        $(this.Container).innerHTML = "";
        $(this.Container).insert({ top: saving });
        $('saving_wait').show();
    },
    StopWaiting: function() {
        $('saving_wait').remove();
    },
    fnRemoveDelay: function() {
        $('saving_wait').remove();
    },
    fnRemoveMessage: function() {
        $(this.Container).innerHTML = '';
    },
    replaceHtml: function(el, html) {
        //http://blog.stevenlevithan.com/archives/faster-than-innerhtml
        var oldEl = typeof el === "string" ? document.getElementById(el) : el;
        /*@cc_on // Pure innerHTML is slightly faster in IE
        oldEl.innerHTML = html;
        return oldEl;
        @*/
        var newEl = oldEl.cloneNode(false);
        newEl.innerHTML = html;
        oldEl.parentNode.replaceChild(newEl, oldEl);
        /* Since we just removed the old element from the DOM, return a reference
        to the new element, which can be used to restore variable references. */
        return newEl;
    },
    fnpasswordChanged:function() {
        var strength = document.getElementById('strength');
        var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
        var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
        var enoughRegex = new RegExp("(?=.{6,}).*", "g");
        var pwd = document.getElementById("password");
        if (pwd.value.length==0) {
        strength.innerHTML = 'Type Password';
        } else if (false == enoughRegex.test(pwd.value)) {
        strength.innerHTML = 'More Characters';
        } else if (strongRegex.test(pwd.value)) {
        strength.innerHTML = '<span style="color:green">Strong!</span>';
        } else if (mediumRegex.test(pwd.value)) {
        strength.innerHTML = '<span style="color:orange">Medium!</span>';
        } else {
        strength.innerHTML = '<span style="color:red">Weak!</span>';
        }
    }
});


