// JavaScript Document
function objetus(file) {
    xmlhttp=false;
    this.AjaxFailedAlert = "Su navegador no soporta las funciónalidades de este sitio \n y podria experimentarlo de forma diferente a la que fue pensada.\n Por favor habilite javascript en su navegador para verlo normalmente.\n";
    this.requestFile = file;
    this.encodeURIString = true;
    this.execute = false;
    if (window.XMLHttpRequest) { 
        this.xmlhttp = new XMLHttpRequest();
        if (this.xmlhttp.overrideMimeType) {
            this.xmlhttp.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                this.xmlhttp = null;
            }
        }
        if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
            this.xmlhttp = new XMLHttpRequest();
            if (!this.xmlhttp){
                this.failed = true; 
            }
        } 
    }
    return this.xmlhttp ;
}
function removeThickBoxEvents() {
        $('.thickbox').each(function(i) {
            $(this).unbind('click');
        });
    }

function bindThickBoxEvents() {
        removeThickBoxEvents();
        tb_init('a.thickbox, area.thickbox, input.thickbox');
    }

function xmlhttpPost
(code,_pagina,valorget,valorpost,capa){ 
	//alert(_pagina);
	ajax=objetus(_pagina);
//	valorpost="";
    if(valorpost!=""){
        ajax.open("POST", _pagina+"?"+valorpost+"&tiempo="+new Date().getTime(),true);
    } else {
        ajax.open("GET", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    }
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1){
            document.getElementById(capa).innerHTML = 
				"<img src='./images/ajax-loader.gif' align='center'> Aguarde por favor...";
        }
        if (ajax.readyState==4) {
			document.getElementById(capa).innerHTML ="";
            if(ajax.status==200)
            {
				switch(code)
				{
					case 1:
							rExp = /\n/gi;
							result = ajax.responseText;
							result = result.replace(rExp, " ");
							//alert(result);
							eval(result);
							break;
					case 3:
					document.getElementById(capa).innerHTML = ajax.responseText;
					//tb_init('a.thickbox, area.thickbox, input.thickbox');
					fdTableSort.init('allStock');
					break;
					case 4:
					document.getElementById(capa).innerHTML = ajax.responseText;
					//tb_init('a.thickbox, area.thickbox, input.thickbox');
					fdTableSort.init('allProducts');
					break;
					case 22: 
					rExp = /\n/gi;
					result = ajax.responseText;
					result = result.replace(rExp, "");
					if (result == "redirect_index") 
					{ 
					window.location = "main_private.php";				
					} else{
					document.getElementById(capa).innerHTML = ajax.responseText;
					
					}

					break;
					default:		
							document.getElementById(capa).innerHTML = ajax.responseText;
							break;
				}
			
			}
			else if(ajax.status==404)
            {
                capa.innerHTML = "La direccion no existe";
            }
            else
            {
                capa.innerHTML = "Error: ".ajax.status;
            }
        }
    }
    if(valorpost!=""){
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send(valorpost);
    } else {
        ajax.send(null);
    }
} 
/********************** 
* Vaciar desplegable: recibe el ID del elemento 
********/  
 function emptySelect(objID){  
     document.getElementById(objID).options.length = 0; 
 }  

var tasks = new Array();
function fifo()
{	
	var ocupado = document.getElementById('ocupado');
	if(ocupado.value == '0' && tasks.length)
	{
		ocupado.value = '1';
		var task = tasks.pop();
		//alert(task);
		eval(task);
		ocupado.value = '0';
		if(tasks.length)
			setTimeout('fifo()',100);
	}
	else
		setTimeout('fifo()',100);
}

function getquerystring(process) 
{
	var qstr  = '';
	var lista = new Array();
	switch(process)
	{
		case 'productIn':
			var proType = document.getElementById('In_ProductType');
			var proCod = document.getElementById('codproducto');			
			var proName = document.getElementById('nameproducto');
			var proDesc = document.getElementById('description');
			var proState = document.getElementById('In_EstadoProd');
			var proUnit = document.getElementById('In_Unidad');
			var proPrice = document.getElementById('unitprice');	
			var proPriv = getRadioButtonSelectedValue(document.mainProduct.private);
			var proLoc = document.getElementById('location');	
			
			
			lista.push('productType='+proType.value);
			lista.push('productCod='+proCod.value);
			lista.push('productName='+proName.value);
			lista.push('productDesc='+proDesc.value);
			lista.push('productState='+proState.value);
			lista.push('productUnit='+proUnit.value);
			lista.push('productPrice='+proPrice.value);
			lista.push('productPriv='+proPriv);
			lista.push('productLoc='+proLoc.value);
			
			//alert(lista.join('&'));
		break;
		case 'productGet':
			var proType = document.getElementById('In_ProductType');

			lista.push('productType='+proType.value);
		break;
		case 'fproductGet':			
			var proType = document.getElementById('fIn_ProductType');

			lista.push('productType='+proType.value);
			lista.push('Filtro=true');			
		break;
		case 'typeProductGet':
			var proCat = document.getElementById('fIn_Category');
			lista.push('category='+proCat.value);
			lista.push('Filtro=true');
		break;
		case 'getProductDesc':
			var producto= document.getElementById('In_Product');
			lista.push('productId='+producto.value);			
		break;
		case 'DivUnidad':
			var producto= document.getElementById('In_Product');
			lista.push('productId='+producto.value);
			lista.push('DivUnidad='+1);
		break;
		case 'DivUnidad2':
			var producto= document.getElementById('In_Product');
			lista.push('productId='+producto.value);
			lista.push('DivUnidad='+1);
			lista.push('documento=yes');
		break;
		case 'stockIn':
			var stockCompany = document.getElementById('In_Company');
			var stockCantidad = document.getElementById('cantidad');			
			var stockProduct = document.getElementById('In_Product');
			var stockTalla = document.getElementById('talla');
			var stockColor = document.getElementById('color');
			
			lista.push('stockCompany='+stockCompany.value);
			lista.push('stockCantidad='+stockCantidad.value);
			lista.push('stockProduct='+stockProduct.value);
			lista.push('stockTalla='+stockTalla.value);
			lista.push('stockColor='+stockColor.value);
		break;
		case 'divListStock':
			lista.push('Filtro=true');
			var stockCompany = document.getElementById('fIn_Company');
			var proCat = document.getElementById('fIn_Category');
			var proType = document.getElementById('fIn_ProductType');
			var stockProduct = document.getElementById('fIn_Product');
			
			if (stockCompany.value != "") lista.push('stockCompany='+stockCompany.value);
			if (proCat.value !="") lista.push('category='+proCat.value);
			if (proType.value !="") lista.push('productType='+proType.value);
			if (stockProduct.value !="") lista.push('stockProduct='+stockProduct.value);
		break;
		case 'productDetailGet':
		var stockProduct = document.getElementById('In_Product');
		if (stockProduct.value !="") lista.push('stockProduct='+stockProduct.value);
		break;
		case 'divDetailStock':
		var stockProduct = document.getElementById('In_Product');
		if (stockProduct.value !="") lista.push('stockProduct='+stockProduct.value);
		break;
		case 'categoryIn':
			var categoryTop = document.getElementById('In_Topico');
			var categoryName = document.getElementById('nombre');
			var categoryVig = document.getElementById('In_Vigencia');
			
			lista.push('categoryTop='+categoryTop.value);
			lista.push('categoryName='+categoryName.value);
			lista.push('categoryVig='+categoryVig.value);
			//alert(lista.join('&'));
		break;
		case 'productTypeIn':
			var productCat = document.getElementById('In_Category');
			var productNombre = document.getElementById('nombre');
			var productVig = document.getElementById('In_Vigencia');
			
			lista.push('productCat='+productCat.value);
			lista.push('productNombre='+productNombre.value);
			lista.push('productVig='+productVig.value);
			//alert(lista.join('&'));
		break;
		case 'divListProducts':
			var productTopic = document.getElementById('fIn_Topico');
			if (productTopic.value !="") lista.push('productTopic='+productTopic.value);
		break;
		case 'divListCategory':
			lista.push('Filtro=true');
			var vigencia = document.getElementById('fIn_Vigencia');
			var topico = document.getElementById('fIn_Topico');
			if (vigencia.value !="") lista.push('vigencia='+vigencia.value);
			if (topico.value !="") lista.push('topico='+topico.value);
		break;
		case 'divProductType':
			lista.push('Filtro=true');
			var vigencia = document.getElementById('fIn_Vigencia');
			var topico = document.getElementById('fIn_Topico');
			var category = document.getElementById('fIn_Category');
			if (vigencia.value !="") lista.push('vigencia='+vigencia.value);
			if (topico.value !="") lista.push('topico='+topico.value);
			if (category.value !="") lista.push('category='+category.value);
		break;
		case 'saveImage':
			var upload=document.getElementById('upload');
			var productId=document.getElementById('id_producto');
			var userfile=document.getElementById('userfile');
			if (upload.value !="") lista.push('upload='+upload.value);
			if (productId.value !="") lista.push('productId='+productId.value);
			if (userfile.value !="") lista.push('userfile='+userfile.value);
			//alert(lista.join('&'));
		break;
		case 'enterpriseIn':
			var EmprName = document.getElementById('nombre');
			var EmprRut = document.getElementById('rut');			
			var EmprDir = document.getElementById('direccion');
			var EmprCom = document.getElementById('comuna');
			var EmprCity = document.getElementById('ciudad');
			var EmprTel1 = document.getElementById('telefono_1');
			var EmprTel2 = document.getElementById('telefono_2');	
			var EmprCel = document.getElementById('celular');
			var EmprCon1 = document.getElementById('contacto1');
			var EmprE1 = document.getElementById('email1');
			var EmprCon2 = document.getElementById('contacto2');
			var EmprE2 = document.getElementById('email2');
			var EmprFax = document.getElementById('fax');
			var EmprType = document.getElementById('In_EnterpriseType');
			var EmprState = getRadioButtonSelectedValue(document.mainEnterprise.state);
						
			
			lista.push('empresaName='+EmprName.value);
			lista.push('empresaRut='+EmprRut.value);
			lista.push('empresaDir='+EmprDir.value);
			lista.push('empresaCom='+EmprCom.value);
			lista.push('empresaCity='+EmprCity.value);
			lista.push('empresaTel1='+EmprTel1.value);
			lista.push('empresaTel2='+EmprTel2.value);
			lista.push('empresaCel='+EmprCel.value);
			lista.push('empresaCon1='+EmprCon1.value);
			lista.push('empresaE1='+EmprE1.value);
			lista.push('empresaCon2='+EmprCon2.value);
			lista.push('empresaE2='+EmprE2.value);
			lista.push('empresaFax='+EmprFax.value);
			lista.push('empresaType='+EmprType.value);
			lista.push('empresaState='+EmprState);
			
			
			//alert(lista.join('&'));
		break;
		default:
			break;
	}
	qstr = lista.join('&'); // NOTE: no '?' before querystring
	return qstr;
}
function getRadioButtonSelectedValue(ctrl)
{
    for(i=0;i<ctrl.length;i++)
        if(ctrl[i].checked) return ctrl[i].value;
return "";
}
function showThickbox(a){
    var t = a.title || a.name || null;
    var g = a.rel || false;
    tb_show(t,a.href,g);
    a.blur();
    return false;
  };

/****************/
function despliega(id,reg)
{
	var det      = document.getElementById('detalle'+id);
	var fotos_    = document.getElementById('fotos_'+id);
	//var abiertas = null;
	var trProductos;
	//var padre;
	
	
	
	if(det.style.display == 'none')
	{
		fotos_.innerHTML   = '<img src="./images/ajax-loader.gif" align="center"> Aguarde por favor...';
		det.style.display = '';
		var fotos_producto = "xmlhttpPost(4,'ajax/../showGalleryProduct.php','','id_producto="+id+"' ,'fotos_"+ id +"');";
		alert(fotos_producto);
		tasks.push(fotos_producto);
		fifo();
	}else{
		det.style.display = '';
	}
	
	
}