﻿/* utils.js */

/********************
String functions
********************/

///Trim
String.prototype.trim = function(){
    return this.replace(/^\s*|\s*$/g,'');
    // ^ = inizio
    // $ = fine 
    // * = 0 to infinite
    // + = 1 to infinite
    // g = ???
    
}

String.prototype.alttrim = function(){ // what do this ???
    return this.replace(/^\s+/g,'');
}



/// Alessandro Piccione  (03/2008)
/// Format (like .Net "String.format" function)
function formatString(text, args)
{
    switch(args.length)
    {
        case 3:
            text = text.replace("{2}", args[2]);
        case 2:
            text = text.replace("{1}", args[1]);
        case 1:
            text = text.replace("{0}", args[0]);
            break;
        default:
            if(args == null)
            {
                alert("formatString: no args defined");
            }
            else
            {
                alert("formatString: max number for args is 3, given: " + args.length);
            }       
            break;
    }
    
    return text;
} 

                                     


/* return a random integer from 0 to max */
function getRandomInt(max)
{
    var ranNum = Math.floor(Math.random()*max);
    return ranNum;
}

/* return a random decimal number from 0 to max */
function getRandom(max)
{
    var ranNum = Math.random()*max;
    return ranNum;
}



function getClientWidth(){
    var w;
    if(document.innerWidth){ w=document.innerWidth;
    } else if(document.documentElement.clientWidth){ w=document.documentElement.clientWidth;
    } else if(document.body){ w=document.body.clientWidth; }
    return w;
    //document.screen.availWidth;
}

function getClientHeight(){
    var h;
    if(document.innerHeight){ h=document.innerHeight;
    } else if(document.documentElement.clientHeight){ h=document.documentElement.clientHeight;
    } else if(document.body){ h=document.body.clientHeight; }
    return h;
    //document.screen.availHeight;
}


/// Textbox and TextArea key filter ///

function filterEnterKey(e)
{
    if (e.keyCode)
    {
        if (e.keyCode == 13) 
            return false;
    }
    else
    {
        if (e.which == 13)
        {
            //e = null;
            //e.preventDefault();
            //e.stopPropagation();
            Event.stop(e);
            return false;
        }
    }
 
    //   Key = String.fromCharCode(event.which); 
}

/// use it for return key pressed on keyup evnt of IE
function getValueOfKey(e)
{
    if (e.keyCode)
    {
        return String.fromCharCode(e.keyCode);
    }
}


/*******************************************************************************
* Alessandro Piccione (02/2008)
* key handler for textbox 
* handle the submit in a control and execute the click of a specified button
********************************************************************************/
function setButtonToClick(buttonId, event)
{
    if(event.keyCode == 13 )
    {
        event.returnValue = false;
        event.cancel = true;        
        document.getElementById(buttonId).click();
    }
}

function setCodeToExecute(code ,event)
{
    if(event.keyCode == 13 )
    {
        event.returnValue = false;
        event.cancel = true; 
        
        eval(code);       
    }  
}


/**********************************
        Events gesture
**********************************/

function addEvent( obj, type, fn )
{
   if (obj.addEventListener) {

      obj.addEventListener( type, fn, false );
   } else if (obj.attachEvent) {

      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
   }
}

function removeEvent( obj, type, fn )
{
   if (obj.removeEventListener) {
      obj.removeEventListener( type, fn, false );
   } else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, obj[type+fn] );
      obj[type+fn] = null;
      obj["e"+type+fn] = null;
   }
}



/********************************************/
/// *** BLINK v1.1 05/2007 © A.P. 2007 *** ///
/********************************************/

var blinkTimes = 10; // number of blink
var blinkCounter = 0;
var blinkObject;
var blinkColor_1;
var blinkColor_2;
var blinkDuration = 500;

function blink(objectId ,startColor ,endColor ,times)
{
    blinkObject = document.getElementById(objectId);
    if (blinkObject == null) return;
    blinkTimes = times;
    blinkCounter = 0;
    blinkColor_1 = startColor;
    blinkColor_2 = endColor;
    setTimeout("blink_start()", blinkDuration);
}

function blink_start()
{
    blinkObject.style.color = blinkColor_1;
    setTimeout("blink_stop()", blinkDuration);
}

function blink_stop()
{
    blinkCounter ++;
    blinkObject.style.color = blinkColor_2;
    if (blinkCounter < blinkTimes) setTimeout("blink_start()", blinkDuration);
}


/// *** BLINK *** ///


/***************************
* COOKIES         
* Alessandro Piccione   
* 18/01/2008 
* 16/12/2008 (Alessanadro) aggiunto parametro path
***************************/

function setCookie(c_name, value, expiredays, path)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
       
    if (!path)
        path = "/";

    document.cookie=c_name+ "=" +escape(value) + ( (expiredays==null) ? "" : ";expires="+exdate.toGMTString() ) + ";path=" + path ;
} 

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) 
                c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
return "";
}


