/////////////////////////////////////////////////////////////////////////////
//  Augmenter taille image
/////////////////////////////////////////////////////////////////////////////
function Upl_MaxImg(theImg)
	{
	if ($('DivMaxScr').style.display=="block")
		{
		$('DivMaxScr').style.display="none";
		return;
		}
	$('ImgMaxScr').src=theImg.src;
	$('DivMaxScr').style.display="block";
	}
/////////////////////////////////////////////////////////////////////////////
//  Changer le type d'envoi
/////////////////////////////////////////////////////////////////////////////
function Upld_ToggleFromUrl()	
	{
	theForm = document.FrmUpload;
	if (theForm.FromUrl.checked==true)
		{
		theForm.UpldLocFile.value="";
		theForm.UpldLocFile.disabled=true;
		theForm.UpldUrlFile.disabled=false;
		}
	else	
		{
		theForm.UpldUrlFile.value="";
		theForm.UpldUrlFile.disabled=true;
		theForm.UpldLocFile.disabled=false;
		}		
	return;
	}
/////////////////////////////////////////////////////////////////////////////
//  Controler type de fichier
/////////////////////////////////////////////////////////////////////////////
function Upl_ChkFileType(theField)
	{
	theForm = document.FrmUpload;
	theForm.BtnCheck.disabled=true;
	theForm.BtnSubmit.disabled=true;			
	$('FirstCheck').style.display="none";	
	theForm.FileTyp.value=0;	
	if (theField.value=="")
		return;
	szFile = trim(theField.value.toLowerCase());
	arParts = szFile.split(".");
	NumParts =  arParts.length;
	UplTyp=0;
	if (NumParts >=2)
		{
		Comp = arParts[NumParts-2];
		if (Comp == "wad")
			{
			Comp = arParts[NumParts-1];
			if (Comp == "zip" || Comp == "rar")
				UplTyp=3;			
			}
		else	
			{
			Comp = arParts[NumParts-1];
			if (Comp == "zip" || Comp == "rar")
				UplTyp=1;
			if (Comp == "jpg")
				UplTyp=2;
			}
		}		
	switch (UplTyp)
		{
		case 1: //Archive 
			$('Detect').innerHTML = _('upld_archive_file');
			break;
		case 2: //Screen
			$('Detect').innerHTML = _('upld_screen_file');
			break;
		case 3: //Wad
			$('Detect').innerHTML = _('upld_archive_wad');
			break;
		default: //None recognized
			$('FirstCheck').style.display="none";
			$('Detect').innerHTML = _('incorrect_filetype');
			return true;
		}
	theForm.FileTyp.value=UplTyp;
	theForm.BtnCheck.disabled=false;
	return true;
	}
/////////////////////////////////////////////////////////////////////////////
//  Vérifier si Fichier existant
/////////////////////////////////////////////////////////////////////////////
function Upl_Chk_Existing_File()
	{
	theForm = document.FrmUpload;
	UplTyp = theForm.FileTyp.value;
	if (!UplTyp)
		{
		alert(_('incorrect_filetype'));
		return;
		}
	FromUrl = ((theForm.FromUrl.checked == true) ? 1 : 0)
	sFileName = FromUrl ? trim(theForm.UpldUrlFile.value) : trim(theForm.UpldLocFile.value);
	if (sFileName =='')
		{
		alert(_('must_select_file'));	
		return;		
		}
	theForm.BtnSubmit.disabled=true;		
	theForm.BtnReset.disabled=true;		
	$('SearchWait').style.display="block";
	$('SearchResult').style.display="none";
	$('FirstCheck').style.display="block";

	// Hack #1 for special chars in MapName
	sFileName=encodeURIComponent(sFileName);
	// Hack #2 for % char in UrlRewriting
	sFileName=sFileName.replace(/\%/g,"||17b||");
	szUrl=GlblUrl+"Push/ChkFile/index.html";
	if (xmlHttp = GetxmlHttp())
		{
		var params = "";
		params 	+=	"FromUrl="+FromUrl;
		params 	+=	"&FileName="+sFileName;
		xmlHttp.open("POST", szUrl,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = function() {
							if (xmlHttp.readyState==4 && xmlHttp.status == 200)
								{																							
								sResult=xmlHttp.responseText;
								Upl_Handle_Check_Existing_File(sResult);
								}
							return;
										};
		xmlHttp.send(params);
		}	
	return;
	}
////////////////////////////////////////////////////////////////////////////
//  Handle Check Existing File Response
/////////////////////////////////////////////////////////////////////////////								
function Upl_Handle_Check_Existing_File(sResult)
	{
	theForm = document.FrmUpload;
	theForm.BtnReset.disabled=false;	
	MapsDatas = sResult.split('|');
	CanSend=parseInt(MapsDatas[0]);
	HtmlRes=MapsDatas[1];
	theForm.BtnSubmit.disabled= (CanSend == 1) ? false : true;
	$('SearchResult').innerHTML=HtmlRes;
	$('SearchWait').style.display="none";
	$('SearchResult').style.display="block";
	return;
	}	
/////////////////////////////////////////////////////////////////////////////
//  Reset du Formulaire
/////////////////////////////////////////////////////////////////////////////
function Upl_Reset_Form()
	{
	$('FirstCheck').style.display="none";
	$('SearchWait').style.display="block";
	$('SearchResult').style.display="none";	
	$('MainDiv').style.display="block";
	$('UploadDiv').style.display="none";
	$('TestsDiv').style.display="none";
	theForm = document.FrmUpload;
	theForm.UpldLocFile.value="";	
	theForm.UpldUrlFile.value="";	
	theForm.BtnCheck.disabled=true;
	theForm.BtnSubmit.disabled=true;		
	theForm.BtnReset.disabled=true;
	if (typeof(theForm.FileGam)!='undefined')
		{
		theForm.FileGam.value=0;			
		theForm.FileMod.value=0;			
		theForm.FileCat.value=0;
		}
	$('Detect').innerHTML = _('none_selected');
	return;
	}
/////////////////////////////////////////////////////////////////////////////
//  Changement de Jeu pour un Wad
/////////////////////////////////////////////////////////////////////////////
function Upl_ChangeWadGame(theWadList)
	{
	theForm = document.FrmUpload;

	WadGam = parseInt(theWadList.options[theWadList.selectedIndex].value);
	theForm.BtnSubmit.disabled = WadGam ? false : true;
	theForm.FileMId.value = 0;
	theForm.FileGam.value = WadGam;
	theForm.FileMod.value = 0;
	theForm.FileCat.value = 0;
	}	
/////////////////////////////////////////////////////////////////////////////
//  Uploads Temporairement désactivés
/////////////////////////////////////////////////////////////////////////////
function Upl_NoUpld()
	{
	alert(_('no_uploads'));
	return false;
	}
/////////////////////////////////////////////////////////////////////////////
//  Validation avant envoi formulaire Upload
/////////////////////////////////////////////////////////////////////////////
var nUplStrt 	= 0;
var nSttcBar	= -1;
function Upl_CheckSendFile()
	{
	if (!CheckMbr())
		return false;										
	theForm = document.FrmUpload;
	if (!parseInt(theForm.FileTyp.value))
		{
		alert(_('incorrect_filetype'));
		return false;
		}

	if (theForm.UpldLocFile.value=="" && theForm.UpldUrlFile.value=="")
		{
		alert(_('must_select_file'));
		return false;
		}
	Upl_Change_PageIcon("Ico-Loading.gif",_('upload_in_progress'));
	$('UploadDiv').style.display="block";	
	$('MainDiv').style.display="none";
	nUplStrt = 1;
	FromUrl = ((theForm.FromUrl.checked == true) ? 1 : 0)
	if (FromUrl)
		Upl_Update_ProgBar();
	else
		setTimeout("Upl_Update_ProgBar();",10);
	return true;
	}
/////////////////////////////////////////////////////////////////////////////
//  Update infos Progress Bar
/////////////////////////////////////////////////////////////////////////////
function Upl_Update_ProgBar()
	{	
	// Si la ProgressBar est statique on ne fait rien
	if (nSttcBar==1)
		return;
	// Récupérer les infos d'upload
	theForm = document.FrmUpload;
	FromUrl = ((theForm.FromUrl.checked == true) ? 1 : 0)
	UnikId = theForm.UPLOAD_IDENTIFIER.value;
	szUrl="Push/ProgBar/index.html";
	if (xmlHttp = GetxmlHttp())
		{
		var params = "";
		params 	+=	"FromUrl="+FromUrl;
		params 	+=	"&UnikId="+UnikId;
		xmlHttp.open("POST", szUrl,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = function() {
							if (xmlHttp.readyState==4 && xmlHttp.status == 200)
								{																							
								sResult=xmlHttp.responseText;
								Upl_Handle_InfosProgress_Bar(sResult);
								return;
								}
										};
		xmlHttp.send(params);
		}
	return;
	}
/////////////////////////////////////////////////////////////////////////////
//  Traiter infos Progress Bar
/////////////////////////////////////////////////////////////////////////////
function Upl_Handle_InfosProgress_Bar(sResult)
	{
	UplDatas 	= sResult.split('|');
	StcBar	=parseInt(UplDatas[0]);
	UplDone	=parseInt(UplDatas[1]);
	UplBytes	=parseInt(UplDatas[2]);
	UplTotal	=parseInt(UplDatas[3]);	
	TimeLeft	=parseInt(UplDatas[4]);		
	// Au premier passage, déterminer si la barre doit être statique ou pas
	if (nUplStrt && nSttcBar == -1)
		{
		nSttcBar = StcBar;
		$('StaticLoadingBar').style.display = (nSttcBar == 1 ? "block" : "none");
		$('ProgressBarMain').style.display = (nSttcBar == 0 ? "block" : "none");	
		$('TmpLoadingBar').style.display = "none";
		if (!nSttcBar)
			Upl_Init_Progress_Bar();
		}
		
	if (nUplStrt && UplTotal)
		{
		if (!UplDone)
			{
			if (!nSttcBar && UplBytes && UplTotal)
				Upl_Show_Progress_Bar(UplBytes, UplTotal);
			Upl_Update_ProgBar();
			}
		}
	}
/////////////////////////////////////////////////////////////////////////////
//  Initialiser Progress Bar
/////////////////////////////////////////////////////////////////////////////
function Upl_Init_Progress_Bar()
	{
	MainWidth = parseInt($('ProgressBarMain').style.width);	
	MainHeight = parseInt($('ProgressBarMain').style.height);
	$('ProgressBarTxt1').style.width = MainWidth+"px";
	$('ProgressBarTxt1').style.height = MainHeight+"px";
	$('ProgressBarTxt2').style.width = MainWidth+"px";		
	$('ProgressBarTxt2').style.height = MainHeight+"px";
	}
/////////////////////////////////////////////////////////////////////////////
//  Mettre à Jour Progress Bar
/////////////////////////////////////////////////////////////////////////////	
function Upl_Show_Progress_Bar(val,valMax)
	{
	var max = valMax ? valMax : 100;
	var width = Math.round((val/max)*100);
	$('ProgressBarFond').style.width=width+"%";
	$('ProgressBarTxt1').innerHTML = width+"%";
	$('ProgressBarTxt2').innerHTML = width+"%";		
	}
/////////////////////////////////////////////////////////////////////////////
//  Upload Effectué
/////////////////////////////////////////////////////////////////////////////
function Upl_Done(sResult)
	{
	UplResult	= sResult.split('|');
	ErrorNum 	= parseInt(UplResult[0]);
	FUnikId	= UplResult[1];
	ErrorStr 	= UplResult[2];
	if (nSttcBar)
		{	
		$('StaticLoadingBar').style.display = "none";
		Upl_Init_Progress_Bar();
		$('ProgressBarMain').style.display = "block";
		}
	if (!ErrorNum || !nSttcBar)
		{
		Upl_Show_Progress_Bar(100, 100);
		}

	$('UplNotice').innerHTML = ErrorStr;
	nUplStrt=0;
	nnSttcBar=-1;
	Upl_Restore_PageIcon();
	if (!ErrorNum)
		setTimeout("Upl_Start_Tests('"+FUnikId+"');",1000);
	else
		$('BtnReturn').style.display="block";
	return;
     }	
/////////////////////////////////////////////////////////////////////////////
//  Change Page Icon
/////////////////////////////////////////////////////////////////////////////
function Upl_Change_PageIcon(iconName,sTitle)
	{
	var oHead = document.getElementsByTagName("head")[0];
	document.title = sTitle;
	var sUA=browser();
	if (sUA != "FF")
		return;
	var NewLink 	= document.createElement("link");
	NewLink.type 	= "image/gif";
	NewLink.rel 	= "icon";
	NewLink.href 	= "Images/Favicons/"+iconName;
	var HeadLinks 	= oHead.getElementsByTagName("link");
	for (var i=0; i < HeadLinks.length; i++) 
		{
		var OldLink = HeadLinks[i];
		if (OldLink.type=="image/gif" && OldLink.rel=="icon") 
			{
			oHead.removeChild(OldLink);
			break;
			}
		}
	oHead.appendChild(NewLink);
	}
/////////////////////////////////////////////////////////////////////////////
//  Restore Page Icon
/////////////////////////////////////////////////////////////////////////////
function Upl_Restore_PageIcon()
	{
	Upl_Change_PageIcon("Ico-17b.gif","17's Buddies");
	}
////////////////////////////////////////////////////////////////////////////
//  Change Submit Btn Change on Selected Map
/////////////////////////////////////////////////////////////////////////////								
function Upl_ChangeBtnState(theMap)
	{
	theForm = document.FrmUpload;
	theForm.BtnSubmit.disabled=false;
	MapId=theMap.value;	
	theForm.FileMId.value = MapId;
	theForm.FileGam.value = parseInt($('Gam_'+MapId).innerHTML);
	theForm.FileMod.value = parseInt($('Mod_'+MapId).innerHTML);
	theForm.FileCat.value = parseInt($('Cat_'+MapId).innerHTML);
	}
////////////////////////////////////////////////////////////////////////////
//  Test de Validation du fichier
/////////////////////////////////////////////////////////////////////////////								
function Upl_Start_Tests(FUnikId)
	{
	ShowMask();
	ShowLoading();
	$('UplNotice').innerHTML = _('val_in_progress');
	szUrl=GlblUrl+"Push/ValUpl/"+FUnikId+".html";
	if (xmlHttp = GetxmlHttp())
		{
		xmlHttp.open("GET", szUrl);
		xmlHttp.onreadystatechange = function() {
						if (xmlHttp.readyState==4)
							{
							HideLoading();
							HideMask();
							sResult=xmlHttp.responseText;
							Upl_Handle_ValidateFile(sResult);
							}
										};
		xmlHttp.send(null);
		}	
	}
/////////////////////////////////////////////////////////////////////////////
//  Traiter infos Progress Bar
/////////////////////////////////////////////////////////////////////////////
function Upl_Handle_ValidateFile(sResult)
	{
	ValDatas 	= sResult.split('|');
	ValError	= parseInt(ValDatas[0]);
	ValDbl	= parseInt(ValDatas[1]);	
	ValErrTxt	= ValDatas[2];

	if (ValError)
		{
		$('val_no_error').style.display = "none";
		$('val_no_error_dbl').style.display="none";					
		$('val_error').style.display = "block";
		$('upld_error_txt').innerHTML = ValErrTxt;
		}
	else
		{
		$('val_error').style.display="none";
		if (ValDbl)
			{
			$('val_no_error_dbl').style.display="block";			
			$('val_no_error').style.display="none";
			}
		else
			{
			$('val_no_error_dbl').style.display="none";			
			$('val_no_error').style.display="block";
			}
		}
	$('UploadDiv').style.display="none";	
	$('TestsDiv').style.display="block";
	}
/////////////////////////////////////////////////////////////////////////////
//  Envoyer screenshot
/////////////////////////////////////////////////////////////////////////////
function Upl_Scr_GoScr(theForm)
	{
	if (!CheckMbr())
		return false;
	theForm.BtnSubmit.disabled=true;
	ShowMask();
	ShowLoading();	
	return true;
	}
/////////////////////////////////////////////////////////////////////////////
//  Controler type de screenshot
/////////////////////////////////////////////////////////////////////////////
function Upl_Chk_Screenshot()
	{
	theForm = document.FrmUploadScr;
	theForm.BtnSubmit.disabled=true;			
	theField = theForm.UpldLocFile;
	if (theField.value=="")
		{
		alert(_('must_select_file'));
		return false;
		}
	szFile = trim(theField.value.toLowerCase());
	arParts = szFile.split(".");
	NumParts =  arParts.length;
	if (NumParts ==2)
		{
		if (arParts[NumParts-1] == "jpg" || arParts[NumParts-1] == "jpeg" || arParts[NumParts-1] == "bmp")
			{
			theForm.BtnSubmit.disabled=false;
			return true;
			}
		}		
	alert(_('incorrect_filetype'));
	return false;
	}	
