if (document.all)
{
    ie  = true;
    moz = false;
    op = false;
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1)
    {
        op = true;
        ie = false;
        moz = false;
    }
}
else
{
    ie = false;
    op = false;
    moz = true;
}

if (moz)
{
    emulateEventHandlers(["click", "mousemove", "keypress", "keyup", "keydown", "mouseover", "mouseout", "mouseup", "mousedown", "focus"]);
}
    
function emulateEventHandlers(eventNames)
{
    for (var i = 0; i < eventNames.length; i++)
    {	
        document.addEventListener(eventNames[i], function (e) {window.event = e;}, true);
    }
}

function attachObjEvent(obj, sEvent, sFunction)
{
	if (moz)
	{
		obj.addEventListener(sEvent, sFunction, false);
	}
	else
	{
		obj.attachEvent("on" + sEvent, sFunction);
	}
}

function checkRegExp(str, pattern)
{
	reg = new RegExp(pattern);
	if (reg.exec(str))
	{
		ret = (RegExp.$1 == str);
	}
	else
	{
		ret = false;
	}
	return ret;
}

function replace(str, vell, nou)
{
	while (str.indexOf(vell) >= 0)
	{
		str = str.replace(vell, nou);
	}
	return str;
}

function num2str(num, length)
{
    var i;
    num = "" + num;

    if (length > 0)
    {
        for (i = num.length; i < length; i++)
        {
            num = "0" + num;
        }
    }
    else
    {
        num = "";
    }
    
    return num;
}

function emulePostBack(oId)
{
    if (typeof(__doPostBack) == 'function')
    {
        if (emuleClientValidate()) __doPostBack(oId,'');
    }
    else
    {
        alert("Error! La funcio doPostBack() de .net no es troba definida");
    }
}

function emulePostBackNoValidate(oId)
{
    __doPostBack(oId,'')
}

function MSPage_ClientValidate() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i]);
    }
    ValidatorUpdateIsValid();    
    ValidationSummaryOnSubmit();
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}

function emuleClientValidate()
{
    if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate())
    {
        return true;
    }
    else
    {
        return false;
    }
}

function onEnterPostBack(oId, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    if (keyCode == 13) { emulePostBack(oId); return false; }
}

function onEnterPostBackNoValidate(oId, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    if (keyCode == 13) { emulePostBackNoValidate(oId); return false; }
}

function CheckCapsLock(controlError, e)
{
	var myKeyCode=0;
	var myShiftKey=false;
    var ctl = document.getElementById(controlError);
    
    if (controlError && ctl != null)
    {
        if ( document.all ) {myKeyCode=e.keyCode;myShiftKey=e.shiftKey;}
        else if ( document.layers ) {myKeyCode=e.which;myShiftKey=( myKeyCode == 16 ) ? true : false;}
        else if ( document.getElementById ) {myKeyCode=e.which;myShiftKey=( myKeyCode == 16 ) ? true : false;}
    
        if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {ctl.style.display="block";}
        else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {ctl.style.display="block";}
        else {ctl.style.display="none";}
    }
}

function EstablirFocus(control)
{
    var ctl = document.getElementById(control);

    if (control && ctl != null)
    {
        ctl.focus();
        ctl.select();
    }
}

function AmagarElements(arrElements)
{
    var i, ctl;

    for (i = 0; i < arrElements.length; i++)
    {
        ctl = document.getElementById(arrElements[i]);
        if (ctl != null) ctl.style.visibility = "hidden";
    }
}

function getForm()
{
    var theform;
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
		theform = document.forms["Form"];
	else 
		theform = document.Form;

    return theform;
}

function getPosition()
{
    getForm().scroll_pos.value=document.body.scrollTop;
}

function setPosition()
{
    document.body.scrollTop=getForm().scroll_pos.value;
}

function RedimensionaIFrame(IdIFrame)
{
    //redimensiona el tamany del iframe en funció del tamany de finestra
    //document.getElementById(IdIFrame).height=document.body.offsetHeight-document.getElementById(IdIFrame).offsetTop-26;
	//obtenim l'alçada del document de dins del iframe
	//var alcada = document.getElementById(IdIFrame).contentWindow.document.body.scrollHeight; 
	//canviem l'alçada del iframe
	//document.getElementById(IdIFrame).style.height=alcada + 3500; 
				
	    browsername = navigator.appName;
        if (browsername.indexOf("Netscape")!=-1)
        {
            browsername="NS"
        }
        else if (browsername.indexOf("Microsoft")!=-1)
        {
            browsername="MSIE"
        }
        else
        {
            browsername="N/A";
        }

        var alcada = 0;
        if (browsername == "NS")
        {
            alcada = parseInt(document.getElementById(IdIFrame).contentDocument.body.offsetHeight) + 100;
            document.getElementById(IdIFrame).style.width = "100%"
            document.getElementById(IdIFrame).style.height = alcada + "px";
            document.getElementById(IdIFrame).style.scrolling = "no";
        }
        else
        {
            alcada = (document.frames[IdIFrame].document.body.scrollHeight > document.frames[IdIFrame].document.body.clientHeight)? document.frames[IdIFrame].document.body.scrollHeight : document.frames[IdIFrame].document.body.clientHeight;  
            document.getElementById(IdIFrame).style.width = "100%"
            document.getElementById(IdIFrame).style.height = alcada + "px" ;
            document.getElementById(IdIFrame).style.scrolling = "no";
        }
}

// MENÚ DE NAVEGACIÓ
var time = 3000;
var numofitems = 4;

// constructor
function menu(allitems,thisitem,startstate){ 
  callname= "nivell1_"+thisitem;
  divname="linksNivell_2_"+thisitem;  
	this.numberofmenuitems = 4;
	this.caller = document.getElementById(callname);
	this.thediv = document.getElementById(divname);
	this.thediv.style.visibility = startstate;
}
				 
// metodes
function ehandler(event,theobj){
  for (var i=1; i<= theobj.numberofmenuitems; i++){
	  var shutdiv =eval( "menuitem"+i+".thediv");
    shutdiv.style.visibility="hidden";
	}
	theobj.thediv.style.visibility="visible";
}

// tancar submenus
function closesubnav(event, force){
  if ((event.clientY <45)||(event.clientY > 90) || force){
    for (var i=1; i<= numofitems; i++){
      var shutdiv =eval('menuitem'+i+'.thediv');
			shutdiv.style.visibility='hidden';
		}  
	}
}

