/* ================== INPUT RADIO HANDLER ========================== */
var radioHandler = {
    radioObj : null,
    init : function(id){
    	this.radioObj = $j("#"+id).getElementsByTagName('input');
    },
	get : function(id){
		$j("#"+id+" input:radio:checked").each(function(i){
			var ch = $j(this).val();
			if($j(this).checked == true){
				return ch;
			}	
		});
	},
	check : function(id,value){
		$j("#"+id+" input:radio").each(function(){
			$j(this).attr("checked","");
			if($j(this).val() == value){
				$j(this).attr("checked", "checked");
			}
		});
	},
	
	disableAll : function(id){
		this.init(id);
		for(var i = 0; i < this.radioObj.length; i++){ 
			this.radioObj[i].disabled = true;
 		}
	},
	enableAll : function(id){
		this.init(id);
		for(var i = 0; i < this.radioObj.length; i++){ 
			this.radioObj[i].disabled = false;
 		}
	}
}
/* ==== END ========= INPUT RADIO HANDLER ========================== */


/* ================== INPUT SELECT HANDLER ========================= */
var selectHandler = {
	set : function(id,value){
		//alert();
		$j("#"+id+" option").each(function(){
			if($j(this).val() == value){
				$j(this).attr('selected','selected');
			}
		});
	},
	getById : function(id){
		return $j("#"+id+" option:selected").val();
	}	
}
/* ==== END ========= INPUT SELECT HANDLER ========================= */

/* ================== INPUT CHECBOXES HANDLER ========================= */
var checkboxHandler = {
    checkboxObj : null,
    init : function(id){
    	this.checkboxObj = $j("#"+id+" :input");
    },
    checkAll : function(id){
    	this.init(id);
    	//alert(this.checkboxObj.length);
    	for(var i = 0; i < this.checkboxObj.length; i++){
    		this.checkboxObj[i].checked = true;
    	} 
    },
    uncheckAll : function(id){
    	this.init(id);
    	for(var i = 0; i < this.checkboxObj.length; i++)
    		this.checkboxObj[i].checked = false;
    },
    disableAll : function(id){
    	this.init(id);
    	for(var i = 0; i < this.checkboxObj.length; i++)
    		this.checkboxObj[i].disabled = true;
    },
    enableAll : function(id){
    	this.init(id);
    	for(var i = 0; i < this.checkboxObj.length; i++)
    		this.checkboxObj[i].disabled = false;
    },
	getSelectedString : function(id){
     	this.init(id);
     	var arr = new Array();
     	for(var i = 0; i < this.checkboxObj.length; i++){ 
			if(this.checkboxObj[i].checked == true)
				arr.push(this.checkboxObj[i].value);
 		}
 		if(arr)
 			return arr.join(',');
 		return false;
	},
	setSelectedString : function(id,str){
     	this.init(id);
     	this.uncheckAll(id);
     	//var arr = new Array();
     	var strArr = str.split(',');
     	for(var i = 0; i < this.checkboxObj.length; i++){ 
			//alert('анализ чекбокса: ' + this.checkboxObj[i].value);
			for(var j = 0; j < strArr.length; j++){
				//alert('анализ чекбокса: ' + this.checkboxObj[i].value + ' анализ строки: '+strArr[j]);
				if(this.checkboxObj[i].value == strArr[j]){ 
					//alert(this.checkboxObj[i].value +' = '+ strArr[j]);
					this.checkboxObj[i].checked = true;
				}
			}
 		}
	}
}
/* ==== END ========= INPUT CHECBOXES HANDLER ========================= */

// ======================== COOKIE handler functions ==============================
// === 1. function Get_Cookie - return Cookie by name =============================
// === 2. function Set_Cookie - set cookie ========================================

function Get_Cookie(name){ 
 	var start = document.cookie.indexOf(name+"="); 
 	var len = start+name.length+1; 
 	if ((!start) && (name != document.cookie.substring(0,name.length))) 
	return null; 
	if (start == -1) 
		return null; 
	var end = document.cookie.indexOf(";",len); 
	if (end == -1) 
		end = document.cookie.length; 
	return unescape(document.cookie.substring(len,end)); 
} 

// This function has been slightly modified
function Set_Cookie(name,value,expires,path,domain,secure) { 
	expires = expires * 60*60*24*1000;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	var cookieString = name + "=" +escape(value) + 
		( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
		( (path) ? ";path=" + path : "") + 
		( (domain) ? ";domain=" + domain : "") + 
		( (secure) ? ";secure" : ""); 
	document.cookie = cookieString; 
} 


var windowSize = {
	getWindowSize: function(doc) {
    	//alert('getWindowSize');
    	doc = doc || document;
    	var win_w, win_h;
    	if (self.innerHeight) {
    		win_w = self.innerWidth;
            win_h = self.innerHeight;
        } 
        else if (doc.documentElement && doc.documentElement.clientHeight) {
            win_w = doc.documentElement.clientWidth;
            win_h = doc.documentElement.clientHeight;
        }
        else if (doc.body) {
            win_w = doc.body.clientWidth;
            win_h = doc.body.clientHeight;
        }
       // alert('win_w = '+win_w);
        this.winWidth = win_w;
        this.winHeight = win_h;
    },
    _width: function(){
    	//alert('_width');
    	this.getWindowSize();
    	//alert('return winWidth = '+this.winWidth);
    	return this.winWidth;
    },
    _height: function(){
    	this.getWindowSize();
    	return this.winHeight;
    }
}



var JsPopupWin = {
	interval : '200',
	scrollTop: null, // scrolloing position
	width: null, //inner win width
	height: null, // inner win height
	init : function(width,height,bgColor,winColor,borderColor,alfa,inner,content,bodyWidth){
		this.active = true;
		this.height = height;
		this.width = width;
		// create structure
		include.div('popup_ext','popup_ext','');
		$j('#popup_ext').css({
			position:'absolute',
			width:'100%',
			height:'100%',
			zIndex:'10020',
			top:'0',
			left:'0',
			backgroundColor:bgColor,
			opacity:alfa
		});
		include.div('popup_inner','popup_inner','');
		$j('#popup_inner').css({
			position:'absolute',
			zIndex:'10025',
			overflow:'visible',
			backgroundColor:winColor,
			border:'1px solid '+ borderColor,
			display: 'none'
		});
		
		var cont =	
		'<div id="popup_content" class="popup_content">'+
			'<img src="/abs-lib/lib/absJsLib/images/loading.gif" border="0">'+
		'</div>';
		this.getWindowSize();
		if(bodyWidth) 
			this.winWidth = bodyWidth;
		this.innerLeft = (this.winWidth - this.width )/2;
		this.innerTop = (this.winHeight - this.height)/2;  
		//alert(this.innerTop);
		this.listener();
		$j('#popup_inner')
			.css({width: this.width + 'px', height: this.height + 'px'})
			.html(cont);
		if(inner)
			$j('#popup_inner').html( $j("#"+inner).html() );
		if(content)
			$j('#popup_inner').html(content);
			
		$j('#popup_inner').show();
		this.adListener();
		
	},
	changeHeight : function(newHeight){
		if(newHeight)
			$j('#popup_inner').css({height: newHeight + 'px'});
	},
	getScrollTop : function(documentElement){
        var t;
        if (document.documentElement && document.documentElement.scrollTop)
                t = document.documentElement.scrollTop;
        else if (document.body)
                t = document.body.scrollTop;
        return t;
 	},
 	adListener : function(){
	 	this.listener();
 	},
 	listener : function(){
 		if(this.active){
	 		var _scroll = this.getScrollTop();
	 		if(_scroll !== this.scrollTop){
	 			this.innerTop = (this.innerTop - this.scrollTop)+_scroll; 
	 			this.scrollTop = _scroll;
	 			this.popupWinPos();
	 			this.innerWinPos();
	 		}
	 		//this.adListener();
	 		this.tl = setTimeout(function(){JsPopupWin.listener()}, this.interval);
 		}
 	},
 	innerWinPos : function(){
 		//alert(this.innerTop);
 		$j('#popup_inner').css({left: this.innerLeft+'px', top: this.innerTop+'px' });
 	},
 	popupWinPos : function(){
 		$j('#popup_ext').css({ top: this.scrollTop });
 	},
 	getWindowSize: function(doc) {
        doc = doc || document;
        var win_w, win_h;
        if (self.innerHeight) {
            win_w = self.innerWidth;
            win_h = self.innerHeight;
        } else if (doc.documentElement && doc.documentElement.clientHeight) {
            win_w = doc.documentElement.clientWidth;
            win_h = doc.documentElement.clientHeight;
        } else if (doc.body) {
            win_w = doc.body.clientWidth;
            win_h = doc.body.clientHeight;
        }
        this.winWidth = win_w;
        this.winHeight = win_h;
    },
    close : function(){
 		//Effect.SlideUp('popup_inner');	
 		$j('#popup_ext').remove();
 		$j('#popup_inner').remove();
		this.active = false;
		clearTimeout(this.tl);
		this.innerTop = null;
		this.scrollTop = null;
 	}
}


var include = {
    js : function(filename){
    	var head = document.getElementsByTagName('head').item(0);
		script = document.createElement('script');
		script.src = filename;
		script.type = 'text/javascript';
		head.appendChild(script);
    },
    div : function(div_name,class_name,display){
	    var body = document.getElementsByTagName('body').item(0);
	    div = document.createElement('div');
	    body.appendChild(div);
	    div.id = div_name;
	    div.className = class_name;
	    div.style.display = display;
    },
    css : function(filename){
    	var head = document.getElementsByTagName('head').item(0);
		link = document.createElement('link');
		link.href = filename;
		link.rel = 'stylesheet';
		head.appendChild(link);
    }
}

function trim(str)
{
    if(!str || typeof str != 'string')
        return null;
    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

function alertError(er) {
	var pattern = /&/;
	var replacement = '\n';
	for(var i = 0; i < 30; i++){
	 	er = er.replace(pattern, replacement);
	}
	alert(er);
}

function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}
