var chkit = {
    date:function(d, m, y) {
        var mlen = [,31,((y%4==0&&(y%100!=0||y%400==0))?29:28),31,30,31,30,31,31,30,31,30,31];

        return !isNaN(d) && !isNaN(m) && !isNaN(y) && !(y<0 || m<1 || m>12 || d<1 || d>mlen[m]);
    },
    email:function(e) {
        return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(e);
    }
};

$(document).ready(

	function() {
        $('a').focus(
            function() {
                this.blur();
            }
        );

		$("#select_country").toggle(
		  function () {
			$('#country').show();
		  },
		  function () {
			$('#country').show();
		  }
		);
		
	
		$("#country").hover(
			  function () {
				$(this).show();
			  },
			  function () {
				$(this).hide();
			  }
		);
		
		$("#select_wallpaper").hover(
			  function () {
				$('#pop_up_wallpapers').show();
			  },
			  function () {
				$('#pop_up_wallpapers').hide();
			  }
		);

		$("#pop_up_wallpapers").hover(
			  function () {
				$('#pop_up_wallpapers').show();
			  },
			  function () {
				$('#pop_up_wallpapers').hide();
				
			  }
		);
		
		$("#select_videos").hover(
			  function () {
				$('#pop_up_videos').show();
			  },
			  function () {
				$('#pop_up_videos').hide();
			  }
		);
		
		$("#pop_up_videos").hover(
			  function () {
				$('#pop_up_videos').show();
			  },
			  function () {
				$('#pop_up_videos').hide();
			  }
		);
		
		
		$('.over').hover(
			function() {
				$('.div-text', this).addClass('change');
			}, 
			function() {
				$('.div-text', this).removeClass('change');
			}
		);
		
		$('#test').click(
            function() {
                $.modal('<iframe id="if_legals" src="guarantee.html" height="300" width="640" style="border:0"></iframe>', {
                    /*  closeHTML:"", */
                    overlayCss: {
                        backgroundColor: '#000',
                        cursor: 'default'
                    },
                    containerCss: {
                        backgroundColor: '#000',
                        border: '1px solid #b68c28',
						color: '#ff0000',
                        height:300,
                        padding:0,
                        width:640
                    },
                    overlayClose:true,
                    opacity: 80
                });
                $('#if_legals')[0].src="guarantee.html";
            }
        );  

        $('form.validate').submit(
            function(e) {
                var form_ok = true;
                $('input.required, textarea.required', this).each(
                    function() {
                        if($.trim(this.value).length < 1) {
                            $(this).css('border-color', '#ff0000');
                            form_ok = false;
                        }
                    }
                );
                if(!form_ok) {
                    alert( $('#error_fields')[0].value);
                    return false;
                }

                var email_valid = true;
                $('input.email', this).each(
                    function() {
                        if($.trim(this.value).length > 0) {
                            var email = $.trim(this.value);
                            if(!chkit.email(email)) {
                                $(this).css('border-color', '#ff0000');
                                email_valid = false;
                            }
                        }
                    }
                );
                if(!email_valid) {
                    alert( $('#error_email')[0].value);
                    return false;
                }

                if($('#email', this).length>0 && $('#confirm', this).length>0) {
                    var email = $.trim($('#email', this)[0].value);
                    var conf = $.trim($('#confirm', this)[0].value);
                    if(email != conf) {
                        alert( $('#error_confirm')[0].value);
                        return false;
                    }
                }

                if($('#day', this).length>0) {
                    var dobday = $('#day', this)[0].value;
                    if(dobday.substr(0,1) == '0') {
                        dobday = dobday.substr(1,1);
                    }
                    dobday = parseInt(dobday);
                    var dobmon = $('#month', this)[0].value;
                    if(dobmon.substr(0,1) == '0') {
                        dobmon = dobmon.substr(1,1);
                    }
                    dobmon = parseInt(dobmon);
                    var dobyear = parseInt($('#year', this)[0].value);
                    if(!isNaN(dobyear) && dobyear<1900) {
                        dobyear += (dobyear > 10 ? 1900 : 2000);
                    }
                    if(!chkit.date(dobday, dobmon, dobyear)) {
                        alert( $('#error_dob')[0].value);
                        return false;
                    }
                }

                if($('input[type=checkbox]#age', this).length>0) {
                    var age = $('input[type=checkbox]#age', this)[0].checked;
                    if(!age) {
                        alert( $('#error_age')[0].value);
                        return false;
                    }
                }
                if($('input[type=checkbox]#terms', this).length>0) {
                    var terms = $('input[type=checkbox]#terms', this)[0].checked;
                    if(!terms) {
                        alert( $('#error_terms')[0].value);
                        return false;
                    }
                }
				if($('input[type=checkbox]#resident', this).length>0) {
                    var resident = $('input[type=checkbox]#resident', this)[0].checked;
                    if(!resident) {
                        alert( $('#error_resident')[0].value);
                        return false;
                    }
                }
				if($('input[type=checkbox]#photo', this).length>0) {
                    var photo = $('input[type=checkbox]#photo', this)[0].checked;
                    if(!photo) {
                        alert( $('#error_photo')[0].value);
                        return false;
                    }
                }

                return form_ok;
            }
        );

	}
);


