var showAs = 0;
var PageIsValid = true;

function showToolTipText(lnk, index, add){
	var contador = -1;
	var txt = document.getElementsByTagName("INPUT");
	for(i=0; i<txt.length; i++){
		if(-1 != txt[i].id.indexOf("txtCantidad")){
			contador++;
			if(contador == index){
				if(!NumeroValido(txt[i])){
				    txt[i].value = "";
				    PageIsValid = true;
	            }else{
	                //document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].className = "";
		            //document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].innerHTML = ((add) ? "Agregar a" : "Quitar de" ) + " la cesta " + txt[i].value + " " + txt[i].attributes["unidad"].value ;
                    PageIsValid = true;
                }
                	document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].className = "";
                    //document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].innerHTML = "Escriba en la caja de texto la cantidad que desea " + ((add) ? "agregar a" : "quitar de" ) + " la cesta.";
		            document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].innerHTML = ((add) ? "Agregar a" : "Quitar de" ) + " la cesta " + txt[i].attributes["inc"].value + " " + txt[i].attributes["unidad"].value ;
		            //document.getElementById("T1TBalloon").getElementsByTagName("DIV")[0].innerHTML += "<br/>Solo n&uacute;meros en intervalos de " + txt[i].attributes["inc"].value + " " + txt[i].attributes["unidad"].value;

			}
		}
	}
    TagToTip('T1TBalloon', BALLOON, true, FADEIN, 200, FADEOUT, 200, LEFT, true, PADDING, 3, TEXTALIGN, 'left', OFFSETX, -17)
}

function changeAddMode(obj, index, overwrite){
    if(overwrite){
        obj.attributes["overwrite"].value = true;
        return;
    }else{
	    var contador = -1;
	    var txt = document.getElementsByTagName("INPUT");
	    for(i=0; i<txt.length; i++){
		    if(-1 != txt[i].id.indexOf("txtCantidad")){
			    contador++;
			    if(contador == index){
    		        txt[i].attributes["overwrite"].value = false;
                    return;	    
			    }
		    }
	    }
    }	    
}

function ValidarCantidad(source, arg){
    arg.IsValid = PageIsValid;
}

function NumeroValido(txt){
    if(""==txt.value)return false;
    //Agrega el 0 delante de la coma en caso que no se haya tecleado, esto es solo para ve se vea mejor
	txt.value = txt.value.replace(/^,/,'0,');
	//Reemplaza la coma del decimal por punto de los valores necesarios para la validacion
	//Cantidad tecleada
	var _cantidad   = txt.value.replace(/,/g,'.');
	//Cantidad minima permitida
	var _minimo     = txt.attributes["min"].value.replace(/,/g,'.');
	//Cantidad maxima permitida
	try {
	    var _maximo = txt.attributes["max"].value.replace(/,/g,'.');
	}catch(ex){
	    var _maximo = -1;
	}
	//Cantidad en que incrementa o decrementa
	var _incremento = txt.attributes["inc"].value.replace(/,/g,'.');
    //Validaciones
    if(isNaN(_cantidad))return false;
    if(Number(_cantidad)<Number(_minimo))return false;
    if(Number(_cantidad)>Number(_maximo)&&-1!=Number(_maximo))return false;
    if(0!=(Number(_cantidad)%Number(_incremento)))return false;
    return true;
}

function Compare(txt, accion){
	var td = document.getElementById("divBuscar")
	if(accion == 0 && txt.value == txt.attributes["compare"].value) {
		txt.value = "";
	}
	if(accion == 1 && txt.value == "") {
		txt.value = txt.attributes["compare"].value;
	}
	td.className = (0 == accion) ? "spinner" : "lupa"
}

function prepareDocument(){
	var lnks = document.getElementsByTagName("A");
	for(lnk=0;lnk<lnks.length;lnk++){
		if(-1 != lnks[lnk].className.indexOf("boton_")) {
			color = lnks[lnk].className.substr(lnks[lnk].className.search("_")+1);
			if(lnks[lnk].getElementsByTagName("span").length == 0) lnks[lnk].innerHTML = '<span class="' + color + '_ibg"><span class="' + color + '_dbg">' + lnks[lnk].innerHTML + '</span></span>'
		}
	}
	if("undefined" != typeof(prepareExclusiveDocument))prepareExclusiveDocument();
}

function addEvent(elemento,nomevento,funcion,captura)
{
    if (elemento.attachEvent)
    {
        elemento.attachEvent('on'+nomevento,funcion);
        return true;
    }
    else  
        if (elemento.addEventListener)
        {
          elemento.addEventListener(nomevento,funcion,captura);
          return true;
        }
    else
        return false;
}

function ReemplazaImagenProducto(){
//    //var arrImages = "mangos.jpg|papaya_temporada.jpg|pimientos.gif";
//    var index = 0
//    var tbls =document.getElementById("tdPrincipal").getElementsByTagName("TABLE");
//    var showAs = getReference("ctl00_ContentPlaceHolder1_cmbVista").selectedIndex;
//    for(tbl=1;tbl<tbls.length;tbl+=3-showAs){
//        index++;
//        //if(index >= arrImages.split("|").length)index = 0;            
//        //tbls[tbl].getElementsByTagName("TD")[0].style.backgroundImage = "url(../images/productos/" + ((0==showAs) ? "medianos" : "pequennos") + "/" + arrImages.split("|")[index]+ ")";
//        tbls[tbl].getElementsByTagName("TD")[0].style.backgroundImage = "url(" + tbls[tbl].attributes["producto"].value.replace("~","http://localhost/FG") + ")";        
//        if(1!=tbl)tbls[tbl].style.borderTop="1px dotted #CCCCCC";
//        //tbls[tbl].getElementsByTagName("TD")[0].style.backgroundImage = "url(../images/productos/medianos/" + tbls[tbl].attributes["producto"].value + ")"
//    }
}

function SetCurrentCategoryName(){
    var lnk = getReference("ctl00_ContentPlaceHolder1_rptProductos_ctl00_hypCategoria")
    if (lnk == null)
        lnk = getReference("ctl00_ContentPlaceHolder1_rptProducticos_ctl00_hypCategoria")
    if(lnk != null)
        setInnerText(getReference("ctl00_Contentplaceholder2_lblNavegacion"), lnk.innerText);   
}            
    
function showErrorMessage(title, msg, y, x){
    var msgError = getReference("msgError");
    var iWidth   = (document.layers)?window.innerWidth:document.body.clientWidth;
    var iHeight  = (document.layers)?window.innerHeight:document.body.clientHeight;
    setInnerHTML(msgError.getElementsByTagName("DIV")[0], title);
    setInnerHTML(msgError.getElementsByTagName("TABLE")[0].getElementsByTagName("TD")[3], msg);
    msgError.style.top = ((null != y) ? y : (iHeight/2 - 30)) + "px";  
    msgError.style.left = ((null != x) ? x : (iWidth/2 - 150)) + "px";  
    msgError.style.display = "block";
    getReference("modal").style.display="block";
    getReference("modal").height = document.getElementsByTagName("BODY")[0].offsetHeight;
}
    
function hideErrorMessage(){
    var msgError = getReference("msgError");
    msgError.style.display = "none";
    getReference("modal").style.display="none";
    var validatingObj;
    //if(null == validatingObj) validatingObj = getReference("ctl00_txtUserName");
    if(validatingObj != null){
    try{
        validatingObj.select();
    }
    catch(e){
        validatingObj.focus();
    }        
    }
}

function test_email(src) {
	    var emailReg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	    var regex = new RegExp(emailReg);
	    var matches = regex.exec(src);
	    return matches!=null && src==matches[0];
}  

function getReference(elem) {
	if( document.layers ) { //Netscape layers
		return document.layers[elem]; }
	if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
		return document.getElementById(elem); }
	if( document.all ) { //Proprietary DOM; IE4
		return document.all[elem]; }
	if( document[elem] ) { //Netscape alternative
		return document[elem]; }
	return false;
}

function setInnerHTML(elem, text){
    var myReference = elem
    if( myReference.adjacentHTML ) {
	    myReference.adjacentHTML (text);
      } else if( typeof( myReference.innerHTML ) != 'undefined' ) {
	    myReference.innerHTML = text;
      }
}

function setInnerText(elem, text){
    var myReference = elem
    if (myReference == null) return false;
    if( myReference.adjacentText ) {
	    myReference.adjacentText (text);
      } else if( typeof( myReference.innerText ) != 'undefined' ) {
	    myReference.innerText = text;
      }
}

String.prototype.Trim = function(){return this.replace(/^\s+|\s+$/g,"");}

String.prototype.LTrim = function(){return this.replace(/^\s+/g,"");}

String.prototype.RTrim = function(){return this.replace(/\s+$/g,"");}

