﻿/* JS/utils.js */

// RANDOM
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;
}

//DIMENSION
var winWidth = 1000; // sets a default width for browsers who do not understand screen.width below
var winheight = 900; // ditto for height

if (screen){ // weeds out older browsers who do not understand screen.width/screen.height
winWidth  = screen.availWidth; //1280
winHeight = screen.availHeight; //994
winWidth  = getClientWidth(); //dipende della finestra browser
winHeight = getClientHeight(); //dipende della finestra browser
winWidth = screen.width;
winHeight = screen.height;
}

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;
}
// EVENT key 
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); 
}
// return key pressed on keyup event of IE
function getValueOfKey(e)
{
    if (e.keyCode)
    {
        return String.fromCharCode(e.keyCode);
    }
}
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)
{
//alert('blink objectId: ' + objectId + ', startColor: ' + startColor + ',endColor: ' + endColor + ',timesblink: ' + 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);
}

// SCROLL
function setScroll(PanelID, HiddenFieldID1, HiddenFieldID2)
{     
 //alert('setScroll panel: '+ PanelID + ', H1: ' + HiddenFieldID1 + ', H2: ' + HiddenFieldID2)
 if (prendiElementoDaId(PanelID) != null)
 { 
     if (prendiElementoDaId(HiddenFieldID1) != null)
     {
       prendiElementoDaId(PanelID).scrollLeft = prendiElementoDaId(HiddenFieldID1).value;
       //alert('getScroll panel.scrollLeft: ' + prendiElementoDaId(PanelID).scrollLeft)
     }
     if (prendiElementoDaId(HiddenFieldID2) != null)
     {
       prendiElementoDaId(PanelID).scrollTop = prendiElementoDaId(HiddenFieldID2).value;
       //alert('getScroll panel.scrollTop: ' + prendiElementoDaId(PanelID).scrollTop)
     }
 }
}
function getScroll(PanelID, HiddenFieldID1, HiddenFieldID2)
{
 //alert('getScroll panel: '+ PanelID + ', H1: ' + HiddenFieldID1 + ', H2: ' + HiddenFieldID2)
 if (prendiElementoDaId(PanelID) != null)
 { 
     if (prendiElementoDaId(HiddenFieldID1) != null)
     {
        prendiElementoDaId(HiddenFieldID1).value = prendiElementoDaId(PanelID).scrollLeft;
        //alert('getScroll panel.scrollLeft: ' + prendiElementoDaId(HiddenFieldID1).value)
     }
     if (prendiElementoDaId(HiddenFieldID2) != null)
     {
        prendiElementoDaId(HiddenFieldID2).value = prendiElementoDaId(PanelID).scrollTop;
        //alert('getScroll panel.scrollTop: ' + prendiElementoDaId(HiddenFieldID2).value)
     }
 }
}  

// POPUP
var popup;
function ShowPop(pop, param1, param2, width, Height)
{
//try
//{   
    wleft = (screen.width - width) / 2;
    wtop = (screen.height - Height) / 2;
    if (wleft < 0) {
        width = screen.width;
        wleft = 0;}
    if (wtop < 0) {
        Height = screen.height;
        wtop = 0;}
  
	if ((param2 == undefined) || (param2 == null)){
		param2 = ''}
		
    var params = 'ptype=' + param1 + '&pExt=' + param2;
    if (window.showModalDialog) { //IE
    	//alert('Modal dailog: ' + pop + '?mod=1&' + params)
		popup =  window.showModalDialog(pop + '?mod=1&' + params, pop, 'dialogWidth:'+width+'px; dialogHeight:'+Height+'px;');
		//alert('Modal dailog return value is <'+popup+'>')    
		return popup;   
    }
    else{
    	//alert('open dailog: ' + pop + '?mod=2&' + params)
		popup = window.open(pop + '?mod=2&' + params,pop,'modal=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+Height+',left='+wleft+',top='+wtop);
        popup.resizeTo(width, Height);
        popup.moveTo(wleft, wtop);
        popup.focus();
		return '';
  	}
//}catch(Err){ShowError('Popup', Err);return false;}	
}    
var nb = 0;
function ShowPop2(pop, width, Height){
width=900;
Height=800;
l=(screen.availWidth-width)/2;
t=(screen.availHeight-Height)/2;
nb=nb+1;                                                                                                              
window.open(pop, nb,'modal=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scroll=no,scrollbars=no,resizable=yes,width='+width+',innerWidth='+width+',height='+Height+',innerHeight='+Height+',left='+l+',top='+t);
}    

function Replacer(obj) {
    var regexpStr = '';
    for ( var k in obj )
            regexpStr += (regexpStr.length ? '|' : '') + k;
    var regexpr = new RegExp(regexpStr,'ig'); // g: global, m:multi-line i: ignore case
    return function(s) {/*unescape(s);*/ return s.replace(regexpr, function(str, p1, p2, offset, s) { var a = obj[str]; return a == undefined ? str : a }) }
}
var myReplacer = Replacer( { '&#39;':'\'', '<BR>':'\n', '<br />':'\n', '<br/>':'\n', '&amp;':'&', '&lt;':'<', '&gt;':'>', '&quot;':'"' } );
//Print( myReplacer('aa<BR>a &amp;&amp;&amp;&lt;') );

