var regEmailExp = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
var regPhoneExp = /^0[1-9]{1}[0-9]{8}$/;
var elems = null;

$(document).ready(function () {
    var pwBoxes = $("input[placeholder]:password");
    for (var i = 0; i < pwBoxes.length; i++) {
        var actualID = $(pwBoxes[i]).attr('id');
        $("<input type='text' placeholder='" + $(pwBoxes[i]).attr('placeholder') + "' id='" + actualID + "2' title='" + actualID + "' rel='noval' alt='pwReplace' style='" + $(pwBoxes[i]).attr("style") + "' class='" + $(pwBoxes[i]).attr("class") + "' />").insertAfter(pwBoxes[i]);

        $(pwBoxes[i]).blur(function () {
            $("#" + $(this).attr('id') + '2').addClass('validationfailed');
            if ($(this).val() == '') {
                $(this).hide();
                $("#" + $(this).attr('id') + '2').show();
            }
        });
        if ($(pwBoxes[i]).val() == '')
            $(pwBoxes[i]).hide();
        else
            $("#" + $(pwBoxes[i]).attr('id') + '2').hide();
    }
    var rpBoxes = $("input[alt=pwReplace]");
    for (var i = 0; i < rpBoxes.length; i++) {
        $(rpBoxes[i]).focus(function () {
            $("#" + $(this).attr('title')).show();
            $("#" + $(this).attr('title')).focus();
            $(this).hide();
        });

        //Placeholder code
        $("input[placeholder]:text").each(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
        })
        $("input[placeholder]:text").focusin(function () {
            if ($(this).val() == $(this).attr("placeholder")) {
                $(this).val("");
            }
        });
        $("input[placeholder]:text").focusout(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
        });

        $("textarea[placeholder]").each(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
        })
        $("textarea[placeholder]").focusin(function () {
            if ($(this).val() == $(this).attr("placeholder")) {
                $(this).val("");
            }
        });
        $("textarea[placeholder]").focusout(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
        });


    }
    //End placeholder code
    //Validation code
    elems = $('#validationform').children('input[type=text], input[type=password],textarea').not('[rel="noval"]');

    for (var i = 0; i < elems.length; i++) {
        var obj = $(elems[i]);
        obj.blur(function () {
            var obj = $(this);
            if (obj.val() == obj.attr('placeholder') || clearSpaces(obj.val()) == '')
                obj.addClass('validationfailed');
            else
                obj.removeClass('validationfailed');

            if (obj.attr('rel') == 'emailval') {
                if (clearSpaces(obj.val()).search(regEmailExp) == -1)
                    obj.addClass('validationfailed');
            }

            if (obj.attr('rel') == 'contactval') {
                var regPhoneExp = /^0[1-9]{1}[0-9]{8}$/;
                if (clearSpaces(obj.val()).search(regPhoneExp) == -1)
                    obj.addClass('validationfailed');
            }
        });
    }
    //End validations
});

function ValidateSubmit(formid) {
    var tempelems = elems;
    if (formid != null)
        elems = $('#' + formid).find('input[type=text], input[type=password],textarea').not('[rel="noval"]');

    var passed = true;
    for (var i = 0; i < elems.length; i++) {
        var obj = $(elems[i]);

        if (obj.val() == obj.attr('placeholder') || clearSpaces(obj.val()) == '') {
            obj.addClass('validationfailed');
            passed = false;
        }
        else
            obj.removeClass('validationfailed');


        if (obj.attr('rel') == 'emailval') {
            if (clearSpaces(obj.val()).search(regEmailExp) == -1) {
                obj.addClass('validationfailed');
                passed = false;
            }
            else
                obj.removeClass('validationfailed');
        }

        if (obj.attr('rel') == 'contactval') {
            if (clearSpaces(obj.val()).search(regPhoneExp) == -1) {
                obj.addClass('validationfailed');
                passed = false;
            }
            else
                obj.removeClass('validationfailed');
        }
    }
    elems = tempelems;
    tempelems = null;
    return passed;
}

function clearSpaces(s) {
    return s.split(' ').join('');
}
