﻿var strFilaSelectedId = "", strFilaSelectedNameClass = "";

var strNavegador = navigator.userAgent;
strNavegador = strNavegador.replace(" ","").toUpperCase();

var boolMSIE = (strNavegador.indexOf("MSIE") >= 0) ? true : false;
var boolFireFox = (strNavegador.indexOf("FIREFOX") >= 0) ? true : false;
var boolSafari = (strNavegador.indexOf("SAFARI") >= 0) ? true : false;

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }

function ObtenerValorCombo(IDCombo, strValue)
{
    var objCombo = document.getElementById(IDCombo);
    for(var i=0; i <= objCombo.length -1; i++)
    {        
        if (objCombo[i].value == strValue)
        {
            objCombo[i].selected = "selected";
            break;            
        }
    }
}

function DesSeleccionarFila()
{
    if (strFilaSelectedId != "" && strFilaSelectedNameClass != "")
    {
        var Tr = document.getElementById(strFilaSelectedId);
        if (Tr != null) Tr.className = strFilaSelectedNameClass;
        strFilaSelectedId = "";
        strFilaSelectedNameClass = "";
    }
}

function ObtenerFoco(strObj)
{
    var obj = document.getElementById(strObj);
    if (obj != null && !obj.disabled) obj.focus();
}

function PintaAlerta(strcampo, msg)
{
    var campo = document.getElementById(strcampo);
    if (campo != null && msg.length > 0)
    {
        var self = this;
        self.tooldiv = null;
        self.errorField = null;
        self.errorIndex = 0;
        self.ErrorStyle = "#FF0000";
        self.noStyle = "";
        self.errorSeq = [self.ErrorStyle,self.noStyle,self.ErrorStyle,self.noStyle,self.ErrorStyle,self.noStyle];
        self.intervalo = null;
        self.timeouterror = 500;
        
        ancho = campo.offsetWidth;
		ancho = ancho/2;
		
		posx = (getLeftPos(campo) + ancho);
		posy = getTopPos(campo) - 75;
        
        if(self.tooldiv == null){
            self.tooldiv =  document.createElement("DIV");
            self.tooldiv.id = "jaketolldiv";
            self.tooldiv.style.display = "none";
            self.tooldiv.style.position = "absolute";
            CrearAlerta(posx, posy, msg);
            document.body.appendChild(self.tooldiv);
        }
        self.tooldiv.style.display = "block";        
		campo.onblur = function(){
			self.tooldiv.style.display = "none";
			document.getElementById("tbGlobo").style.display = "none";
			campo.style.borderColor = "#7F9DB9";
		}
        campo.focus();
        campo.style.borderColor = "#FF0000";
        
        self.errorField = campo;	
        self.errorIndex = 0;
        DisableErrorStyle();
    }
}

function getLeftPos(inputObj)
{
      var returnValue = inputObj.offsetLeft;
      while((inputObj = inputObj.offsetParent) != null){
  	    if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
      }
      return returnValue;
}

function getTopPos(inputObj)
{		
      var returnValue = inputObj.offsetTop;
      while((inputObj = inputObj.offsetParent) != null){
  	    if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
      }
      return returnValue;
}

function CrearAlerta(posx, posy, msg)
{
    var tbGlobo = document.getElementById("tbGlobo");
    tbGlobo.style.top = posy + 'px';
    tbGlobo.style.left = posx  + 'px';
    document.getElementById("tdMensaje").innerHTML = msg;
    tbGlobo.style.display = "inline";
}

function DisableErrorStyle()
{
     try{
	        if(self.errorIndex < self.errorSeq.length){
		        clsName = self.errorSeq[self.errorIndex];
				self.errorField.style.borderColor = clsName;
		        self.errorIndex++;
		        self.intervalo = setTimeout("DisableErrorStyle()",self.timeouterror);
	        }else{
		        self.errorField = null;
		        self.errorIndex = 0;
	        }
        }catch(ex){}
        return;
}

function Validar(Objeto, e, Tipo) 
{ 
    var unicode= (e.keyCode) ? e.keyCode : e.which; //e.which //charCode
    unicode = String.fromCharCode(unicode).toUpperCase().charCodeAt(0);

    var LongitudValor = Objeto.value.length + 1; 
    var SubCadena = String.fromCharCode(unicode).toUpperCase(); 
    var Cadena = ""; 
    var blnResult = true; 
    switch(Tipo) 
    {
        case 1: var cadStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ. '; break; 
        case 2: var cadStr = '0123456789'; break; 
        case 3: var cadStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; 
        case 4: var cadStr = '0123456789.'; break; 
        case 5: var cadStr = '0123456789/'; break;         
    }

    if (LongitudValor>0) 
    {
        for (i=1; i<=cadStr.length; i++) 
            if ((cadStr.substring(i,i-1)==SubCadena)) 
                Cadena=cadStr.substring(i,i-1);
       
        if ( (Tipo==1 || Tipo==3) && (unicode == 209 || unicode == 241)) Cadena = " " 

        if (Cadena.length==0) blnResult = false;
        
        if (boolMSIE) { if (Tipo==1) e.keyCode = unicode; }
         
        if (boolSafari)
        {
            if (Tipo==1 && Cadena != "")
            { 
                Objeto.value += String.fromCharCode(unicode).toUpperCase();
                blnResult = false;
            }
        }
        
        if (boolFireFox)
        {
            if (Tipo==1 && unicode!="8" && unicode!="0" && unicode!="37" && unicode!="38" && unicode!="39" && unicode!="40" && Cadena != "") 
            { 
                Objeto.value += String.fromCharCode(unicode).toUpperCase();
                blnResult = false;
            }
            
            if (unicode == "8") Objeto.value = Objeto.value.substring(0,Objeto.value.length -1);
            if (unicode == "37") Objeto.form[(getIndex(Objeto)+1) % Objeto.form.length].focus();
            if (unicode == "39") Objeto.form[(getIndex(Objeto)- 1) % Objeto.form.length].focus();
        }        
        return blnResult; 
    }
}

function BloquearObjeto(strIdBt)
{
    var objBt = document.getElementById(strIdBt);
    if(!objBt.disabled)
    {
        var newAttr = document.createAttribute("disabled");
        newAttr.nodeValue = "disabled";
        objBt.setAttributeNode(newAttr);
    }
}

function MoveToLogin() { window.top.location.href = window.top.document.getElementById("LoginPage").value; }

function DesbloquearObjeto(strId)
{
    var objBt = document.getElementById(strId);
    if(objBt.disabled)objBt.removeAttribute("disabled");
}

function CambiarPuntero(pointer, obj)
{
    if(!obj.disabled)
    {
        if (pointer) obj.style.cursor = "pointer";
        else obj.style.cursor = "default";
    }
    else obj.style.cursor = "default";
}

function Imprimir(strobject, strTitle)
{
    var obj = document.getElementById(strobject);
    
    if (obj != null)
    {
        var strHTML = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
        strHTML += "<html xmlns='http://www.w3.org/1999/xhtml'><head><title>" + strTitle + "</title>";
        strHTML += "<link href='Styles/General.css' rel='stylesheet' type='text/css' />";
        strHTML += "<link href='Styles/ReportePedidos.css' rel='stylesheet' type='text/css' /></head><body><center>";
        strHTML += obj.innerHTML + "</center></body></html>";
        
        var ventimp = window.open(' ', 'CIPRESES');
        ventimp.document.write(strHTML);
        ventimp.document.close();
        ventimp.print();
        ventimp.close();
    }
}