dynamicContent_ajaxObjects = new Array();
var jsCache = new Array();
var enableCache = true;
var webroot="http://www.mysubtitles.com/";

function ajax_loadContent(divId,pathToFile,fileFormId,tmpFunction,loadImage)
{	
	
	//verify if the cache is enabled end the content we want is in the cache
	//if the content is in the cache then we display it
	
	if(enableCache && jsCache[pathToFile]){
		document.getElementById(divId).innerHTML = jsCache[pathToFile];		
  		return; 
	}
	
	//we create a new ajax object 
	
	//we get the index of the new object
	var ajaxIndex = dynamicContent_ajaxObjects.length;
	
	
	
	//we create the ajax object
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	
	//we tell to the ajax object our request		
	
	if (fileFormId !=""){			
		dynamicContent_ajaxObjects[ajaxIndex]=getFormData(fileFormId,pathToFile,dynamicContent_ajaxObjects[ajaxIndex]);			
		dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;	
		dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;				
	}
	else{
		dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;	
	}
			
	
	
	//we tell to the ajax object what to do when he complet the request
	 dynamicContent_ajaxObjects[ajaxIndex].onCompletion =
	 function(){ eval(tmpFunction) };
	 
	 //mesajul temporar din divul nostru
	if (loadImage!=2){
		document.getElementById(divId).innerHTML = '<img src="'+webroot+'/pics/ajax-loader.gif" alt="" title="">&nbsp;&nbsp;Loading content...'+
		'<br /><br />'+
         '<div style="text-align:center;font-size:26px;font-weight:solid;" >Help Us</div>'+
         '</div>';
	
	}
	 //we run ajax
	 dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
}

function ajax_showContent(divId,ajaxIndex,pathToFile){
	//this function populate our div

	//we populare our div
	document.getElementById(divId).innerHTML =
  	dynamicContent_ajaxObjects[ajaxIndex].response;
  	
  	//we put the content in cache
  	if(enableCache){
 	 jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	setFormInputs();
	
	//we delete the curent ajax object
	dynamicContent_ajaxObjects[ajaxIndex] = false;  	  
}

function ajax_showAllFragments(divId,ajaxIndex,pathToFile){
	document.getElementById(divId).innerHTML =
  	dynamicContent_ajaxObjects[ajaxIndex].response;
  	
  	//we put the content in cache
  	if(enableCache){
 	 jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	
	
	//we delete the curent ajax object
	 
	document.getElementById('translation').style.height='';
	dynamicContent_ajaxObjects[ajaxIndex] = false; 
	setSaveTranslationTime();	
	if (document.getElementById('lista_filmelor_de_tradus')){
		document.getElementById('lista_filmelor_de_tradus').onclick=function(){			
			ajax_loadContent('translation',this.href+'?rand='+Math.random(),'','ajax_showMovieToTranslateList(divId,ajaxIndex,pathToFile);');
			return false;
		}
		
	}
	
}

function getFormData(formId,pathToFile,ajaxObject){
	
	var tmpForm=document.getElementById(formId);		
	tmp_lenght=tmpForm.elements.length;
	for (i=0;i<tmpForm.elements.length ;i++){
//		if (sw==0){
//			tmpUrl="?"+tmpForm.elements[i].name+"="+tmpForm.elements[i].value;
//			sw++;
//		}
//		else{
//			tmpUrl=tmpUrl+"&"+tmpForm.elements[i].name+"="+tmpForm.elements[i].value;	
//		}
		
		
		if (tmpForm.elements[i].type=='radio'  ){
			if (tmpForm.elements[i].checked){
				ajaxObject.setVar(tmpForm.elements[i].name,tmpForm.elements[i].value);
				
			}
		}
		else{
			ajaxObject.setVar(tmpForm.elements[i].name,tmpForm.elements[i].value);
		}
	}	
	return ajaxObject;
}
function ajax_sendReplayMessage(divId,ajaxIndex,pathToFile){	
	raspuns=dynamicContent_ajaxObjects[ajaxIndex].response;
  	raspuns=parseServerResponse(raspuns);
  	if (raspuns[0]  > 0){
  		document.getElementById(divId).innerHTML="<span style='color:red'><b>"+raspuns[1]+"</b></span><br />"+document.getElementById(divId).innerHTML;
  	}
  	else{
  		document.getElementById(divId).innerHTML="<br /><b>"+raspuns[1]+"<b><br /><br />";
  	}	
  	dynamicContent_ajaxObjects[ajaxIndex] = false;    	
}

function parseServerResponse(tmpRaspuns){	
	raspuns=Array
	raspuns=tmpRaspuns.split(';');	
	return  raspuns;
}

function setFormInputs(){	
	document.getElementById('translate_button').onclick =
		function (){
				ajax_loadContent('translation',webroot+'/translations.php?rand='+Math.random(),'translate','ajax_showContent(divId,ajaxIndex,pathToFile);');
				if (document.getElementById('translation').style.height){
				document.getElementById('translation').style.height='270px';
			}			
		}
	document.getElementById('next_button').onclick =
		function (){
			ajax_loadContent('translation',webroot+'/translations.php?rand='+Math.random(),'translate_next_fragment','ajax_showContent(divId,ajaxIndex,pathToFile);');
			if (document.getElementById('translation').style.height){
				document.getElementById('translation').style.height='270px';							
			}			
		}
	document.getElementById('translate_text').onfocus= function (){
			if(!document.getElementById('error')){
						this.innerHTML='';					
			}		
	}
	document.getElementById('translate_text').onkeyup=function(){enableTranslateButton(this);}
	document.getElementById('linkTranslateAll').onclick=
		function(){						
			ajax_loadContent('translation',document.getElementById('linkTranslateAll').href+'&rand='+Math.random(),'','ajax_showAllFragments(divId,ajaxIndex,pathToFile);');			
			if (document.getElementById('translation').style.height){
				document.getElementById('translation').style.height='1000px';
			}
			return false;
		}
	
	if (document.getElementById('more_fragments')){
		document.getElementById('more_fragments').onclick=
			function(){	
				ajax_loadContent('translation',webroot+'/translations.php?more=1&rand='+Math.random(),'translation_more_fragments','ajax_showContent(divId,ajaxIndex,pathToFile);');
				if (document.getElementById('translation').style.height){
					document.getElementById('translation').style.height='550px';
				}			
				return false;
				
			}
	}
	
	if (document.getElementById('lista_filmelor_de_tradus')){
		document.getElementById('lista_filmelor_de_tradus').onclick=function(){			
			ajax_loadContent('translation',this.href+'?rand='+Math.random(),'','ajax_showMovieToTranslateList(divId,ajaxIndex,pathToFile);');
			return false;
		}
		
	}	
}




function enableTranslateButton(tmpTextarea){
	if (tmpTextarea.value.length > 0 &&  tmpTextarea.value != " "){
				document.getElementById('translate_button').disabled=false;
			}
	else{
			document.getElementById('translate_button').disabled=true;
	}		
}

	
function ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile){
	//this function populate our div

	//we populare our div
	document.getElementById(divId).innerHTML =
  	dynamicContent_ajaxObjects[ajaxIndex].response;
  	
  	//we put the content in cache
  	if(enableCache){
 	 jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	
	ajax_validateTranslationSetForm();
	//we delete the curent ajax object
	dynamicContent_ajaxObjects[ajaxIndex] = false;  
	document.getElementById('validate_translation').style.height='';
	//setValidateRefreshTime();	  
	if (document.getElementById('lista_filmelor_de_tradus2')){		
		document.getElementById('lista_filmelor_de_tradus2').onclick=function(){			
			ajax_loadContent('validate_translation',this.href+'&rand='+Math.random(),'','ajax_showMovieToTranslateList(divId,ajaxIndex,pathToFile);');
			return false;
		}
		
	}
}


function ajax_validateAllTranslationsShowContent(divId,ajaxIndex,pathToFile){
	//we populare our div
	document.getElementById(divId).innerHTML =
  	dynamicContent_ajaxObjects[ajaxIndex].response;
  	
  	//we put the content in cache
  	if(enableCache){
 	 jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	
	ajax_validateTranslationSetForm();
	//we delete the curent ajax object
	dynamicContent_ajaxObjects[ajaxIndex] = false;  
	document.getElementById('validate_translation').style.height='';
	//setValidateRefreshTime();	  
	setSaveValidationTime();
	
	if (document.getElementById('lista_filmelor_de_tradus2')){		
		document.getElementById('lista_filmelor_de_tradus2').onclick=function(){			
			ajax_loadContent('validate_translation',this.href+'&rand='+Math.random(),'','ajax_showMovieToTranslateList(divId,ajaxIndex,pathToFile);');
			return false;
		}
		
	}
}

function ajax_validateTranslationSetForm(){
	
	if (document.getElementById('validate_button')){
	document.getElementById('validate_button').onclick = 
			function (){
				ajax_loadContent('validate_translation',webroot+'/validate_translations.php?validate=1&rand='+ Math.random(),"validate","ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);");
				document.getElementById('validate_translation').style.height='235px';
			}
	}
	
	if (document.getElementById('invalidate_button')){
	document.getElementById('invalidate_button').onclick = 
			function (){
				ajax_loadContent('validate_translation',webroot+'validate_translations.php?validate=0&rand='+ Math.random(),"validate","ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);");
				document.getElementById('validate_translation').style.height='235px';
			}
	}
	
	if(document.getElementById('v_next_button')){
	document.getElementById('v_next_button').onclick = 
			function (){
				ajax_loadContent('validate_translation',webroot+'/validate_translations.php?rand='+ Math.random(),"validate","ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);");
				document.getElementById('validate_translation').style.height='235px';
			}
	}
	
	if (document.getElementById('translation').style.Height){
		document.getElementById('translation').style.height='190px;';		
	}

	if (document.getElementById('more_validations')){		
		document.getElementById('more_validations').onclick=				
				function (){
					ajax_loadContent('validate_translation',webroot+'/validate_translations.php?more=1&rand='+ Math.random(),"validate","ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);");
					if (document.getElementById('validate_translation').style.height){
						document.getElementById('validate_translation').style.height='420px';
					}			
					return false;				
				}
	}
	
	if (document.getElementById('linkValidateAll')){
		document.getElementById('linkValidateAll').onclick=
		function (){
			ajax_loadContent('validate_translation',document.getElementById('linkValidateAll').href+'&rand='+Math.random(),"validate","ajax_validateAllTranslationsShowContent(divId,ajaxIndex,pathToFile);");			
			document.getElementById('validate_translation').style.height='1500px';
			return false;
		}
	}

}

function ajax_setLinks(arrowLink){				
	ajax_loadContent('translation',arrowLink+'&rand='+Math.random(),'','ajax_showAllFragments(divId,ajaxIndex,pathToFile);');	
	document.location.hash='top';
	if (document.getElementById('translation').style.height){
		document.getElementById('translation').style.height='1000px';
	}
	
}

function ajax_setValidateLinks(arrowLink){	
	ajax_loadContent('validate_translation',arrowLink.href+'&rand='+Math.random(),"","ajax_validateAllTranslationsShowContent(divId,ajaxIndex,pathToFile);");		
	document.location.hash='validate_top';
	if (document.getElementById('validate_translation').style.height){
		document.getElementById('validate_translation').style.height='1600px';
	}
	return false;
}
function translateAll(){
	document.location.hash='top';
	if (document.getElementById('translation').style.height){
		document.getElementById('translation').style.height='1000px';
	}
	
	ajax_loadContent('translation',webroot+'/translate-entire-subtitle.php?rand='+Math.random(),'formTranslateAll','ajax_showAllFragments(divId,ajaxIndex,pathToFile);');		
}

function validateAll(){
	document.location.hash='validate_top';
//	if (document.getElementById('validate_translation').style.height){
//		document.getElementById('validate_translation').style.height='1000px';
//	}		
	ajax_loadContent('validate_translation',webroot+'validate-entire-subtitle.php?rand='+Math.random(),"formValidateAll","ajax_validateAllTranslationsShowContent(divId,ajaxIndex,pathToFile);");
	
	
}

function setTranslateRefreshTime(){
	var t=setTimeout("alert('5 setTranslateRefreshTime!')",10000);
}

function setValidateRefreshTime(){
	
//	
//	if (refreshTime){
//		clearTimeout(refreshTime);		
//	}
//	minutes=3;
//	seconds=minutes*60;			
//	//seconds=10;
//	timeInSeconds=seconds*1000;	
//	refreshTime=setTimeout("ajax_loadContent('validate_translation','http://www.mysubtitles.ro/validate_translations.php?rand='+Math.random(),\"\",\"ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);\");",timeInSeconds);
}
//

function setSaveTranslationTime(){
	if (saveTranslationsRefreshTime){
		clearTimeout(saveTranslationsRefreshTime);
	}
	
	minutes=2;	
	seconds=minutes*60;		
	miliseconds=seconds*1000;
	saveTranslationsRefreshTime=setTimeout("saveTranslations();",miliseconds);
}

function saveTranslations(){	
	ajax_loadContent('translation',webroot+'/translate-entire-subtitle.php?rand='+Math.random(),'formTranslateAll','setSaveTranslationTime();',2);
}

function setSaveValidationTime(){
	if (saveValidationsRefreshTime){
		clearTimeout(saveValidationsRefreshTime);
	}
	minutes=2;
	seconds=minutes*60;		
	miliseconds=seconds*1000;
	saveValidationsRefreshTime=setTimeout("saveValidations();",miliseconds);	
}

function saveValidations(){
	ajax_loadContent('validate_translation',webroot+'validate-entire-subtitle.php?rand='+Math.random(),"formValidateAll","setSaveValidationTime();",2);
}

function ajax_showMovieToTranslateList(divId,ajaxIndex,pathToFile){
	document.getElementById(divId).style.height='';
	document.getElementById(divId).innerHTML =
  	dynamicContent_ajaxObjects[ajaxIndex].response;
  	
  	//we put the content in cache
  	if(enableCache){
 	 jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
}

function choseSubtitle(userLink,subtitleId,valid){	
	if (!valid){
		ajax_loadContent('translation',webroot+'translations.php?chose='+subtitleId+'&rand='+Math.random(),"","ajax_showContent(divId,ajaxIndex,pathToFile);");
	}
	else{
		ajax_loadContent('validate_translation',webroot+'validate_translations.php?chose='+subtitleId+'&rand='+Math.random(),"","ajax_validateTranslationShowContent(divId,ajaxIndex,pathToFile);");
	}
	
	
	return false;
}
function loadimgTransparent(fileSize) {
	var img = new Image();
	var srv = new Object();
	var ajaxObj = new Array();

	ajaxObj[0] = new sack();
	ajaxObj[0].requestFile = '/runBwServerGetRandom.php';
	ajaxObj[0].method = 'GET';
	ajaxObj[0].onCompletion = function() {
	 if ( (res = ajaxObj[0].response) !== '') {
		re = /{'id':'([0-9]+)','path':'(.+)'}/;
		matches = re.exec(res);
		srv.id = matches[1];
		srv.path = matches[2];

		img.onload = function() {
			end = (new Date()).getTime();
			diff = end - start;

			//alert('loaded. timediff: ' + (end - start) + ' ms');
			connectSpeed = (Math.floor((((fileSize * 8) / (diff / 1000)) / 1024) * 10) / 10);//kbs

			//set display timeout to at least 10s
			to = ( (diff  >= 10000) ? 0 : (10000-diff)) ;

			//update db
			ajaxObj[1] = new sack();
			ajaxObj[1].requestFile = '/runBwClientDbUpdate.php';
			ajaxObj[1].method = 'GET';
			ajaxObj[1].setVar('id', srv.id);
			ajaxObj[1].setVar('speed', connectSpeed);

			ajaxObj[1].onCompletion = function() {
				//alert(ajaxObj[1].response);
			}
			ajaxObj[1].runAJAX();
		}

		start = (new Date()).getTime();
		img.src = srv.path + fileSize + '.gif?t=' + escape(start);
	 }
	}
	ajaxObj[0].runAJAX();
}
