/*      
	func: inner.fm and read.fm js functions
*/

function readShowLoadedDataFolder2(){

	if (document.location.href.indexOf('inner') != -1){
		readInner();
		if ( typeof csord != 'undefined' )
			top.current_sord = csord;
		if ( typeof cstype != 'undefined' )
			top.current_stype = cstype;
		return;
	}

	if ( top.current_folder_array[top.current_folder].data.length == 0 ) {
		document.getElementById('koszonto').innerHTML = '<span>'+top.current_folder+'</span>';
		top.current_folder_array[top.current_folder].current_msg = '';
		document.getElementById('level-test').innerHTML = '';
		top.current_folder_array[top.current_folder].current_page = readFindPage();
		readShowFolder();
		parent.outerLoadReadData();
		return;
	}
	// find page to open and display pages
	if (top.current_folder_array[top.current_folder].current_page == 1) top.current_folder_array[top.current_folder].current_page = readFindPage();
	readShowFolder();
	readAutoopenRead();
}

function readGetPageHeaders(resp) {
	if ( resp != null ) {
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}
		
		top.current_folder_array[top.current_folder].headers = obj.header;
		readShowLoadedDataFolder2();
		return;
	}

	if (document.location.href.indexOf('inner') != -1)
		var frameName = 'levelezes_data';
	else
		var frameName = 'readIFrame';

	if (top.current_folder_array[top.current_folder].searchresult) {
		var page = top.current_folder_array[top.current_folder].current_page;
		var last = ( (page)*top.pagesize > top.current_folder_array[top.current_folder].searchresult.length )?top.current_folder_array[top.current_folder].searchresult.length:(page)*top.pagesize;
		var uids_string = '';
		for ( var i = (page-1)*top.pagesize; i < last; i++ ){
			uids_string += '&uid='+top.current_folder_array[top.current_folder].searchresult[i].uid;
		}
	} else {
		var page = top.current_folder_array[top.current_folder].current_page;
		var last = ( (page)*top.pagesize > top.current_folder_array[top.current_folder].data.length )?top.current_folder_array[top.current_folder].data.length:(page)*top.pagesize;
		var uids_string = '';
		for ( var i = (page-1)*top.pagesize; i < last; i++ ){
			uids_string += '&uid='+top.current_folder_array[top.current_folder].data[i].uid;
			if ( (document.location.href.indexOf('inner') != -1) && ( i > 5) )
				break;
		}
	}

	sUri = "cmd=header&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder)+uids_string;
	top.loadXmlFile(sUri, 'top.outer.document.getElementById(\''+frameName+'\').contentWindow.readGetPageHeaders', 'fm', 1, true);
}

function readLoadFolder(resp, sort){
	if ( resp != null ) {
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}

	//	prompt('','resp:'+resp);

		if (top.current_folder_array[top.current_folder]) {
			top.current_folder_array[top.current_folder].data = obj.folder;   
			if (document.location.href.indexOf('inner') != -1)
				top.current_folder_array[top.current_folder].current_page = '1';
			else
				top.current_folder_array[top.current_folder].current_page = readFindPage();
			if (top.current_folder_array[top.current_folder].searchresult) {
				// itt sortolni kell a searchresult-ot is, ha eppen van!
				var searchresult_new = new Array();
				for (var i = 0; i < top.current_folder_array[top.current_folder].data.length; i++) {
					var j = 0;
					while (j < top.current_folder_array[top.current_folder].searchresult.length && top.current_folder_array[top.current_folder].searchresult[j].uid != top.current_folder_array[top.current_folder].data[i].uid) j++;
					if (j < top.current_folder_array[top.current_folder].searchresult.length) {
						searchresult_new.push(top.current_folder_array[top.current_folder].searchresult[j]);
					}
					if (searchresult_new.length == top.current_folder_array[top.current_folder].searchresult.length) i = top.current_folder_array[top.current_folder].data.length;
				}
				top.current_folder_array[top.current_folder].searchresult = searchresult_new;
			}
		} else {
			parent.utilsFolderCreate(top.current_folder);
			top.current_folder_array[top.current_folder].name = top.current_folder;
			top.current_folder_array[top.current_folder].data = obj.folder;   
			top.current_folder_array[top.current_folder].current_page = '1';
		}

		if (top.current_sord){
			top.current_folder_array[top.current_folder].data.reverse();
			if (top.current_folder_array[top.current_folder].searchresult) {
				top.current_folder_array[top.current_folder].searchresult.reverse();
			}
		}
		readGetPageHeaders();
		
		return;
	}

	//alert('rlf');
	if (document.location.href.indexOf('inner') != -1)
		var frameName = 'levelezes_data';
	else
		var frameName = 'readIFrame';

	sUri = "cmd=folder2&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder)+"&sort="+sort;
	top.loadXmlFile(sUri, 'top.outer.document.getElementById(\''+frameName+'\').contentWindow.readLoadFolder', 'fm', 0, true);
}

/*
	func: readLoadFolder - read the xml data of the current folder
	called by: onLoad, TODO auto update
	param: resp - xmlhttprequest response, upd - 1 if update only
*/

function readLoadFolderPreFolder2(resp, upd){
	if ( resp != null ) {

		//setTimeout('top.showLoad(-1)',1);
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}

		//prompt('','resp:'+resp);

		if ( upd == 0 ){
			parent.utilsFolderCreate(top.current_folder)
			top.current_folder_array[top.current_folder].name = top.current_folder;
			top.current_folder_array[top.current_folder].current_page = '0';
			top.current_folder_array[top.current_folder].current_page = '1';
			top.current_folder_array[top.current_folder].data = top.current_folder_array[top.current_folder].data.concat(obj.folder);   
		} else {
			if ( obj.folder.length == 0 ){ // no update needed
				readShowLoadedData();
				return;
			}
			top.current_folder_array[top.current_folder].data = top.current_folder_array[top.current_folder].data.concat(obj.folder);   
		}

		// PISTI CSINALJA
//		alert(obj.last);
		top.current_folder_array[top.current_folder].last_msg = obj.last;
		
		readShowLoadedData();
		
		return;
	}

	
	if (document.location.href.indexOf('inner') != -1)
		var frameName = 'levelezes_data';
	else
		var frameName = 'readIFrame';

	//setTimeout('top.showLoad(1)',1);
	
	if(!top.current_folder_array[top.current_folder]) { // load new folder
		sUri = "cmd=folder&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder);	 
		//prompt('',sUri);
		top.loadXmlFile(sUri, 'top.outer.document.getElementById(\''+frameName+'\').contentWindow.readLoadFolder', 'fm', 0, false);
	} else {
		sUri = "cmd=folder&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder)+"&last="+top.current_folder_array[top.current_folder].last_msg;
		top.loadXmlFile(sUri, 'top.outer.document.getElementById(\''+frameName+'\').contentWindow.readLoadFolder', 'fm', 1, false);
	}
}

/*
	func: readShowLoadedData - show or update loaded data;
	called by: readLoadFolder
	param: -
*/

function readShowLoadedData(){
	// sort folder
	csord = top.current_sord;
	cstype = top.current_stype;
	if (document.location.href.indexOf('inner') != -1){
		top.current_folder_array[top.current_folder].data.sort(myFolderSortfnD);
		//top.current_folder_array[top.current_folder].data.reverse();
	} else {
		top.current_folder_array[top.current_folder].data.sort(myFolderSortfnD);
		top.current_folder_array[top.current_folder].data.reverse();
	}



	if (document.location.href.indexOf('inner') != -1){
		readInner();
		if ( typeof csord != 'undefined' )
			top.current_sord = csord;
		if ( typeof cstype != 'undefined' )
			top.current_stype = cstype;
		return;
	}

	// if update do not find unread
	//
	//alert(top.current_folder_array[top.current_folder].data.length);
	if ( top.current_folder_array[top.current_folder].data.length == 0 ) {
		document.getElementById('koszonto').innerHTML = '<span>'+top.current_folder+'</span>';
		top.current_folder_array[top.current_folder].current_msg = '';
		document.getElementById('level-test').innerHTML = '';
		top.current_folder_array[top.current_folder].current_page = readFindPage();
		readShowFolder();
		parent.outerLoadReadData();
		return;
	}
	if ( top.current_folder_array[top.current_folder].current_msg == '' ) {
		// find first unread
		for ( var i in top.current_folder_array[top.current_folder].data ){
			var cdata = top.current_folder_array[top.current_folder].data[i];
			if ( cdata.isnew == 1 ){
				top.current_folder_array[top.current_folder].current_msg = cdata.uid;
				break;
			}
		}
		if ( top.current_folder_array[top.current_folder].current_msg == '' ){
			// no new msg
			var cdata = top.current_folder_array[top.current_folder].data[0];
			top.current_folder_array[top.current_folder].current_msg = cdata.uid;
		}
	}

	// find page to open and display pages
	top.current_folder_array[top.current_folder].current_page = readFindPage();
	readShowFolder();
	readAutoopenRead();
}


/*
	func: sort emails by DATE
	called by: folderOrd
	param: two elemnt to compare
*/

function myFolderSortfnD(a,b){     // sort folder by the current sort order Date
	return (a.received - b.received)
}

/*
	func: sort emails by SIZE
	called by: folderOrd
	param: two elemnt to compare
*/

function myFolderSortfnM(a,b){     // sort folder by the current sort order Size
	return (a.size - b.size)
}

/*
	func: sort emails by FROM
	called by: folderOrd
	param: two elemnt to compare
*/

function myFolderSortfnF(a,b){	  // sort folder by the current sort order From
	//alert(a.fromlex);
	var s1 = (a.fromlex?a.fromlex:'');
	var s2 = (b.fromlex?b.fromlex:'');
	if (s1 < s2) return -1;
	if (s1 > s2) return 1;
	return (a.received - b.received);
}

/*
	func: sort emails by SIZE
	called by: folderOrd
	param: two elemnt to compare
*/

function myFolderSortfnS(a,b){	  // sort folder by the current sort order Subject
	//alert(a.subjectlex);
	var s1 = (a.subjectlex?a.subjectlex:'');
	var s2 = (b.subjectlex?b.subjectlex:'');
	if (s1 < s2) return -1;
	if (s1 > s2) return 1;
	return (a.received - b.received);
}

/*
	func: sort emails
	called by: readInner, TODO read folders... sort...
	param: type - d for date; f for from; s for subject; m for size;
*/

function folderOrd(type, isInner){
	// set the order icons to normal
	if ( (isInner != 1) && document.getElementById('sort'+top.current_stype) )
		document.getElementById('sort'+top.current_stype).className = '';
	if ( type == top.current_stype ){
		top.current_sord = top.current_sord ? 0 : 1;
	} else {
		top.current_stype = type;
		top.current_sord = 0;
		switch (top.current_stype){
			case 'D':	      // if date
				top.current_sname = 'received';
				break;
			case 'N':	      // if from
				top.current_sname = 'from';
				break;
			case 'S':	      // if subject
				top.current_sname = 'subject';
				break;
			case 'Z':	      // if size
				top.current_sname = 'size';
				break;
		}
	}
	// set the order icons to current
	if ( isInner != 1 ){
		if ( top.current_sord ) {
			document.getElementById('sort'+top.current_stype).className = 'akt-fel';
		} else {
			document.getElementById('sort'+top.current_stype).className = 'akt-le';
		}
	}
	if (top.current_sord){
		top.current_folder_array[top.current_folder].data.reverse();
		if (top.current_folder_array[top.current_folder].searchresult) {
			top.current_folder_array[top.current_folder].searchresult.reverse();
		}
		readGetPageHeaders();
	} else {
		readLoadFolder(null, top.current_sname);
	}
}

/*
	func: form data for output
	called by: * 
	param: d ;
*/

function readFormData(d){
	if ( !d ){
		return '';
	} else {
		d = d.replace(/&/g, "&amp;");
		d = d.replace(/</g,'&lt;');
		d = d.replace(/>/g,'&gt;');
		d = d.replace(/"/g, '&quot;');
		d = d.replace(/'/g, '&#039;');
		return d;
	}
}

/*
	func: get hungarian formated date from epoc
	called by: read header, folder list
	param: epoc - timestamp
*/

function readGetFormedDate(epoc){
	if(!epoc){
		return '';
	}
	var hmonth = new Array('jan','feb','már','ápr','máj','jún','júl','aug','szep','okt','nov','dec');
	mdate = new Date();
	mdate.setTime(epoc*1000);
	var y = mdate.getFullYear();
	var d = mdate.getDate() < 10 ? '0'+mdate.getDate(): ''+mdate.getDate();
	var h = mdate.getHours() < 10 ? '0'+mdate.getHours(): ''+mdate.getHours();
	var m = mdate.getMinutes() < 10 ? '0'+mdate.getMinutes(): ''+mdate.getMinutes();
	var s = mdate.getSeconds() < 10 ? '0'+mdate.getSeconds(): ''+mdate.getSeconds();

	var sdate = y+'. '+hmonth[mdate.getMonth()] +'. '+ d +'. '+ h +':'+ m;
	return sdate;
}

/*
	func: get hungarian sort format date from epoc
	called by: inner page
	param: epoc - timestamp
*/

function readGetSortDate(epoc){
	if(!epoc){
		return '';
	}
	mdate = new Date();
	mdate.setTime(epoc*1000);
	var d = mdate.getDate() < 10 ? '0'+mdate.getDate(): ''+mdate.getDate();
	var m = (mdate.getMonth()+1) < 10 ? '0'+(mdate.getMonth()+1): ''+(mdate.getMonth()+1);

	var sdate = m+'.'+d+'.';
	return sdate;
}

/*
	func: get hungarian long format date from epoc
	called by: read header, folder list
	param: epoc - timestamp
*/

function readGetLongDate(epoc){
	if(!epoc){
		return '';
	}
	var hday = new Array('Vas\xe1rnap','H\xe9tf\xf5','Kedd','Szerda','Cs\xfct\xf6rt\xf6k','P\xe9ntek','Szombat');
	var hmonth = new Array('jan','feb','már','ápr','máj','jún','júl','aug','szep','okt','nov','dec');
	mdate = new Date();
	mdate.setTime(epoc*1000);
	var d = mdate.getDate() < 10 ? '0'+mdate.getDate(): ''+mdate.getDate();
	var h = mdate.getHours() < 10 ? '0'+mdate.getHours(): ''+mdate.getHours();
	var m = mdate.getMinutes() < 10 ? '0'+mdate.getMinutes(): ''+mdate.getMinutes();
	var s = mdate.getSeconds() < 10 ? '0'+mdate.getSeconds(): ''+mdate.getSeconds();

	var sdate = (mdate.getYear()<1000?mdate.getYear()+1900:mdate.getYear()) +'. '+ hmonth[mdate.getMonth()] +'. '+ d +'. '+ hday[mdate.getDay()] +' '+ h +':'+ m +':'+ s;
	return sdate;
}

/*      
	func: set the flag and current state of the file
	called by: called by the chFlags pupup and by read (isnew)
	param: uid, flag, vale
*/

function setFlags(uid, flag, val){
/*
	for ( var i in top.current_folder_array[top.current_folder].data ){
		if ( top.current_folder_array[top.current_folder].data[i].uid == uid ){
			var curElement = top.current_folder_array[top.current_folder].data[i];
*/
	for ( i=0; i < top.current_folder_array[top.current_folder].headers.length; i++ ){
		if ( top.current_folder_array[top.current_folder].headers[i].uid == uid ){
			var curElement = top.current_folder_array[top.current_folder].headers[i];
			switch (flag){
				case 'isnew':
					curElement.isnew = val;
					break;
				case 'isnew_first':
					curElement.isnew = val;
					break;
				case 'importance':
					curElement.importance = val;
					break;
				case 'flag':
					curElement.flag = val;
					break;
				case 'highlight':
					curElement.highlight = val;
					break;
			}
		}
	}
	if ( top.current_folder == 'Eredmény'){
/*
		for ( var i in top.current_folder_array[top.searchFolder].data ){
			if ( top.current_folder_array[top.searchFolder].data[i].uid == uid ){
				var curElement = top.current_folder_array[top.searchFolder].data[i];
*/
		for ( i=0; i < top.current_folder_array[top.searchFolder].headers.length; i++ ){
			if ( top.current_folder_array[top.searchFolder].headers[i].uid == uid ){
				var curElement = top.current_folder_array[top.searchFolder].headers[i];
				switch (flag){
					case 'isnew':
						curElement.isnew = val;
						break;
					case 'isnew_first':
						curElement.isnew = val;
						break;
					case 'importance':
						curElement.importance = val;
						break;
					case 'flag':
						curElement.flag = val;
						break;
					case 'highlight':
						curElement.highlight = val;
						break;
				}
			}
		}

	}
	if ( (flag == 'flag') || (flag == 'isnew') ){
		var sUri = '';
		if ( top.current_folder == 'Eredmény')
			sUri = "cmd=setstatus&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.searchFolder)+"&uid="+uid;
		else
			sUri = "cmd=setstatus&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder)+"&uid="+uid;
		if ( flag == 'isnew' )
			sUri = sUri + "&isnew = "+val;
		else
			sUri = sUri + "&flag = "+val;
		top.loadXmlFile(sUri, 'top.outer.document.getElementById(\'readIFrame\').contentWindow.readSetFlagsResp', 'fm', uid, true);
	}
}

/*      
	func: response function on setstatus call
	called by: setFlags by xmlhttrequest
	param: resp - xmlhttp response
*/

function readSetFlagsResp(resp){
	if ( resp != null ) {
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}
		//update folders list
		parent.outerLoadReadData();
	}
	return;	
}

/*
	func: escape sting to include in js
	called by: * 
	param: s ;
*/

function readStringEscape(s){
	if ( !s ){
		return '';
	} else {
		if ( s.replace )
			s = s.replace(/\'/g,'\\\'');
		return s;
	}
}


/*      
	func: compose the mail header sendRr
	called by: read  - sendrr
	param: 
*/

function cMailHeaderText(date, to, from, cc, reply_to, hugefile, subject){
	var ih = '';
	if ( date )
		ih = ih + 'Dátum: ' + date + '\n';
	if ( from )
		ih = ih + 'Feladó: ' + top.outer.utilsGetMails(from, 1) + '\n';
	if ( to )
		ih = ih + 'Címzett: ' + top.outer.utilsGetMails(to, 1) + '\n';
	if ( reply_to )
		ih = ih + 'Válaszcím: ' + top.outer.utilsGetMails(reply_to, 1) + '\n';
	if ( hugefile )
		ih = ih + 'Oriásfájl: ' + hugefile + '\n';
	if ( subject )
		ih = ih + 'Tárgy: ' + subject + '\n';
	
	return ih;
}
/*      
	func: compose the mail header to display in read, forward and reply
	called by: read and write (reply, forward)
	param: 
*/

function cMailHeader(date, to, from, cc, reply_to, hugefile, subject, isMainHeader){
	 
 	//var ih = '<div id="MyId"><ul>';       
 	var ih = '<div id="MyId"><table>';       
	if (date){
		ih = ih + '<tr><td class="levh0">Dátum: </td><td class="levh1">';
		ih = ih + readFormData(readStringEscape(date)) +'</td></tr>';
	}
	if (from){
		ih = ih + '<tr><td class="levh0">Feladó: </td><td class="levh1">';
		for (var i in from){
			var cih = '';
			if (from[i].name && from[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(from[i].mail)) +'\', \''+ readFormData(readStringEscape(from[i].name)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(from[i].mail)) +'\',\''+ readFormData(readStringEscape(from[i].name)) +'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(from[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(from[i].mail) +'&gt;</a>';
				} else {
					cih = cih + readFormData(from[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(from[i].mail) +'&gt;';
				}
				cih = cih + '<br />';
			} else if (from[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(from[i].mail)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(from[i].mail)) +'\',\'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(from[i].mail) +'</a>';
				} else {
					cih = cih + readFormData(from[i].mail);
				}
				cih = cih + '<br />';
			}
			/*
			if ( i < (from.length-1) )
				cih = cih + '<li><span class="levh0">&nbsp;</span>';
			*/
			ih = ih + cih;
		}
		ih = ih + '</td></tr>';
	}
	if (to){
		ih = ih + '<tr><td class="levh0">Címzett: </td><td class="levh1">';
		for (var i in to){
			var cih = '';
			if (to[i].name && to[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(to[i].mail)) +'\', \''+ readFormData(readStringEscape(to[i].name)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(to[i].mail)) +'\',\''+ readFormData(readStringEscape(to[i].name)) +'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(to[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(to[i].mail) +'&gt;</a>';
				} else {
					cih = cih + readFormData(to[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(to[i].mail) +'&gt;';
				}
				cih = cih + '<br />';
			} else if (to[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(to[i].mail)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(to[i].mail)) +'\',\'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(to[i].mail) +'</a>';
				} else {
					cih = cih + readFormData(to[i].mail);
				}
				cih = cih + '<br />';
			}
			/*
			if ( i < (to.length-1) )
				cih = cih + '<li><span class="levh0">&nbsp;</span>';
			*/
			ih = ih + cih;
		}
		ih = ih + '</td></tr>';
	}
	if (cc){
		ih = ih + '<tr><td class="levh0">Másolat: </td><td class="levh1">';
		for (var i in cc){
			var cih = '';
			if (cc[i].name && cc[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(cc[i].mail)) +'\', \''+ readFormData(readStringEscape(cc[i].name)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(cc[i].mail)) +'\',\''+ readFormData(readStringEscape(cc[i].name)) +'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(cc[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(cc[i].mail) +'&gt;</a>';
				} else {
					cih = cih + readFormData(cc[i].name) +' ';
					cih = cih + '&lt;'+ readFormData(cc[i].mail) +'&gt;';
				}
				cih = cih + '<br />';
			} else if (cc[i].mail){
				if ( top.ciframe.indexOf('read') != -1 ){
					cih = cih + '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \''+ readFormData(readStringEscape(cc[i].mail)) +'\',\'\',\'\',\'read\');" onmouseover="readQuickLayer(this, this.id, true, \''+ readFormData(readStringEscape(cc[i].mail)) +'\',\'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">';
					cih = cih + readFormData(cc[i].mail) +'</a>';
				} else {
					cih = cih + readFormData(cc[i].mail);
				}
				cih = cih + '<br />';
			}
			/*
			if ( i < (cc.length-1) )
				cih = cih + '<li><span class="levh0">&nbsp;</span>';
			*/
			ih = ih + cih;
		}
		ih = ih + '</td></tr>';
	}
	if (reply_to){
		ih = ih + '<tr><td class="levh0">Válaszcím: </td><td class="levh1">';
		for (var i in reply_to){
			var cih = '';
			if (reply_to[i].name){
				cih = cih + readFormData(reply_to[i].name) +' ';
				if (reply_to[i].mail){
					cih = cih + '&lt;'+ readFormData(reply_to[i].mail) +'&gt;<br />';
				}
			} else if (reply_to[i].mail){
				cih = cih + readFormData(reply_to[i].mail) +'<br />';
			}
			ih = ih + cih;
		}
		ih = ih + '</td></tr>';
	}
	if (subject){
		ih = ih + '<tr><td class="levh0">Tárgy: </td><td class="levh1">';
		ih = ih + readFormData(subject) +'</td></tr>';
	}
	if (hugefile){
		ih = ih + '<tr><td class="levh0">Oriáslevél:</td>';
		ih = ih	 + '<td class="levh1">'+readFormData(hugefile.name)+' [ <a target="hugemail" href="http://freemail.hu/oriaslevel/download.php?id='+readFormData(readStringEscape(hugefile.id))+'" style="display: inline; float: none; margin: 0px;">let\xf6lt\xe9s</a> ]</td></tr>';
	}
	// attachments
	if (isMainHeader){
		ih = ih + '<tr id="mailHeadAttachLi" style="display: none;"><td class="levh0">Melléklet:</td><td class="levh1"><span class="levh2" id="mailHeadAttach"></span><span class="levh2" id="mailHeadAttachAll"></span></td></tr>';
	}

	//ih = ih + '</ul></div>';
	ih = ih + '</table></div>';

	return ih;
}

/*     
	func: format text part of the msg
	called by: readShowMail
	param: 
*/

function readFormatText(data, name){
//      data = data.replace(/&/g, "&amp;");
	data = data.replace(/</g, "&lt;");
	data = data.replace(/>/g, "&gt;");
	data = data.replace(/\t/g, "&nbsp; &nbsp; &nbsp; &nbsp;");
	 
	//url
	data = data.replace(/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/ig, '<a href="$&" target="_blank">$&</a>');
	//email
	//modified by pisti//data = data.replace(/(\s)\b(?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b/im, '$1<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \'$2\',\'\',\'\',\'\',\'read\')">$2</a>');
	data = data.replace(/(?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b/ig, '<a href="javascript:void(0)" onClick="parent.outerOpenWrite(\'write\', \'$1\',\'\',\'\',\'\',\'read\')">$1</a>');
	 
	data = data.replace(/\n/g, "<br />");
	 
	if (name) {
		var ih='';
		ih = ih + '<br><hr>';
		ih = ih + '<b>F\xe1jln\xe9v:'+readFormData(name);
		ih = ih + '</b><pre>';
		data = ih + data + '</pre>';
	}      
	return data;
}

/*     
	func: format html part of the msg
	called by: readShowMail
	param: 
*/

function readFormatHTML(host, data, uid){
	data = data.replace(/\b(cid):([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, 'http://'+host+'/fm/xmldata?cmd=getattach&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&uid='+uid+'&$1=$2');       
	//pisti-mozilla data = data.replace(/\bhref\s*=\s*["']([hf]t[tp][p:][:s/][^<>\s'"]*)["']/ig, 'href="$1" target="_blank"');
	data = data.replace(/\bhref\s*=\s*["']([hf]t[tp][p:][:s\/][^<>\s'"]*)["']/ig, 'href="$1" target="_blank"');
	data = data.replace(/\bhref\s*=\s*["']mailto:\s*([^<>\b"']*)["']/ig, 'href="javascript:top.outer.outerOpenWrite(\'write\', \'$1\',\'\',\'\',\'\',\'read\');"');
	return data;
}

/*     
	func: format attachment part of the msg
	called by: readShowMail
	param: 
*/

function readFormatAttachment(host, name, cid, ct, description, size, uid){
	var ih='';
/*
	ih = ih + '<table border="1">';
	if (name) {
		ih = ih + '<tr><td class="attachTable"><b>F\xe1jln\xe9v:</td>';
		ih = ih + '<td class="attachTable">';
		ih = ih + '<a href="http://'+host+'/fm/xmldata?cmd=getattach&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&cid='+cid+'&uid='+uid+'&save=1" tager="_blank">'+name+'</a>';
		ih = ih + '</td></tr>'
	}
	if (ct)
		ih = ih + '<tr><td class="attachTable"><b>T\xedpus:</td><td class="attachTable">'+ct+'</td></tr>'
	if (description)
		ih = ih + '<tr><td class="attachTable"><b>Megjegyz\xe9s:</td><td class="attachTable">'+description+'</td></tr>'
	if (size)
		ih = ih + '<tr><td class="attachTable"><b>M\xe9ret:</td><td class="attachTable">'+size+' byte</td></tr>'
	ih = ih + '</table>';
*/
	var aih = document.getElementById('mailHeadAttach').innerHTML
	aih = aih + '<a href="http://'+host+'/fm/xmldata?cmd=getattach&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&cid='+readFormData(readStringEscape(cid))+'&uid='+uid+'&save=1" target="_blank" onmouseover="readQuickLayerDownload(this, this.id, true, \''+ readFormData(readStringEscape(name)) +'\', \''+ host +'\',\''+ readFormData(readStringEscape(cid)) +'\',\''+ readFormData(readStringEscape(ct)) +'\',\''+ uid +'\');" onmouseout="QuickLayerShown = setTimeout(\'HideQuickLayer()\', 10);">'+readFormData(name)+'</a><br />\n';
	document.getElementById('mailHeadAttach').innerHTML = aih;

	top.readAttached.push(cid);
	if ( top.readAttached.length >= 2 ){
		//var aih = document.getElementById('mailHeadAttachAll').innerHTML
		var aih = '<a href="http://'+host+'/fm/xmldata?cmd=getattachzip&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+utilsGetReadAttached()+'&save=1" tager="_blank">Összes&nbsp;tömörítve</a>';
		document.getElementById('mailHeadAttachAll').innerHTML = aih;
	}
	//document.getElementById('mailHeadAttachLi').style.display = 'list-item';
	document.getElementById('mailHeadAttachLi').style.display = '';

	//var mh = document.getElementById('mailHeadAttach').parentNode.offsetHeight;
	//document.getElementById('mailHeadAttachLi').style.height = mh + 'px';

	if ( ( ct.toLowerCase().indexOf("image/gif") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpeg") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpg") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpe") != -1 ) ||
		( ct.toLowerCase().indexOf("image/pjpeg") != -1 ) ||
		( ct.toLowerCase().indexOf("image/png") != -1 ) ){
		ih = ih + '<br><hr>'+readFormData(name)+'<br>';
		ih = ih + '<img src="http://'+host+'/fm/xmldata?cmd=getattach&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&cid='+readFormData(cid)+'&uid='+uid+'&save=0">';
	}
	return ih;
}

/*     
	func: dispaly the search layer
	called by: search btn click
	param: 
*/

function readSearchDisp(){
				//top.current_folder_array[top.current_folder].current_page = 1;
				//alert(top.current_folder_array[top.current_folder].current_page);
				//readShowFolder();
				//folderToPage();
				//return;
	if ( !document.getElementById("searchMsg").style.display || (document.getElementById("searchMsg").style.display == 'none') )
	{
		document.sform.sin.options[0].selected = true;
		document.getElementById("sDateTd").style.display = 'none';
		document.getElementById("sTxtTd").style.display = 'block';

		/*var searchBtnTop = document.getElementById('searchBtn').offsetTop;
		var searchBtnLeft = document.getElementById('searchBtn').offsetLeft;

		document.getElementById("searchMsg").style.top = searchBtnTop+30;
		document.getElementById("searchMsg").style.left = searchBtnLeft+top.xsep;
		*/
		document.getElementById("searchMsg").style.top = 50+'px';
		document.getElementById("searchMsg").style.right = 0+'px';
		document.getElementById("searchMsg").style.display = 'block';
		document.getElementById("searchIn").focus();
	} else {
		document.getElementById("searchMsg").style.display = 'none';

	}
}

/*     
	func: change the search type
	called by:  select search type
	param: type - select from the dropdown 
*/

function readChSearch(type){
	if ( type == 'date' ){
		document.getElementById("sTxtTd").style.display = 'none';
		document.getElementById("sDateTd").style.display = 'block';
		if ( document.sform.dvalue.value.length <= 1 ){
			var mdate = new Date();
			var y = ''+mdate.getFullYear();
			var m = mdate.getMonth() < 9 ? '0'+(mdate.getMonth() + 1): ''+(mdate.getMonth() + 1);
			var d = mdate.getDate() < 10 ? '0'+mdate.getDate() : ''+mdate.getDate();
			document.sform.dvalue.value= y+'/'+m+'/'+d;
		}
	} else {
		document.getElementById("sDateTd").style.display = 'none';
		document.getElementById("sTxtTd").style.display = 'block';
	}
}

function readSearchResp(resp) {
	var obj = top.utilsDataParse(resp);
	if (obj.ret) {
		if (obj.ret == 4){
			//sync
			setTimeout('window.readInit()',10);
		}
		return;
	}
	top.current_folder_array[top.current_folder].current_page = '1';
	top.current_folder_array[top.current_folder].searchresult = obj.result;
	// search on the top page
	if (document.location.href.indexOf('inner') != -1){
		parent.readLoad();
	} else {
		readGetPageHeaders();
	}
}

/*     
	func: search by the criteria in the current folder
	called by: 
	param: 	type - [all; participant; subject; date]; 
		value - serch value;  
		din - date criteria;
		dvalue - date value
		res - 1 if research - set current folder to Eredmeny (if no Eredmeny use current)
*/

function readSearch(type, value, din, dvalue, res){
	if (document.location.href.indexOf('inner') != -1)
		var frameName = 'levelezes_data';
	else
		var frameName = 'readIFrame';

	sUri = "cmd=search&tid="+top.tid+"&ulid="+top.ulid+"&homeid="+top.home+"&id="+top.id+"&folder="+top.outer.urlEncode(top.current_folder)+"&searchstring="+top.outer.urlEncode(value);
	if (type == 'to' || type == 'all') sUri += "&searchfrom=1&searchto=1&searchcc=1";
	if (type == 'subject' || type == 'all') sUri += "&searchsubj=1";
	sUri += "&searchbody=0";
	top.loadXmlFile(sUri, 'top.outer.document.getElementById(\''+frameName+'\').contentWindow.readSearchResp', 'fm', 1, false);
}

/*     
	func: modify and show the quick layer for the mail header
	called by:  mail header user onmouseover
	param: this - object, email current mail
*/

function readQuickLayer(obj, SId, isDiv, mail, name){
	clearTimeout(QuickLayerShown);

	document.getElementById('QuickLayer').style.width = obj.offsetWidth + 6 + 'px';
	document.getElementById('ql_1').style.width = obj.offsetWidth + 'px';
	document.getElementById('ql_2').style.width = obj.offsetWidth + 'px';
	document.getElementById('ql_2').onmousedown = function() { HideQuickLayer(); parent.outerOpenWrite('write', mail, name,'','','read'); };
	document.getElementById('ql_2').innerHTML = 'Levelet ír';
	document.getElementById('ql_3').style.width = obj.offsetWidth + 'px';
	document.getElementById('ql_3').onmousedown = function() { parent.outerUpdateCC( null, mail, name); HideQuickLayer(); };
	document.getElementById('ql_3').innerHTML = 'Címtárba';
	document.getElementById('ql_4').style.width = obj.offsetWidth + 'px';
	document.getElementById('ql_4').onmousedown = function() { readMailBlock( null, mail); HideQuickLayer(); };
	document.getElementById('ql_4').innerHTML = 'Blokkolási listába';
	document.getElementById('ql_1').style.display = 'block';
	document.getElementById('ql_2').style.display = 'block';
	document.getElementById('ql_3').style.display = 'block';
	document.getElementById('ql_4').style.display = 'block';
	if (name && mail){
		document.getElementById('ql_1').innerHTML = readFormData(name) +' &lt;'+ readFormData(mail) +'&gt';
		document.getElementById('ql_1').style.textDecoration = 'underline';
		ShowQuickLayer(obj, SId, isDiv);
	} else if (mail){ 
		document.getElementById('ql_1').innerHTML = readFormData(mail);	
		document.getElementById('ql_1').style.textDecoration = 'underline';
		ShowQuickLayer(obj, SId, isDiv);
	}
}

/*     
	func: set the given email in the block list
	called by: read header gui
	param: resp - xmlhttp response, email - email
*/

function readMailBlock (resp, email){
	if ( resp != null ) {
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}
		return;
	}

	sUri = "cmd=addblock&tid="+top.tid+"&ulid="+top.ulid+"&mail="+email;
	top.loadXmlFile(sUri, 'top.outer.document.getElementById(\'readIFrame\').contentWindow.readMailBlock', 'fm', true);
}

/*     
	func: modify and show the quick layer for the mail header download
	called by:  mail header user onmouseover
	param: this - object, current file
*/

function readQuickLayerDownload(obj, SId, isDiv, name, host, cid, ct, uid){
	clearTimeout(QuickLayerShown);

	if ( ( ct.toLowerCase().indexOf("image/gif") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpeg") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpg") != -1 ) ||
		( ct.toLowerCase().indexOf("image/jpe") != -1 ) ||
		( ct.toLowerCase().indexOf("image/pjpeg") != -1 ) ){
		var isimg = 1;
	}
	var hw = (obj.offsetWidth < 100 ) ? 100 : obj.offsetWidth;
	document.getElementById('QuickLayer').style.width = hw + 5 + 'px';
	document.getElementById('ql_1').style.width = hw + 'px';
	document.getElementById('ql_2').style.width = hw + 'px';
	document.getElementById('ql_2').innerHTML = '<a href="http://'+host+'/fm/xmldata?cmd=getattach&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&cid='+readFormData(readStringEscape(cid))+'&uid='+uid+'&save=1" class="header2"><span style="display:block;width:100%;cursor:default">Letölt</span></a>';
	document.getElementById('ql_2').onmousedown = function() { setTimeout('HideQuickLayer()', 300); return true; };
	document.getElementById('ql_3').style.width = hw + 'px';
	document.getElementById('ql_3').onmousedown = function() { setTimeout('HideQuickLayer()', 300);  return true; };
	document.getElementById('ql_3').innerHTML = '<a href="http://'+host+'/fm/xmldata?cmd=getattachzip&tid='+top.tid+'&ulid='+top.ulid+'&id='+top.id+'&cid='+readFormData(readStringEscape(cid))+'&save=1" class="header2"><span style="display:block;width:100%;cursor:default">Tömörítve letölt</a>';
	document.getElementById('ql_4').style.width = hw + 'px';
	if ( isimg ) {
		document.getElementById('ql_4').onmousedown = function() { readSaveImg(null, cid, uid); setTimeout('HideQuickLayer()', 300);  return true; };
		document.getElementById('ql_4').innerHTML = '<a href="javascript:void(0)" class="header2"><span style="display:block;width:100%;cursor:default">Fotótárba ment</a>';
	}
	document.getElementById('ql_1').style.display = 'block';
	document.getElementById('ql_2').style.display = 'block';
	document.getElementById('ql_3').style.display = 'block';
	if ( isimg ) {
		document.getElementById('ql_4').style.display = 'block';
	} else {
		document.getElementById('ql_4').style.display = 'none';
	}
	document.getElementById('ql_1').innerHTML = readFormData(name);
	document.getElementById('ql_1').style.textDecoration = 'underline';
// amig nincs zip es mentes
	//document.getElementById('ql_3').style.display = 'none';
	//document.getElementById('ql_4').style.display = 'none';

	ShowQuickLayer(obj, SId, isDiv);
}

/*     
	func: save img to fototar
	called by:  
	param: 
*/

function readSaveImg(resp, cid, uid){

	if (resp != null) {
		var obj = top.utilsDataParse(resp);
		if (obj.ret) {
			if (obj.ret == 4){
				//sync
				setTimeout('window.readInit()',10);
			}
			return;
		}
		setTimeout('parent.outerCloseOverlayFM()',1);
		return;
	}
	var my_ct = parent.document.getElementById('overlayMsg_content');
	my_ct.innerHTML = '<div class="msgtext">Kép mentése folyamatban...</div>';
	parent.outerOpenOverlayFM();

	sUri = "cmd=imgtofototar&tid="+top.tid+"&ulid="+top.ulid+"&id="+top.id+"&cid="+cid+"&uid="+uid;
	top.loadXmlFile(sUri, 'top.outer.document.getElementById(\'readIFrame\').contentWindow.readSaveImg', 'fm', null, true);
}

