function showSMS() {
    var div = 'sms_text';
    //generateSMS(div);
    showObj(div);
}

function generateSMS(div, program) {
    // индикатор за залог
    var betIndicator = '#7';
    // разделител между секциите
    var valueSeparator = ':';
    // разделител между срещите
    var matchSeparator = ',';
    // разделител между системите
    var systemSeparator = ',';
    // символ с който се отбелязват твърдите знаци
    var hardSymbol = '*';
    
    var sms = '';
    sms = sms + betIndicator;
    sms = sms + valueSeparator;
    sms = sms + 'username';
    sms = sms + valueSeparator;
    sms = sms + 'password';
    sms = sms + valueSeparator;
    sms = sms + program;
    sms = sms + valueSeparator;
    
    $(div).innerHTML = sms;
}

function userLogin(program, lang) {
    hideObj('bet_slip_extra_panel');
    sendRequest($('frm_user_login'), 'bet_slip', 'revolve('+program+', \''+lang+'\');'); // bet_slip_extra_panel
    
    if($('not_full_system') != null)
        showObj('not_full_system');
}

/* AJAX */

function $(div) {
    return document.getElementById(div);
}

function showObj(div) {
    $(div).style.visibility = 'visible';
    $(div).style.display = 'block';
}

function hideObj(div) {
    $(div).style.visibility = 'hidden';
    $(div).style.display = 'none';
}

function parentShowObj(div) {
    parent.document.getElementById(div).style.visibility = 'visible';
    parent.document.getElementById(div).style.display = 'block';
}

function parentHideObj(div) {
    parent.document.getElementById(div).style.visibility = 'hidden';
    parent.document.getElementById(div).style.display = 'none';
}

function insert_code(script, div, ok_func, err_func){
	if(div != null)
		showObj('loading');
        
    script = script+'&'+new Date().getTime();
	
	var oXmlHttp = zXmlHttp.createRequest();
	var syncronity = false;
	oXmlHttp.open("get", script, syncronity);
	
	if(syncronity) {
		oXmlHttp.onreadystatechange = function () {
			if(oXmlHttp.readyState == 4){
				if(oXmlHttp.status == 200){
					var result = oXmlHttp.responseText;
					if(div != null && div != '') {
						var divId = document.getElementById(div);
						divId.innerHTML = result;
					}
					
					if(ok_func != '')
						eval(ok_func);
					
					if(div != null)
						hideObj('loading');
				}
			}
		}
		
		oXmlHttp.send(null);
	} else {
		oXmlHttp.send(null);
		
		var result = oXmlHttp.responseText;
		if(div != null && div != '') {
			var divId = parent.document.getElementById(div);
			divId.innerHTML = result;
		}
		
		if(ok_func != '')
			eval(ok_func);
			
		if(div != null)
			hideObj('loading');
	}
}

function parent_insert_code(script, div, ok_func, err_func){
	if(div != null)
		parentShowObj('loading');
        
    script = script+'&'+new Date().getTime();
	
	var oXmlHttp = zXmlHttp.createRequest();
	var syncronity = false;
	oXmlHttp.open("get", script, syncronity);
	
	if(syncronity) {
		oXmlHttp.onreadystatechange = function () {
			if(oXmlHttp.readyState == 4){
				if(oXmlHttp.status == 200){
					var result = oXmlHttp.responseText;
					if(div != null && div != '') {
						var divId = parent.document.getElementById(div);
						divId.innerHTML = result;
					}
					
					if(ok_func != '')
						eval(ok_func);
						
					if(div != null)
						parentHideObj('loading');
				}
			}
		}
		
		oXmlHttp.send(null);
	} else {
		oXmlHttp.send(null);
		
		var result = oXmlHttp.responseText;
		if(div != null && div != '') {
			var divId = parent.document.getElementById(div);
			divId.innerHTML = result;
		}
		
		if(ok_func != '')
			eval(ok_func);
			
		if(div != null)
			parentHideObj('loading');
	}
}

function getRequestBody(oForm){
    var aParams = new Array();

    for(var i = 0; i < oForm.elements.length; i++){
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
    }
    return aParams.join("&");
}

function sendRequest(oForm, div, ok_func){
    var sBody = getRequestBody(oForm);

    var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open('POST', oForm.action, true);
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4){
            if(oXmlHttp.status == 200){
                if(div != null && div != '') {
                    var divId = $(div);
                    divId.innerHTML = oXmlHttp.responseText;
                }

                if(typeof(ok_func) != 'undefined')
                    eval(ok_func);
            }
        }
    }
    
    oXmlHttp.send(sBody);
}

function selectNotFullCombination(fullSystemCount, notFullSystemId, program, lang, ajax_timestamp) {
    for(i = 0; i < fullSystemCount; i++)
        $('system_'+i).checked = false;
        
    if(!notFullSystemId.length)
        notFullSystemId = '_';
		
    //$('bet_slip').innerHTML = "Зарежда се, моля изчакайте...";
	showObj('loading');
	
    insert_code('bet_slip/_sender.php?system_posible_profit='+notFullSystemId+'&program='+program+'&lang='+lang+'&'+ajax_timestamp, 'bet_slip');
}

function selectFullCombination(fullSystemCount, program, lang) {
    var str = '';
    for(i = 0; i < fullSystemCount; i++)
        if($('system_'+i).checked) {
            if(str.length)
                str = str + ",";
            str = str + $('system_'+i).value;
        }
    if($('not_full_system') != null)    
        $('not_full_system').selectedIndex = 0;
    
    if(!str.length)
        str = '_';
    
	//$('bet_slip').innerHTML = "Зарежда се, моля изчакайте...";
	showObj('loading');
	
    insert_code('bet_slip/_sender.php?system_posible_profit='+str+'&program='+program+'&lang='+lang, 'bet_slip');
}

function revolve(program, lang) {
    if(parseInt($('revolution_alive').value)) {
        if($('talon_num') != null)
            if($('talon_num').value) return;
        
        var uniqueGuid = $('unique_identifier').value;
        var smsSendDate = $('sms_send_date').value;
        var userPlayerId = $('user_player_id').value;
		
        $('bet_slip_extra_panel').innerHTML = '';
		
        insert_code('bet_slip/_sender.php?revolution='+uniqueGuid+'&smsSendDate='+smsSendDate+'&user_player_id='+userPlayerId+'&program='+program+'&lang='+lang, 'revolution');
        
        setTimeout('revolve('+program+', \''+lang+'\');', 1000);
    }
}

function confirmSmsWithPass(uniqueIdentifier, userPlayerId, program, lang, alertMsg) {
    if($('confirmation_password').value != '' && $('confirmation_username').value != '') {
        hideObj('bet_slip_extra_panel');
        insert_code('bet_slip/_sender.php?confirmation='+uniqueIdentifier+'&user_player_id='+userPlayerId+'&program='+program+'&lang='+lang+'&confirmation_username='+$('confirmation_username').value+'&confirmation_password='+$('confirmation_password').value, 'bet_slip', 'revolve('+program+', \''+lang+'\');');
    } else
        alert(alertMsg);
}


function sendSMS(domain, bet_slip_id, bet_slip_program, bet_slip_lang) {
    if($('not_full_system') != null)
        hideObj('not_full_system');
    insert_code(domain+'/bet_slip/_sender.php?bet_slip='+bet_slip_id+'&program='+bet_slip_program+'&lang='+bet_slip_lang, 'bet_slip');
}

function cancelConfirmation(program, lang) {
    hideObj('bet_slip_extra_panel');
    
    insert_code('bet_slip/_sender.php?cancel_confirmation=true&program='+program+'&lang='+lang);
    reloadBetSlip();
}

function closeExtraPanel() {
    if($('not_full_system') != null)
        showObj('not_full_system');
        
    hideObj('bet_slip_extra_panel');
}

// betCoef(2, '<?php echo strtolower($lang_current); ?>', '<?php echo $sreshta_id[$s]; ?>', 'F', '1')
function betCoef(program, lang, event_id, game_type, game_outcome) {
	parent_insert_code('bet_slip/_sender.php?match=import&program='+program+'&lang='+lang+'&event_id='+event_id+'&game_type='+game_type+'&game_outcome='+game_outcome, 'bet_slip', 'showErrorBar();');
}

function showErrorBar() {
	if($('show_error_garantie') != null) {
		$('show_error_garantie').focus();
		window.scrollBy(0, -500);
	}
}

// презарежда Bet Slip-a
function reloadBetSlip() {
    if($('loading') != null) {
        var loading_lang = $('loading').lang.split("|");
        insert_code(loading_lang[2]+'/bet_slip/_sender.php?refresh=show&lang='+loading_lang[0]+'&program='+loading_lang[1], 'bet_slip');
    }
}
