var main_div 		= null;
var aDimensions 	= new Array();
var bGobalDebugMode = 1;
var oForm			= null;

function ShadowCLS(){
	this.fOpacity 			= 0.4;
	this.fCurrentOpacity 	= 0;
	this.Style 				= null;
	this.bProcessMode		= 0;
	this.iClickCounter		= 0;
	this.move_shadow = function(){
		if(document.getElementById("shadow_div") && document.getElementById("shadow_div").style.display == "block"){
			var oShadowStyle = document.getElementById("shadow_div").style;
			if(window.pageXOffset){
				var iScrollLeft = window.pageXOffset;
			}else{
				var iScrollLeft = document.documentElement.scrollLeft;
			}
			oShadowStyle.left 	= iScrollLeft + 'px';
		}
		if(window.pageYOffset){
			var iScrollTop = window.pageYOffset;
		}else{
			var iScrollTop = document.documentElement.scrollTop;
		}

	}
	this.getPageSize = function(){
	    var xScroll, yScroll;
	    if (window.innerHeight && window.scrollMaxY) {
	        xScroll = window.innerWidth + window.scrollMaxX;
	        yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	        xScroll = document.body.scrollWidth;
	        yScroll = document.body.scrollHeight;
	    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	        xScroll = document.body.offsetWidth;
	        yScroll = document.body.offsetHeight;
	    }
	    var windowWidth, windowHeight;
	    if (self.innerHeight) { // all except Explorer
	        if(document.documentElement.clientWidth){
	            windowWidth = document.documentElement.clientWidth;
	        } else {
	            windowWidth = self.innerWidth;
	        }
	        windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	        windowWidth = document.documentElement.clientWidth;
	        windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) { // other Explorers
	        windowWidth = document.body.clientWidth;
	        windowHeight = document.body.clientHeight;
	    }
	    // for small pages with total height less then height of the viewport
	    if(yScroll < windowHeight){
	        pageHeight = windowHeight;
	    } else {
	        pageHeight = yScroll;
	    }
	    // for small pages with total width less then width of the viewport
	    if(xScroll < windowWidth){
	        pageWidth = xScroll;
	    } else {
	        pageWidth = windowWidth;
	    }
	    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	    return arrayPageSize;
	}
	this.openShadowLayer = function (){
        if(this.Style == null){
        	if(document.getElementById("shadow_div")){
				this.Style 	= document.getElementById("shadow_div").style;
				if(document.getElementById("shadow_div").filters){
					this.fOpDisplayStap = this.fOpacity;
					this.fOpHideStap	= this.fOpacity;
				}else{
					this.fOpDisplayStap = this.fOpacity/20;
					this.fOpHideStap	= this.fOpacity/20;
				}
			}
		}
		if(this.Style){
			this.bProcessMode 		= 1;
			var aScreenDimensions 	= this.getPageSize();
			if(window.pageXOffset){
				var iScrollLeft = window.pageXOffset;
			}else{
				var iScrollLeft = document.documentElement.scrollLeft;
			}
			this.Style.left 	= iScrollLeft + 'px';
			this.Style.backgroundColor 	= "#000000";
		 	if(document.getElementById("shadow_div").filters){
		 		document.getElementById("shadow_div").filters.alpha.opacity = 0;
		 	}else{
		 		this.Style.opacity		= 0;
			}
			this.Style.display = "block";
	        if(aScreenDimensions[1] > 1000){
	        	this.Style.height 	= aScreenDimensions[1] + 'px';
	        }else{
	        	this.Style.height 	= '1000px';
	        }
	        changeShadowOpacity();
		}
	}
	this.closeShadowLayer = function(){
		if(this.Style){
			this.bProcessMode = 0;
			changeShadowOpacity();
		}
	}
	this.click = function(){
		if(this.iClickCounter >= 5 || main_div == null){
			this.iClickCounter = 0;
			this.closeShadowLayer();
		}else{
			this.iClickCounter++;
		}
	}
}

function changeShadowOpacity(){
	if(oShadow.bProcessMode){
		if(oShadow.fOpacity > oShadow.fCurrentOpacity){
			oShadow.fCurrentOpacity 	+= oShadow.fOpDisplayStap;
			setTimeout("changeShadowOpacity();",10);
		}else{
			oShadow.fCurrentOpacity		= oShadow.fOpacity;
		}
	}else{
		if(oShadow.fCurrentOpacity > 0){
			oShadow.fCurrentOpacity 	-= oShadow.fOpHideStap;
			setTimeout("changeShadowOpacity();",10);
		}else{
			oShadow.fCurrentOpacity	= 0;
			oShadow.Style.display 	= 'none';
		}
	}
 	/*if(document.getElementById("shadow_div").filters){
 		document.getElementById("shadow_div").filters.alpha.opacity = oShadow.fCurrentOpacity*100;
 	}else{
		oShadow.Style.opacity 		= oShadow.fCurrentOpacity;
	}*/
	$('#shadow_div').css('opacity',oShadow.fCurrentOpacity)
	//oShadowStyle.MozOpacity 		= fCurrentOpacity;
	//oShadowStyle.KhtmlOpacity 	= fCurrentOpacity;
}

function DataTransferCLS(){
    this.names 		= new Array();
    this.values 	= new Array();
    this.cnt 		= 0;
    this.inputs 	= new Array();
    this.iCnt 		= 0;
	 
	this.AddData 	= function(sName,sValue){
        this.names[this.cnt]  = sName;
        this.values[this.cnt] = sValue;
        this.cnt++;
    };
	this.AddInput 	= function(oInput){
        this.inputs[this.iCnt++] = oInput;
    };
	this.SendData 	= function(sPath){
        var oTrasferForm = document.createElement('FORM');
		
        oTrasferForm.target   = this.targetFrm||'service_frm';  
        oTrasferForm.method   = 'post';
        oTrasferForm.enctype  = "multipart/form-data";
        oTrasferForm.encoding = "multipart/form-data";
        oTrasferForm.action   = sPath;
		for(var item = 0; item < this.cnt;item++){
            var oInput   = document.createElement('INPUT');
            oInput.type  = 'hidden';
            oInput.name  = this.names[item]; 
            oInput.value = this.values[item];
            oTrasferForm.appendChild(oInput);
        }
 
        for(var i = 0; i < this.iCnt;i++){
            oTrasferForm.appendChild(this.inputs[i]);
        }
        document.getElementById('output_tranfer_div').appendChild(oTrasferForm); 
        oTrasferForm.submit();//return;
        document.getElementById('output_tranfer_div').removeChild(oTrasferForm);
        document.getElementById('output_tranfer_div').innerHTML = '';
    }
}

function redirect(sURL){
	var oForm = document.createElement('FORM');
   	oForm.method = 'POST';
	oForm.action = sURL;
    document.body.appendChild(oForm);
	oForm.submit();
}

function get_html_translation_table (table, quote_style) {

    var entities = {}, hash_map = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};

    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';

    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';

    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }

    entities['38'] = '&amp;';
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
    }

    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';


    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        hash_map[symbol] = entities[decimal];
    }

    return hash_map;
}

function htmlspecialchars_decode (string, quote_style) {
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    if (false === (hash_map = get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {
        return false;
    }
    for (symbol in hash_map) {
        entity  = hash_map[symbol];
        tmp_str = tmp_str.split(entity).join(symbol);
    }
    tmp_str = tmp_str.split('&#039;').join("'");
    return tmp_str;
}

function closeStandardForm(){
	closeLayers('main_div','content_div');
	if(oForm){
		oForm = null;
	}
}

//Js debug tools
/*
debug_var(sel.duplicate(),2);
*/
function debug_var(mVar,iDebugLevel){
	oShowArea     			= document.createElement('DIV');
	document.body.appendChild(oShowArea);
	oShowArea.align					= "left";
	oShowArea.style.width 			= '640px';
	oShowArea.style.height 			= '480px';
	oShowArea.style.position 		= 'absolute';
	oShowArea.style.overflow 		= 'auto';
    oShowArea.style.left 			= '100px';
    oShowArea.style.top 			= '100px';
	oShowArea.style.backgroundColor = '#EEEEEE';
    oShowArea.style.fontSize		= '16px';
	oShowArea.innerHTML = '<div align="center" onclick="document.body.removeChild(this.parentNode);" style="font-size:16px;background-color:#FFFF55;cursor:pointer">Close</div>';
	oShowArea.innerHTML += print_r(mVar,iDebugLevel);
}

function print_r(mVar,iDebugLevel){
	var sOut = '';
	if(iDebugLevel){
		iDebugLevel--;
		switch(typeof(mVar)){
			case 'object':
			case 'array':
				sOut += typeof(mVar) + "{<BLOCKQUOTE>";
				for(key in mVar){
					try {
						sOut += "<br>" + "[" + key + "] = " + print_r(mVar[key],iDebugLevel);
					}
					catch (e) {
						sOut += "<br>" + "[" + key + "] = " + e;
					}
				}
				sOut += "</BLOCKQUOTE>}";
			break;
			case 'string':
				sOut += mVar;
			break;
			case 'number':
				sOut += mVar;
			break;
			case 'boolean':
				sOut += "boolean (" + mVar + ")";
			break;
			case 'function':
				if(0){
					sOut += mVar;
				}else{
					sOut += '...handler source code...';
				}
			break;
			default:
				sOut += typeof(mVar);
		}
	}else{
		sOut += "...";
	}
	return sOut;
}

function detectCode(event,form_id){
	var e = (window.event) ? window.event : event;
	if (e.keyCode == 13 && document.getElementById('email').value != "" && document.getElementById('pass').value != ""){
		document.getElementById(form_id).submit();
	}
}

function moveDiv2center(oDiv){
	oShadow.openShadowLayer();
	var aScreenDimensions = oShadow.getPageSize();
	if(window.pageYOffset){
		var iScrollTop = window.pageYOffset;
	}else{
		var iScrollTop = document.documentElement.scrollTop;
	}
	if(window.pageXOffset){
		var iScrollLeft = window.pageXOffset;
	}else{
		var iScrollLeft = document.documentElement.scrollLeft;
	}
	oDiv.style.top 	= Math.round(iScrollTop + aScreenDimensions[3]/2 - (oDiv.style.height.replace('px','')/2)) + "px";
	oDiv.style.left = Math.round(iScrollLeft + aScreenDimensions[2]/2 - (oDiv.style.width.replace('px','')/2)) + "px";
}

function moveDiv2centerNew(oDiv){
	oShadow.openShadowLayer();
	var oContentDiv = document.getElementById('content_div');
	var aScreenDimensions = oShadow.getPageSize();
	if(window.pageYOffset){
		var iScrollTop = window.pageYOffset;
	}else{
		var iScrollTop = document.documentElement.scrollTop;
	}
	if(window.pageXOffset){
		var iScrollLeft = window.pageXOffset;
	}else{
		var iScrollLeft = document.documentElement.scrollLeft;
	}
	oDiv.style.top 	= Math.round(iScrollTop + aScreenDimensions[3]/2 - (oContentDiv.style.height.replace('px','')/2)) + "px";
	oDiv.style.left = Math.round(iScrollLeft + aScreenDimensions[2]/2 - (oContentDiv.style.width.replace('px','')/2)) + "px";
	oContentDiv.style.height = 'auto';
}

function showAjaxLoader(){
	main_div = document.getElementById("al_main_div");
	msg_div = document.getElementById("al_area_div"); 
	moveDiv2center(main_div);
	main_div.style.display="block";
	msg_div.style.display="block";
}

function closeAjaxLoader(){
	closeLayers("al_main_div","al_area_div");
}

function closeLayers(sMainLayer,sInternalLayer){
	oShadow.closeShadowLayer();
	if(main_div = document.getElementById(sMainLayer)){
		main_div.style.top	= 0;
		main_div.style.left	= 0;
		main_div.style.display = "none";
		if(msg_div = document.getElementById(sInternalLayer)){
			msg_div.style.display = "none";
		}
	}else{
		alert('There is no "' + sMainLayer + '" object in this document.');
	}
	main_div = null;
	return false;
}

function showSFcontent(){
	if(document.all){
        debug_var(document.frames['service_frm'].document.body.innerHTML,1);
    }else{
        debug_var(document.getElementById('service_frm').contentWindow.document.body.innerHTML,1);
    }
}

function sendRequest(sPath,aParameters){
	showAjaxLoader();
	var oDataTransfer = new DataTransferCLS();   
	if(aParameters){
		for(key in aParameters){
			if(typeof(aParameters[key]) == 'object'){
				oDataTransfer.AddInput(aParameters[key]);
			}else{
				oDataTransfer.AddData(key,aParameters[key]);
			}
		}
      }
	oDataTransfer.SendData(sPath);
}
 

/*	re = new RegExp("^[_a-z0-9.-]{2,32}@[a-z0-9.-]{3,64}\.[a-z]{2,6}?$","i");
	if (!re.test(document.getElementById("invite_email").value)){
		 alert('Error! You gave an invalid email address!');
		 return false;
	}
	*/


var qTipTag = "span";//"a,label,input"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle, elements;

	var elementList = qTipTag.split(",");
	for(var j = 0; j < elementList.length; j++)
	{
		elements = document.getElementsByTagName(elementList[j]);
		if(elements)
		{
			for (var i = 0; i < elements.length; i ++)
			{
				a = elements[i];
				sTitle = a.getAttribute("title");
				if(sTitle)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;

	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.attachEvents = function (a) {
	if(!a.getAttribute('tiptitle'))
	{
		var sTitle = a.getAttribute("title");
		a.setAttribute("tiptitle", sTitle);
		a.removeAttribute("title");
		a.removeAttribute("alt");
		a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
		a.onmouseout = function() {tooltip.hide()};
		tooltip.show(a.getAttribute('tiptitle'))
	}
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

var oShadow = new ShadowCLS;

function select_all() {

           var tchk     = $(".mychb");
		   var tot_box  = document.getElementById("total_box");
		    
           for(var i =0;i<tchk.length;i++){
			    if(tot_box.checked==true){
					tchk[i].checked=true
				}else{
					tchk[i].checked=false;
				}
		   }

}

function sendChbRequest(sPath,aParameters){ 
	 
	var oDataTransfer = new DataTransferCLS();
	oDataTransfer.targetFrm='_self';
	if(aParameters){
		for(key in aParameters){
			if(aParameters[key] instanceof Array){
				for(var i=0; i<aParameters[key].length; i++){
                                     oDataTransfer.AddData(key+"[]",aParameters[key][i]);
                                }
			}else{ 
				oDataTransfer.AddData(key,aParameters[key]);
			}
		}
      }
	oDataTransfer.SendData(sPath);
}

function make_chb_set(sel_id){
         
	     var tchk         =  $(".mychb");
	     var chb_set_arr  = new Array();
		 for(var i =0;i<tchk.length;i++){
			    if(tchk[i].checked==true){
					chb_set_arr[chb_set_arr.length]=tchk[i].value;
				}
		   }
		    
         document.getElementById("chb_set").value = chb_set_arr;
		 if(document.getElementById(sel_id).value==0){
			 alert(" Выберите действие"); 
			 return false;
		 }
         else if(chb_set_arr.length==0){
			 alert(" Отсутствуют выбранные элементы! "); 
			 return false;
		 }

		 var mysel = $('#'+sel_id+' option:selected').attr('class');

		 var chb_set_info = ": "+chb_set_arr
		 for(var i =0;i<chb_set_arr.length;i++){
			    if(is_multy_id(chb_set_arr[i])){
					 chb_set_info = "";
				}
		   }

		 if(mysel=='confirm'){
			 var mysel_name = $('#'+sel_id+' option:selected').attr('name');
			 if(!confirm(" Вы действительно хотите "+mysel_name.toLowerCase()+" выбранные объекты"+chb_set_info+" ?")){
				 return false;
			 }
		 }
		 

         var data_set ={};

		 // Передаем обязательные параметры
		 data_set[$("#id_parameter").attr('value')]    = chb_set_arr;                     // набор элементов
		 data_set[$("#action_parameter").attr('value')]= $("#"+sel_id+"").attr("value");  // id действия над элементами
         // ********************************

         // Передаем дополнительные параметры
		 if(0!=$("#count_add_field").attr('value')){
			  for(i=0;i<$("#count_add_field").attr('value');i++){
				  if(null!=$("#addField_"+i)){
					  data_set[$("#addField_"+i).attr('name')]=$("#addField_"+i).attr('value');
				  }
			  }
		 }
		 // ************************************
		 sendChbRequest('',data_set)
}

function is_multy_id(AInputText) {
    var VRegExp = new RegExp(/[\|]/);
    var VResult = VRegExp.exec(AInputText); 
    return VResult;
  }

  $("a.top_link").attr({
	          "onclick":"this.blur()"
  });


  

 
