﻿


$(document).ready(
    function () {
        $('div').filter(
                    function () {
                        return this.className.match('frmValid');
                    }
                ).each(
                function () {
                    (new validtion()).init(this);
                }
                );

        $(".clk").each(function (index) {
            $(this).click(function () {
                $prnt = $(this).parents(".sld");
                $prnt.find(".clk").removeClass("act");
                $(this).addClass("act");
                $prnt.find(".bxs").slideUp(400);
                $prnt.find(".bxs:eq(" + index + ")").slideDown(400);
            });
        });

        loop(-1);
        picAct();
    }
    );

function gEId(id) {
    return document.getElementById(id);
}

function gEsTag(obj, tag) {
    return obj.getElementsByTagName(tag);
}

function gEsName(p, tg) {
    return p.getElementsByTagName(tg);
}
///////////////////// validation //////////////////////

$(document).ready(
    function () {
        $('div').filter(
                    function () {
                        return this.className.match('frmValid');
                    }
                ).each(
                function () {
                    (new validtion()).init(this);
                }
                );
    }
);

//validation
function validtion() {

    var $inpts;

    this.init = function (dv) {
        $inpts = $(dv).find(':input,:checkbox').filter(
                    function () {
                        return (this.parentNode.className.match('req') || this.className.match('req'));
                    }
                );

        $inpts.focus(function () {
            var inpPr = !this.className.match('req') ? $(this).parent().parent() : $(this).parent();
            $(inpPr).find('span').filter(
            function () {
                return this.className.match('req');
            }
            ).fadeOut(500);
            inpPr.find('span.overLable').fadeOut(500);
        });

        $inpts.blur(function () {
            check(this);
            if ($.trim(this.value) == '')
                $(this).parent().find('span.overLable').fadeIn(500);
        });

        $inpts.each(
            function () {
                $(this).parent().find('span.overLable').click(
                    function () {
                        $(this).fadeOut(500);
                        $(this).parent().find(':input').focus();
                    }
                )
            }
        );

        $(':button,:submit,a').filter(
            function () {
                return this.className.match('frmBtn' + $(dv).attr('class').match('frmValid(.*)')[1]);
            }
        ).click(
            function (event) {
                var frst = true;
                var ret = true;
                $inpts.each(
                    function () {
                        ret = (check(this) && ret);
                        if (!ret && frst) {
                            $('html, body').animate({
                                scrollTop: $(this).offset().top
                            }, 500);
                            frst = false;
                        }
                    }
                )
                ret ? true : event.preventDefault();
            }
        );
    };

    check = function (inpt) {
        var cls = !inpt.className.match('req') ? inpt.parentNode.className : inpt.className;
        var val = inpt.value;
        var pr = !inpt.className.match('req') ? $(inpt).parent().parent() : $(inpt).parent();

        if (cls.match('reqVal') && $.trim(val) == '') {
            pr.find("span.reqVal").fadeIn(500); return false;
        }
        if (cls.match('reqCheck') && !$(inpt).is(':checked')) {
            pr.find("span.reqCheck").fadeIn(500); return false;
        }
        if (cls.match('reqNum') && !val.match('\^[0-9]*\$')) {
            pr.find("span.reqNum").fadeIn(500); return false;
        }
        if (cls.match('reqMail') && !val.match('\^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\$')) {
            pr.find("span.reqMail").fadeIn(500); return false;
        }
        if (cls.match('reqUrl') && !val.match('\^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+\$')) {
            pr.find("span.reqUrl").fadeIn(500); return false;
        }
        if (cls.match('reqUser') && !val.match('\^[a-zA-Z][a-zA-Z0-9_-]*\$')) {
            pr.find("span.reqUser").fadeIn(500); return false;
        }
        if (cls.match('reqDate') && !val.match('\^(|19|20|13)[0-9]{2}[-/]([1-9]|0[1-9]|1[0-2])[-/]([1-9]|[0-2][0-9]|3[0-1])\$')) {
            pr.find("span.reqDate").fadeIn(500); return false;
        }
        if (cls.match('reqMobile') && !val.match('\^09[1-3][0-9]{8}\$')) {
            pr.find("span.reqMobile").fadeIn(500); return false;
        }
        if (cls.match('reqPhone') && !val.match('\^0[1-9]{2,5}-[0-9]{6,8}\$')) {
            pr.find("span.reqPhone").fadeIn(500); return false;
        }
        if (cls.match('reqEqual') && $('.' + cls.match('reqEqual(.+)')[1]).val() != val) {
            pr.find("span.reqEqual").fadeIn(500); return false;
        }
        if (cls.match('reqMin') && Number(cls.match('reqMin([0-9])')[1]) > ($.trim(val).length)) {
            pr.find("span.reqMin").fadeIn(500); return false;
        }
        return true;
    };
}

/////////////////////////////////pop Up//////////////////////////////////

function showPop(pId, hfId, conId, imgId) {
    getScrollWidth();
    var bck = gEId('popback');
    var pop = gEId(pId);
    if (hfId) {
        var hf = gEId(hfId);
        if (hf) {
            hf.value = conId;
        }
        else {
            gEId('bigImg').src = hfId;
        }
    }
    if (bck.style.display == 'block') {
        bck.style.display = 'none';
        pop.style.display = 'none';
    }
    else {
        bck.style.display = 'block';
        pop.style.display = 'block';
        if (imgId)
            gEId('popImg').src = gEId(imgId).src;
        setWH(pop, bck);
        window.onresize = function () { setWH(pop, bck); };
        window.onscroll = function () { setWH(pop, bck); };
    }
}
function setWH(pop, bck) {
    getScrollWidth();
    var wdt = pop.offsetWidth;
    var hgt = pop.offsetHeight;
    bck.style.width = scnWid + "px";
    bck.style.height = scnHei + "px";
    pop.style.left = scnWid / 2 - wdt / 2 + "px";
    pop.style.top = scnHeiP / 2 + scrTop - hgt / 2 + "px";
}

function getScrollWidth() {
    if (self.innerHeight) {
        scnWid = self.innerWidth;
        scnHei = document.documentElement.scrollHeight;
        scnHeiP = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        scnWid = document.documentElement.clientWidth;
        scnHei = document.documentElement.scrollHeight;
        scnHeiP = document.documentElement.clientHeight;
    }
    else if (document.body) {
        scnWid = document.body.clientWidth;
        scnHei = document.body.scrollHeight;
        scnHeiP = ddocument.body.clientHeight;
    }

    scrTop = document.body.scrollTop;
    if (scrTop == 0) {
        if (window.pageYOffset)
            scrTop = window.pageYOffset;
        else
            scrTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
}

var loopTm;
function loop(indx) {
    clearTimeout(loopTm);

    var sls = $('.sld-pic img');
    if (indx == -1)
        indx = $(sls).length - 1;

    $(sls).each(function (eInx) {
        if (indx != eInx)
            $(this).fadeOut(1000);
        else
            $(this).fadeIn(1000);
    });

    if (indx < $(sls).length - 1)
        indx++
    else
        indx = 0;
    loopTm = setTimeout("loop(" + indx + ")", 5000);
}

function picAct() {
    $('.pic-vw .pic-sml').click(function () {
        var sml = $(this).attr('src');
        $(this).attr('src', $('.pic-vw .pic-big').attr('src'));
        $('.pic-vw .pic-big').attr('src', sml);
    }
    )

}


function doClick(elementName) {

    //Press Enter Key in Firefox
    var b = gEId(elementName);
    if (b && typeof (b.click) == 'undefined') {
        b.click = function () {
            var result = true;
            if (b.onclick) result = b.onclick();
            if (typeof (result) == 'undefined' || result) {
                eval(b.getAttribute('href'));
            }
        }
    }
}
