
/*
function addOpt(oCntrl, iPos, sTxt, sVal){
     var selOpcion=new Option(sTxt, sVal);
     eval(oCntrl.options[iPos]=selOpcion);
   }
*/ 

function jumpMenu(targ,selObj,ubicacion,restore)
{ 

  eval(targ+".location='"+ubicacion+selObj.options[selObj.selectedIndex].value+"'"); 
  if (restore) selObj.selectedIndex=0;

}

function jumpMenuMultiple(targ,sel,ubicacion,restore)
{ 
 sal=String();
 arg=String();
 sal= targ+".location='"+ubicacion;
 for(i=0; i < sel.options.length; i++){	
    if(sel.options[i].selected)  
 		arg += eval(sel.options[i].value)+","; 
 }
 
 //alert(eval(sel.options.length));
 //alert(sal+arg+"'");
 eval(sal+arg+"'");
 if (restore) sel.selectedIndex=0;
}

function ocultarFila(tabla,num,ver) {
  dis= ver ? '' : 'none';
  tab=document.getElementById(tabla);
  tab.getElementsByTagName('tr')[num].style.display=dis;
}

//name: nombre del grupo de radio button
//value: id = nombre_value
function setRadioButtonGroup(name,value){
	var id_radio;
	var encontrado = false;
	id_radio = name + "_" + value;
	while( document.getElementById(id_radio) && !encontrado){
		if( document.getElementById(id_radio).value == value ){
			document.getElementById(id_radio).checked = true;
			encontrado = true;		
		}
	}
	return encontrado;
}

// str_names = un string con un separador '@'con todos los names de los group de radio button a setear
// element checkbox true = 'N' false = 'I'
function setAllRadioButtonGroupOrigenes(str_names,value){
	var origen = 'I';
	if (value){
		origen = 'N';
	}
	var names = str_names.split("@");
	for(i=0; i < names.length; i++){
		setRadioButtonGroup(names[i],origen);
	}
}

function checkAll() {
	var nodoCheck = document.getElementsByTagName("input");
	var varCheck = document.getElementById("checkall").checked;
	for (i=1; i<nodoCheck.length; i++){
		if (nodoCheck[i].type == "checkbox" && nodoCheck[i].name != "checkall" && nodoCheck[i].name != 'okm' && nodoCheck[i].disabled == false) {
			nodoCheck[i].checked = varCheck;
		}
	}
}

function foco_input(obj_input) {
	var imagesPath = "/sistemas/multicotizador/sistema/www/css/images/";
	obj_input.className = "textinputHovered";
	obj_input.previousSibling.src = imagesPath + "input_left_xon.gif";
	obj_input.nextSibling.src = imagesPath + "input_right_xon.gif";
}

function no_foco_input(obj_input) {
	var imagesPath = "/sistemas/multicotizador/sistema/www/css/images/";
	obj_input.className = "textinput";
	obj_input.previousSibling.src = imagesPath + "input_left.gif";
	obj_input.nextSibling.src = imagesPath + "input_right.gif";
}

function foco_input1(obj_input) {
	var imagesPath = "../../css/images/";
	obj_input.className = "textinputHovered";
	obj_input.previousSibling.src = imagesPath + "input_left_xon.gif";
	obj_input.nextSibling.src = imagesPath + "input_right_xon.gif";
}

function no_foco_input1(obj_input) {
	var imagesPath = "../../css/images/";
	obj_input.className = "textinput";
	obj_input.previousSibling.src = imagesPath + "input_left.gif";
	obj_input.nextSibling.src = imagesPath + "input_right.gif";
}

//validate form

//validar tarjeta

function validar_tarjeta(tipo, numero){
    
var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Tarjeta Desconcida";
ccErrors [1] = "No existe el numero de tarjeta";
ccErrors [2] = "El formato del numero de tarjeta es invalido";
ccErrors [3] = "El numero de tarjeta es invalido";
ccErrors [4] = "El numero de tarjeta tiene digitos inapropiados";

//function checkCreditCard (cardnumber, cardname) {
     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [1] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [2] = {name: "VisaElectron", 
               length: "16", 
               prefixes: "4517",
//               prefixes: "417500,4917,4913",
               checkdigit: true};
  cards [3] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [4] = {name: "Maestro", 
               length: "12,13,14,15,16,18", 
               prefixes: "5018,5020,5038,6304,6759,6761",
               checkdigit: true};
  cards [5] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [6] = {name: "Tarjeta Naranja", 
               length: "16", 
               prefixes: "",
               checkdigit: true};
  cards [7] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [8] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,650",
               checkdigit: true};
  cards [9] = {name: "JCB", 
               length: "15,16", 
               prefixes: "3,1800,2131",
               checkdigit: true};
  cards [10] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [11] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334, 6767",
               checkdigit: true};
  cards [12] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [13] = {name: "DinersClub", 
               length: "14,16", 
               prefixes: "300,301,302,303,304,305,36,38,55",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
//    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = tipo;
//      break;
//    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
  
  // Ensure that the user has provided a credit card number
  if (numero.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  numero = numero.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = numero
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;


/*
    var numero_str = new String(numero);
    var longitud = 16;
    if (tipo==5){//amex
        longitud = 15;
    }
    if (numero_str.length!=longitud)
        return false;
    var suma = 0;
    for (i=longitud-1; i>=0; i--){
      if (i % 2){
        suma += parseInt(numero_str.charAt(i));
      }else{
        // impar
        if (numero_str.charAt(i) != 9){
            suma += 2*parseInt(numero_str.charAt(i))%9;
        }else{
            suma += 9;
        }
      }
    }
    if (suma % 10 ==0 && suma < 150){
        return true;
    }else{
        return false;
    }*/
}

function validarCBU(cbu) {
   var VEC1 = new Array(7, 1, 3, 9, 7, 1, 3);
   var VEC2 = new Array(3, 9, 7, 1, 3, 9, 7, 1, 3, 9, 7, 1, 3);
   var valido = false;

   bloque1 = cbu.substring(0, 7);
   digitoValidador1 = cbu.substring(7, 8);
   bloque2 = cbu.substring(8, 21);
   digitoValidador2 = cbu.substring(21);
   var acum = 0;
   for (i = 0; i < 7; i++) {
      acum += bloque1.substring(i, i + 1) * VEC1[i];
   }
   strAcum = (acum + '');
   var digitoVCalculado1 = 10 - strAcum.substring(strAcum.length - 1);
   valido = (digitoVCalculado1 == digitoValidador1);
   acum = 0;
   for (i = 0; i < 13; i++) {
      acum += bloque2.substring(i, i + 1) * VEC2[i];
   }
   strAcum = (acum + '');
   var digitoVCalculado2 = 10 - strAcum.substring(strAcum.length - 1);
   valido = (digitoVCalculado2 == digitoValidador2) && valido;
   return true;
}

function validar_tarjeta_cbu(values){
    if (values[0]==8){//Efectivo
        return true;
    }
    else if (values[0]==7){//Cbu
        return validarCBU(values[1]);
    }
    else{
        var tipo = values[0];
        var numero = values[1];
        return validar_tarjeta(tipo,numero);
    }
}

