$(document).ready(function() {
	
	$("input.hint, input.useLabelAsHint").focus(function(){
		if (this.defaultValue == this.value && this.defaultValue != '') {
		
			defaultValue = this.defaultValue
			this.value = ''; 
			if ($(this).hasClass('password')) { 
				// This does not work: attr("type", "password") - the type attribute cannot be set through code (in IE)
				// Therefor create a clone of the input, with only a different type
				$(this).after("<input name=\"" + this.name + "\" id=\"" + this.id + "\" value=\"" + "\" type=\"password\" class=\"" + this.className + "\" \>");
				var id = this.id;
				$(this).remove(); // Remove the old input
				$("#" + id).focus(); // Give focus to the new input
			} 
		};
	});

	$("input.hint, input.useLabelAsHint").blur(function(){
		if (this.value + '' == '' && this.defaultValue != '') {
			this.value = this.defaultValue; 
			// Please note that a password field that was cloned by the above function no longer has a blur function.
			// Trying to copy the events using jquery.copyEvents.js plugin also failed.
		};
	});
	

	$("input.first:not(.useLabelAsHint)").focus();
	
	$("form.validate").validate({
		errorClass: "validationerror",
		errorElement: "div"
	});

	
	
    jQuery.validator.addClassRules({
	
		tinyint: {
            digits: true
            , minlength: 1
            , maxlength: 3
            , min: 0
            , max: 255
        }
        , smallint: {
            digits: true
            , minlength: 1
            , maxlength: 6 //including negative sign
            , min: -32768
            , max: 32767            
        }
        , int: {
            digits: true
            , minlength: 1
            , maxlength: 11 //including negative sign
            , range: [-2147483648, 2147483647]      
        }
        , bigint: {
            digits: true
            , minlength: 1
            , maxlength: 20 //including negative sign
            , min: -9223372036854775808
            , max: 9223372036854775807            
        }
        , dotdecimalnumber: {
            number: true
		}
        , commadecimalnumber: {
           	numberDE: true
        }
        , dotmoney: {
            number: true
		}
        , commamoney: {
           	numberDE: true
        }
        , text: {
           maxlength: 4000
        }
		
    });

});

function IsValidDate(value) {
    var dateorder   
    switch (Date.format.toLowerCase().charAt(0)) {
        case "d": { dateorder = "dmy"; break }
        case "m": { dateorder = "mdy"; break }
        default: { dateorder = "ymd"; break }
    }
  
    var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
    m = value.match(yearFirstExp);
    var day, month, year;
    if (m != null && (m[2].length == 4 || dateorder == "ymd")) {
        day = m[6];
        month = m[5];
        year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10), Date.century, Date.cutoffyear)
    }
    else {
        if (dateorder == "ymd"){
            return false;		
        }						
        var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
        m = value.match(yearLastExp);
        if (m == null) {
            return false;
        }
        
        switch (dateorder) {
            case "mdy": {
                day = m[3];
                month = m[1];
                break;
            }
            case "dmy": {
                day = m[1];
                month = m[3];
                break;
            }
            case "ymd": {
                day = m[3];
                month = m[1];
                break;
            }
        }

        year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10), Date.century, Date.cutoffyear)
    }
    //alert(m[1] + " " + m[3] + " " + m[5])
    //alert(dateorder + " " + year + "y " + month + "m " + day + "d")
    month -= 1; 
    if (month > 12) { return false };
    var date = new Date(year, month, day);
    return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? true : false;

}

	function GetFullYear(year, century, cutoffyear) {
		return (year + parseInt(century)) - ((year < cutoffyear) ? 0 : 100);
		// Example: @TwoDigitYearMax="2029" --> cutoffyear = 29
		// if year < cutoffyear then  (70 + 2000) - 0   = 2070
		// if year >= cutoffyear then (70 + 2000) - 100 = 1970
   }

