//<!--

var whitespace = " \t\n\r";
var decPoint = ".";
var sepGroup = ",";

function GetIndex(arr, value) {
	var index = 0;
	while (index < arr.length) {
		if (arr[index] == value) break;
		index++;
	}
	if (index == arr.length) return -1;
	else return index;
}

function pad(s, len){
var i;
	while (s.length < Math.abs(len)) s = len < 0 ? s+'0' : '0'+s;
	return s;
}

function isEmail (s) {
	var reEmail = /^[A-Z_a-z0-9]+((\-|\.)?[A-Z_a-z0-9]+)*\@[A-Z_a-z0-9]+((\-|\.)?[A-Z_a-z]+[0-9]*)*\.[A-Z_a-z][A-Z_a-z]+$/;
	return (reEmail.test(s));
}

function isURL (s) {
// acc.to rfc1738 url is: scheme://machine.domain/full-path-of-file
	var scheme = "(https?:\\/\\/)";  // scheme HTTP is: http://(host):(port)/(path)?(searchpart)
	var machine = "\\w+((\\.|(\\-)+)\\w+)*";
	var domain = "[A-Za-z]{2,3}"
	var file_path = "(\\/[\\w\\.\\-]*)*";
	var arguments = "(\\?(.)*)?";
	var reURL = new RegExp("^" + scheme + "?(" + machine + "\\." + domain + ")?" + file_path + arguments + "$");
	return (reURL.test(s));
}

function isIP (s) {
	reIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
	if (reIP.test(s) &&
		(Number(RegExp.$1) < 256) &&
		(Number(RegExp.$2) < 256) &&
		(Number(RegExp.$3) < 256) &&
		(Number(RegExp.$4) < 256))
		return true;
	return false;
}

function isNumber(format, n){
	n = stripWhitespace(n.toString());
	reNumberFormat = new RegExp('^([\\+n])?[09]{1,3}(\\,[09]{3})*((\\.)[09]+)?$');
	if (!reNumberFormat.test(format)) {
		alert(error_msg['invalid-number-format']+': '+format+'\n\n'+error_msg['contact-administrator']);
		return null;
	}

	sign = RegExp.$1;
	group = sepGroup;
	decimals = RegExp.$3.length ? RegExp.$3.length - 1 : 0;
	point = decimals>0 ? decPoint : "";

	if (point && (point == group)) {
		alert(error_msg['invalid-number-format']+': '+format+'\n'+error_msg['invalid-number-separators']+'\n\n'+
			  error_msg['contact-administrator']);
		return null;
	}
	ptSign = "[\\+\\-]?";
	ptGroup = "\\d+";
	ptFract = "";
	if (group) ptGroup = "(\\d*)(\\"+group+"?\\d{3})*";
	if (point) ptFract = "(\\"+point+"\\d{0,"+decimals+"})?";
	reNumber = new RegExp("^(" + ptSign +")("+ ptGroup+ ")"+ ptFract + "$");
	if (reNumber.test(n)) {
		s = '';
		switch (sign) {
			case '' : s = (RegExp.$1 == '-') ? '-' : ''; break;
			case '+': s = (RegExp.$1 != '') ? RegExp.$1 : '+'; break;
		}

		modul = RegExp.$2.split(group);
		j=0; modGroups = new Array();
		for (i=modul.length; i>0; i--) {
			while(modul[i-1].length>3){
				modGroups[j] = modul[i-1].substr(modul[i-1].length-3,modul[i-1].length);
				modul[i-1] = modul[i-1].substr(0,modul[i-1].length-3);
				j++;
			}
			if (modul[i-1]) {
				modGroups[j]=modul[i-1];
				j++;
			}
		}

		modGroups = modGroups.reverse();
		grp = (m = modGroups.join('')) ? m : '0';
		group = (m = modGroups.join(group)) ? m : '0';

		frc = RegExp.$5.substr(1,RegExp.$5.length);
		fract = pad(frc, -decimals);
		fract = fract ? point + fract : fract;
		return  new Array(s+group+fract, Number(RegExp.$1+grp+'.'+frc));
	} else return null;
}

function formatDate(format, d, m, y, h, mi){
	var day, month, year, m3, m5;

	m3 = monthShort.join('|');
	m5 = months.join('|');

	ptYear  = new Array('Y{4}',   'Y{1,2}'  );
	reYear  = new Array('\\d{1,4}', '\\d{1,2}');

	ptMonth = new Array('M{2}',    'Month', 'Mon' );
	reMonth = new Array('\\d{1,2}', m5,      m3   );

	ptDay   = new Array('D{2}'    );
	reDay   = new Array('\\d{1,2}');

	delimiter = '([\\.\\-\\/\\s\\:])';
	
	time  	= '('+delimiter+'(H{2}|H{2}24?)'+delimiter+'(MI)'+delimiter+'?(S{2})?)';
	reTime  = delimiter+'(\\d{1,2})'+delimiter+'(\\d{1,2})('+delimiter+'\\d{1,2})?';

	year  = '('+ptYear.join('|')+')';
	month = '('+ptMonth.join('|')+')';
	day   = '('+ptDay.join('|')+')';

	reDateFormat= new RegExp(
		'^(('+year+delimiter+month+delimiter+day+time+'?)|'+
		'('+month+delimiter+year+delimiter+day+time+'?)|'+
		'('+month+delimiter+day+delimiter+year+time+'?)|'+
		'('+day+delimiter+month+delimiter+year+time+'?)|'+
		'('+day+delimiter+year+delimiter+month+time+'?)|'+
		'('+year+delimiter+day+delimiter+month+time+'?))$');

	if (!reDateFormat.test(format)) {
		alert(error_msg['invalid-date-format']+': '+format+'\n\n'+error_msg['contact-administrator']);
		return null;
	}

	p = new Array();
	p[format.search(year)]  = 'Y';
	p[format.search(month)] = 'M';
	p[format.search(day)]   = 'D';
	p[format.search(time)]  = 'T';

	j=1; result = new Array();
	for (i=0; i<p.length; i++) if (p[i]) {result[j]=p[i]; j+=2;}

	if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
		y = Number(y); m = Number(m);
		if (y <   50) y += 2000;
		if (y <  100) y += 1900;
		if (y < 1000) y += 1000;

		reYear[0] = y.toString();
		reYear[1] = reYear[0].substr(2,2);

		reMonth[0] =  pad(m.toString(),2);
		reMonth[1] = months[m-1];
		reMonth[2] = monthShort[m-1];

		reDay[0] =  pad(d.toString(),2);

		if (!isNaN(h) && !isNaN(mi)) {
			h = Number(h); mi = Number(mi);
			reTime = ' '+pad(h.toString(),2)+':'+pad(mi.toString(),2);
		} else reTime = '';

		delimiter='';
	}

	pattern = ptYear.concat(ptMonth,ptDay,time, delimiter ? '\\)'+delimiter+'\\(' : '');
	expr = reYear.concat(reMonth,reDay,reTime, ')'+delimiter+'(');
	for (i=0; i<pattern.length; i++) {
		if (pattern[i] != '') {
			if (delimiter && i!=pattern.length-1) expr[i] = '('+expr[i]+')';
			format = format.replace(new RegExp(pattern[i],'g'),expr[i]);
		}
	}

	result[0] = format;
	return result;
}

function isTime(str) {
	var reTime = /^(\d{1,2}):?(\d{1,2})$/;
	str = pad(str,4)
	if (reTime.test(str) &&	Number(RegExp.$1) < 24 && Number(RegExp.$2) < 60)
		return pad(RegExp.$1, 2)+':'+pad(RegExp.$2, 2);
	else return null;
}

function isDate(format, str) {
	var day, month, year, hour=0, minute=0;
	if (!(f = formatDate(format))) return null;

	reDate = new RegExp('^'+f[0]+'$');
//alert(str+'\n\n'+format+'\n\n'+f[0]);

	if (data = reDate.exec(str)) {
		for (i=1; i<f.length; i++) {
			switch (f[i]) {
				case 'Y': year = Number(data[i]);
				break;
				case 'M':
					month = data[i];
					switch (month.length) {
						case 1:	case 2:
							month = Number(month);
						break;
						case 3 : month=GetIndex(monthShort, month)+1;
						break;
						default : month=GetIndex(months, month)+1;
					}
				break;
				case 'D': day = Number(data[i]);
				break;
				
				case 'T': 
					hour=Number(data[i+1]);
					minute=Number(data[i+3]);
				break;
			}
		}
		if ((month > 12) || (month < 1)) return null;
		if ((day < 1) || (day > days[month-1])) return null;
		if ((month == 2) && (day == 29) && !(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))) return null;
		if (hour >= 24 && minute >= 60) return null;
	} else return null;
	date = formatDate(format,day,month,year,hour,minute);
	return new Array(date[0], day, month, year, hour, minute);
}

function isString(str) {
	if (isWhitespace(str))
		return "";
	var s = stripMeanlessWhitespaces(str);
//	alert(">"+s+"<");
	return s;
}
// Check whether string s is empty.
function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s) {
	var i;

    if (isEmpty(s))	return true;

	for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1)
        	return false;
    }
    return true;
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {

	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Removes all characters which do NOT appear in string bag
// from string s.
function stripCharsNotInBag (s, bag) {
	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

// Removes all whitespace characters from s.
function stripWhitespace (s) {
	return stripCharsInBag (s, whitespace);
}

// Removes all neighbour whitespace characters from s, changes tabs to spaces, removes initial and last whitespace
function stripMeanlessWhitespaces (str) {
	var i;
	var s = stripInitialWhitespace(str);
    var returnString = "";
    for (i = 0; i < s.length - 1; i++) {
        var c = s.charAt(i);
        var cn = s.charAt(i + 1);
        if (whitespace.indexOf(c) != -1) {
        	if (whitespace.indexOf(cn) != -1)
        		continue;
			c = " ";
		}
       	returnString += c;
    }
	if (whitespace.indexOf(s.charAt(i)) == -1) {
    	returnString += s.charAt(i);
	}
    return returnString
}

// Removes only leading whitespace characters from s.
function stripInitialWhitespace (s) {
	var i = 0;

    while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1)) i++;

    return s.substring (i, s.length);
}

//-->