var useMask = true; //gloabl var to allow for skipping a mask function
var mtype = 1; //global var to hold month type for add and remove date mask functions
var mflag = true; // global form error and submission flag
var arrMonthsLong = ["","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var arrMonthsShort = ["","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var arrDaysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var arrLeapDaysInMonth = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function selectAll(obj) {
	txtRng = obj.createTextRange();
	txtRng.select(obj.value);
}
function uCase(obj) {
	obj.value = obj.value.toUpperCase();
}
function lCase(obj) {
	obj.value = obj.value.toLowerCase();
}
function getDaysInMonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return arrDaysInMonth[monthNo];
		else
			return arrLeapDaysInMonth[monthNo];
	} 
	else
		return arrDaysInMonth[monthNo];
}
function applyDBMask(mask,obj) {
	switch(mask.toLowerCase()) {
		case 'phone':
			if(obj.value == "##########")
				obj.value = '';
			else
				obj.value = obj.value.replace(/[^0-9]/g,'');
			break;
		case 'sin':
			if(obj.value == '#########')
				obj.value = '';
			else
				obj.value = obj.value.replace(/[^0-9]/g,'');
			break;
		case 'date':
			if(obj.value == 'yyyy-mm-dd');
				obj.value = '';
			break;
	}
}

    function padDigits(n, totalDigits) 
    { 
        n = n.toString(); 
        var pd = ''; 
        if (totalDigits > n.length) 
        { 
            for (i=0; i < (totalDigits-n.length); i++) 
            { 
                pd += '0'; 
            } 
        } 
        return pd + n.toString(); 
    } 

function applyMask(mask,obj) {
	mflag = true;
	switch(mask.toLowerCase()) {
		case 'phone':
			switch(obj.value.length) {
				case 0:
					// do nothing
					break;
				case 7:
					obj.value = '(403) ' + obj.value.substr(0,3) + '-' + obj.value.substr(3,4);
					break;
				case 10:
					if(obj.value != '##########')
						obj.value = '(' + obj.value.substr(0,3) + ') ' + obj.value.substr(3,3) + '-' + obj.value.substr(6,4);
					else
						obj.value = '';
					break;
				default:
					mflag = false;
					alert('The phone number you entered is not a regular 7 or 10 digit number. Please try again.');
					obj.focus();
			}
			break;
		case 'money':
				if(obj.value.length != 0) {
					str = obj.value;
					decPos = str.indexOf(".");
					if(decPos == -1) {
						decPos = str.length;
						str = str + ".0";
					}
					dollars = str.substring(0,decPos);
					cents = Math.round(parseFloat(str.substring(decPos+1,decPos+3)+'.'+str.substring(decPos+3,str.length))).toString();
					cents = (cents.length < 2) ? cents + "0" : cents;
					obj.value = dollars + "." + cents;
				}
				else
					obj.value = '0.00';
				break;
		case 'sin':
			switch(obj.value.length) {
				case 0:
					//do nothing
					break;
				case 9:
					if(obj.value != '#########')
						obj.value = obj.value.substr(0,3) + '-' + obj.value.substr(3,3) + '-' + obj.value.substr(6,3);
					else
						obj.value = '';
					break;
				default:
					mflag = false;
					alert('The Social Insurance Number you entered is not a valid 9 digit number. Please try again.');
					obj.focus();
			}
			break;
		case 'date':
			switch(obj.value.length) {
				case 0:
					//do nothing
					break;
				case 10:
					if(obj.value != 'MM/DD/YYYY') {
						break;	
					}
					else
						obj.value = '';
						break;
				default:
					mflag = false;
					alert('You did not enter the complete date in the format \'MM/DD/YYYY\'. Please try again.');
					obj.value = '';
					obj.focus();
			}
			break;
		default:
			break;
	}
}

function removeMask(mask,obj) {
	switch(mask.toLowerCase()) {
		case 'phone':
			if(obj.value.length != 0)
				obj.value = obj.value.replace(/[^0-9]/g,'');
			else
				obj.value = '##########';
			break;
		case 'sin':
			if(obj.value.length != 0)
				obj.value = obj.value.replace(/[^0-9]/g,'');
			else
				obj.value = '#########';
			break;
		case 'date':
				if(useMask) {
					if(obj.value.length != 0) {
						arrdateint = obj.value.split("/");

						arrdateint[0] = padDigits(parseInt(arrdateint[0]),2);
						arrdateint[1] = padDigits(parseInt(arrdateint[1]),2);
						arrdateint[2] = padDigits(parseInt(arrdateint[2]),4);
						
						obj.select();
					}
					else
						obj.value = 'MM/DD/YYYY';
					    obj.select();
				}
				else
					useMask = true;
			break;
	}
	selectAll(obj);
}