// returns parent object which tagname is equal to the entered tagName value 
function GetParentByTag(oObject, tagName)
{
	while (oObject && oObject.tagName && oObject.tagName.toLowerCase() != tagName.toLowerCase()) oObject = oObject.parentNode;
	return oObject;
}

function WindowSizeX() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return  myWidth 
  
}

function WindowSizeY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return  myHeight 
  
}
/**************Writes the formatted string**********************/
function sprintf(format )
{
	var s = "";
	for( var i = 0; i < format.length; ++i)
	{
		var c = format.charAt(i);
		switch( c) {
		case '%':
			var n = format.charAt(++i);
			switch( n) {
			case '%':
				s += '%';
			break;
			default:
				n = parseInt(n,10);
				if( isNaN(n))
					s += 'ERROR';
				else
					s += arguments[n];
			}
		break;
		default:
			s += c;
		}
	}
	return s;
}

/****************************************************************************/
function ReplaceQuot(source)
{
	var replacedText = source.replace("&quot;", "\"");
	while (replacedText != replacedText.replace("&quot;", "\""))
	{
		replacedText = replacedText.replace("&quot;", "\"");
	}
	return replacedText;
}
/************Disable all dhtml elements of a container (dhtml element)******/
function DisableContainer(obj, disable)
{
	for (var i = 0; i < obj.childNodes.length; i++)
	{					
		try
		{
			obj.childNodes[i].disabled = disable;
		}
		catch(e){}				
		if (obj.childNodes.length > 0)
		{
			DisableContainer(obj.childNodes[i], disable);
		}
	}
}
////
function OpenInNewWindow(url)
{
	window.open(url,null);
}
/****************Function split string************/
function StringSplit(stringValue, charsCount, charToAdd)
{
	var stringVal  = new String(stringValue);
	var strings = stringVal.split(" ");
	var m = new Array();
	var sResultValue = "";
	
	for (var j=0; j < strings.length; j++){
		var str = strings[j];
		var sResult = "";
		if (str.length > charsCount)
		{
			for (var i = 0 ; i < str.length; i = i + charsCount)
			{
				sResult = sResult + str.substring (i, i + charsCount> str.length ? str.length : i + charsCount)+ charToAdd;
			}
		}
		else
		{
			sResult = sResult + charToAdd + str;
		}
		sResultValue = sResultValue + sResult
	}
	return sResultValue;
}

/****************Attach event function***********/
function RegisterWindowLoadEvent(fnHandler)
{
	var evt = "onload";
	var source = window;
	if (arguments.length > 1) evt = arguments[1];
	if (arguments.length > 2) source = arguments[2];
	if ( typeof window.addEventListener != "undefined" )
	{
		source.addEventListener( evt.replace("on", ""), fnHandler, false );
	}
	else 
	{
		if ( typeof source.attachEvent != "undefined" ) 
		{
			source.attachEvent( evt, fnHandler );
			//alert(evt);
		}
		else {
			if ( source.onload != null ) 
			{
				var oldOnload = source.onload
				source.onload = function ( e ) {
					oldOnload( e );
					fnHandler;
				}
			}
			else
			{ 
				source.onload = fnHandler;
			}
		}
	}
}

function RegisterWindowLoadEvent(fnHandler, isFirst)
{
	/*if ( typeof window.addEventListener != "undefined" )
	{
		window.addEventListener( "load", fnHandler, false );
	}
	else 
	{
		if ( typeof window.attachEvent != "undefined" ) 
		{
			window.attachEvent( "onload", fnHandler );
		}
		else {*/
			//alert(fnHandler.toString());
			//alert(fnHandler.toString().indexOf("ClientObject = new ClientObj"));
			
			if ( window.onload != null ) 
			{
				var oldOnload = window.onload
				if (isFirst)
				{
					window.onload = function ( e ) {
						fnHandler( e );
						oldOnload( e );
					}
				}
				else{
					window.onload = function ( e ) {				
						oldOnload( e );
						fnHandler( e );
					}
				}
			}
			else
			{ 
				window.onload = fnHandler;
			}
		//}
	//}
}
/****************Detect browser******************/
function IsBrowserNetscape()
{
	return (window.navigator.appName.toLowerCase().indexOf("netscape") > -1);
}

function IsBrowserIE()
{
	return (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1);
}
/****************Open Pop-up window******************/
function WindowOpen(url, params)
{
	var position = "top=30,left=100;";
	params = (params) ? params + "," + position : position;
	return window.open(url,'', params); 
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
    for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		{ 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0; a && i<a.length && (x=a[i])&& x.oSrc; i++) 
	x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x; 
  if(!d) d=document; 
  alert(n.indexOf("?"));
  if((p=n.indexOf("?"))>0 && parent.frames.length)
  {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
    alert(n);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); 
  return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
  {
   if ((x=document.getElementById(a[i]))!=null)
   {
		document.MM_sr[j++]=x; 
		if(!x.oSrc) x.oSrc=x.src; 
		x.src=a[i+2];
	}
}
}
/************End Menu functionality **************************************************/

/*****************************Message functionality ************************************/


			function Message()
			{
				this.time = 0;
				this.height = 40;
				this.image = document.getElementById("icon");
				this.div = document.getElementById("MessageDialog");
				this.textPlace = document.getElementById("msgText");
				this.color = "black";
				this.path = "../Resources/img/";
				this.image.src= this.path + "spacer.gif";

				this.textPlace.innerHTML = "";
				//this.div.style.backgroundColor = this.color;
				this.Show	  = ShowDialog;
				this.Close	  = CloseDialog;
				this.MoveDown = MoveDown;
				this.MoveUp	  = MoveUp;
				this.SetAnotherPath = SetAnotherPath;
			}
			function SetAnotherPath(bool)
			{
				if(bool)this.path = "Resources/img/";				
			}
			function ShowDialog(type, text)
			{
				
				switch(type)
				{
					case "Error":	//error
						this.image.src = this.path +  "exception.gif";						
						this.color = "red";
						break;
					case "Message":	//message
						this.image.src = this.path +  "ok.gif";
						this.color = "blue";
						break;
					case "Warning":	//warning
						this.image.src = this.path +  "warning.gif";
						this.color = "#cc9900";
						break;	
				}
				//this.time = window.setInterval("ExtendMessage()",15);
				try
				{
					document.getElementById("UserMessagesZone").style.position = "relative";
					this.textPlace.className = "err";
					this.textPlace.style.color = this.color;
					//this.textPlace.style.position = "relative";
					//this.textPlace.style.top = "6px";
					//this.textPlace.style.left = "10px";
					this.div.style.border = "1px solid " + this.color;
					this.textPlace.innerHTML = text;
					document.getElementById("UserMessagesZone").innerHTML = this.div.innerHTML;	
				}
				catch(Exception){}
				
			}
			function CloseDialog()
			{
				this.time = window.setInterval("CollapseMessage()",15);
			}
			function MoveDown()
			{
				if(this.div != null)
				{
					var height = parseInt(this.div.style.top.substring(0,this.div.style.top.length-2));
					height += 1;
					this.div.style.top = height;
				}
			}
			function MoveUp()
			{
				if(this.div != null)
				{
					var height = parseInt(this.div.style.top.substring(0,this.div.style.top.length-2));
					height -= 1;
					this.div.style.top = height;
				}
			}
						
			function CollapseMessage()
			{
				if((msg.height++) < 41)
					msg.MoveUp();
				else
				{
					window.clearInterval(msg.time);
					msg.time = 0;
				}
			} 
			function ExtendMessage(obj)
			{
				if((msg.height--) > 0)
					msg.MoveDown();
				else
				{
					window.clearInterval(msg.time);
					msg.time = 0;
				//	window.setTimeout("msg.time = window.setInterval('Collapse()',15)",3000);
				}
			}
/*************************End Message functionality ************************************/
