/************************************************************************
* IBZValidations.js								   FECHA: 26-10-2005 *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* PROPOSITO :														   *
*	 Conjunto de funciones de validacion para campos en paginas		*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* AUTOR: Mauricio Mendez M.											 *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* id = "$Id: IBZValidations.js,v 1.3 2009/01/19 16:18:56 GOYA\admingoya Exp $"
************************************************************************/

/************************************************************************
* Funciones para realizar procesos de Trim
*	 Retornan cadenas con los espacios eliminados
************************************************************************/

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function LTrim( str ) {
	if ( str == null ) {
		return null;
	}
	for( var i = 0; str.charAt( i ) == " "; i++ );
	return str.substring( i, str.length );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function RTrim( str ) {
	if ( str == null ) {
		return null;
	}
	for( var i = str.length-1; str.charAt( i ) == " "; i-- );
	return str.substring( 0, i + 1 );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function Trim( str ) {
	return LTrim( RTrim( str ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function LTrimAll( str ) {
	if ( str == null ) {
		return str;
	}
	for ( var i = 0; str.charAt( i ) == " " || str.charAt( i ) == "\n" || str.charAt( i ) == "\t"; i++ );
	return str.substring( i, str.length );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function RTrimAll( str ) {
	if ( str == null ) {
		return str;
	}
	for ( var i = str.length-1; str.charAt( i ) == " " || str.charAt( i ) == "\n" || str.charAt( i ) == "\t"; i-- );
	return str.substring( 0, i + 1 );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function TrimAll( str ) {
	return LTrimAll( RTrimAll( str ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNull( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is null
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isNull( val ) {
	return( val == null );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isBlank( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value only contains spaces
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isBlank( val ) {
	if( val == null ) {
		return true;
	}
	for( var i = 0; i < val.length; i++ ) {
		if ( ( val.charAt( i ) != ' ' ) && ( val.charAt( i ) != "\t" ) && ( val.charAt( i ) != "\n" ) && ( val.charAt( i ) != "\r" ) ) {
			return false;
		}
	}
	return true;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isInteger( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value contains all digits
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isInteger( val ) {
	if ( isBlank( val ) ) {
		return false;
	}
	for( var i = 0; i < val.length; i++ ) {
		if( !isDigit( val.charAt( i ) ) ) {
			return false;
		}
	}
	return true;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isNumeric( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value contains a positive float value
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isNumeric( val ) {
	return( parseFloat( val, 10 ) == ( val * 1 ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isArray( widget )
* PARAMETROS:
* RESULTADO:
*	 Returns true if the object is an array, else false
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isArray( widget ) {
	return( typeof( widget.length ) == "undefined" )?false:true;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isDigit( value )
* PARAMETROS:
* RESULTADO:
*	 Returns true if value is a 1-character digit
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isDigit( num ) {
	if ( num.length > 1 ) {
		return false;
	}
	var string = "1234567890";
	if ( string.indexOf( num ) != -1 ) {
		return true;
	}
	return false;
}


/*-----------------------------------------------------------------------
* DESCRIPCION: setNullIfBlank( input_object )
*	 Sets a form field to "" if it isBlank( )
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function setNullIfBlank( widget ) {
	if( isBlank( widget.value ) ) {
		widget.value= "";
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION: setFieldsToUpperCase( input_object )
*	 Sets value of form field toUpperCase( ) for all fields passed
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function setFieldsToUpperCase( ) {
	for( var i = 0; i < arguments.length; i++ ) {
		arguments[i].value = arguments[i].value.toUpperCase( );
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION: disallowBlank( input_object[, message[,true]] )
*	 Checks a form field for a blank value. Optionally alerts if 
*	 blank and focuses
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function disallowBlank( widget ) {
	var msg=( arguments.length > 1 )?arguments[1]:"";
	var dofocus=( arguments.length > 2 )?arguments[2]:false;

	if ( isBlank( getInputValue( widget ) ) ) {
		if( !isBlank( msg ) ) {
			alert( msg );
		}
		if( dofocus ) {
			if ( isArray( widget ) && ( typeof( widget.type ) == "undefined" ) ) {
				widget=widget[0];
			}
			if( widget.type == "text"||widget.type == "textarea"||widget.type == "password" ) {
				widget.select( );
			}
			widget.focus( );
		}
		return true;
	}
	return false;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: disallowModify( input_object[,message[,true]] )
*	 Checks a form field for a value different than defaultValue. 
*	 Optionally alerts and focuses
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function disallowModify( widget ) {
	var msg=( arguments.length > 1 )?arguments[1]:"";
	var dofocus=( arguments.length > 2 )?arguments[2]:false;

	if ( getInputValue( widget ) != getInputDefaultValue( widget ) ) {
		if( !isBlank( msg ) ) {
			alert( msg );
		}
		if( dofocus ) {
			if ( isArray( widget ) && ( typeof( widget.type ) == "undefined" ) ) {
				widget=widget[0];
			}
			if( widget.type == "text"||widget.type == "textarea"||widget.type == "password" ) {
				widget.select( );
			}
			widget.focus( );
		}
		setInputValue( widget, getInputDefaultValue( widget ) );
		return true;
	}
	return false;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: commifyArray( array[,delimiter] )
*	 Take an array of values and turn it into a comma-separated string
*	 Pass an optional second argument to specify a delimiter other than
*	 comma.
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function commifyArray( widget, delimiter ) {
	if ( typeof( delimiter ) == "undefined" || delimiter == null ) {
		delimiter = ",";
	}
	var s= "";
	if( widget == null||widget.length <=  0 ) {
		return s;
	}
	for( var i = 0; i < widget.length; i++ ) {
		s= s+( ( s == "" )?"":delimiter )+widget[i].toString( );
	}
	return s;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: getSingleInputValue( input_object,use_default,delimiter )
*	 Utility function used by others
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function getSingleInputValue( widget, use_default, delimiter ) {
	switch( widget.type ) {
		case 'radio': case 'checkbox': return( ( ( use_default )?widget.defaultChecked:widget.checked )?widget.value:null );
		case 'text': case 'hidden': case 'textarea': return( use_default )?widget.defaultValue:widget.value;
		case 'password': return( ( use_default )?null:widget.value );
		case 'select-one':
			if ( widget.options == null ) {
				return null;
			}
			if( use_default ) {
				var o=widget.options;
				for( var i = 0; i < o.length; i++ ) {
					if( o[i].defaultSelected ) {
						return o[i].value;
					}
				}
				return o[0].value;
			}
			if ( widget.selectedIndex < 0 ) {
				return null;
			}
			return( widget.options.length > 0 )?widget.options[widget.selectedIndex].value:null;
		case 'select-multiple': 
			if ( widget.options == null ) {
				return null;
			}
			var values=new Array( );
			for( var i = 0; i < widget.options.length; i++ ) {
				if( ( use_default && widget.options[i].defaultSelected )||( !use_default && widget.options[i].selected ) ) {
					values[values.length]=widget.options[i].value;
				}
			}
			return ( values.length == 0 )?null:commifyArray( values, delimiter );
	}
	alert( "FATAL ERROR: Field type "+widget.type+" is not supported for this function" );
	return null;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: getSingleInputText( input_object,use_default,delimiter )
*	 Utility function used by others
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function getSingleInputText( widget, use_default, delimiter ) {
	switch( widget.type ) {
		case 'radio': case 'checkbox': 	return "";
		case 'text': case 'hidden': case 'textarea': return( use_default )?widget.defaultValue:widget.value;
		case 'password': return( ( use_default )?null:widget.value );
		case 'select-one':
			if ( widget.options == null ) {
				return null;
			}
			if( use_default ) {
				var o=widget.options;
				for( var i = 0; i < o.length; i++ ) {
					if( o[i].defaultSelected ) {
						return o[i].text;
					}
				}
				return o[0].text;
			}
			if ( widget.selectedIndex < 0 ) {
				return null;
			}
			return( widget.options.length > 0 )?widget.options[widget.selectedIndex].text:null;
		case 'select-multiple': 
			if ( widget.options == null ) {
				return null;
			}
			var values=new Array( );
			for( var i = 0; i < widget.options.length; i++ ) {
				if( ( use_default && widget.options[i].defaultSelected )||( !use_default && widget.options[i].selected ) ) {
					values[values.length]=widget.options[i].text;
				}
			}
			return ( values.length == 0 )?null:commifyArray( values, delimiter );
	}
	alert( "FATAL ERROR: Field type "+widget.type+" is not supported for this function" );
	return null;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: setSingleInputValue( input_object,value )
*	 Utility function used by others
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function setSingleInputValue( widget,value ) {
	switch( widget.type ) {
		case 'radio': case 'checkbox': 
			if( widget.value == value ) {
				widget.checked = true;
				return true;
			}
			else{
				widget.checked=false;
				return false;
			}
		case 'text': case 'hidden': case 'textarea': case 'password': widget.value=value; return true;
		case 'select-one': case 'select-multiple': 
			var o=widget.options;
			for( var i = 0; i < o.length; i++ ) {
				if( o[i].value == value ) {
					o[i].selected = true;
				}
				else{
					o[i].selected=false;
				}
			}
			return true;
	}
	alert( "FATAL ERROR: Field type "+widget.type+" is not supported for this function" );
	return false;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: getInputValue( input_object[,delimiter] )
*	 Get the value of any form input field
*	 Multiple-select fields are returned as comma-separated values, or
*	 delmited by the optional second argument
*	 ( Doesn't support input types: button,file,reset,submit )
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function getInputValue( widget, delimiter ) {
	var use_default=( arguments.length > 2 )?arguments[2]:false;
	if ( isArray( widget ) && ( typeof( widget.type ) == "undefined" ) ) {
		var values=new Array( );
		for( var i = 0; i < widget.length; i++ ) {
			var v=getSingleInputValue( widget[i], use_default, delimiter );
			if( v != null ) {
				values[values.length]=v;
			}
		}
		return commifyArray( values, delimiter );
	}
	return getSingleInputValue( widget, use_default, delimiter );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: getInputText( input_object[,delimiter] )
*	 Get the displayed text of any form input field
*	 Multiple-select fields are returned as comma-separated values, or
*	 delmited by the optional second argument
*	 ( Doesn't support input types: button,file,reset,submit )
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function getInputText( widget, delimiter ) {
	var use_default=( arguments.length > 2 )?arguments[2]:false;
	if ( isArray( widget ) && ( typeof( widget.type ) == "undefined" ) ) {
		var values=new Array( );
		for( var i = 0; i < widget.length; i++ ) {
			var v=getSingleInputText( widget[i], use_default, delimiter );
			if( v != null ) {
				values[values.length]=v;
			}
		}
		return commifyArray( values, delimiter );
	}
	return getSingleInputText( widget, use_default, delimiter );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: getInputDefaultValue( input_object[,delimiter] )
*	 Get the default value of any form input field when it was created
*	 Multiple-select fields are returned as comma-separated values, or
*	 delmited by the optional second argument
*	 ( Doesn't support input types: button,file,password,reset,submit )
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function getInputDefaultValue( widget, delimiter ) {
	return getInputValue( widget, delimiter, true );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: isChanged( input_object )
* PARAMETROS:
* RESULTADO:
*	 Returns true if input object's value has changed since it was
*	 created.
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isChanged( widget ) {
	return( getInputValue( widget ) != getInputDefaultValue( widget ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: setInputValue( widget,value )
*	 Set the value of any form field. In cases where no matching value
*	 is available ( select, radio, etc ) then no option will be selected
*	 ( Doesn't support input types: button,file,password,reset,submit )
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function setInputValue( widget, value ) {
	var use_default=( arguments.length > 1 )?arguments[1]:false;
	if( isArray( widget ) && ( typeof( widget.type ) == "undefined" ) ) {
		for( var i = 0; i < widget.length; i++ ) {
			setSingleInputValue( widget[i], value );
		}
	}
	else{
		setSingleInputValue( widget, value );
	}
}
	
/*-----------------------------------------------------------------------
* DESCRIPCION: isFormModified( form_object,hidden_fields,ignore_fields )
*	 Check to see if anything in a form has been changed. By default
*	 it will check all visible form elements and ignore all hidden 
*	 fields. 
*	 You can pass a comma-separated list of field names to check in
*	 addition to visible fields ( for hiddens, etc ).
*	 You can also pass a comma-separated list of field names to be
*	 ignored in the check.
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function isFormModified( theform, hidden_fields, ignore_fields ) {
	if( hidden_fields == null ) {
		hidden_fields= "";
	}
	if( ignore_fields == null ) {
		ignore_fields= "";
	}
	var hiddenFields=new Object( );
	var ignoreFields=new Object( );
	var i, field;
	var hidden_fields_array=hidden_fields.split( ',' );
	for ( i = 0; i < hidden_fields_array.length; i++ ) {
		hiddenFields[Trim( hidden_fields_array[i] )] = true;
	}
	var ignore_fields_array=ignore_fields.split( ',' );
	for ( i = 0; i < ignore_fields_array.length; i++ ) {
		ignoreFields[Trim( ignore_fields_array[i] )] = true;
	}
	for ( i = 0; i < theform.elements.length; i++ ) {
		var changed=false;
		var name=theform.elements[i].name;
		if( !isBlank( name ) ) {
			var type=theform.elements[i].type;
			if( !ignoreFields[name] ) {
				if( type == "hidden" && hiddenFields[name] ) {
					changed=isChanged( theform[name] );
				}
				else if( type == "hidden" ) {
					changed=false;
				}
				else {
					changed=isChanged( theform[name] );
				}
			}
		}
		if( changed ) {
			return true;
		}
	}
	return false;
}

/*-----------------------------------------------------------------------
* DESCRIPCION: ConvertToInt( valueToSearch )
*	Convert String to Integer
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO:
*-------------------------------------------------------------------- */
function convertToInt( valueToSearch ){
    var returnValue = String();

    returnValue = valueToSearch;

    while( ( letterPosition = returnValue.search( /\D/ ) ) >= 0 )
	{
		letter = returnValue.substr( letterPosition, 1 );
		returnValue = returnValue.replace( letter, '' );
	}

	return( returnValue );
}

/*-----------------------------------------------------------------------
* DESCRIPCION: IsMail( widget, intValue, decimalValue )
*	Parse String to Number
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO: No se puede utilizar con el evento onchange
*-------------------------------------------------------------------- */
function IsMail(mail)
{
	var ismail=new Boolean(), filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (filter.test(mail)){
		ismail=true;
	}
	else 
	{
		ismail=false;
	}

	return(ismail);
}

var tmpNumber = new String();

/*-----------------------------------------------------------------------
* DESCRIPCION: function( widget, intValue, decimalValue )
*	Parse String to Number
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO: No se puede utilizar con el evento onchange
*-------------------------------------------------------------------- */
function parseToNumber( widget, intValue, decimalValue )
{
	var letterPosition = new Number();
	var letter = new String();
	var partNumber = String();
	var partDecimal = String();

    if( tmpNumber != widget.value )
    {

        if( !decimalValue ){
	        widget.value = convertToInt( widget.value.substr(0, intValue) );
	    }
	    else if( widget.value.search(/\./) >= 0 ){
		    partNumber = widget.value.substr( 0, widget.value.search(/\./) );
		    partDecimal = widget.value.substr( widget.value.search(/\./) + 1 );

		    widget.value = convertToInt( partNumber.substr(0, intValue) ) + '.' + convertToInt( partDecimal.substr(0, decimalValue) );
	    }
	    else{
		    widget.value = convertToInt( widget.value.substr(0, intValue) );
	    }

	    tmpNumber = widget.value;
	}
}


/*-----------------------------------------------------------------------
* DESCRIPCION: parseToCurrency( widget, intValue, decimalValue )
*	Parse String to Number
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO: No se puede utilizar con el evento onchange
*-------------------------------------------------------------------- */
function parseToCurrency(opm1_Input){
	var objRegExp=/(\d)/, objRegExp1=/\-/, npm1_Contador=new Number(0), npm1_Temporal=new Number(0), spm1_Negativo=new String('');

	if(window.event.keyCode!=9 && window.event.keyCode!=37 && window.event.keyCode!=38 && window.event.keyCode!=39 && window.event.keyCode!=40){
		if(objRegExp1.test(opm1_Input.value.substr(0,opm1_Input.value.length-1))==true){
			if(opm1_Input.value.substr(opm1_Input.value.length-1,1)=='-'){
				spm1_Negativo='';
			}else{
				if(opm1_Input.value.substr(opm1_Input.value.length-1,1)=='+'){
					spm1_Negativo='';
				}else{
					spm1_Negativo='-';
				}
			}
		}else{
			if(opm1_Input.value.substr(opm1_Input.value.length-1,1)=='-'){
				spm1_Negativo='-';
			}
		}

		for(npm1_Contador=0;npm1_Contador<opm1_Input.value.length;npm1_Contador++){

			if(objRegExp.test(opm1_Input.value.substr(npm1_Contador,1))==true){
				npm1_Temporal=Number(opm1_Input.value.substr(npm1_Contador,1)) + (npm1_Temporal*10);
			}
		}
		if(npm1_Temporal!=0){
			opm1_Input.value='$' + spm1_Negativo + FormatCurrency(npm1_Temporal.toString());
		}else{
			opm1_Input.value='$' + FormatCurrency(npm1_Temporal.toString());
		}
	}
}
/*-----------------------------------------------------------------------
* DESCRIPCION:
Metodo auxiliar utilizado para dar formato Currency
* PARAMETROS:
* RESULTADO:
* PRE:
* POST:
* OJO: No se puede utilizar con el evento onchange
*-------------------------------------------------------------------- */
function FormatCurrency(ocf2_InputValue){
	var scf2_Temporal=new String(), scf2_Cambiar=new String(), icf2_Contador=new Number(), icf2_Miles=new Number();

	if(ocf2_InputValue!=''){

		if(Number(ocf2_InputValue).toString()!='NaN' && !(ocf2_InputValue.search(/\./)>=0)){
			scf2_Cambiar=String(Number(ocf2_InputValue));
			icf2_Miles=0;
			for(icf2_Contador=scf2_Cambiar.length-1;icf2_Contador>=0;icf2_Contador--){
				if(icf2_Miles==2 && icf2_Contador>0){
					scf2_Temporal=',' + scf2_Cambiar.substr(icf2_Contador,1) + '' + scf2_Temporal;
					icf2_Miles=-1;
				}else{
					scf2_Temporal=scf2_Cambiar.substr(icf2_Contador,1) + '' + scf2_Temporal;
				}
				icf2_Miles++;
			}
		}
	}
	scf2_Cambiar=null;
	icf2_Contador=null;
	icf2_Miles=null;
	return(scf2_Temporal);
	scf2_Temporal=null;
}
