function correctField(fieldRef, stripSpaces)
{
	var newValue = replace(fieldRef.value, "\\", "");
	newValue = replace(newValue, "'", "");
	newValue = replace(newValue, '"', "");

	if (stripSpaces)
		newValue = replace(newValue, " ", "");

	fieldRef.value = newValue;

	return false;
}

function formatNumber(number, dp)
{
    var newNumber = number.toFixed(dp);
    
    if (newNumber.substring(0, 1) == ".")
    {
        newNumber = "0" + newNumber;
    }
    
    return newNumber;
}

function isNumeric(strString, allowDecimal, allowNegative)	
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	if (allowDecimal)
	{
		strValidChars = strValidChars + ".";
		strValidChars = strValidChars + ",";
	}
	
	if (allowNegative)
	{
		strValidChars = strValidChars + "-";
	}

	if (strString.length == 0) return false;

	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function forceUpperCase(field)
{
    field.value = field.value.toUpperCase();
    
    return false;
}

function forceUpperAlphaNumeric(field)
{
    var temp = field.value;
    
    temp = temp.toUpperCase();
    temp = temp.replace(/[^A-Z_0-9\-]+/g, "");
    
    field.value = temp;
}

function forceAlphaNumeric(field)
{
    var temp = field.value;
    
    temp = temp.replace(/[^a-zA-Z_0-9\-]+/g, "");
    
    field.value = temp;
}

function forceNumeric(field, allowDecimal, allowNegative)
{
	if (! isNumeric(field.value, allowDecimal, allowNegative))
	{
	    field.value = "0";
	}
	
	return false;
}

function forceNumericOrEmpty(field, allowDecimal, allowNegative)
{
	if (! isNumeric(field.value, allowDecimal, allowNegative))
	{
		field.value = "";
	}
	
	return false;
}

function forceNumericOrMinValue(field, allowDecimal, allowNegative, defaultValue)
{
	if (! isNumeric(field.value, allowDecimal, allowNegative))
	{
		field.value = defaultValue;
	}
	else
	{
	    var theNumber = parseFloat(field.value);
	    if (theNumber < defaultValue)
	    {
		    field.value = defaultValue;	    
	    }
	}
	
	return false;
}

function forceNumericOrValue(field, allowDecimal, allowNegative, defaultValue)
{
	if (! isNumeric(field.value, allowDecimal, allowNegative))
	{
		field.value = defaultValue;
	}
	
	return false;
}

function roundNumber(Num, Places)
{
    if (Places > 0) 
    {
        var numAsString = Num.toString();
        if (numAsString.lastIndexOf('.') == -1)
        {
            var theNumber = parseFloat(Num);
            return theNumber.toFixed(Places);
        }
        if ((numAsString.length - numAsString.lastIndexOf('.')) > (Places + 1)) 
        {
            var Rounder = Math.pow(10, Places);
            var theNumber = Math.round(Num * Rounder) / Rounder;
            return theNumber.toFixed(Places);
        }
        else if ((numAsString.length - numAsString.lastIndexOf('.')) < (Places + 1)) 
        {
            var theNumber = parseFloat(Num);
            return theNumber.toFixed(Places);
        }
        else 
            return Num;
    }
    else 
        return Math.round(Num);
}

function forceDP(field, allowNegative, Places)
{
    forceNumeric(field, true, allowNegative);
    field.value = roundNumber(field.value, Places);
}

function validateEmailAddress(pEmailAddress)
{
    var validAddress = true;
    
    var atPos = pEmailAddress.indexOf("@")
    var stopPos = pEmailAddress.lastIndexOf(".")

    if (pEmailAddress == "") 
    {
        validAddress = false;
    }
    else
    {
        if (atPos == -1 || stopPos == -1) 
        {
            validAddress = false;
        }
        
        if (stopPos < atPos) 
        {
            validAddress = false;
        }
        
        if (stopPos - atPos == 1) 
        {
            validAddress = false;
        }
    }
    
    return validAddress;
}

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
function ArrayIndexOf(source, v) 
{
    for (var i = 0, l = source.length; i < l; i++) 
    {
        if (source[i] == v) 
        {
            return i;
        }
    }
    return -1;
}

function ltrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function trim(str, chars)
{
    return ltrim(rtrim(str, chars), chars);
}

function enterKeyPressed(e) 
{
  var characterCode
  if(e && e.which)
  {           // NN4 specific code
    e = e
    characterCode = e.which
  }
  else 
  {
    e = event
    characterCode = e.keyCode // IE specific code
  }
  if (characterCode == 13)
  {
    return true;   // Enter key is 13
  }
  else
  {
    return false;
  }
}

function changeSystemLanguage(pFuseBoxAction)
{
    var theList = document.getElementById("systemlanguagelist");
    
    if (theList.selectedIndex > -1)
    {
        createCookie("maweblocale", theList.options[theList.selectedIndex].value, 24 * 365);
        
        document.submitform.fsaction.value = pFuseBoxAction;
        document.submitform.submit();
        
        return false;
    }
    
    return true;
}

function debugObjectValues(objName, obj) 
{
  var output = "";
  for (var prop in obj) 
  {
    output += objName + "." + prop + " = " + obj[prop] + ", ";
  }
  
  alert(output);
}

function EncodeEmailPassword(pPassword)
	{
	
	var Base64 = {
	
		// private property
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
		
		// public method for encoding
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	
			while (i < input.length) {
	
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	
				output = output +
				this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	
			}
	
			return (output);
		}
		
	}
		return(Base64.encode(pPassword));
}









