﻿// File JScript

function FacilityOnReturn(e){

    var retValue = false;
    
    //var element = (e.target) ? e.target: e.srcElement;
    var element = (e == null) ? event.srcElement : ( (e.target) ? e.target : null );
    var keynum = window.event ? event.keyCode : (e.which ? e.which : null);
    
    if (keynum == null || keynum != 13 || element == null)
        return true;

    var facility = (!IsAttributeNull(element.attributes["facility"])) ? element.attributes["facility"].value : "blank";
    
    switch(facility){
        
        case "samplesearch":
            retValue = NewSampleSearch();
            break;
        
        default:
            //alert("Errore: facility sconosciuta!");
            return false;
            
    }    
    
    if (!retValue){
        if(e.preventDefault) 
            e.preventDefault()
        else 
            e.returnValue = true;
    }

}

function CartQtyInput(e){

    var retValue = false;
    
    //var element = (e.target) ? e.target: e.srcElement;
    var element = (e == null) ? event.srcElement : ( (e.target) ? e.target : null );
    var keynum = window.event ? event.keyCode : (e.which ? e.which : null);
    
    retValue = (keynum >= 48 && keynum <= 57) || (keynum == 8);
    
    if (!retValue){
        if(e.preventDefault) 
            e.preventDefault()
        else 
            e.returnValue = true;
    }else
        return true;    

}
function SelectThisInput(e, activeCssStyle, unactiveCssStyle){
    //var element = (e == null) ? event.srcElement : ( (e.target) ? e.target : e );
    var element = (e.className == null) ? this.event.srcElement : e;
    element.className = (!IsAttributeNull(element.attributes['readonly']) && element.attributes['readonly'].value == 'readonly') ? unactiveCssStyle : activeCssStyle;
}
function UnselectThisInput(e, unactiveCssStyle){
    var element = (e.className == null) ? this.event.srcElement : e;
    element.className = unactiveCssStyle;
}


function SetTextBoxStyle(){

    var elements = document.getElementsByTagName("input");
    for(var i = 0; i < elements.length; i++){
        //AddEventOnObject(elements[i], "onfocus", eventCode);
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "textbox"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'textbox' : 'textboxactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'textbox'"));
            
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'textboxactive', 'textbox');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'textbox');"));
            AddEventOnObject(elements[i], "onkeypress", FacilityOnReturn);
            
        }
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "textboxobbligatory"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'textboxobbligatory' : 'textboxobbligatoryactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'textboxobbligatory'"));
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'textboxobbligatoryactive', 'textboxobbligatory');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'textboxobbligatory');"));
            AddEventOnObject(elements[i], "onkeypress", FacilityOnReturn);
        }
    }
    
    elements = document.getElementsByTagName("textarea");
    for(var i = 0; i < elements.length; i++){
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "textarea"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'textarea' : 'textareaactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'textarea'"));
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'textboxactive', 'textbox');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'textbox');"));            
        }
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "textareaobbligatory"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'textareaobbligatory' : 'textareaobbligatoryactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'textareaobbligatory'"));
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'textboxobbligatoryactive', 'textboxobbligatory');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'textboxobbligatory');"));
            
        }
    }    
    
    elements = document.getElementsByTagName("select");
    for(var i = 0; i < elements.length; i++){
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "combobox"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'combobox' : 'comboboxactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'combobox'"));
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'comboboxactive', 'combobox');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'combobox');"));            
        }
        if (elements[i].attributes["class"] != null && elements[i].attributes["class"].value == "comboboxobbligatory"){
            //AddEventOnObject(elements[i], "onfocus", new Function("this.className = (!IsAttributeNull(this.attributes['readonly']) && this.attributes['readonly'].value == 'readonly') ? 'comboboxobbligatory' : 'comboboxobbligatoryactive';"));
            //AddEventOnObject(elements[i], "onblur", new Function("this.className = 'comboboxobbligatory'"));
            AddEventOnObject(elements[i], "onfocus", new Function("SelectThisInput(this, 'comboboxobbligatoryactive', 'comboboxobbligatory');"));
            AddEventOnObject(elements[i], "onblur", new Function("UnselectThisInput(this, 'comboboxobbligatory');"));             
        }
    }    

}

