function createHttpRequest() {
var httpRequest;
// Mozilla/Safari

if (window.XMLHttpRequest)
	{
    httpRequest=new XMLHttpRequest();
	}
	// Internet Explorer
else if (typeof ActiveXObject != 'undefined')
		{
		httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
		}
return httpRequest;
}

function sendRequest(file, _resultId, getRequestProc) {
//- проверяем можно ли отправлять данные
if(document.getElementById('ajax_flag').value==0)
	{
	resultId = _resultId;
	document.getElementById(resultId).innerHTML = '<center><img src="../style/load.gif"></center>';
	httpRequest.open('POST', file);
	httpRequest.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
	httpRequest.onreadystatechange = getRequestProc;
	httpRequest.send(null);
	}
}


function getRequest() {
//-- прячим десктопы
if (httpRequest.readyState == 0) {
document.getElementById(resultId).innerHTML = 'неинициализирован';
}
if (httpRequest.readyState == 1) {
//-- ставим флаг что начали отправку
document.getElementById('ajax_flag').value=1;
}

if (httpRequest.readyState == 4) {
//-- ставим флаг  что приняли
document.getElementById('ajax_flag').value=0;
document.getElementById(resultId).innerHTML = httpRequest.responseText;
document.getElementById(resultId).style.visibility = 'visible';
}
}
var httpRequest = createHttpRequest();
var resultId = '';



function onlynum()
	{
		   var k;
		   k=event.keyCode;
		   if( (k >= 48 && k <= 57) || k == 8) return k
		   else return false
	}
function onlylet()
	{
		   var k;
		   k=event.keyCode;
		   if(  ( k >=66 && k <=91 ) || ( k >=98 && k <=122 ) || ( k >=1040 && k <=1104 ) || k == 8) return k
		   else return false
	}
function onlylet_ru()
	{
		   var k;
		   k=event.keyCode;
		   if( k >=1040 && k <=1104 || k == 8) return k
		   else return false
	}
function onlylet_en()
	{
		   var k;
		   k=event.keyCode;
		   if(  ( k >=66 && k <=91 ) || ( k >=98 && k <=122 ) || k == 8) return k
		   else return false
	}
function only()
	{
		   var k;
		   k=event.keyCode;
		   if( (k >= 48 && k <= 57) || ( k >=66 && k <=91 ) || ( k >=98 && k <=122 ) || k == 8) return k
		   else return false
	}


var lastDiv = "";
function showDiv(divName) {
	// hide last div
	if (lastDiv) {
		document.getElementById(lastDiv).className = "hidden";
	}
	//if value of the box is not nothing and an object with that name exists, then change the class
	if (divName && document.getElementById(divName)) {
		document.getElementById(divName).className = "show";
		lastDiv = divName;
	}
}

function checkpwd(pwd1,pwd2) {
	if (pwd1.length < 5) {
        showDiv("badpwd");
	}
	else{
		if(pwd1 == pwd2)
			{showDiv("good");}
		else
			{showDiv("bad");}
	}
}
function chkemail(email)
{
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i.test(email))
	{showDiv("goodemail");}
	else
	{showDiv("bademail");}
}
//


//
function createIFrame() {
  var id = 'f' + Math.floor(Math.random() * 99999);
  var div = document.createElement('div');
  div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="sendComplete(\''+id+'\')"></iframe>';
  document.body.appendChild(div);
  return document.getElementById(id);
}

function sendForm(form, url, func, arg) {
  if (!document.createElement) return; // not supported
  if (typeof(form)=="string") form=document.getElementById(form);
  var frame=createIFrame();
  frame.onSendComplete = function() { func(arg, getIFrameXML(frame)); };
  form.setAttribute('target', frame.id);
  form.setAttribute('action', url);
  form.submit();
}

function sendComplete(id) {
  var iframe=document.getElementById(id);
  if (iframe.onSendComplete && typeof(iframe.onSendComplete) == 'function') iframe.onSendComplete();
}

function getIFrameXML(iframe) {
  var doc=iframe.contentDocument;
  if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
  if (!doc) doc=window.frames[iframe.id].document;
  if (!doc) return null;
  if (doc.location=="about:blank") return null;
  if (doc.XMLDocument) doc=doc.XMLDocument;
  return doc;
}

var cnt=0;

function uploadComplete(element, doc) {
  if (!doc) return;
  if (typeof(element)=="string") element=document.getElementById(element);
  element.innerHTML='Результат запроса #'+(++cnt)+': '+doc.documentElement.firstChild.nodeValue;
}


function calculate(){
  var UsersTarifs;
  var TotalMoney = new Number(document.getElementById('Totalmoney').value);
  var UserTarif  = document.getElementById('tarif').value;
  var BuyMoney   = new Number(document.getElementById('BuyMoney').value);
  UsersTarifs = UserTarif.split('_');
  var TarifID = UsersTarifs[0];
  var TarifPriceToShow = UsersTarifs[1];
  //var TarifPrice = UsersTarifs[2];
  TarifPrice=new Number(UsersTarifs[2]);
  var BuyShows;
  var textOfBuy;
  var TotalMoneyNew;
  if(TarifPrice==0)
  {
  document.getElementById('pricediv').style.display='none';
     if(TotalMoney > TarifPriceToShow)
	{
	TotalMoneyNew = TotalMoney - BuyMoney;
	BuyShows=0;
	textOfBuy = "Безлимитный. Сумма: "+TarifPriceToShow+" грн";
	}
	else
	{
	 textOfBuy= "Не достаточно денег. Цена: "+TarifPriceToShow+" грн";
	 TotalMoneyNew = TotalMoney;
	}
  }
  if(TarifPrice!=0)
  {
  document.getElementById('pricediv').style.display='block';
  if( (TotalMoney-BuyMoney) >= 0)
    {
    BuyShows = parseInt((BuyMoney/TarifPrice)*1000);
    TotalMoneyNew = TotalMoney - BuyMoney;
    textOfBuy = "1 000 показов. Сумма: "+TarifPriceToShow+" грн";
    }
    else
    {
    BuyMoney = TotalMoney;
    BuyShows = parseInt((BuyMoney/TarifPrice)*1000);
    TotalMoneyNew = TotalMoney - BuyMoney;
    document.getElementById('BuyMoney').value = BuyMoney;
    textOfBuy = "1 000 показов. Сумма: "+TarifPriceToShow+" грн";
    }  }

  document.getElementById('price_to_show').value = textOfBuy;
  document.getElementById('Totalmoney').value = TotalMoneyNew;
  document.getElementById('BuyShows').value = "Купленно показов:"+BuyShows ;

}