jQuery(document).ready(function() {

    function validCreateEmail(obj, f)
    {
        if(f == true)
        {
            jQuery('#formCreate #email2').val("");
        }
        var val = jQuery(obj).val();
        if(val == "" || isEmail(val) == false)
        {
            jQuery(obj).css('border', '2px solid red');
            jQuery(obj).parent('td').children('span').css('color', 'red');
            jQuery(obj).parent('td').children('.checkmark').hide();
            return false;
        }
        else
        {
            jQuery(obj).css('border', '1px solid #BBBBBB');
            jQuery(obj).parent('td').children('span').css('color', '#3A3838');
            jQuery(obj).parent('td').children('.checkmark').show();

            return true;
        }
    }

    function validCreateEmail2(obj)
    {
        var first_email = jQuery('#formCreate #email').val();
        var email = jQuery(obj).val();
        validCreateEmail(obj);
        if(first_email != "")
        {
            if(first_email != email)
            {
                jQuery(obj).css('border', '2px solid red');
                jQuery(obj).parent('td').children('span').css('color', 'red');
                jQuery(obj).parent('td').children('.checkmark').hide();

                jQuery('#formCreate #email').css('border', '2px solid red');
                jQuery('#formCreate #email').parent('td').children('span').css('color', 'red');
                jQuery('#formCreate #email').parent('td').children('.checkmark').hide();

                return false;
            }
            else
            {
                jQuery(obj).css('border', '1px solid #BBBBBB');
                jQuery(obj).parent('td').children('span').css('color', '#3A3838');
                jQuery(obj).parent('td').children('.checkmark').show();

                jQuery('#formCreate #email').css('border', '1px solid #BBBBBB');
                jQuery('#formCreate #email').parent('td').children('span').css('color', '#3A3838');
                jQuery('#formCreate #email').parent('td').children('.checkmark').show();
                return true;
            }
        }
    }

    function validCreateIsEmpty(obj)
    {
        var val = jQuery(obj).val();
        if(val == "")
        {
            jQuery(obj).css('border', '2px solid red');
            jQuery(obj).parent('td').children('span').css('color', 'red');
            jQuery(obj).parent('td').children('.checkmark').hide();
            return false;
        }
        else
        {
            jQuery(obj).css('border', '1px solid #BBBBBB');
            jQuery(obj).parent('td').children('span').css('color', '#3A3838');
            jQuery(obj).parent('td').children('.checkmark').show();
            return true;
        }
    }

    function validPassword(pass1, pass2)
    {
        if(pass1 == "" || pass2 == "" || pass1 != pass2)
        {
            jQuery('#formCreate #pas_1').css('border', '2px solid red');
            jQuery('#formCreate #pas_1').parent('td').children('span').css('color', 'red');

            jQuery('#formCreate #pas_2').css('border', '2px solid red');
            jQuery('#formCreate #pas_2').parent('td').children('span').css('color', 'red');
            jQuery('#formCreate #pas_2').parent('td').children('.checkmark').hide();
            return false;
        }
        else
        {
            jQuery('#formCreate #pas_1').css('border', '1px solid #BBBBBB');
            jQuery('#formCreate #pas_1').parent('td').children('span').css('color', '#3A3838');

            jQuery('#formCreate #pas_2').css('border', '1px solid #BBBBBB');
            jQuery('#formCreate #pas_2').parent('td').children('span').css('color', '#3A3838');
            jQuery('#formCreate #pas_2').parent('td').children('.checkmark').show();
            return true;
        }
    }

    function validCreatePassword()
    {
        var pass1 = jQuery('#formCreate #pas_1').val();
        var pass2 = jQuery('#formCreate #pas_2').val();
        validPassword(pass1, pass2);
    }

    function validCreatePasswordFirst()
    {
        var pass1 = jQuery('#formCreate #pas_1').val();
        var pass2 = jQuery('#formCreate #pas_2').val();
        if(pass2 != "")
        {
            validPassword(pass1, pass2);
        }
    }

    function validCreatePostCode()
    {
        obj = jQuery('#formCreate #code');
        var code = jQuery(obj).val();
        if(code != "" && isPostcode(code))
        {
            jQuery(obj).css('border', '1px solid #BBBBBB');
            jQuery(obj).parent('td').children('span').css('color', '#3A3838');
            jQuery(obj).parent('td').children('.checkmark').show();
            return true;
        }
        else
        {
            jQuery(obj).css('border', '2px solid red');
            jQuery(obj).parent('td').children('span').css('color', 'red');
            jQuery(obj).parent('td').children('.checkmark').hide();
            return false;
        }
        
    }
    //validCreateTwoEmails(this)
    jQuery('#formCreate #email').blur(function () {validCreateEmail(this, true);})
    jQuery('#formCreate #email2').blur(function () {validCreateEmail2(this);})
    jQuery('#formCreate #name').blur(function () {validCreateIsEmpty(this);})
    jQuery('#formCreate #street').blur(function () {validCreateIsEmpty(this);})
    jQuery('#formCreate #code').blur(function () {validCreatePostCode();})
    jQuery('#formCreate #city').blur(function () {validCreateIsEmpty(this);})

    jQuery('#formCreate #pas_2').blur(function () {validCreatePassword();})
    jQuery('#formCreate #pas_1').blur(function () {validCreatePasswordFirst();})

    jQuery('#formCreate #email2').live("paste", function(e) {
        jQuery('#formCreate #email2').val("");
        return false;
    });


    function isEmail(string) {
        if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
            return true;
        else
            return false;
    }

    function isPostcode(p) {
        return new RegExp(/^\d{2}-\d{3}$/).exec(p);
    }

    function checkCtrlIns(field)
    {
//        alert(window.event.ctrlKey);
        if (window.event.ctrlKey){
            if (window.event.keyCode == 86) {
               return false
            }
            return true
        }
        else
        {
            return true
        }
    }

});
