/*===========================================================
 Funciones para el rollover de imagenes (swapImgRestore y swapImage)
============================================================*/
function swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++) 
  	x.src=x.oSrc;
}

function swapImage() { //v3.0
	var i,j=0,x,a=swapImage.arguments; 
	document.MM_sr=new Array; 
	
	for(i=0; i<(a.length-2); i+=3)
	if ($(a[i])){
		x = $(a[i]);
		//alert(a[0])
		document.MM_sr[j++] = x; 
		if(!x.oSrc)
			x.oSrc = x.src; 
		x.src=a[i+2];
	}
}

/*===========================================================
 Funcion para quitar espacion en los extremos izquierdo y derecho de un string
============================================================*/
function trim(cadena) {
	var nuevacadenal="";
	var nuevacadenar="";
	var intLimitel;
	var intLimiter;
	
	for(i=0;i<cadena.length;i++) {
	  if(cadena.charAt(i)==" ") 
	    intLimitel++;
	  else 
	  	break;
	}
	
	nuevacadenal=cadena.substring(intLimitel,cadena.length);
	intLimiter=nuevacadenal.length;
	for(i=nuevacadenal.length-1;i>=0;i--) {
	    if(nuevacadenal.charAt(i)==" ")
			intLimiter--;
	    else
		  break;
	}
	nuevacadenar=nuevacadenal.substring(0,intLimiter);
	return(nuevacadenar)
}

// Crea un input (type,name,value)
function createInput(type,name,value)
{
	var i = document.createElement("input");
	i.name = name;
	i.type = type;
	i.value = value;
	i.id = name;
	return i;
}
//==============================================
// Cierra UL's abiertos usando style.display
//==============================================
function closeUls(){
	var uls = document.getElementsByTagName("ul");
	var t = uls.length;
	if(t < 1)return false;
	for(i=0;i<t;i++){
		if(uls[i].style.display == "block")
			uls[i].style.display = "none";
	}
}
//==============================================
// Prende o apaga un elemento usando style.display 
// Argumentos: id de elemento
//==============================================
var lastOpened = "";
var lastBotSelected = "";
function showHide(id,botId){
	if(botId != undefined){
		var defClass = $(botId).className;
		if(lastBotSelected != "")
			$(lastBotSelected).className = "";
		$(botId).className = defClass+" selected";
		$(botId).blur();
		lastBotSelected = botId;
	}
	if($(id).style.display == "none"){
		// Cierra los abiertos
		//closeUls();
		if(lastOpened != "")
			$(lastOpened).style.display = "none";
		$(id).style.display = "block";
		lastOpened = id;
	}
	else{
		$(id).style.display = "none";
		lastOpened = "";
	}
}



function showHideSimple(id){
	if($(id).style.display == "none")
		$(id).style.display = "block";
	else 
		$(id).style.display = "none";
}
//==============================================
// Prende un elemento y apaga el otro usando style.display 
// Argumentos: id de elementos (2)
//==============================================
function switchDivs(id1,id2){
	if($(id1).style.display == "none"){
		$(id1).style.display = "block";
		$(id2).style.display = "none";
	}
	else {
		$(id2).style.display = "block";
		$(id1).style.display = "none";
	}
}
//================================================
// Agrega clase addClass a tags strTags clase strClass
// Si linkTag existe, contiene url para aplicar onclick
//================================================
function elementRollOver(strTag,strClass,addClass,linkTag) {
	for (i=0;i<document.getElementsByTagName(strTag).length; i++) {
		if (document.getElementsByTagName(strTag).item(i).className == strClass) {
			var newClass = strClass+" "+addClass;
			document.getElementsByTagName(strTag).item(i).onmouseover=function(){this.className=newClass;this.style.cursor="default";return false}
       		document.getElementsByTagName(strTag).item(i).onmouseout=function(){this.className=strClass;return false}
			if(linkTag != undefined){
			document.getElementsByTagName(strTag).item(i).onclick = function(){
				var t = this.getElementsByTagName(linkTag)[0];
				var a = t.getElementsByTagName("a")[0];
				if(a){
					var href = a.getAttribute("href");
					//document.location.href = href;
				}
			}
			}
		}
	}
}
//======================================================
// Retorna array con elementos con class determinada
// Implementaci{on de getElementsByClassName
//======================================================
document.getElementsByClass = function (needle)
{
  var tag  = "div";
  if(document.getElementsByClass.arguments[1])
  	tag = document.getElementsByClass.arguments[1];
  var         my_array = this.getElementsByTagName(tag);
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}
/*======================================================
Aplica clase colored a tr's por medio de tablas de documento
 (tablas deben tener tbody)
 =======================================================*/
function colorRows(){
	var tabs = false;
	if(document.createElement && document.getElementById)
		tabs = document.getElementsByTagName("table");
	else
		tabs = document.tables;
	if(!tabs)return;
	for(var i=0;i<tabs.length;i++){
		tab = tabs[i];
		tbodies = tab.getElementsByTagName("tbody");
		for(var t = 0;t<tbodies.length;t++){
				tbody = tbodies[t];
				trs = tbody.getElementsByTagName("tr");
				for(var u=0;u<trs.length;u++){
					if(u%2)trs[u].className = "colored";
				}
		}
	}
}

/* 

abre popup
ej:javascript:open_win('101','compra_en_nuestro_mall','','http://www.mouse.cl/index.php','popup','');

abre document
ej:javascript:open_win('101','beneficios_de_tarjeta','','http://pweb1/contenidos/productos/tarjetas_credito/index.asp','document','undefined');

*/

function open_win(posicion,id,archivo,ruta,doc,argumentos){
	var wns = Array("_self","_blank");
	var str = "";
	argumentos = (argumentos != undefined)?argumentos:"";
	for(var i=0;i<open_win.arguments.length;i++){
		if(open_win.arguments[i] == "")
			str+="vacio\n";
			else
		str += open_win.arguments[i] + "\n";
	}
	//alert("aa "+str);
	grabaLog(posicion,id,archivo,"false");//true es asincrónico
	// - - - - - - -  href - - - - - - - -
	if(doc == "document"){
		//alert(doc);
		document.location.href=ruta;
	}
	else if(doc == "popup"){
		window.open(ruta,id,argumentos);
	}

	//return false;
}

// LOG
xmlhttp = null;
function grabaLog(posicion,id,archivo,modo)
{
	 if (xmlhttp != null && xmlhttp.readyState != 0 && xmlhttp.readyState != 4)
	 {
	   xmlhttp.abort();
	 }
	 
	var url = "include/asp/createlogbanner/createlog.asp?posicion="+posicion+"&id="+id+"&archivo="+archivo+"&ran="+Math.random()*100;
	//document.write(url)
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		if(!xmlhttp)return false;
		//xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,(modo=="true"));
		xmlhttp.send(null);
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)
		{
			//xmlhttp.onreadystatechange=state_Change;
			xmlhttp.open("GET",url,(modo=="true"));
			xmlhttp.send();
		}
	}
}

