// The next line enables jQuery IntelliSense in Visual Studio:
/// <reference path="/common/js/IntelliSense/jquery-1.3.1-vsdoc.js" />

$(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):not(.datepicker_jqueryui)").focus();

	$("#reload_captcha").click(function() {
		var oldSrc = $("#captcha > img").attr("src");
		$("#captcha > img").attr("src", oldSrc + "&amp;reload=1");
		return false;
	});

	jQuery.validator.addClassRules({
		tinyint: {
			digits: true
            , minlength: 1
            , maxlength: 3
            , min: 0
            , max: 255
		}
		
        , dotsmallint: {
			dotint: true
            , minlength: 1
            , maxlength: 6 //including negative sign
            , min: -32768
            , max: 32767
        }
        , commasmallint: {
			commaint: true
            , minlength: 1
            , maxlength: 6 //including negative sign
            , min: -32768
            , max: 32767
             }
   
        , dotint: {
        	dotint: true
            , minlength: 1
            , maxlength: 14 //including negative sign and Group Separators
            , range: [-2147483648, 2147483647]
        }
        , commaint: {
			commaint: true
            , minlength: 1
            , maxlength: 14 //including negative sign and Group Separators
            , range: [-2147483648, 2147483647]
		}
        
        , dotbigint: {
			dotint: true
            , minlength: 1
            , maxlength: 26 //including negative sign and Group Separators
            , min: -9223372036854775808
            , max: 9223372036854775807
        }
        , commabigint: {
			commaint: true
            , minlength: 1
            , maxlength: 26 //including negative sign and Group Separators
            , 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
}

function removeGroupSeparators(value) { // Remove all group separators. Used by jquery.validate.js - Added by Iconum.FvE
	var intIndexOfMatch = value.indexOf(",");
	while (intIndexOfMatch != -1) {
		value = value.replace(",", "")
		intIndexOfMatch = value.indexOf(",");
	}
	intIndexOfMatch = value.indexOf(".");
	while (intIndexOfMatch != -1) {
		value = value.replace(".", "")
		intIndexOfMatch = value.indexOf(".");
	}
	return value;
}