/* funciones*/
function updateCountry(sel) {
    sel.form.elements['reload'].value = 1;
    sel.form.elements['continue'].disabled = true;
    sel.form.submit();
}

function setActionResidenceCountry( sel ) {
	if (sel.value == 0) {
		return false;
	}
	sel.form.city.value = '';
    sel.form.action = 'registro-personal.php?countrysel='+sel.value+'#res_country';
    updateCountry(sel);
}

function getParentTR(e) {
    try {
        var p = e.parentNode;
        while(p && (p.nodeName !== 'TR')) {
            p = p.parentNode;
        }

        return p;
    } catch(ex) {
        alert(ex.description);
        alert(e);
    }
}

function validate(frm, edit) {
    var lbls = frm.getElementsByTagName('label');
    var e, errs = false;
    var mensaje_err = document.getElementById('mensaje_error');
  
    // solo se usan en modo add
    if(!edit)
    {
        //var CP = document.getElementById('char_pwd');
        //CP.style.display = "none";

        //var div_eml = document.getElementById('div_eml');
        //div_eml.style.display = "none";
    }


    mensaje_err.innerHTML = '';


    var pwd_corta, email_diff, doc_number_error = false;


    for(var i = 0, j = lbls.length; i < j; i++) {
        var lbl = lbls.item(i);

        if(lbl && (lbl.className == 'requerido') && (e = document.getElementById(lbl.htmlFor)))
        {
            var tr = getParentTR(lbl);

            if((e.nodeName == 'INPUT' && e.value=='') || (e.nodeName == 'TEXTAREA' && e.value=='')
            || (e.nodeName == 'SELECT' && e.selectedIndex==0))
            {

                errs = true;

                tr.oldClassName = tr.className;

                tr.className = 'requerido-incompleto';
            } else {
                tr.className = '';//(tr.oldClassName ? '' : '');
            }
        }
    }
    try
    {
     	  var es = frm.elements;
		
		/*if (isEqualString(es['email_main'].value,es['cemail'].value) == false) {
			getParentTR(es['email_main']).className = 'requerido-incompleto';
			getParentTR(es['cemail']).className = 'requerido-incompleto';
			errs = true;
		} else {
			getParentTR(es['email_main']).className = '';
			getParentTR(es['cemail']).className = '';
		}
		if (isEmail(es['email_main'].value) == false) {
			getParentTR(es['email_main']).className = 'requerido-incompleto';
			errs = true;
		} else {
			getParentTR(es['email_main']).className = '';
		}*/
			
       
						
			if (((es['phone1_area'].value).length == 0)||((es['phone1_number'].value).length == 0)) {
				getParentTR(es['phone1_number']).className = 'requerido-incompleto';
				errs = true;
			} else {
				getParentTR(es['phone1_number']).className = '';
			}
			
           
        
    } catch(e) { }
    if(errs) {
        IF.style.display = "block";
    }else{
        IF.style.display = "none";
    }

    if(errs)
    {
        return false;
    }

   
    // validar email
    //email_diff = validar_conf_email();
    
	//email = isEmail(es['email']) 
    
    //if(!email)
    //{
    //   return false;
    //}
}

/*** REGISTRO ESTUDIOS ***/

/*
 * Mostrar el combo de porcentaje de avance solo si se
 * selecciona estado "En Curso"
 */
function showAdvancePercentage(sel, id)
{
    var ap = document.getElementById('AdvancePercentage' + (id ? id : ''));
    var selInd = sel.options[sel.selectedIndex].value;

    if( 'F' == selInd )
    {
        ap.className = 'finished';
    } else {
        ap.className = 'in-course';
    }

    var es = sel.form.elements;
    var disableDate = !(( 'U' == selInd ) || ( 'F' == selInd ));

    if(id) {
        es['fd[' + id + '][Date_Month]'].disabled =
        es['fd[' + id + '][Date_Year]'].disabled =
        disableDate;
    } else {
        es['n_fd[Date_Month]'].disabled =
        es['n_fd[Date_Year]'].disabled =
        disableDate;
    }

}




/*
 * Actualizar la informacion segun la seleccion
 * de Industria
 */
function changeIndustry(sel, childID)
{
    var es = sel.form.elements;
    var child = es[childID];
    var opts = IndustryAreas[sel.options[sel.selectedIndex].value];

    child.options.length = 0;
    for(var i in opts) {
        child.options[child.options.length] = new Option(opts[i], i);
    }
}

function showOtherIndustryArea(sel, id)
{
    var O = document.getElementById('OtherIndustryArea' + (id ? id : ''));
    var isOther = ('0' == sel.options[sel.selectedIndex].value);

    O.style.display = isOther ? 'block' : 'none';
    O.disabled = !isOther;
}

/*** REGISTRO OBJETIVO ***/

/*
 * Actualizar la pagina con la informacion
 * correspondiente a la seleccion del combo
 */
function updateIndustryArea(sel)
{
    sel.form.elements['continue'].disabled = true;
    sel.form.submit();
}

function updateIndustry(sel)
{
    var I = sel.form.elements['industry'];
    if(sel.selectedIndex > 0) {
        I.value = '1';
    } else {
        I.value = '';
    }
}


function showStates(country)
{
    document.location = document.location.pathname + '?country=' + country;
}

function testeo()
{

    var el = document.getElementById('__action');
    //alert('before submit');
    //alert(el.value);
    return true;
}

function setAction(action)
{

    var el = document.getElementById('__action');
    el.value = action;
    return true;

}



function validar_conf_email()
{
    if(!document.getElementById('cemail')) return false;

    var mensaje_err = document.getElementById('mensaje_error');
    var email_m = document.getElementById('email_main').value;
    var email_c = document.getElementById('cemail').value;
    var tr_conf      = document.getElementById('conf_eml');
    var tr_eml      = document.getElementById('eml_main');
    var div_eml      = document.getElementById('div_eml');
    var email_diff = false;

    if( email_m != email_c  )
    {

        div_eml.style.display = "block";
        tr_conf.className  = 'requerido-incompleto';
        tr_eml.className   = tr_conf.className;

        email_diff = true;

    }

    return email_diff;

}

function isEmail(sEmail) {
    // Boolean, returns TRUE if the string passed is a valid email format string.
	var regEmail = /^([\w-]+\.?)*\w+@([\da-zA-z-]+\.)+[a-zA-z]{2,3}$/
	if (sEmail==null || sEmail == "") { 
		if (isEmail.arguments.length == 1) {
			return true;
		} else {
			return (isEmail.arguments[1] == true);
		}
	} else {
		return regEmail.test(sEmail)
	}
}

function isNumeric(strString) {
    // Boolean, returns TRUE if the string passed is blank or numeric only.
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function isEqualString(Str1,Str2) {
    // Boolean, returns TRUE if the strings passed are identical.
    // Returns FALSE if an empty string is passed.
	if (Str1.length == 0) return false;
	if (Str1 == Str2) {
		return true;
	} else {
		return false;
	}
}

function selStateID(frm) {
    var opts = frm.elements['state_id[]'].options;
    for(var i = 0; i < opts.length; i++) {
        opts[i].selected = true;
    }
}



var IF;
var onLoad = window.onload;
window.onload = function(event) {
    // Almacenar en la variable LT la tabla de idiomas
    LT = document.getElementById('LanguagesTable');
    IF = document.getElementById('IncompleteFields');
    if(IF) IF.style.display = "none";

    if('function' == typeof onLoad) onLoad(event);
}