

var dFilterAlphaStep

function dFilterAlphaStrip (dFilterAlphaTemp, dFilterAlphaMask)
{
    dFilterAlphaMask = replace(dFilterAlphaMask,'#','');
	dFilterAlphaTemp=replace(dFilterAlphaTemp,'_','');
    for (dFilterAlphaStep = 0; dFilterAlphaStep < dFilterAlphaMask.length++; dFilterAlphaStep++)
		{
		    dFilterAlphaTemp = replace(dFilterAlphaTemp,dFilterAlphaMask.substring(dFilterAlphaStep,dFilterAlphaStep+1),'');
		}
		return dFilterAlphaTemp;
}

function dFilterAlphaMax (dFilterAlphaMask)
{
 		dFilterAlphaTemp = dFilterAlphaMask;
    for (dFilterAlphaStep = 0; dFilterAlphaStep < (dFilterAlphaMask.length+1); dFilterAlphaStep++)
		{
		 		if (dFilterAlphaMask.charAt(dFilterAlphaStep)!='#')
				{
		        dFilterAlphaTemp = replace(dFilterAlphaTemp,dFilterAlphaMask.charAt(dFilterAlphaStep),'');
				}
		}
		return dFilterAlphaTemp.length;
}

function dFilterAlpha (key, textbox, dFilterAlphaMask)
{
		dFilterAlphaNum = dFilterAlphaStrip(textbox.value, dFilterAlphaMask);
		
		if (key==9)
		{
		    return true;
		}
		else if (key==8&&dFilterAlphaNum.length!=0)
		{
		 	 	dFilterAlphaNum = dFilterAlphaNum.substring(0,dFilterAlphaNum.length-1);
		}
 	  else if ( ((key>=65&&key<=90)||(key>=90&&key<=122)) && dFilterAlphaNum.length<dFilterAlphaMax(dFilterAlphaMask) )
		{
        dFilterAlphaNum=dFilterAlphaNum+String.fromCharCode(key);
		}

		var dFilterAlphaFinal='';
    for (dFilterAlphaStep = 0; dFilterAlphaStep < dFilterAlphaMask.length; dFilterAlphaStep++)
		{
        if (dFilterAlphaMask.charAt(dFilterAlphaStep)=='#')
				{
					  if (dFilterAlphaNum.length!=0)
					  {
				        dFilterAlphaFinal = dFilterAlphaFinal + dFilterAlphaNum.charAt(0);
					      dFilterAlphaNum = dFilterAlphaNum.substring(1,dFilterAlphaNum.length);
					  }
				    else
				    {
				        dFilterAlphaFinal = dFilterAlphaFinal + "_";
				    }
				}
		 		else if (dFilterAlphaMask.charAt(dFilterAlphaStep)!='#')
				{
				    dFilterAlphaFinal = dFilterAlphaFinal + dFilterAlphaMask.charAt(dFilterAlphaStep); 			
				}
//		    dFilterAlphaTemp = replace(dFilterAlphaTemp,dFilterAlphaMask.substring(dFilterAlphaStep,dFilterAlphaStep+1),'');
		}
		
		textbox.value = dFilterAlphaFinal;
    return false;
}

function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}

