var CKEditorFlag=0;
function stopUpload(success, archivodestino, idpr, mensaje, Ri, IdEtiqueta)
{
    if (success == 1)
    {
        if(mensaje == "")
        {
            if (archivodestino != "")
            {
                var ajaxformulario = nuevoajax();
                ajaxformulario.open("GET", "webapp_aux_image_converter.php?archivodestino="+archivodestino+"&idpr="+idpr, true);
                ajaxformulario.onreadystatechange=function()
                {
                    if (ajaxformulario.readyState==4 && ajaxformulario.status == 200)
                    {
                        ajaxformulario.responseText;
                        pageTracker._trackPageview("/webapp_aux_image_converter.php");
                        ListarProductosPersona(Ri,IdEtiqueta, idpr);
                    }
                }

                ajaxformulario.send(null);


            }
            else
                ListarProductosPersona(Ri,IdEtiqueta,idpr);//ListarProductosPersona(Ri,IdEtiqueta);
        }
        else
        {
            document.getElementById('f1_upload_process').style.visibility = 'hidden';
        }
    }
    else
        var result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
    return true;
}

function cgimg(success, archivodestino, idpr, nrofoto,Ri,IdEtiqueta)
{
    if (success == 1)
    {
        if (archivodestino != "")
        {
            var ajaxformulario = nuevoajax();
            ajaxformulario.open("GET", "webapp_aux_image_converter.php?archivodestino="+archivodestino+"&idpr="+idpr+"&nrofoto="+nrofoto, true);
            ajaxformulario.onreadystatechange=function()
            {
                if (ajaxformulario.readyState==4 && ajaxformulario.status == 200)
                {
                    ajaxformulario.responseText;
                    pageTracker._trackPageview("/webapp_aux_image_converter.php");
                    imagenes(idpr,Ri,IdEtiqueta);
                }
            }

            ajaxformulario.send(null);


        }
    }
    else
        var result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
    return true;
}

function startUpload()
{
    document.getElementById('f1_upload_process').style.visibility = 'visible';
    document.getElementById('f1_upload_form').style.visibility = 'hidden';
    return true;
}

function ValidaProducto(modo)
{
    if(document.getElementById("Producto").value == "")
    {
        alert("The field Name can not be left blank.")
        document.getElementById("Producto").focus();
        return false;
    }
    if(document.getElementById("NA").checked == false && document.getElementById("AC").checked == false)
    {
        if(document.getElementById("Precio").value == "")
        {
            alert("The field Price can not be left blank.")
            document.getElementById("Precio").focus();
            return false;
        }
        if(EsDecimal(document.getElementById("Precio").value))
        {
            alert("Invalid price format.")
            document.getElementById("Precio").focus();
            return false;
        }
    }
    if (document.getElementById("ImagenProducto").value)
    {
        var archivo = document.getElementById("ImagenProducto").value;
        var extensiones_permitidas = new Array(".gif", ".jpg", ".png");
        extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase();
        permitida = false;
        for (var i = 0; i < extensiones_permitidas.length; i++)
            if (extensiones_permitidas[i] == extension)
                permitida = true;
        if(!permitida)
        {
            alert("Is not a valid file extension. The extension must be gif, jpg or png.");
            document.getElementById("ImagenProducto").focus();
            return false;
        }
    }
    if(document.getElementById("Texto").value == "")
    {
        alert("The field Product description can not be left blank.")
        document.getElementById("Texto").focus();
        return false;
    }
    if(document.getElementById("Texto").value.length > 2000)
    {
        alert("The field Text have exceeded the maximum size allowed (2000 characters).")
        document.getElementById("Texto").focus();
        return false;
    }
    /*
    var cantidad = document.getElementById("Texto").value.match(/\n+/g);
    var cuenta = cantidad?cantidad.length:0;
    if (cuenta+1 > 30)
    {
        alert("No puede haber m�s de 30 saltos de l�nea.");
        return false;
    }*/
    if (document.getElementById("formvideo").style.display!='none')
    {
        if (document.getElementById("Video").value!='')
        {
            if(!ValidarUrl(document.getElementById("Video").value))
            {
                alert("Is not a valid Website.");
                document.getElementById("Video").select();
                return false;
            }
        }
    }
    if (document.getElementById("formfoto").style.display!='none')
    {
        if (document.getElementById("Foto").value!='')
        {
            if(!ValidarUrl(document.getElementById("Foto").value))
            {
                alert("Is not a valid Website.");
                document.getElementById("Foto").select();
                return false;
            }
        }
    }
    /*if(document.getElementById("formvideo").style.display!='none' && document.getElementById("formfoto").style.display!='none')
    {
		if(document.getElementById("Foto").value!='' && document.getElementById("Video").value!='')
		{
			if(document.getElementById("Foto").value == document.getElementById("Video").value)
			{
				alert("The Websites can not be the same.");
				document.getElementById("Video").select();
				return false;
			}
		}
	}*/
    if (document.getElementById("formdownload").style.display!='none')
    {
        if (document.getElementById("Download").value!='')
        {
            if(!ValidarUrl(document.getElementById("Download").value))
            {
                alert("Is not a valid Website.");
                document.getElementById("Download").select();
                return false;
            }
        }
    }
    if (document.getElementById("formlink").style.display!='none')
    {
        if (document.getElementById("Link").value!='')
        {
            if(!ValidarUrl(document.getElementById("Link").value))
            {
                alert("Is not a valid Website.");
                document.getElementById("Link").select();
                return false;
            }
        }
    }
    if(document.getElementById("formvideo").style.display!='none' && !(ValidarUrlDiferentesProductos(document.getElementById("Video"))))
        return false;
    if(document.getElementById("formfoto").style.display!='none' && !(ValidarUrlDiferentesProductos(document.getElementById("Foto"))))
        return false;
    if(document.getElementById("formdownload").style.display!='none' && !(ValidarUrlDiferentesProductos(document.getElementById("Download"))))
        return false;
    if(document.getElementById("formlink").style.display!='none' && !(ValidarUrlDiferentesProductos(document.getElementById("Link"))))
        return false;
    startUpload();
    return true
}

function Validarcamposproducto()
{
    if(document.getElementById("Producto").value == "")
    {
        alert("The field Name can not be left blank.")
        document.getElementById("Producto").focus();
        return false;
    }
    if(document.getElementById("Texto").value.length > 2000)
    {
        alert("The field Description have exceeded the maximum size allowed (2000 characters).")
        document.getElementById("Texto").focus();
        return false;
    }

    if(document.getElementById("Precio").value == "")
    {
        if (document.getElementById("NA").checked || document.getElementById("AC").checked ) {
            return true;
        } else {
            alert("The field Price can not be left blank.");
            document.getElementById("Precio").focus();
            return false;
        }
    }

    if(isNaN(document.getElementById("Precio").value.replace(',','.') ))
    {
        alert("Invalid price format.")
        document.getElementById("Precio").focus();
        return false;
    }
    return true;
}

function DetalleProducto(Modo, IdPersona, Ri, IdEtiqueta, IdProducto, Cadena, RiInactivo)
{
    var ajaxformulario2 = nuevoajax();
    var Aleat = new Date();
    if(Cadena == null)
        Cadena = "";
    if(RiInactivo == null)
        RiInactivo = 0;
    ajaxformulario2.open("GET", "webapp_aux_es_editable.php?IdProducto="+IdProducto+"&Aleat="+Aleat, true);
    ajaxformulario2.onreadystatechange=function()
    {
        if (ajaxformulario2.readyState==4 && ajaxformulario2.status == 200)
        {
            var respuesta = ajaxformulario2.responseText;
            if(respuesta.substr(0,2) == "OK")
            {
                var formulario = document.getElementById("panelderecho");
                formulario.innerHTML = "";
                var ajaxformulario = nuevoajax();
                ajaxformulario.open("GET", "webapp_fproducto.php?modo="+Modo+"&idpr="+IdProducto+"&IdPersona="+IdPersona+"&Ri="+Ri+"&IdEtiqueta="+IdEtiqueta+"&Cadena="+Cadena+"&RiInactivo="+RiInactivo, true);
                ajaxformulario.onreadystatechange=function()
                {
                    if (ajaxformulario.readyState==4 && ajaxformulario.status == 200)
                    {
                        pageTracker._trackPageview("/webapp_fproducto.php");
                        formulario.innerHTML = ajaxformulario.responseText;
                        document.getElementById("Producto").focus();
                                    var scs = ajaxformulario.responseText.extractScript();    //capturamos los scripts
            //myDivUoTroLugar.innerHTML = myAjax.responseText.stripScript();    //eliminamos los scripts... ya son innecesarios
            scs.evalScript();       //ahora si, comenzamos a interpretar todo
                    }
                }
                ajaxformulario.send(null);
            }
            else
                alert("The product can't be editable");
        }
    }
    ajaxformulario2.send(null);
}

function ValidarSeleccion (Modo, IdPersona, Ri, IdEtiqueta, sure)
{
    var IdProducto = document.forms['subcategorias']['IdProducto'];
    var bandera = 0;
    var tieneproducto = 0;
    j=0;
    var arrayproductos=new Array();
    for (i=0;i<IdProducto.length;i++)
    {
        tieneproducto = 1;
        if(IdProducto[i].checked == 1 && Modo!='E' && Modo!='UP' && Modo!='DOWN' && Modo!='NEXT' && Modo!='PREV' )
        {
            IdPr = IdProducto[i].value;
            bandera = 1;
            break;
        }
        else if(IdProducto[i].checked == 1 && (Modo=='E'|| Modo=='UP' || Modo=='DOWN' || Modo=='NEXT' || Modo=='PREV') )
        {

        arrayproductos[j] = IdProducto[i].value;
        productosSeleccionados[j] = IdProducto[i].value;
        j++;
         bandera = 1;
        }

    }
    if(tieneproducto == 0)
    {
        if(IdProducto.checked == 1)
        {
            IdPr = IdProducto.value;
            bandera = 1;
        }
    }
    if(bandera == 0)
    {
        alert("Select a product.");
        return false;
    }
    switch(Modo)
    {
        case 'N':
            DetalleProducto(Modo, IdPersona, Ri, IdEtiqueta, IdPr);
            break;
        case 'M':
            DetalleProducto(Modo, IdPersona, Ri, IdEtiqueta, IdPr);
            break;
        case 'E':
            if(confirm(sure+'?'))
                for(t=0;t<j;t++)
                    AmbProducto('E', arrayproductos[t],0,Ri, IdEtiqueta);
             else
                return;     
            break;
        case 'UP':
            for(t=0;t<j;t++)
            {
             CambiarPosicion(Modo, arrayproductos[t], Ri);
            }
            break;
        case 'DOWN':
             for(t=j;t>0;t--)
            {
             CambiarPosicion(Modo, arrayproductos[t-1], Ri);
            }
            break;
        case 'NEXT':
             for(t=0;t<j;t++)
            {
             CambiarPosicion(Modo, arrayproductos[t], Ri);
            }
            break;
        case 'PREV':
             for(t=j;t>0;t--)
             {
              CambiarPosicion(Modo, arrayproductos[t-1], Ri);
             }
            break;
    }
    if(Modo != "M") ListarProductosPersona(Ri,IdEtiqueta,0);
}

function CambiarPosicion(Modo, IdProducto, Ri)
{
    var ajaxformulario = nuevoajax();
    var Aleat = new Date();
    ajaxformulario.open("GET", "webapp_aux_cambiar_posicion.php?Modo="+Modo+"&IdProducto="+IdProducto+"&Aleat="+Aleat, true);
    ajaxformulario.onreadystatechange=function()
    {
        if (ajaxformulario.readyState==4 && ajaxformulario.status == 200)
        {
            var respuesta = ajaxformulario.responseText;
            if(respuesta.substr(0,2) == "OK")
            {
                Ri = (respuesta.substr(2,(respuesta.length)-3))+'0';
                
            }
            else
                alert (respuesta);
        }
    }
    ajaxformulario.send(null);
}

function MostrarVideo()
{
    if(document.getElementById("formvideo").style.display == "none")
        document.getElementById("formvideo").style.display = "block";
    else
        document.getElementById("formvideo").style.display = "none";
}

function MostrarDownload()
{
    if(document.getElementById("formdownload").style.display == "none")
        document.getElementById("formdownload").style.display = "block";
    else
        document.getElementById("formdownload").style.display = "none";
}

function MostrarLink()
{
    if(document.getElementById("formlink").style.display == "none")
        document.getElementById("formlink").style.display = "block";
    else
        document.getElementById("formlink").style.display = "none";
}

function MostrarFoto()
{
    if(document.getElementById("formfoto").style.display == "none")
        document.getElementById("formfoto").style.display = "block";
    else
        document.getElementById("formfoto").style.display = "none";
}

function MostrarURLProducto()
{
    if(document.getElementById("formpages").style.display == "none")
    {
        document.getElementById("formpages").style.display = "block";
        if (document.getElementById("IdVideo").checked!='')
            document.getElementById("formvideo").style.display = "block";
        if (document.getElementById("IdFoto").checked!='')
            document.getElementById("formfoto").style.display = "block";
    }
    else
    {
        document.getElementById("formpages").style.display = "none";
        document.getElementById("formfoto").style.display = "none";
        document.getElementById("formvideo").style.display = "none";
    }
}

function ControlarChecks(Origen)
{
    NA = document.getElementById('NA');
    AC = document.getElementById('AC');
    Precio = document.getElementById('Precio');
    if(Origen=='NA')
    {
        if(NA.checked==true)
        {
            AC.checked = false;
            Precio.disabled= true;
        }
    }
    else
    {
        if(AC.checked==true)
        {
            NA.checked = false;
            Precio.disabled= true;
        }
    }
    if(AC.checked==false && NA.checked==false)
    {
        Precio.disabled= false;
    }
}

function ValidarUrlDiferentesProductos(CampoOrigen)
{
    if(CampoOrigen.value!='')
    {
        if(document.getElementById("formvideo").style.display!='none')
        {
            if((document.getElementById("Video").value==CampoOrigen.value) && (CampoOrigen.id!=document.getElementById("Video").id))
            {
                alert("The Websites can not be the same.");
                document.getElementById("Video").select();
                return false;
            }
        }
        if(document.getElementById("formfoto").style.display!='none')
        {
            if((document.getElementById("Foto").value==CampoOrigen.value) && (CampoOrigen.id!=document.getElementById("Foto").id))
            {
                alert("The Websites can not be the same.");
                document.getElementById("Foto").select();
                return false;
            }
        }
        if(document.getElementById("formdownload").style.display!='none')
        {
            if((document.getElementById("Download").value==CampoOrigen.value) && (CampoOrigen.id!=document.getElementById("Download").id))
            {
                alert("The Websites can not be the same.");
                document.getElementById("Download").select();
                return false;
            }
        }
        if(document.getElementById("formlink").style.display!='none')
        {
            if((document.getElementById("Link").value==CampoOrigen.value) && (CampoOrigen.id!=document.getElementById("Link").id))
            {
                alert("The Websites can not be the same.");
                document.getElementById("Link").select();
                return false;
            }
        }
    }
    return true;
}
