/******************** validate value **********************/
function isPosInteger(inputVal,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(inputVal)) return(!boolNotNull);
	
	inputStr = inputVal.toString()
	for(var i = 0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if(oneChar<"0" || oneChar>"9")
		{
			return false
		}
	}
	return true
}

function isInteger(inputVal,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(inputVal)) return(!boolNotNull);
	
	inputStr = inputVal.toString()
	for(var i = 0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if(i==0 && oneChar=="-")
		{
			continue
		}
		if(oneChar<"0" || oneChar>"9")
		{
			return false
		}
	}
	return true
}

function isNumber(inputVal,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(inputVal)) return(!boolNotNull);
	
	oneDecimal = false
	inputStr = inputVal.toString()
	for(var i = 0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if(i==0 && oneChar=="-")
		{
			continue
		}
		if(oneChar=="." && !oneDecimal)
		{
			oneDecimal = true
			continue
		}
		if(oneChar<"0" || oneChar>"9")
		{
			return false
		}
	}
	return true
}

/******************** Date validate **********************/
function DaysOfMonth(strYear,strMonth)
{
		var arrDaysOfMonth;
		var theYear, theMonth;
		
		arrDaysOfMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
		
		if( (theYear == "")||( theMonth == "") )
		{
				return 0;
		}
		
		theYear = parseInt(strYear);
		theMonth = parseInt(strMonth,10);
		
		if ((theMonth>12) || (theMonth<1))
		{
				return 0;
		}
		if (theMonth == 2 )
		{
			if ( ((theYear % 4 == 0) && (theYear %100 != 0)) || (theYear % 400 == 0) )
				return 29;
		}
		return arrDaysOfMonth [theMonth-1];

}

//validate year in range (1753-9999£¬also SqlServer valid range of year)
function isYearAvail(strYear,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(strYear)) return(!boolNotNull);
	
	if(!isPosInteger(strYear,true))
	{
		return false
	}
	
	var theYear;
	
	theYear = parseInt(strYear);
	if((theYear>9999) || (theYear<1753))
	{
		return false;
	}
	
	return true;
}

//Validate month(1-12)
function isMonthAvail(strMonth,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(strMonth)) return(!boolNotNull);
	
	if(!isPosInteger(strMonth,true))
	{
		return false
	}
	
	var theMonth;
	
	theMonth = parseInt(strMonth,10);
	if(theMonth>12 || theMonth<1)
	{
		return false;
	}
	
	return true;
}

//validate day(not exceed max number of days)
function isDayAvail(strYear,strMonth,strDay,boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(strDay)) return(!boolNotNull);
	
	if(!isPosInteger(strDay,true))
	{
		return false
	}
	
	var theDay;
	
	theDay		= parseInt(strDay,10);
	
	if(theDay>DaysOfMonth(strYear,strMonth) || theDay<1)
	{
		return false;
	}
	
	return true;
}

function isHourAvail(strHour, boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(strHour)) return(!boolNotNull);
	
	if(!isPosInteger(strHour))
	{
		return false;
	}

	var theHour;
	
	theHour = parseInt(strHour,10);
	if(theHour>24 || theHour<0)
	{
		return false;
	}
	
	return true;
}

function isMinuteAvail(strMinute, boolNotNull)
{
	if(boolNotNull!= true) boolNotNull = false; 
	if(isEmpty(strMinute)) return(!boolNotNull);
	
	if(!isInteger(strMinute))
	{
		return false;
	}

	var theMinute;
	
	theMinute = parseInt(strMinute,10);
	if(theMinute>=60 || theMinute<0)
	{
		return false;
	}
	
	return true;
}

/******************** other validation **********************/
//validate empty or null
function isEmpty(inputStr)
{
	if(inputStr==null || inputStr=="")
	{
		return true
	}
	
	var isBlank = true;
	for(var i = 0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if(oneChar==" ")
		{
			continue
		}
		else
		{
			isBlank = false;
		}
	}
	return isBlank;
}

//validate available date
function isDateAvail(strDate, boolNotNull)
{

	if(isEmpty(strDate) && !boolNotNull)
	{
		return true;	
	}
	else if(isEmpty(strDate) && boolNotNull)
	{
		return false;
	}
	else
	{
		var Year,Month,Day;
		var length = strDate.length;
		var beginPos = strDate.indexOf("-");
		var endPos = strDate.lastIndexOf("-");
		if(beginPos<=0 || endPos<=0)
		{
			return false;
		}
		Year = strDate.substr(0, beginPos);
		Month = strDate.substr(beginPos+1, endPos - beginPos - 1);
		Day = strDate.substr(endPos+1,length - endPos - 1);

		alert(Year);
		alert(Month);
		alert(Day);
		return(isYearAvail(Year, true) && isMonthAvail(Month, true) && isDayAvail(Year,Month,Day, true));
	}
}

//validate time

function isTimeAvail(strTime,boolNotNull)
{
	if(isEmpty(strTime) == "" && !boolNotNull)
	{
		return true;	
	}
	else if(isEmpty(strTime) && boolNotNull)
	{
		return false;
	}
	else
	{
		if(strTime.indexOf(":") <= 0 || strTime.indexOf(":") == strTime.length - 1)
		{
			return false;
		}
		else
		{
			var timeStr1,timeStr2;
			var length = strTime.length;
			var pos = strTime.indexOf(":");
			timeStr1 = strTime.substring(0,pos);
			timeStr2 = strTime.substring(pos + 1, length);
			if(!isInteger(timeStr1,true) && !isInteger(timeStr2,true))
			{
				return false;
			}
			
			
			var time1 = parseInt(timeStr1);
			var time2 = parseInt(timeStr2);
					
			if((time1 >= 0) && (time1<= 24) && (time2 >=0) && (time2 < 60))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	return false;
}

