	var c = new Array("#E5E5E5", "#D3DCE3", "#CCFFCC", "#dab8e4", "#f78383" );
	function fnSetColor( obj, n, e )
	{ obj.style.backgroundColor = e? c[2] : c[n]; } 



	function get_element(id)
	{
		return document.getElementById ? document.getElementById(id) : document.all[id];
	}

	function put_in_place(obj, put, offx, offy)
	{
		var s = get_element(put);
		var l = s.offsetLeft;
		var t = s.offsetTop;

		while (s.tagName != "BODY") {
			s = s.offsetParent;
			l += s.offsetLeft;
			t += s.offsetTop;
		}
		l += offx;
		t += offy;
		get_element(obj).style.left = l + 'px';
		get_element(obj).style.top = t + 'px';
		show(obj);
	}
	function hide(obj)
	{
		get_element(obj).style.visibility = 'hidden';
	}
	function show(obj)
	{
		get_element(obj).style.visibility = 'visible';
	}


	function GetAreas(need_cities,need_companies,with_videos)
	 {
		if (need_cities) var city_id=get_element('city_id'); 
		if (need_companies)
		 {
		  var company_id=get_element('company_id');
		  while (company_id.options.length) company_id.options[0]=null; 
		 }
		var region_id=get_element('region_id'); var id=region_id.options[region_id.selectedIndex].value;
		var area_id=get_element('area_id');
		while (area_id.options.length) area_id.options[0]=null; 
		if (with_videos) with_videos='&with_videos=1'; else with_videos='';
		var url="index.php?page=service&whois=prepare_areas&region_id="+id+with_videos;

		if (window.XMLHttpRequest)
		 {
			xrequest=new XMLHttpRequest();
			xrequest.onreadystatechange=processReqChange;
			var my_status='start'; area_id.disabled=true; if (need_cities) city_id.disabled=true; if (need_companies) company_id.disabled=true;
			xrequest.open("GET", url, true);
			xrequest.send(null);
	     } else if (window.ActiveXObject) 
			{
				var xrequest=new ActiveXObject("Microsoft.XMLHTTP");
				xrequest.onreadystatechange = processReqChange;
				var my_status='start'; area_id.disabled=true; if (need_cities) city_id.disabled=true; if (need_companies) company_id.disabled=true;
				xrequest.open("GET",url,false);
				xrequest.send();
			}

		function processReqChange() 
		 {
		  if (xrequest.readyState==4 && my_status=='start')
		   {
			if (xrequest.status==200)
			 {
              var result=xrequest.responseText;
              if (result.substr(0,2)=='__') alert (result.substr(2));
			    else
				 {
					var a=result.split('~');
					while (a.length) area_id.options[area_id.options.length]=new Option(a.shift(),a.shift());
					if (need_cities) GetCities(need_companies,with_videos);
				 }
			 } else alert("Can not get areas!:\n" +	xrequest.statusText);
			my_status='end'; area_id.disabled=false;
		   }
		 }

	 }


	function GetCities(need_companies,with_videos)
	 {
		if (need_companies)
		 {
		  var company_id=get_element('company_id');
		  while (company_id.options.length) company_id.options[0]=null; 
		 }
		var area_id=get_element('area_id'); var id=area_id.options[area_id.selectedIndex].value;
		var city_id=get_element('city_id');
		while (city_id.options.length) city_id.options[0]=null; 
		if (with_videos) with_videos='&with_videos=1'; else with_videos='';
		var url="index.php?page=service&whois=prepare_cities&area_id="+id+with_videos;

		if (window.XMLHttpRequest)
		 {
			xrequest=new XMLHttpRequest();
			xrequest.onreadystatechange=processReqChange_2;
			var my_status='start'; city_id.disabled=true; if (need_companies) company_id.disabled=true;
			xrequest.open("GET", url, true);
			xrequest.send(null);
	     } else if (window.ActiveXObject) 
			{
				var xrequest=new ActiveXObject("Microsoft.XMLHTTP");
				xrequest.onreadystatechange = processReqChange_2;
				var my_status='start'; city_id.disabled=true; if (need_companies) company_id.disabled=true;
				xrequest.open("GET",url,false);
				xrequest.send();
			}

		function processReqChange_2() 
		 {
		  if (xrequest.readyState==4 && my_status=='start')
		   {
			if (xrequest.status==200)
			 {
              var result=xrequest.responseText;
              if (result.substr(0,2)=='__') alert (result.substr(2));
			    else
				 {
					var a=result.split('~');
					while (a.length) city_id.options[city_id.options.length]=new Option(a.shift(),a.shift());
					if (need_companies) GetCompanies();
				 }
			 } else alert("Can not get cities!:\n" +	xrequest.statusText);
			my_status='end'; city_id.disabled=false;
		   }
		 }

	 }


	function GetCompanies()
	 {
		var city_id=get_element('city_id'); var id=city_id.options[city_id.selectedIndex].value;
		var company_id=get_element('company_id');
		while (company_id.options.length) company_id.options[0]=null; 
		var url="index.php?page=service&whois=prepare_companies&city_id="+id;

		if (window.XMLHttpRequest)
		 {
			xrequest=new XMLHttpRequest();
			xrequest.onreadystatechange=processReqChange_3;
			var my_status='start'; company_id.disabled=true;
			xrequest.open("GET", url, true);
			xrequest.send(null);
	     } else if (window.ActiveXObject) 
			{
				var xrequest=new ActiveXObject("Microsoft.XMLHTTP");
				xrequest.onreadystatechange = processReqChange_3;
				var my_status='start'; company_id.disabled=true;
				xrequest.open("GET",url,false);
				xrequest.send();
			}

		function processReqChange_3()
		 {
		  if (xrequest.readyState==4 && my_status=='start')
		   {
			if (xrequest.status==200)
			 {
              var result=xrequest.responseText;
              if (result.substr(0,2)=='__') alert (result.substr(2));
			    else
				 {
					var a=result.split('~');
					while (a.length) company_id.options[company_id.options.length]=new Option(a.shift(),a.shift());
				 }
			 } else alert("Can not get cities!:\n" +	xrequest.statusText);
			my_status='end'; company_id.disabled=false;
		   }
		 }

	 }


	function ChangeOwner()
	{
		get_element('submit_button').disabled=true;
		get_element('uch_message').innerHTML='';
		hide('owners');
		var owner_id=get_element('owner_id'); var id=owner_id.options[owner_id.selectedIndex].value;
		if (id == 1 || id == 3 || id == 4)
		{
			get_element('input_name_text').innerHTML = owner_id.options[owner_id.selectedIndex].text + '<font color="red">*</font>:';
		}
			else
			{
				get_element('input_name_text').innerHTML = '';
			}

		if (id == 1)
		{
			get_element('uch_id').name='user_id';
			get_element('uch_name').name='user_fullname';
			show('uch_name_block');
			show('categories_block');
			get_element('uch_name').focus();
		}
		if (id == 2)
		{
			get_element('uch_id').name='null';
			get_element('uch_name').name='null';
			hide('uch_name_block');
			show('categories_block');
			get_element('submit_button').disabled=false;
		}
		if (id == 3)
		{
			get_element('uch_id').name='company_id';
			get_element('uch_name').name='company_title_D';
			show('uch_name_block');
			show('categories_block');
			get_element('uch_name').focus();
		}
		if (id == 4)
		{
			get_element('uch_id').name='hist_id';
			get_element('uch_name').name='hist_title_D';
			show('uch_name_block');
			hide('categories_block');
			get_element('uch_name').focus();
		}

		get_element('uch_id').value='0';
		get_element('uch_name').value='';

		if (id != 4) GetCategories(id);
	}
	

	var old_value_name='';
	function GetOwnersList()
	 {
		if (get_element('uch_name').value==old_value_name) return;
		old_value_name=get_element('uch_name').value;
		get_element('submit_button').disabled=true;
		get_element('uch_message').innerHTML='';
		if (get_element('uch_name').value.length < 3) return;
		var owner_id=get_element('owner_id'); var id=owner_id.options[owner_id.selectedIndex].value;
		var owners=get_element('owners');
		while (owners.options.length > 1) owners.options[1]=null; 
		var url="index.php?page=service&whois=prepare_owners&owner_id="+id+"&name="+get_element('uch_name').value;

		owners.disabled=true;
		hide('owners');
		if (window.XMLHttpRequest)
		 {
			xrequest=new XMLHttpRequest();
			xrequest.onreadystatechange=processReqChange_GOL;
			var my_status='start';
			xrequest.open("GET", url, true);
			xrequest.send(null);
	     } else if (window.ActiveXObject) 
			{
				var xrequest=new ActiveXObject("Microsoft.XMLHTTP");
				xrequest.onreadystatechange = processReqChange_GOL;
				var my_status='start';
				xrequest.open("GET",url,false);
				xrequest.send();
			}

		function processReqChange_GOL()
		 {
		  if (xrequest.readyState==4 && my_status=='start')
		   {
			if (xrequest.status==200)
			 {
              var result=xrequest.responseText;
              if (result.substr(0,2)=='__') alert (result.substr(2));
			    else
				 {
					var a=result.split('~');
					while (a.length) owners.options[owners.options.length]=new Option(a.shift(),a.shift());
					if (owners.options.length == 2)
					{
						owners.selectedIndex = 1;
						ChangeOwnerOne();
					}
						else
						{
							owners.disabled=false;
							show('owners');
						}
				 }
			 } else alert("Can not get owners!:\n" +	xrequest.statusText);
			my_status='end';
		   }
		 }

	 }


	function ChangeOwnerOne()
	{
		var owners=get_element('owners');
		var id=owners.options[owners.selectedIndex].value;
		var _name=owners.options[owners.selectedIndex].text;
		if (id <= 0) return;
		get_element('uch_id').value = id;
		get_element('uch_name').value = _name;
		hide('owners');
		get_element('submit_button').disabled=false;
		get_element('uch_message').innerHTML='(ID: ' + id + ')';
	}
	

	function GetCategories(id)
	 {
		if (id < 1 || id > 3) return;
		var categories=get_element('category_id');
		while (categories.options.length) categories.options[0]=null; 
		var url="index.php?page=service&whois=prepare_categories&owner_id="+id;

		get_element('owner_id').disabled=true;
		categories.disabled=true;
		if (window.XMLHttpRequest)
		 {
			xrequest=new XMLHttpRequest();
			xrequest.onreadystatechange=processReqChange_Cat;
			var my_status='start';
			xrequest.open("GET", url, true);
			xrequest.send(null);
	     } else if (window.ActiveXObject) 
			{
				var xrequest=new ActiveXObject("Microsoft.XMLHTTP");
				xrequest.onreadystatechange = processReqChange_Cat;
				var my_status='start';
				xrequest.open("GET",url,false);
				xrequest.send();
			}

		function processReqChange_Cat()
		 {
		  if (xrequest.readyState==4 && my_status=='start')
		   {
			if (xrequest.status==200)
			 {
              var result=xrequest.responseText;
              if (result.substr(0,2)=='__') alert (result.substr(2));
			    else
				 {
					var a=result.split('~');
					while (a.length) categories.options[categories.options.length]=new Option(a.shift(),a.shift());
					categories.disabled=false;
				 }
			 } else alert("Can not get owners!:\n" +	xrequest.statusText);
			my_status='end';
			get_element('owner_id').disabled=false;
		   }
		 }

	 }


	function GoCity()
	{
		var city_id=get_element('city_id');
		var loc="index.php?change_city="+city_id.options[city_id.selectedIndex].value;
		GoURL(loc);
	}


	function GoVideo(id)
	{
		GoURL("index.php?page=show_movie&id="+id);
	}


	function GoURL(url,new_wind)
	{
	 if (new_wind>=1)
	 {
		window.open(url,'',''); return false;
	 } else
		{
		if (navigator.userAgent.indexOf("Opera")!=-1) document.location=url;
			else document.location.href=url;
		}
	}

	function ratenow(i)
	{
		if (i==0)
			for (k=1; k<=5; k++)
				get_element('star_'+k).src='images/icon-star-minus.gif';
			else
			{
				for (k=1; k<=i; k++)
					get_element('star_'+k).src='images/icon-star-plus.gif';
			}
	}

	function rate(i,id)
	{
		if (i>=1 && i<=5 && confirm('Are You sure rate '+i+' from 5 for this video?'))
			document.location='index.php?page=rate&value='+i+'&id='+id;
	}

	function CopyBuf(src)
	{
      if (document.body.createTextRange) {
        var BodyRange = document.body.createTextRange(); 
        BodyRange.moveToElementText(get_element(src)); 
        BodyRange.execCommand('Copy');  
        alert('URL copied to the clipboard');
      } else {
        alert('Your browser does not support operations with cliboard. Please copy URL from right block');
      }
      return false;
	}

function urlencode(plaintext)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}

function urldecode(encoded)
{
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}
