function checkInt(val) {
    var val;
    var intArray = Array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.');
    for (var i = String(val).length - 1; i >= 0; i--) {
        if (!val.substr(i, 1) in intArray)
            return false;
    }
}

function onlyNumber(e) {
    var keyCode = (is.ns) ? e.which : event.keyCode;
    if ((keyCode < 48 || keyCode > 57) && keyCode != 8 && keyCode != 0) {
        return false;
    }
}

function defaultValueAction() {
    $("INPUT[defVal=true]").focus(function () {
        var id = $(this).attr('id');
        $(this).val('');
    });
    $("INPUT[defVal=true]").blur(function () {
        var id = $(this).attr('id');
        if (document.getElementById(id).value == '') {
            document.getElementById(id).value = document.getElementById(id).defaultValue;
        }
    });
}

function findActiveControl(x, y) {
    var x;
    var y;
    return document.elementFromPoint(x, y);
}

function checkDate(date) {
    var date;
    var error = 0;
    var expl = date.split('-');
    var year = expl[0];
    var month = expl[1];
    var ltc = year.substr(2);
    var day = expl[2];
    var years = new Number(year);
    var mnt = new Number(month);

    if (years < 1940 || years > 2010)
        error = 1;

    if (mnt < 0 || mnt > 12)
        error = 1;

    var yC = ltc % 4;
    if (day < 0) error = 1;
    maxDay = 0;
    switch (month) {
        case '01':
            maxDay = 31;
            break;
        case '02':
            if (yC == 0) maxDay = 29; else maxDay = 28;
            break;
        case '03':
            maxDay = 31;
            break;
        case '04':
            maxDay = 30;
            break;
        case '05':
            maxDay = 31;
            break;
        case '06':
            maxDay = 30;
            break;
        case '07':
            maxDay = 31;
            break;
        case '08':
            maxDay = 31;
            break;
        case '09':
            maxDay = 30;
            break;
        case '10':
            maxDay = 31;
            break;
        case '11':
            maxDay = 30;
            break;
        case '12':
            maxDay = 31;
            break;
    }
    var theDay = new Number(day);
    if (theDay > maxDay) error = 1;
    var res = (error == 1 ? false : true);
    return res;
}

function checkAnyForms(theForm) {
    var theForm = document.getElementById(theForm);
    var theSelects = theForm.getElementsByTagName('select');
    var theTextAreas = theForm.getElementsByTagName('textarea');
    var ret = true;
    var $errovDivInner = $("." + theForm.getAttribute('errovDivInner'));
    $errovDivInner.html("");

    /**
     *
     * @author Loft visual & audio arts
     * @author Mehmet Gülbilge
     *
     * (id)_Error = Inputa bağlı hata mesajı gösterilecek div
     *
     * errovDivInner hata mesajlarının içerisine yazılacağı divi belirtir.
     *
     * ***** INPUT *****
     * req = Gerekli olup olmadıgı ıcın
     * errorString = eğer boşsa veya geçersiz değer içeriyorsa çıkacak uyarı
     *
     * ***** INPUT.TEXT *****
     * chBorder = Yanlış veya geçersiz karakterde kırmızı kenarlık
     * minChar = Min. Karakter sayısı
     * isMail = Mail alanı mı?
     * isPass = Parola alanı mı?
     * isDate = Tarih mi?
     * mustMatch = Parola alanlarının eşleşmesi gereken doğrulama alanı
     * isNumber = Float field
     * err = 0-1 hatalı yada hatasız alan
     *
     **/


    for (var i = 0; i <= theTextAreas.length - 1; i++) {
        var theObj = theTextAreas.item(i);
        var errorDiv = theObj.id + '_Error'; // Error DivName Generate
        var objType = theObj.type.toLowerCase();

        if ($('#' + theObj.id).attr('req') == "true") {
            if ((String(theObj.value).length < $('#' + theObj.id).attr('minChar')) || theObj.value == "") {
                if ($('#' + theObj.id).attr('onError') != '') {
                    var onError = $('#' + theObj.id).attr('onError');
                    eval(onError);
                }
                if (theForm.getAttribute('createDivs') == 'true') {
                    $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr("errorString") + '</div>');

                }
                ret = false;
            } else {

                if ($('#' + theObj.id).attr('chBorder') == 'true') {
                    theObj.style.border = '1px solid lime';
                }
                $errovDivInner.append('');
            }
        } else {

            if ($('#' + theObj.id).attr('chBorder') == 'true') {
                theObj.style.border = '1px solid lime';
            }
            $errovDivInner.append('');
        }
    }

    var theInputs = theForm.getElementsByTagName('input');

    for (var i = 0; i <= theInputs.length - 1; i++) {
        var theObj = theInputs.item(i);
        var errorDiv = theObj.id + '_Error'; // Error DivName Generate
        var objType = String(theObj.type).toLowerCase();


        //Text typeler için
        if ((objType == "text" || objType == "password") && $('#' + theObj.id).attr('req') == 'true') {
            if ((String(theObj.value).length < $('#' + theObj.id).attr('minChar')) || theObj.value == "") {

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid red';


                if ($('#' + theObj.id).attr('onError') != '') {
                    var onError = $('#' + theObj.id).attr('onError');
                    eval(onError);
                }

                if (theForm.getAttribute('createDivs') == 'true') {
                     $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                }
                ret = false;
            } else {//End charSize check

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid lime';

                if (document.getElementById(errorDiv) != null)
                $errovDivInner.append('');

                    if ($('#' + theObj.id).attr('isMail') == 'true') {
                        var domain = Array();
                        domain = String(theObj.value).split('@');
                        if ((String(theObj.value).indexOf('@') < 2) || (domain[1].indexOf('.') < 0)) {

                            if ($('#' + theObj.id).attr('chBorder') == 'true')
                                theObj.style.border = '1px solid red';


                            if ($('#' + theObj.id).attr('onError') != '') {
                                var onError = $('#' + theObj.id).attr('onError');
                                eval(onError);
                            }
                            //alert(theForm.getAttribute('createDivs'));
                            if (theForm.getAttribute('createDivs') == 'true') {
                                $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                            }

                            ret = false;
                        } else {
                            if ($('#' + theObj.id).attr('chBorder') == 'true') {
                                theObj.style.border = '1px solid lime';
                            }
                            $errovDivInner.append('');
                        }
                    }// End mail check

                if (objType == "password" && $('#' + theObj.id).attr('isPass') == 'true') {
                    if (document.getElementById($('#' + theObj.id).attr('mustMatch')) != null) {
                        var mMatchValueObject = $("#" + $('#' + theObj.id).attr('mustMatch'));
                        var mMatchValue = mMatchValueObject.val();

                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid red';

                        if (mMatchValue != theObj.value) {

                            if ($('#' + theObj.id).attr('onError') != '') {
                                var onError = $('#' + theObj.id).attr('onError');
                                eval(onError);
                            }

                            if (theForm.getAttribute('createDivs') == 'true') {
                                $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + mMatchValueObject.attr('errorString') + '</div>');
                            }
                            ret = false;
                        } else {
                            if ($('#' + theObj.id).attr('chBorder') == 'true') {
                                theObj.style.border = '1px solid lime';
                            }

                            $errovDivInner.append('');
                        }
                    }
                }//Check if objType password. is match
                //
                if ($('#' + theObj.id).attr('isDate') == 'true') {
                    if (checkDate(theObj.value) == false) {

                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid red';

                        if ($('#' + theObj.id).attr('onError') != '') {
                            var onError = $('#' + theObj.id).attr('onError');
                            eval(onError);
                        }

                        if (theForm.getAttribute('createDivs') == 'true') {
                            $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                        }

                        ret = false;
                    } else if (theObj.style.borderColor != 'red') {
                        if ($('#' + theObj.id).attr('chBorder') == 'true') {
                            theObj.style.border = '1px solid lime';
                        }


                        $errovDivInner.append('');
                    }//Check Date
                }//End Check Date

                //Start Check Number
                if ($('#' + theObj.id).attr('isNumber') == 'true') {
                    if (checkInt(theObj.value) == false) {

                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid red';

                        if ($('#' + theObj.id).attr('onError') != '') {
                            var onError = $('#' + theObj.id).attr('onError');
                            eval(onError);
                        }

                        if (theForm.getAttribute('createDivs') == 'true') {
                            $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                        }

                        ret = false;
                    } else if (theObj.style.borderColor != 'red') {
                        if ($('#' + theObj.id).attr('chBorder') == 'true') {
                            theObj.style.border = '1px solid lime';
                        }


                        $errovDivInner.append('');
                    }//Check Number
                }//End Check Number

            }//End if charsize check
        } //End Req check


        if (objType == "checkbox" && $('#' + theObj.id).attr('req') == 'true') {
            //alert($('#' + theObj.id).attr('checked'));
            //alert($('#' + theObj.id).is(':checked'));
            if ($('#' + theObj.id).is(':checked')) {

            }else{
                if ($('#' + theObj.id).attr('onError') != '') {
                    var onError = $('#' + theObj.id).attr('onError');
                    eval(onError);
                }
                if (theForm.getAttribute('createDivs') == 'true') {
                    $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                }

                ret = false;
            }
        }//Check if objType checkbox.
    }

    //Select Checking
    for (var i = 0; i <= theSelects.length - 1; i++) {
        var theObj = theSelects.item(i);
        var errorDiv = theObj.id + '_Error'; // Error DivName Generate


        if (theObj.getAttribute('req') == 'true') {
            if (theObj.value == $('#' + theObj.id).attr('noValue')) {

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid red';

                if (theForm.getAttribute('createDivs') == 'true') {
                    $errovDivInner.append('<div id="' + errorDiv + '" class="error_div"><b>Uyarı:</b> ' + $('#' + theObj.id).attr('errorString') + '</div>');
                }
                ret = false;
            } else {
                if (document.getElementById(errorDiv) != null)
                $errovDivInner.append('');

                    if ($('#' + theObj.id).attr('chBorder') == 'true')
                        theObj.style.border = '1px solid lime';
            }
        }
    }
    $errovDivInner.fadeIn("slow");
    return ret;
}

$(document).ready(function() {
    defaultValueAction();
});
