if (document && 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", "resize"]);
}
    
function emulateEventHandlers(eventNames)
{
    for (var i = 0; i < eventNames.length; i++)
    {
        window.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 InArray(aArray, sValue) {
    for (var i = 0; i < aArray.length; i++) {
        if (aArray[i] == sValue) {
            return true;
        }
    }
    
    return false;
}

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;
}
