function isint(strvalue) {
	for (var i = 0; i < strvalue.length; i++) {
		if ((strvalue.charAt(i) < '0') || (strvalue.charAt(i) > '9')) return false;
	}
	return true;
}

function Trim(TRIM_VALUE){
	if (TRIM_VALUE.length < 1) return "";
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	return TRIM_VALUE;
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if (v_length < 0) return "";
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space);
		else {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if (v_length < 1) return "";
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if (VALUE.charAt(iTemp) == w_space) ;
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

function isdatetime(dtstr) {
	//check if the string contain 2 datetime
	var dt2 = dtstr.split('&');
	if (dt2.length > 2) return false;
	if (dt2.length == 2) return ( isdatetime(Trim(dt2[0])) && isdatetime(Trim(dt2[1])) );

	//check if the string contain both date & time
	var dt = dtstr.split(' ');
	var thedate = '';
	var thetime = '';
	if (dt.length < 2) return false;
	else if (dt.length == 2) {
		thedate = Trim(dt[0]);
		thetime = Trim(dt[1]);
	} else {
		for (i = 0; i < dt.length; i++) {
			if (Trim(dt[i]) != '') {
				if (thedate == '') thedate = Trim(dt[i]);
				else if (thetime == '') thetime = Trim(dt[i]);
				else return false;
			}
		}
	}
	if ((thedate == '') || (thetime == '')) return false;

	//check if the date is yyyy-mm-dd
	var ymd = thedate.split('-');
	if (ymd.length != 3) return false;

	//check if year is 0000 ~ 9999
	if (ymd[0].length != 4) return false;
	if (! isint(ymd[0])) return false;
	var yr = 1 * ymd[0];

	// check if month is 01-12
	if (ymd[1].length != 2) return false;
	if (! isint(ymd[1])) return false;
	if ( ((1*ymd[1]) < 1) || ((1*ymd[1]) > 12 ) ) return false;
	var mth = 1 * ymd[1];

	//check if day is 01-31
	if (ymd[2].length != 2) return false;
	if (! isint(ymd[2])) return false;
	if ( ((1*ymd[2]) < 1) || ((1*ymd[2]) > 31 ) ) return false;
	var dy = 1 * ymd[2];

	//check if day is small than the maximum possible day in that month
	var maxday = 30;
	if ((mth == 1) || (mth == 3) || (mth == 5) || (mth == 7) || (mth == 8) || (mth == 10) || (mth == 12))
		maxday = 31;
	else if (mth == 2) {
		if ((((yr % 4) == 0) && ((yr % 100) != 0)) || ((yr % 400) == 0)) maxday = 29;
		else maxday = 28;
	}
	if (dy > maxday) return false;
	
	var hms = thetime.split(':');
	if (!(isint(hms[0]) && isint(hms[1]) && isint(hms[2]))) return false;
	var hr = 1*hms[0];
	var mn = 1*hms[1];
	var scd = 1*hms[2];
	if ((hr < 0) || (hr > 23) || (mn < 0) || (mn > 59) || (scd < 0) || (scd > 59)) return false;

	return true;
}

function isNumber(numstr) {
	if (Trim(numstr) == '') return false;
	try {
		var t = 1.0 * numstr;
	} catch(e) {
		return false;
	}
	return (! isNaN(t));
}

function validation() {
	if (document.getElementById('catalog1').checked || document.getElementById('catalog2').checked) ;
	else {
		alert('You have to choose at least 1 catalog service!');
		return false;
	}
	
	var w = document.getElementById('west').value;
	var s = document.getElementById('south').value;
	var e = document.getElementById('east').value;
	var n = document.getElementById('north').value;
	if ( ((w == '') && (s == '') && (e == '') && (n == '')) || document.getElementById('nbbox').checked
		|| (isNumber(w) && isNumber(s) && isNumber(e) && isNumber(n))  );
	else {
		alert('The BBox in "Spatial" page is not valid! ');
		return false;
	}
	
	if ((document.getElementById('ptv').value != '') &&
		((! isdatetime(document.getElementById('ptv').value)) ||
		((document.getElementById('ptv').value.indexOf('&') >= 0) && 
		(document.getElementById('ptc').value != '><')) ||
		((document.getElementById('ptv').value.indexOf('&') <= 0) &&
		(document.getElementById('ptc').value == '><'))
		)) {
		alert('Your produce datetime is not valid!');
		return false;
	}
	if ((document.getElementById('bdtv').value != '') &&
		((! isdatetime(document.getElementById('bdtv').value)) ||
		((document.getElementById('bdtv').value.indexOf('&') >= 0) && 
		(document.getElementById('bdtc').value != '><')) ||
		((document.getElementById('bdtv').value.indexOf('&') <= 0) &&
		(document.getElementById('bdtc').value == '><'))
		)) {
		alert('Your begin datetime is not valid!');
		return false;
	}
	if ((document.getElementById('edtv').value != '') &&
		((! isdatetime(document.getElementById('edtv').value)) ||
		((document.getElementById('edtv').value.indexOf('&') >= 0) && 
		(document.getElementById('edtc').value != '><')) ||
		((document.getElementById('edtv').value.indexOf('&') <= 0) &&
		(document.getElementById('edtc').value == '><'))
		)) {
		alert('Your end datetime is not valid!');
		return false;
	}

	return true;
}
