function Warn( hField, sMsg )
{
 alert( sMsg );
 hField.focus();
/* hField.select();*/
}


function checkForRequiredFields( ahFields, asMsgs, sFormName )
{
 var hField;

 for( var i=0; i< ahFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + ahFields[i]);
  if( (hField.value=="") || (hField.value==null) )
  {
   Warn(hField, asMsgs[i]);
	return false;
  }
 }
 return true;
}

function checkForInvalidChars( ahCharCheckFields, asCharsToBeChecked, asCharCheckMessages, sFormName )
{
 var hField;
 var sChars;
 
 for( var i=0; i< ahCharCheckFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + ahCharCheckFields[i]);
  sChars = asCharsToBeChecked[i];
  if( !checkField( hField.value, sChars ) )
  {
   Warn(hField, asCharCheckMessages[i]);
	return false;
  }
 }
 return true;
}


function checkField( sStr, sChars )
{
 var j=0;
 var bRetVal = true;
 while( j < sChars.length )
 {
	if( sStr.indexOf( sChars.substr(j,1) ) >= 0 )
	{
	 bRetVal = false;
	 break;
	}
	j++
 }
 
 return bRetVal;
}

function checkForRequiredChars( ahRequiredCharFields, asRequiredChars, asRequiredCharMessages, sFormName )
{
 var hField;
 var sChars;
 
 for( var i=0; i< ahRequiredCharFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + ahRequiredCharFields[i]);
  sChars = asRequiredChars[i];
  for( var n=0; n < sChars.length; n++)
  {
	if(hField.value.indexOf( sChars.substr(n,1) ) == -1)
	{
	 Warn(hField, asRequiredCharMessages[i]);
	 return false;
	}
  }
 }
 return true;
}

function checkForDateFields( ahDateFields, asDateMessages, sFormName )
{
 var hField;
 
 for( var i=0; i< ahDateFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + ahDateFields[i]);
  if( !checkDate( hField ) )
  {
   Warn(hField, asDateMessages[i]);
   return false;
  }
 }
 return true;
}

function checkForFieldLength( ahLengthFields, aiFieldLengths, asFieldLengthMessages, sFormName )
{
 var hField;
 
 for( var i=0; i< ahLengthFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + ahLengthFields[i]);
  if( hField.value.length > aiFieldLengths[i]  )
  {
   Warn(hField, asFieldLengthMessages[i]);
	return false;
  }
 }
 return true;
}

function checkForDoubleChars( asDoubleCharFields, asDoubleCharMessage, asWhichDoubleChar, sFormName )
{
 var hField;
 var iVirgem;
 
 for( var i=0; i< asDoubleCharFields.length; i++)
 {
  hField = eval("document." + sFormName + "." + asDoubleCharFields[i]);
	iVirgem = 0;
  for( var n=0; n < hField.value.length; n++ )
	{
	 if((hField.value.substr(n,1) == asWhichDoubleChar[i]))
	 {
		iVirgem++;
		if(iVirgem > 1)	{ break; }
	 }
	}
	if( (iVirgem > 1) )
  {
   Warn(hField, asDoubleCharMessage[i]);
	 return false;
  }
 }
 return true;
}

// verifica se, em um select multiple, existe pelo menos 1 valor selecionado
function checkforSelectMultiple( ahFields, sFormName )
{
 var hField;
 var count;
  
 for( var i=0; i< ahFields.length; i++)
 {

	count = 0; 	
 	hField = eval("document." + sFormName + "." + ahFields[i]);
 	
 	if( hField.length == 0)
  	{
  		alert('Campo Select Multiple Livre Required ');
  		return false;
  	}
  	else 
  	{	
		for(var j=0; j< hField.length; j++) {
		  if( hField.options[j].selected == true)
		  {
		   count = count + 1
		  }
	  	}
	    if (count == 0)
	  	{
	  	  alert('Campo Select Multiple Livre Required ');
	  	  return false;
	  	}
	 }
  }
 return true;
}


function cpf_check( cpf ) 
{
	var dac = "", inicio = 2, fim = 10, soma, digito, i, j
	for (j=1;j<=2;j++) 
	{
		soma = 0
		for (i=inicio;i<=fim;i++) {
			soma += parseInt(cpf.substring(i-j-1,i-j))*(fim+1+j-i)
		}
		if (j == 2) { soma += 2*digito }
		digito = (10*soma) % 11
		if (digito == 10) { digito = 0 }
		dac += digito
		inicio = 3
		fim = 11
	}
	return (dac == cpf.substring(cpf.length-2,cpf.length))
}


function cgc_check(cgc) 
{
  	var df, resto, dac = ""
	df = 5*cgc.charAt(0)+4*cgc.charAt(1)+3*cgc.charAt(2)+2*cgc.charAt(3)+9*cgc.charAt(4)+8*cgc.charAt(5)+7*cgc.charAt(6)+6*cgc.charAt(7)+5*cgc.charAt(8)+4*cgc.charAt(9)+3*cgc.charAt(10)+2*cgc.charAt(11)
	resto = df % 11
	dac += ( (resto <= 1) ? 0 : (11-resto) )
	df = 6*cgc.charAt(0)+5*cgc.charAt(1)+4*cgc.charAt(2)+3*cgc.charAt(3)+2*cgc.charAt(4)+9*cgc.charAt(5)+8*cgc.charAt(6)+7*cgc.charAt(7)+6*cgc.charAt(8)+5*cgc.charAt(9)+4*cgc.charAt(10)+3*cgc.charAt(11)+2*parseInt(dac)
	resto = df % 11
	dac += ( (resto <= 1) ? 0 : (11-resto) )
	return (dac == cgc.substring(cgc.length-2,cgc.length))
}


// verifica se, em um select multiple, existe pelo menos 1 valor selecionado
// creio que nao usaremos mais
//apagar??
//function checkforSelectLength( ahFields, sFormName )
//{
// var hField;
// 
// for( var i=0; i< ahFields.length; i++)
// {
//  hField = eval("document." + sFormName + "." + ahFields[i]);
//  alert(ahFields)
//  if( hField.length == 0)
//  {
//   alert('Campo Select Multiple Livre Required ');
//   return false;
//  }
// }
// return true;
//}

function linesLoad(oA,oB) {
	oA.length=0;
	oB.length=0;
	if (lines.length != 0) {
		for (i=0; i < lines.length; i++) {
			if (lines[i][2]=="1") {
				oA.length=oA.length+1;
				oA[oA.length-1].value=lines[i][0];
				oA[oA.length-1].text=lines[i][1];
			}
			else {
				oB.length=oB.length+1;
				oB[oB.length-1].value=lines[i][0];
				oB[oB.length-1].text=lines[i][1];
			}
		}
	}
}

function linesGroupLoad(oA,oB,oLines) {
	
	oA.length=0;
	oB.length=0;

	if (oLines.length != 0) {
		for (i=0; i < oLines.length; i++) {
			if (oLines[i][2]=="1") {
				oA.length=oA.length+1;
				oA[oA.length-1].value=oLines[i][0];
				oA[oA.length-1].text=oLines[i][1];
			}
			else {
				oB.length=oB.length+1;
				oB[oB.length-1].value=oLines[i][0];
				oB[oB.length-1].text=oLines[i][1];
			}
		}
	}
}
function linesSwap(oFrom,nFrom,oTo,nTo,oLeft,oRight) {

	for (i=oFrom.length-1; i >= 0; i--) {
		if (oFrom[i].selected) {
			t = 0;
			for (l=0; l <= lines.length; l++) {
				if (lines[l][2]==nFrom) {
					t++;
					if (t==i+1) {
						lines[l][2]=nTo;
						break;
					}
				}
			}
		}
	}
	linesLoad(oLeft,oRight);
}
function getValues(oA){
	for (i=0; i < oA.length; i++) {
		oA[i].selected = true;
	}
}

function getElementsBySelect(field_h, field_c,field_s){
	var xpath = ''
	field_h.value = '';
	for(i=0;i<field_s.length;i++){
		xpath = xpath + field_s[i].value + ',';
	}
	xpath = new String(xpath).substr(0,xpath.length-1)
	field_h.value = xpath;				
}
