﻿//FILE inputy - odkomentovat r.18

var file_index=1;
var tiscali=0;

function pridat_soubor() {
	file_index++;
	var d=document.getElementById('soubory');
	
	var newfile=document.createElement('div');
	newfile.setAttribute('id','f'+file_index+'d');
	newfile.className='file_input_div';
	newfile.innerHTML="<input class='file_input' type='file' id='f"+file_index+"' size='60' name='f[]' onclick='top.reklama_vrchni.ukazIgelit();top.reklama_prava.ukazIgelit()' /> <a href='javascript:smazat_soubor(\"f"+file_index+"d\")'><img src='/images/x.png' alt='Odstranit soubor' class='delete_link' /></a>";
	
	var lastfile=document.getElementById('lastfile');
	lastfile.parentNode.insertBefore(newfile,lastfile);
	
	//predesignovani input type='file';
	//initFileUploads();
}

function smazat_soubor(file_id) {
	var f=document.getElementById(file_id);
	//if(f) 
	f.parentNode.removeChild(f);
}

function doba(x) {
	if(!x) {cena(x+velikost);doba_ukaz(x);}
	else if(potvrd_cenu(x)) {cena(x+velikost);doba_ukaz(x);}
}

var doba_dny=new Array();
doba_dny[0]=14;
doba_dny[1]=30;
doba_dny[2]=90;

var velikost=0;

velikost_mb=new Array();
velikost_mb[0]=300;
velikost_mb[1]=600;
velikost_mb[2]=1024;
velikost_mb[3]=2048;

var nad_cena=new Array();
nad_cena[0]=0;
nad_cena[1]=30;
nad_cena[2]=50;
nad_cena[3]=79;
nad_cena[4]=79;
nad_cena[5]=79;
nad_cena[6]=79;



var shadow=document.getElementById('shadow');
var progress_out=document.getElementById('progress_out');
var progress_bar=document.getElementById('progress_bar');
var progress_bar_in=document.getElementById('progress_bar_in');
var progress_text=document.getElementById('progress_text');
var page=document.getElementById('page');
var submit=document.getElementById('submit');
var upload_frame=document.getElementById('upload_frame');
var odesilatel=document.getElementById('odesilatel');
var doba_hid=document.getElementById('doba_hid');

function potvrd_cenu(x) {
	return(confirm("Zvolili jste dobu uložení "+doba_dny[x]+" dnů. Cena zásilky je "+nad_cena[x+velikost]+" Kč. K platbě pomocí SMS budete automaticky vyzváni po dokončení nahrávání. Neplaťte předem! Pokračovat?"));
}

var doba_min=0;
function doba_ukaz(x) {
	document.getElementById('doba'+x).className='highlight';
	var img = document.getElementById('doba'+x).getElementsByTagName('img');
	activate(img[0]);
  
	document.getElementById('doba'+doba_min).className='';
	img = document.getElementById('doba'+doba_min).getElementsByTagName('img');
	deactivate(img[0]);
	doba_min=x;
	doba_hid.value=x;
}
/*
function activate(img) {
  img.src = img.src.replace(".png","_active.png");
}

function deactivate(img) {
  img.src = img.src.replace("_active","");
}*/

function cena(x) {
	if(nad_cena[x]) {
		document.getElementById('cena_popis').style.display='block';
		document.getElementById('cena').innerHTML=nad_cena[x]+' Kč';
	} else {
		document.getElementById('cena_popis').style.display='none';
		document.getElementById('cena').innerHTML='';
	}
}

function kontrola() {
	if(!isValidEmail(odesilatel.value)) return false;
	return true;
}

var timeout;
function odeslat() {
	//odeslani
	submit.style.display='none';
	shadow.style.display='block';
	//shadow.style.width=document.body.offsetWidth+'px';
	shadow.style.width=document.body.clientWidth+'px';
	shadow.style.height=page.offsetHeight+'px';
	
	progress_out.style.display='block';
	if(tiscali!=true){
		progress_out.style.left=(document.body.offsetWidth/2)-(726/2)+'px';
	} else {
		progress_out.style.left=(document.body.offsetWidth/2)-(800/2)+'px';
		progress_out.style.top='0px';
	}
	//progress_out.style.left=(document.body.offsetWidth/2)+'px';
	
	timeout=window.setTimeout(progress_send, 2000);
	return true;
}

function zrusit_odesilani() {
	window.clearTimeout(timeout);
	upload_frame.src='/nic.html';
	progress_bar_in.style.width = "0px";
	progress_text.innerHTML = "";
	submit.style.display='inline';
	shadow.style.display='none';
	progress_out.style.display='none';
}

function dokoncit_odesilani() {
	window.clearTimeout(timeout);
	progress_bar_in.style.width = "0px";
	progress_text.innerHTML = "";
	submit.style.display='inline';
	shadow.style.display='none';
	progress_out.style.display='none';
}

var ajax_anticache=0;
function send(obsluha, method, url, content, headers) {
	ajax_anticache++;

	var xmlhttp = false;
	if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	} else {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/plain');
	}

	if (!xmlhttp) {
		return false;
	}
	xmlhttp.open(method, url+'&anticache='+ajax_anticache,true);
	if(obsluha) {
		xmlhttp.onreadystatechange = function() {
			obsluha(xmlhttp);
		};
	}
	if (headers) {
		for (var key in headers) {
			xmlhttp.setRequestHeader(key, headers[key]);
		}
	}
	xmlhttp.send(content);
	return true;
}

var unknown=0;

function progress_update(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		if(xmlhttp.responseText=='BIG') {
			zrusit_odesilani();
			alert('T_SOUBOR_MOC_VELKY');
		} else if(xmlhttp.responseText=='UNKNOWN') {
			unknown++;
			if(unknown>30) {
				zrusit_odesilani();
				alert('Při odesílání dat došlo k chybě. Buď bylo přerušeno spojení, nebo velikost zásilky přesáhla 2GB.');
			}
		} else {
			unknown=0;
			try {
				var parts=xmlhttp.responseText.split("\t");
				var w=parseFloat(parts[0]);
				w=parseInt(w*344/100);
				if(w<1) w=1;
				progress_bar_in.style.width = w+"px";
				var total = parts[1];
				if(total > velikost_mb[velikost]) {
					//zmena limitu na velikost
					window.clearTimeout(timeout);
					var velikost_new=velikost;
					while(total > velikost_mb[velikost_new]) velikost_new++;
					if(confirm('Velikost zásilky je '+total+' MB a přesahuje základní limit 300 MB. Cena zásilky bude '+nad_cena[velikost_new+parseInt(doba_hid.value)]+' Kč. K platbě pomocí SMS budete automaticky vyzváni po dokončení nahrávání. Neplaťte předem! Pokračovat?')) { //pokracovat
						velikost=velikost_new;
						cena(parseInt(doba_hid.value)+velikost);
						timeout=window.setTimeout(progress_send, 2000);
					} else { //zrusit
						zrusit_odesilani();
						return;
					}
				}
				progress_text.innerHTML = parts[2];
			} catch(e) { }
		}
	}
}

function progress_send() {
	window.clearTimeout(timeout);
	send(progress_update, 'GET', '/progress.php?lang='+lang+'&id='+upload_id+'&velikost='+velikost_mb[velikost]);
	timeout=window.setTimeout(progress_send, 5000);
}

function isValidEmail(q){
   var d = 'ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|ye|yt|yu|za|zm|zw';
   var r1 = new RegExp("^((.* )?<[a-z0-9_\.\-]+@[a-z0-9\.\-]+\.("+d+")> ?)$",'i');
   var r2 = new RegExp("^( ?[a-z0-9_\.\-]+@[a-z0-9\.\-]+\.("+d+") ?)$",'i');
   return q.match(r1)?true:(q.match(r2)?true:false);
}

