function trim(InString)
{
  // Remove os espaços do início e do final de uma string
  var LoopCtrl=true;
  while (LoopCtrl)
  {
	if (InString.indexOf("  ") != -1)
	{
	  Temp = InString.substring(0, InString.indexOf("  "));
	  InString = Temp + InString.substring(InString.indexOf("  ")+1, InString.length);
	}
	else
	  LoopCtrl = false;
  }
  if (InString.substring(0, 1) == " ")
	InString = InString.substring(1, InString.length);
  if (InString.substring (InString.length-1) == " ")
	InString = InString.substring(0, InString.length-1);
  return (InString);
}

function emailValido (EmailCompleto)
{
  // Verifica se determinado e-mail é válido, verificando se há uma @,
  // se há alguma coisa antes da @ e se há algo depois da @, com pelo
  // menos um ponto (.).
  var email;
  var DepoisDaArroba;

  email = trim (EmailCompleto);
  // Se não houver uma arroba (ou se a arroba for o primeiro caracter, então sai
  if (email.indexOf ("@") == -1 || email.indexOf ("@") == 0)
	return false;

  // Se a arroba for o último caracter, então sai
  if (email.length == email.lastIndexOf ("@") + 1)
	return false;

  DepoisDaArroba = email.substring (email.indexOf ("@") + 1);

  // Se houver mais de uma arroba, então sai
  if (DepoisDaArroba.indexOf ("@") != -1)
	return false;

  // Se não houver um ponto depois da arroba, então sai
  if (DepoisDaArroba.indexOf (".") == -1)
	return false;

  // Se o ponto for ou o primeiro ou o último caracter depois da arroba, então sai
  if (DepoisDaArroba.indexOf (".") == 0 || DepoisDaArroba.lastIndexOf (".") == DepoisDaArroba.length - 1)
	return false;

  return true;
}

function checkinteger(object_value)
{
  //Returns true if value is a number or is NULL
  //otherwise returns false   

  if (object_value.length == 0)
    return true;

  //Returns true if value is an integer defined as
  //   having an optional leading + or -.
  //   otherwise containing only the characters 0-9.
  var decimal_format = ",";
  var check_char;

  //The first character can be + -  blank or a digit.
  check_char = object_value.indexOf(decimal_format)
  //Was it a decimal?
  if (check_char < 1)
    return checknumber(object_value);
  else
    return false;
}

function checknumber(object_value)
{
  //Returns true if value is a number or is NULL
  //otherwise returns false   

  if (object_value.length == 0)
    return true;

  //Returns true if value is a number defined as
  //   having an optional leading + or -.
  //   having at most 1 decimal point.
  //   otherwise containing only the characters 0-9.
  var start_format = " ,+-0123456789";
  var number_format = " ,0123456789";
  var check_char;
  var decimal = false;
  var trailing_blank = false;
  var digits = false;

  //The first character can be + - .  blank or a digit.
  check_char = start_format.indexOf(object_value.charAt(0))
  //Was it a decimal?
  if (check_char == 1)
    decimal = true;
  else if (check_char < 1)
    return false;
        
  //Remaining characters can be only . or a digit, but only one decimal.
  for (var i = 1; i < object_value.length; i++)
  {
    check_char = number_format.indexOf(object_value.charAt(i))
    if (check_char < 0)
      return false;
    else if (check_char == 1)
    {
      if (decimal)            // Second decimal.
        return false;
      else
        decimal = true;
    }
    else if (check_char == 0)
    {
      if (decimal || digits)  
        trailing_blank = true;
      // ignore leading blanks
    }
    else if (trailing_blank)
      return false;
    else
      digits = true;
  }       
  //All tests passed, so...
  return true;
}

function checkcreditcard(object_value)
{
        var white_space = " -";
        var creditcard_string="";
        var check_char;


    if (object_value.length == 0)
        return true;

        // squish out the white space
        for (var i = 0; i < object_value.length; i++)
        {
                check_char = white_space.indexOf(object_value.charAt(i))
                if (check_char < 0)
                        creditcard_string += object_value.substring(i, (i + 1));
        }       

        // if all white space return error
    if (creditcard_string.length == 0)
        return false;
         
                
        // make sure number is a valid integer
        if (creditcard_string.charAt(0) == "+")
        return false;

        if (!checkinteger(creditcard_string))
                return false;

    // now check mod10

        var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
        var checkdigit = 0;
        var tempdigit;

        for (var i = 0; i < creditcard_string.length; i++)
        {
                tempdigit = eval(creditcard_string.charAt(i))

                if (doubledigit)
                {
                        tempdigit *= 2;
                        checkdigit += (tempdigit % 10);

                        if ((tempdigit / 10) >= 1.0)
                        {
                                checkdigit++;
                        }

                        doubledigit = false;
                }
                else
                {
                        checkdigit += tempdigit;
                        doubledigit = true;
                }
        }       
        return (checkdigit % 10) == 0 ? true : false;

}

// Checa cartão de crédito
function ChecaCartao(numero)
{
	var espaco = " -.";
	var verifica;
    // squish out the white space
    for (var i = 0; i < numero.length; i++)
    {
            verifica = espaco.indexOf(numero.charAt(i))
            if (verifica >= 0)
				return false;
	}
	
	if (checkcreditcard(numero))
	{
		return true;
	}
	return false;
}

// Remove todos os caracteres não alfanuméricos de uma string
function RemoveAlfa (S)
{
  var Numeros;
  var Aux;
  var i;
  Aux = "";
  Numeros = '0123456789';

  for (i = 0; i < S.length; i++)
	if (Numeros.indexOf (S.charAt (i)) != -1)
	  Aux += Numeros.charAt (Numeros.indexOf (S.charAt (i)));
  return Aux;
}

// Verifica um CPF. Retorna true se for válido ou false em
// caso contrário.
function VerificaCPF(CPF) {
	var wVr, wTam, wSoma, wSoma2, i, j, wDig1, wDig2,
	    wVETOR_CC = new Array(9),
	    wVETOR_PESO = new Array(9);

	wVr = RemoveAlfa (CPF);
	wTam = wVr.length + 1;

    if (wTam < 11)
	  return false;
    
	for (i = 0; i < wVr.length; i++) {
	   if (isNaN(parseInt(wVr.charAt(i))) )
	     return false;
	}
	
	if (wVr == '11111111111' || wVr == '22222222222' || wVr == '33333333333' || wVr == '44444444444' || wVr == '55555555555' || 
	    wVr == '66666666666' || wVr == '77777777777' || wVr == '88888888888' || wVr == '99999999999' || wVr == '01234567890')
	     return false;
    wSoma = 0;
	wSoma2 = 0;
    j = 2;
    for (i = 0; i < 11; i++) {
	   wVETOR_CC[i] = wVr.charAt(i);
	   wVETOR_PESO[i] = j;
	   j++; 
	} 
    i = 0;	  
    while (i < 9)  {
	   i++;
	   if (i < 10) {
          wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1]; }
	   wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
	}
	wDig1 = (wSoma * 10) % 11;
	wDig2 = (wSoma2 * 10) % 11;
	if (wDig1 == 10) { 
	    wDig1 = 0;
	}	
	if (wDig2 == 10) { 
	    wDig2 = 0;
	}		
    if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2)
	  return false;

	return true;
}

function VerificaCNPJ (cnpj)
{
var CNPJ = RemoveAlfa (cnpj)
var Numero = CNPJ.substr(0, 12);
var Digito = CNPJ.substr(12, 2);

var CGC = Numero;
var peso1 = '543298765432';
var peso2 = '654329876543';
var soma1 = 0;
var soma2 = 0;
var digito1 = 0;
var digito2 = 0;

	 if (CNPJ == '00000000000000')
	  	return false;
	 
	 if ((Numero.length + Digito.length + 1 > 1) && (Numero.length + Digito.length + 1 < 15)) 
	  return false;
 
 	if (Numero.length + Digito.length + 1 > 1){
 		 
		 for (i = 1; i < 12 - Numero.length + 1; i++) 
		 {
		  CGC = eval("'" + 0 + CGC + "'")
		 }
		    
		 for (i = 1; i < CGC.length+1; i++) 
		 {
		  soma1 += CGC.substring(i, i-1) * peso1.substring(i, i-1);
		 } 
		
		 soma1 %= 11;
		
		 if (soma1  < 2) 
		 {
		  digito1 = 0;
		 }
		 else 
		 {
		  digito1 = 11 - soma1; 
		 }
		
		 for (i = 1; i < CGC.length+1; i++) 
		 {
		  soma2 += CGC.substring(i, i-1) * peso2.substring(i, i-1);
		 } 
		 
		 soma2 += digito1 * 2 
		 soma2 %= 11;
		
		 if (soma2  < 2) 
		 {
		  digito2 = 0;
		 }
		 else 
		 {
		  digito2 = 11 - soma2; 
		 }
		
		 if (eval("'" + digito1 + digito2 + "'") != Digito)
		  return false;
		 else 
		 {
		  return true;
		 }
	}
}

function criaArray (numElementos)
{
  this.length = numElementos;
  for (i = 1; i < numElementos; i++)
	this[i] = 0;
  return this;
}

/*Abre a primeira janela no canto superior*/
function AbreJanelaDemanda()
{
	janela = 'prodservd';
	arquivo = '/lista_ps_demanda.asp';
	jan = window.open(arquivo, janela,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300",top=0,left=0');
	jan.focus();
}