﻿function execDateCommand(e, dateBox) {
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	
	var isSystemKey = isAllowedSystemKey(e);
	var booValid = (isSystemKey || isAllowedKey(e));
	
	if(isSystemKey || keychar=="t" || keychar=="T") {
		if(keychar=="t" || keychar=="T") { // t, Today
			var today = new Date();
			writeDate(dateBox,today);
		} else if(e.keyCode == 33 && (e.which == 0 || e.which == undefined)) { // Page up
			increaseMonth(dateBox);
		} else if(e.keyCode == 34 && (e.which == 0 || e.which == undefined)) { // Page down
			decreaseMonth(dateBox);
		} else if(e.keyCode == 38 && (e.which == 0 || e.which == undefined)) { // Arrow up
			increaseDay(dateBox);
		} else if(e.keyCode == 40 && (e.which == 0 || e.which == undefined)) { // Arrow down
			decreaseDay(dateBox);
		} else if((key == 43 && e.type == "keypress") || (e.keyCode == 187 && e.type == "keydown")) { // + pluss
			decreaseYear(dateBox);
			booValid = false;
		} else if((key == 45 && e.type == "keypress") || (e.keyCode == 189 && e.type == "keydown")) { // - minus
			increaseYear(dateBox);
			booValid = false;
		}
	}
	
	return booValid;
}

function isAllowedKey(e) {
	var key = window.event ? e.keyCode : e.which;	
	var keychar = String.fromCharCode(key);
		
	switch(keychar) {
		case "0":
		case "1":
		case "2":
		case "3":
		case "4":
		case "5":
		case "6":
		case "7":
		case "8":
		case "9":
		case ".":
			return true;
			break;
	}
	
	switch(key) {
		case 96: // 0
		case 97: // 1
		case 98: // 2
		case 99: // 3
		case 100: // 4
		case 101: // 5
		case 102: // 6
		case 103: // 7
		case 104: // 8
		case 105: // 9
			return true;
			break;
	}
	
	if(key == 190 && e.type == "keydown") { // . punctuation
		return true;
	}
	
	return false;
}

function isAllowedSystemKey(e) {
	var key = window.event ? e.keyCode : e.which;	
	var keychar = String.fromCharCode(key);
	
	return ((e.ctrlKey && keychar != "v") || (e.shiftKey && e.keyCode==9) || e.altKey || e.keyCode == 8 || e.keyCode == 9 || 
		   (((e.keyCode >= 33 && e.keyCode <= 40) || e.keyCode == 46 || e.keyCode == 45) && (e.which == 0 || e.which == undefined)) || 
		   ((key == 43 || key == 45) && e.type == "keypress") || 
		   ((e.keyCode == 187 || e.keyCode == 189) && e.type == "keydown"));
}



function replaceDateBoxValue(dateBoxID, clearOutConditionString, replaceMentString, giveFocus) {
	var dateBox = document.getElementById(dateBoxID);
	if(dateBox != null) {
		if(dateBox.value == clearOutConditionString) {
			dateBox.value = replaceMentString;
			if(giveFocus) {
				dateBox.document.selection.createRange().select();
			}
			return true;
		}
	}
	return false;
}

function KeyPressDateIntellisense(e, objTxtId) {
    var key = window.event ? e.keyCode : e.which;
    if(key==13) {
        dateIntellisense(objTxtId,"");
    }
}

function dateIntellisense(dateBoxID, strFormat){
	if(!replaceDateBoxValue(dateBoxID, "", strFormat, false)) {
		var dateBox = document.getElementById(dateBoxID);
		var strOutputDate = getDateInstance(dateBox.value, strFormat);
		if(strOutputDate.length > 0) {
			dateBox.value = strOutputDate;
		}
	}
}

function getDateInstance(strDate,strFormat) {
	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) && ((strDate.indexOf('-') > -1) || (strDate.indexOf('/') > -1)))
		return '';
	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 nCurrentDay = new Date().getDate();
	var nCurrentMonth = new Date().getMonth() + 1;
	var nCurrentYear = 1;
	
	//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),10) == 0) ? strDate.substr(1, 1) : strDate.substr(0, 2),10);
		if(nStringLength > 2) nMonth = parseInt((parseInt(strDate.substr(2,1),10) == 0) ? strDate.substr(3,1) :strDate.substr(2,2));
		if(nStringLength > 4) nYear = parseInt(strDate.substr(4),10);
		else nYear = new Date().getFullYear();
	}
	else {
		var strD = arrDate[0];
		nDay = parseInt(strD.length == 1 ? strD : (parseInt(strD,10) == 0 ? strD.substr(1,1) : strD));
		
		var strM = arrDate[1];
		nMonth = parseInt(strM.length == 0 ? -1 : (strM.length == 1 ? strM : (parseInt(strM,10) == 0 ? strM.substr(1,1) : strM)),10);
		
		if(nArrLength == 3){
			var strY = arrDate[2];
			nYear = parseInt(strY.length == 0 ? -1 : (strY.length == 1 ? strY : (parseInt(strY.substr(0,1),10) == 0 ? strY.substr(1) : strY)),10);
		} else {
			nYear = new Date().getFullYear();
		}
	}
	
	if(strFormat.trim()!="") {
	    if(strFormat.toLowerCase().indexOf("d") > strFormat.toLowerCase().indexOf("m")) {
	        var tmp = nDay;
	        nDay = nMonth;
	        nMonth = tmp;
	    }
	    
	    if(nDay==-1) nDay = nCurrentDay;
	}
	
	if(nDay == 0 || nDay > 31) return '';
	if(nMonth == -1) nMonth = nCurrentMonth;
	else if(nMonth == 0 || nMonth > 12) return '';
	
	var bSet = false;
	
	if (nYear < 20){ 
		if (nYear + 2000 == nCurrentYear){
			if (nMonth == nCurrentMonth && nDay >= nCurrentDay){
				nYear += 2000;
				bSet = true;
			}
			if ((nMonth > nCurrentMonth) && !bSet){
				nYear += 2000;
				bSet = true;
			}
		}
		if ((nYear + 2000 > nCurrentYear) && !bSet){
			nYear += 2000;
		}
	} else if(nYear < 100) {
		nYear += 1900;
	} 
	
	if(nYear == -1) nYear = nMonth > nCurrentMonth || nMonth == nCurrentMonth && nDay >= nCurrentDay ? nCurrentYear : nCurrentYear + 1;
	
	if(!isValidDate(nDay, nMonth, nYear)) return '';
	return dateToString(nDay, nMonth, nYear, strFormat);
}

function KeyPressTimeIntellisense(e, objTxtId) {
    var key = window.event ? e.keyCode : e.which;
    if(key==13) {
        timeIntellisense(objTxtId,"");
    }
}

function timeIntellisense(dateBoxID, strFormat){
	if(!replaceDateBoxValue(dateBoxID, "", strFormat, false)) {
		var dateBox = document.getElementById(dateBoxID);
		var strOutputDate = getTimeInstance(dateBox.value);
		if(strOutputDate.length > 0) {
			dateBox.value = strOutputDate;
		}
	}
}

function getTimeInstance(strTime) {
	if(strTime == null) return '';
	var nStringLength = strTime.length;
	if(nStringLength == 0) return '';
	var arrTime = strTime.split(":");
	
	var nArrLength = arrTime.length;
	if(nArrLength > 2) return '';
	var nHour = -1;
	var nMinute = -1;
	
	if(nArrLength == 1) {
		if(nStringLength > 2 && nStringLength % 2 != 0) return '';
		nHour = parseInt(nStringLength <= 2 ? strTime : (parseInt(strTime.substr(0,1),10) == 0) ? strTime.substr(1, 1) : strTime.substr(0, 2),10);
		if(nStringLength > 2) nMinute = parseInt((parseInt(strTime.substr(2,1),10) == 0) ? strTime.substr(3,1) :strTime.substr(2,2));
	}
	else {
		var strH = arrTime[0];
		nHour = parseInt(strH.length == 1 ? strH : (parseInt(strH,10) == 0 ? strH.substr(1,1) : strH),10);
		
		var strM = arrTime[1];
		nMinute = parseInt(strM.length == 0 ? -1 : (strM.length == 1 ? strM : (parseInt(strM,10) == 0 ? strM.substr(1,1) : strM)),10);
	}
	
	if(!isValidTime(nHour, nMinute)) return '';
	return (nHour < 10 ? "0" : "") + nHour + ":" + (nMinute < 10 ? "0" : "") + nMinute;
}

function dateToString(nDay, nMonth, nYear, strFormat){
	var strDay;
	var strMonth;
	
	var fillDays = (strFormat.toLowerCase().indexOf('d')<strFormat.toLowerCase().lastIndexOf('d'))	
	var fillMonth = (strFormat.toLowerCase().indexOf('m')<strFormat.toLowerCase().lastIndexOf('m'))	
	
	if(nDay < 10)
		strDay = (fillDays ? "0" : "") + nDay;
	else
		strDay = "" + nDay;
		
	if(nMonth < 10)
		strMonth = (fillMonth ? "0" : "") + nMonth;
	else
		strMonth = "" + nMonth;
	
	if (strFormat != "") 
	    return strFormat.toLowerCase().replace(fillDays ? "dd" : "d", strDay).replace(fillMonth ? "mm" : "m", strMonth).replace("yyyy",nYear.toString());
	else
	    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);
}
function isValidTime(nHour, nMinute) {
	if(nHour < 0 || nHour > 23 || nMinute < 0 || nMinute > 59) return false;
	return true
}

function increaseDay(oDateBox) {
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setDate(nextDate.getDate() + 1);
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function decreaseDay(oDateBox) {
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setDate(nextDate.getDate() - 1);
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function increaseMonth(oDateBox) {
	var currentDate = getCurrentDate(oDateBox);
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setMonth(nextDate.getMonth() + 1);
	if((nextDate.getMonth() - currentDate.getMonth()) > 1) {
		var finished = false
		do {
			nextDate.setDate(nextDate.getDate() - 1);
			if((nextDate.getMonth() - currentDate.getMonth()) == 1) {
				finished = true;
			}
		} while(!finished);
	}
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function decreaseMonth(oDateBox) {
	var currentDate = getCurrentDate(oDateBox);
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setMonth(nextDate.getMonth() - 1);
	if((currentDate.getMonth() - nextDate.getMonth()) == 0) {
		var finished = false
		do {
			nextDate.setDate(nextDate.getDate() - 1);
			if((currentDate.getMonth() - nextDate.getMonth()) == 1) {
				finished = true;
			}
		} while(!finished);
	}
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function increaseYear(oDateBox) {
	var currentDate = getCurrentDate(oDateBox);
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setFullYear(nextDate.getFullYear() - 1);
	if((nextDate.getMonth() - currentDate.getMonth()) > 0) {
		var finished = false
		do {
			nextDate.setDate(nextDate.getDate() - 1);
			if((nextDate.getMonth() - currentDate.getMonth()) == 0) {
				finished = true;
			}
		} while(!finished);
	}
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function decreaseYear(oDateBox) {
	var currentDate = getCurrentDate(oDateBox);
	var nextDate = getCurrentDate(oDateBox);
	nextDate.setFullYear(nextDate.getFullYear() + 1);				
	if((nextDate.getMonth() - currentDate.getMonth()) > 0) {
		var finished = false
		do {
			nextDate.setDate(nextDate.getDate() - 1);
			if((nextDate.getMonth() - currentDate.getMonth()) == 0) {
				finished = true;
			}
		} while(!finished);
	}
	writeDate(oDateBox,nextDate);
	event.returnValue = false;
}

function getCurrentDate(oDateBox) {
	var xDate = new Date();
	if(oDateBox != null) {
		if(oDateBox.value != '') {
			var date = parseInt(oDateBox.value.substring(0,2), 10);
			var month = parseInt(oDateBox.value.substring(3,5), 10) - 1;
			var year = parseInt(oDateBox.value.substring(6,10));
			xDate = new Date(year,month,date);
		}
	}
	return xDate;
}

function writeDate(oDateBox, xdate) {
	if(oDateBox != null) {
		oDateBox.value = ((xdate.getDate() < 10) ? '0' : '') + xdate.getDate() + '.' + (((xdate.getMonth() + 1) < 10) ? '0' : '') + (xdate.getMonth() + 1) + '.' + xdate.getFullYear();
	}
}



/*****************************************************************/
/******************* R E G I S T E R M E R K E *******************/
/*****************************************************************/


function KeyPressRegMrkIntellisense(e, objTxt) {
    var key = window.event ? e.keyCode : e.which;
    if(key==13) {
        regMrkIntellisense(objTxt);
    }
}

function regMrkIntellisense(objTxt) {
    var original = objTxt.value;
    try {
        var strRM = objTxt.value;
        
        if(strRM == null) return '';
	    var nStringLength = strRM.length;
	    if(nStringLength == 0) return '';
	    var arr = strRM.split("-");
    	
	    var nArrLength = arr.length;
	    if(nArrLength > 3) return '';
	    var nFirst = "";
	    var nSecond = "";
	    var nThird = "";
    	
    	
	    var rg = strRM;
	    if(nArrLength==1||nArrLength==2) {
	        rg = "";
	        var bFSet = false;
	        for(var i=0;i<nStringLength;i++) {
	            var c = strRM.charAt(i);
	            if((c>="a"&&c<="z")||(c==unescape("%E5"))||(c==unescape("%E6"))||(c==unescape("%F8"))||
	               (c>="A"&&c<="Z")||(c==unescape("%C5"))||(c==unescape("%C6"))||(c==unescape("%D8"))) {
	                if(bFSet) { 
	                    rg += "-";
	                    bFSet = false;
	                }
	                rg += c;
	            } else if(c>="0"&&c<="9") {
	                if(!bFSet) { 
	                    rg += "-";
	                    bFSet = true;
	                }
	                rg += c;
	            } else if(c=="-") {
	                bFSet=!bFSet;
	                rg += c;
	            }
	        }  
	    } 
    	
	    arr=rg.split("-");
	    if(arr.length>3) return;  
    	
	    nFirst=arr[0];
	    if(nFirst.length<2) {
	        var l=nFirst.length;
	        for(var i=0;i<2-l;i++) nFirst+=" ";
	    }
    	
	    if(arr.length>1) {
	        nSecond=arr[1];
	        var l=nSecond.length;
	        if(nSecond.length<4) for(var i=0;i<4-l;i++) nSecond="0"+nSecond;
	    }
	    if(arr.length>2) nThird=arr[2];
    	objTxt.value = (nFirst+"-"+nSecond+(nThird.length>0?"-"+nThird:"")).toUpperCase();
	} catch(ex) {
	    objTxt.value = original;
	}
}

