var i;
var request = false;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = false;
    }
  }
}

if (!request)
  alert("Error initializing XMLHttpRequest!");

function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}
function vidChange()
{
	
	var objSel = document.getElementById('vidpList')
		var para = document.getElementById("resultsPane");
		//document.getElementById('krainaList').selectedIndex = 0;
		while (para.hasChildNodes())
		{
			para.removeChild(para.firstChild);
		}
		i=0;
		if (objSel.options[objSel.selectedIndex].value == "postTrans")
		{
			document.getElementById('chkVruch').disabled=false;
		}
		else
		{
			document.getElementById('chkVruch').disabled=true;
			document.getElementById('chkVruch').checked=false;
		}
		krainaChange();
}

function krainaChange()
{
	var objSel = document.getElementById('krainaList')

		var povSel = document.getElementById("povidList");
		povSel.options.length=1;
		if (document.getElementById('vidpList').value=="postTrans") 
		{

			if (objSel.options.length != 0)
			{
				if (document.getElementById('krainaList').value=="10")
				{
				//addOption(povSel,"Просте повідомлення","prostePovid",false,false);
				addOption(povSel,"Рекомендоване повідомлення","recomPovid",false,false);
				document.getElementById('chkVruch').disabled=true;
				document.getElementById('chkVruch').checked=false;
				
				}
				else
				{
				addOption(povSel,"Просте повідомлення","prostePovid",false,false);
				document.getElementById('chkVruch').disabled=false;
				}
			}
		}
		else
		{
		//addOption(povSel,"Просте повідомлення","prostePovid",false,false);
		addOption(povSel,"Рекомендоване повідомлення","recomPovid",false,false);
		}
		sendData();
}

function costKopChng()
{

		checkField(document.getElementById('summKop'));
		if(document.getElementById('summGrn').value=="0"&&document.getElementById('summKop').value=="0")
		{

		}
		else
		{
		
			sendData();
		}

}

function costGrnChng()
{

	checkField(document.getElementById('summGrn'));	
	sendData();
}

function povidChange()
{
	sendData();
	
}

function vruchCnage()
{
	sendData();
}
function checkField(field)
{
	var re = /^[0-9]*$/;
	if (!re.test(field.value))
	{
		//alert("Невірно введена сума переказу");
		field.value="0";
	}
}


			
function updatePage()
{
    if (request.readyState == 4)
     {
       if (request.status == 200)
       {

	       
		var transSumm = request.responseXML.getElementsByTagName("cost");
		var cur = request.responseXML.getElementsByTagName("cur");

		if (i==0)
		{
			       var name = request.responseXML.getElementsByTagName("country");
			       //var name = request.responseXML.getElementsByTagName("ValCurs");
			       var krainaSel = document.getElementById("krainaList");
			       krainaSel.options.length=0;
			       for (i=0;i<name.length;i++)
			       {
				       var countryName = name[i].getAttribute('name');
				       var tZone = name[i].childNodes[0].nodeValue
				     
				       addOption(krainaSel,countryName,tZone,false,false);
			       }		
				
			i=1;
		}
		else
		{
			
		}
		
		var vidSel = document.getElementById('vidpList');
		var countrySel = document.getElementById('krainaList');
		var povSel = document.getElementById('povidList');
		if ((vidSel.options[vidSel.selectedIndex].value != "nonType")&&(countrySel.options[countrySel.selectedIndex].value != "nonKraina"))
		{
			
		
		var newtext0 = document.createTextNode("За переказ: "+transSumm[0].childNodes[0].nodeValue+" грн.");
		var br0 = document.createElement("br");
		var newtext1 = document.createTextNode("За повідомлення: "+transSumm[1].childNodes[0].nodeValue+" грн.");
		var br1 = document.createElement("br");
	//	var newtext2 = document.createTextNode("Плата за вручення переказу у власні руки: "+transSumm[2].childNodes[0].nodeValue+" грн.");
	//	var br2 = document.createElement("br");
	//	var newtext3 = document.createTextNode("Плата за письмове повідомлення: "+transSumm[3].childNodes[0].nodeValue);
	//	var br3 = document.createElement("br");
		var newtext3 = document.createTextNode("Вартість за послугу: "+transSumm[3].childNodes[0].nodeValue+" грн.");
		var br3 = document.createElement("br");
		var newtext4 = document.createTextNode("в.т.ч. ПДВ: "+transSumm[4].childNodes[0].nodeValue+" грн.");
		var br4 = document.createElement("br");
		var newtext5 = document.createTextNode("Всього: "+transSumm[5].childNodes[0].nodeValue+" грн.");
		var br5 = document.createElement("br");
		var br6 = document.createElement("br");
		var brI = document.createElement("i");
		brI.innerHTML = "Курс долара: "+transSumm[6].childNodes[0].nodeValue;
		var newtext6 = document.createTextNode("Курс долара: "+transSumm[6].childNodes[0].nodeValue);
		var dolc = document.getElementById("curs");
		dolc.innerHTML = "Курс долара: "+transSumm[6].childNodes[0].nodeValue;
		var para = document.getElementById("resultsPane");
		while (para.hasChildNodes())
		{
			para.removeChild(para.firstChild);
		}
		
			para.appendChild(newtext0);	
			para.appendChild(br0);	
			para.appendChild(newtext1);
			para.appendChild(br1);
			if (vidSel.options[vidSel.selectedIndex].value != "elTrans")
			{
                  //para.appendChild(newtext2);
			//para.appendChild(br2);
			}
			para.appendChild(newtext3);
			para.appendChild(br3);
			para.appendChild(newtext4);
			para.appendChild(br4);
			para.appendChild(newtext5);
			para.appendChild(br5);
			para.appendChild(br6);
			//para.appendChild(brI);
			//para.appendChild(newtext6);
		}
		
       }	
       else if (request.status == 404)
       {
         alert("Request URL does not exist");
       }
	 else
	 {
         //alert("Error: status code is " + request.status);
	 }
     }
}


function sendData()
{
		
		var cs;
		var vidSel = document.getElementById('vidpList');
		var countrySel = document.getElementById('krainaList');
		if (countrySel.options.length != 0) cs = countrySel.options[countrySel.selectedIndex].value; else cs =10;
		var kop=document.getElementById('summKop').value;
		var grn=document.getElementById('summGrn').value;
		var povSel = document.getElementById('povidList');
		
		if (kop=="") 
		{
			kop=0;
		}
		if (grn=="") 
		{
			grn=0;
		}		
            var url = SERVLET+"?srvc=MoneyTrans" + "&vid="+vidSel.options[vidSel.selectedIndex].value+"&summGrn="+grn+	"&summKop="+kop+"&country="+cs+"&vruch="+document.getElementById('chkVruch').checked+"&povid="+povSel.options[povSel.selectedIndex].value;
	    request.open("GET",url,true);
		request.onreadystatechange = updatePage;
		request.send(null);
}

