//
//***********************************************************************************
// Clase cspToken
//
// Constructor: cspToken(Text, Separators)
//
// Métodos
//    nextToken()      Devuelve el siguiente token
//
//
// Separators es un string que contiene la lista de caracteres
// separadores de tokens.  Por default son " " y ",".
//
//**********************************************************************************
//

function cspToken ( Text, Separators ) {
    if (cspToken.arguments.length==0) Text = "";
    if (cspToken.arguments.length==1) Separators = " ,";

    this.text       = Text;
    this.separators = Separators;
    this.parsePos   = 0;
    this.nextToken  = cspNextToken;
}

function cspNextToken () {
    if (this.parsePos >= this.text.length) return "";

    var min = this.text.length + 2;
    var i   = 0;
    var n   = 0;
    var sep = "";

    for (i=0;i<this.separators.length;i++) {
        sep=this.separators.substr(i,1);
        n = this.text.indexOf(sep, this.parsePos);
        if ((n>=0)&&(n<min)) min=n;
    }

    if (min>this.text.length) {
        n=this.parsePos;
        this.parsePos=this.text.length;
        return this.text.substr(n);
    }

    n = this.parsePos;
    this.parsePos = min+1;
    return this.text.substring(n,min);
}


//
//****************************************************************
// Clase cspDate
//
// Constructor: cspDate(dateString)
//
// Métodos
//    toString(Lenguaje)  Lenguage=0(español), 1(ingles).
//    isNull()
//    isInvalid()
//    isLeap()            true=bisiesto  false=normal
//
// Propiedades
//    value               numero yyyymmdd
//    year                numero yyyy
//    month               numero mm
//    day                 numero dd
//
//****************************************************************
//


function cspDate ( dateString ) {
    if (cspDate.arguments.length==0) dateString="";

    this.dateString    = dateString;
    this.year          = 0;
    this.month         = 0;
    this.day           = 0;
    this.date          = 0;
    this.token         = new cspToken(dateString, "-/");

    this.toString      = cspDateToString;
    this.isNull        = cspDateIsNull;
    this.isInvalid     = cspDateIsInvalid;
    this.isLeap        = cspDateIsLeap;
    this.cspPrDate     = cspPrDate;

    this.cspPrDate();
}

function cspDateIsNull () {
    return (this.date==0);
}

function cspDateIsInvalid () {
    return (this.date==-1);
}

function cspDateToString (dateLanguage) {
    if (cspDateToString.arguments.length==0) dateLanguage=0;
    if (this.date<10000) return "";
    if (isNaN(dateLanguage)) dateLanguage = 0;

    if (dateLanguage==0) {
        switch (this.month) {
            case  1: return this.day + "-Ene-" + this.year;
            case  2: return this.day + "-Feb-" + this.year;
            case  3: return this.day + "-Mar-" + this.year;
            case  4: return this.day + "-Abr-" + this.year;
            case  5: return this.day + "-May-" + this.year;
            case  6: return this.day + "-Jun-" + this.year;
            case  7: return this.day + "-Jul-" + this.year;
            case  8: return this.day + "-Ago-" + this.year;
            case  9: return this.day + "-Sep-" + this.year;
            case 10: return this.day + "-Oct-" + this.year;
            case 11: return this.day + "-Nov-" + this.year;
            case 12: return this.day + "-Dic-" + this.year;
        }
    }

      if (dateLanguage==1) {
        switch (this.month) {
            case  1: return this.day + "-Jan-" + this.year;
            case  2: return this.day + "-Feb-" + this.year;
            case  3: return this.day + "-Mar-" + this.year;
            case  4: return this.day + "-Apr-" + this.year;
            case  5: return this.day + "-May-" + this.year;
            case  6: return this.day + "-Jun-" + this.year;
            case  7: return this.day + "-Jul-" + this.year;
            case  8: return this.day + "-Aug-" + this.year;
            case  9: return this.day + "-Sep-" + this.year;
            case 10: return this.day + "-Oct-" + this.year;
            case 11: return this.day + "-Nov-" + this.year;
            case 12: return this.day + "-Dec-" + this.year;
        }
    }
    return "";
}

function cspGetMonth ( NombreMes ) {
    var Mes = NombreMes.toUpperCase();

    if ((Mes == "")||(Mes == null)||(Mes.length==0)) return 0;
    if ((Mes == "ENE")||(Mes == "JAN")||(Mes=="ENERO")||(Mes=="JANUARY")) return 1;
    if ((Mes == "FEB")||(Mes=="FEBRERO")||(Mes=="FEBRUARY")) return 2;
    if ((Mes == "MAR")||(Mes=="MARZO")||(Mes=="MARCH")) return 3;
    if ((Mes == "ABR")||(Mes == "APR")||(Mes=="ABRIL")||(Mes=="APRIL")) return 4;
    if ((Mes == "MAY")||(Mes=="MAYO")) return 5;
    if ((Mes == "JUN")||(Mes=="JUNIO")||(Mes=="JUNE")) return 6;
    if ((Mes == "JUL")||(Mes=="JULIO")||(Mes=="JULY")) return 7;
    if ((Mes == "AGO")||(Mes == "AUG")||(Mes=="AGOSTO")||(Mes=="AUGUST")) return 8;
    if ((Mes == "SEP")||(Mes=="SEPTIEMBRE")||(Mes=="SEPTEMBER")) return 9;
    if ((Mes == "OCT")||(Mes=="OCTUBRE")||(Mes=="OCTOBER")) return 10;
    if ((Mes == "NOV")||(Mes == "NOVIEMBRE")||(Mes=="NOVEMBER")) return 11;
    if ((Mes == "DIC")||(Mes == "DEC")||(Mes=="DICIEMBRE")||(Mes=="DECEMBER")) return 12;
    return 0;
}

function cspPrDate() {
//Hay tres posibilidades válidas:  DMY, MDY, YMD.  Se evaluarán en ese orden.
    var n       = 0;
    var ltk1    = 0;
    var ltk2    = 0;
    var ltk3    = 0;
    var stk     = "";
    var formato = 1;
    var aux     = 0;

    this.year    = 0;
    this.month   = 0;
    this.day     = 0;
    this.date    = 0;

    if (this.dateString=="") return;
    this.date = -1;

    stk = this.token.nextToken();
    if (stk == "") return;

    ltk1 = parseInt(stk, 10);
    if (isNaN(ltk1)) {
        ltk1 = cspGetMonth ( stk );
        if (ltk1==0) return;
        n = n + 1; 
        formato = 2;
    }

    stk = this.token.nextToken();
    if (stk == "") return;

    ltk2 = parseInt(stk, 10);
    if (isNaN(ltk2)) {
        ltk2 = cspGetMonth ( stk );
        if (ltk2==0) return;
        n = n + 1; 
    }

    stk = this.token.nextToken();
    if (stk == "") return;

    ltk3 = parseInt(stk, 10);
    if (isNaN(ltk2)) return;

    if (n>1) return;     //Sólo uno de los tokens puede ser el mes 

    //Validaciones generales
    if ((ltk1 > 3000)|(ltk1 < 0)|(ltk2 > 3000)|(ltk2 < 0)|(ltk3 > 3000)|(ltk3 < 0)) return;

    //Formato 2 - MDY.  Obligado, cuando el primer token no es numérico.
    if (formato==2)  {
        if (cspVerifyDate(ltk3, ltk1, ltk2)) {
            this.year    = cspVerifyYear(ltk3);
            this.month   = ltk1;
            this.day     = ltk2;
            this.date    = (this.year * 10000) + (this.month * 100) + this.day;
        }
        return;
    }

    //Formato 1 - DMY
    if (cspVerifyDate(ltk3, ltk2, ltk1)) {
        this.year    = cspVerifyYear(ltk3);
        this.month   = ltk2;
        this.day     = ltk1;
        this.date    = (this.year * 10000) + (this.month * 100) + this.day;
        return;
    }

    //Formato 2 - MDY
    if (cspVerifyDate(ltk3, ltk1, ltk2)) {
        this.year    = cspVerifyYear(ltk3);
        this.month   = ltk1;
        this.day     = ltk2;
        this.date    = (this.year * 10000) + (this.month * 100) + this.day;
        return;
    }

    //Formato 3 - YMD
    if (cspVerifyDate(ltk1, ltk2, ltk3)) {
        this.year    = cspVerifyYear(ltk1);
        this.month   = ltk2;
        this.day     = ltk3;
        this.date    = (this.year * 10000) + (this.month * 100) + this.day;
    }
}

function cspVerifyYear( ano ) {
    if (ano<50) return ano + 2000;
    if ((ano<100)&(ano>=50)) return ano + 1900;
    return ano;
}

function cspVerifyDate(ano, mes, dia) {
    var aux = false;

    ano = cspVerifyYear(ano);
    if (ano<1000) return false;
    if (dia>31)   return false;
    if (mes>12)   return false;

    if ((mes==4)||(mes==6)||(mes==9)||(mes==11))
        if (dia==31) return false;

    if ((mes==2)&&(dia>29)) return false;

    if ((mes==2)&&(dia==29)) {
        aux = cspDateIsLeap(ano);
        if (!aux) return false;
    }
    return true;
}

function cspDateIsLeap( ano ) {
    if ((ano % 4)!=0)   return false;
    if ((ano % 400)==0) return true;
    return false;
}

//
//***********************************************************************************
// Clase cspText
//
// Constructor: cspText(Lenguaje, PuntoDecimal) 
//                    Lenguage     = 0(español), 1(ingles).
//                    PuntoDecimal = 0(.)  1(,)
//
// Métodos
//    addItem (Control, Tipo, Nombre, Valor, Minimo, Maximo, 
//             AceptaNulos, Cambiar )
//
//             Tipo:    "T" = Text
//                      "N" = Integer Number
//                      "F" = Float Number
//                      "D" = Date
//                      "E" = EMail
//
//             Cambiar:  0  = No tocar
//                       1  = MAYUSCULAS
//                       2  = minúsculas
//                       3  = Tipo frase
//
//    toString (Index)
//    validate(Index, Value)    Devuelve boolean, muestra ventana de error.
//    ValidarCampos()           Devuelve boolean, muestra ventana de error.
//    setValue(Index, Valor)
//
//    Ok                 = 0;
//    IsNull             = 1;
//    InvalidNumber      = 2;
//    LessThanMin        = 3;
//    GreaterThanMax     = 4;
//    InvalidIndex       = 5;
//    InvalidDate        = 6;
//
//***********************************************************************************
//

function cspItem () {
    this.dataType        = 1;
    this.name            = "";
    this.currentValue    = "";
    this.initialValue    = "";
    this.minValue        = "";
    this.maxValue        = "";
    this.allowNull       = true;
    this.capitalize      = 0;       // 0=No tocar  1=MAYUSCULAS  2=minúsculas  3=Tipo frase
    this.nValue          = 0;
    this.nMin            = 0;
    this.nMax            = 0;
		this.control         = null;
}

function cspText ( Language, DecimalPoint ) {
    this.language           = 0;
    this.decimalPoint       = 1;
    this.cspArray           = new Array();

		this.addItem            = cspAddItem;
    this.validate           = cspValidate;
    this.errorName          = cspErrorName;
    this.toString           = cspToString;
    this.setValue           = cspSetValue;
    this.initialValues      = cspInitialValues;
		this.IndiceDeControl    = indiceDeControl;
		this.ValidarRadioCheck  = validarRadioCheck;
		this.ValidarSelect      = validarSelect;
		this.ValidarTexto       = validarTexto;
		this.ValidarControl     = validarControl;
		this.ValidarCampos      = validarCampos;
		this.ValidarEmail       = validarEmail;

    if (cspText.arguments.length>=1)
        if ((Language==0)||(Language==1)) this.language = Language;

    if (cspText.arguments.length==2)
        if ((DecimalPoint==0)||(DecimalPoint==1)) this.decimalPoint = DecimalPoint;
}

function indiceDeControl(control){
	var i=0;
	for(i=1; i<this.cspArray.length; i++)
		if(this.cspArray[i].control==control) return i;
	return 0;
}

function cspAddItem ( control, sDataType, sName, sCurrentValue, sMinValue, sMaxValue, bAllowNull, nCase ) {
    var aux = ""

    aux = typeof sDataType;
		var I = this.IndiceDeControl(control);
		if(I<=0) I = (this.cspArray.length==0?1:this.cspArray.length);
    this.cspArray[I] = new cspItem();

    if (aux!="string") sDataType = "T";
    if (sDataType=="") sDataType = "T";

    sDataType = sDataType.substr(0,1);
    sDataType = sDataType.toUpperCase();

    if (sDataType=="N") this.cspArray[I].dataType = 2;
    if (sDataType=="D") this.cspArray[I].dataType = 3;
		if (sDataType=="E") this.cspArray[I].dataType = 4;
    if (sDataType=="F") this.cspArray[I].dataType = 5;

    aux = typeof sCurrentValue;
    if (aux!="string") sCurrentValue = "";

        switch (nCase) {
            case 1:
                sCurrentValue = sCurrentValue.toUpperCase();
                break;
            case 2:
                sCurrentValue = sCurrentValue.toLowerCase();
                break;
            case 3:
                sCurrentValue = sCurrentValue.substr(0,1).toUpperCase() + sCurrentValue.substr(1)
                break;
        }

    this.cspArray[I].name            = sName;
    this.cspArray[I].currentValue    = sCurrentValue;
    this.cspArray[I].initialValue    = sCurrentValue;
    this.cspArray[I].minValue        = sMinValue;
    this.cspArray[I].maxValue        = sMaxValue;
    this.cspArray[I].allowNull       = bAllowNull;
    this.cspArray[I].capitalize      = nCase;
		this.cspArray[I].control         = control;

    if (this.cspArray[I].dataType==2) {
        this.cspArray[I].nMin   = parseInt(sMinValue);
        this.cspArray[I].nMax   = parseInt(sMaxValue);
        this.cspArray[I].nValue = parseInt(sCurrentValue);
        this.cspArray[I].currentValue = parseInt(sCurrentValue);
    }

		if (this.cspArray[I].dataType==5) {
        this.cspArray[I].nMin   = parseFloat(sMinValue);
        this.cspArray[I].nMax   = parseFloat(sMaxValue);
        this.cspArray[I].nValue = parseFloat(sCurrentValue);
        this.cspArray[I].currentValue = parseFloat(sCurrentValue);
    }

    if (this.cspArray[I].dataType==3) {
        var d = new cspDate(sMinValue);
        if ((d.isNull())||(d.isInvalid())) this.cspArray[I].nMin = -1000;
        else this.cspArray[I].nMin = d.date;

        d = new cspDate(sMaxValue);
        if ((d.isNull())||(d.isInvalid())) this.cspArray[I].nMax = 99999999;
        else this.cspArray[I].nMax = d.date;

        d = new cspDate(sCurrentValue);
        this.cspArray[I].nValue = d.date;
    }
		
		if(control==null) alert(this.errorName(9, I));
}

function cspSetValue ( I, Value ) {
    var Nulo = false;
    var aux  = "";

    if (cspSetValue.arguments.length!=2) {
        alert(this.errorName(5, I));      //Invalid index
        return;
    }

    if ((I<1)||(I>this.cspArray.length-1)) {
        alert(this.errorName(5, I));      //Invalid index
        return;
    }

    aux = typeof Value;
    if (aux!="string") Value="";

    switch (this.cspArray[I].capitalize) {
        case 1:
            Value = Value.toUpperCase();
            break;
        case 2:
            Value = Value.toLowerCase();
            break;
        case 3:
            Value = Value.substr(0,1).toUpperCase() + Value.substr(1);
            break;
    }

    this.cspArray[I].currentValue = Value;
    if (this.cspArray[I].dataType==2) {
        if (Value!=""){
					Value = Value.replace(".", "").replace(",", "");
					this.cspArray[I].currentValue = parseInt(Value);
				}
        this.cspArray[I].nValue = parseInt(Value);
    }
    if (this.cspArray[I].dataType==5) {
        if (Value!="")  
					if (this.decimalPoint==0) 
						this.cspArray[I].currentValue = parseFloat(Value.replace(/\,/,"."));
        this.cspArray[I].nValue = parseFloat(Value);
    }

    if (this.cspArray[I].dataType==3) {
        var d = new cspDate(Value);
        this.cspArray[I].nValue = d.date;
    }
}

function cspValidate ( I, Value ) {
    var Nulo = false;

    if (cspValidate.arguments.length==0) {
        alert(this.errorName(5, I));      //Invalid index
        return false;
    }

    if ((I<1)||(I>this.cspArray.length-1)) {
        alert(this.errorName(5, I));      //Invalid index
        return false;
    }

    if (cspValidate.arguments.length==2) { this.setValue(I, Value); }

    //Verifica valores nulos
    if (this.cspArray[I].currentValue.length==0) {
        if (this.cspArray[I].allowNull) return true;
        else { alert (this.errorName(1, I));      //Is null
               return false;
        }
    }

    if ((this.cspArray[I].dataType==3)&&(this.cspArray[I].nValue==0)) {
        if (this.cspArray[I].allowNull) return true;
        else { alert (this.errorName(1, I));      //Is null
               return false;
        }
    }

    //Verifica valores válidos
    if (this.cspArray[I].dataType==2 || this.cspArray[I].dataType==5) {
        if (isNaN(this.cspArray[I].nValue)) {
            alert (this.errorName(2, I));      //Invalid number
            return false;
        }
    }

    if (this.cspArray[I].dataType==3) {
        if (this.cspArray[I].nValue<0) {
            alert (this.errorName(6, I));      //Invalid date
            return false;
        }
    }

    //Verifica valores máximos y mínimos en number y date
    if (this.cspArray[I].dataType==2 || this.cspArray[I].dataType==5 || this.cspArray[I].dataType==3) {
        if (!isNaN(this.cspArray[I].nMin))
            if (this.cspArray[I].nMin > this.cspArray[I].nValue) {
                alert (this.errorName(3, I));
                return false;
            }

        if (!isNaN(this.cspArray[I].nMax))
            if (this.cspArray[I].nMax < this.cspArray[I].nValue) {
                alert (this.errorName(4, I));
                return false;
            }
    }
		
		if(this.cspArray[I].dataType==4){
			if(this.cspArray[I].currentValue==""){
				if(this.cspArray[I].allowNull) return true;
				alert(this.errorName(1, I));
				return false;
			}
			else{
				if(this.ValidarEmail(this.cspArray[I].currentValue)) return true;
				alert(this.errorName(8, I));
				return false;
			}
		}
    return true;
}

function cspToString ( I ) {
    if (cspToString.arguments.length!=1) {
        alert( this.errorName(5, I));
        return "";
    }

    if ((I<1)||(I>this.cspArray.length-1)) {
        alert( this.errorName(5, I));
        return "";
    }

    if (this.cspArray[I].dataType==3) {
        var d = new cspDate(this.cspArray[I].currentValue);
//        var s = d.toString(this.language);
        var s = d.toString(1);

        switch (this.cspArray[I].capitalize) {
            case 1:
                s = s.toUpperCase();
                break;
            case 2:
                s = s.toLowerCase();
                break;
            case 3:
                s = s.substr(0,1).toUpperCase() + s.substr(1)
        }
        return s;
    }

    if (this.cspArray[I].dataType==2) {
        var s = String(this.cspArray[I].currentValue);
        if (this.decimalPoint==0) s = s.replace(/\,/,".");
        return s;
    }
    return this.cspArray[I].currentValue;
}


function cspInitialValues() {
    for (I=1;I<this.cspArray.length;I++) { this.setValue (I, this.cspArray[I].initialValue)}
}

function cspErrorName ( nError, I ) {
    var Nombre = "";

    if ((I>=1)&&(I<=this.cspArray.length-1)) Nombre = this.cspArray[I].name;

    if (this.language==0) {
        if (Nombre!="") Nombre = "Error en <" + this.cspArray[I].name + ">: ";

        switch (nError)  {
            case 0:  return "";
            case 1:  return Nombre + "No se permiten valores nulos";
            case 2:  return Nombre + "Número inválido";
            case 3:  return Nombre + "Valor muy pequeño";
            case 4:  return Nombre + "Valor muy grande";
            case 5:  return "Índice inválido (" + I + ")";
            case 6:  return Nombre + "Fecha inválida";
						case 7:  return Nombre + "Debe eligir una opción";
						case 8:  return Nombre + "e-mail inválido";
						case 9:  return Nombre + "Control asociado no existe";
						case 10: return Nombre + "Desconocido tipo del control asociado";
            default: return Nombre + "Error no reconocido (" + nError + ")";
        }
    }

    if (this.language==1) {
        if (Nombre.length>0) Nombre = "Error in <" + this.cspArray[I].name + ">: ";

        switch (nError)  {
            case 0:  return "";
            case 1:  return Nombre + "Not null allowed";
            case 2:  return Nombre + "Invalid number";
            case 3:  return Nombre + "Value too small";
            case 4:  return Nombre + "Value too big";
            case 5:  return "Invalid index";
            case 6:  return Nombre + "Invalid date";
						case 7:  return Nombre + "You must select an option";
						case 8:  return Nombre + "Invalid e-mail";
						case 9:  return Nombre + "Associated control doesn't exist";
						case 10: return Nombre + "Associated control type unknowed";
            default: return Nombre + "Error not recognized (" + nError + ")";
        }
    }
    return "Error no reconocido";
}

function validarEmail(email){
	var bValido=true;

	if(email.length < 5){
		//a@a.ti por ejemplo
		bValido = false;
	}
	else if(email.indexOf("@") < 0){
		//No tiene una @
		bValido = false;
	}
	else if(email.indexOf(".") < 0){
		//No tiene un punto
		bValido = false;
	}
	else if(email.indexOf(".", email.indexOf("@")) < email.indexOf("@")){
		//No tiene un punto después de la @
		bValido = false;
	}
			
	return bValido;
}


function validarRadioCheck(control, I){
	if (this.cspArray[I].allowNull) return true;
	control=this.cspArray[I].control;
	if(control.length>0){
		var i=0;
		for(i=0; i<control.length; i++)
			if(control[i].checked) return true;
		control[0].focus();
	}
	else {
		if(control.checked) return true;
		control.focus();
	}
	alert (this.errorName(7, I));      //No seleccionado
	return false;
}

function validarSelect(control, I){
	if (this.cspArray[I].allowNull) return true;
	var i=1;
	if(control.type=="select-multiple") i=0;
	for(; i<control.options.length; i++){
		if(control.options[i].selected) return true;
	}
	control.focus();
	alert (this.errorName(7, I));      //No seleccionado
	return false;
}

function validarTexto(control, I){
	if(this.validate(I, control.value)){
		control.value=this.toString(I);
		return true;
	}
	if (control.type != "hidden") control.focus();
	return false;
}

function validarControl(control, I){
	if(control.type=="radio" || control.type=="checkbox"){
		if(!this.ValidarRadioCheck(control, I)) return false;
	}
	else if(control.type=="select-one" || control.type=="select-multiple"){
		if(!this.ValidarSelect(control, I)) return false;
	}
	else if(control.type=="text" || control.type=="textArea" 
					|| control.type == "hidden" || control.type == "password"){
		if(!this.ValidarTexto(control, I)) return false;
	}
	else{
		alert(this.errorName(10, I)); //tipo desconocido
		return false;
	}
	return true;
}

function validarCampos(){
	var i=0;
	for(i=1; i<this.cspArray.length; i++){
		var control=this.cspArray[i].control;
		if(control.type=="" || control.type==null){
			if(control.length!=null)
				for(var j=0; j<control.length; j++){
					control=control[j];
					if(!this.ValidarControl(control, i)) return false;
				}
			else{
				alert(this.errorName(10, I)); //tipo desconocido
				return false;
			}
		}
		else if(!this.ValidarControl(control, i)) return false;
	}
	return true;
}

function ValidarFormulario(campos){
	if(campos!=null) return campos.ValidarCampos();
	alert("No existe la lista de campos a validar");
	return false;
}

// JavaScript Document
//
//***********************************************************************************
// Clase opcOption
//
// Constructor: opcOption ( opValue, opText)
//
// Métodos
//
//***********************************************************************************
//


function opcOption ( opValue, opText ) {
  this.opValue = opValue;         //value
  this.opText  = opText;          //innerText
}

//
//***********************************************************************************
// Clase cmbCombo
//
// Constructor: cmbCombo(Nombre)
//
// Métodos
//    cmbAddOption ( Valor, Texto )
//
//***********************************************************************************
//

function cmbAddOption1 ( opValue, opText ) {
  var Item;
  var i;

  i = this.cmbOptions.length;
  this.cmbOptions.length = i + 1;

  Item = new opcOption(opValue, opText);

  this.cmbOptions[i] = Item;
}

function cmbCombo ( Nombre ) {
  this.cmbName    = Nombre;
  this.cmbOptions = new Array();
  this.addOption  = cmbAddOption1;
}

//
//***********************************************************************************
// Clase cmbCombos
//
// Constructor: cmbCombos()
//
// Métodos
//    cmbAddCombo ( Nombre )
//    cmbFindCombo ( Nombre )
//
//***********************************************************************************
//

function cmbAddCombo ( Nombre ) {
  var objCombo;
  var i;

  objCombo           = new cmbCombo(Nombre);
  i                  = this.combos.length;
  this.combos.length = this.combos.length + 1;
  this.combos[i]     = objCombo;
  return i;
}

function cmbFindCombo ( Nombre ) {
  var n;

  n = this.combos.length;
  for (i=0; i<n; i++)  {
    if (this.combos[i].cmbName==Nombre)
      return(i);
  }
  return -1;
}

function cmbAddOption2 ( i, opValue, opText ) {
  var Obj;

  if (i==-1)
    return;
  Obj = this.combos[i];
  Obj.addOption(opValue, opText);
}

function cmbSetCombo ( Combo, Item ) {
  var i;
  var n;

  Item.length = 0;
  oCombo = this.combos[Combo];
  n = oCombo.cmbOptions.length-1;

  for (i=0;i<=n;i++) {
      Item.options[i] = new Option(oCombo.cmbOptions[i].opText, oCombo.cmbOptions[i].opValue);
  }
  Item.options[0].selected = true;
}

function cmbSetComboNuevo ( Combo, Item, Valor ) {
  var i;
  var n;
  var marca=0;

  Item.length = 0;
  oCombo = this.combos[Combo];
  n = oCombo.cmbOptions.length-1;
  for (i=0;i<=n;i++) {
      Item.options[i] = new Option(oCombo.cmbOptions[i].opText, oCombo.cmbOptions[i].opValue);
		if (Item.options[i].value == Valor)
		{
			Item.options[i].selected = true;
			marca = 1;
		}
//alert(Item.options[i].value)
  }
  if (marca != 1) Item.options[0].selected = true;
}

function cmbSetComboNuevoColegio ( Combo, Item, Valor ) {
  var i;
  var n;
  var marca=0;
  var selval;
  	n = this.combos.length-1;
     for ( i = 0; i<= n; i++ ){		   
		   oCombo = this.combos[i];
		   oCombo.selectedIndex = i;
		   selval = this.combos[i].options[0]// [i].elements[0].options[i].value
		   
		  if ( selval == Combo ) {	  
		  	alert(selval +'---'+Combo) 
			/*
			 
			Item.options[i] = new Option(oCombo.cmbOptions[i].opText, oCombo.cmbOptions[i].opValue);
			if (Item.options[i].value == Valor){
				Item.options[i].selected = true;
				marca = 1;
			}
			break;*/
		  }
    }  
	alert(selval +'- mi mi--'+Combo)
	if (marca != 1) Item.options[0].selected = true;
}

function cmbGetOptionValue ( Combo, Item ) {
  oCombo = this.combos[Combo];
  return oCombo.cmbOptions[Item].opValue;
}

function cmbCombos () {
  this.combos    = new Array();
  this.addCombo  = cmbAddCombo;
  this.addOption = cmbAddOption2;
  this.setCombo  = cmbSetCombo;
  this.setComboNuevo  = cmbSetComboNuevo; //modificacion formulario
  this.setComboNuevoColegio  = cmbSetComboNuevoColegio; //modificacion formulario
  this.findCombo = cmbFindCombo;
  this.getOptionValue = cmbGetOptionValue;
}

function CambiarAction(formulario, vldFields, action){
	formulario.action = action;
	if(ValidarFormulario(vldFields))
		formulario.submit();
}

