YAHOO.util.Dispatcher.init ({relative:true});

getUrlEncodedKey = function(key, query) {
    if (!query)
        query = window.location.search;    
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
};

setUrlEncodedKey = function(key, value, query) {
    query = query || window.location.search;
    var q = query + "&";
    var re = new RegExp(key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, key + "=" + encodeURIComponent(value) + "&");
    q = q.trimStart("&").trimEnd("&");
    return q;
};
String.prototype.trimEnd = function(c) {
    if (c)        
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}

String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};

getDynamicParamList=function(strparams,mode)
{
	var outparamlist_str = '';
	var outparamlist_obj={};
	
	
	if(typeof(mode)=='undefined')
	{
		mode='string';
	}
	
	if(strparams===null || strparams==='')
	{
		if(mode=='object')
		{
			return null;
		}
		else
			return outparamlist_str;
	}
	var inparamlist = strparams.split('&');

	for(var inparam_index=0;inparam_index<inparamlist.length;inparam_index++)
	{
		var paramdef=inparamlist[inparam_index];
		var arr=paramdef.split(':');
		//if we have alias : js : code
		var alias='';
		var elemid='';
		var code='';
		var value='';
		
		if(arr.length==3)
		{	
			alias=arr[0];
			code=arr[2];
		}
		else if(arr.length==2)
		{
			alias=arr[0];
			elemid=arr[1];
		}
		else if(arr.length==1)
		{
			elemid=arr[0];
			alias=elemid;
		}
		
		if(elemid!='')
		{
			value = encodeURIComponent(getControlValue(elemid));
		}
		else
		{			
			value=eval(code);
		}
		if(outparamlist_str.length>0)
		{
			outparamlist_str+='&';
		}
		outparamlist_str+=alias+'='+encodeURIComponent(value);
		outparamlist_obj[alias]=encodeURIComponent(value);
	}
	if(mode=='string')
	{
		return outparamlist_str;
	}
	else
	{
		return outparamlist_obj;
	}
};

// get a value from a control in the GUI
getControlValue=function(controlId) {
  var controlValue = '';
  var controlObj = document.getElementById(controlId);

  // check that the control exists in the GUI
  if (controlObj != undefined)
  {
    // action to perform depends on the type of GUI element (input, div, ...)
    switch (controlObj.tagName.toLowerCase())
    {
      case 'input':
        switch (controlObj.type.toLowerCase())
        {
          case 'checkbox':
            controlValue = (controlObj.checked ? "1" : "0");
          break;
          case 'radio':
            controlObjs = document.getElementsByName(controlObj.name);
            for(var j=0; j<controlObjs.length; j++)
            {
                if(controlObjs[j].checked == true)
                {
                    controlValue = controlObjs[j].value;
                    break;
                }
            }
          break;
          case 'text':
          default:
            controlValue = controlObj.value;
          break;
        }
      break;
      case 'div':
      case 'span':
        controlValue = controlObj.innerHTML;
      break;
      default:
        controlValue = controlObj.value;
      break;
    }
  }
  
  return controlValue;
};

// get a value from a control in the GUI
getControl=function(controlId) {
  var controlObj = document.getElementById(controlId);

  if (controlObj != undefined)
  {
    return controlObj;
  }
  else
  {
    return null;
  }
};

enableControl = function(controlId) {
  control = getControl(controlId);
  if (control)
  {
    control.disabled = false;
  }
};

disableControl = function(controlId) {
  control = getControl(controlId);
  if (control)
  {
    control.disabled = true;
  }
};

addCustomTag=function(type,id,instance)
{
	if(!window.custom_taglist)
	{
		window.custom_taglist=[];
		window.custom_tagidlist=[];
	}
	
	if(window.custom_taglist[type]==null)
	{
		window.custom_taglist[type]=[];		
	}
	window.custom_taglist[type][id]=instance;
	window.custom_tagidlist[id]=instance;
};

getCustomTag=function(id)
{
	return window.custom_tagidlist[id];
};
removeCustomTag = function(id)
{
	window.custom_tagidlist[id] = null;
}
getTypedCustomTag=function(type,id)
{
	typearr=window.custom_taglist[type];
	if(typearr!=null)
	{
		return typearr[id];
	}
	return null;
};

getTagValue=function(id)
{
	return document.getElementById(id).value;
};

getTagProperty=function(property, id)
{
    return eval("document.getElementById(id)."+property);
};

setTagProperty=function(property, id, value)
{
    document.getElementById(id)[property] = value;
};

update_sections=function(zlist, callbacks)
{		
		update_elements('section',zlist, callbacks);
};

/* Update_elements(type,zlist)
    - type : type of the elements 
    - zlist: list of elements to update
    @ summary : 
        call the update fonction for each elements of the list 'zlist' 
*/
update_elements=function(type,zlist, callbacks)
{       
        var tlist=window.custom_taglist[type];
        if (tlist)
        {
	        for(i=0;i<zlist.length;i++)
	        {
	            if(tlist[zlist[i]] != null)
	            {
	                var callback = null;
	                if (callbacks)
	                {
	                  callback = callbacks[i];
	                }
	                tlist[zlist[i]].update(callback);                                 
	            }
	        }
        }
};


create_overlay=function()
{
		ovl=document.createElement('div');
		ovl.setAttribute('id','overlay')
		ovl.className='overlay';
		document.body.appendChild(ovl);

}
destroy_overlay=function()
{
	ovl=document.getElementById('overlay');
	document.body.removeChild(ovl);
}

show_div=function(did)
{
	var div=document.getElementById(did);
	div.style.display="block";
}

hide_div=function(did)
{
	var div=document.getElementById(did);
	div.style.display="none";
}

