////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


var glow_ok="glow_blue", glow_ko="glow_red", glow_warning="glow_orange";
$(document).ready(function(){
	$("input, select, button, textarea").focus(function(){
		$(this).addClass(glow_ok);
	}).blur(function(){
		$(this).removeClass(glow_ok);
	});
});



function openPopupWindow(url,name,w,h,centered,opts){
	var t,l;
	if(centered==true){
		t=(screen.availHeight-h)/2;
		l=(screen.availWidth-w)/2;
	}else{
		t=l=0;
	}
	var options="width=" + w + ",height=" + h + ",top=" + t + ",left=" + l;
	if(opts!=undefined) options=opts+","+options;
	var win=window.open(url,name,options);
	return win;
}
function changeURL(param){
	var actuales=location.search.split("?").join("");
	actuales=actuales.split("&");
	var nuevos=param.split("?").join("");
	nuevos=nuevos.split("&");
	for(var i=0;i<actuales.length;i++){
		var temp=actuales[i].split("=");
		var act_key=temp[0];
		var act_val=temp[1];
		for(var n=0;n<nuevos.length;n++){
			var temp=nuevos[n].split("=");
			var nue_key=temp[0];
			var nue_val=temp[1];
			if(act_key==nue_key){
				act_val=nue_val;
				nuevos.splice(n,1);
			}
		}
		actuales[i]=act_key+"="+act_val;
	}
	for(var i=0;i<nuevos.length;i++){
		actuales.push(nuevos[i]);	
	}
	actuales="?"+actuales.join("&");
	location.href=location.pathname+actuales;

}



function toggleVis(param,vis){
	var element=document.getElementById(param);
	if(vis!=undefined){
		element.style.display=vis;
	}else{
		element.style.display=element.style.display=="none" ? "" : "none";
	}
}




function saveCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else expires = ""
	document.cookie = name+"="+value+expires+"; path=/"
}
function readCookie(name) {
	var nameEQ = name + "="
	var ca = document.cookie.split(';')
	for(var i=0;i<ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length)
	}
	return null
}
function deleteCookie(name) {
    saveCookie(name,"",-1)
}




function filter_chOp(param){
	var base=param.id.substr(0,param.id.length-3);
	var el=document.getElementById(base+"_row");
	if(param.selectedIndex==6){
		el.style.display="";	
	}else{
		el.style.display="none";
	}
}
function handleFilterCalendarSelect(type,args,obj){
	var dates = args[0]; 
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
	var target="fil_"+obj.id.substr(4,obj.id.length);
	var control=document.getElementById(target);
	
	var temp=cal_date_format.split(" ");
	var dest=control.value.split(" ");
	var fecha="Y-m-d";	
	temp=fecha.replace("d",day);
	temp=temp.replace("m",month);
	temp=temp.replace("Y",year);
	dest[0]=temp;
	
	dest=dest.join(" ");
	control.value=dest;
	
	obj.hide();
}
function validaFiltroFecha(nombre){
	var expr=/^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))))?$/;
	var retorno=true;
	var combo=document.getElementById('fil_'+nombre+'_op');
	var fech1=document.getElementById('fil_'+nombre+'_1');
	var fech2=document.getElementById('fil_'+nombre+'_2');
	if(combo.selectedIndex==6){
		if(fech1.value=='' | fech2.value=='') retorno=false;
		if(!expr.test(fech1.value) || !expr.test(fech2.value)) retorno=false;
	}else{
		if(fech1.value!='' && !expr.test(fech1.value)) retorno=false;
	}
	return retorno;
}





if (!this.JSON) {
    JSON = function () {
        function f(n) {    
            return n < 10 ? '0' + n : n;
        }
        Date.prototype.toJSON = function () {
            return this.getUTCFullYear()   + '-' +
                 f(this.getUTCMonth() + 1) + '-' +
                 f(this.getUTCDate())      + 'T' +
                 f(this.getUTCHours())     + ':' +
                 f(this.getUTCMinutes())   + ':' +
                 f(this.getUTCSeconds())   + 'Z';
        };
        var m = {    
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        };
        function stringify(value, whitelist) {
            var a,          
                i,          
                k,         
                l,          
                r = /["\\\x00-\x1f\x7f-\x9f]/g,
                v;          /
            switch (typeof value) {
            case 'string':
                return r.test(value) ?
                    '"' + value.replace(r, function (a) {
                        var c = m[a];
                        if (c) {
                            return c;
                        }
                        c = a.charCodeAt();
                        return '\\u00' + Math.floor(c / 16).toString(16) +
                                                   (c % 16).toString(16);
                    }) + '"' :
                    '"' + value + '"';
            case 'number':
                return isFinite(value) ? String(value) : 'null';
            case 'boolean':
            case 'null':
                return String(value);
            case 'object':
                if (!value) {
                    return 'null';
                }
                if (typeof value.toJSON === 'function') {
                    return stringify(value.toJSON());
                }
                a = [];
                if (typeof value.length === 'number' &&
                        !(value.propertyIsEnumerable('length'))) {
                    l = value.length;
                    for (i = 0; i < l; i += 1) {
                        a.push(stringify(value[i], whitelist) || 'null');
                    }
                    return '[' + a.join(',') + ']';
                }
                if (whitelist) {
                    l = whitelist.length;
                    for (i = 0; i < l; i += 1) {
                        k = whitelist[i];
                        if (typeof k === 'string') {
                            v = stringify(value[k], whitelist);
                            if (v) {
                                a.push(stringify(k) + ':' + v);
                            }
                        }
                    }
                } else {
                    for (k in value) {
                        if (typeof k === 'string') {
                            v = stringify(value[k], whitelist);
                            if (v) {
                                a.push(stringify(k) + ':' + v);
                            }
                        }
                    }
                }
                return '{' + a.join(',') + '}';
            }
        }
        return {
            stringify: stringify,
            parse: function (text, filter) {
                var j;
                function walk(k, v) {
                    var i, n;
                    if (v && typeof v === 'object') {
                        for (i in v) {
                            if (Object.prototype.hasOwnProperty.apply(v, [i])) {
                                n = walk(i, v[i]);
                                if (n !== undefined) {
                                    v[i] = n;
                                }
                            }
                        }
                    }
                    return filter(k, v);
                }
                if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
                    j = eval('(' + text + ')');
                    return typeof filter === 'function' ? walk('', j) : j;
                }
                throw new SyntaxError('parseJSON');
            }
        };
    }();
}
