/*
 * Soft Balear S.L. (softbal@sotbal.com)
 * Nombre:		sb_Util
 * Descripcion:	Funciones útiles para el tratamiento de datos
*/

//api p3
var sb_Request={
	Params:location.search.substr(1).toLowerCase(),
	Form:function(fr){
		var rf=new Array(), f=sb_Id(fr)
		for(i=0;i<f.length;i++){
			if(f[i].type&&f[i].type!="button"&&f[i].type!="submit"){
				if(f[i].type=="checkbox"&&f[i].checked){rf.push((f[i].id||f[i].name)+'='+f[i].value)}
				if(f[i].type=="radio"&&f[i].checked){rf.push(f[i].name+'='+f[i].value)}
				else if(f[i].type!="checkbox"&&f[i].type!="radio"&&f[i].type!="select-multiple"){rf.push(f[i].id+'='+escape(f[i].value))}
				else if(f[i].type=="select-multiple"){
					var multValue=new Array();
					for(j=0;j<f[i].options.length;j++){if(f[i].options[j].selected==true){multValue.push(f[i].options[j].value)}}
					rf.push(f[i].id+'='+multValue)
				}
			}
		}
		return(rf.join("&"))
	},
	QueryString:function(pa,sp){
		var paramVal=''
		if(this.Params!=''){
			var Param=eval("new Array('"+this.Params.replace(/&/g,"','").replace(/=/g,"','")+"')")
			for(i=0;i<Param.length;i++){if(Param[i]==pa){paramVal=Param[i+1];break;}}
		}
		return paramVal
		
		var par=pa.toLowerCase()
	if(this.indexOf(par)==-1){return ''}
	else{
		var pVal=this.substr(this.indexOf(par));
		var rVal=pVal.indexOf(sp||'&')!=-1?pVal.substring((pVal.indexOf('=')+1),pVal.indexOf(sp||'&')):pVal.substr((pVal.indexOf('=')+1))
		return rVal
	}
	}
}
var sb_IsNum={
	Int:function(c){if(!vNum.test(c.value)){alert("Este campo solo puede contener números");c.value="";c.focus()}},
	Dec:function(c){if(!vNumDec.test(c.value)){alert("Este campo solo puede contener valores numéricos");c.value="";c.focus()}}
}
function sb_RecuperaValor(tp){
	args=sb_RecuperaValor.arguments;
	var nArgs=args.length
	for(i=1;i<nArgs;i+=2){
		for(j=0;j<sb_Id(args[i]).options.length;j++){
			if(tp==1){if(sb_Id(args[i]).options[j].value==args[i+1]){sb_Id(args[i]).options[j].selected=true;break}}//para seleccionar por valor
			if(tp==2){sb_Id(args[i]).options[args[i+1]].selected=true;break}//para seleccionar por opción
		}
	}
}
var eActiv="",eBgColor=""
function sb_rowActiv(color,elem){
	try{sb_Id(eActiv).style.background=eBgColor;}catch(e){}
	eActiv=elem.id;eBgColor=elem.style.background
	elem.style.background=color
	
}
//api p3
var sb_Cookie={
	Set:function(b){
		var expire=new Date();var MLL=b?(1000*24*60*60*1000):(-1*24*60*60*1000);expire.setTime(expire.getTime()+MLL);
		var Cookie="BC_session=usuario="+sb_Id("usuario").value+":pass="+sb_Id("clave").value+":lang="+lang+"; path=/bc4"+"; expires="+expire.toGMTString()
		if(!navigator.cookieEnabled){alert(idAlerts[34]);return}else{document.cookie=Cookie}
	},
	Get:function(b){
		var gCookie=D.cookie;
		if(gCookie.substring(0,gCookie.indexOf("="))=="BC_session"){
			var aCookie=gCookie.substring(gCookie.indexOf("=")+1,gCookie.indexOf(";"))
			try{sb_Id("usuario").value=aCookie.queryString("usuario",":");sb_Id("clave").value=aCookie.queryString("pass",":")}catch(e){}
			try{sb_Id("saveIntro").checked=true}catch(e){};return true;
		}
	}
}
//api p3
var sb_Resize={
	Width:function(elem,mlft,mrgh){
		var wPant=D.body.clientWidth; 
		var minWidth=(wPant<779)?774:(wPant-1);
		sb_Id('global').style.width=minWidth+'px';
	},
	Height:function(elemHeight,elem,mtop,mbott){
		var hPant=sb_Id(elemHeight).clientHeight
		var nH=hPant-(mtop+mbott)
		//alert("nH="+nH)
		sb_Id(elem).style.height=nH+"px"
	}
}
//api 3
var sb_showElem={
	Display:function(){
		args=this.Display.arguments
		for(i=0;i<args.length;i+=2){this.Show(args[i],args[i+1]);}
	},
	Visibility:function(){
		args=this.Visibility.arguments
		for(i=0;i<args.length;i+=2){if(sb_Id(args[i])){sb_Id(args[i]).style.visibility=args[i+1]=='v'?'visible':'hidden'}}
	},
	Show:function(layerId,est){
		var stl
		if(sb_Id(layerId)){
			if(est=='b'){stl='block'}else if(est=='i'){stl='inline'}else if(est=='n'){stl='none'}
			return sb_Id(layerId).style.display=stl;
		}
	},
	Window:function(pag,name,w,h,scroll,status){
		var winl=(screen.availWidth-w)/2+'px';var wint=(screen.availHeight-h)/2+'px';
		winprops='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',status='+status;
		win=window.open(pag,name,winprops);
		if(parseInt(navigator.appVersion)>=4){win.window.focus()}
	},
	Images:function(img,p,visor){
		sb_Id(visor).src="file:///"+img; if(p){sb_Id(p).innerHTML=img.substr(img.lastIndexOf("\\")+1);}
	},
	InternalWindow:{
		Create:function(t,l,h,w,p,src,brdStyle,id,alt,bkg){
			if(!sb_Id(id)){
				var fragHTML=D.createDocumentFragment()
				var NW=D.createElement("div");NW.id=id;
				with(NW.style){
					position="absolute";
					top="50%"/*(D.body.scrollHeight/2)+"px"*/;
					left="50%";
					width=w+"px";
					height=h+"px"
					zIndex="10"
					marginLeft=parseInt(-(w/2))+"px";
					marginTop=parseInt(-(h/2))+"px"
				}
				NW.innerHTML="<div style='background:#"+(bkg||"186BB3")+";padding:2px;clear:both;height:21px;-moz-border-radius: 5px 5px 0 0;color:#fff;'><span style='float:left;'><b>"+alt+"</b></span><span style='float:right;' ><button class='btClose1' onclick='sb_showElem.InternalWindow.Remove(\""+id+"\")'>X</button></span></div><iframe src='"+src+"' width='"+(w)+"' height='"+(h-37)+"' frameborder='0' scrolling='auto' style='background: #fff;' />"+alt+"</iframe>"
				fragHTML.appendChild(NW)
				try{sb_Id("black").style.visibility="visible"}catch(e){}
				D.body.appendChild(fragHTML)
			}
		},
		Remove:function(elem){
			try{sb_Id("black").style.visibility="hidden"}catch(e){}
			try{D.body.removeChild(sb_Id(elem));D.body.style.overflow="auto"}catch(e){}
		}
	}
}

var stateDragDrop=false
var dragElem="left",selectElem,posLeft,posTop,activeObject;
var sb_dragDropElem={
	OnPressMouse:function(e){
	  	selectElem=!ie?e.target:event.srcElement;
	    activeObject=!ie?selectElem.parentNode:selectElem.parentElement;
	    if (activeObject.id==dragElem){
	    	stateDragDrop=true
	    }
	    return false;
	},
	OnDragMouse:function(e){
		if(stateDragDrop){
			activeObject.style.marginLeft=0;
	    	activeObject.style.marginTop=0;
			posLeft=!ie?e.clientX:event.clientX;
			posTop=!ie?e.clientY:event.clientY;
			activeObject.style.left=(posLeft-parseInt(activeObject.style.width)/2)+"px";
			activeObject.style.top=(posTop)+"px";
		//	sb_Id("pie").innerHTML=activeObject.style.left
			return false;
		}
	},
	OnDropMouse:function(){
		stateDragDrop=false
	}
}
/*document.onmousedown=sb_dragDropElem.OnPressMouse;
document.onmousemove=sb_dragDropElem.OnDragMouse;
document.onmouseup=sb_dragDropElem.OnDropMouse;*/

//validar numero cuenta bancaria
function sb_CCControl(val){
	var valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
	var control = 0;
	for (i=0; i<=9; i++){control += parseInt(val.charAt(i)) * valores[i];}
	control = 11 - (control % 11);
	if (control == 11) control = 0;
	else if (control == 10) control = 1;
	return control;
}

Rot13 = {
    map: null,
    convert: function(a) {
        Rot13.init();

        var s = "";
        for (i=0; i < a.length; i++) {
            var b = a.charAt(i);
            s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot13.map[b] : b);
        }
        return s;
    },
    init: function() {
        if (Rot13.map != null)
            return;
              
        var map = new Array();
        var s   = "abcdefghijklmnopqrstuvwxyz";

        for (i=0; i<s.length; i++)
            map[s.charAt(i)] = s.charAt((i+13)%26);
        for (i=0; i<s.length; i++)
            map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();

        Rot13.map = map;
    },
    write: function(a) {
        document.write(Rot13.convert(a));
    }
}

