var client_form_obligs = new Array(0,1);

var invalid_symbols = new Array("*","/",".","%","#","!","¡","?","¿","'","$","&","\\","+",'"',"<",">","á","é","í","ó","ú","à","è","ì","ò","ù","ä","ë","ï","ö","ü","Á","É","Í","Ó","Ú","À","È","Ì","Ò","Ù","Ä","Ë","Ï","Ö","Ü");

//alerts de formularios en ambos idiomas
var client_form_msg = new Array("Deben rellenarse\nambos campos.","You must fill\nin both fields.");

var newdir_form_msgs = new Array();
newdir_form_msgs[0] = new Array("Campo no rellenado.","Empty field.");
newdir_form_msgs[1] = new Array("Símbolo no seguro en\nnombre de directorio: ","Invalid symbol in\ndirectory name: ");

var area_file_form_msgs = new Array();
area_file_form_msgs[0] = new Array("No se ha seleccionado archivo.","No file selected.");
area_file_form_msgs[1] = new Array("Ruta de archivo inválida.","Invalid file path.");
area_file_form_msgs[2] = new Array("Símbolo no seguro en\nnombre de archivo: ","Invalid symbol in\nfile name: ");

//strings para contadores de frames y horas
var counter_strs = new Array();
counter_strs['hours'] = new Array(" horas de rendering"," rendering hours");
counter_strs['frames'] = new Array(" fotogramas montados"," edited frames");

function client_form(lang){
	
	var empty_fields=0;
	
	for(i=0;i<client_form_obligs.length;i++){
		with(document.forms[1].elements[client_form_obligs[i]]){
			if(value == ""){
				empty_fields++;
			}
		}
	}
	
	if(empty_fields == 0){document.forms[1].submit();}
	else{alert(client_form_msg[lang]);}
}

function newdir_form(idioma){
	
	with(document.forms[0].elements[0]){
		
		if(value == ""){
			alert(newdir_form_msgs[0][idioma]);
			return false;
		}
		
		for(i=0;i<invalid_symbols.length;i++){
			if(value.indexOf(invalid_symbols[i]) != -1){
				alert(newdir_form_msgs[1][idioma]+invalid_symbols[i]);
				return false;
			}
		}
	}
	
	return true;
}

function area_file_form(form_num,lang){

	var field_value;
	
	field_value = document.forms[form_num][0].value;

	if(field_value == ""){alert(area_file_form_msgs[0][lang]);return false;}

	if(navigator.userAgent.indexOf('Mac') != -1){
		var last_slash = field_value.lastIndexOf("/");
	}
	else{
		var last_slash = field_value.lastIndexOf("\\");
	}
	
	var last_dot = field_value.lastIndexOf(".");
	
	if(last_slash == -1 || last_dot == -1){
		alert(area_file_form_msgs[1][lang]);
		return false;
	}
	
	file_name = field_value.substring(last_slash+1,last_dot);

	for(i=0;i<invalid_symbols.length;i++){
		if(file_name.indexOf(invalid_symbols[i]) != -1){
			alert(area_file_form_msgs[2][lang]+invalid_symbols[i]);
			return false;
		}
	}
	
	return true;
}

function confirm_action(msg){
	var confirmation = confirm(msg);
	if(confirmation == true){return true;}
	else{return false;}
}


//FUNCIONES PARA CONTADOR DE FRAMES MONTADOS Y CONTADOR DE HORAS DE RENDERING

function reverse_str(str) { 

	var reversed = ""; 

	for(i=str.length-1;i>=0; i--){
		reversed += str.charAt(i)
	} 

	return reversed;
} 

function dotted_format(int_num){
	
	var convert_to_str = 'int_num = "'+int_num+'";';
	eval(convert_to_str);
	
	var total_chars = int_num.length;
		
	if(total_chars < 4){return int_num;}

	var dot_counter = 0;
	var formatted_num = "";
	
	for(i=total_chars-1;i>=0;i--){
		
		formatted_num += int_num.charAt(i);

		if(dot_counter == 2 && i > 0){
			formatted_num += ".";
			dot_counter = 0;
		}
		else{dot_counter++;}
	}
	
	return reverse_str(formatted_num);
}

function get_total_frames(lang){

	var initial_frames = 300000;//total de frames del que se parte

	var init_date = new Date(2006,6,17,2,30,0);//año,mes (con índice 0),dia,minutos,segundos
	var unix_init_date = Date.parse(init_date);

	var today = new Date();
	var today_unix_date = Date.parse(today);

	var seconds = (today_unix_date-unix_init_date)/1000;

	var frames = seconds*2;
	
	var total_frames = initial_frames + frames;

	document.forms[0].frames.value = "/ "+dotted_format(total_frames)+counter_strs['frames'][lang];
	
	setTimeout("get_total_frames("+lang+")",500);
}

function get_total_renderhours(lang){
	
	initial_hours = 5000;//total de horas del que se parte
	
	var init_date = new Date(2006,6,17,2,30,0);//año,mes (con índice 0),dia,minutos,segundos
	var unix_init_date = Date.parse(init_date);
	
	var today = new Date();
	var today_unix_date = Date.parse(today);
	
	var seconds = (today_unix_date-unix_init_date)/1000;
	
	var hours_passed = Math.ceil((seconds/60)/60);
	
	var hours_to_add = Math.ceil(hours_passed*0.2);
	
	var total_hours = initial_hours + hours_to_add;
	
	document.forms[0].hours.value = "/ "+dotted_format(total_hours)+counter_strs['hours'][lang];
	
	setTimeout("get_total_renderhours("+lang+")",500);
}

function start_counters(lang){
	get_total_frames(lang);
	get_total_renderhours(lang);
}