function placeFocus() 
{
   if (document.forms.length > 0) 
   {
      var field = document.forms[0];
      for (i = 0; i < field.length; i++) 
      {
         if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea")) 
         {
            document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}

function show_hide_rows(fieldsToSet, do_show) 
{    
	for (var i=0; i < fieldsToSet.length; i++)
	{	
      var rw  = document.getElementById(fieldsToSet[i]);
      if (rw) {
        if (do_show)
           rw.style.display='';
        else  
        {
           rw.style.display='none'; 
           var inputs = rw.getElementsByTagName('input');    // if the row is hidden delete the inserted value
           for (var j=0; j < inputs.length; j++)             // possibly need to add other tags eg. textarea, select
	       {	         									   // also need to exclude hidden inputs
	  	     inputs[j].value = '';
	       }
        } 
      }
    }
}

function show_hide_row(rowid, valuesel, valuereq) 
{    
    var do_show = false;

    for (var i=0; i < valuesel.length; i++)
	{	
	  do_show = false;	    
      if (valuesel[i] == valuereq)
        do_show = true;
      show_hide_rows(rowid[i], do_show);   
    }
      
}
      
function resetform() {
   document.forms[0].reset();
   showHideSections();
   return false;
}

function resetaction(formname) {
        document.forms[formname].reset();
}

function checkForm() {
	if (document.getElementsByName('confirm').item(0).checked)
	   return true;
	else {
		alert('Please confirm acceptance of the notes and declaration');
		return false;
	}   
}





function filterRating(){
  var list = document.forms[0].ratingid;	
  /*
  if the dropdown list passed in hasn't
  already been backed up, we'll do that now
  */
  if (!list.bak){
    /*
    We're going to attach an array to the select object
    where we'll keep a backup of the original dropdown list
    */
    list.bak = new Array();
    for (n=0;n<list.length;n++){
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
    }
  }
  
  // selected size
  var sizeIdx = 5;
  var ratingIdx = 5;
  
  var colSize = document.getElementsByName('sizeradio');
  for (var i = 0; i < colSize.length; i++)
  {
    if (colSize[i].checked)
    {
       sizeIdx = i;
    }
  }
  
  var colRating = document.getElementsByName('ratingradio');
  for (var i = 0; i < colRating.length; i++)
  {
    if (colRating[i].checked)
    {
       ratingIdx = i;
    }
  }
  

  match = new Array();

  if (sizeIdx == 2 && ratingIdx == 0) {
	 var blkownership = document.getElementsByName('blkownership').item(0).value;
	 var femblkownership = document.getElementsByName('femblkownership').item(0).value; 
	 if (isNaN(blkownership) || (blkownership==""))
	    blkownership = 0;
	 if (isNaN(femblkownership) || (femblkownership==""))
	    femblkownership = 0;
	 if ((blkownership>=50) || (femblkownership >= 30))
        match[0] = new Array(list.bak[2][0], list.bak[2][1]);
     else 
        match[0] = new Array(list.bak[3][0], list.bak[3][1]);
  } else if (ratingIdx == 0)
     match[0] = new Array(list.bak[8][0], list.bak[8][1]);
  else
     match = list.bak;       

  /*
  Now we completely rewrite the dropdown list.
  First we write in the matches, then we write
  in the non matches
  */
  var currSelectedRating = list.options[list.selectedIndex].value;
  for(i=list.options.length-1;i>=0;i--) {
    list.remove(i);
  }
  
  for (n=0;n<match.length;n++){
    var optn = document.createElement("OPTION");
    optn.text = match[n][1];
    optn.value = match[n][0];
    list.options.add(optn);
	  
//    list[n].value = match[n][0];
//    list[n].text = match[n][1];
  }

  /*
  Select the last rating before the change
  */
  if (match.length >= currSelectedRating)
     list.selectedIndex=currSelectedRating-1;
  else
     list.selectedIndex=0;
     
}



function showHideSections(){
   var ratingCbx = document.getElementsByName('ratingradio');   
   showHideRatingBlock(ratingCbx.item(1).checked);
   var sizeCbx = document.getElementsByName('sizeradio');   
   showHideMicroBlock(sizeCbx.item(2).checked);
   filterRating(document.forms[0].ratingid);
   if (ratingCbx.item(1).checked || sizeCbx.item(2).checked) {
      showHideEnhancedBlock(true);
   } else if (ratingCbx.item(0).checked)
      showHideEnhancedBlock(false);   
   var currentCbx = document.getElementsByName('iscurrentsupplier');   
   
   if (currentCbx.item(1).checked) {
      enableDisableCompanyList(true);
   } else if (currentCbx.item(0).checked)
      enableDisableCompanyList(false);   
      
}


function showHideMicroBlock(flag) {

   show_hide_rows(new Array('rowownerheader', 'rowownblk', 'rowownfemblk', 'rowownfinrep', 'rowownacccert', 'rowownratingcert'), flag);

   var ratingCbx = document.getElementsByName('ratingradio');   
   var sizeCbx = document.getElementsByName('sizeradio');   
   if (sizeCbx.item(2).checked || ratingCbx.item(1).checked) {
      showHideEnhancedBlock(true);
   } else
      showHideEnhancedBlock(false);
}


function showHideRatingBlock(flag) {
   show_hide_rows(new Array('rowratedby', 'rowexpiryrating', 'rowuseparent', 'rowparentname', 'rowratingcert'), flag);
   var sizeCbx = document.getElementsByName('sizeradio');   
   var ratingCbx = document.getElementsByName('ratingradio');   
   var useparentCbx = document.getElementsByName('useparentradio');   
   if (sizeCbx.item(2).checked) {
      showHideEnhancedBlock(true);
   } else if (ratingCbx.item(1).checked)
      showHideEnhancedBlock(true);
   else
      showHideEnhancedBlock(false);
   if (useparentCbx.item(1).checked) {
	  showHideParentBlock(true); 
   } else {
	  showHideParentBlock(false); 
   }    
   
}

function showHideParentBlock(flag) {
   show_hide_rows(new Array('rowparentname'), flag);
   
}

function showHideEnhancedBlock(flag) {
   show_hide_rows(new Array('rowempheader', 'rowemptxt', 'rowempentdev', 'rowempvaladd', 'rowempfile'), flag);
   var valaddCbx = document.getElementsByName('valaddradio');   

   if (flag && !valaddCbx.item(1).checked || !flag) {
      showHideEnhancedFileBlock(false);
   } else
      showHideEnhancedFileBlock(true);
   var entdevCbx = document.getElementsByName('entdevradio');   
   if (flag && !entdevCbx.item(1).checked || !flag) {
      showHideEntdevFileBlock(false);
   } else
      showHideEntdevFileBlock(true);
  
}

function showHideEnhancedFileBlock(flag) {
      show_hide_rows(new Array('rowempfile'), flag);
}

function showHideEntdevFileBlock(flag) {
      show_hide_rows(new Array('rowempentdevfile'), flag);
}


function showHideFields(){
   var sel = document.forms['user'].isSupplier.selectedIndex; 
   show_hide_rows(new Array('rowsysadmin', 'rowadmin'), !sel);
 
}


function enableDisableCompanyList(currentsupplier){
   var companyCbx = document.getElementsByName('division[]');
   
  for (i=0; i < companyCbx.length; i++)
  {  
	if (currentsupplier) {
		if (companyCbx[i].value == -1) {
			companyCbx[i].checked = false;
			companyCbx[i].disabled = true;
		} else
			companyCbx[i].disabled = false;
	} else {
		if (companyCbx[i].value == -1) {
			companyCbx[i].checked = true;
			companyCbx[i].disabled = true;
		} else {
			companyCbx[i].checked = false;
			companyCbx[i].disabled = true;
	    }
	} 
  }
}
