// CONSTANTS

var ACCENTS_TRANSLATE_MAP = { 
				"\u0386" : "\u0391", // GREEK CAPITAL ALPHA
				"\u03ac" : "\u03b1", // GREEK ALPHA
				"\u0388" : "\u0395", // GREEK CAPITAL EPSILON
				"\u03ad" : "\u03b5", // GREEK EPSILON
				"\u0389" : "\u0397", // GREEK CAPITAL HTA
				"\u03ae" : "\u03b7", // GREEK HTA
				"\u038a" : "\u0399", "\u03aa" : "\u0399", // GREEK CAPITAL GIOTA
				"\u03af" : "\u03b9", "\u03ca" : "\u03b9", "\u0390" : "\u03b9", // GREEK GIOTA
				"\u038c" : "\u039f", // GREEK CAPITAL OMICRON
				"\u03cc" : "\u03bf", // GREEK OMICRON
				"\u038e" : "\u03a5", "\u03ab" : "\u03a5", // GREEK CAPITAL YPSILON
				"\u03cd" : "\u03c5", "\u03cb" : "\u03c5", "\u03b0" : "\u03c5", // GREEK YPSILON
				"\u038f" : "\u03a9", // GREEK CAPITAL OMEGA
				"\u03ce" : "\u03c9" // GREEK OMEGA
			  };

// localized CURRENCY_SIGN, CURRENCY_NMTOKEN MONTH_NAMES, MONTH_ABBRVS, DAY_NAMES, 
// DAY_ABBRVS must be defined in page as script

var NBSP = '\u00a0'; // nbsp

function startsWith(str1, str2) {
	if(str1 && str2 && str2.length > 0 && str2.length <= str1.length && str1.substr(0, str2.length) == str2)
		return true;
	return false;
}

function endsWith(str1, str2) {
	if(str1 && str2 && str2.length > 0 && str2.length <= str1.length && str1.substr(str1.length - str2.length) == str2)
		return true;
	return false;
}

function trim(str) {
	var len = str.length;
	var st = 0;
	while ((st < len) && (str.charAt(st) <= ' '))
	    st++;
	while ((st < len) && (str.charAt(len - 1) <= ' '))
	    len--;
	return ((st > 0) || (len < str.length)) ? str.substring(st, len) : str;
}

function leftPad(str, length, padChar) {
	while(str.length < length)
		str = padChar + str;
	return str;
}

function rightPad(str, length, padChar) {
	while(str.length < length)
		str = str + padChar;
	return str;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++)
		if (chars.indexOf(input.charAt(i)) == -1)
			output += input.charAt(i);
	return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
	input = "" + input;
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++) {
		if (i != 0 && (input.length - i) % 3 == 0) output += separator;
			output += input.charAt(i);
	}
	return output;
}

function removeAccent(str) {
	var newstr = '';
	for(var i = 0; i < str.length; i++)
		if(ACCENTS_TRANSLATE_MAP[str.charAt(i)])
			newstr += ACCENTS_TRANSLATE_MAP[str.charAt(i)];
		else
			newstr += str.charAt(i);
	return newstr;
}

function formatNumber(number, format) {  // use: formatNumber(number, "format")

	number = changeDecimalSeparator(number, DECIMAL_SEPARATOR, '.');

	if (number - 0 != number) return null;  // if number is NaN return null

	var useSeparator = format.indexOf(THOUSANDS_SEPARATOR) != -1;  // use separators in number
	var usePercent = format.indexOf(PERCENT_SIGN) != -1;  // convert output to percentage
	var useCurrency = format.indexOf(CURRENCY_SYMBOL) != -1;  // use currency format
	var isNegative = (number < 0);
	
	number = Math.abs(number);
	if (usePercent) number *= 100;
	format = strip(format, THOUSANDS_SEPARATOR + PERCENT_SIGN + CURRENCY_SYMBOL);  // remove key characters
	number = "" + number;  // convert number input to string

	// split input value into LHS and RHS using DECIMAL_SEPARATOR as divider
	var dec = number.indexOf('.') != -1;
	var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
	var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

	// split format string into LHS and RHS using DECIMAL_SEPARATOR as divider
	dec = format.indexOf(DECIMAL_SEPARATOR) != -1;
	var sleftEnd = (dec) ? format.substring(0, format.indexOf(DECIMAL_SEPARATOR)) : format;
	var srightEnd = (dec) ? format.substring(format.indexOf(DECIMAL_SEPARATOR) + 1) : "";

	// adjust decimal places by cropping or adding zeros to LHS of number
	if (srightEnd.length < nrightEnd.length) {
		var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
		nrightEnd = nrightEnd.substring(0, srightEnd.length);
		if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

		while (srightEnd.length > nrightEnd.length) {
			nrightEnd = "0" + nrightEnd;
		}

		if (srightEnd.length < nrightEnd.length) {
			nrightEnd = nrightEnd.substring(1);
			nleftEnd = (nleftEnd - 0) + 1;
		}
	} else {
		for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
			if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
			else break;
		}
	}

	// adjust leading zeros
	sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
	while (sleftEnd.length > nleftEnd.length) {
		nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
	}

	if (useSeparator) nleftEnd = separate(nleftEnd, THOUSANDS_SEPARATOR);  // add separator
	var output = nleftEnd + ((nrightEnd != "") ? DECIMAL_SEPARATOR + nrightEnd : "");  // combine parts
	if (isNegative) {
		// patch suggested by Tom Denn 25/4/2001
//		output = (useCurrency) ? "(" + output + ")" : "-" + output;
		output = '-' + output;
	}
	output = ((useCurrency) ? CURRENCY_SYMBOL + ' ' : "") + output + ((usePercent) ? PERCENT_SIGN : "");
	return output;
}

function unFormatNumber(number) {
	number = "" + number;  // convert number input to string
	number = strip(number, THOUSANDS_SEPARATOR + PERCENT_SIGN + CURRENCY_SYMBOL);
	return number;
}

function changeDecimalSeparator(number, separator1, separator2) {
	number = "" + number;  // convert number input to string
	if(separator1 != separator2) {
		var dec = number.indexOf(separator1);  // use separators in number
		if(dec != -1) {
			number = number.substring(0, dec) + separator2 +  number.substring(dec + 1);
		}
	}
	return number;
}

function numberValue(number) {
	return changeDecimalSeparator(unFormatNumber(number), DECIMAL_SEPARATOR, '.');
}

function hasClass(classname, clazz) {
	return (classname && classname.indexOf(clazz) != -1)
}

function addClass(classname, clazz) {
	if((pos = classname.indexOf(clazz)) < 0) {
		return classname + ' ' + clazz;
	}
	return classname;
}

function removeClass(classname, clazz) {
	if((pos = classname.indexOf(clazz)) >= 0) {
		if(pos > 0)
			return classname.substr(0, pos - 1) + classname.substr(pos + clazz.length);
		else
			return classname.substr(clazz.length + 1);
	}
	return classname;
}

function replaceClass(classname, newclazz, oldclazz) {
	if((pos = classname.indexOf(oldclazz)) >= 0) {
		if(pos > 0)
			return classname.substr(0, pos - 1) + ' ' + newclazz + classname.substr(pos + oldclazz.length);
		else
			return newclazz + ' ' + classname.substr(oldclazz.length + 1);
	} else {
		if(classname.length == 0)
			return newclazz;
		else
			return classname + ' ' + newclazz;
	}
	return classname;
}

function changeClass(classname, clazz, on) {
	if(on) 
		return addClass(classname, clazz);
	else
		return removeClass(classname, clazz);
}

function addStr(initStr, addition) {
	if((pos = initStr.indexOf(addition)) < 0) {
		if (initStr.length>0)
		 return initStr+','+addition;
	  else 
		 return initStr+addition;
	}
	return initStr;
}

function removeStr(initStr, removal) {
	if((pos = initStr.indexOf(removal)) >= 0) {
		if(pos > 0) 
			return initStr.substr(0, pos - 1) + initStr.substr(pos + removal.length);
		else
			return initStr.substr(removal.length + 1);
	}
	
	return initStr;
}
