/***************************************************/
/*												   */
/*	Validation Script/Functions By Kevin Hamilton  */
/*												   */
/*	    Ask me if you need any help with them      */
/*              kevin@skillsite.co.uk              */
/*												   */
/***************************************************/

function checkint(inputStr, msg) {
	var errchar = 0;
	var inputvalue = inputStr.value.toString();
	for (c = 0; c < inputvalue.length; c++) {
		var oneChar = inputvalue.charAt(c);
		if (oneChar < "0" || oneChar > "9") {
			errchar++;
		}
	}
	if(errchar > 0) {
		if(!msg) {
			alert("Please enter a valid integer or ID");
			inputStr.focus();
		}
		else {
			alert(msg);
			inputStr.focus();
		}
	}
}

function checktime(inputTime,searchable) {
	if(inputTime.value == '') return;
	var now = new Date();
	var time = inputTime.value;
	var hours; var mins;
	if(time.indexOf(':') == -1) {
		if(parseInt(time) < 24 && parseInt(time) >= 0) {
			time = time+':00';
		}
		else {
			alert('Problem\nCannot find \':\' time separator');
			inputTime.focus();
			return;
		}
	}
	if(time.charAt(0)==':') {
		var hours = now.getHours();
		hours = hours.toString();
		time = hours+time;
	}
	if(time.charAt(time.length-1) == ':') {
		var mins = now.getMinutes();
		mins = mins.toString();
		time = time+mins;
	}
	timesplit = time.split(':');
	if(timesplit[0] > 23 || timesplit[0] < 0) {
		alert('Problem\n'+timesplit[0]+' is not a valid hour!');
		inputTime.focus();
		return;
	}
	if(timesplit[1] > 59 || timesplit[1] < 0) {
		alert('Problem\n'+timesplit[1]+' is not a valid minute!');
		inputTime.focus();
		return;
	}
	timesplit[0] = timesplit[0].toString();
	timesplit[1] = timesplit[1].toString();
	if(timesplit[0].length==1) timesplit[0] = '0'+timesplit[0];
	if(timesplit[1].length==1) timesplit[1] = '0'+timesplit[1];
	inputTime.value = timesplit[0]+':'+timesplit[1];
}

function checkdate(inputDate, searchable, msg) { // to format any date to correct date format i.e. 10-MAY-2001
	inputval = inputDate.value.toUpperCase();    // 
	if (inputval == '') {						 // if 'searchable' is true will ignore strings with '%'
		return;									 // 
	}
	if(inputval.indexOf('%') != -1 && searchable) {
		inputDate.value = inputval;
		return;
	}
	var allthemonths = Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
	var yr = new Date();
	if(inputval.length <= 2) {
		inputval = parseInt(inputval);
		inputval = inputval.toString() + '-' + allthemonths[yr.getMonth()] + '-' + yr.getFullYear();
	}
	var xdate = 0; 
	var errmsg = 'Following Errors Occured:\n--------------------------------\n';
	var testlength = 'DD-MON-YYYY';
	var testlengthtime = 'DD-MON-YYYY HH:MI';
	var errchars = Array("/","\\",".","|","=",',',' ','*'); 
	for(i=0;i<=errchars.length-1;i++) {
		if(inputval.indexOf(errchars[i])>0) {
			var one = inputval.substring(0,inputval.indexOf(errchars[i]));
			var two = inputval.substring(inputval.indexOf(errchars[i])+1,inputval.length);
			inputval = one+'-'+two;
		}		
		if(inputval.lastIndexOf(errchars[i])>0) {
			var one = inputval.substring(0,inputval.lastIndexOf(errchars[i]));
			var two = inputval.substring(inputval.lastIndexOf(errchars[i])+1,inputval.length);
			inputval = one+'-'+two;			
		}
	}
	if(inputval.indexOf('-') == inputval.lastIndexOf('-')) {
		inputval = inputval+'-'+yr.getFullYear();
	}

	//Validate Day
	var theChar = inputval.substring(0,inputval.indexOf('-'));
	if(theChar.length==2 && theChar.charAt(0) == '0') {
		theChar = theChar.charAt(1);
		//alert(theChar)
	}
	theChar = parseInt(theChar);
		if(!(theChar > 0 && theChar < 32)) {
			xdate++;
			errmsg += '  - I couldn\'t figure out what day you put, so I\'ve put in today. : '+theChar+'\n';
			inputval = yr.getDate() + inputval.substring(inputval.indexOf('-'),inputval.length);
		}
		else {
			inputval = theChar + inputval.substring(inputval.indexOf('-'),inputval.length);
		}
	if(inputval.substring(0,inputval.indexOf('-')).length==1) { //if day only 1 char make it 2
		inputval = "0" + inputval; 
	}


	//Do Month
	var theChar = inputval.substring(inputval.indexOf('-')+1,inputval.lastIndexOf('-'));
	if(theChar.length==2 && theChar.charAt(0) == '0') {
		theChar = theChar.charAt(1);
	}
	if(!(theChar == "JAN" || theChar == "FEB" || theChar == "MAR" || theChar == "APR" || theChar == "MAY" || theChar == "JUN" || theChar == "JUL" || theChar == "AUG" || theChar == "SEP" || theChar == "OCT" || theChar == "NOV" || theChar == "DEC")) { //confirm the month is a 3 char str
		if(theChar == "JANUARY" || theChar == "FEBUARY" || theChar == "MARCH" || theChar == "APRIL" || theChar == "MAY" || theChar == "JUNE" || theChar == "JULY" || theChar == "AUGUST" || theChar == "SEPTEMBER" || theChar == "OCTOBER" || theChar == "NOVEMBER" || theChar == "DECEMBER") {
			inputval = inputval.substring(0,inputval.indexOf('-')+1) + theChar.substring(0,3) + inputval.substring(inputval.lastIndexOf('-'),inputval.length);
		}
		else if(parseInt(theChar)>0 && parseInt(theChar)<13) {
			inputval = inputval.substring(0,inputval.indexOf('-')+1) + allthemonths[parseInt(theChar)-1].toString() + inputval.substring(inputval.lastIndexOf('-'),inputval.length);
		}
		else {
			xdate++;
			errmsg += '  - I have no idea what month you wanted, I\'ve put in this month instead : '+theChar+'\n';
			inputval = inputval.substring(0,inputval.indexOf('-')+1) + allthemonths[yr.getMonth()] + inputval.substring(inputval.lastIndexOf('-'),inputval.length);
		}
	}
	if(inputval.charAt(2) != '-' || inputval.charAt(6) != '-') {		// look for the separator
		xdate++;
		errmsg += '  - I havn\'t reconised a date separator. '+inputval.charAt(2)+'\n';
	}

	
	//Check Year
	var theChar = parseInt(inputval.substring(inputval.lastIndexOf('-')+1,inputval.length));
		if(theChar < 1000 || !theChar) {			// and then check that we have a 4 digit year
			if(theChar <= 30) {					// if not work it out!
				var newyear = 2000 + theChar;	// 
				newyear = newyear.toString();
				inputval = inputval.substring(0,inputval.lastIndexOf('-')+1) + newyear;
			}
			else if(theChar > 30 && theChar < 100) {
				var newyear = 1900 + theChar;
				newyear = newyear.toString()
				inputval = inputval.substring(0,inputval.lastIndexOf('-')+1) + newyear;
			}
			else {
				xdate++;
				errmsg += '  - I couldn\'t work out what year you want. This year is there how. '+theChar+'\n';
				inputval = inputval.substring(0,inputval.lastIndexOf('-')+1) + yr.getFullYear();
			}
		}
	if(inputval.substring(0,inputval.indexOf('-')).length==1) { //if day only 1 char make it 2
		inputval = "0" + inputval; 
	}
	if(inputval.length != testlength.length && inputval.length != testlengthtime.length) {			//first check that the date length is right, else the date *will* be wrong
		inputval = inputval.substring(0,inputval.length-1);
		xdate++;
		errmsg += '  - For some reason the date is to long or short. '+inputval.length+'\n';
	}

	if(xdate != 0) {
		if(!msg) {
			//alert("The Date is not valid");
			alert(errmsg);
			inputDate.focus();
		}
		else {
			alert(msg);
			inputDate.focus();
		}
		inputDate.value = inputval;
	}
	else {
		inputDate.value = inputval;
	}
}

function checkmand(objectname) {
	if (objectname.value == "") {
		alert("This field is mandatory - please give a value");
		objectname.focus();
	}
}

function round(num,places) {
	tplaces = places ;
	tplaces = Math.pow(10 ,tplaces);
	bignum = Math.round(num * tplaces);
	return bignum/tplaces;
}

