function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

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){

	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';
	ajax(url_api+"ajax.php?round="+round+"&uid="+uid+"&price="+amount+"&random="+Math.ceil(Math.random()*100000),function(text){
		if(text==''){
			ajaxerror('getcountries');
		}else{
			FillCombo('CountrySelect',text);
		}
		Preloader(false);
	});
}

function getoperators(){
	document.getElementById('OperatorSelect').style.display='none';
	if(GetValue('CountrySelect')!=''){
		ajax(url_api+"ajax.php?round="+round+"&uid="+uid+"&price="+amount+"&country="+GetValue('CountrySelect')+"&random="+Math.ceil(Math.random()*100000),function(text){
			if(text==''){
				ajaxerror('getoperators');
			}else{
				FillCombo('OperatorSelect',text);
			}
			Preloader(false);
		});
	}
}

function gettext(){
	document.getElementById('CountrySelect').style.display='none';
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('SmsReceived').style.display='none';
	document.getElementById('TimeIsOver').style.display='none';
	ajax(url_api+"ajax.php?uid="+uid+"&country="+GetValue('CountrySelect')+"&operator="+GetValue('OperatorSelect')+"&price="+amount+"&round="+round+"&random="+Math.ceil(Math.random()*100000),function(text){
		if(text==''){
			ajaxerror('gettext');
		}else{
			Elements=text.split("||");
			document.getElementById('SmsNum').innerHTML=Elements[0];
			if(sendtext=='(ваш текст)'){
				document.getElementById('SmsText').innerHTML="<b>"+Elements[1]+"</b>"+sendtext;
			}else{
				document.getElementById('SmsText').innerHTML="<b>"+Elements[1]+""+sendtext+"</b>";
			}
			Ex=Elements[2].split("|");
			document.getElementById('SmsCost').innerHTML=trim(Ex[0]);
			if(Ex[1]!="" && Ex[1]!=" "){
				document.getElementById('SmsComment').innerHTML="&#0149;&nbsp;"+trim(Ex[1]);
				document.getElementById('SmsComment').style.display='block';
			}
			smsid=Elements[3];
			document.getElementById('SmsInfo').style.display='block';
			time=600;
			smsgot=false;
			timer();
		}
		Preloader(false);
	});
}

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';
		}
	}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 checksms(){
	return "";
	preloader_show=false;
	ajax(url_api+"ajax.php?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('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('SmsInfo').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=='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;
}

function check_access(txt){
	document.getElementById('CountrySelect').style.display='none';
	document.getElementById('OperatorSelect').style.display='none';
	document.getElementById('SmsReceived').style.display='none';
	document.getElementById('TimeIsOver').style.display='none';
	document.getElementById('AlertError').style.display='block';
	document.getElementById('AlertError').innerHTML=txt;
}

function show_panel_smsdeluxe(){
	if(document.getElementById('ShowErr')!=null)
	document.getElementById('ShowErr').style.display='none';
	document.getElementById('GetPass').style.display='none';
	document.getElementById('SmsSend').style.display='block';
	return false;
}

window.onload = function(){
	preloader_show=true;
	if(document.getElementById('TextOplata').innerHTML=="") document.getElementById('TextOplata').style.display='none'; else document.getElementById('TextOplata').style.display='block';
	ajax(url_api+"ajax.php?url=check&round="+round+"&uid="+uid+"&price="+amount+"&version_script="+version_script+"&random="+Math.ceil(Math.random()*100000),function(text){
		if(text.indexOf('ok')+1){
			if(document.getElementById('CountrySelect')!=null) getcountries();
		}else{
			if(text=="") text="Невозможно соедениться с сервером. Проверьте настройки в файле lib/pay.php";
			check_access(text);
			Preloader(false);
		}
	});
}