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 > 2012)
        error = 1;

    if (mnt < 0 || mnt > 12)
        error = 1;

    var yC = ltc % 4;
    if (day < 0) error = 1;
    var 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;

    /**
     *
     * @author Loft visual & audio arts
     * @author GEN3 CREATIVE
     *
     * (id)_Error = Inputa bağlı hata mesajı gösterilecek div
     *
     * ***** 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();
        var divElement = '<div id="' + errorDiv + '" class="error_div"></div>';
        if ($('#' + 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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                    $('#' + theObj.id).after(divElement);

                if (document.getElementById(errorDiv) != null)
                    $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                ret = false;
            } else {

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid lime';

                if (document.getElementById(errorDiv) != null)
                    document.getElementById(errorDiv).innerHTML = '';
            }
        } else {
            if ((String(theObj.value).length > $('#' + theObj.id).attr('maxChar'))) {
                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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true') {
                    $('#' + theObj.id).after(divElement);
                }

                if (document.getElementById(errorDiv) != null) {
                    $('#' + errorDiv).html($('#' + theObj.id).attr('errorString') + '<b style="color:#ff0000;">' + String(theObj.value).length + '</b> karakter');
                }

                ret = false;
            } else {

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid lime';

                if (document.getElementById(errorDiv) != null)
                    document.getElementById(errorDiv).innerHTML = '';
            }
        }
    }

    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();
        var divElement = '<div id="' + errorDiv + '" class="error_div"></div>';

        //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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                    $('#' + theObj.id).after(divElement);

                if (document.getElementById(errorDiv) != null)
                    $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                ret = false;
            } else {//End charSize check

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid lime';

                if (document.getElementById(errorDiv) != null)
                    document.getElementById(errorDiv).innerHTML = '';

                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);
                        }

                        if (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                            $('#' + theObj.id).after(divElement);

                        if (document.getElementById(errorDiv) != null)
                            $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                        ret = false;
                    } else {
                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid lime';

                        if (document.getElementById(errorDiv) != null)
                            document.getElementById(errorDiv).innerHTML = '';
                    }
                }// End mail check

                if (objType == "password" && $('#' + theObj.id).attr('isPass') == 'true') {
                    if (document.getElementById($('#' + theObj.id).attr('mustMatch')) != null) {
                        var mMatchValue = document.getElementById($('#' + theObj.id).attr('mustMatch')).value;

                        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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                                $('#' + theObj.id).after(divElement);

                            if (document.getElementById(errorDiv) != null)
                                $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));
                            ret = false;
                        } else {
                            if ($('#' + theObj.id).attr('chBorder') == 'true')
                                theObj.style.border = '1px solid lime';

                            if (document.getElementById(errorDiv) != null)
                                document.getElementById(errorDiv).innerHTML = '';
                        }
                    }
                }//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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                            $('#' + theObj.id).after(divElement);

                        if (document.getElementById(errorDiv) != null)
                            $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                        ret = false;
                    } else if (theObj.style.borderColor != 'red') {
                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid lime';

                        if (document.getElementById(errorDiv) != null)
                            document.getElementById(errorDiv).innerHTML = '';
                    }//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 (document.getElementById(errorDiv) == null && theForm.getAttribute('createDivs') == 'true')
                            $('#' + theObj.id).after(divElement);

                        if (document.getElementById(errorDiv) != null)
                            $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                        ret = false;
                    } else if (theObj.style.borderColor != 'red') {
                        if ($('#' + theObj.id).attr('chBorder') == 'true')
                            theObj.style.border = '1px solid lime';

                        if (document.getElementById(errorDiv) != null)
                            document.getElementById(errorDiv).innerHTML = '';
                    }//Check Number
                }//End Check Number

            }//End if charsize check
        } //End Req check


        if (objType == "checkbox" && $('#' + theObj.id).attr('req') == 'true') {
            if ($('#' + theObj.id).attr('checked') != true) {
                if ($('#' + theObj.id).attr('onError') != '') {
                    var onError = $('#' + theObj.id).attr('onError');
                    eval(onError);
                }
                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
        var divElement = '<div id="' + errorDiv + '" class="error_div"></div>';

        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 (document.getElementById(errorDiv) == null && $("#" + theForm.id).attr('createDivs') == 'true')
                    $('#' + theObj.id).after(divElement);

                if (document.getElementById(errorDiv) != null)
                    $('#' + errorDiv).html($('#' + theObj.id).attr('errorString'));

                ret = false;
            } else {
                if (document.getElementById(errorDiv) != null)
                    document.getElementById(errorDiv).innerHTML = '';

                if ($('#' + theObj.id).attr('chBorder') == 'true')
                    theObj.style.border = '1px solid lime';
            }
        }
    }
    return ret;
}

$(document).ready(function(){
  defaultValueAction();
})
