
function ValidateDdl(source, arguments)
{
    arguments.IsValid=false; 

    if(arguments.Value == "")
        return;
    arguments.IsValid=true;
}

function ValidateTwoDdl(source, arguments)
{
    arguments.IsValid=false;
    var AdverDdl = $(".AdvertisementField");
    var RubricDdl = $(".RubricField");
    if(RubricDdl[0].value != "")
    {
        arguments.IsValid=true;
        return;
    }
    if(AdverDdl[0].value != "")
    {
        arguments.IsValid=true;
        return;
    }
    return;
}

function ChangeMenuColor()
{

       var div = $("#search_sub1_menu_end");
       if(div.length != 0)
       {
            div[0].style.backgroundColor = "rgb(232, 236, 247)";
       }
}

function ChangeRowColor(row)
{
    var grid = $(".searchgrid");
    
    for(var i=0;i<grid[0].rows.length;i++)
    {
        if($(grid[0].rows[i]).attr("backgroundColor") == undefined)
        {
            $(grid[0].rows[i]).attr("backgroundColor", grid[0].rows[i].style.backgroundColor);
        }
        else
        {
            grid[0].rows[i].style.backgroundColor = $(grid[0].rows[i]).attr("backgroundColor");
        }
    }
    var field = $('#ctl00_PC_AdvertisementTemplatePickCtrl1_HdnFld');
    if(field.length == 0)
    {
        field = $('#ctl00_PC_TariffPlanPickerCtrl1_HdnFld');
    }
    
    field[0].value = row.rowIndex;
    
    $(row).attr("backgroundColor", row.style.backgroundColor);
    row.style.backgroundColor = "rgb(156, 203, 219)";
}

function ChangeRowColorTariffPlan(row)
{
    var grid = $(".searchgrid");
    
    for(var i=0;i<grid[0].rows.length;i++)
    {
        if($(grid[0].rows[i]).attr("backgroundColor") == undefined)
        {
            $(grid[0].rows[i]).attr("backgroundColor", grid[0].rows[i].style.backgroundColor);
        }
        else
        {
            grid[0].rows[i].style.backgroundColor = $(grid[0].rows[i]).attr("backgroundColor");
        }
    }

    field = $('#ctl00_PC_TariffPlanPickerCtrl1_HdnFld');

    field[0].value = row.rowIndex;
    
    $(row).attr("backgroundColor", row.style.backgroundColor);
    row.style.backgroundColor = "rgb(156, 203, 219)";
}

function getRow()
{
    var grid = $("#HiddenField1");
    return $(grid[0]).attr("currentRow");
}

function ChangeColor(row)
{
       $(row).attr("backgroundColor", row.style.backgroundColor);
       
       row.style.backgroundColor = "rgb(232, 236, 247)";
       $(row).attr("color", row.style.color);
       
       row.style.color = "#000000";
       var descrip = $(row).find("#spanDescription");
       if(descrip.length != 0)
       {
            $(row).attr("colorDescription",descrip[0].style.color);
            descrip[0].style.color = "#000000";
            descrip[0].style.cursor = "hand";
       }
       
       var date = $(row).find("#spanDate");
       if(date.length != 0)
       {
            $(row).attr("colorDate",date[0].style.color);
            date[0].style.color = "#770000";
            date[0].style.cursor = "hand";
       }
       var notepadLink = $(row).find(".link_search");
       if(notepadLink.length != 0)
       {
            $(row).attr("colorNotepadLink",notepadLink[0].style.color);
            notepadLink[0].style.color = "#333333";
            notepadLink[0].style.cursor = "hand";
            notepadLink[1].style.color = "#333333";
            notepadLink[1].style.cursor = "hand";
       }
       var sitedLink = $(row).find(".link_search_site");
       if(sitedLink.length != 0)
       {
            $(row).attr("colorSiteLink",sitedLink[0].style.color);
            sitedLink[0].style.color = "#333333";
            sitedLink[0].style.cursor = "hand";
       }
}

function ReduseColor(row)
{
       row.style.backgroundColor = $(row).attr("backgroundColor");
       row.style.color = $(row).attr("color");
       
       var descrip = $(row).find("#spanDescription");
       if(descrip.length != 0)
       {
            descrip[0].style.color = $(row).attr("colorDescription");
       }
       var date = $(row).find("#spanDate");
       if(date.length != 0)
       {
            date[0].style.color = $(row).attr("colorDate");
       }
       var notepadLink = $(row).find(".link_search");
       if(notepadLink.length != 0)     
       {
            notepadLink[0].style.color = $(row).attr("colorNotepadLink");
            notepadLink[1].style.color = $(row).attr("colorNotepadLink");
       }
       var sitedLink = $(row).find(".link_search_site");
       if(sitedLink.length != 0)     
       {
            sitedLink[0].style.color = $(row).attr("colorSiteLink");
       }
}

function ChangeHiddenFieldValue(text)
{
    var field = $(".hiddenValue");
    field[0].value = text;
    $('.hiddenValue').trigger('onkeypress');
    $('.hiddenValue').trigger('onchange');
}

function GetCharCount(obj, maxLen)
{
	if (obj.value.length > maxLen) 
	{
		obj.value = obj.value.substr(0, maxLen);
		if (obj.value.length > maxLen) 
		{
			obj.value = obj.value.substr(0, maxLen - 1);
		}
	}
}

function stopPropagation()
{
	if (arguments[0])
	{
		e = arguments[0];
	}
	else
	{
		e = window.event;
	}
	
	if (!e) return;
	e.cancelBubble = true;
	
	if (e.stopPropagation) 
		e.stopPropagation()
}

function get(id)
{
	return document.getElementById(id);
}

ScrollBar = function (scroller, scroller_bar, menu)
{
	this.canDrag = false;
	this.prepared = false;

	this.shift_x;
	this.delta;

	this.scroller = scroller;
	this.scrollerBar = scroller_bar;
	this.menu = menu;

	this.scrollerStartShift;
	this.menuStartShift;
	this.menuWidth = width;

	this.scrollerTrackWidth = this.menuWidth - 33;
	this.menuTrackWidth;

	this.scrollerWidth;
	this.step;

	this.dontmove = false;

	this.a = false;

	this.prepare = function()
	{	
		if(get(this.scroller)!=null && get(this.menu)!=null)
		{
			this.scroller = get(this.scroller);
			this.scrollerBar = get(this.scroller_bar);

			this.menu = get(this.menu);
			this.scrollerStartShift = parseInt(this.scroller.style.marginLeft);
			this.menuStartShift = parseInt(this.menu.style.marginLeft);

			this.menuTrackWidth = this.menu.offsetWidth + this.menuStartShift;
			if (width == this.menuTrackWidth)
			{
				get('scroll-bar').style.display='none';
				//var w=$(this.menu).width();
				//$(this.menu).width(width-this.menuStartShift);
			}
			else 
			{
				get('scroll-bar').style.display='block';
			}
			
			this.scrollerWidth = Math.round( (this.menuWidth * this.scrollerTrackWidth) / this.menuTrackWidth );

			// 8 px - arrow width => min-width of scroller 16px
			this.scrollerWidth = (this.scrollerWidth < 16) ?  16 : this.scrollerWidth;

			// max-width of scroller - width of track
			this.scrollerWidth = (this.scrollerWidth > this.scrollerTrackWidth) ?  this.scrollerTrackWidth : this.scrollerWidth;

			// set scroller width
			this.scroller.style.width = this.scrollerWidth + "px";

			// теперь принимаем за скроллер точку (его левую границу), все расчеты будем производить относительно нее
			// set width of track of scroller and menu
			this.scrollerTrackWidth -= this.scrollerWidth;
			this.menuTrackWidth -= this.menuWidth;

			// calculate koeficient
			
			
			this.delta = this.menuTrackWidth / this.scrollerTrackWidth;

			this.prepared = true;
		}
		
		return false;
	}

	this.fixForBrowsers = function(event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		
		if(event.stopPropagation) 
			event.stopPropagation();
		else 
			event.cancelBubble = true;
		
		if(event.preventDefault) 
			event.preventDefault();
		else 
			event.returnValue = false;
	}

	this.setStep = function()
	{		
		this.step = 20;
	}

	this.setPosition = function(newPosition)
	{
		if(newPosition <= this.scrollerTrackWidth + this.scrollerStartShift && newPosition >= this.scrollerStartShift)
		{
			this.scroller.style.marginLeft = newPosition + "px";
		}
		else
		{
			if(newPosition >= this.scrollerTrackWidth + this.scrollerStartShift)
			{
				this.scroller.style.marginLeft = this.scrollerTrackWidth + this.scrollerStartShift + "px";
			}
			
			if(newPosition < this.scrollerStartShift)
			{
				this.scroller.style.marginLeft = this.scrollerStartShift + "px";
			}
		}

		var marginLeft=parseInt(this.scroller.style.marginLeft);
		
		this.menu.style.marginLeft = Math.round( (marginLeft - this.scrollerStartShift) * this.delta * (-1) ) + this.menuStartShift + "px";
		
		return false;
	}

	this.drag = function(event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		
		if (this.prepared)
		{
			this.canDrag = true;
			this.shift_x = event.clientX - parseInt(this.scroller.style.marginLeft);
			this.fixForBrowsers(event);
		}
		
		return false;
	}

	this.movescroller = function(event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		
		if (this.prepared && !this.dontmove)
		{
			this.setStep();
			var clickX = event.layerX ? event.layerX : event.offsetX;
			var currentPosition = parseInt(this.scroller.style.marginLeft);
			var i = (clickX > currentPosition) ? 1 : -1;
			var newPosition = 2*i*this.step + parseInt(this.scroller.style.marginLeft);
			this.setPosition(newPosition);
			this.fixForBrowsers(event);
		}
		else
		{
			this.dontmove = false;
		}
		return false;
	}

	this.move = function(event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		if (this.prepared && this.canDrag)
		{
			this.setPosition(event.clientX-this.shift_x);
			this.fixForBrowsers(event);
		}
		
		return false;
	}

	this.drop = function()
	{
		this.canDrag=false;
	}

	this.scrollerClickHandler = function()
	{
		this.dontmove=true;
	}

	this.handle = function(delta, event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		
		var i = (delta < 0) ? 1 : -1;
		this.setStep()
		var currentPosition = parseInt(this.scroller.style.marginLeft);
		var newPosition = i*this.step + currentPosition;
		this.setPosition(newPosition);
		this.fixForBrowsers(event);
	}

	this.cancelWheelAction = function(event)
	{
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		if (event.preventDefault)
		{
			event.preventDefault();
		}
		
		event.returnValue = false;
	}

	this.wheel = function(event)
	{
		var delta = 0;
		if (!event)
		{
			// For IE.
			event = window.event;
		}
		if (event.wheelDelta)
		{
			// IE/Opera.
			delta = event.wheelDelta/120;

			// In the Opera 9, delta value does not differs in sign from value in IE.
			if (window.opera)
			{
				delta = delta;
			}
		}
		else if (event.detail)
		{			
			//In the Mozilla, delta value differs in sign from value in IE.
			//Usualy delta is multiplied by 3
			
			delta = -event.detail/3;
		}
					
		if (delta)
		{
			this.handle(delta, event);
			this.cancelWheelAction(event);
			this.fixForBrowsers(event);
			return false;
		}
	}
}

function onMouseUpHandler(event)
{
	scrollBarObj.drop(event);
}

function onMouseMoveHandler(event)
{
	scrollBarObj.move(event);
}

// first
function onClickBarFirstHandler(event)
{
	scrollBarObj.movescroller(event);
}

function onMouseDownFirstHandler(event)
{
	scrollBarObj.drag(event);
}

function onClickFirstHandler(event)
{
	scrollBarObj.scrollerClickHandler(event);
}

function handleOnMouseWheelFirst(event)
{
	scrollBarObj.wheel(event);
}

//third
function InitTableScroller()
{
	if (get('scroll_table_container')!=null)
	{
		$('.AdsGridTable').css("marginLeft", 0);
		var browserName=navigator.appName;
		var s_top = self.pageYOffset ||
		(document.documentElement && document.documentElement.scrollTop) ||
		(document.body && document.body.scrollTop);

		var scroller = get('scroller');
		if(!scroller) return;

		if (browserName == 'Microsoft Internet Explorer')
		{
			//Если IE
			scroller.style.marginLeft = 0;
			get('scroll-bar').style.top = document.documentElement.clientHeight + s_top + 20 + 'px';
		}
		else 
		{
			scroller.style.marginLeft = 17+'px';
		}

		width = document.getElementById ('scroll_table_container').offsetWidth;
		get('scroll-bar').style.width = width+'px';
		
		if (browserName == 'Microsoft Internet Explorer')
		{
		    get('scroll-bar').style.width = (width-17)+'px';
		}

		scrollBarObj = new ScrollBar('scroller', 'scroller_bar', $(".AdsGridTable").attr("id"));
		scrollBarObj.prepare();

		document.onmousemove = onMouseMoveHandler;
		window.onmouseup = onMouseUpHandler;
		get('scroller_bar').onclick = onClickBarFirstHandler;
		scroller.onmousedown = onMouseDownFirstHandler;
		scroller.onmouseup = onMouseUpHandler;
		scroller.onclick = onClickFirstHandler;

		if (browserName != 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6.0') > -1)
		{
			//Если не IE
			if (s_top+document.documentElement.clientHeight >= (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
			{
				get('scroll-bar').style.position='absolute'; 
				get('scroll-bar').style.top = get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight+'px';
			}
			
			if (s_top+document.documentElement.clientHeight < (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
			{
				get('scroll-bar').style.position='fixed'; 
				get('scroll-bar').style.top = 'auto';
			}
			
			if (document.documentElement.clientHeight+s_top <= get('scroll_table_container').offsetTop)
			{
				get('scroll-bar').style.position='absolute'; 
				get('scroll-bar').style.top = get('scroll_table_container').offsetTop;
			}
		}
		
		onWindowScrollHandler();
	}
}

function onWindowResizeHandler()
{
    InitTableScroller();
}

function onWindowLoadHandler()
{
    InitTableScroller();
}

function onWindowScrollHandler()
{	
	if (get('scroll-bar')!=null && get('scroll_table_container')!=null)
	{
		var browserName=navigator.appName;
		var s_top = self.pageYOffset ||
		(document.documentElement && document.documentElement.scrollTop) ||
		(document.body && document.body.scrollTop);

		if (browserName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6.0') > -1)
		{
			//Если IE
			get('scroll-bar').style.top = document.documentElement.clientHeight + s_top - 20+'px';
		}
		
		if (browserName == 'Microsoft Internet Explorer')
		{
			//Если IE
			if (navigator.appVersion.indexOf('MSIE 6.0') > -1)
			{
				if (s_top+document.documentElement.clientHeight-423 >= (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
				{
					get('scroll-bar').style.position='absolute'; 
					get('scroll-bar').style.top = get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight+423+'px';
				}
				
				if (s_top+document.documentElement.clientHeight-130 < get('scroll_table_container').offsetTop)
				{
					get('scroll-bar').style.position='absolute'; 
					get('scroll-bar').style.top = get('scroll_table_container').offsetTop+130+'px';
				}
			}
			else 
			{

				if (s_top+document.documentElement.clientHeight >= (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
				{
					get('scroll-bar').style.position='absolute'; 
					get('scroll-bar').style.top = get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight+422+'px';
				}
				
				if (s_top+document.documentElement.clientHeight-160 < (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
				{
					get('scroll-bar').style.position='fixed'; 
					get('scroll-bar').style.top = 'auto';
				}
				
				if (document.documentElement.clientHeight+s_top-130 < get('scroll_table_container').offsetTop)
				{
					get('scroll-bar').style.position='absolute'; 
					get('scroll-bar').style.top = get('scroll_table_container').offsetTop+130+'px';
				}
			}
		}
		else 
		{
			if (s_top+document.documentElement.clientHeight >= (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
			{
				get('scroll-bar').style.position='absolute'; 
				get('scroll-bar').style.top = get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight+'px';
			}
			
			if (s_top+document.documentElement.clientHeight < (get('scroll_table_container').offsetTop+get('scroll_table_container').clientHeight))
			{
				get('scroll-bar').style.position='fixed'; 
				get('scroll-bar').style.top = 'auto';
			}
			
			if (document.documentElement.clientHeight+s_top-80 <= get('scroll_table_container').offsetTop)
			{
				get('scroll-bar').style.position='absolute'; 
				get('scroll-bar').style.top = get('scroll_table_container').offsetTop;
			}
		}
	}
}

window.onscroll=onWindowScrollHandler;
window.onresize=onWindowResizeHandler;
window.onload=onWindowLoadHandler;

var width;
var scrollBarObj;

