var file_num = 0; //must be defined early in order for check types function to myltiply if mp.3 is there!!!!
var postLocation="../uploader/pgbar.php";

var re = /^(\.jpg)|(\.jpeg)|(\.JPG)|(\.JPEG)|(\.mp3)|(\.MP3)/;  // allow only these types files to upload
var re1= /^(\.mp3)|(\.MP3)/; 

/**
 * dofilter = true; to enable filtering
 */
var dofilter=true;


//text to display on error
/*
var msg1='Sorry, but ';
var msg2=' filetypes are not allowed!';
var msg3='Please specify a file with a valid file extension!';
*/

var msg1='Sajnáljuk, de ';
var msg2=' fájltípusok nem engedélyezettek!';
var msg3='Kérjük, adj meg egy érvényes fájltípust!';
// this method will filter out all the filetypes that are not allowed
 


function check_types() {
	if(dofilter==false)
		return true; 
	var sForm=document.forms[0];

		for(i=1 ; i < sForm.elements.length; i++)
		{
			if (sForm.elements[i].type=="file" && sForm.elements[i].value!=''){
			var sIndex= new Array();
			var end_string= new Array();
			var end= new Array();
			var myS = new Array();
			var num = new Array();
			//alert(sForm.elements[i].value);
			 sIndex[i]=sForm.elements[i].value.lastIndexOf('.');
			// alert(sForm.elements[i].value);
		if (sIndex[i] > -1) {
				myS[i] = sForm.elements[i].value; 
                num[i]=sIndex[i]+5;
			//	alert("sIndex="+sIndex[i]);
			 	end_string[i]=myS[i].substring(sIndex[i],num[i]);
			 //	alert("end_string="+end_string[i]);
				if(!end_string[i].match(re))	{
					alert(msg1 + end_string[i] + msg2);
				return false;
				}
				if (end_string[i].match(re1)) {//it is an mp3 
				file_num += 3; //treat it as 4 pics in the upload limit!! num is set to 3 because file func will add the last count at th bottom
				}
			 } else {
			 alert(msg3);
			return false;
				}
			}
		}
		return true;
	}

function popUP(mypage, myname, w, h, scroll, titlebar)
{

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}


function postIt()
{
	if(check_types() == false)
	{
		return false;
	} 
	baseUrl = postLocation;
	sid = document.forms[0].sessionid.value;
	iTotal = escape("-1");
	baseUrl += "?iTotal=" + iTotal;
	baseUrl += "&iRead=0";
	baseUrl += "&iStatus=1";
	baseUrl += "&sessionid=" + sid;

	popUP(baseUrl,"Uploader",460,162,false,false);
//pass	
file_num += file_count();
var form = document.forms[0];
form.action = form.action+"&files="+file_num;
///sending cookie out
var last_name = document.forms[0].elements[1].value;
var first_name = document.forms[0].elements[2].value;
var year = document.forms[0].elements[3].value;
var month = document.forms[0].elements[4].value;
var day = document.forms[0].elements[5].value;
var hour = document.forms[0].elements[6].value;
var minute = document.forms[0].elements[7].value;
var phrase = document.forms[0].elements[8].value;
if (year != '' && month != '' && day != '' && hour != '' || minute != '') {
var dd = +year+'-'+month+'-'+day+'-'+hour+'-'+minute;
} else {
var dd = '';	
}
var dc = GetCookie('d');
//alert("dc="+dc);
if (dc != '' && dc != null && dc != false) {
var ddc = dc.split('||');
var cook = ddc[0]+'||'+dd+'||'+encodeURI(last_name)+'||'+encodeURI(first_name)+'||'+encodeURI(phrase);
} else {
var cook = '||'+dd+'||'+encodeURI(last_name)+'||'+encodeURI(first_name)+'||'+encodeURI(phrase);
}
SetCookie("d",cook);
document.forms[0].submit();
document.getElementById('subm').onclick='';
}

function file_count() {// this will also count the number of files that will be uploaded
	var sForm=document.forms[0];
	var  file_num = 0;
		for(i=1 ; i < sForm.elements.length-1 ; i++)
		{
			//to count files to upload
			if (sForm.elements[i].type == "file" && sForm.elements[i].value !='') {
				file_num++;
			}
		}
	return file_num;
}