var upload_range = 1;
var total_upload_size;
var get_status_speed;
var get_status_url;
var get_data_loop = true;
var seconds = 0;
var minutes = 0;
var hours = 0;
var info_width = 0;
var info_bytes = 0;
var info_time_width = 500;
var info_time_bytes = 15;
var cedric_hold = true;

function checkFileNameFormat(){
	if(check_file_name_format == false){ return false; }

	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			var string = document.form_upload.elements['upfile_' + i].value;
			var num_of_last_slash = string.lastIndexOf("\\");

			if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

			var file_name = string.slice(num_of_last_slash + 1, string.length);
			var re = /^[\w][\w\.\-]{1,32}$/i;

			if(!re.test(file_name)){
  				alert("Lo sentimos, subir archivos en este formato no está permitido. Por favor, asegurarse de que sus nombres de los archivos sigue este formato. \n\n1.Todo el archivo no puede superar los 32 caracteres \n2. Se permiten los caracteres 1-9, a-z, A-Z, '_', '-'\n");
  				return true;
  			}
  		}
  	}
	return false;
}

function checkDisallowFileExtensions(){
	if(check_disallow_extensions == false){ return false; }

  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			if(document.form_upload.elements['upfile_' + i].value.match(disallow_extensions)){
  				var string = document.form_upload.elements['upfile_' + i].value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extension = file_name.slice(file_name.indexOf(".")).toLowerCase();

				var valor = document.form_upload.tag_java_ext_not_allowed1.value;
				var valor1 = document.form_upload.tag_java_ext_not_allowed2.value;
  				alert(valor+' "' + file_extension + '" '+valor1);
  				return true;
  			}
  		}
  	}
	return false;
}

function checkAllowFileExtensions(){
	if(check_allow_extensions == false){ return false; }

  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			if(!document.form_upload.elements['upfile_' + i].value.match(allow_extensions)){
  				var string = document.form_upload.elements['upfile_' + i].value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extension = file_name.slice(file_name.indexOf(".")).toLowerCase();

				var valor = document.form_upload.tag_java_ext_not_allowed1.value;
				var valor1 = document.form_upload.tag_java_ext_not_allowed2.value;
  				alert(valor+' con la extensi&oacute;n "' + file_extension + '" no est&aacute; permitido .'+valor1);
  				return true;
  			}
  		}
  	}
	return false;
}

function checkNullFileCount(){
  	if(check_null_file_count == false){ return false; }

  	var null_file_count = 0;

  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value == ""){ null_file_count++; }
  	}

  	titulo_video = document.form_upload.titulo_video.value;
	titulo_video = allTrim(titulo_video); // SE QUITAN LOS ESPACIOS EN BLANCO

  	descripcion_video = document.form_upload.descripcion_video.value;
	descripcion_video = allTrim(descripcion_video); // SE QUITAN LOS ESPACIOS EN BLANCO


	tipo = document.form_upload.tipo.value;
	categoria = document.form_upload.categoria.value;

	if(titulo_video.length==0){//VALIDA QUE EL CAMPO NO ESTE VACIO
		alert('Debe Colocar el Titulo del Video');
		document.form_upload.titulo_video.focus();
		return true;
	}
	/*else
	if(descripcion_video.length==0){//VALIDA QUE EL CAMPO NO ESTE VACIO
		alert('Debe Colocar la descripción del Video');
		document.form_upload.descripcion_video.focus();
		return true;
	}*/
	else
	if(tipo!=1&&tipo!=0)
	{
		alert('Debe Colocar Si es Público o Privado');
		document.form_upload.tipo.focus();
		return true;
	}
	else
	if(categoria==0)
	{
		alert('Debe Especificar la Categoría del Video');
		document.form_upload.categoria.focus();
		return true;
	}
	else
	if(null_file_count == upload_range){
		var valor = document.form_upload.tag_java_empty_file.value;
		alert("Seleccione archivo de video");
		return true;
  	}
  	else{ return false; }
}

function checkDuplicateFileCount(){
	if(check_duplicate_file_count == false){ return false; }

	var duplicate_flag = false;
	var file_count = 0;
	var duplicate_msg = "Archivos duplicados .\n\n";
	var file_name_array = new Array();

	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){
  			var string = document.form_upload.elements['upfile_' + i].value;
			var num_of_last_slash = string.lastIndexOf("\\");

			if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

			var file_name = string.slice(num_of_last_slash + 1, string.length);

			file_name_array[i] = file_name;
  		}
  	}

  	var num_files = file_name_array.length;

	for(var i = 0; i < num_files; i++){
		for(var j = 0; j < num_files; j++){
			if(file_name_array[i] == file_name_array[j] && file_name_array[i] != null){ file_count++; }
		}
		if(file_count > 1){
			duplicate_msg += 'Duplicate file "' + file_name_array[i] + '" detected in slot ' + (i + 1) + ".\n";
			duplicate_flag = true;
		}
		file_count = 0;
	}

	if(duplicate_flag){
		alert(duplicate_msg);
		return true;
	}
	else{ return false; }
}

function uploadFiles(){
	if(checkFileNameFormat()){ return false; }
	if(checkDisallowFileExtensions()){ return false; }
	if(checkAllowFileExtensions()){ return false; }
	if(checkNullFileCount()){ return false; }
	if(checkDuplicateFileCount()){ return false; }

	var total_uploads = 0;

	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){ total_uploads++; }
	}

	document.getElementById('total_uploads').innerHTML = total_uploads;
	document.form_upload.upload_range.value = upload_range;


	document.form_upload.submit();
	document.getElementById('upload_button').disabled = true;

	iniProgressRequest();
	getElapsedTime();

	for(var i = 0; i < upload_range; i++){ document.form_upload.elements['upfile_' + i].disabled = true; }
}

//function resetForm(){ top.location.href = self.location; }
function resetForm(){
//alert('epalex');
top.location.href = self.location;
}

function iniFilePage(){

for(var i = 0; i < upload_range; i++){
		document.form_upload.elements['upfile_' + i].disabled = false;
		document.form_upload.elements['upfile_' + i].value = "";
	}

	document.getElementById('progress_info').innerHTML = "";
	document.getElementById('upload_button').disabled = false;
	document.getElementById('progress_bar').style.display = "none";
	document.form_upload.reset();
}


function stopUpload(){
	try{ window.stop(); }
	catch(e){
		try{ document.execCommand('Stop'); }
		catch(e){}
	}
}

function addUploadSlot(num){
	if(upload_range < max_upload_slots){
		if(num == upload_range){
			var up = document.getElementById('upload_slots');
			var dv = document.createElement("div");

			dv.innerHTML = '<input type="file" name="upfile_' + upload_range + '" size="90" onchange="addUploadSlot('+(upload_range + 1)+')">';
			up.appendChild(dv);
			upload_range++;
			document.form_upload.upload_range.value = upload_range;
		}
	}
}

function smoothCedricStatus(){
	if(info_width < progress_bar_width && !cedric_hold){
		info_width = info_width + 1;
		document.getElementById('upload_status').style.width = info_width + 'px';
	}

	if(get_data_loop){ self.setTimeout("smoothCedricStatus()", info_time_width); }
}

function smoothCedricBytes(){
	if(info_bytes < total_upload_size && !cedric_hold){
		info_bytes = info_bytes + 1;
		document.getElementById('current').innerHTML = info_bytes;
	}

	if(get_data_loop){ self.setTimeout("smoothCedricBytes()", info_time_bytes); }
}

function updateCedricStatus(stats, bytes){
	// var deviant_stat = stats + 20; //Add 5% deviation

	// if(deviant_stat < info_width){ cedric_hold = true; }
	// else{
	// 	cedric_hold = false;
	//	info_width = stats;
	//	info_bytes = bytes;
	// }

	cedric_hold = false;
	info_width = stats;
	info_bytes = bytes;
}

function getProgressStatus(){
	var jsel = document.createElement('SCRIPT');

	jsel.type = 'text/javascript';
	jsel.src = get_status_url + "&rnd_id=" + Math.random();
	//jsel.src = get_status_url;

	document.body.appendChild(jsel);

	if(get_data_loop){ self.setTimeout("getProgressStatus()", get_status_speed); }
}

function getElapsedTime(){
	seconds += 1;

    	if(seconds == 60){
    		seconds = 0;
    		minutes += 1;
    	}

    	if(minutes == 60){
    		minutes = 0;
    		hours += 1;
    	}

    	var hr = "" + ((hours < 10) ? "0" : "") + hours;
    	var min = "" + ((minutes < 10) ? "0" : "") + minutes;
    	var sec = "" + ((seconds < 10) ? "0" : "") + seconds;

    	document.getElementById('time').innerHTML = hr + ":" + min + ":" + sec;

    	if(get_data_loop){ self.setTimeout("getElapsedTime()", 1000); }
}

function createRequestObject(){
	var req = false;

	if(window.XMLHttpRequest){
		req = new XMLHttpRequest();

		if(req.overrideMimeType){ req.overrideMimeType('text/xml'); }
	}
	else if(window.ActiveXObject){
		try{ req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e){
			try{ req = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e){}
		}
	}

	if(!req){
		document.getElementById('progress_info').innerHTML = "Error: Your browser does not support AJAX";
		return false;
	}
	else{ return req; }
}

function iniProgressRequest(){
	var req = false;
	req = createRequestObject();

	if(req){

		var valor5 = document.form_upload.tag_java_initial_progress.value;
		document.getElementById('progress_info').innerHTML = valor5;
		req.open("GET", path_to_ini_status_script + "&rnd_id=" + Math.random(), true);
		//req.open("GET", path_to_ini_status_script, true);
		req.onreadystatechange = function(){ iniProgressResponse(req); };
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(null);
	}
}

function iniProgressResponse(req){
	if(req.readyState == 4){
		if(req.status == 200){
			var xml = req.responseXML;			
			if(xml.getElementsByTagName('error_status').item(0).firstChild.nodeValue == 1){
				document.getElementById('progress_info').innerHTML = xml.getElementsByTagName('error_msg').item(0).firstChild.nodeValue;
				if(xml.getElementsByTagName('stop_upload').item(0).firstChild.nodeValue == 1){ stopUpload(); }
			}
			else{
				get_status_speed = xml.getElementsByTagName('get_data_speed').item(0).firstChild.nodeValue;
				get_status_url = "../../uber_upload/uu_get_status.php?temp_dir_sid=" + xml.getElementsByTagName('temp_dir').item(0).firstChild.nodeValue + tmp_sid + "&start_time=" + xml.getElementsByTagName('start_time').item(0).firstChild.nodeValue + "&total_upload_size=" + xml.getElementsByTagName('total_bytes').item(0).firstChild.nodeValue + "&cedric_progress_bar=" + xml.getElementsByTagName('cedric_progress_bar').item(0).firstChild.nodeValue;

				document.getElementById('progress_bar').style.display = "";
				document.getElementById('total_kbytes').innerHTML = Math.round(Number(xml.getElementsByTagName('total_bytes').item(0).firstChild.nodeValue / 1024)) + " ";

					var valor4 = document.form_upload.tag_java_upload_progress.value;
        			document.getElementById('progress_info').innerHTML = valor4;

				getProgressStatus();

				if(xml.getElementsByTagName('cedric_progress_bar').item(0).firstChild.nodeValue == 1){
					total_upload_size = xml.getElementsByTagName('total_bytes').item(0).firstChild.nodeValue;
	  				smoothCedricBytes();
	  				smoothCedricStatus();
				}
      			}
    		}
    		else{
    			document.getElementById('progress_info').innerHTML = "Error: returned status code " + req.status + " " + req.statusText;
    			//stopUpload();
    		}
  	}
}