function stopErrors( msg , file , num ) {
	alert( 'ERROR: '+file+' (' + num + ') ' + msg )
	return true
}
window.onerror = stopErrors

function cambia_comillas(s) {
	var t='';	
	while( (t=s.replace('\"','``'))!=s ) 
		s = t
	while( (t=s.replace('\'','`'))!=s ) 
		s = t
	return s
}

function descambia_comillas(s) {
	var t=''
	while( (t=s.replace('``','\"'))!=s ) 
		s = t
	while( (t=s.replace('\`','\''))!=s ) 
		s = t
	return s
}

function splitString( str , chr , max ) {
	var arr = Array(max)
	
	var b = 0, e = -1

	for(var i=0 ; i < arr.length ; i++ ) {
	        b = e;
	        e = str.indexOf( chr , b+1 )

		if(e==-1)
		e = str.length;
		arr[i] = str.substring(b+1,e)
	}
	return arr
}

function formatCurrency( num , digits ) {
	digits =  (digits)?digits:8
	var s = '$        '+ ((num<1)?'0':'') + String( Math.round(num*1000) )
	s = s.substr(s.length-digits, digits)
	s = s.substr(0,s.length-3) + '.' + s.substr(s.length-3,3)
	return s
}

function soloDosDecimales( num ) {
	var s = '        0'+String( Math.round(num*1000) )
	s = s.substr(s.length-8, 8)
	s = s.substr(0,s.length-3) + '.' + s.substr(s.length-3,3)
	return Number(s)
}
function forceNumber( n, def) {
	if(!def) def=0

	return (isNaN(Number(n))? def : n)
}

////////////////////////////////////////////////////////////////////////////////////////////
//////////////
////////////// SUPER CARRO 2000 ;-) Todavia fucionando y util.
//////////////
////////////////////////////////////////////////////////////////////////////////////////////

function	Item_encode() {
	var text=''
    for(var i in this.data)
    	if( this.data[i]!=null )
	    	text += ''+ this.data[i] + '|'
	return escape(text+'~!~')
}
function	Item_change( field, value ) {
	this.data[field] = value
}
function	Item_recalc() {
	// Codigo dependiente. Recalcula algun dato interno del item al modificarse. Tipico caso: subtotal de un item en factura
	this.data.precio = soloDosDecimales( this.data.precio )
	this.data.subtotal = soloDosDecimales( forceNumber(this.data.cantidad) * forceNumber(this.data.precio) )
}

function	Item( text ) {
	var t = splitString(text,'|',10)
	
	for(var i in t)
		t[i] = unescape(t[i])

	// codigo dependiente
	this.data = {
		codigo:			t[0],
		cantidad:		forceNumber(t[1],1),
		rubro:			t[2],
		subrubro:		t[3],
		marca:			t[4],
		descripcion:	t[5],
		precio:			forceNumber(t[6]),
		subtotal:		0
	}
	
	this.encode			= Item_encode
	this.change			= Item_change
	this.recalc			= Item_recalc
}

function	Collection_encodeItems() {
    var s='';
    for(var i in this.items) {
        if( this.items[i] ) {
            s += this.items[i].encode()
        }
    }
    return s
}
function	Collection_encodeExtras() {
    var s = ''
    if(this.extras) {
    	for(var i in this.extras) {
	    	s+= '|'+ this.extras[i]
    	}
    }
    return escape(s)
}
function	Collection_addItem( encodedItem,text ) {
    text = ((text)?text:'Usted está a punto de agregar un item ya existente.\n ¿Está seguro que desea hacer eso?')

	var a = unescape( splitString(encodedItem,'|',1)[0] )
	if( this.items[a] )
		if(!confirm(text) )
			return;

	this.items[a] = new Item( encodedItem )

}
function	Collection_rmvItem(code,text) {
    text = ((text)?text:'Usted está a punto de eliminar un item.\n ¿Está seguro que desea hacer esto?')
    if(this.items[code])
        if(confirm(text))
            this.items[code] = null
}
function	Collection_chgItem(code,field,value,text, numberonly) {
    text = ((text)?text:'Ingrese la cantidad deseada')

    if(this.items[code]) {
        if(value!=null) {
			this.items[code].data[field] = value
		}else{
			var r = null
			while( r==null || (numberonly && isNaN(Number(r))))
				r = prompt(text, this.items[code].data[field] )
			if(r!=null) {
				this.items[code].data[field] = r
			}
		}
    }
}

function	Collection_recalc() {
	// Codigo dependiente. Tipico aca: Poner el total de factura a 0, etc

	this.extras.total = 0
	for(var i in this.items) {
		if(this.items[i]) {
			this.items[i].recalc()

			// codigo dependiente. Tipico aca: Total de factura, cuenta de articulos, etc
			with(this.extras) {
				total += this.items[i].data.subtotal
			}
		}
	}
	this.extras.total = soloDosDecimales(this.extras.total)
}

function	Collection_redraw() {
	
	this.recalc()
	
	if( this.redrawDoc.document )
		with( this.redrawDoc.document ) {
			open()
			write( this.redrawFn() )
			close()
		}
}

function	Collection_redrawBrief() {

	this.recalc()

	if(this.briefFn) {
		with( this.briefDoc ) {
			open()
			write( this.briefFn() )
			close()
		}
	}
}

function	Collection( redrawDoc, briefDoc, redrawFn, briefFn ) {
    this.items      = []
	
	// codigo dependiente
	this.extras		= {total:0}

    this.recalc   		= Collection_recalc

    this.encodeItems   	= Collection_encodeItems
    this.encodeExtras	= Collection_encodeExtras
    this.addItem   		= Collection_addItem
    this.rmvItem    	= Collection_rmvItem
    this.chgItem    	= Collection_chgItem
    this.redraw     	= Collection_redraw
    this.redrawBrief   	= Collection_redrawBrief

	this.redrawFn		= redrawFn
	this.briefFn		= briefFn

	this.redrawDoc		= redrawDoc
	this.briefDoc		= briefDoc

    return this
}
