
/*
 * XMLComponents - JavaScript routines for Lookup systems.
 * http://xmlcomponents.com
 *
 * Version: 3.0.0.71
 * Date:    2000/03/17
 *
 * 2000.03.17 Correction in setLookupValuesEx (now uses ...Ex fx in it)
 * 2000.03.17 Modified Lookup system (setLookupValuesEx): now handles POST.
 * 2000.03.02 Got former versions of the routines back in for compatibility.
 * 2000.03.02 v3's fx are now SetLookupValuesEx and SetLookupFieldValuesEx.
 */

// ** Traditional implementation ***********************************************

function setLookupFieldValues(value,id,elementName,elementId,formName)
{
  document.forms[formName].elements[elementName].value=value;
  if (elementId=='') return;
  else document.forms[formName].elements[elementId].value=id;
} // setLookupFieldValues

function lookup(action,filter,formName)
{
  var lAction='';
  if (filter=='') lAction=action;
  else lAction=action+'?'+filter+'='+document.forms[formName].elements[filter].value;
  win=window.open(lAction,filter,'left=200,top=200,width=400,height=500,scrollbars,resizable');
  win.focus();
} // lookup

function setLookupValues(value,id)
{
  opener.setLookupFieldValues(value,id,'MANAGER','MNGR_NO','MainForm');
  self.close();
} // setLookupValues

// ** V3+ implementation *******************************************************

var lookupMatchingsName = 'LOOKUP_MATCHINGS=';
var separatorValue = '=';
var separatorResult = ',';

function lookupFieldAction(LookupAction, FormName, KeyField, DataField, ListField, DisplayField)
{
  var matchings = new Array(KeyField+'='+DataField, ListField+'='+DisplayField);
  lookupEx(LookupAction, FormName, matchings);
} // lookupFieldAction

function setLookupFieldValuesEx(formName, elementName, elementValue)
{
  document.forms[formName].elements[elementName].value = elementValue; 
} // setLookupFieldValuesEx

function lookupEx(action, formName, lookupResult)
{
  var index;
  var lAction = action;
  var lookupResultString = '';

  // current values
  for (var i = 0; i < lookupResult.length; i++) 
  {
    matching = lookupResult[i];
    index = matching.indexOf(separatorValue, 0);
    fieldNameLookup = matching.substring(0, index);
    fieldName = matching.substring(index+1, matching.length);
    if ((fieldNameLookup != '') && (fieldName != '')) 
    {
      fieldValue = document.forms[formName].elements[fieldName].value;

      if (lAction.indexOf('?', 0) == -1)
        lAction = lAction+'?';
      else
        lAction = lAction+'&';
      lAction = lAction+fieldNameLookup+'='+fieldValue;

      if (lookupResultString == '')
       lookupResultString = matching;
      else
       lookupResultString = lookupResultString+','+matching;
    }
  }

  // matchings
  if (lookupResultString != '')
  {
    if (lAction.indexOf('?', 0) == -1)
      lAction = lAction+'?';
    else
      lAction = lAction+'&';
    lAction = lAction+lookupMatchingsName+lookupResult+',formName='+formName;
  }

  win = window.open(lAction, '', 'left=200,top=200,width=400,height=500,scrollbars,resizable');
  win.focus();
} // lookupEx

function getLookupMatch(targets, name)
{
  var index;
  var indexEnd;
  var indexStart;

  index = targets.indexOf(separatorResult+name+separatorValue, 0);
  indexStart = targets.indexOf(separatorValue, index);
  indexEnd = targets.indexOf(separatorResult, indexStart);
  return targets.substring(indexStart+separatorValue.length, indexEnd);
} // getLookupMatch

function validadata(mData)
	{
   var datefield = mData;
   if (chkdate(mData) == false) 	   {   
		alert("Data Invalida. Tente novamente. \n Formato correto: dd/mm/aaaa.");
      datefield.value="";
   	datefield.focus();
   	return false;}
	}
	
	
function comparedate(mDataMaior, mpDataMenor) 
	{	
	var datefield = mDataMaior;
	if (chkdate(mDataMaior) == false) 	   {   
	   alert("Data Invalida. Tente novamente. \n Formato correto: dd/mm/aaaa.");
	   datefield.value="";
	   datefield.focus();
	   return false;
	   }
	else
	   {
	   datefield = mpDataMenor;  
  	   if (chkdate(mpDataMenor) == false) 	   {   
	      alert("Data Invalida. Tente novamente. \n Formato correto: dd/mm/aaaa.");
	      datefield.value="";
	      datefield.focus();
	      return false;
	      } 
	   else
	     	{
	   	if ((mDataMaior.value == '') && (mpDataMenor.value != ''))
	   		{
	   		if (confirm('Deseja manter a data em branco? \nCaso deseje click em OK caso contrário click em CANCELAR !'))
	   			return true;
	   		else
	   			{
	   	    	mDataMaior.value="";
	      	   mDataMaior.focus();
		    		return false;
		    		}
	   		}
	   	else
	   		{
		   	dataMenor = mpDataMenor.value.split("/");
		   	dataMaior = mDataMaior.value.split("/");
	   		data1 = new Date(dataMenor[2],dataMenor[1],dataMenor[0]);
	   		data2 = new Date(dataMaior[2],dataMaior[1],dataMaior[0]);
			   if (data1 > data2) {
		      	alert("Essa data precisa ser maior que a anterior. \n Formato correto: dd/mm/aaaa.");
	   	    	mDataMaior.value="";
	      	   mDataMaior.focus();
		    		return false;
			 		} 
			 	else 
			    	return true;
	      	}
	      }	
	   }
	}

function monta_campo_selecao(selecao,campo,seletor)
	{
	alert(selecao);
	var oOption;
	var i=0;
	var texto = '';
	for (i=0;i<=campo.length;i++)
		{
		if (campo.substr(i,1) == '|')
			{
			oOption = document.createElement("OPTION");
			oOption.text = texto;
			oOption.value = texto;
			seletor.add(oOption);
			if (texto == selecao)
				oOption.selected = true;
			texto='';
			}
		else	
			{
			texto= texto + campo.substr(i,1);
			}
		}	
	}

function setLookupValuesEx()
{
  var arg;
  var argName;
  var argvalue;
  var formName;
  var index;
  var indexEnd;
  var indexStart;
  var target;
  var targets; 

  // extract LOOKUP_MATCHINGS
  targets = document.location.search;
  
  index = targets.indexOf(lookupMatchingsName, 0);
  if (index != -1)
  {
    index = targets.indexOf('?', 0);
    if (index != -1)
      targets = targets.substring(index+1, targets.length);
    while (targets != '')
    {
      if (targets.substring(0, lookupMatchingsName.length) == lookupMatchingsName) 
      {
        index = targets.indexOf('&', 0);
        if (index == -1)
          index = targets.length;
        targets = targets.substring(lookupMatchingsName.length, index);
        break;
      }
  
      index = targets.indexOf('&', 0);
      if (index == -1)
        return;
      targets = targets.substring(index+1, targets.length);
    }
  }
  else
    targets = document.forms['MainForm'].elements['LOOKUP_MATCHINGS'].value;

  targets = separatorResult+targets+separatorResult;
  formName = getLookupMatch(targets, 'formName');

  for (var i = 0; i < setLookupValuesEx.arguments.length; i++)
  {
    arg = setLookupValuesEx.arguments[i];
    index = arg.indexOf(separatorValue, 0);
    argName = arg.substring(0, index);
    argValue = arg.substring(index+1);    

    target = getLookupMatch(targets, argName);
    if (target != '')
      opener.setLookupFieldValuesEx(formName, target, argValue);
  }

  self.close();
} // setLookupValuesEx

// ** Funcoes para o lancamento de notas ***********************************************


