// JScript File By Team Data System AS (c)

function clearDateField(objName){
	var strOutputDate = getDateInstance(objName.value);
	objName.value = strOutputDate;
}
function dateIntellisense(objName){
	var strOutputDate = getDateInstance(objName.value);
	if(strOutputDate.length > 0)
		objName.value = strOutputDate;
	else if(objName.value.length>0)
	    objName.value = '#';
 }
function getDateInstance(strDate){
	if(strDate == null) return '';
	var nStringLength = strDate.length;
	if(nStringLength == 0) return '';
	var arrDate;

	if ((strDate.indexOf('-') > -1) && (strDate.indexOf('.') > -1) && (strDate.indexOf(',') > -1))
		return '';
	
	if (strDate.indexOf('-') > -1)
		arrDate = strDate.split("-");
	else if (strDate.indexOf(',') > -1)
		arrDate = strDate.split(",");
	else 
		arrDate = strDate.split(".");
	
	var nArrLength = arrDate.length;
	if(nArrLength > 3) return '';
	var nDay = -1;
	var nMonth = -1;
	var nYear = -1;
	
	var d=new Date();
	var nCurrentDay = d.getDate();
	var nCurrentMonth = d.getMonth() + 1;
	var nCurrentYear = d.getFullYear();
	var nAutoMinYear = 11; //1911
	
	if (strDate.indexOf("/") > -1) return '';
	
	if(nArrLength == 1) {
		if(nStringLength > 2 && nStringLength % 2 != 0) return '';
		nDay = parseInt(nStringLength <= 2 ? strDate : (parseInt(strDate.substr(0,1)) == 0) ? strDate.substr(1, 1) : strDate.substr(0, 2));
		if(nStringLength > 2) nMonth = parseInt((parseInt(strDate.substr(2,1)) == 0) ? strDate.substr(3,1) :strDate.substr(2,2));
		if(nStringLength > 4) nYear = parseInt(strDate.substr(4));
	}
	else {
		var strD = arrDate[0];
		nDay = parseInt(strD.length == 1 ? strD : (parseInt(strD) == 0 ? strD.substr(1,1) : strD));
		
		var strM = arrDate[1];
		nMonth = parseInt(strM.length == 0 ? -1 : (strM.length == 1 ? strM : (parseInt(strM) == 0 ? strM.substr(1,1) : strM)));
		
		if(nArrLength == 3){
			var strY = arrDate[2];
			nYear = parseInt(strY.length == 0 ? -1 : (strY.length == 1 ? strY : (parseInt(strY.substr(0,1)) == 0 ? strY.substr(1) : strY)));
		}
	}
	if(nDay == 0 || nDay > 31) return '';
	if(nMonth == -1) {
		//nMonth = nDay >= nCurrentDay ? nCurrentMonth : (nCurrentMonth + 0) % 13;
	    nMonth = nCurrentMonth
	}
	else if(nMonth == 0 || nMonth > 12) return '';
	
	var bSet = false;
	if (nYear > 0){
	if (nYear < 100){
	    if (nYear > nAutoMinYear){
		   	nYear += 1900;
	    }
	    if (nYear < nAutoMinYear){
			nYear += 2000;
	    }
    }
    }

	//if(nYear == -1) nYear = nMonth > nCurrentMonth || nMonth == nCurrentMonth && nDay >= nCurrentDay ? nCurrentYear : nCurrentYear ;
    if(nYear == -1) nYear = nCurrentYear;
	
	if(!isValidDate(nDay, nMonth, nYear)) return '';
	
	return dateToString(nDay, nMonth, nYear);
}
function dateToString(nDay, nMonth, nYear){
	var strDay;
	var strMonth;
		
	if(nDay < 10)
		strDay = "0" + nDay;
	else
		strDay = "" + nDay;
		
	if(nMonth < 10)
		strMonth = "0" + nMonth;
	else
		strMonth = "" + nMonth;
	
	return strDay + "." + strMonth + "." + nYear;
}
function isValidDate(nDay, nMonth, nYear) {
	if(nDay < 1 || nDay > 31 || nMonth < 1 || nMonth > 12 || (nYear < 1000)) return false;
	return nDay <= (nMonth == 2 ? nYear % 400 == 0 ? 29 : nYear % 100 == 0 ? 28 : nYear % 4 == 0 ? 29 : 28 : (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) ? 30 : 31);
}
