var Utils = {
  replaceAll: function (sOld){
    var sNew = sOld;
    while (sNew.indexOf(" ")>=0){
      sNew=sNew.replace(" ","");
    }
    return sNew;
  },
  checkTags: function (oInput){
	if(oInput.value.length>0){

		var tagsSplittedCreating = oInput.value.split(" ");

		for (var i=0; i<tagsSplittedCreating.length; i++){

			if(tagsSplittedCreating[i].length > 25){

				alert("Las etiquetas no pueden exceder de 25 caracteres");

				return false;

			}

		}

	}if (oInput.value.length == 0 || oInput.value.length > 100){

		alert("El campo etiquetas debe tener entre 1 y 100 caracteres");

		return false;

	}
        return true;

  },
  paintDeletedVideo:function(){
    document.write("<img width='248' height='170' alt='eliminado' src='/img/foto_eliminada_grande.gif'/>");
  },
pictLoadListener: function(sImgId, sWidth, sHeight){
//		alert("pictLoadListener");
/*
		var sWidthAux = (sWidth == undefined ? this.PICT_MAX_WIDTH: sWidth);
		var sHeightAux = (sHeight == undefined ? this.PICT_MAX_HEIGHT: sHeight);
		this.PICT_MAX_WIDTH_M = sWidthAux;
		this.PICT_MAX_HEIGHT_M = sHeightAux;
		*/
	    var oImgElement = document.images[sImgId];
	    var oImg = new Image();
	    oImg.error = false;
	    oImg.onerror = function() {
	        oImg.error = true;
	    };
	    oImg.src = oImgElement.src;
	    var iTimerID;
	    /*
		if (oImg.complete && !oImg.error) {
	            clearInterval(iTimerID);
	            Utils.pictResizer(oImgElement, oImg.width, oImg.height);
	        } else if (oImg.error) {
	            clearInterval(iTimerID);
	        }*/	    
	    function pictResizerCaller() {
			//alert("pictResizerCaller");
	        if (oImg.complete && !oImg.error) {
	            clearInterval(iTimerID);
	            Utils.pictResizer(oImgElement, oImg.width, oImg.height, sWidth, sHeight);
	        } else if (oImg.error) {
	            clearInterval(iTimerID);
	        }
	    };
	    iTimerID = setInterval(pictResizerCaller, 300);
	    
	},
	pictResizer: function(oImgElement, iWidth, iHeight, iMaxWidth, iMaxHeight){
		//alert("pictResizer");
	    if (oImgElement!=null && iWidth > 0 && iHeight > 0 ) {
	        var iScaledWidth, iScaledHeight;
	        if (iWidth <= iMaxWidth && iHeight <= iMaxHeight) {
	            iScaledWidth = iWidth;
	            iScaledHeight = iHeight;
	        } else {
	            var dScaleByX = iMaxWidth / iWidth;
	            var iWidthByX = Math.floor(iWidth * dScaleByX); 
	            var iHeightByX = Math.floor(iHeight * dScaleByX); 
	    
	            var dScaleByY = iMaxHeight / iHeight;
	            var iWidthByY = Math.floor(iWidth * dScaleByY); 
	            var iHeightByY = Math.floor(iHeight * dScaleByY); 
	           
	            var bScaleByXValid = (iWidthByX<=iMaxWidth && iHeightByX<=iMaxHeight);
	            var bScaleByYValid = (iWidthByY<=iMaxWidth && iHeightByY<=iMaxHeight);
	           
	            if (bScaleByXValid && bScaleByYValid) {
	                if (dScaleByX > dScaleByY) {
	                    iScaledWidth = iWidthByX;
	                    iScaledHeight = iHeightByX;
	                } else {
	                    iScaledWidth = iWidthByY;
	                    iScaledHeight = iHeightByY;
	                }
	            } else if (!bScaleByXValid && bScaleByYValid) {
	                iScaledWidth = iWidthByY;
	                iScaledHeight = iHeightByY;
	            } else {
	                iScaledWidth = iWidthByX;
	                iScaledHeight = iHeightByX;
	            }
	        }
			//alert("casi al final");
	        oImgElement.style.width = iScaledWidth + "px";
	        oImgElement.style.height = iScaledHeight + "px";
	        oImgElement.style.left = Math.floor((iMaxWidth - iScaledWidth) / 2) + "px";
	        oImgElement.style.display = "inline";
	    }
	}  
};
