﻿// ####################################################################################
// ####################################################################################
// ###                         START CSS EDITING CODE                               ###
// ####################################################################################
// ####################################################################################


//Custom JavaScript Functions by Shawn Olson
//Copyright 2006-2008
//http://www.shawnolson.net
//If you copy any functions from this page into your scripts, you must provide credit to Shawn Olson & http://www.shawnolson.net
//*******************************************
/*
function stripCharacter(words, character) {
    //documentation for this script at http://www.shawnolson.net/a/499/
    var spaces = words.length;
    for (var x = 1; x < spaces; ++x) {
        words = words.replace(character, "");
    }
    return words;
}

function changecss(theClass, element, value) {
    //Last Updated on October 10, 1020
    //documentation for this script at
    //http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
    var cssRules;

    var added = false;
    for (var S = 0; S < document.styleSheets.length; S++) {
        if (document.styleSheets[S]['rules']) {
            cssRules = 'rules';
        } else if (document.styleSheets[S]['cssRules']) {
            cssRules = 'cssRules';
        } else {
            //no rules found... browser unknown
        }

        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
                if (document.styleSheets[S][cssRules][R].style[element]) {
                    document.styleSheets[S][cssRules][R].style[element] = value;
                    added = true;
                    break;
                }
            }
        }
        if (!added) {
            try {
                document.styleSheets[S].insertRule(theClass + ' { ' + element + ': ' + value + '; }', document.styleSheets[S][cssRules].length);
            }
            catch (err) {
                try {
                    document.styleSheets[S].addRule(theClass, element + ': ' + value + ';');
                }
                catch (err) { }

            }
        }
    }
}

function checkUncheckAll(theElement) {
    var theForm = theElement.form, z = 0;
    for (z = 0; z < theForm.length; z++) {
        if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
            theForm[z].checked = theElement.checked;
        }
    }
}

function checkUncheckSome(controller, theElements) {
    //Programmed by Shawn Olson
    //Copyright (c) 2006-2007
    //Updated on August 12, 2007
    //Permission to use this function provided that it always includes this credit text
    //  http://www.shawnolson.net
    //Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/

    //theElements is an array of objects designated as a comma separated list of their IDs
    //If an element in theElements is not a checkbox, then it is assumed
    //that the function is recursive for that object and will check/uncheck
    //all checkboxes contained in that element

    var formElements = theElements.split(',');
    var theController = document.getElementById(controller);
    for (var z = 0; z < formElements.length; z++) {
        theItem = document.getElementById(formElements[z]);
        if (theItem.type) {
            if (theItem.type == 'checkbox') {
                theItem.checked = theController.checked;
            }
        } else {
            theInputs = theItem.getElementsByTagName('input');
            for (var y = 0; y < theInputs.length; y++) {
                if (theInputs[y].type == 'checkbox' && theInputs[y].id != theController.id) {
                    theInputs[y].checked = theController.checked;
                }
            }
        }
    }
}

function changeImgSize(objectId, newWidth, newHeight) {
    imgString = 'theImg = document.getElementById("' + objectId + '")';
    eval(imgString);
    oldWidth = theImg.width;
    oldHeight = theImg.height;
    if (newWidth > 0) {
        theImg.width = newWidth;
    }
    if (newHeight > 0) {
        theImg.height = newHeight;
    }

}

function changeColor(theObj, newColor) {
    eval('var theObject = document.getElementById("' + theObj + '")');
    if (theObject.style.backgroundColor == null) { theBG = 'white'; } else { theBG = theObject.style.backgroundColor; }
    if (theObject.style.color == null) { theColor = 'black'; } else { theColor = theObject.style.color; }
    //alert(theObject.style.color+' '+theObject.style.backgroundColor);
    switch (theColor) {
        case newColor:
            switch (theBG) {
                case 'white':
                    theObject.style.color = 'black';
                    break;
                case 'black':
                    theObject.style.color = 'white';
                    break;
                default:
                    theObject.style.color = 'black';
                    break;
            }
            break;
        default:
            theObject.style.color = newColor;
            break;
    }
}

*/
// ####################################################################################
// ####################################################################################
// ###                          END CSS EDITING CODE                                ###
// ####################################################################################
// ####################################################################################








$(function () {
    //alert('window.load event fired.');
    //var iev = getIEVersion();
    if (typeof String.prototype.trim !== 'function')
    {
        String.prototype.trim = customTrim;
    }
});





function isExplorer() {
    return (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
}

function getIEVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


Array.prototype.search = function (text) {
    var pos = -1;
    if (text) {
        for (var item in this) {
            if (typeof (this[item]) != 'function') {
                pos = this[item].indexOf(text);
                if (pos >= 0) return this[item];
            }
        }
    }
    else { return null; }
}

/*
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}




Array.prototype.search = function (text) {
var pos;
for (i = 0; i < this.length; i++) {
pos = this[i].indexOf(text);
if (pos >= 0) return this[i];
}
}
*/

Array.prototype.clean = function () {
    var deleteValue = undefined;
    for (var i = 0; i < this.length; i++) {
        if (this[i] == deleteValue) {
            this.splice(i, 1);
            i--;
        }
    }
    return this;
}


// Restituisce il percorso di una pagina relativamente al percorso della pagina principale (nel caso di frames)
function getRelativeUrl(url) {
    var newurl = null;
    if (url != null) {
        var pathName = getRelativePath();
        var startpos = url.lastIndexOf(pathName) + pathName.length;
        var endpos = url.lastIndexOf("#");
        if (endpos < 0) endpos = 99999;
        newurl = url.slice(startpos, endpos);
    }
    return newurl;
}

// Restituisce il percorso corrente in formato relativo
function getRelativePath() {
    var loc = window.location;
    var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
    return pathName;
}



function checkBack() {
    var body = document.getElementById('pagecontainer');
    if (body != null) {
        //var realUrl = body.contentDocument.location.href;
        var realUrl = body.contentWindow.location.href;
        var srcUrl = body.src;
        if (realUrl != srcUrl) {
            // è stato premuto BACK!
            setMenuFromUrl(realUrl);

            // Questo metodo fino a questo punto riesce ad intercettare i tasti back e forward
            // con eccezione dell'ultimo forward. L'istruzione qui sotto risolve il problema
            // dell'ultimo forward, ma il frame si ricarica 2 volte ad ogni pressione dei suddetti 
            // tasti.
            //body.src = realUrl; 
        }
    }
}


function findElementPosition(obj) {
    var curleft = curtop = 0;
    if (obj != null) {
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
        }
    }
    var position = new Object();
    position['left'] = curleft;
    position['top'] = curtop;
    return position;
}

function ScaleFont() {

    var body = document.getElementsByTagName('body')[0];
    var dimSchermo = window.screen.width;
    var fontsize = Math.ceil(dimSchermo / 117);
    body.style.fontSize = fontsize + 'px';

}



//  Restituisce la dimensione dello schermo. Per le funzioni di scaling utilizzare questa funzione
//  rispetto a quella nativa di javascript, per poter fissare dei limiti massimi e minimi. 
function getScreenSize() {
    var size = { width:window.screen.width, height:window.screen.height };
    if (size.width > 1280) size.width = 1280;
    if (size.width < 1024) size.width = 1024;
    if (size.height > 1024) size.height = 1024;
    if (size.height < 768) size.height = 768;

    return size;
}



function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}







function customTrim() {
    
    //var str = this.replace(/^\s\s*/, ''),
    var str = this.replace(/^\s+|\s+$/g, ''),
		ws = /\s/,
		i = this.length;
    while (ws.test(this.charAt(--i)));
    return this.slice(0, i + 1);
}

function killNode(mynode) {
    while (mynode.hasChildNodes()) {
        killNode(mynode.firstChild);
    }
    mynode.parentNode.removeChild(mynode);
}
