function checktime(o){
if(o.value.length<5){
	if (o.value.search(/:/)==true){
	   temp=o.value
	   if(temp.length==4){
		temp="0"+temp
		o.value=temp
	}
	if(temp.length==3){
		temp="0"+temp+"0"
		      o.value=temp

                   }
                }else{
                    temp=o.value
                    if(temp.length==4){
                         temp1=temp.substr(0,2)+":"+temp.substr(2,2)
			 o.value=temp1
                    }
                    if(temp.length==3){
                         temp1="0"+temp.substr(0,1)+":"+temp.substr(1,2)
			 o.value=temp1
                    }
                    if(temp.length==2){
                         temp1=temp.substr(0,2)+":00"
			 o.value=temp1
                    }
                    if(temp.length==1){
                         temp1="0"+temp.substr(0,1)+":00"
			 o.value=temp1
                    }

                }


        }
		if(o.value.charAt(2)==":"){
			if(isNaN(o.value.substr(0,2)/1)==false){
				if((o.value.substr(0,2)/1)<23){
					if(isNaN(o.value.substr(3,2)/1)==false){
						if((o.value.substr(3,2)/1)<59){
							return true;
						}
					}
				}
			}
		}
	alert("This is not a recognised time value - \n Please re-enter it in the format \n HH:MM")
	return false;
}

function checkdate(objName,strDatestyle) {
var strMonthNames = new Array(12);
strMonthNames[0] = "Jan";
strMonthNames[1] = "Feb";
strMonthNames[2] = "Mar";
strMonthNames[3] = "Apr";
strMonthNames[4] = "May";
strMonthNames[5] = "Jun";
strMonthNames[6] = "Jul";
strMonthNames[7] = "Aug";
strMonthNames[8] = "Sep";
strMonthNames[9] = "Oct";
strMonthNames[10] = "Nov";
strMonthNames[11] = "Dec";
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var bFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;

if(strDatestyle==""){
	strDatestyle="DD/MM/YYYY"
}


	strDate = datefield.value;

	if (strDate.length < 1) {objName.value="  /  /    ";return true;}
	if (strDate == "  /  /    "){return true;}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				alert("Date must be entered in the format: "+strDatestyle)
				return false;
				}
			else {
				switch (strDatestyle)
				{
				case "DD/MM/YYYY":
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
					break;
				case "MM/DD/YYYY":
					strDay = strDateArray[1];
					strMonth = strDateArray[0];
					strYear = strDateArray[2];
					break;
				case "YYYY/MM/DD":
					strDay = strDateArray[2];
					strMonth = strDateArray[1];
					strYear = strDateArray[0];			
					break;
				}

			}
			bFound = true;
		}
	}
	if (bFound == false) {
		if (strDate.length>5) {
			switch (strDatestyle)
			{
			case "DD/MM/YYYY":
				strDay = strDate.substr(0, 2);
				strMonth = strDate.substr(2, 2);
				strYear = strDate.substr(4);
					break;
			case "MM/DD/YYYY":
				strMonth = strDate.substr(0, 2);
				strDay = strDate.substr(2, 2);
				strYear = strDate.substr(4);
					break;
			case "YYYY/MM/DD":
				if (strDate.length==6) {
					strYear=strDate.substr(0,2)
					strMonth=strDate.substr(2,2)
					strDay=strDate.substr(4)
				}
				else {
					strYear=strDate.substr(0,4)
					strMonth=strDate.substr(4,2)
					strDay=strDate.substr(6)
				}
					break;

			}
		}else{
				alert("Date must be entered in the format: "+strDatestyle)

		return false;
		}



	}
	if (strYear.length == 2) {
		datenow		=	new Date();
		datenow		=	datenow.getFullYear()

		datenow	=	datenow	+	""	
		strYear	=	datenow.substring(0,2)+strYear
		if(strYear>datenow+50){strYear-=100}
		if(strYear<datenow-50){strYear+=100}
	}


	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		alert("Day must be a number")
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthNames[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthNames[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
				alert("Month must be a number")

			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		alert("Year must be a number")
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		alert("Month must be between 1 and 12")
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		alert("Day must be between 1 and 31")

		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		alert("Day must be between 1 and 30")
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			alert("Day must be greater than 1")
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				alert("Day cannot be greater than 29")
				return false;
			}
		}
		else {
			if (intday > 28) {
				alert("Day cannot be greater than 28")
				return false;
			}
		}
	}

	intMonth	=	"0"+intMonth;				//  Make the month a 2 digit figure
	intMonth	=	intMonth.substring(intMonth.length-2,intMonth.length);
	intday		=	"0"+intday;				//  Make the day a 2 digit figure
	intday		=	intday.substring(intday.length-2,intday.length);

	switch (strDatestyle)
	{
	case "DD/MM/YYYY":
		datefield.value = intday + "/" + intMonth + "/" + strYear;
		break;
	case "MM/DD/YYYY":
		datefield.value = intMonth + "/" + intday + "/" + strYear;
		break;
	case "YYYY/MM/DD":
		datefield.value = strYear + "/" + intMonth + "/" + intday;
		break;

	}


	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
function uppercase(o){
	var strval = o.value;
	o.value=strval.toUpperCase(strval);
}

function number_valid(objName,inputMask,lowerrange,upperrange){
var checkOK = "0123456789-., ";
var checkStr 
var allValid = true;
var decPoints = 0;
var allNum = "";
var floatval
if(objName){
	checkStr= objName.value;
if(objName.value=="NaN"){objName.value="0"}
if(lowerrange==""){lowerrange=0}
if(upperrange==""){upperrange=999999}
if (inputMask == null){inputMask=99999999.99}
for (i = 0;  i < checkStr.length;  i++){
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length){
		allValid = false;
		break;
	}
	if (ch == "."){
		allNum += ".";
		decPoints++;
	}else if (ch != ",")
		allNum += ch;
}
if (!allValid){
    if(option!="view"){
	alert("Please enter a valid number");
    }
	return (false);
}
if (decPoints > 1){
    if(option!="view"){
	alert("Please enter a valid number");
    }
	return (false);
}

var chkVal = allNum;
var prsVal = parseFloat(allNum);
var requiredDecimals;
var currentDecimals;
var AddOnDecimals;

if (chkVal != "" && !(prsVal >= (inputMask*-1) && prsVal <= inputMask)){
    if(option!="view"){
	alert("Not a valid number.  Enter a number in the range -"+inputMask+" upto "+inputMask+".");
	return (false);
    }
}

for (i = 0;  i < inputMask.length;  i++){
	if (inputMask.substr(i,1)=="."){requiredDecimals = inputMask.length-(i+1)}
}
if (requiredDecimals == null){requiredDecimals="0"}

checkStr = Math.round(checkStr*Math.pow(10,requiredDecimals))/Math.pow(10,requiredDecimals)+""

for (i = 0;  i < checkStr.length;  i++){
	if (checkStr.substr(i,1)=="."){currentDecimals = checkStr.length-(i+1)}
}
if (currentDecimals == null){currentDecimals="0"}
if (requiredDecimals>currentDecimals){	//we need to add on some zero's
	for (i = 0; i < requiredDecimals-currentDecimals; i++){
		if (AddOnDecimals==null){AddOnDecimals="0"}
		else{AddOnDecimals=AddOnDecimals+"0"}
	}
	if (currentDecimals==0){AddOnDecimals="."+AddOnDecimals}	//add on the decimal point if we don't have one already
	checkStr = checkStr+AddOnDecimals
}


floatval=parseFloat(checkStr)
if(floatval<lowerrange){
   return(false)

}
if(floatval>upperrange){
   return(false)

}

objName.value = checkStr

}
return (true);
}

function addWeeks(cDate,nWeeks)
{


if(cDate=="  /  /    "){
   return "  /  /    "
}
else {
//alert(cDate)

   nDay=(cDate.substr(0,2))
   nMonth=(cDate.substr(3,2))


   nYear=(cDate.substr(6,4))

//alert(nDay+" "+nMonth+" "+nYear)
 
   var dNew=new Date(nYear,nMonth-1,nDay)
//alert(dNew)
   
   dNew.addWeeks(nWeeks)
//alert(dNew)

   cDay=""+dNew.getDate()
   cMonth=""+(dNew.getMonth()+1)
   cYear=""+dNew.getFullYear()
   if(cDay.length==1) { cDay="0"+cDay}
   if(cMonth.length==1) { cMonth="0"+cMonth}
   
   cDate=cDay+"/"+cMonth+"/"+cYear

   
return cDate

}
}

Date.prototype.addDays = function(d) {
  this.setDate( this.getDate() + d );
};

Date.prototype.addWeeks = function(w) {
  this.addDays(w * 7);
};

Date.prototype.addMonths= function(m) {
  var d = this.getDate();
  this.setMonth(this.getMonth() + m);

  if (this.getDate() < d)
    this.setDate(0);
};

Date.prototype.addYears = function(y) {
  var m = this.getMonth();
  this.setFullYear(this.getFullYear() + y);

  if (m < this.getMonth()) {
    this.setDate(0);
  }
};

Date.prototype.copy = function () {
  return new Date(this.getTime());
};
