/**********************************************************************
    Javascript functions for Magic-Labs
    Generated by EL - SM
**********************************************************************/
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
}
function toggleElementDisplay( element, strStyle )
{
setBrowser();
    // strStyle = (none,block,inline)
    var strID

    //get the element id
    if (typeof element == "object")
    {
        strID = element.id
    }
    else if (typeof element == "string")
    {
        strID = element
    }
    if ((strID != "") && (strID != null)){
        if ( getStylePropertyById( strID,'display')=='none' ){
            setStylePropertyById( strID, 'display', strStyle ) //show the element
        }else{
            setStylePropertyById( strID, 'display', 'none' ) //hide the element
        }
    }
}

function cambiarImagen(){
    img = arguments[0];
    var imagen = img.src.substr(img.src.lastIndexOf("/"));
    if(imagen.indexOf("mas") != -1){
        img.src = img.src.substr(0,img.src.lastIndexOf("/"))+"/cuadro_bajo_menos.jpg";
        for(i=1;i<3;i++){
            document.getElementById(arguments[i]).disabled = false;
        }
    }else{
        img.src = img.src.substr(0,img.src.lastIndexOf("/"))+"/cuadro_bajo_mas.jpg";
        for(i=1;i<arguments.length;i++){
            document.getElementById(arguments[i]).disabled = true;
        }
    }
}
function disableOpciones(){
    for(i=0;i<arguments.length;i++){
        document.getElementById(arguments[i]).disabled = true;
    }
}
function calcularPvp (txt, pvp, oBj, oBjtp, oBjtc) {
    if(reales(txt.value)){
        tmp = String(opciones[getElementById(oBj).selectedIndex][2] * txt.value);
        tmp = Math.round(tmp*100)/100;
        getElementById(pvp).innerHTML = setDecimales(tmp);
        getElementById(oBjtp).innerHTML = String(opciones[getElementById(oBj).selectedIndex][3]);
        getElementById(oBjtc).innerHTML = String(opciones[getElementById(oBj).selectedIndex][4]);

    }else{
        txt.value = "";
    }
}
function calcularGenvio(oBj){
    if (oBj.selectedIndex > 0) {
        tmp = String(opciones[oBj.options[oBj.selectedIndex].value][3]);
        getElementById("genvio").innerHTML = setDecimales(tmp);
        if(opciones[oBj.options[oBj.selectedIndex].value][2] != "0.00") {

            getElementById("iva").innerHTML = "<span class=\"seleccionar\">(+IVA)</span>";
        } else {

            getElementById("iva").innerHTML = "";
        }
    }
}
function NewWin(url,name,xpos,ypos,width,height){
       newWin=window.open(url,name,"screenX="+xpos+",screenY="+ypos+",WIDTH="+width+",HEIGHT="+height+",location=0,resizable=1,status=0,titlebar=1,directories=0,toolbar=0,menubar=0,scrollbars=1,status=0"       );
       newWin.focus();
       onscreen=true;
}
function setDecimales(precio){
    if(!precio){
        return "0.00";
    }else{
        precio = new String(precio);
    }
    if(precio.indexOf(".") != -1){
        if(precio.substr(precio.indexOf(".")+1).length >=3 ){
            precio = precio.substr(0,precio.indexOf(".")+4);
                precio = precio.substr(0,precio.indexOf(".")+4);
            if(precio.substr(precio.length -1) >=5){
                precio = String(Number(precio) - Number("0.00"+precio.substr(precio.length -1)) - 0.010);
                if(precio.substr(precio.indexOf(".")+1).length != 2)
                    if(precio.substr(precio.indexOf(".")+1).length > 3)
                        precio = precio.substr(0,precio.indexOf(".")+3);
                    precio = setDecimales(precio);
            }else{
                precio = precio.substr(0,precio.length -1);
            }
            return precio;
        }else{
            if(precio.substr(precio.indexOf(".")+1).length < 2){
                do{
                    precio = precio.substr(0,(precio.length-String(precio.indexOf(".")).length))+precio.substr(precio.indexOf(".")+1) + 0;
                }while(precio.substr(precio.indexOf(".")+1).length == 3);
            }
            return precio;
        }
    }else{
        precio = precio + ".0";
        if(precio.substr(precio.indexOf(".")+1).length < 2){
            do{
                precio = precio.substr(0,(precio.length-String(precio.indexOf(".")).length))+precio.substr(precio.indexOf(".")+1) + 0;
            }while(precio.substr(precio.indexOf(".")+1).length == 3);
        }
        return precio;
    }
}

function MM_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 MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function changeDisplay (sName, sForce) {

    var oName = document.getElementById (sName);
    if (oName== undefined) return false;

    if (sForce == undefined) {
        if ((oName.style.display == "block" && oName.style.visibility == "visible") || (oName.style.display == "" && oName.style.visibility == "")) {
            sForce = "hide";
        }
        else {
            sForce = "show";
        }
    }

    if (sForce == "hide") {
        oName.style.display = "none";
        oName.style.visibility = "hidden";
    }
    else if (sForce == "show") {
        oName.style.display = "block";
        oName.style.visibility = "visible";
    }
}
