// JavaScript Document
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function YY_checkform() { //v4.71
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='* '+a[i+3]+'\n'; err=false}
  }
  if (s!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}

function Menu(Entrada) {
	cierraMenu();
	var menu=document.getElementById("boton_"+Entrada);
	if (menu) menu.style.visibility="visible";
	menuActual = Entrada;
}

var menuActual = 0;

function cierraMenu() {
	var menu;
	if (menuActual != 0) {
		menu = document.getElementById("boton_" + menuActual);	
		if (menu) menu.style.visibility="hidden";
		menuActual = 0;
	}
	try {
		if (typeof($) != "undefined") {
			$("#panelbookmarks").hide("slow");
			$("#botonbookmarks").attr("clicked",false);
		}
	} catch (e) {}
}

function GP_popupConfirmMsg(msg) { //v1.0
  document.MM_returnValue = confirm(msg);
}


function verMenu(enlace, destino)
{

	var menu=document.getElementById(destino);
	if (menu) {
		menu.innerHTML = enlace.getAttribute("description");
	}
}


function setobjeto(objeto,valor)
{ 
	objeto.value=valor; 
}

function selSelect(objeto,valor)
{ 
	objeto.options[valor].selected=true; 
}

function salir() {
	return(confirm("Seguro que desea finalizar la sesión?"));
}

//código de google Chart
var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
function simpleEncode(valueArray,maxValue) {

var chartData = ['s:'];
  for (var i = 0; i < valueArray.length; i++) {
    var currentValue = valueArray[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
return chartData.join('');
}

function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting 
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;

	//extract day portion
	curPos=dateString.indexOf(sepChar);
	cDate=dateString.substring(0,curPos);
	
	//extract month portion				
	endPos=dateString.indexOf(sepChar,curPos+1);
	cMonth=dateString.substring(curPos+1,endPos);

	//extract year portion				
	curPos=endPos;
	endPos=curPos+5;			
	cYear=curValue.substring(curPos+1,endPos);
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth-1,cDate);	
	return dtObject;
}

function valida(cPassword) {
	// -- Validamos que la contraseña contiene todos los caracteres que solicitamos
	var numberChars = "0123456789";
	var upperChars = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
	var otherChars = "_!@#$%&*()=+|<>?.-,";
	var num_errores=0;
	var cad_error="";
	
	num_errores=0;
	if (cPassword.length < 9) {
		cad_error = cad_error + "La contraseña contiene menos de 9 caracteres.\n";
		num_errores=num_errores + 1 ;
	}

	if (Contiene(cPassword,upperChars)==false) {
		cad_error = cad_error + "La contraseña debe de contener un caracter alfabético.\n";
		num_errores=num_errores + 1 ;
	}

	if (Contiene(cPassword,numberChars)==false) {
		cad_error = cad_error + "La contraseña debe de contener un caracter numérico.\n";
		num_errores=num_errores + 1 ;
	}

	if (Contiene(cPassword,otherChars)==false) {
		cad_error = cad_error + "La contraseña debe de contener un carácter especial.\n";
		num_errores=num_errores + 1 ;
	}
	
	if (cPassword != document.forms['modify'].password2.value) {
		cad_error = cad_error + "Las contraseñas no coinciden.\n";
		num_errores=num_errores + 1 ;
	}

	if (cPassword == document.forms['modify'].old_password.value) {
		cad_error = cad_error + "La contraseña debe ser diferente a la anterior.\n";
		num_errores=num_errores + 1 ;
	}

	if (num_errores > 0) {
		alert("Han ocurrido " + num_errores + " errores.\n" + cad_error);
		return false;
	}
	else {
		return true;
	}
}

function validaUsuario(dato)
{
	if (valida(dato)==true) {
		alert("La contraseña es valida-");
	}
}

function validaFormulario(dato)
{
	if (valida(dato)==true) {
		return true;
	}
	else
		return false;
}

function Contiene(cPassword,cadena) {
	// Se utiliza para validar los passwords
	var aux;
	var aux2;
	var resultado;
	resultado=false;	
	for (aux=0;aux < cPassword.length; aux++) {
		for (aux2=0;aux2<cadena.length;aux2++) {
			if (cPassword.charAt(aux)==cadena.charAt(aux2)) {
				resultado=true;
				aux2=cadena.length;
				aux=cPassword.length;
			}
		}
	}	
	return resultado;
}

//Google Charts
var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
function simpleEncode(valueArray,maxValue) {

var chartData = ['s:'];
  for (var i = 0; i < valueArray.length; i++) {
    var currentValue = valueArray[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
return chartData.join('');
}

function simpleEncode2(valueArray,valueArray2,maxValue) {

var chartData = ['s:'];
  for (var i = 0; i < valueArray.length; i++) {
    var currentValue = valueArray[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
  chartData.push(',');
  for (var i = 0; i < valueArray2.length; i++) {
    var currentValue = valueArray2[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
  
return chartData.join('');
}


// Cargar los bookmarks
try {
if (typeof($) != "undefined") {
    $(document).ready(function(){
		try {
			var zIndexNumber = 1000;
			$('div').each(function() {
					$(this).css('zIndex', zIndexNumber);
					zIndexNumber -= 10;
			});
		
			$("#botonbookmarks").click(function(){
				if($(this).attr("clicked") == "false" || !$(this).attr("clicked")) {
					$(this).nextAll("#panelbookmarks").slideDown();
					$(this).nextAll("#panelbookmarks").css("z-index","1111");
					$(this).attr("clicked",true);
				}
				else {
					$("#panelbookmarks").hide("slow");
					$(this).attr("clicked",false);
				}
			});
		} catch(e) {}
    });
}
} catch(e) {}

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};
