// Funciones propias de la parte de directorio empresarial

function irDirectorioListado(){
	document.data_frm.scid.value='DIR_002_jsp';
	document.data_frm.frm_action_type.value='buscar';
	//resetVariablesListado();
	doSubmit();
}

function irDirectorioDetalle(id){
	document.data_frm.scid.value='DIR_003_jsp';
	document.data_frm.frm_action_type.value='detalle';
	document.data_frm.ID.value=id;
	
	doSubmit();
}

function irDirectorioValidar(){
	document.data_frm.scid.value='DIR_006_jsp';
	document.data_frm.frm_action_type.value='validar';
	
	doSubmit();
}

function irDirectorioCambiarClave(){
	document.data_frm.frm_action_type.value='cambiarclave';
	document.data_frm.scid.value='DIR_005_jsp';
	
	doSubmit();
}

function irDirectorioPrimeraClave(){
	document.data_frm.frm_action_type.value='clave';
	document.data_frm.scid.value='DIR_007_jsp';
	
	doSubmit();
}

function irDirectorioCambiarClave(){
	document.data_frm.scid.value='DIR_005_jsp';
	document.data_frm.frm_action_type.value='cambiarclave';
	
	doSubmit();
}


function irAdmDirectorio(){
	document.data_frm.scid.value='DIR_001_jsp';
	doSubmit();
}

function irDirectorioCambiarProvincia()
{
	if (document.data_frm.my_provincia_id.value>0){
		document.data_frm.frm_action_type.value="cambiar";
		doSubmit();
	}
}

function irDirectorioGuardar()
{
	if (comprobarDatos())
	{
		document.data_frm.frm_action_type.value="guardar";
		doSubmit();
	}

}


function ltrim ( s ){ 
	return s.replace( /^\s*/, "" ) 
} 

function rtrim ( s ) { 
	return s.replace( /\s*$/, "" ); 
} 

function trim ( s ) { 
	return rtrim(ltrim(s)); 
} 

var digits = "0123456789";

var defaultEmptyOK = false;

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer > 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}


function comprobarDatos(){
	if (trim(document.data_frm.my_empresa_nombre.value)==''){
		alert("El campo Nombre debe rellenarse");
		return false;
	}

	if (isPositiveInteger(document.data_frm.cp.value)==''){
		alert("El campo Código postal debe ser numérico");
		return false;
	}

	return true;
}

function doConsejos(url){
	window.open(url,'','scrollbars=yes,resizable=no,toolbar=no,location=no,directories=no,menubar=no,width=400,height=280', true);
}
