// 
// ------------------------------------------------------------------
// FindAndreplace(text, find, replace)
// Replaces all occurences of string "find" in "text" with "replace"
// ------------------------------------------------------------------
function FindAndReplace(myText, find, replace) {
    var result = myText.split(find).join(replace);
    return result;
}


function Trim(orgString){
  return LTrim(RTrim(orgString))
}

function LTrim(orgString){
  return orgString.replace(/^\s+/,'')
}

function RTrim(orgString){
  return orgString.replace(/\s+$/,'')
}

// 
// ------------------------------------------------------------------
// searchQuery(words)
// Strips words which are not allowed in search term
// ------------------------------------------------------------------
function searchQuery(words) {
        words=Trim(words);
        words=FindAndReplace(words," or "," ");
        words=FindAndReplace(words," Or "," ");
        words=FindAndReplace(words," OR "," ");
        words=FindAndReplace(words," and "," ");
        words=FindAndReplace(words," And "," ");
        words=FindAndReplace(words," AND "," ");
        words=FindAndReplace(words,"\"","");
        words=FindAndReplace(words,"\'","");
        words=FindAndReplace(words,"&","");
        words=FindAndReplace(words,"\\","");
        words=FindAndReplace(words,"   "," ");
        words=FindAndReplace(words,"  "," ");
        words=FindAndReplace(words," "," AND ");
		return words;
}

// ------------------------------------------------------------------
// checkEmail(src)
// validates mail address
// returns true if valid, false if invalid
// ------------------------------------------------------------------
function checkEMail(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
 }

// ------------------------------------------------------------------
// formatNumber(number, pattern, debug)
// Formats currency, percentage or any other number
// @param number The number 
// @param format The pattern (use ###,##0.00)
// @param print if true prints debug info
// @returns formatted number
// ------------------------------------------------------------------
// U can set the seperator, decimal point
var OP_separator = ",";  // use comma as 000's separator
var OP_decpoint = ".";  // use period as decimal point
var OP_percent = "%";
var OP_currency = "";

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")


    if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

    if (number - 0 != number) return null;  // if number is NaN return null
    var useSeparator = format.indexOf(OP_separator) != -1;  // use separators in number
    var usePercent = format.indexOf(OP_percent) != -1;  // convert output to percentage
    var useCurrency = format.indexOf(OP_currency) != -1;  // use currency format
    var isNegative = (number < 0);
    number = Math.abs (number);
    if (usePercent) number *= 100;
    format = stripCharacters(format, OP_separator + OP_percent + OP_currency);  // remove key characters
    number = "" + number;  // convert number input to string

     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(OP_decpoint) != -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 decpoint as divider
    dec = format.indexOf(OP_decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 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

 // patch provided by Patti Marcoux 1999/08/06
      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 = stripCharacters(sleftEnd, "#");  // remove hashes from LHS of format
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) nleftEnd = formatNumberSeparate(nleftEnd, OP_separator);  // add separator
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
    output = ((useCurrency) ? OP_currency : "") + output + ((usePercent) ? OP_percent : "");
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
    return output;
}


// ------------------------------------------------------------------
// formatNumberSeparate ( string, separator_character )
//  format input using 'separator' to mark 000's
// ------------------------------------------------------------------
function formatNumberSeparate(input, separator) {  
    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;
}


// ------------------------------------------------------------------
// stripCharacters ( string, characters )
// strip all characters in the 2nd parameter from the 1st parameter
// ------------------------------------------------------------------
function stripCharacters(input, chars) {  
    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;
}


// ------------------------------------------------------------------
// format(number, decimal_places)
// Formats number with decimal point (no other formatting characters)
// returns true if valid, false if invalid
// ------------------------------------------------------------------
function format (expr, decplaces) {
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) {
		str = "0" + str
	}
	// establish location of decimal point
	var decpoint = str.length - decplaces
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}


// 
// ------------------------------------------------------------------
// SplitString(string, separator_string)
// Deprecated
// ------------------------------------------------------------------

function SplitString(str, separator) {
	var result = new MakeArray(0);
	if (!str) {result[1] = str; return result;}

    	var s = str.split(separator);
	for (var i = 0; i < s.length; i++) result.append(s[i]);
         return result;
}


// 
// ------------------------------------------------------------------
// ReplaceToken(text, find, replace)
// Deprecated: has error, because it adds an extra replacement token at the end
// ------------------------------------------------------------------
function ReplaceToken(str, t, _t) {
	var strs = str.split(t), _output = "";
	for (var i = 0; i < strs.length; i++)	
            if (i < strs.length)
                  _output += strs[i] + _t;
	return _output;
}

