// JavaScript Document
// Ajax para trabajar con Shop items.

function setBrowser(){
	if (document.all)
	{
		isIE4 = true;isNav6 = true;
	}
	else if (document.layers)
	{
		isNav4 = true
	}
	else if (document.getElementById)
	{
		isNav6 = true
	}
}

function getElementById( strId ){
setBrowser();
    if (isNav6)
    {
        return document.getElementById( strId );
    }
    else if (isIE4)
    {
        return document.all[strId]
    }
    else
	{
		return null
	}
}
function getElementObject( element )
{
setBrowser();
	var oElement = null;
	
	// get the element
	if (typeof element == "object")
	{
		oElement = element;
	}
	else if (typeof element == "string")
	{
		oElement = getElementById( element );
	}
	return oElement;	
}
function getStyleBySelector( selector )
{
setBrowser();
    if (!isNav6)
    {
        return null;
    }
    var sheetList = document.styleSheets;
    var ruleList;
    var i, j;

    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        ruleList = sheetList[i].cssRules;
        for (j=0; j<ruleList.length; j++)
        {
            if (ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector)
            {
                return ruleList[j].style;
            }   
        }
    }
    return null;
}
function getStylePropertyById( strId, strProperty )
{
setBrowser();
    if (isNav6)
    {
        var styleObject = document.getElementById( strId );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            if (styleObject[strProperty])
            {
                return styleObject[ strProperty ];
            }
        }
        styleObject = getStyleBySelector( "#" + strId );
        return (styleObject != null) ?
            styleObject[strProperty] :
            null;
    }
    else if (isIE4)
    {
        return document.all[strId].style[strProperty];
    }
    else
	{
		return ""
	}
}
function setStylePropertyById( strId, strProperty, strValue )
{
setBrowser();
    if (isNav6)
    {
        var styleObject = document.getElementById( strId );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ strProperty ] = strValue;
        }
    }
    else if (isIE4)
    {
		if (document.all[strId] != null)
			document.all[strId].style[strProperty] = strValue;
    }
    else
	{}	//so Nav4 won't return error
}
function setStylePropertyByElement( oElement, strProperty, strValue )
{
setBrowser();
    if (isNav6)
    {
        var styleObject = oElement;
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ strProperty ] = strValue;
        }
    }
    else if (isIE4)
    {
		if (oElement != null)
			oElement.style[strProperty] = strValue;
    }
    else
	{}	//so Nav4 won't return error
}
// Verifica si un objeto es un arreglo
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
// Function utilizada para escribir contenido HTML en un id específico.
// si no recive el id ejecuta una función.
function handleHttpResponse() { 
	var arrayDvve = Array();
	var i = 0;
//alert(http.readyState);
    if (http.readyState == 4) { 
//alert("enrto "+http.status);
//getElementById("debbugging").value = http.responseText;
		if (http.status == 200) { 
			// Si no escribe contenido entonces la consulta devuelve un valor para ejecutar una funcion.
				arrayDvve = http.responseText.split("**;**");
				for(i=0; i<arrayDvve.length; i++){
					if(arrayDvve[i].indexOf("dvveFctn") != -1){
						tmp = Array();
						tmp = arrayDvve[i].split("**=**");
						eval(tmp[1]);
					}
					if(arrayDvve[i].indexOf("dvveId") != -1){
						tmp = Array();
						tmp = arrayDvve[i].split("**=**");
						getElementById(tmp[1]).innerHTML = '';
						getElementById(tmp[1]).innerHTML = arrayDvve[1];
					}
				}
				enProcesoSetStatus(false);
		}else{
			alertar(0);
			enProcesoSetStatus(false);
		}
    }
}

function getHTTPObject(){
	var xmlhttp = '';
// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) { 
			xmlhttp = false; 
		}
	}
// code for IE
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return xmlhttp;
}
// Setea el estado de proceso y nos muestra el LOADING img
function enProcesoSetStatus(stdo){
	enProceso = stdo;
	if(enProceso)
		doLoadin();
	else
		undoLoadin();
}
// Retorna el estado de proceso
function enProcesoGetStatus(){
	return enProceso;
}
// Genera el Loading Div Layer
// Lo genera en el centro de la pantala del usuario.
function doLoadin(){
	LeftPosition = (screen.width) ? (screen.width-172)/2 : 0; 
	TopPosition = (screen.height) ? (screen.height-16)/2 : 0;
	formato = '<div id="Layer1" style="position:absolute; left:'+LeftPosition+'px; top:'+TopPosition+'px; width:172; height:16; z-index:1">';
	formato += '<table width="172" border="0" cellspacing="0" cellpadding="0">  <tr>    <td width="22" height="16"><img src="../imgs/indicator.gif" width="16" height="16"></td>    <td width="150"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Loading...</font></td>  </tr></table>';
	formato += '</div>';
	getElementById('loadingSpace').innerHTML = formato;
}
// Oculta el Loading Div Layer
function undoLoadin(){
	getElementById('loadingSpace').innerHTML = '';
}
// lo usamos para ver si hay un proceso activo
var enProceso = false;
// Creamos el objeto XMLHttpRequest
var http = getHTTPObject();

// AJAX SITE FUNCTIONS
// Asigna el valor de tarifa con el que el usuario navegará el contenido del sitio.
function setTarifa(){
	var valor = escape(arguments[0]); // Codigo de Tarifa
	var idProd = arguments[1]; // ID de producto en tránsito.
	var idCad = arguments[2]; // ID de categoria en transito.
	if (!enProcesoGetStatus() && http) {
		var url = "../includes/ajax_Turismo_Consulta.asp?opt=setTarifa&ivalue=" + valor + "&itemid=" + idProd+"&catid="+idCad;
//		document.getElementById('debbugging').value = url;
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		enProcesoSetStatus(true);
		http.send(null);
    }
}
function escribirLista(){
	http = getHTTPObject();
	var setStatus = arguments[0]; // Estado de la variable que indica si mostrará o no el cargando - LO MUESTRA POR DEFECTO
	var idProd = arguments[1]; // ID de producto en tránsito.
	var idCad = arguments[2]; // ID de categoria en transito.
	var url = "../includes/ajax_Turismo_Consulta.asp?opt=escribirLista&itemid="+idProd+"&catid="+idCad;
//	document.getElementById('debbugging').value = url;
	http.open("GET", url, true);
	http.onreadystatechange = handleHttpResponse;
	enProcesoSetStatus(true);
	http.send(null);
}

function comprarItem(){
	http = getHTTPObject();
	var addLess = arguments[0]; // Indica si se está comprando (true) o quitando (less)
	var idProd = arguments[1]; // ID de producto en tránsito.
	var prodid = arguments[2]; // ID del paquete navegado
	var url = "../includes/ajax_Turismo_Consulta.asp?opt=comprarItem&addless="+addLess+"&itemid="+idProd+"&prodid="+prodid;
	http.open("GET", url, true);
	http.onreadystatechange = handleHttpResponse;
	enProcesoSetStatus(true);
	http.send(null);
}
function validarPaquete(){
	http = getHTTPObject();
	var prodid = arguments[0]; // ID del paquete navegado
	var url = "../includes/ajax_Turismo_Consulta.asp?opt=validarPaquete&prodid="+prodid;
	http.open("GET", url, true);
	http.onreadystatechange = handleHttpResponse;
	enProcesoSetStatus(true);
	http.send(null);
}