/*

	core lib for all scripts
	
	by zoonman
	
*/


var _eCache = {};
function $(id) {
	return _eCache[id] || (_eCache[id] = (document.getElementById) ? document.getElementById(id) : document.all(id));
}
 
function $_(id) {
	return ((document.getElementById) ? document.getElementById(id) : document.all(id));
}


function bindReady(handler){
	var called = false;
	function ready() { // (1)
		if (called) return;
		called = true;
		handler();
	}
	if ( document.addEventListener ) { // (2)
		document.addEventListener( "DOMContentLoaded", function(){
			ready();
		}, false );
	} 
	else if ( document.attachEvent ) {  // (3)
		// (3.1)
		if ( document.documentElement.doScroll && window == window.top ) {
			function tryScroll(){
				if (called) return;
				if (!document.body) return;
				try {
					document.documentElement.doScroll("left");
					ready();
				} catch(e) {
					setTimeout(tryScroll, 0);
				}
			}
			tryScroll();
		}
		// (3.2)
		document.attachEvent("onreadystatechange", function(){
			if ( document.readyState === "complete" ) {
				ready()
			}
		});
	}
	// (4)
  if (window.addEventListener)
      window.addEventListener('load', ready, false)
  else if (window.attachEvent)
      window.attachEvent('onload', ready)
  /*  else  // (4.1)
      window.onload=ready
	*/
}

var readyList = []

function onReady(handler) {
	if (!readyList.length) {
		bindReady(function() {
			for(var i=0; i<readyList.length; i++) {
				readyList[i]()
			}
		})
	}
	readyList.push(handler)
}



/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType,update_object){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.update_object = update_object;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (!this.req) try {
    this.req = new XMLHttpRequest();
  } catch (e){;};
  if (!this.req) try {
    this.req = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e){;};
  if (!this.req) try {
    this.req = new ActiveXObject('Microsoft.XMLHTTP');
  } catch (e){;};
  
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }
    catch (err){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
  	var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}



/* после повышения рейтинга */
function a_p1(o) {
	return (function() {
		o.onclick=null;	
		o.className = 'plus1 inactive';
	});
}
/* повышаем рейтинг */
function plus_odin(o,b){
	//o.innerHTML = 'Запоминаем...';	
	var s_u= 'http://'+window.location.host+'/rpc.php?p=rate.scenarios';
	var s_p = 'id=' + encodeURIComponent(b);
	var p_f = a_p1(o);
	var s_l=new net.ContentLoader(s_u,p_f,function (){},'POST',s_p	);
	return false;
}

/* форма обратной связи */
function maxlength(){
	var message=document.feedback.message.value;
	var maxLenght=1000;//Максимальное количество символов в сообщении
	if (message.length>maxLenght) document.feedback.message.value=message.substring(0,maxLenght);
	verifyForm(document.feedback);
}

function verifyForm(form) {
	if(form.email.value=="" 
			|| (!/^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i.test(form.email.value))) {
		form.email.style.color='red';
	}
	else {
		form.email.style.color='green';
	}
	
	if(form.name.value=="" 
			|| form.email.value=="" 
			|| (!/^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i.test(form.email.value))
			|| form.message.value=="")	{
		form.submit.disabled=true;
	}
	else {
		form.submit.disabled=false;
	}
}

/* переключение блока дополнения */
function ocl(){
	if( $('addition-container').style.display != 'block') {
		$('addition-container').style.display = 'block';
			$_('addition-message').focus() ;
			$_('addition-header').innerHTML = 'Ваш текст:';
	}
	else{
		$('addition-container').style.display = 'none';
		$_('addition-header').innerHTML = 'Если у вас есть что добавить, нажмите здесь';
	}
}

/* отправление дополнения */
function review_it(){
	//o.innerHTML = 'Запоминаем...';	
	var s_u= 'http://'+window.location.host+'/rpc.php?p=review.holidays';
	var s_p = 'addition-message=' + encodeURIComponent($_('addition-message').value)+
	'&addition-email=' + encodeURIComponent($_('addition-email').value);
	$_('addition-email').disabled = 
	$_('addition-message').disabled = 
	$_('addition-send').disabled = true;
	$_('addition-header').innerHTML = 'Отправляется...';
	var s_l=new net.ContentLoader(s_u,function (){
		$_('addition-header').innerHTML = 'Ваше дополнение было отправлено';
		var w = window.setTimeout(function(){
				ocl();
				$_('addition-message').disabled = 
				$_('addition-email').disabled = 
				$_('addition-send').disabled = false;
				$_('addition-message').value = '';
		},3000);
	},function (){},'POST',s_p	);
	return false;
}


onReady(function () {
	if ($_('feedback'))
		setInterval("maxlength()",100);
});
