var xmlReqS=false;
var commentsCaptchaRequested = false;

function Form_MakeQueryString( form_name )
{
	var d=document, f, qry="", sep="";

	if( d && (f=d.forms[form_name]) )
	{
		for(var i=0;i<f.length;i++)
		{
			if( f[i].name!="" && f[i].name!="__VIEWSTATE" )
			{
				if( f[i].type=="hidden" || f[i].type=="text" || f[i].type=="textarea" )
				{
					qry += sep + escape(f[i].name) +"="+ escape(f[i].value);	sep = "&";
				}

				if( f[i].type=="select" || f[i].type=="select-one" )
				{
					if( f[i].selectedIndex >=0 && f[i].selectedIndex < f[i].options.length )
					{
						qry += sep + escape(f[i].name) +"="+ escape(f[i].options[f[i].selectedIndex].value);		sep = "&";
					}
				}

				if( f[i].type=="checkbox" && f[i].checked )
				{
					qry += sep + escape(f[i].name) +"="+ escape(f[i].value);	sep = "&";
				}

				if( f[i].type=="radio" && f[i].checked )
				{
					qry += sep + escape(f[i].name) +"="+ escape(f[i].value);	sep = "&";
				}
			}
		}
	}
	return qry;
}
function xmlRequestS () {
	try {
		xmlReqS = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			xmlReqS = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			xmlReqS = false;
		}
	}
	if (!xmlReqS && 'undefined' != typeof(XMLHttpRequest)) {
		try {
			xmlReqS = new XMLHttpRequest();
		}
		catch (e) {
			xmlReqS=false;
		}
	}
	if (!xmlReqS && window.createRequest) {
		try {
			xmlReqS = window.createRequest();
		} catch (e) {
			xmlReqS=false;
		}
	}
}
function xmlAsyncRequestText (file, params, id) {
//prompt('Ajax request:', file +', '+ params +', '+ id)
	if (file) {
		if ( file.indexOf("?") >= 0 ){
			var url = file +'&'+ params;
		}
		else {
			var url = file +'?'+ params;
		}
		xmlRequestS ();
		xmlReqS.open("GET", url, true);
		xmlReqS.send(null);
		xmlReqS.onreadystatechange = function() {
			if (4 == xmlReqS.readyState) {
				if (404 != xmlReqS.status) {
					document.getElementById(id).innerHTML = xmlReqS.responseText;
				}
				else {
					alert(
						'Source: ajax.js: \n\n' + 
						'File: '+ file +' not found!'
					);
				}
			}
		}
	}
	else {
		alert(
			'Source: ajax.js: \n\n' + 
			'Malformed request: no URL!'
		);
	}
}


function pollVote(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest'
		+'&rnd='+Math.random()
		, Form_MakeQueryString('fp'+poll_id)
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}
function pollShowResults(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest&output=results'
		+'&rnd='+Math.random()
		, ''
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}


function loadFLV (name, src, preview, width, height, autostart) {
	loadFile (name, src, preview, width, height, autostart);
}

function loadMP3 (name, src, preview, width, height, autostart) {
	loadFile (name, src, preview, width, height, autostart);
}

function loadFile(name, src, preview, width, height, autoplay) {
		var volumetype = '';
        var _width = width;
        var _height = height;
        var _name = name;
		var fo = new SWFObject("/cms/thirdparty/flvplayer_3.swf", 'single', _width, _height, "7");
        fo.addParam("quality", "high");
        fo.addParam("wmode", "transparent");
        fo.addParam("menu", "false");
		fo.addParam("allowfullscreen","true");

        fo.addVariable("file",src);
        if(preview != "" && preview != null) {
                fo.addVariable("image", preview);
        }
        if(autoplay == "true") {
                fo.addVariable("autostart","true");
        } else {
                fo.addVariable("autostart","false");
		}
        if(volumetype != "" && volumetype != null) {
                fo.addVariable("volumetype", volumetype);
        }
		fo.addVariable("starttxt","Click aici...");
		fo.addVariable("buffer","Se incarca...");
		fo.addVariable("backcolor","0xD90000");
		fo.addVariable("frontcolor","0xFFA4A4");
		fo.addVariable("lightcolor","0xFFFFFF");
		fo.addVariable("logo","/flv-logo.png");
		fo.addVariable("contextmenulink","#");
		fo.addVariable("contextmenuitem","Copyrighted by ProSport");
 		fo.write(_name);
		
}

function pronostic(box_id, item_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+item_id+'&service_type=Pronostic'
		+'&rnd='+Math.random()
		, Form_MakeQueryString('fp'+item_id)
		, 'dinamicContent'+box_id+'_'+item_id);
	return false;
}
function pronosticResults(box_id, item_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+item_id+'&service_type=Pronostic&output=results'
		+'&rnd='+Math.random()
		, ''
		, 'dinamicContent'+box_id+'_'+item_id);
	return false;
}

function enableCaptcha(id, state) {
        if (state && !commentsCaptchaRequested && !getCookie('user_name') && document.getElementById('message'+ id).value.length > 0 && 'comentariu' != document.getElementById('message'+ id).value) {
				document.getElementById("imgCaptcha"+ id).src = "/cms/images/captcha.jpg.php?" + Math.random();
                document.getElementById('divCaptcha'+ id).style.display = 'block';
                commentsCaptchaRequested = true;
        }
}
function submitComments (e) {
	if (e.anonymous_author_name.value.search('^[ \t\r\n]*$')>-1) {
		alert('Nu ati completat campul Nume !'); 
		return false;
	}
	if (e.message.value.search('^[ \t\r\n]*$')>-1 || 'comentariu' == e.message.value) {
		alert('Nu ati completat campul Comentariu !'); 
		return false;
	}

	e.submit();
}

jQuery(function( $ )
{
	$('.ctxJucatori').each( function( i ) 
	{
		for ( jucator in jucatori )
		{
			var regex = new RegExp('( |^|>)' + jucator + '(?=$|<|.|,| )(?!(.*\<\/a\>))', "g");
			this.innerHTML = this.innerHTML.replace(regex,' <a class="ctxHrefJucator" href="' + jucatori[jucator] + '">' + jucator + '</a>');
		}
	});
});

function getCookie(c_name) 
{
	    //alert(document.cookie +"\n"+ 'Length: '+ document.cookie.length);
	    if (document.cookie.length>0) 
	    {
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1) 
		{ 
		    c_start=c_start + c_name.length+1 ;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) 
		    {
			c_end=document.cookie.length;
		    }
		    return unescape(document.cookie.substring(c_start,c_end));
		} 
		return "";
	    }
	    return "";
}

function loginf()
{
	//if (window.name == '' || window.name == 'logout') window.name = 'login';
	//else if (window.name == 'login') return false;
	var v;
	$.ajax({
			type: "POST",
			url: "/login",
			data: "username=" + $('#user').val() + "&password=" + $('#pass').val() + "&action=" + $('#act').val(),
			async: false,
			success: function(msg){
				if (msg.indexOf('error') > 0) {
					document.getElementById('login_error').style.display = '';
					v = false;
				} 
				else {
					//window.location.href = 'http://fiiselectioner.echipamea.ro/user/account';
					v = true;
				}
			}
			});
	return v;
}

function logoutf()
{
    //if (window.name == 'login') window.name = 'logout';
    //else if (window.name == 'logout') return false;
    var v;
    $.ajax({
			type: "GET",
			url: "/login",
			data: "action=1&login_cmd=logout",
			async: false,
			success: function(msg){
				if (msg.indexOf('error') > 0) {
					v = false;
				} 
				else {
					//window.location.href = document.referrer;
					v = true;
				}
			}
			});
    return v;
}

function loginHash(hash)
{
	//if (window.name == '' || window.name == 'logout') window.name = 'login';
	//else if (window.name == 'login') return false;
	var v;
	$.ajax({
			type: "POST",
			url: "/login",
			data: "hash=" + hash + "&action=1",
			async: false,
			success: function(msg){
				if (msg.indexOf('error') > 0) {
					v = false;
				} 
				else {				
					window.location.reload();
					v = true;
				}
			}
			});
	return v;
}

function login()
{
		if (getCookie('apropo_session') == '')
		{
		    //if (getCookie('user_id') > 0) window.location.href = '/login?login_cmd=logout';
		    if (getCookie('user_id') > 0) logoutf();
		}   
		else
		{
		    if (getCookie('user_id') <= 0) loginHash(getCookie('apropo_session'));
		}     

		if (getCookie('user_id') > 0)
		{    
		    document.getElementById('divLogin').innerHTML = '<div id="login_logout">' +
				    '<form action="/login" method=post id="logout_form">'+
				    '<div class="login_right">'+
				    '<div class="login_left">'+
				    '<input type="hidden" name="action" value="logout" />'+
				    '<input type="hidden" name="login_cmd" value="logout" />'+
				    '<span>'+getCookie('email') + '</span>' +
				    '<input type="submit" value="logout" class="submit" />' +
				    '</div>'+
				    '</div>'+
				    '<ul><li><a href="http://fiiselectioner.echipamea.ro/user/account">contul meu</a></li></ul>' +
				    '</form>'+
				    '</div>';
		    
		    
		}
		else
		{    
		    document.getElementById('divLogin').innerHTML = '<form id="login" method="post" onsubmit="return loginf();">'+
				    '<div class="login_right">'+
					'<div class="login_left">'+
					    '<input type="hidden" name="action" value="1" id="act"/>'+
					    '<input type="text" name="username" id="user" value="email"  onClick="if (this.value==\'email\') this.value=\'\';"/>'+
					    '<input type="password" name="password" id="pass" value="********" onClick="if (this.value==\'********\') this.value=\'\';" />'+
					    '<input type="submit" value="login" class="submit" />'+
					'</div>'+
				    '</div>'+
				    '<ul><li><a href="http://fiiselectioner.echipamea.ro/user/register">creeaza cont</a></li><li><a href="http://fiiselectioner.echipamea.ro/user/recover_password">am uitat parola</a></li></ul>'+
				    '<p class="login_err" id="login_error" style="display: none;"><span>user sau parola incorecta</span><a href="javascript:;" onclick="$(\'#login_error\').hide();">x</a></p>'
				'</form>';
		}
}

function pronosticuri_home(box_id) 
{
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=0&type=tabelhome&service_type=Pronostic'
		+'&rnd='+Math.random()
		, ''
		, 'dinamicContent'+box_id);
	return false;
}

function getGameCode()
{
	xmlAsyncRequestText('/euro2008/main.php?nm=Euro2008', '', 'dinamicContent142');
}

function getBtnTrimite(login, register)
{
    var id = getCookie('id');
    var email = getCookie('email');
    var username = getCookie('user_name');
    if (username != '') {document.getElementById('mesaj_username').value = username; document.getElementById('mesaj_username').readonly = true;}
    else document.getElementById('mesaj_username').value = 'nume*';
    if (email != '') {document.getElementById('mesaj_email').value = email; document.getElementById('mesaj_email').readonly = true;}
    else document.getElementById('mesaj_email').value = 'email*';
    if (id != '') document.getElementById('btnRight').innerHTML = '<div class="btn"><div class="btn_right"><a href="javascript:;" onclick="send_news.submit();">trimite</a></div></div><p>Mesajul tau poate fi publicat pe prima pagina in <strong>PROsport</strong> si <strong>Gazeta Sporturilor</strong>.</p>';
    else
    {
	document.getElementById('btnRight').innerHTML = '<p>Mesajul tau poate fi publicat pe prima pagina in <strong>PROsport</strong> si <strong>Gazeta Sporturilor</strong>.</p>';
	document.getElementById('message_Login').innerHTML = 'Ca sa trimiti un mesaj trebuie sa fii <a href="' + login + '">logat</a>, daca nu ai cont <a href="' + register + '">inregistreaza-te</a>.';
    }
}