//define browser............................................

var br_ie = false;
var br_ns = false;
var br_opera = false;

function DefineBrowser()
{
    var user_agent = navigator.userAgent.toLowerCase();
    if (user_agent.indexOf("opera") != -1)
        br_opera = true;
    else
    {
       if (user_agent.indexOf("webtv") == -1)
       { 
           if (user_agent.indexOf("msie") != -1)
			   br_ie = true;
		   else
		   {
		       if (user_agent.indexOf("compatible") == -1 && user_agent.indexOf("spoofer") == -1 && user_agent.indexOf("hotjava") == -1)
                   br_ns = true; 		           
		   }	   
       }    
    } 
    
    if (!br_opera && !br_ie && !br_ns)
        alert("The browser you are using is not supported.  Some functionality will be lost.");
}

DefineBrowser();

//............................................................

/*************************************************************************************************************/
/* show page loading process*/
/* to enable on page load process indicator uncoment next row and remove in #pgloader_container (default.css) display: none;*/
var t_id// = setInterval(animate,20);
var pos=0;
var dir=2;
var len=0;

function animate()
{
	var elem = document.getElementById('pgloader_progress');
	if(elem != null) {
		if (pos==0) len += dir;
		if (len>32 || pos>79) pos += dir;
		if (pos>79) len -= dir;
		if (pos>79 && len==0) pos=0;
		elem.style.left = pos;
		elem.style.width = len;
	}
	
	var targelem = document.getElementById('pgloader_container');
	if (targelem != null)
	{
	    targelem.style.top = document.body.scrollTop + (screen.availHeight)/2;
		if (br_ie)
		{
			var targIframe = document.getElementById('pgloader_if');
			if (targIframe != null)
				targIframe.style.top = targelem.style.top;
		}
	}		
}

function remove_loading() 
{
	this.clearInterval(t_id);
	var targelem = document.getElementById('pgloader_container');
	if (targelem != null)
	{
		targelem.style.display='none';
		targelem.style.visibility='hidden';
	}
	
	if (br_ie)
	{
		var targIframe = document.getElementById('pgloader_if');
		if (targIframe != null)
		{
			targIframe.style.display='none';
			targIframe.style.top = 0;
			targIframe.style.left = 0; 
		}
	}	
}

function start_loading()
{
	var targelem = document.getElementById('pgloader_container');
	if (targelem != null)
	{
	    targelem.style.top = document.body.scrollTop + (screen.availHeight)/2;
		targelem.style.position = 'absolute';
		targelem.style.display='block';
		targelem.style.visibility='visible';
		
		if (br_ie)
		{
			var targIframe = document.getElementById('pgloader_if');
			if (targIframe != null)
			{
				targIframe.style.top = targelem.style.top;
				targIframe.style.left = document.getElementById('pgloader_main').offsetLeft; 
				targIframe.style.height = document.getElementById('pgloader_main').offsetHeight; 
				targIframe.style.display = "block";
			}
		}
		
		t_id = setInterval(animate,20);
	}	
}
/*************************************************************************************************************/

//operation with string ......................................
String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g , "" );
}
String.prototype.endsWith = function(value)
{
    if (this.length < value.length)
        return false;
    
    if (this.substr(this.length - value.length, value.length) == value)
       return true;
    
    return false;
}
//............................................................

Array.prototype.contains = function(value)
{
   for(var it = 0; it < this.length; it++)
   {
       if (this[it] == value)
         return true;
   }
   return false;
}

function getElementStyle(elem, IEStyleProp, CSSStyleProp)
{
    if (elem.currentStyle) 
        return elem.currentStyle[IEStyleProp];
    else if (window.getComputedStyle) 
    {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}
//............................................................
// when .ascx is put on page element id is something like "_ctrl0tbInput"  instead of "tbInput"
function GetElementById(value)
{
   for(var i=0; i<document.all.length; i++)
   {
        if (document.all[i].id != null &&
            document.all[i].id.length >= value.length &&
            document.all[i].id.indexOf(value) != -1)
            return document.all[i];
   }
   return null;
}
//............................................................
//  change search string
function QueryParser(value)
{
   this.ParamArray = value.split("&");
   this.ParamArray[0] = this.ParamArray[0].substr(1);
   this.Remove = function(param){
			var tempArray = new Array();
			for(var i=0; i<this.ParamArray.length; i++)
			{
			    if (this.ParamArray[i].indexOf(param) == -1)
			        tempArray.push(this.ParamArray[i]);
			}
			this.ParamArray = tempArray;
   }
   
   this.ValueOf = function(param){
            var res = null;
			for(var i=0; i<this.ParamArray.length; i++)
			{
			    if (this.ParamArray[i].indexOf(param) != -1)
			    {
			        res = this.ParamArray[i].substr(this.ParamArray[i].indexOf("="));
			    }
			}
			return res;
   }

   this.ToString = function(){
            var res = "";
			for(var i=0; i<this.ParamArray.length; i++)
				res += "&"+this.ParamArray[i];
			if (res.length > 0)
			    res = "?"+res.substr(1);
			return res;    	
   }					             
					             
}
//............................................................
// навысити функцыю на кейдаун 
function aspKeyDown(iKeyCode, sFunc, e)
{
	if (e == null)
		e = window.event;

	if (e.keyCode == iKeyCode)
	{
		eval(unescape(sFunc));
		return false;
	}
}
//open 
function PreviewHtmlInNewWindow(bodyText, params)
{
   var w = window.open('about:blank', '_blank', params, true); 
   var res, pattern;  
   pattern = /__quote__/g;
   res = bodyText.replace(pattern, "'"); 
   pattern = /__dblquote__/g;
   res = res.replace(pattern, "\""); 
   w.document.open();
   w.document.write(res);
   w.document.close();
   
   	//document.write call onbeforeunload but not reload page, 
   	//status bar have to be explicit cleared. See: showPageLoadingInStatusBar 
	if (br_ie)	
	{
		status="";	
		remove_loading();
	}	
}

function PreviewHtmlInPrintPage(url, bodyText, params)
{
   var w = window.open(url);
   var res, pattern;  
   pattern = /__quote__/g;
   res = bodyText.replace(pattern, "'"); 
   pattern = /__dblquote__/g;
   res = res.replace(pattern, "\""); 
   w.document.write(res);
   w.document.close();

   	//document.write call onbeforeunload but not reload page, 
   	//status bar have to be explicit cleared. See: showPageLoadingInStatusBar 
	if (br_ie)	
	{
		status="";	
		remove_loading();
	}
}

//для кастом валідаторів клієнтська перевірка
function CustomValidateInteger(source, arguments)
{
  if (arguments.Value == "-2147483648")
     arguments.IsValid=false;
  else
     arguments.IsValid=true;
}      

function CustomValidateString(source, arguments)
{
  if (arguments.Value == "")
     arguments.IsValid=false;
  else
     arguments.IsValid=true;
}      

function showPageLoadingInStatusBar()
{   
	if (br_ie)
	{
		document.body.onbeforeunload=function(){ 
				if (typeof(Page_ClientValidate) != 'function' || Page_IsValid) 
				{
					status='Page loading... Please Wait.';
					//start_loading();
					window.setTimeout("CheckForSlowValidation()", 500);
				}	
			}	
	}									
}


function CheckForSlowValidation()
{
	if (typeof(Page_ClientValidate) == 'function' && Page_IsValid == false)
	{
		status='';
		remove_loading();
	}	
}

function HideSelectElements(msgBottom, msgRight)
{
   if (br_ie)
   {
     var selects = document.getElementsByTagName("SELECT");
	 for(var i =0; i < selects.length; i++)
	 {
	     var sel = selects[i];   
		 var select_left = sel.offsetLeft;
		 var select_top = sel.offsetTop;
		 var offset_parent = sel.offsetParent;
		 while(offset_parent != null && select_left != null && select_top != null)
		 {
		       select_left += offset_parent.offsetLeft;
		       select_top += offset_parent.offsetTop;
		       offset_parent = offset_parent.offsetParent;
		 }      
         if (select_top < msgBottom && select_left < msgRight)
	              sel.style.visibility = "hidden";
       }
   }
}

/********************* Attractive Button Mouse Over/Out***********************************/
function attrBtnHover(sender, over, e)
{
   var cells = sender.rows[0].cells;
   if (over)
   {
      var re = /-up-/g;
      for(var i=0; i<cells.length; i++)
	 	  cells[i].style.backgroundImage = cells[i].style.backgroundImage.replace(re, '-over-');
      
      if (document.attrBtnHint == null && sender.getAttribute('hint') != null)
      {
		if (!e)
			e = window.event;
			
		if (document.attrBtnHintTimeout != null)
		   window.clearTimeout(document.attrBtnHintTimeout);
		document.attrBtnHintTimeout = window.setTimeout("attrBtnShowHint('"+sender.getAttribute('hint')+"',"+e.clientX+","+e.clientY+");", 1000);
	  }
   }
   else
   {
      var re = /-over-/g;
      for(var i=0; i<cells.length; i++)
		cells[i].style.backgroundImage = cells[i].style.backgroundImage.replace(re, '-up-');

 	 if (document.attrBtnHintTimeout != null)
	 	window.clearTimeout(document.attrBtnHintTimeout);
      
      if (document.attrBtnHint != null)
      {
        document.body.removeChild(document.attrBtnHint);
	    document.attrBtnHint = null;
      }
   }
}

function attrBtnShowHint(hint, x, y)
{
	document.attrBtnHint = document.createElement("DIV");
	document.attrBtnHint.innerHTML = "<div style='dispaly=block;background-color:#f9f7de;padding:3px 5px 3px 5px; border:solid 1px #967b68;font-size:10px;color:#967b68'>"+hint+"</div>";
	document.attrBtnHint.style.position = "absolute";
	document.attrBtnHint.style.left = x + 5 + document.body.scrollLeft;;
	document.attrBtnHint.style.top = y + 5 + document.body.scrollTop;
		
	document.body.appendChild(document.attrBtnHint);
}
/*************************************************************************************************************/

/*********************************** module roolup ***********************************************************/

var reqRollUp;	
var reqUnroll;	
function InitializeXMLHttpRequest(req)
{
    if (req != null)
       req.abort();
       
	try{ 
		req=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e){    
		try { req=new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(oc){ req=null;}
	}
	if(!req && typeof XMLHttpRequest!="undefined")
		req = new XMLHttpRequest();
	
	//XMLHttpRequest call onbeforeunload but not reload page, status bar have to be explicit cleared. See: showPageLoadingInStatusBar 
	if (br_ie)	
	{
		status="";	
		remove_loading();
	}	
		
	return req;	
}

function rollupModule(sender, contName)
{
   var rollup = (sender.src.toLowerCase().indexOf("-unroll") == -1);
   var pmid = sender.getAttribute("pmid");
   var url = document.utilityPagePath+"?app="+document.applicationId+"&pmid="+pmid+"&modrup="+rollup + "&time="+(new Date()).getTime();
   
   if (rollup)
   {
        document.getElementById(contName).innerHTML = "";
        var re = /-roll/g;
		sender.src = sender.src.replace(re, '-unroll');
		reqRollUp = InitializeXMLHttpRequest(reqRollUp);
		reqRollUp.onreadystatechange = function(){};
		reqRollUp.open('GET', url, true);
		reqRollUp.send(null);
   }		
   else
   {
        reqUnroll = InitializeXMLHttpRequest(reqUnroll);
		reqUnroll.onreadystatechange = function(){
		    if (reqUnroll.readyState == 4 && reqUnroll.status == 200)
		    {
				document.location = document.location;
			}	
		};
		reqUnroll.open('GET', url, true);
		reqUnroll.send(null);
   }     
	
   return false;
}
/*************************************************************************************************************/

function ShowProcessing()
{
	start_loading();
	window.setTimeout("CheckForSlowValidation()", 500);
}

/*ask for confirmation and disable button*/
function askConfirmationAndDisableButton(msg, btnCilentID)
{
	var btn = document.getElementById(btnCilentID);
	if (!btn.disabled && confirm(msg)) 
	{
		btn.style.visibility='hidden';
		ShowProcessing();
		return true;	
	}	
	return false;
}
/*stub for radmenu to avoid postback*/
function donotPostBack(item)
{
	return false;
}

/******************************************** Font Selector Mouse Over/Out **********************************/
function fontHover(sender, over, e)
{
   if (over)
   {
      if (!e)
		e = window.event;
      if (document.attrBtnHint == null && sender.getAttribute('hint') != null)
      {
		if (document.attrBtnHintTimeout != null)
		   window.clearTimeout(document.attrBtnHintTimeout);
		document.attrBtnHintTimeout = window.setTimeout("attrBtnShowHint('"+sender.getAttribute('hint')+"',"+e.clientX+","+e.clientY+");", 1000);
	  }
   }
   else
   {
   	  if (document.attrBtnHintTimeout != null)
 	  	 window.clearTimeout(document.attrBtnHintTimeout);
      if (document.attrBtnHint != null)
      {
        document.body.removeChild(document.attrBtnHint);
	    document.attrBtnHint = null;
      }
   }
}
/*************************************************************************************************************/


/********************************************  Intro page  ***************************************************/
function CloseIntroPageOnClick()
{
   if (br_ie)
   {
	 document.attachEvent('onclick', CloseIntroPage);
	 document.body.style.cursor = "hand";
   }	 
   if (br_ns)
   {
	 document.addEventListener('click', CloseIntroPage, false);
	 document.body.style.cursor = "pointer";
   }	 
}

function CloseIntroPage()
{
   document.location.assign(document.location.href);
}
/*************************************************************************************************************/

