var agent=navigator.userAgent.toLowerCase();
if ((agent.indexOf("msie 6") > -1)||(agent.indexOf("msie 7") > -1)||(agent.indexOf("msie 8") > -1)) {
  document.write("<link rel='stylesheet' href='/css/ie.css' type='text/css'>");
}

function InfObj(type) {
	var inf = this;
	inf.type = type;
	inf.index = null;
	inf.townname = "";
	inf.cIndex = null;
	inf.width = null;
  inf.height = null;
  inf.domain = null;

  //--PNG
  inf.text=null;
  inf.border=null;
  inf.bgtitle=null;
  inf.titleinf=null;
  inf.bgbodycolor1=null;
  inf.bgbodycolor2=null;
  inf.bglogo1=null;
  inf.bglogo2=null;

  //--flash
  inf.name=null;
  inf.params=null;
  
  //--html
  inf.cities = null;
  inf.iconsHtmlType = 2;

  inf.countRequest=0;

  inf.callfunc=false;

	inf.img=null;
	
	inf.setDomain = function() {
			var arr = document.location.hostname.split('.');
			this.domain = arr[arr.length-1];		
	}

	inf.setInfParam=function(text, border, bgtitle, bgbodycolor1, bgbodycolor2, bglogo1, bglogo2){
    inf.text=text;
    inf.border=border;
    inf.bgtitle=bgtitle;
    inf.bgbodycolor1=bgbodycolor1;
    inf.bgbodycolor2=bgbodycolor2;
    inf.bglogo1=bglogo1;
    inf.bglogo2=bglogo2;  }

  inf.infChange=function(text, border, bgtitle, bgbodycolor1, bgbodycolor2, bglogo1,bglogo2){
  	inf.setInfParam(text, border, bgtitle, bgbodycolor1, bgbodycolor2, bglogo1,bglogo2);
  	inf.fillOwnColor(border);
  	inf.setInformer(inf.index, border, false);
  }

  inf.infFlashChange=function(params){
  	inf.params=params;
  	inf.setFlashInformer(inf.index, false);
  }

  inf.setCallbackFunct = function(name){
		inf.callfunc = name;
  }
  
  inf.setInformer=function(index, color, bUpdateSamples) {  	inf.index=index;
  	var towslist=document.getElementById("townlist");
  	if (towslist && inf.townname) inf.townname=towslist.options[towslist.selectedIndex].text;
  	if (color=="")  color=null;
  	if (color!=null) {
  		if (document.getElementById("borderinf").checked) inf.border=color;
  		if (document.getElementById("textinf").checked) inf.text=color;
  		if (document.getElementById("titleinf").checked) inf.bgtitle=color;
  		if (document.getElementById("body1inf").checked) inf.bgbodycolor1=color;
  		if (document.getElementById("body2inf").checked) inf.bgbodycolor2=color;
  		if (document.getElementById("logo1inf").checked) inf.bglogo1=color;
  		if (document.getElementById("logo2inf").checked) inf.bglogo2=color;

  		inf.fillOwnColor(color);
    }

    if(inf.callfunc!=false) {
      inf.img=document.createElement("img");      
      eval(inf.callfunc(inf.img, inf.type, inf.index, inf.text, inf.border, inf.bgtitle, inf.bgbodycolor1, inf.bgbodycolor2, inf.bglogo1, inf.bglogo2, bUpdateSamples));
    }

  }

  inf.setFlashInformer=function(index, bUpdateSamples) {
  	inf.index=index;
    if(inf.callfunc!=false)
      eval(inf.callfunc(bUpdateSamples));
  }
  
  inf.setHtmlInformer = function() {
  	if(inf.callfunc!=false) {
  		eval(inf.callfunc());
  	}
  }
  
  inf.setIconsHtmlType = function(value) {
  	this.iconsHtmlType = 2;
  	if (value) this.iconsHtmlType = 1;
  	this.setHtmlInformer();
  }
  
  inf.setHtmlInformerStyle = function(color) {
  	if (color=="")  color=null;
  	if (color!=null) {
  		if (document.getElementById("bgtitle").checked) 
  			this.addStyleCSS("meteonova-informer-bgtitle-color", "table.meteonova-informer td.mn-title", "background-color", "#"+color);
  		
  		if (document.getElementById("texttitle").checked)
  			this.addStyleCSS("meteonova-informer-texttitle-color-a", "table.meteonova-informer a.mn-title-a", "color", "#"+color);

  		if (document.getElementById("bgbody1").checked)
  			this.addStyleCSS("meteonova-informer-bgbody1-color", "table.meteonova-informer td.mn-main-td", "background-color", "#"+color);
  		    
  		if (document.getElementById("textbody").checked) {
   			//this.addStyleCSS("meteonova-informer-textbody-color-a", "table.meteonova-informer td.mn-main-td a", "color", "#"+color);
  			this.addStyleCSS("meteonova-informer-textbody-color", "table.meteonova-informer td.mn-main-td", "color", "#"+color);
  		}  

  		if (document.getElementById("bgtitledate").checked)
  			this.addStyleCSS("meteonova-informer-bgtitledate-color", "table.meteonova-informer td.mn-main-td-date", "background-color", "#"+color);

  		if (document.getElementById("texttitledate").checked)
  			this.addStyleCSS("meteonova-informer-texttitledate-color", "table.meteonova-informer td.mn-main-td-date", "color", "#"+color);

  		if (document.getElementById("bgbody2").checked)
  			this.addStyleCSS("meteonova-informer-bgbody2-color", "table.meteonova-informer td.mn-sub-td", "background-color", "#"+color);
    
  		if (document.getElementById("textbody2").checked)
   			this.addStyleCSS("meteonova-informer-textbody2-color", "table.meteonova-informer td.mn-sub-td", "color", "#"+color);
    
    	inf.fillOwnColor(color);
    }
    
    fillIcode();  	
  }
  
  inf.addStyleCSS = function(objId, selector, propert, value) {
  	var e = document.getElementById(objId);
  	if (!e) {
  		var e = document.createElement("style"); 
			e.type = "text/css";
			e.setAttribute("id", objId);
			document.getElementsByTagName("head")[0].appendChild(e);
		} 
		e.innerHTML = selector+"{"+propert+":"+value+"}";  
  }

  inf.checkColor=function(color) {
    document.getElementById("buttoncolor").disabled=true;
    if (color.length==6) {
        var reg=/[0-9a-f]{6}/i;
        var bOk=reg.test(color); 
        if (bOk) document.getElementById("buttoncolor").disabled=false;
  	}
  }
  
  inf.fillOwnColor=function(color){
  	document.getElementById("owncolor").value=color;
  	inf.checkColor(color);
  }

/////////////////////////////////// Списки /////////////////////////////////////

  inf.initAllListVars=function(){    
  	inf.countRequest++;
  	var request=inf.set_AjaxReguest();
  	inf.clearCountriesList("Загружается список");
  	inf.clearTownsList("Загружается список");
    inf.sendAjaxRequest(request,"/loadXML.php?id="+inf.countRequest+"&searchby=countries&tIndex="+inf.index+"&r="+Math.random(),"GET", inf.setAllLists, inf.fErrorAllList);
  }

  inf.setAllLists=function(request) {
  	var listrecords=request.responseXML.getElementsByTagName('c');
  	if (listrecords.length>0) {  		 inf.cIndex=listrecords[0].getAttribute("i");
  		 inf.clearRegionsList();  		 inf.setTownsAndRegionLists(inf.cIndex,0);
  	}
  	var request=inf.set_AjaxReguest();
  	inf.sendAjaxRequest(request,"/loadXML.php?id="+inf.countRequest+"&searchby=countries&r="+Math.random(),"GET", inf.fillCountriesList, inf.fErrorCountriesList);
  }


  inf.setTownsAndRegionLists=function(cIndex,nRegion){
    inf.clearRegionsList();
    inf.setTownsLists(cIndex,nRegion);
    inf.setRegionLists(cIndex,nRegion);
  }

  inf.setTownsLists=function(cIndex,nRegion) {
  	var request=inf.set_AjaxReguest();
  	if (cIndex!=inf.cIndex) {
  		inf.cIndex=cIndex;
  	}
  	inf.clearTownsList("Загружается список");
    inf.sendAjaxRequest(request,"/loadXML.php?id="+inf.countRequest+"&searchby=towns&cIndex="+inf.cIndex+"&state="+nRegion+"&r="+Math.random(),"GET", inf.fillTownsLists, inf.fErrorTownsAndRegionLists);
  }


  inf.setRegionLists=function(cIndex,nRegion){
  	var request=inf.set_AjaxReguest();
  	if (cIndex!=inf.cIndex) {
  		inf.clearRegionsList();
  		inf.cIndex=cIndex;
  	}
  	//alert("/loadXML.php?id="+sel.countRequest+"&searchby=regions&cIndex="+sel.cIndex+"&state="+nRegion+"&r="+Math.random());
    inf.sendAjaxRequest(request,"/loadXML.php?id="+inf.countRequest+"&searchby=regions&cIndex="+inf.cIndex+"&state="+nRegion+"&r="+Math.random(),"GET", inf.fillRegionsLists, inf.fErrorTownsAndRegionLists);

  }

  inf.fillTownsLists=function(request) {
  	var listrecords=request.responseXML.getElementsByTagName('t');
  	if (listrecords.length>0) inf.fillSelectTownsList(listrecords);
  	else inf.clearTownsList("Отсутствует список");
  }

  inf.fillRegionsLists=function(request) {
  	var listrecords=request.responseXML.getElementsByTagName('r');
  	inf.fillSelectRegionsList(listrecords);
  }

  inf.fillSelectTownsList=function(listrecords){	  var towslist=document.getElementById("townlist");
	  if (towslist) {
	    towslist.options.length=0;
        towslist.disabled=false;
	    for (i=0; i<listrecords.length;i++) {
	      var newOption = document.createElement("OPTION");
	      newOption.value = listrecords[i].getAttribute("i");
	      newOption.text = listrecords[i].getAttribute("n");
		  towslist.options[towslist.options.length] = newOption;
		  if (inf.index==newOption.value)  {		  	towslist.selectedIndex=i;
		  	inf.townname=towslist.options[towslist.selectedIndex].text;
		  	inf.setInformer(inf.index,null,true);
	      }
	      newOption = null;
	    }
	  }  }

  inf.fillSelectRegionsList=function(listrecords){
	  var regionslist=document.getElementById("regionlist");
	  if (regionslist) {
	  	if (listrecords.length>0) regionslist.disabled=false;
	    for (i=0; i<listrecords.length;i++) {
	      var newOption = document.createElement("OPTION");
	      newOption.value = listrecords[i].getAttribute("state");
	      newOption.text = listrecords[i].getAttribute("n");
		  regionslist.options[regionslist.options.length] = newOption;
	      newOption = null;
	    }
	  }
  }

  inf.fillCountriesList=function(request) {  	var listrecords=request.responseXML.getElementsByTagName('c');
    var countrieslist=document.getElementById("countrylist");
    if (listrecords.length==0) {
	  inf.clearCountriesList("Отсутствует список");
	  return;
	}
  	else {
	  countrieslist.disabled=false;
	  countrieslist.options.length=0;
	  for (i=0; i<listrecords.length;i++) {
	    var newOption = document.createElement("OPTION");
	    newOption.value = listrecords[i].getAttribute("i");
	    newOption.text = listrecords[i].getAttribute("n");
		countrieslist.options[countrieslist.options.length] = newOption;
		if (inf.cIndex==newOption.value) countrieslist.selectedIndex=i;
	    newOption = null;
	  }    }  }

  inf.clearTownsList=function(str) {
    var towslist=document.getElementById("townlist");
    towslist.options.length=0;
    towslist.disabled=true;
	var newOption = document.createElement("OPTION");
	newOption.value = 0;
	newOption.text = str;
    towslist.options[towslist.options.length] = newOption;
    newOption=null;
  }

  inf.clearRegionsList=function() {
    var regionslist=document.getElementById("regionlist");
    regionslist.options.length=0;
    regionslist.disabled=true;
	var newOption = document.createElement("OPTION");
	newOption.value = 0;
	newOption.text = "Все области";
    regionslist.options[regionslist.options.length] = newOption;
    newOption=null;
  }

  inf.clearCountriesList=function(str) {    
  	var countrylist=document.getElementById("countrylist");
    countrylist.options.length=0;
    countrylist.disabled=true;
	var newOption = document.createElement("OPTION");
	newOption.value = 0;
	newOption.text = str;
    countrylist.options[countrylist.options.length] = newOption;
    newOption=null; 
  }

  inf.fErrorAllList=function() {
  	inf.clearCountriesList("Отсутствует список");
  	inf.clearTownsList("Отсутствует список");  }

  inf.fErrorCountriesList=function() {
  	inf.clearCountriesList("Отсутствует список");
  }

  inf.fErrorTownsAndRegionLists=function() {
  	inf.clearTownsList("Отсутствует список");
  }

  inf.set_AjaxReguest=function(){  	var request = false;
  	if (window.XMLHttpRequest) // Mozilla, Safari, ...
    	request = new XMLHttpRequest();
  	else if (window.ActiveXObject) // IE
  		request = new ActiveXObject("Microsoft.XMLHTTP");
  	return request;  }

  inf.sendAjaxRequest=function(request,url,method,callback,onerror) {  	request.onreadystatechange = function() { inf.requestFunction(request,method,callback,onerror); };
  	request.open(method, url, true);
  	request.send(null);  }

  inf.requestFunction=function(req,method,fcall,ferror){		var self;
	  if (req) self={req:req,method:method,fcall:fcall,ferror:ferror,request:arguments.callee.request}
	  else self=arguments.callee;
	  if(self.req.readyState == 4) {
	    var callbackArgsArray=new Array(self.req);
	    try{
	    	if(self.req.status == 200) {
	        self.fcall.apply(null,callbackArgsArray);
	      }
	      else self.ferror.apply(null,callbackArgsArray);
	    }
	    catch(err) {
	    	self.ferror.apply(null, callbackArgsArray);
	    }
			//self.req.ready=0;
	  	//self.req.abort();
	  }  }
	  
		inf.setGeotarg = function() {
			var e = document.getElementById("setGeotarg");
			if (e) {
				//if (e.checked) inf.disabledSelectLists(true);
				//else inf.disabledSelectLists(false);
				inf.setInformer(inf.index, null, true);
			}
		}	
		
	 	inf.disabledSelectLists = function(value) {
	 		var arr = new Array("countrylist", "regionlist", "townlist");
	 		for (var i=0; i<arr.length; i++) {
	 			var e = document.getElementById(arr[i]);
	 			if (e) {
	 				e.disabled = value;	
	 				if (value) e.className = "selectList_disable";
	 				else e.className = "selectList";
	 			} 
	 		}
	 		arr=null;	 		
	 	}
	 	
	inf.fillInformerCityList = function(value) {
		var e = document.getElementById("infcitylist");
		if (e) {
			for (var i = 0; i< value.length; i++) {
				var newOption = document.createElement("OPTION");
				newOption.value = parseInt(value[i]);
				newOption.text = value[i].replace(parseInt(value[i]), '');
    		e.options[e.options.length] = newOption;
    		newOption=null;	
			}
		}
	}	
	
	inf.addCity = function() {
		var e = document.getElementById("townlist");
		if (this.isInformerCityList(e.options[e.selectedIndex].value)) { alert('Выбранный город уже присутствует в списке городов для информера'); return; }
		var arr = [];
		arr.push(e.options[e.selectedIndex].value+e.options[e.selectedIndex].text);
		this.fillInformerCityList(arr);
		arr = null;
		this.setCities();
		this.setHtmlInformer();
	}	
	
	inf.upCity = function() {
 		var e = document.getElementById("infcitylist");
 		var i = e.selectedIndex;
 		if(i > 0) {
   		e1 = e.options[i];
   		e2 = e.options[i-1];
   		e.insertBefore(e1,e2);
 		}	
		this.setCities();
		this.setHtmlInformer();
	}
	
	inf.downCity = function() {
 		var e = document.getElementById("infcitylist");
 		var i = e.selectedIndex;
		if(i < e.options.length-1) {
			e1 = e.options[i];
		  e2 = e.options[i+1];
		  e.insertBefore(e2,e1);
		}
		this.setCities();
		this.setHtmlInformer();		
	}
	
	inf.deleteCity = function() {
 		var e = document.getElementById("infcitylist");
		if (e.options.length == 1) { alert("Список выбранных городов должен содержать хотя бы один город"); return; }
		if (e.selectedIndex == -1) return;
		e.remove(e.selectedIndex);
		e.selectedIndex = e.length -1;
		this.setCities();
		this.setHtmlInformer();		
	}    

	inf.setCities = function(){
		this.cities = [];
		var e = document.getElementById("infcitylist");
		for(var i=0; i<e.length; i++) 
			this.cities.push(e.options[i].value+e.options[i].text);
	}
	
	inf.isInformerCityList = function(value) {
		var e = document.getElementById("infcitylist");
		if (e) {
			for (var i = 0; i < e.options.length; i++) {
				if (e.options[i].value == value) return true;
			}
		}	
		return false;	
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

var bIE6=agent.indexOf("msie 6")>-1;

function transparent(im) {
   setTimeout(function(){
   if (bIE6 && (!im.transparented)) {
      im.transparented = 1;
      var picture = im.src;
      var w = im.width;
      var h = im.height;
      im.src = "/images/clear.gif";
      im.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='" + picture + "');";
      im.width = w;
      im.height = h;
      }
   return "transparent";
   }, Math.floor(Math.random()*400));
}
