$(document).ready(function(){
        	$("#giftlistSearch").find("input").each(function(){
        		$(this).keydown(function(event){
            		if(event.keyCode==13){
            			$('#searchLink').click();
            			return false;
            		}
           		});
        	});
	    	});
            function showErrorMessage(invalidEmail,invalidInput,invalidDate0,invalidDate1,invalidDate2,invalidDate3){
            	document.getElementById("errorDiv").clear;
                if(!testInput(invalidInput)){
					return;
                }
                var type = checkDate();
                switch (type) {
                    case 0:
                    	document.getElementById("errorDiv").innerHTML = "<p>" + invalidDate2 + "</p>";
                        return;
                    case 1:
                    	document.getElementById("errorDiv").innerHTML = "<p>" + invalidDate2 + "</p>";
                    	return;
					case 2:
						document.getElementById("errorDiv").innerHTML = "<p>" + invalidDate2 + "</p>";
						return;
					case 3:
						document.getElementById("errorDiv").innerHTML = "<p>" + invalidDate3 + "</p>";
						return;
                }
                var emailValue = document.getElementById("GiftlistSearch.searchInput").value;
                emailValue = $.trim(emailValue);
                if(emailValue==""){
                    // if the email is empty,don't verify the email
                    document.getElementById('giftlistSearch').submit();
					return;
                }
    			var regex1=/^([a-zA-ZáéíóúÁÉÍÓÚüÜñÑ0-9_\.\-]{2,249})[\@]\S+/;
    			var regex2=/\S+\@([a-zA-Z0-9\-]{2,}[\.]{1})+[a-zA-Z0-9]{2,4}$/;
    			var regex3=/^[^@]+\@{1}[^@]+$/;
    			if(regex1.test(emailValue)&&regex2.test(emailValue)&&regex2.test(emailValue)){
    				 document.getElementById('giftlistSearch').submit();
    			}else{
    				document.getElementById("errorDiv").innerHTML = "<p>" + invalidEmail + "</p>"
    			}
            }
            function testInput(invalidInput){
                var isOk = false;
                var eventType = document.getElementById("GiftlistSearch.propertyValues.eventName").value;
                var eventDate = document.getElementById("GiftlistSearch.propertyValues.eventDate").value;
                var userName = document.getElementById("GiftlistSearch.userName").value;
                var userLastName = document.getElementById("GiftlistSearch.userLastName").value;
                var email = document.getElementById("GiftlistSearch.searchInput").value;
                var input = eventType + eventDate + userName + userLastName + email;
                input = $.trim(input);
                if (input != "DD/MM/YYYY" && input != "") {
                    isOk = true;
                }else{
                    document.getElementById("errorDiv").innerHTML = "<p>" + invalidInput + "</p>";
                }
                return isOk;
            }
            function checkDate(){
                var value = document.getElementById("GiftlistSearch.propertyValues.eventDate").value;
                value = $.trim(value);
                if(value == "" || value =="DD/MM/YYYY"){
                    // don't check date
					return -1;
                }
                var end = value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
                if (end != null) {
                    // match success
                    var ls = value.split(/\//);
                    var day = ls[0];
                    var month = ls[1];
                    var year = ls[2];
                    if(year < 1){
						return 0;
					}
                    if (month < 1 || month > 12) {
                        // out of index of month
                        return 1;
                    }
                    var runnian = 0;
                    if ((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0)) {
                        runnian = 1;
                    }
                    if (month == 2) {
                        if (day < 1 || day > (28 + runnian)) {
                            // out of index of day
                            return 2;
                        }
                    }
                    var addOneDay = 0;
                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                        addOneDay = 1;
                    }
                    if (day < 1 || day > (30 + addOneDay)) {
                    	 // out of index of day
                        return 2;
                    }
                }else{
					// The date format do not match
                	return 3;
                }
            }
            function setEventDateValue(type){
            	document.getElementById("GiftlistSearch.propertyValues.eventDate").value
            	var value = document.getElementById("GiftlistSearch.propertyValues.eventDate").value;
            	value = $.trim(value);
            	switch(type) {
            	case 2:
                	if(value == "DD/MM/YYYY"){
                		document.getElementById("GiftlistSearch.propertyValues.eventDate").value = "";
                	}
            		break;
            	case 1:
            		if(value == ""){
            			document.getElementById("GiftlistSearch.propertyValues.eventDate").value = "DD/MM/YYYY";
            		}
            		break;
            	}
            }
