/*-----------------------------------*/
/* Requires: Prototype 1.5 or higher */
/*-----------------------------------*/

//////////////////////////////////////////////////
////////// SUBMIT FORM ON 'ENTER PRESS' //////////
//////////////////////////////////////////////////	
/*
 * Used when the buttons are just images, we trigger the
 * submission through javascript
 */
function onEnterSubmit(e, elm){
    // Trigger with 'Enter' or Spacebar (it seems not to work everytime)
    if(e.keyCode == 13 || e.keyCode == 32){
        $(elm).submit();
    }
}

//////////////////////////////////////////////////
/////////////////// RESIZE BODY //////////////////
//////////////////////////////////////////////////	
/*
 * When the body is an iframe sometimes we need to 
 * enlarge that iframe to the same height of its content.
 */
function resizeBody(){
    var myHeight = 0;
    var constCorrection = 165;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;

    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;

    }
	
    $("content").style.height = (myHeight - constCorrection)+'px';
    $("menuLeft").style.height = (myHeight - constCorrection)+'px';

}

//////////////////////////////////////////////////
////////// DISABLE CHAR (WHILE TIPYNG) ///////////
//////////////////////////////////////////////////	

function disableChr(chrCode, evt){
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if(chrCode=='all'){
        return false;
    }
    if(charCode==chrCode){
        return false;
    }
}

//////////////////////////////////////////////////
////////// ONLY NUMBRS (WHILE TIPYNG) ////////////
//////////////////////////////////////////////////	

function onlyNumbers(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if(isNaN(charCode)){
        return false;
    }
}

//////////////////////////////////////////////////
///////////////// TEXT COUNTER ///////////////////
//////////////////////////////////////////////////

function textCounter(field, maxlimit, msg, debug) {
    if(debug!=='undefined'){
        if(field === 'undefined'){
            alert('** Hey Developer: No field reference passed!! ** \n Function: `textCounter`');
        } /* DEBUG */
        if(maxlimit === 'undefined'){
            alert('** Hey Developer: No `maxlimit` passed!! ** \n Function: `textCounter`');
        }	 /* DEBUG */
    }
    if(msg==='undefined'){
        msg='Maximum characters limit reached.';
    }

    if (field.value.length > maxlimit){
        alert(msg);
        field.value = field.value.substring(0, maxlimit);
        field.focus();
    }
}

//////////////////////////////////////////////////
/////////////////// TRIM /////////////////////////
//////////////////////////////////////////////////	
	
function trim(field){
    var strAux1 = '';
    var strAux2 = field.value;
    var space = 0;
    
    if (strAux2.length > 0){
    
        for(i=0;i<strAux2.length;i++){
            if(strAux2.substring(i,i+1) == ' '){
                strAux1 = strAux2.substring(i+1,strAux2.length);
                space = 1;
            }
            else
                break;
        }//end for
        if(strAux1.length>0){
            strAux2 = strAux1;
        }
        
        for(i=strAux2.length-1;i>=0;i--){
            if(strAux2.substr(i,1) == ' '){
                strAux1 = strAux2.substring(0,i);
                space = 1;
            }
            else
                break;
        }//end for
        if(strAux1.length>0 || space == 1){
            strAux2 = strAux1;
        }
    
    } //if (strAux2.length > 0){
    //obj.focus();
    return strAux2;
}

//////////////////////////////////////////////////
//////////// COMBO: SELECTED ITEM ////////////////
//////////////////////////////////////////////////

function getComboSelectedItem(comboID){
    var label;
    label = $(comboID).options[$(comboID).selectedIndex].text;
    return label;
}

//////////////////////////////////////////////////
////////////// HTML BUTTON ROLLOVER //////////////
//////////////////////////////////////////////////

function HTMLbutton_rollover(elementID, state){
    if(state=='over'){
        $(elementID).addClassName('HTMLButton_over');
    }else if(state=='out'){
        $(elementID).removeClassName('HTMLButton_over');
    }
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

function popUp(direccion, anchoVar, altoVar){
	pantallacompleta = 0;
	herramientas= 1;
	direcciones= direccion;
	estado= 1;
	barramenu= 0;
	barrascroll= 0;
	cambiatamano= 0;
	ancho = anchoVar;
	alto= altoVar;
	izquierda= 400;
	arriba= 200;
	sustituir= 1;
	var opciones = "fullscreen=" + pantallacompleta +
				 ",toolbar=" + herramientas +
				 ",location=" + direcciones +
				 ",status=" + estado +
				 ",menubar=" + barramenu +
				 ",scrollbars=" + barrascroll +
				 ",resizable=" + cambiatamano +
				 ",width=" + ancho +
				 ",height=" + alto +
				 ",left=" + izquierda +
				 ",top=" + arriba;
	 var ventana = window.open(direccion,"Beneficio",opciones,sustituir);
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

function getDataServer(url, vars){
    var xml = null;
    try{
        xml = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        xml = new XMLHttpRequest();
    }
    xml.open("GET",url + vars, false);
    xml.send(null);
    //if(xml.status == 404) alert("Url no valida");
    return xml.responseText;
}

//////////////////////////////////////////////////
///////// CSS STYLED FILE INPUT ELEMENTS /////////
//////////////////////////////////////////////////
/*
 * Be aware that requires to care about some own CSS classes:
 *
 * div.fileinputs {position: relative;}
 * div.fakefile {position: absolute; top: 0px; left: 0px; z-index: 1;}
 * input.file {position: relative; text-align: right; -moz-opacity:0 ; filter:alpha(opacity: 0); opacity: 0; z-index: 2;}
 *
 * This is its implementation:
 *
 * <div class="fileinputs">
 *	<input type="file" class="file">
 * </div>
 * 
 **/
var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads(imgsrc) {
    if (!W3CDOM) return;
    var fakeFileUpload = document.createElement('div');
    //fakeFileUpload.id = 'fakefilediv_';
    fakeFileUpload.className = 'fakefile';
    fakeFileUpload.appendChild(document.createElement('input'));
    var image = document.createElement('img');
    image.src= imgsrc;
    fakeFileUpload.appendChild(image);
    var x = document.getElementsByTagName('input');
    for (var i=0;i<x.length;i++) {
        if (x[i].type != 'file') continue;
        if (x[i].parentNode.className != 'fileinputs') continue;
        realField = x[i];
        realFieldID = realField.id;
        alert(realFieldID);
        //alert(fakeFileUpload.id);
        //fakeFileUpload.onclick = function () { alert("CLICK!"); realField.click();}
        fakeFileUpload.setAttribute("onclick", "$('"+realFieldID+"').click();")
        x[i].className = 'file hidden';
        var clone = fakeFileUpload.cloneNode(true);
        x[i].parentNode.appendChild(clone);
        x[i].relatedElement = clone.getElementsByTagName('input')[0];
        x[i].onchange = x[i].onmouseout = function () {
            this.relatedElement.value = this.value;
        }
    }
}
