﻿
function validateRequired(parent, addMessage) {
    var result = true;

    $(parent + " .required").each(function () {
        var cleanedValue = trim($(this).val());

        if (cleanedValue == $(this).attr("default"))
            cleanedValue = "";

        if (cleanedValue.length == 0) {
            $(this).parent().addClass("input-error");

            if (addMessage)
                $(this).siblings("span:first").text("Required");

            result = false;
        }
        else {
            $(this).parent().removeClass("input-error");

            if (addMessage)
                $(this).siblings("span:first").text("");
        }
    });
    
    return result;
}

function validateEmail(parent, addMessage) {
    var result = true;
    
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    
    $(parent + " .email").each(function() {
        var address = $(this).val();
        
        if(reg.test(address) == false) {
            $(this).parent().addClass("input-error");
            
            if (addMessage)
                $(this).siblings("span:first").text("Invalid");
            
            result = false;
        }
        else {
            $(this).parent().removeClass("input-error");
            
            if (addMessage)
                $(this).siblings("span:first").text("");
        }
    });
    
    return result;
}

function handleDefaults() {
    $(".default").each(function() {
        var defaultValue = trim($(this).attr("default"));
        $(this).addClass("input-default-text");
        $(this).val(defaultValue);
        
        $(this).focus(function() {
            $(this).removeClass("input-default-text");
            if (trim($(this).val()) == defaultValue) {
                $(this).val("");
            }
        });
        
        $(this).blur(function() {
            if ($(this).val().length == 0) {
                $(this).val(defaultValue);
                $(this).addClass("input-default-text");
            }
        });
    });
    
    
    /*$('#search input').keyup(function(e) {
        if(e.keyCode == 13) {
           YukonoDoSearch();
        }
    });
    */
}
