//browser check
	dm=document.getElementById?1:0;
    op=(window.opera&&dm)?1:0;
    ie=(document.all&&!op)?1:0;
    ns=(dm&&!ie&&!op)?1:0;
    isIE = navigator.appName.indexOf("Microsoft") != -1;
    
var AEValue = null;
var AEdecp = null;
var AEprec = null;
var AEkey = null; 

function AEHT(ctrl,show,value,color,ml){
  if ((ctrl.value == "") || ((ctrl.value == value)) )
  {
    if (show)
    {
      ctrl.maxLength = "125000";
      ctrl.value = value;
    }
    else
    {
      ctrl.maxLength = ml;
      ctrl.value = "";
    }
    
    ctrl.style.color = color;
  }
}

function AEHandleMin(ctrl,sgn){ 
if (sgn){
	s = ctrl.value;
	if (s.indexOf('-') == -1)
	ctrl.value = '-' + s; 
	else
	ctrl.value = s.substring(1);}
}

function AEOnBlur(ctrl,startval)
{
  s = ctrl.value;  
  if (s != startval)
    __doPostBack(ctrl.id, "")
}


function AEKeyUp(ctrl,event,updown,aom,aor){ 	    
var key;
if (isIE) key = event.keyCode; else key = event.which;

  if (AEkey)
  {
      if (ctrl.value.length - ctrl.value.indexOf(AEdecp)-1 > AEprec) 
        ctrl.value = ctrl.value.replace(AEdecp,"");
        
     AEkey = null;
  }

if (AEValue != null)
{
  if (ctrl.value.length - ctrl.value.indexOf(AEdecp)-1 > AEprec) 
    ctrl.value = AEValue;

  AEValue = null;
}

if ( (ctrl.value.length >= ctrl.maxLength) && (ctrl.maxLength != -1) && (aom) ){
  updown = true;
  key = 40;
}

if (updown == true) {

		if (key == 40)
			SelectNextControl(ctrl);

		if (key == 38)
			SelectPreviousControl(ctrl);
/*
var i;
if (key == 40){
	for (i = 0; i < ctrl.form.elements.length; i++)
	if (ctrl == ctrl.form.elements[i])
		break; 
  	i = (i + 1) % ctrl.form.elements.length;
	while ((ctrl.form.elements[i].type != 'text') && (i < ctrl.form.elements.length-1))
	i++; 
	if (ctrl.form.elements[i].type == 'text') 
	ctrl.form.elements[i].focus(); 
	return false;}
if (key == 38){
	for (i = 0; i < ctrl.form.elements.length; i++)
	if (ctrl == ctrl.form.elements[i])
		break; 
	i = (i - 1) % ctrl.form.elements.length;
	while ((ctrl.form.elements[i].type != 'text') && (i > 1))
		i--; 
	if (ctrl.form.elements[i].type == 'text') 
	ctrl.form.elements[i].focus();
	return false;}
	*/
}
}	


function AEKeyReturn(ctrl,event){		    
var key;
if (isIE) key = event.keyCode; else key = event.which;

if (key == 13)
{
     SelectNextControl(ctrl);
     return false;
}

/*		
var i;
if (key == 13){
	for (i = 0; i < ctrl.form.elements.length; i++)
	if (ctrl == ctrl.form.elements[i])
		break;
	i = (i + 1) % ctrl.form.elements.length;
	while ((ctrl.form.elements[i].type != 'text') && (i < ctrl.form.elements.length-1)) 
  		i++;
	if (ctrl.form.elements[i].type == 'text') 
		ctrl.form.elements[i].focus();
		return false;}                
*/		
} 


function AEKeyPress(ctrl,edittype,event,sgn,decp,prec,aor)
{

  if ( (op == 1) && aor)
    return AEKeyReturn(ctrl,event);
    
  AEdecp = decp;
  AEprec = prec;
          
  var key;
  if (isIE) 
    key = event.keyCode; 
  else 
    key = event.which;
    
  if ((key == 8) || (key == 0) || (key == 13))
    return; 

  if (edittype == 1)
  {
    if (key < 48 || key > 57)
    { 
	    if (isIE) 
	      event.returnValue = false; 
	    else
	      return false;
	  }

    if (key == 45) 
      AEHandleMin(ctrl,sgn)
  } 

  if (edittype == 2)
  { 
 
    if (key) 
      code = key;
      
    if (key == 45) 
      AEHandleMin(ctrl,sgn) 
            
    var character = String.fromCharCode(code);
    
    if ((key < 48 || key > 57) && (character != decp))
    {
	    if (isIE) 
	      event.returnValue = false; 
	    else
	      return false;
	  }
	  
    if (character == decp)
    {
      if (ctrl.value.indexOf(decp) != -1)
      {
	      if (isIE) 
	        event.returnValue = false; 
	      else 
	        return false;
	    }
	    else
	    {
	      AEkey = true;
	    }		
    }
    
    if ((key >= 48) && (key <= 57) && (prec > 0))
    {

      if (ctrl.value.indexOf(AEdecp) != -1)
      {
        //if (ctrl.value.length - ctrl.value.indexOf(decp) > prec) 
          //return false;
         if(AEValue == null)
          AEValue = ctrl.value;

       }
    }

  } 
  
  if (edittype == 3)
  {
    if (key > 96 && key < 123) 
    {
	    if (isIE)
	    {
          event.keyCode = key - 32; 
          //event.returnValue = false;
      }
	    else
	    {
		    ctrl.value += String.fromCharCode(event.which - 32);
		    return false;
		  }
	  } 
  } 
  
  if (edittype == 5)
  { 
    if (key > 64 && key < 91) 
    {
      //key = key + 32;
      if(isIE)
	      event.keyCode = key + 32; 
	    else
	    {
	      ctrl.value += String.fromCharCode(event.which + 32);
	      return false;
	    }
    }
  } 
  
  if (edittype == 6)
  { 
    if (((key > 47 && key < 58)) || ((key > 64) && (key < 71)))
	    key = key;
	  else if (key > 96 && key < 103)
		  key = key - 32;
	  else
		  if (isIE) 
		    event.returnValue = false; 
		  else 
		    return false;
	} 
	
  if (edittype == 4)
  {
  } 
}

function SelectNextControl(ctrl)
{
	var i;
	var found = false;
	var tabi = ctrl.tabIndex;
	var di = null;
	var sel = null;
	
	if (tabi > 0)
	{
		for (i = 0; i < ctrl.form.elements.length; i++)
		{
			if ( (ctrl.form.elements[i].tabIndex > tabi) && (CheckFocus(ctrl.form.elements[i])) )
			{
				if ( (di == null) || (ctrl.form.elements[i].tabIndex < di) )
				{
					di = ctrl.form.elements[i].tabIndex;						
					sel = i;
				}
			}
		}

		if (sel != null)
		{
			ctrl.form.elements[sel].focus(); 
			return false;
		}
		else
		{
			for (i = 0; i < ctrl.form.elements.length; i++)
			{
				if ( CheckFocus(ctrl.form.elements[i]) )
				{
					if (isIE)
					{
						if (ctrl.form.elements[i].tabIndex == 0)
						{
							ctrl.form.elements[i].focus(); 
							return false;
						}
					}
					else
					{
						if  ( (ctrl.form.elements[i].tabIndex == 0) || (ctrl.form.elements[i].tabIndex == -1) )
						{
							ctrl.form.elements[i].focus(); 
							return false;
						}
					}
				}
			}

			//if last tabindex go to first tabindex
			var di = null;
			var sel = null;
			for (i = 0; i < ctrl.form.elements.length; i++)
			{
			
				if ( (ctrl.form.elements[i].tabIndex > 0) 
				  && (ctrl.form.elements[i].type != 'hidden') 
				  && (!ctrl.form.elements[i].disabled) )
				{
						if ( (di == null) || (ctrl.form.elements[i].tabIndex < di) )
				    {
					    di = ctrl.form.elements[i].tabIndex;						
					    sel = i;
				    }
				}
				
			}
			
				 if (sel != null)
					ctrl.form.elements[sel].focus(); 
					return false;			

		}
	}
	else
	{
		for (i = 0; i < ctrl.form.elements.length; i++)
		{
			if (ctrl == ctrl.form.elements[i])
				break; 
		}
		var previ = i;
	
		if (isIE)
		{
			for (j = i; j < ctrl.form.elements.length-1; j++)
			{
			
				if ((ctrl.form.elements[j+1].tabIndex != 0) || (!CheckFocus(ctrl.form.elements[j+1])) )
				{
					i++;
				}
				else
				{
					i++;
					break;
				}
			}
		}
		else
		{
			for (j = i; j < ctrl.form.elements.length-1; j++)
			{
				if ((ctrl.form.elements[j+1].tabIndex > 0) || (!CheckFocus(ctrl.form.elements[j+1])) )
				{
  				i++;
				}
				else
				{
					i++
					break;
				}
			}
		}
		
		if (!CheckFocus(ctrl.form.elements[i]))
		 i--;
		
	
		//if last element go to first element
		if (previ == i)
		{
			for (i = 0; i < ctrl.form.elements.length; i++)
			{
				if (CheckFocus(ctrl.form.elements[i]))
					break;
			}
		}

		if (CheckFocus(ctrl.form.elements[i]))
		  ctrl.form.elements[i].focus(); 
		return false;
	}
	
}

function CheckFocus(el)
{
	var setFocus = true;

	if (el.type == 'hidden')
		setFocus = false;

	if (el.disabled)
		setFocus = false;
	  
	if (el.style)
	{
	  if (el.style.visibility)
	  {
		  if  (el.style.visibility == 'hidden') 
			  setFocus = false;
	  }
  	  
	  if (el.style.display)
	  {
		  if (el.style.display == 'none') 
			  setFocus = false;
	  }
	}

	return setFocus;

}

function SelectPreviousControl(ctrl)
{
	var i;
	var found = false;
	var tabi = ctrl.tabIndex;
	var di = null;
	var sel = null;

	if (tabi == 1)
	{
		//if first tabindex go to last tabindex
		var di = tabi;

		for (i = 0; i < ctrl.form.elements.length; i++)
		{
			if ( (ctrl.form.elements[i].tabIndex > di) && (CheckFocus(ctrl.form.elements[i])) )
			{
				di = ctrl.form.elements[i].tabIndex;						
				sel = i;
			}
		}

		if (sel != null)
		  ctrl.form.elements[sel].focus();		
		return false;
	}
	else if (tabi > 1)
	{

		for (i = 0; i < ctrl.form.elements.length; i++)
		{
			if ( (ctrl.form.elements[i].tabIndex < tabi) &&  (CheckFocus(ctrl.form.elements[i])) )
			{
				if ( (di == null) || (ctrl.form.elements[i].tabIndex > di) )
				{
					di = ctrl.form.elements[i].tabIndex;						
					sel = i;
				}
			}
		}

		if (sel != null)
		{
			if (ctrl.form.elements[sel].tabIndex != -1)
				ctrl.form.elements[sel].focus(); 
			return false;
		}
		else
		{
			for (i = 0; i < ctrl.form.elements.length; i++)
			{
				if ( CheckFocus(ctrl.form.elements[i]))
				{
					if (isIE)
					{
						if (ctrl.form.elements[i].tabIndex == 0)
						{
							ctrl.form.elements[i].focus(); 
							return false;
						}
					}
					else
					{
						if ( (ctrl.form.elements[i].tabIndex == 0) || (ctrl.form.elements[i].tabIndex == -1) )
						{
							ctrl.form.elements[i].focus(); 
							return false;
						}
					}
				}
			}
		}
	}
	else
	{
	
		var firstEl = true;
		for (i = 0; i < ctrl.form.elements.length; i++)
		{
			if (ctrl == ctrl.form.elements[i])
				break; 
			else if (  CheckFocus(ctrl.form.elements[i]))
				firstEl = false;

		}

		//if first element go to last element
		if (firstEl)
		{
			for (i = ctrl.form.elements.length - 1; i > 0; i--)
			{
				if ( CheckFocus(ctrl.form.elements[i]))
				{
					ctrl.form.elements[i].focus();
					return false;
				}
			}
		}

		if (isIE)
		{
  		    for (j = i; j > 0; j--)
			{
				if ((ctrl.form.elements[j-1].tabIndex != 0) || (!CheckFocus(ctrl.form.elements[j-1])) )
					i--;
				else
				{
					i--;
					break;
				}
			}
		}
		else
		{
  		    for (j = i; j > 0; j--)
			{
				if ((ctrl.form.elements[j-1].tabIndex > 0)  || (!CheckFocus(ctrl.form.elements[j-1]) ))
				{
					i--;
				}
				else
				{
					i--;
					break;
				}
			}
		}

		var seli = i;

		if (isIE)
		{
			if (ctrl.form.elements[i].tabIndex != 0)
			{
				var tabi = ctrl.form.elements[i].tabIndex;
				for (i = 0; i < ctrl.form.elements.length; i++)
				{
					if ( (ctrl.form.elements[i].tabIndex > tabi) && (CheckFocus(ctrl.form.elements[i])) )
					{
						tabi = ctrl.form.elements[i].tabIndex;
						seli = i;
					}
				}
			}
		}
		else
		{
			if ( (ctrl.form.elements[i].tabIndex != 0) && (ctrl.form.elements[i].tabIndex != -1) )
			{
				var tabi = ctrl.form.elements[i].tabIndex;
				for (i = 0; i < ctrl.form.elements.length; i++)
				{
					if ( (ctrl.form.elements[i].tabIndex > tabi) && (CheckFocus(ctrl.form.elements[i])) )
					{
						tabi = ctrl.form.elements[i].tabIndex;
						seli = i;
					}
				}
			}
		}
		
	if (CheckFocus(ctrl.form.elements[seli]))
		  ctrl.form.elements[seli].focus();

	return false;
	}
}