
function get_http(){
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new
				ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function GetValue(name){
	return document.getElementById(name).value;
}

function FillCombo(name,values){
	Elements=values.split("||");
	document.getElementById(name).options.length = 1;
	for (var i=0;i<Elements.length;i++){
		valueLabelPair = Elements[i].split("|")
		document.getElementById(name).options[i+1] = new Option(valueLabelPair[0], valueLabelPair[1]);
	}
	document.getElementById(name).style.display = 'block';
}

function Preloader(visible){
	document.getElementById('Preloader').style.display = visible?'block':'none';
}

function ajax(url,fn){
	if(!this.http){
		this.http = get_http();
		working = false;
	}

	if (!working && this.http){
		var http = this.http;
		this.http.open("GET", url, true);
		this.http.onreadystatechange = function(){
			if (http.readyState == 4) {
				fn(http.responseText);
				working = false;
				Preloader(false);
			}
		}
		working = true;
		Preloader(preloader_show);
		this.http.send(null);
	}
}

function getcountries(){
	preloader_show=true;
	document.getElementById('CountrySelect').style.display='none';
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('AmountSelect').style.display='none';
	ajax("/ajax.html?lang="+lang+"&uid="+uid+"&random="+Math.ceil(Math.random()*100000),function(text){
		if(text==''){
			ajaxerror('getcountries');
		}else{
			FillCombo('CountrySelect',text);
		}
	});
}

function getoperators(){
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('AmountSelect').style.display='none';
	if(GetValue('CountrySelect')!=''){
		ajax("/ajax.html?lang="+lang+"&uid="+uid+"&country="+GetValue('CountrySelect')+"&random="+Math.ceil(Math.random()*100000),function(text){
			if(text==''){
				ajaxerror('getoperators');
			}else{
				FillCombo('OperatorSelect',text);
			}
		});
	}
}

function getamount(){
	document.getElementById('AmountSelect').style.display='none';
	if(GetValue('CountrySelect')!=''){
		if(amountbase==0){
			ajax("/ajax.html?lang="+lang+"&uid="+uid+"&country="+GetValue('CountrySelect')+"&operator="+GetValue('OperatorSelect')+"&random="+Math.ceil(Math.random()*100000),function(text){
				if(text==''){
					ajaxerror('getamount');
				}else{
					FillCombo('AmountSelect',text);
				}
			});
		}else{
			amount=amountbase;
			gettext();
		}
	}
}

function gettext(){
	document.getElementById('CountrySelect').style.display='none';
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('AmountSelect').style.display='none';
	document.getElementById('SmsReceived').style.display='none';
	document.getElementById('TimeIsOver').style.display='none';
	if(amount==0){amount=GetValue('AmountSelect');}
	ajax("/ajax.html?lang="+lang+"&uid="+uid+"&country="+GetValue('CountrySelect')+"&operator="+GetValue('OperatorSelect')+"&amount="+amount+"&random="+Math.ceil(Math.random()*100000),function(text){
		if(text==''){
			ajaxerror('gettext');
		}else{
			Elements=text.split("||");
			document.getElementById('SmsNum').innerHTML=Elements[0];
			document.getElementById('SmsNum2').innerHTML=Elements[0];
			document.getElementById('SmsText').innerHTML="<b>"+Elements[1]+"</b> "+sendtext;
			document.getElementById('SmsCost').innerHTML=Elements[2];
			smsid=Elements[3];
			document.getElementById('SmsInfo').style.display='block';
			document.getElementById('CabinetSelectOnce').style.display='block';
			time=600;
			smsgot=false;
			timer();
		}
	});
}

function timer(){
	time--;

	if((time%20==0) && (document.getElementById('SmsInfo').style.display == 'block'))
		checksms();

	if(time==0){
		if(document.getElementById('SmsInfo').style.display=='block'){
			document.getElementById('TimeIsOver').style.display='block';
			document.getElementById('SmsInfo').style.display='none';
			document.getElementById('CabinetSelectOnce').style.display='none';
		}
	}else{
		minutes=Math.floor(time/60);
		seconds=time%60;
		document.getElementById('SmsMinutes').innerHTML=minutes;
		document.getElementById('SmsSeconds').innerHTML=seconds;
	}

	if((time>0) && (!smsgot)) setTimeout("timer()",1000);
}

function setlang(){
	var l=GetValue('LanguageSelect');
	if(l!='' && l!=lang){
		url='http://pay.smsdeluxe.ru/'+uid+'/?lang='+l;
		if(showtext!=''){url+='&showtext='+showtext;}
		if(deftext!=''){url+='&text='+escape(sendtext);}
		document.location=url;
	}
}

function checksms(){
	preloader_show=false;
	ajax("/ajax.html?smsid="+smsid+"&random="+Math.round(Math.random()*1000),function(text){
		if(text=='0'){
			smsreceived();
		}
	});
	preloader_show=true;
}

function smsreceived(){
	smsgot=true;
	document.getElementById('SmsInfo').style.display='none';
	document.getElementById('CabinetSelectOnce').style.display='none';
	document.getElementById('SmsReceived').style.display='block';
}

function selectagain(){
	amount=0;
	smsgot=true;
	document.getElementById('CountrySelect').selectedIndex=0;
	document.getElementById('CountrySelect').style.display='block';
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('AmountSelect').style.display='none';
	document.getElementById('SmsInfo').style.display='none';
	document.getElementById('CabinetSelectOnce').style.display='none';
	document.getElementById('SmsReceived').style.display='none';
	document.getElementById('TimeIsOver').style.display='none';
}

function ajaxreload(){
	if(last_op=='getcountries') getcountries();
	if(last_op=='getamount') getamount();
	if(last_op=='gettext') gettext();
	if(last_op=='getoperators') getoperators();
	document.getElementById('AjaxError').style.display='none';
}

function ajaxerror(fn){
	document.getElementById('AjaxError').style.display='block';
	last_op=fn;
}

window.onload = function(){
	if(document.getElementById('CountrySelect')!=null) getcountries();
}