///////////////////GENERAL
//set the namespace
Type.registerNamespace("General");
//a global variabel to generate unique id.
var id=0;

//a global variable which can be changed in the html document to the path where the graphic is
var webGraphicsPath="";


/**
 * A class to store textlists value and text
 * @param {Object} text
 * @param {Object} value
 */
General.OptionClass=function(text,value)
{
this.text=text;
this.value=value;
}
General.OptionClass.registerClass('General.OptionClass', null, Sys.IDisposable);



/**
 * A class to read and write to a form
 * @param {Object} form
 */
General.FormClass=function(form)
{
	if(typeof(form)=="undefined"){form=0;}
	if(typeof(form)=="string" || typeof(form)=="number"){form=document.forms[form];}
	this.form=form;
	this.focusField=null;
	this.changeFocusField=true;
	this.DisplayDiv="DisplayDiv";
}

General.FormClass.prototype = {

	getForm: function()
	{
		return this.form;
	},

/**
 * makes the form trace wich field who has focus in the attribute focusField
 * @param {Object} parentElement
 */
	traceFocus: function(parentElement)
	{
		if(typeof(parentElement)=="undefined"){parentElement=document;}
		var	tags = new Array("TEXTAREA","INPUT");	
		for (var j = 0; j < tags.length; j++) 
		{
			var tagElements = parentElement.getElementsByTagName(tags[j]);
			for (var k = 0 ; k < tagElements.length; k++) 
			{
				if(!tagElements[k].traceFocus || parentElement!=document)
				{
					if(tagElements[k].readOnly!=true)
					{
						tagElements[k].traceFocus=true;
						Sys.UI.DomEvent.addHandler(tagElements[k],"focus",function(e){form.focusField=this;});
						Sys.UI.DomEvent.addHandler(tagElements[k],"blur",function(e){form.focusField=null;});				
					}
				}
			}
		}	

	},

	setReadOnly: function (elementId,b,className)
	{
		if(typeof(b)=="undefined"){b=true}
		if(typeof(className)=="undefined"){className="ReadOnly"}
		var e=this.getElement(elementId);

		if(b)
		{
			if(e.getAttribute("AlignRight")!=null)
		    {
		        className+="AlignRight";
		    }
			e.readOnly=true;
//			e.oldOnclick=e.onclick;		
//			e.onclick=new Function("return false;");		
//			e.oldOnfocus=e.onfocus;
//			e.onfocus=new Function("this.blur();return false;");		
			this.setProperty(e,"className",className)		
			this.setProperty(e,"tabbIndex",10000);		
		}
		else
		{
			e.readOnly=false;
			this.unSetProperty(e,"tabbIndex");		
//			this.unSetProperty(e,"onclick");		
//			this.unSetProperty(e,"onfocus");		
			this.unSetProperty(e,"className");		

		}		
	},
	setProperty: function(e,property,value)
	{
		if(eval("e.old"+property)==null || eval('typeof(e.old'+property+')')=="undefined")
		{
			eval("e.old"+property+'=e.'+property);	
		}
		eval('e.'+property+'=value');	
	},	
	unSetProperty: function(e,property)
	{
		if(eval("e.old"+property)!=null && eval('typeof(e.old'+property+')')!="undefined")
		{
			eval("e."+property+'=e.old'+property);			
			eval("e.old"+property+'=null');	
		}
	},	

	getElement: function (elementId)
	{
  		if(typeof(elementId)!="string"){return elementId;}
		var o=document.getElementById(elementId);
		if(o==null)
		{
			
//			o=Sys.Application.findComponent(elementId,null); //,this.form);
		}
		if(o==null)
		{
			throw("Cant find field:"+elementId);
		}
		return o;
	},

	getValue: function (elementId,opt)
	{
  		var v='';
		var efield;
  		if(typeof(elementId)=="string"){efield=this.getElement(elementId);}else{efield=elementId;}
  		var typ;
  		opt=typeof(opt)=="undefined"?false:opt;
  		if(typeof(efield)=="undefined"){throw "Error Field:"+elementId+", dont exist!"}
  		if(typeof(efield.type)=="undefined")
  		{
    			typ=efield[0].type;
  		}
  		else
  		{
    			typ=efield.type;
  		}
  		switch (typ)
  		{
    		case "text" :
    		case "textarea":
    		case "hidden" :
      		v=efield.value;
      	break;
	    case "select-multiple":
     		v=new Array();
     		for (var i=0; i<efield.options.length;i++)
     		{
       			if (efield.options[i].selected)
       			{
         				v[v.length]=this._getSelectValue(efield.options[i],opt)
       			}
     		}
     	break;
    		case "select-one" :
      		if(efield.selectedIndex>-1)
      		{
        			v=this._getSelectValue(efield.options[efield.selectedIndex],opt)
      		}
      		else
      		{
				v="";
			}
      		break;
    		case "radio" : 
      		for (var i=0;i<efield.length;i++)
      		{
        			if(efield[i].checked==true)
        			{
          			v= efield[i].value;
        			}
      		}
      		break;
    		case "checkbox" :
      		if(efield.length>1)
      		{
        			v=new Array();
        			for (var i=0;i<efield.length;i++)
        			{
         				if(efield[i].checked==true)
          			{
            				v[i]= efield[i].value;
          			}
          			else
          			{
             				v[i]="";
          			}
        			}
      		}
      		else
      		{
        			if(efield.checked==true)
        			{
          			v= efield.value;
        			}
      		}
      		break;
    		default :
      		v='unsupported type:'+typ;
  		}
  		return v;
	},

	//opt true= ta texten,false ta value
	_getSelectValue: function (f,opt)
	{
  		var v;
		if(opt=="both")
		{
			if(f.value=="" && f.text!="")
			{
				v=new optionClass(f.text,f.text);
			}
			else
			{
				v=new optionClass(f.text,f.value);
			}
		}
		else
		{
      		if(opt)
      		{
        			v=f.text;
        			if(v=="")
        			{
          			v=f.value;
        			}
      		}
      		else
      		{
        			v=f.value;
        			if(v=="")
        			{
          			v=f.text;
        			}
     		}
		}
		return v;
	},


	_convertToValue: function(v,opt)
	{	    
		if(v!=null && typeof(v.join)!="undefined" && typeof(v[0])=="object")
		{
			var vv=new Array();
			for (var i=0; i<v.length;i++)
			{
				vv[i]=this._getSelectValue(v[i],opt)
			}
			return vv;
		}
		else
		{
			return v;
		}
	},
	updateDisplayDivs: function()
	{
		var a=new Array("INPUT","SELECT","TEXTAREA");
		
		for(var i=0;i<a.length;i++)
		{
			var tags=this.form.getElementsByTagName(a[i]);
			for(var j=0;j<tags.length;j++)
			{
				if(tags[j].id!=null && document.getElementById(tags[j].id+this.DisplayDiv)!=null)
				{
					this.setValue(document.getElementById(tags[j].id+this.DisplayDiv),this.getValue(tags[j].id));
					
				}
			}
		}
	},	
	setValue: function(elementId,v,opt,add)
	{
		var found=false;
		var efield;
		if(typeof(elementId)=="string"){efield=this.getElement(elementId);}else{efield=elementId;}
		if(!this.changeFocusField)
		{
			if(this.focusField!=null && this.focusField.id==efield.id)
			{
				return;
			}
		}
		var typ;
		
		
		opt=typeof(opt)=='undefined'?false:opt;
		add=typeof(add)=='undefined'?false:add;
		var vv,tt,iso=false;
		if(typeof(efield.type)=='undefined' && typeof(efield[0])!="undefined")
		{
			typ=efield[0].type;
		}
		else
		{
			typ=efield.type;
		}
		if(typeof(typ)=='undefined')
		{
			typ=efield.tagName;
		}
		var div=document.getElementById(efield.id+this.DisplayDiv);
		if(div!=null)
		{
			this.setValue(div,v,opt,add);
		}
		switch (typ.toLowerCase()){
		case "span" :
		case "div" :
			v=this._convertToValue(v,false);
			if(v!=null && typeof(v.join)!="undefined")
			{
				efield.innerHTML=v.join("");			
			}
			else
			{
				efield.innerHTML=v;
			}			
			break;
		case "checkbox" :
			if(typeof(v)!="object"){va=new Array(v);}
			if(efield.length>1)
			{
				for (var i=0;i<efield.length;i++)
				{
					efield[i].checked==false;
				}
				for(var j=0;j<va.length;j++)
				{
					for (var i=0;i<efield.length;i++)
					{
						if(efield[i].value==va[j])
						{
							efield[i].checked==true;
						}
					}
				}
			}
			else
			{
				if(efield.value==v)
				{
					efield.checked==true;
        			}
				else
				{
      				efield.checked==false;
        			}
			}
			break;

		case "radio" :
    			for (var i=0;i<efield.length;i++)
			{
				if(efield[i].value==v)
				{
					efield[i].checked=true;
            		}
				else
				{
					efield[i].checked=false;
				}
			}
			break;
		
		case "text" :
		case "hidden" :
		case "textarea":
			v=this._convertToValue(v,false);
			if(v!=null && typeof(v.join)!="undefined")//if(typeof(v)=="string")
			{
				efield.value=v.join("");			
			}
			else
			{
				efield.value=v;
			}
			break;
		
		case "select-multiple":
		case "select-one" :
			//v=i_convertToValue(v,opt);
			if(typeof(v)=="string"){var vo=v;v=new Array();v[0]=vo;}
			if(typeof(v[0])=="object"){iso=true}
			for(var i=0; i<efield.options.length;i++)
			{
				efield.options[i].selected=false
			}
			for(var i=0;i<v.length;i++)
			{
				found=false;
				if(iso){vv=v[i].value;tt=v[i].text;}else{vv=v[i];tt=v[i];}
				for(var l=0;l<efield.options.length;l++)
				{
					if(iso)
					{
						if(efield.options[l].value==vv){found=true;efield.options[l].selected=true}
					}
					else
					{
						if(opt)
						{
							if(efield.options[l].text==tt){found=true;efield.options[l].selected=true}
						}
						else
						{
							if(efield.options[l].value==vv){found=true;efield.options[l].selected=true}							
						}
					}
				}
				if(!found && add && vv!="")
				{
					efield.options.add(new Option(tt,vv));
					efield.options[efield.options.length-1].selected=true;
				}
			}
			break;
		
		default :
			v='unsupported type:'+typ;
			alert(v);

  		}
		return found;
	}
}
General.FormClass.registerClass('General.FormClass', null, Sys.IDisposable);
var form=new General.FormClass();


/**
 * A string class to handel string manipulations
 */
General.SuperStringClass=function()
{
}
General.SuperStringClass.prototype = {	
	htmlEncode: function(value)
	{
		value=this.replace(value,"<","&lt;");
		value=this.replace(value,">","&gt;");

		value=this.replace(value,"&","&amp;");
		value=this.replace(value,'"',"&quot;");
		value=this.replace(value,"'","&#39;");		
		return value;
	},
	
	htmlDecode: function(value)
	{
		value=this.replace(value,"&lt;","<");
		value=this.replace(value,"&gt;",">");

		value=this.replace(value,"&amp;","&");
		value=this.replace(value,"&quot;",'"');
		value=this.replace(value,"&#39;","'");		
		return value;
	},
	truncate: function(s,n)
	{
		var ss=this.left(s,n);
		if(ss.length<s.length)
		{
			ss=this.left(s,n-3)+"...";
		}
		return ss;
	},	
	left: function(s,n)
	{
        if(n<1)
        {
            return "";
        }
        else
        {
		    return s.substring(0,n);
		}
	},
	right: function(s,n)
	{
        if(n<1)
        {
            return "";
        }
        else
        {
		    return s.substring(s.length-n,s.length);
		}
	},
	divideBack: function(s,sep)
	{
		s=""+s;
		var i=s.lastIndexOf(sep);
		var a=new Array();
		if(i==-1)
		{
			a[0]=s
		}
		else
		{
			a[0]=this.left(s,i);
			a[1]=this.right(s,s.length-i-1);		
		}
		return a;
	},
	divide: function(s,sep)
	{
		var i=s.indexOf(sep);
		var a=new Array();
		if(i==-1)
		{
			a[0]=s;
		}
		else
		{
			a[0]=this.left(s,i);
			a[1]=this.right(s,s.length-i-sep.length);		
		}
		return a;
	},
	replace: function(s,f,t)
	{
		var r=new RegExp(f,"gi");
		return  s.replace(r,t);
	},
	trim: function(s)
	{
		var t=new String(s);	
		return t.replace(/^\s+|\s+$/g, '') ;
	}
}
General.SuperStringClass.registerClass('General.SuperStringClass', null, Sys.IDisposable);
var superString=new General.SuperStringClass();

/**
 * A class to handle common ui operations crossbrowser compatibel
 */
General.UIClass=function()
{
	this.oldWindowSTatus="";
	this.attachClientClickToGridArray=new Array();
	this.getClientIdList=null;
}
General.UIClass.prototype = {
	clickElement: function(element)
	{
		if(typeof(jQuery)!="undefined")
		{
			if(jQuery.browser["msie"])
			{
				$(element).click();				
			}
			else
			{
				this._clickElement(element);				
			}

		}
		else
		{
			this._clickElement(element);
		}
	},
	isJQueryObject: function(element)
	{
	    return (typeof(element.attr)!="undefined");
	},
	getDomElementFromJQuery: function(element)
	{
	    if(this.isJQueryObject(element))
	    {
	        return document.getElementById(element.attr("ID"));
	    }
	    else
	    {
	        return element;
	    }
	    
	},
	_clickElement: function(element)
	{
	    element=this.getDomElementFromJQuery(element);
		if(document.dispatchEvent) { // W3C
    		var oEvent = document.createEvent( "MouseEvents" );
    		oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, element);
    		element.dispatchEvent( oEvent );
    	}
		else if(document.fireEvent) { // IE
    		element.fireEvent("onclick");
	    }    
		else 
		{
			element.click();
			return false;
		}	
	}
,
  childNodes: function(o)
  {
  	var a=new Array();
	for(var i=0;i<o.childNodes.length;i++)
	{
		if(o.childNodes[i].nodeName!="#text")
		{
			a.push(o.childNodes[i]);
		}	
	}
	return a;
  },
  cancelBubble: function(e)
  {
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; 
  },
  innerText: function(o)
  {
  	return typeof(o.innerText)!="undefined"?o.innerText:o.textContent;
  },
  resolveUrl: function(url)
  {
    if(typeof(sitePath)=="undefined")throw("must delcare sitePath!")
    return superString.replace(url,"~",sitePath);
  },
  attachClientClickToGrid: function(tableId,col,eventname)
  {
    if(typeof(col)=="undefined") col=0;
    if(typeof(eventname)=="undefined") eventname="click";
    var oTable=document.getElementById(tableId);
    oTable=oTable.getElementsByTagName("TABLE")[0];
    if(oTable==null)throw("cant find table:"+tableId);
    for(var i=0;i<oTable.rows.length;i++)
    {

        var script=null;
		if(ui.childNodes(oTable.rows[i].cells[col])[0])
		{
			script=ui.innerText(ui.childNodes(oTable.rows[i].cells[col])[0]);
		}
/*        if(typeof(script)=="undefined" && oTable.rows[i].cells[col].childNodes.length>1)
        {   
            script=ui.innerText(oTable.rows[i].cells[col].childNodes[1]);
        }*/
        if(typeof(script)!="undefined" && script!=null)
        {
            if(eventname=="click")
            {
                try
                {
                    oTable.rows[i].onclick=Function("try{"+script+"}catch(e){}");
                }
                catch(e)
                {}
            }
            else
            {
                Sys.UI.DomEvent.addHandler(oTable.rows[i],eventname,Function(script));
            }
        }
    }
    var func='ui.attachClientClickToGrid("'+tableId+'",'+col+',"'+eventname+'");'
    if(this.attachClientClickToGridArray[func]==null)       
    {
        this.attachClientClickToGridArray[func]=true;
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(Function(func));
    }

  },

	setOnload: function(func)
	{
		if (document.addEventListener)
		{
			document.addEventListener("DOMContentLoaded", func, false);
			return;
		}
		else
		{
			if(window.attachEvent)
			{
				window.attachEvent("onload",func);
				return;
			}
		}
		// for other browsers
		window.onload = func;		
	},
	setWindowStatus: function(status)
	{
		this.oldWindowSTatus=window.status;		
		window.status=status;
	},
	resetWindowStatus: function()
	{
		window.status=this.oldWindowSTatus;		
	},
	getClientId: function(id,parentElement,firstElement)
	{
		if(typeof(parentElement)=="undefined" || parentElement==null){parentElement=document.body;}
		if(typeof(firstElement)=="undefined"){firstElement=true;}else{	this.getClientIdList=new Array();}
		return this._getClientId(id,parentElement,firstElement);
	},
	_getClientId: function(id,parentElement,firstElement)
	{
        var aNodeList = parentElement.childNodes;

        for(var i=0;i<aNodeList.length;i++)
        {
            var sid=""+aNodeList[i].id;
            if(sid.indexOf(id)>-1)
            {
                if(superString.right(sid,(new String(id)).length)==id)    //check so the id  ends with it
                {                    
                    if(firstElement){return sid;}else{this.getClientIdList.push(sid);}	
                }
                else
                {
                    var sid=this._getClientId(id,aNodeList[i],firstElement);
                    if(sid!="") if(firstElement){return sid;}//else{this.getClientIdList.push(sid);}	
                }
            }
            else
            {
                var sid=this._getClientId(id,aNodeList[i],firstElement);
                if(sid!="") if(firstElement){return sid;}//else{this.getClientIdList.push(sid);}	
            }            
        }
		if(firstElement){return "";}else{return this.getClientIdList;}	
	},
	hideShow: function(o,h)
	{
		if(typeof(o)=="string") o=document.getElementById(o);
   		if(h)
   		{
     		o.style.display=''; //inline
   		}
   		else
   		{
     	    o.style.display='none';
   		}  		
	},
	_fixBrokenImagesNow: function()
	{
		for(var i=0;i<document.images.length;i++)
		{
			if(document.images[i].readyState!="complete")
			{		
				document.images[i].src=document.images[i].src;
			}
		}
	},
	fixBrokenImages: function()
	{
		window.setTimeout('ui._fixBrokenImagesNow()', 0);		
	},
	swapImage: function(id,img)
	{
		document.images[id].src=webGraphicsPath+img;
	}	
}
General.UIClass.registerClass('General.UIClass', null, Sys.IDisposable);
var ui=new General.UIClass();	

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
