function getKupon(){
	var 
		from = ['KuponInpSurname', 'KuponInpName', 'KuponInpEmail'],
		into = ['KuponSurname', 'KuponName', 'KuponEmail'];
	for(var i = 0; i < from.length; i++){
		f = document.getElementById(from[i]);
		if(f.value.length == 0){
			alert('Необходимо заполнить все поля.');
			return false;
		}
		t = document.getElementById(into[i]);
		t.removeChild(t.firstChild);
		t.appendChild(document.createTextNode(f.value + (f.value.replace(' ', '').length == 0 ? '-' : '')));
	}
	updKuponCounter(parseInt(getKuponCounter())+1);
	document.getElementById('KuponOutTbl').style.display = 'inline';
	return true;
}

function printKupon(){
	updKuponCounter(getKuponCounter(true));
	var pr = document.getElementById('KuponOutTbl').innerHTML; 
	newWin = window.open('','printWindow','Width=500,Height=400,Toolbar=0,Location=0,Directories=0,Status=0,Menubar=1,Scrollbars=0,Resizable=0');
	newWin.document.open();
	newWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n');
	newWin.document.write('<html xmlns="http://www.w3.org/1999/xhtml">\r\n');
	newWin.document.write('<head><title></title>\r\n');
	newWin.document.write('<link href="http://www.bechterev.ru/styles/styles.css" type="text/css" rel="stylesheet"/>\r\n');
    newWin.document.write('<link href="http://www.bechterev.ru/styles/content-user.css" type="text/css" rel="stylesheet"/>\r\n'); 
	newWin.document.write('</head><body class="kuponOutBody">\r\n');
	newWin.document.write(pr);
	newWin.document.write('\r\n</body>\r\n</html>');
	newWin.document.getElementById('KuponOutBtn').style.display = 'none';
	newWin.document.getElementById('kuponDivImg').style.margin = '-120px 0 0 0';
	newWin.document.getElementById('kuponDivTxt').style.margin = '-5px 0 0 200px';
	newWin.document.close(); 
	newWin.print();
	newWin.close();
	return true;
}

function updKuponCounter(cnt){
	if((c = document.getElementById('KuponCounter'))!= null){
		c.removeChild(c.firstChild);
		c.appendChild(document.createTextNode(cnt));
	}
}

function getHttpReq(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function getKuponCounter(upd){
	var 
		xmlhttp = getHttpReq()
		url = '/kupon/get_kupon_counter.php?phrase=KPnCnT';
	if(upd == true)
		url = url + '&c=1';
	xmlhttp.open('GET', url, false);
	xmlhttp.send(null);
	if(xmlhttp.status == 200)
	  return xmlhttp.responseText;
  	return 0;
}


