var formatDatePattern = new Array("MM/dd/yyyy", "MM-dd-yyyy", "MM.dd.yyyy", "MMddyyyy", 
            "MM/dd", "MM-dd", "MM.dd", "MMdd",
            "dd/MM/yyyy", "dd-MM-yyyy", "dd.MM.yyyy", 
            "yyyy/MM/dd", "yyyy-MM-dd", "yyyy.MM.dd", "yyyyMMdd",
            "yyyy/M/dd", "yyyy-M-dd", "yyyy.M.dd", 
            "yyyy/MM/d", "yyyy-MM-d", "yyyy.MM.d", 
            "yyyy/M/d", "yyyy.M.d", "yyyy.M.d", 
            "yyyy/MM", "yyyy-MM", "yyyy.MM", "yyyyMM",
            "yyyy/M", "yyyy-M", "yyyy.M", 
            "yy/MM/dd", "yy-MM-dd", "yy.MM.dd", "yyMMdd",
            "yy/M/dd", "yy-M-dd", "yy.M.dd",
            "yy/MM/d", "yy-MM-d", "yy.MM.d",
            "yy/M/d", "yy-M-d", "yy.M.d", 
            "yy/MM", "yy-MM", "yy.MM", "yyMM",
            "yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss", "yyyyMMdd HHmmss",
            "yyyyMMddHHmmss", "yyyyMMddHHmm", "HH:mm:ss", "HHmmss", "HHmm", "HH:mm" 
							  );
var MAX_SPLIT_CHAR_NUM = 10;
							  
function DateProperties() {
	this.dateStr = "";
	this.matchedFormat = "";
	this.bFormatOK = false;
	this.year = "";
	this.month = "";
	this.day = "";
	this.hour = "";
	this.minute = "";
	this.second = "";
};

function DateFormatCtrl(dateStr) {
	this.dateStr = dateStr;
	
	this.getDateProperties = getDateProperties;
	
	this.format = format;
};

function getDateProperties() {
	return _getMatchedDateFormat(this.dateStr);
};

function format(dateProp, pattern) {
    var bMatchedFormat = false;

    var _year = "";
    var _month = "";
    var _day = "";
    var _hour = "";
    var _minute = "";
    var _second = "";
    	
    if (dateProp.bFormatOK == false) {
	return dateProp.dateStr;
    }

    for (var i = 0; i < formatDatePattern.length; i++) {
    	   	
    	var currPattern = formatDatePattern[i];
    	if (pattern == currPattern) {
    		bMatchedFormat = true;
    		break;
    	}
    }
    
    if (bMatchedFormat == false) {
    	return this.dateStr;
    }

    var y_Num = 0;
    var M_Num = 0;
    var d_Num = 0;
    var H_Num = 0;
    var h_Num = 0;
    var m_Num = 0;
    var s_Num = 0;
        
    var num = 0;
    var hourType = "";
    for (var j=0; j<pattern.length; j++) {
    	var ch1 = pattern.charAt(j);
    	
    	if (ch1 == 'y') {
           	y_Num += 1;
        } else if (ch1 == 'M') {
           	M_Num += 1;
        } else if (ch1 == 'd') {
           	d_Num += 1;
        } else if (ch1 == 'H') {
        	hourType += "H";
           	H_Num += 1;
        } else if (ch1 == 'h') {
        	hourType += "h";
           	h_Num += 1;
        } else if (ch1 == 'm') {
           	m_Num += 1;
        } else if (ch1 == 's') {
           	s_Num += 1;
        }
    }
    
    //TODO format 
    _year = _convertYear(dateProp.year, y_Num);
    _month = _convertMonth(dateProp.month, M_Num);
//alert("dateProp.month:"+dateProp.month+" M_Num:"+M_Num+ " _month:" + _month);
    _day = _convertDay(dateProp.day, d_Num);
    _hour = _convertHour(dateProp.hour, hourType);
    _minute = dateProp.minute;
    _second = dateProp.second;
    
    if (y_Num > 0 && _year == "")
	return this.dateStr;
    if (M_Num > 0 && _month == "")
	return this.dateStr;
    if (d_Num > 0 && _day == "")
	return this.dateStr;
    if ((H_Num > 0 || h_Num > 0)  && _hour == "")
	return this.dateStr;
    if (m_Num > 0 && _month == "")
	return this.dateStr;
    if (s_Num > 0 && _second == "")
	return this.dateStr;

    ret = pattern;
    ret = ret.replace("yyyy", _year);
    ret = ret.replace("yy", _year);
    ret = ret.replace("MM", _month);
    ret = ret.replace("M", _month);
    ret = ret.replace("dd", _day);
    ret = ret.replace("d", _day);
    ret = ret.replace("HH", _hour);
    ret = ret.replace("H", _hour);
    ret = ret.replace("hh", _hour);
    ret = ret.replace("h", _hour);
    ret = ret.replace("mm", _minute);
    ret = ret.replace("ss", _second);
    
    return ret;
}

function test(dateStr, y) {
//	alert("dateStr "+dateStr + "format :" + y);
};
/*********************************************************************************
*	FUNCTION:		isDate checks a valid date
*	PARAMETERS:		dateStr		AS String
*	CALLS:			isBetween
*	RETURNS:		TRUE if dateStr is a valid date otherwise false.
**********************************************************************************/
function formatDate(ctrlName, pattern) {
	var dateStr = ctrlName.value;
	
	var dateCtrl = new DateFormatCtrl(dateStr);
	var dateProp = dateCtrl.getDateProperties();

	ctrlName.value = dateCtrl.format(dateProp, pattern);

};

function _getMatchedDateFormat(strDate) {
  	var dateProp = new DateProperties();
  	dateProp.dateStr = strDate;

    for (var i = 0; i < formatDatePattern.length; i++) {
    	var year = "";
    	var month = "";
    	var day = "";
    	var hour = "";
    	var minute = "";
    	var second = "";
    	
    	var currPattern = formatDatePattern[i];
        if (strDate.length != currPattern.length)
            continue;

        for (var j = 0; j < strDate.length; j++) {
            var ch1 = strDate.charAt(j);
            var ch2 = currPattern.charAt(j);
            
            if (ch2 == 'y') {
            	year += ch1;
            } else if (ch2 == 'M') {
            	month += ch1;
            } else if (ch2 == 'd') {
            	day += ch1;
            } else if (ch2 == 'H' || ch2 == 'h') {
            	hour += ch1;
            } else if (ch2 == 'm') {
            	minute += ch1;
            } else if (ch2 == 's') {
            	second += ch1;
            }
            
            var timeElements = "yMdHhms";
            if ((ch1 < '0' || ch1 > '9') && (ch1 != ch2)) {
                break;
            } else if ((timeElements.indexOf(ch2) < 0) && (ch1 != ch2)) {
                break;
            }

            if (j == strDate.length - 1) {
            	if (isValidMonth(month) == true &&
            		isValidDay(day, month, year) == true &&
            		isValidHour(hour) == true &&
            		isValidMinute(minute) == true &&
            		isValidSecond(second) == true) {
            			dateProp.matchedFormat = currPattern;
            			dateProp.bFormatOK = true;
            			dateProp.year = year;
            			dateProp.month = month;
            			dateProp.day = day;
            			dateProp.hour = hour;
            			dateProp.minute = minute;
            			dateProp.second = second;
                		return dateProp;
                }
            }
        }
    }

    return dateProp;
}

function isValidMonth(month) {
	if (month == "")
		return true;
		
	if (month >= 1 && month <=12)
		return true;
	
	return false;
}

function isValidDay(day, month, year) {
	if (day == "")
		return true;
		
	var maxDays = 31;
	
	if (month==4 || month==6 || month==9 || month==11) 
		maxDays = 30;
	else if (month==2) {
		if (year % 4 > 0) 
			maxDays = 28;
		else if (year % 100 == 0 && year % 400 > 0) 
			maxDays = 28;
	    else 
			maxDays = 29;
	}
	if (day >=1 && day <= maxDays) { 
		return true; 
	}
	else { 
		return false; 
	}

	return false;
}

function isValidHour(hour) {
	if (hour == "")
		return true;
		
	if (hour >= 1 && hour <= 23)
		return true;
	
	return false;
}

function isValidMinute(minute) {
	if (minute == "")
		return true;
		
	if (minute >= 1 && minute <= 59)
		return true;
	
	return false;
}

function isValidSecond(second) {
	if (second == "")
		return true;
		
	if (second >= 1 && second <= 59)
		return true;
	
	return false;
}

/*********************************************************
* @function convertYear
* @param year
* @param num the output year's length
* @return the output year
*********************************************************/
function _convertYear(year, num) {

	var len = year.length;
	
	if (len != 2 && len != 4 && num != 2 && num != 4) {
		return year;
	}
	
	var ret = year;
	
	if (len == 2) {
		if (num == 4) {
			ret = "20" + year;
		}
	} else {		// len = 4
		if (num == 2) {
			ret = year.substring(2, 4);
		}
	}

	return ret;
}

/*********************************************************
* @function convertMonth
* @param month : can be 1-12 or 01-12
* @param num the output year's length
* @return the output month
*   if num = 1; the output is 1-12
* 	if num = 2; the output is 01-12
*********************************************************/
function _convertMonth(month, num) {
	var len = month.length;

	if (len != 1 && len != 2 && num != 1 && num != 2) {
		return month;
	}
	

//alert("month:"+month+" len:"+len+ " num:" + num);
	var ret = month;
	
	if (len == 1) {
		if (num == 2) {

			ret = "0" + month;
		}
	} else {		// len = 2
		if (num == 1 && month.substring(0, 1) == '0') {
			ret = month.substring(1, 2);
		}
	}
	
	return ret;
}

/*********************************************************
* @function convertDay
* @param day : can be 1-31 or 01-31
* @param num the output day's length
* @return the output day
*   if num = 1; the output is 1-31
* 	if num = 2; the output is 01-31
*********************************************************/
function _convertDay(day, num) {
	var len = day.length;
	
	if (len != 1 && len != 2 && num != 1 && num != 2) {
		return day;
	}
	
	var ret = day;
	
	if (len == 1) {
		if (num == 2) {
			ret = "0" + day;
		}
	} else {		// len = 2
		if (num == 1 && day.substring(0, 1) == '0') {
			ret = day.substring(1, 2);
		}
	}
	
	return ret;
}

/*********************************************************
* @function _convertHour
* @param hour : can be 0-23 or 00-23
* @param type : can be HH or hh
* @param num the output hour's length
* @return the output hour
*   if num = 1; the output is 1-31
* 	if num = 2; the output is 01-31
*********************************************************/
function _convertHour(hour, type) {
	var len = hour.length;
	
	if (len != 1 && len != 2 && num != 1 && num != 2) {
		return hour;
	} else if (type != "HH" && type != "hh" && type != "H" && type != "h") {
		return hour;
	}
	
	var num = type.length;
	var ret = hour;
	if (type == "HH" && hour <= 12) {
		ret = ret + 12;
	} else if (type == "hh" && hour > 12) {
		ret = ret - 12;
	}
	
	len = ret.length;
	if (len == 1) {
		if (num == 2) {
			ret = "0" + hour;
		}
	} else {		// len = 2
		if (num == 1 && hour.substring(0, 1) == '0') {
			ret = hour.substring(1, 2);
		}
	}
	
	return ret;
}
