function ge (id)
{
	return document.getElementById(id);
};

/*
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,fparams,method,params,contentType){
  this.req=null;
  this.fparams=fparams;
  this.onload=onload;
  //this.onerror=(onerror) ? onerror : this.defaultError;
  this.onerror = this.defaultError;
  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 (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  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.setRequestHeader("Content-Type","multipart/form-data"); 
      }
      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,this.fparams);
    }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()
     +"\ncontent: "+this.req.responseText);
}



function prepare_post(dataarray)
{
	// important!!! data come in script in utf-8
	var ajax_post = "";
	for (i=0;i<dataarray.length;i++)
	{
		ajax_post += dataarray[i].parametr+"="+encodeURIComponent(dataarray[i].value)+'&';
	};
	return ajax_post;
};

function ajax_url (url,resid)
{
	new net.ContentLoader(url,ajax_res_in_block,(resid),'GET');
};

function ajax_form (id,resid)
{
	var data = new Array();
	count = 0;
	for (i=0;i<ge(id).elements.length;i++)
	{
		if (ge(id).elements[i].type == "checkbox" && !ge(id).elements[i].checked)
		{
			continue;
		};
		if (ge(id).elements[i].type == "radio" && !ge(id).elements[i].checked)
		{
			continue;
		};		
		data[count] = {parametr:ge(id).elements[i].name,value:ge(id).elements[i].value};
		count++;
	};
	new net.ContentLoader(ge(id).action,ajax_from_frame_to_block,(resid),'POST',prepare_post(data));
};
function ajax_from_frame_to_block(id)
{
var response = this.req.responseText;
  
 
	if (response.indexOf('redirect:') != -1)
	{
		 alert(response);
		response = response.replace('redirect:','');
		window.location.href=response;
	}
	else
	{
		//	alert('qweqweqwe');
		ge(id).innerHTML = response;
	}
};

function ajax_res_in_block(id)
{
	var response = this.req.responseText;
	//alert(response);
	if (response.indexOf('redirect:') != -1)
	{
		response = response.replace('redirect:','');
		redirect(response);
	}
	else
	{
		//	alert(response);
		ge(id).innerHTML = response;
	}
};

function clear_block (id)
{
	ge(id).innerHTML = '';
};

function redirect(url)
{
	window.location = url;
};

function ShowPreview (img,preview)
{
	ge(preview).src = ge(img).value;
	ge(preview).style.display = "block";
};	

