var countCarousel=0;
var countCarousel=0;
var countCarouselMax=0;
var lastObj;

function addGame(id)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=addGame&ajax=true&id='+id,'');
}

function deleteGame(id)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=deleteGame&ajax=true&id='+id,'');
}

function addDeveloper(id)
{
    xmlreqPOST('../../index.php?mod=profile&com=addAsDeveloper&ajax=true&id='+id,'');
}

function addFriend(id)
{
    xmlreqPOST('../../index.php?mod=profile&com=addAsFriend&ajax=true&id='+id,'');
}

function checkAvailability(frmField)
{
    if(checkGameTitle())
    {
        xmlreqPOST('../../index.php?mod=portfolio&com=ajaxCheckUniqueGameTitle&ajax=true',document.getElementById(frmField).name+'='+document.getElementById(frmField).value);
    }
}

function rate(game_id,rating)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=rateGame&ajax=true&game_id='+game_id+'&rating='+rating,'');
}

function rateNLGD(game_id,rating)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=rateGameNLGD&ajax=true&game_id='+game_id+'&rating='+rating,'');
}

function selectPeriod(period)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=gamesPeriod&ajax=true&period='+period,'');
}

function playGame(game_id)
{
    xmlreqPOST('../../index.php?mod=portfolio&com=playGame&ajax=true&game_id='+game_id,'');
}

function extendCarousel()
{
    countCarousel++;
    if(countCarouselMax<countCarousel && countCarousel<=sizeCarousel)
    {
        countCarouselMax=countCarousel;
        xmlreqPOST('../../index.php?mod=portfolio&com=extendCarousel&ajax=true','');
    }
}

function extendGameCarousel(user_id)
{
    countCarousel++;
    if(countCarouselMax<countCarousel && countCarousel<=sizeCarousel)
    {
        countCarouselMax=countCarousel;
        xmlreqPOST('../../index.php?mod=portfolio&com=extendGameCarousel&ajax=true&user_id='+user_id,'');
    }
}

function previousCarousel()
{
    countCarousel--;
}

function constestRegister()
{
    document.forms['frmContestRegister'].submit();
}

function addDataToDiv(data)
{
    document.getElementById('storeGameData').innerHTML=document.getElementById('storeGameData').innerHTML+data;
}

function addDataToCarousel(data)
{
    var container = document.getElementById('carousel-list');
    var new_element = document.createElement('li');
    new_element.innerHTML = data;
    container.insertBefore(new_element, container.lastChild);
}

function showGameDetails(obj,id)
{
    if(lastObj)
    {
        lastObj.style.borderTop='1px solid #FFFFFF';
        lastObj.style.borderLeft='1px solid #FFFFFF';
        lastObj.style.borderBottom='1px solid #FFFFFF';
        lastObj.style.borderRight='1px solid #FFFFFF';    
    }    
    document.getElementById('gameDetails').innerHTML=document.getElementById('gameDetails'+id).innerHTML;
    obj.style.borderTop='1px solid #123456';
    obj.style.borderLeft='1px solid #123456';
    obj.style.borderBottom='1px solid #123456';
    obj.style.borderRight='1px solid #123456';
    lastObj=obj;
}

function hideGameDetails()
{
    //document.getElementById('gameDetails').innerHTML='';
}

function submitUpload()
{
    document.forms['frmUpload'].elements['activate'].value=1;
    document.forms['frmUpload'].submit();
}

function editUpload()
{
    document.forms['frmUpload'].action='manageGame.html';
    document.forms['frmUpload'].elements['edit'].value=1;
    document.forms['frmUpload'].submit();
}

function submitGameInvite(objName)
{
    if(!checkName(objName))return false;
    if(!checkMailFrom(objName))return false;
    if(!checkMailTo(objName))return false;
    if(!checkVerification(objName))return false;
    checkVerificationLive(objName);
}

function checkUpload()
{
    var error = '';
	if(checkGameTitle() != true) {
		error += '- '+checkGameTitle()+'\n';
    }
    if(checkTags() != true) {
    	error += '- '+checkTags()+'\n';
    }
    if(checkCategory() != true) {
    	error += '- '+checkCategory()+'\n';
    }
    if(checkThumbnail() != true) {
    	error += '- '+checkThumbnail()+'\n';	
    }
    if(checkGameFile() != true) {
    	error += '- '+checkGameFile()+'\n';	
    }
    if(checkAcknowledge() != true) {
    	error += '- '+checkAcknowledge()+'\n';	
    }
    if(checkCreator() != true) {
    	error += '- '+checkCreator()+'\n';	
    }
    
    if(error != '') {
		alert('ERROR(S) OCCURRED:\n\n'+error+'\n\nPlease correct this.');
		return false;
	} else {
		toggle();
		document.forms['frmUpload'].submit();
	}
}

function checkGameTitle()
{
    var frmField='gametitle';
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    var str_check = document.forms['frmUpload'].elements[frmField].value;
    if(str_check.length<4||str_check.length>30)
    {
        return str_message;
    }
    else
    {
        var filter = /^[a-z 0-9]+$/i;
        if(filter.test(str_check))
        {
            return true;
        }
        else
        {
            return str_message;
        }
    }
}

function checkTags()
{
    var frmField='tags';
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    var str_check = document.forms['frmUpload'].elements[frmField].value;
    var arrayTags=str_check.split(',');
    if(arrayTags.length<=10)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function checkCategory()
{
    var frmField='category';
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    var int_check = document.forms['frmUpload'].elements[frmField].value;
    if(int_check>0)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function checkGameFile()
{
    var frmField='game_upload';
    if(document.getElementById('preview_'+frmField))return true;
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    var str_check = document.forms['frmUpload'].elements[frmField].value;
    if(str_check.length>0)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function checkCreator()
{
    var frmField='creator';
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    if(document.forms['frmUpload'].elements[frmField].checked==true)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function checkAcknowledge()
{
    var frmField='acknowledge';
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    if(document.forms['frmUpload'].elements[frmField].checked==true)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function checkThumbnail()
{
    var frmField='thumbnail_upload';
    if(document.getElementById('preview_'+frmField))return true;
    var str_message = document.getElementById(frmField+'_alert').innerHTML;
    var str_check = document.forms['frmUpload'].elements[frmField].value;
    if(str_check.length>0)
    {
        return true;
    }
    else
    {
        return str_message;
    }
}

function activateStars(stars)
{
    for(a=1;a<=5;a++)
    {
        if(stars>=a)
        {
            document.getElementById('star'+a).src='../../templates/front/default/images/starOk.gif';
        }
        else
        {
            document.getElementById('star'+a).src='../../templates/front/default/images/starNot.gif';
        }
    }
}

function showComment()
{
    document.getElementById('commentDiv').style.display='inline';
}

function submitComment()
{
    document.getElementById('commentDiv').style.display='none';
    game_id = document.getElementById('game_id').value;
    var commentTmp = document.getElementById('commentField').value
    commentTmp = encodeURIComponent(commentTmp);
    xmlreqPOST('../../index.php?mod=portfolio&com=addComment&ajax=true','game_id='+game_id+'&comment='+commentTmp);
    document.getElementById('commentField').value='';
}

function showFolder(objName)
{
    document.getElementById('showFolder').innerHTML=document.getElementById(objName).innerHTML;
}

function showFolderRoot()
{
    document.getElementById('showFolder').innerHTML=document.getElementById('root').innerHTML;
}


function submitEnter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   checkUpload();
   return false;
   }
else
   return true;
}

function textCounter(field,cntfield,maxlimit)
{
    if (field.length > maxlimit)
    {
        field = field.substring(0, maxlimit);
    }
    else
    {
        document.getElementById(cntfield).innerHTML=maxlimit - field.length;
    }
}

