
//-------------------------------------------------------------------
// getElementIndex(input_object)
//   Pass an input object, returns index in form.elements[] for the object
//   Returns -1 if error
//-------------------------------------------------------------------
function getElementIndex(obj) {
	var theform = obj.form;              
	for (var i=0; i<theform.elements.length; i++) {
		if (obj.name == theform.elements[i].name) {
			return i;
			}
		}
	return -1;
	}

// -------------------------------------------------------------------
// tabNext(input_object)
//   Pass an form input object. Will focus() the next field in the form
//   after the passed element.
//   a) Will not focus to hidden or disabled fields
//   b) If end of form is reached, it will loop to beginning
//   c) If it loops through and reaches the original field again without
//      finding a valid field to focus, it stops
// -------------------------------------------------------------------
function tabNext(obj) {
	if (navigator.platform.toUpperCase().indexOf("SUNOS") != -1) {
		obj.blur(); return; // Sun's onFocus() is messed up
		}
	var theform = obj.form;
	var i = getElementIndex(obj);
	var j=i+1;
	if (j >= theform.elements.length) { j=0; }
	if (i == -1) { return; }
	while (j != i) {
		if ((theform.elements[j].type!="hidden") && 
		    (theform.elements[j].name != theform.elements[i].name) && 
			(!theform.elements[j].disabled)) {
			theform.elements[j].focus();
			break;
			}
		j++;
		if (j >= theform.elements.length) { j=0; }
		}
	}
	
function focus_it() {
/* if (cs_g_position>0) document.location.href= -#- +cs_g_position; */
}

function autofocus(obj) {
var gdb = obj.form.g_database;
/* var thisvalue = gdb.options[obj.selectedIndex].value;*/
var thisvalue=global_dbase;

if (thisvalue=="FAX")
   {         
      if (obj.name=="g_category") tabNext(obj);
      if (obj.name=="g_profes") tabNext(obj);	  
      if (obj.name=="g_business") tabNext(obj);
      if (obj.name=="g_residential") tabNext(obj);
   }	
if (thisvalue=="GOV")
   {
      if (obj.name=="g_category") tabNext(obj);
      if (obj.name=="g_profes") tabNext(obj);	  
      if (obj.name=="g_name") tabNext(obj);
      if (obj.name=="g_house") tabNext(obj);	  	  
      if (obj.name=="g_street") tabNext(obj);
      if (obj.name=="g_locality") tabNext(obj);
      if (obj.name=="g_area") tabNext(obj);
      if (obj.name=="g_business") tabNext(obj);
      if (obj.name=="g_residential") tabNext(obj);	  	  	    
      if (obj.name=="g_perpage") tabNext(obj);
   }	   
if (thisvalue=="MISC")
   {
      if (obj.name=="g_category") tabNext(obj);
      if (obj.name=="g_profes") tabNext(obj);
      if (obj.name=="g_name") tabNext(obj);
      if (obj.name=="g_house") tabNext(obj);
      if (obj.name=="g_street") tabNext(obj);
      if (obj.name=="g_locality") tabNext(obj);
      if (obj.name=="g_area") tabNext(obj);
      if (obj.name=="g_business") tabNext(obj);
      if (obj.name=="g_residential") tabNext(obj);
      if (obj.name=="g_perpage") tabNext(obj);
   }	      
}

function autoclear(obj)
{

var theform = obj.form;
var lastvalue = obj.options[obj.selectedIndex].value;
var lastselectedIndex = obj.selectedIndex;
global_dbase=lastvalue;

clean(theform.g_category);
clean(theform.g_profes);
clean(theform.g_surname);
clean(theform.g_name);
clean(theform.g_house);
clean(theform.g_street);
clean(theform.g_locality);
clean(theform.g_area);
clean(theform.g_perpage);
	  
if (lastvalue=="FAX")
   {
      disable (theform.g_profes);
      disable (theform.g_category);
   }	   

if (lastvalue=="GOV")
   {
      disable (theform.g_profes);
      disable (theform.g_category);
      disable (theform.g_name);
      disable (theform.g_house);
      disable (theform.g_street);
      disable (theform.g_locality);
      disable (theform.g_area);
      disable (theform.g_perpage);
   }	   
if (lastvalue=="MISC")
   {
      disable (theform.g_profes);
      disable (theform.g_category);   
      disable (theform.g_name);
      disable (theform.g_house);
      disable (theform.g_street);
      disable (theform.g_locality);
      disable (theform.g_area);
      disable (theform.g_perpage);
   }	      
   obj.options[lastselectedIndex].selected = true;
   global_dbase=lastvalue;
}

function cleartel(obj)
{
  var theform = obj.form;
  if (theform.g_telephone.value!="")
  {
  theform.g_telephone.value=""
  }
}

function disable(obj)
{
  if (obj.type=="text")
  {                                                       
  obj.value="[Disabled]";
  obj.disabled=true;
  }
  if (obj.type.substr(0,6)=="select")
  {
    obj.options[obj.options.length-1].selected= true;
	obj.disabled=true;
  }  
}

function clean(obj)
{
  if (obj.type=="text")
  {                                                       
    if (obj.value=="ALL")
    {
    obj.value="";	 
    }
    if (obj.value=="[Disabled]")
    {
    obj.value="";	 
    }
    obj.disabled=false;
  }
  if ((obj.type.substr(0,6)=="select") || (obj.type.substr(0,6)=="ALL"))
  {
    if (obj.options[obj.selectedIndex].text=="ALL")
      {
	  obj.options[1].selected= true;
	  }
    if (obj.options[obj.selectedIndex].text=="[Disabled]")
      {
	  obj.options[0].selected= true;
	  }
	obj.disabled=false;
  }  
}

