var xmlHttp
var inform
var nomeKey
var pS

function addName(str)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	if(str!=""){
		if(str.substring(1,0)!=" " && str!="Write the name here..."){
			document.getElementById("txtError1").innerHTML='';
			str = str.replace(/&/g,"E___E");			
			document.getElementById('nameGame').style.visibility = 'hidden';
			document.getElementById('partLoader').style.visibility = 'visible';			
			document.getElementById('partLoader').innerHTML = '<strong>Loading...</strong><br /><img src=\"gif/loader.gif\"><br />'; 
			var url="upfunctions.php"
			url=url+"?N="+str
			xmlHttp.onreadystatechange=stateChanged 
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
		}
		else{document.getElementById("txtError1").innerHTML="Write the name of your game correctly!"}
	}
	else{document.getElementById("txtError1").innerHTML="You have to write the name of the game!"}
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 if(xmlHttp.responseText.charAt(0)!="G"){	
	document.getElementById("partName").innerHTML=xmlHttp.responseText	
	createUploader("Upload an image (GIF, JPG or PNG), it will be resized to 80x80", "IMG");
 }
 else{
	document.getElementById("txtError1").innerHTML=xmlHttp.responseText;
	document.getElementById('nameGame').style.visibility = 'visible';
	document.getElementById('partLoader').style.visibility = 'hidden';
}
 document.getElementById("partLoader").innerHTML='';
 } 
}

function startUpload(){
	document.getElementById('partLoader').style.visibility = 'visible';
	document.getElementById('partLoader').innerHTML = '<strong>Loading...</strong><br /><img src=\"gif/loader.gif\"><br />';    
    document.getElementById('partUp').style.visibility = 'hidden';
	document.getElementById('partInfo').style.visibility = 'hidden';
	document.getElementById('txtErrorUp').innerHTML = "";
	document.getElementById('partImg2').innerHTML = "";
    return true;
}

function showSWF(atr1,atr2,atr3){	
	document.getElementById('partTestSWF').innerHTML ="<div style=\"border: 5px solid #7c812a; background: #373A32; width:"+atr2+"px; height:"+atr3+"px;\"><embed src=\"temp/"+atr1+".swf\" width=\""+atr2+"\" height=\""+atr3+"\"></div><br /><input class='sbtn' onclick=\"document.getElementById('partTestSWF').innerHTML ='';\" type='button' value='Close it'><br /><br />";
}

function stopUpload(success,atr1,atr2,atr3){
	document.getElementById('partLoader').innerHTML = '';
	document.getElementById('partLoader').style.visibility = 'hidden';      
    document.getElementById('partUp').style.visibility = 'visible';      
	document.getElementById('partInfo').style.visibility = 'visible';
    if (success==0){document.getElementById('txtErrorUp').innerHTML = 'There was an error during file upload !!!<br />Please check it and retry...';}
    else if (success==1){document.getElementById('txtErrorUp').innerHTML = 'Image file type not correct! Upload a GIF, JPG or PNG...';}	
	else if (success==2){document.getElementById('txtErrorUp').innerHTML = 'The file is too big! Upload a smaller file...';}
	else if (success==22){document.getElementById('txtErrorUp').innerHTML = 'There was a problem reading the file... try again!';}
	else if (success==3){document.getElementById('txtErrorUp').innerHTML = 'Game file type not correct! Upload a SWF...';}
	else if (success==4){		
		pS="<br /><strong>SWF uploaded successfully!</strong><br /><br />";
		pS=pS+"<input class='sbtn' onclick=\"showSWF('"+atr1+"','"+atr2+"','"+atr3+"');\" type='button' value='Test it'>";
		pS=pS+" <input class='sbtn' onclick=\"document.getElementById('partImg2').innerHTML ='';createUploader('Upload the file SWF of the game', 'SWF');document.getElementById('partInfo').innerHTML ='';document.getElementById('partTestSWF').innerHTML ='';\" type='button' value='Delete SWF'>";
		
		document.getElementById('partImg2').innerHTML = pS;
		document.getElementById("partUp").innerHTML="";
		createInfoForm();
		document.getElementById('partInfo').innerHTML = inform;		
	}
	else if (success==5){document.getElementById('txtErrorInfo').innerHTML = 'Remember to write at least one keyword!';}
	else if (success==52){document.getElementById('txtErrorInfo').innerHTML = 'Remember to write only 5 words or less!';}
	else if (success==53){document.getElementById('txtErrorInfo').innerHTML = 'Do not write the category inside the keywords!';}
	else if (success==54){document.getElementById('txtErrorInfo').innerHTML = 'Do not write keywords like FUN, FLASH, GAME, PLAY Or the name of you game!';}
	else if (success==55){document.getElementById('txtErrorInfo').innerHTML = 'Do not write the name of your game in the keywords!';}
	else if (success==6){document.getElementById('txtErrorInfo').innerHTML = 'Remember to write a short description!';}
	else if (success==7){document.getElementById('txtErrorInfo').innerHTML = 'Remember to write an extended description!';}
	else if (success==8){document.getElementById('txtErrorInfo').innerHTML = 'Remember to write the instructions!';}
	else if (success==88){document.getElementById('txtErrorInfo').innerHTML = 'Unknown error, please verify all the fields...';}
	else if (success==9){
		document.getElementById('partLoader').innerHTML = '';
		document.getElementById('partImg2').innerHTML ='';
		document.getElementById('partInfo').innerHTML = "<br />Thank you! The game has been successfully inserted in our DataBase.<br />A group of moderators will publish it on the front page shortly.<br /><a href='game/"+atr1+"' target='_blank'>Click HERE</a> to vote your game!";
	}
	else if (success==10){document.getElementById('txtErrorInfo').innerHTML = 'Remember to choose a category!';}
	else {			
		document.getElementById('partImg').innerHTML = "<img src='"+success+"?"+(new Date()).getTime()+"' class='bordo'>";
		document.getElementById('partImg2').innerHTML = "<br /><input class='sbtn' onclick=\"document.getElementById('partImg').innerHTML ='';document.getElementById('partImg2').innerHTML ='';createUploader('Upload an image (GIF, JPG or PNG), it will be resized to 80x80', 'IMG')\" type='button' value='Delete IMG'>";
		createUploader('Upload the file SWF of the game', 'SWF');
	}
		 
	document.getElementById('partLoader').innerHTML = "";    
    return true;   
}

function createUploader(labelUp, objUP){
	document.getElementById('partUp').style.visibility = 'visible';
	document.getElementById('partLoader').style.visibility = 'hidden';
	var uploader="";
	uploader="<form action='upfunctions.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >";        
	uploader=uploader+"<p id='txtErrorUp' class='errore'></p><br />";     
	uploader=uploader+labelUp+":<br /><br />";
    uploader=uploader+"<input name='myfile' type='file' size='47' class='inCol' onclick=\"document.getElementById('url"+objUP+"').value='Or write the URL of the "+objUP+" here...'\" ><br /><br />";
	uploader=uploader+"<input onfocus=\"resInputTXT('url"+objUP+"')\" class='inCol' type='text' value='Or write the URL of the "+objUP+" here...' id='url"+objUP+"' size='50' name='Url"+objUP+"'><br />";
    uploader=uploader+"<br /><input type='submit' name='Upload' class='sbtn' value='Upload "+objUP+"' />";                        
    uploader=uploader+"<iframe id='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #000;' /></iframe>";
    uploader=uploader+"</form>";
	document.getElementById("partUp").innerHTML=uploader;		
}

function createInfoForm(){
	inform="<br /><div id='txtErrorInfo' class='errore'></div>";	
	inform=inform+"<form action='upfunctions.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();'>";		
	inform=inform+"<p>Category:<br /><select name='Category' class='inCol' id='category' onchange=\"creaKeywords(this.value)\">";
	inform=inform+"<option>-Choose One-</option><option value='Action'>Action</option><option value='Adventure'>Adventure</option><option value='Arcade'>Arcade</option><option value='Defense'>Defense</option><option value='Dressup'>Dressup</option>";
	inform=inform+"<option value='Driving'>Driving</option><option value='Fighting'>Fighting</option><option value='Multiplayer'>Multiplayer</option><option value='Puzzle'>Puzzle</option><option value='Rhythm'>Rhythm</option><option value='Shooting'>Shooting</option>";
	inform=inform+"<option value='Sport'>Sport</option><option value='War'>War</option><option value='Webcam'>Webcam</option></select><br /></p>";		
	inform=inform+"<p>Keywords, space separated max 5 words:<br /><input type='text' class='inCol' id='keywords' name='Keywords' size='35' maxlength='35'/><br/></p>";
	inform=inform+"<div id='keyTags'></div>";
	inform=inform+"<br /><p>Short Description, 90 chars:<br /><input name='Description' type='text' class='inCol' id='description' size='60' maxlength='90' /><br /></p>";
	inform=inform+"<p>Extended Description:<br /><textarea name='Exdes' cols='53' rows='4' class='inCol' id='exdes' /></textarea><br /></p>";		
	inform=inform+"<p>Instructions:<br /><textarea name='Inst' cols='53' rows='4' class='inCol' id='inst' /></textarea><br /></p>";		
	inform=inform+"<input type='submit' name='Upload' value='Done' class='sbtn' />";	
	inform=inform+"<iframe id='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #000;'></iframe>";	
	inform=inform+"</form>";	
}

function creaKeywords(category){	
	document.getElementById("keyTags").innerHTML='';
	if(category=="Action"){document.getElementById("keyTags").innerHTML=cK("gun")+cK("weapon")+cK("plane")+cK("punch")+cK("kick")+cK("explosion")+cK("kill")+cK("blood");}
	if(category=="Adventure"){document.getElementById("keyTags").innerHTML=cK("story")+cK("retro")+cK("dialog")+cK("history")+cK("find")+cK("talk")+cK("treasure")+cK("escape");}
	if(category=="Arcade"){document.getElementById("keyTags").innerHTML=cK("platform")+cK("retro")+cK("classic")+cK("remake")+cK("mouse")+cK("casino")+cK("8bit")+cK("atari");}
	if(category=="Defense"){document.getElementById("keyTags").innerHTML=cK("tower")+cK("protect")+cK("wave")+cK("strategy")+cK("map")+cK("command")+cK("castle")+cK("space");}
	if(category=="Dressup"){document.getElementById("keyTags").innerHTML=cK("bride")+cK("doll")+cK("babe")+cK("makeup")+cK("cooking")+cK("tuning")+cK("decor")+cK("colouring");}
	if(category=="Driving"){document.getElementById("keyTags").innerHTML=cK("car")+cK("bike")+cK("motorbike")+cK("racing")+cK("speed")+cK("turbo")+cK("fast")+cK("running");}
	if(category=="Fighting"){document.getElementById("keyTags").innerHTML=cK("fighter")+cK("martial")+cK("mortal")+cK("punch")+cK("sword")+cK("kungfu")+cK("kicks")+cK("samurai");}
	if(category=="Multiplayer"){document.getElementById("keyTags").innerHTML=cK("strategy")+cK("rpg")+cK("cards")+cK("chat")+cK("friends")+cK("realtime")+cK("mmo")+cK("points");}
	if(category=="Puzzle"){document.getElementById("keyTags").innerHTML=cK("maze")+cK("quiz")+cK("retro")+cK("logic")+cK("physic")+cK("avoid")+cK("falling")+cK("number");}
	if(category=="Rhythm"){document.getElementById("keyTags").innerHTML=cK("music")+cK("sound")+cK("chord")+cK("guitar")+cK("pop")+cK("noise")+cK("tone")+cK("piano");}
	if(category=="Shooting"){document.getElementById("keyTags").innerHTML=cK("shooter")+cK("gun")+cK("bomb")+cK("weapon")+cK("zombie")+cK("rocket")+cK("tank")+cK("sniper");}
	if(category=="Sport"){document.getElementById("keyTags").innerHTML=cK("olympic")+cK("run")+cK("swim")+cK("tennis")+cK("socccer")+cK("football")+cK("ball")+cK("bike");}
	if(category=="War"){document.getElementById("keyTags").innerHTML=cK("tank")+cK("tower")+cK("kill")+cK("wwi")+cK("aliens")+cK("weapon")+cK("explosion")+cK("destroy");}
	if(category=="Webcam"){document.getElementById("keyTags").innerHTML=cK("head")+cK("hand")+cK("move")+cK("camera")+cK("personal")+cK("dance")+cK("myself")+cK("punch");}
}

function cK(nome){
	nomeKey="<a class='keyClick' onclick=\"checkKeys('"+nome+"');\">"+nome+"</a> ";
	return nomeKey;
}

function checkKeys(nome){
	if(document.getElementById('keywords').value!=""){
		document.getElementById('keywords').value=document.getElementById('keywords').value+' ';
	}
	document.getElementById('keywords').value=document.getElementById('keywords').value+nome;
}

function resInputTXT(inputTXT){
	if(document.getElementById(inputTXT).value=="Write the name here..."){document.getElementById(inputTXT).value="";}
	if(document.getElementById(inputTXT).value=="Or write the URL of the IMG here..."){document.getElementById(inputTXT).value="";}
	if(document.getElementById(inputTXT).value=="Or write the URL of the SWF here..."){document.getElementById(inputTXT).value="";}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}