var personalise=['idEmail','idNick'];

// --------------
// Menu functions
// --------------

// Admin = 1
// Config = 2
// Rapp = 4
// Natofice = 8
// View = 16
// Trans = 32

var ROOTWIDTH=4;

var aMenuItems = [
// Symbol, Menu frame, Body frame, Combo, Right flag
	['HOM', 'mainmenu.html', 'overview.htm', 'Home', 63],
	['IPC', 'ipc.html', 'overview.htm', 'IPC View/Search', 55],
	['ANN', 'amendments_ipc.html', 'amendments_overview.htm', 'Annex View/Search', 55],
	['ERR', 'error_form.html', 'error_overview.htm', 'Error View/Search', 15],
	['ADM', 'adminintmenu.html', 'xmlframeset.htm', 'Administrator interface', 1],
	['CWE', 'cw_menu.html', plbase +'cw.xmlget', 'Catchword Editor', 5],
	['RCL', 'rcl_menu.html', 'blank.htm', 'RCL Editor', 5],
	['COM', 'compilationfilemenu.htm', 'compilationfileoverview.htm', 'Compilation and merge', 13],
	['VAL', 'validityfilemenu.htm', 'validityfileoverview.htm', 'Validity file', 1],
	['IPF', 'ipcfilemenu.htm', 'ipcfileoverview.htm', 'IPC file', 1],
	['RAP', 'rapporteurintmenu.html', 'xmlframesetrapporteur.htm', 'Rapporteur interface', 13],
	['TRA', 'translationintmenu.html', 'xmlframesetrapporteur.htm', 'Translation interface', 33],
	['DBC', 'databasecheckintmenu.html', 'databasecheckoverview.htm', 'Database check', 1],
//	['ILL', 'empty.htm', 'upload.htm', 'Illustration upload', 29],
	['DEF', 'defeditintmenu.html', 'empty.htm', 'Definition editor', 13],
//	['DEF', 'defeditintmenu.html', 'defeditoverview.htm', 'Definition editor', 2],
	['EOC', 'empty.htm', 'eoc.htm', 'EOC interface', 1],
	['ILB', 'empty.htm', plbase +'pillustr.browse?dmode=hierarchic&symbol=', 'Illustration browser', 1],
	['FIL', 'empty.htm', 'fileframeset.htm', 'File browser', 1]
];

function buildMenu(oSelect, iRights) {
	var aRow;
	for (var i = 0; i < aMenuItems.length; i++) {
		aRow = aMenuItems[i];
		if ((aRow[4] & iRights) != 0)
			oSelect.add(new Option(aRow[3], aRow[0]));
	}
}

function setRipcisCombo(sShortcut){
/*	var iIndex = -1;
	var aRow;
	for (var i = 0; i < aMenuItems.length; i++) {
		aRow = aMenuItems[i];
		if (aRow[0] == sShortcut) {
			iIndex = i;
			break;
		}
	}
	if (iIndex != -1)
		window.parent.frames['combobox'].document.getElementById('idSel').selectedIndex = iIndex;*/
}

function jumpMenu(oThis) {
	var sShortcut = oThis.value;
	var aRow;
	for (var i = 0; i < aMenuItems.length; i++) {
		aRow = aMenuItems[i];
		if (aRow[0] == sShortcut) {
			window.parent.frames['ipc'].location.href = aRow[1];
			window.parent.frames['xml'].location.href = aRow[2];
			break;
		}
	}
}

// --------------

function setResultWindow(){
	if (document.getElementById('jumpModeDisplayMode')!=null && document.getElementById('jumpModeTargetMode')!=null && document.getElementById('jumpModeSymbol')!=null){
		if (parent.xml!=null && parent.xml.document.search!=null){//@SZM
			displaymode = document.getElementById('jumpModeDisplayMode').value
			targetmode = document.getElementById('jumpModeTargetMode').value
			symbol = document.getElementById('jumpModeSymbol').value
//			jumpMode(displaymode,targetmode,symbol,'xml')
			jump(displaymode,symbol,'xml')
		}
	}
}

function hitEnter(event){
	if ( event.keyCode==13 ) {
		return true;
	}else{
		return false;
	}
}

function checkNumChar(o){

	o.value = o.value.replace(/[^a-zA-Z0-9]/g,"")
	o.value = o.value.toUpperCase()
}
function checkChar(o){

	o.value = o.value.replace(/[^a-zA-Z]/g,"")
	o.value = o.value.toUpperCase()
}

function jumpWindow(mode,symbol){
	if( mode=='' || mode==null){
		mode='h';
	}
	var url = plbase + "pipcentry.xmlget?ipcsymbol=" + symbol+"&displaymode="+mode+"#ipc"+symbol;
//	url = taskAddTask(url);
	myWindow = window.open(url,'hitWindow');
	myWindow.focus();
//	taskRemoveTask(url);
}

function jump(mode,symbol,target){
	var  file;
	var thisFile;

	thisFile = window.location+"";

	thisFile = thisFile.substr(thisFile.lastIndexOf('/')+1,ROOTWIDTH).toLowerCase();


	file = symbol.substr(symbol,ROOTWIDTH).toLowerCase();
	if( thisFile == file ){
		window.location = '#ipc' + symbol;
	}else{
		window.location = file.toLowerCase() + '.xml#ipc' + symbol;
	}
return;


//	alert(1);
	if( mode=='' || mode==null){
		mode='h';
	}
	var myWindow;
	if (target!=null){
		eval('myWindow = parent.'+target);
		if (myWindow==null){
			myWindow = window;
		}else{
			eval('myWindow = parent.'+target+'.window');
			if (myWindow==null){
				myWindow = window;
			}
		}
	}else{
	 	eval('myWindow = window')
	}
	if (myWindow != null){
//SZM
	var leftWindow=parent.parent.ipc;
	if (leftWindow!=null){
		mode = leftWindow.getDisplayMode();
	}
		var url = plbase + "pipcentry.xmlget?ipcsymbol=" + symbol+"&displaymode="+mode+'#'+symbol;
		url = taskAddTask(url); // SSZ
		myWindow.location = url
//		prompt('a',url)
	}
}

function jumpDirect(mode,symbol,target){
	jump(mode,symbol,target);
}

function jo(symbol){
	jump("",symbol,"");
}

function jumpDirect1(mode,symbol,target){
	if( mode=='' || mode==null){
		mode='h';
	}

	var myWindow;
	if (target!=null){
		eval('myWindow = parent.'+target);
		if (myWindow==null){
			myWindow = window;
		}else{
			eval('myWindow = parent.'+target+'.window');
			if (myWindow==null){
				myWindow = window;
			}
		}
	}else{
	 	eval('myWindow = window')
	}
	if (myWindow != null){
		var url = plbase + "pipcentry.xmlget?ipcsymbol=" + symbol+"&displaymode="+mode+'#'+symbol;
		url = taskAddTask(url); // SSZ
		myWindow.location = url;
	}
}

function jumpMode(mode,m,symbol,target)
{
	var re;
	re=/[faSh]/;
	mode = mode.replace(re,m);
	jumpDirect(mode,symbol,target);
}

function jh(mode,symbol,target)
{
	var re;
	re=/[faS]/;
	mode=mode.replace(re,"h");
  if (target!=null){
		eval('myWindow = parent.'+target+'.window');
		if (myWindow==null){
			myWindow = window;
		}
	}else{
	 	eval('myWindow = window')
	}
	var sURL = plbase +"pipcentry.xmlget?ipcsymbol=" + symbol+"&displaymode="+mode+"#ipc"+symbol;
	sURL = taskAddTask(sURL);
 	myWindow.location = sURL;
}

function getEntry( id )
{
	var sURL = plbase + "pipcentry.xmlget?displaymode=i&ipcsymbol=" + id+"#ipc"+id;
	sURL = taskAddTask(sURL, false, true);
 	window.open(sURL);
}  // getEntry

function getAmendment( id )
{
	var sURL = plbase + "pipcamend.xmlget?amendmentid=" + id+"#amn"+id;
	sURL = taskAddTask(sURL);
 	window.location = sURL;
}  // getEntry

function getProject( type, projNo, symbol )
{
	var sURL;

	if( symbol != '' && symbol != null ){
 		sURL = (plbase + "pipcamend.xmlget?p_project=" + type + projNo+"#amn"+symbol);
	}else{
 		sURL = plbase + "pipcamend.xmlget?p_project=" + type + projNo;
	}

	sURL = taskAddTask(sURL);
 	window.location = sURL;

}  // getEntry

function getSession( type, sessNo, symbol )
{
	var sURL;

	if( symbol != '' && symbol != null ){
 		sURL = (plbase + "pipcamend.xmlget?p_session=" + type + sessNo+"#amn"+symbol);
	}else{
 		sURL = plbase + "pipcamend.xmlget?p_session=" + type + sessNo;
	}

	sURL = taskAddTask(sURL);
 	window.location = sURL;
}  // getEntry


function getAllEntry( symbol )
{
	var sURL = plbase +"pipcentry.xmlget?displaymode=se*v*n&ipcsymbol=" + symbol+"#ipc"+symbol;
	sURL = taskAddTask(sURL);
 	window.location = sURL;
}  // getEntry

function viewError( errorName )
{
		var sFilter = window.location.href;
		var iPos = sFilter.indexOf('p_filter=');
		if (iPos == -1)
			sFilter = '';
		else {
			sFilter = sFilter.substr(iPos +'p_filter='.length);
			iPos = sFilter.indexOf('&');
			sFilter = '&p_filter=' +sFilter.substr(0, iPos);
		}

		var sURL = plbase +"perror.xmlget?p_name=" + errorName +sFilter;
		sURL = taskAddTask(sURL);
	 	window.location = sURL;

}  // viewErrors( errorName )

function checkIPCSymbol(ipc)
{
	star = false
	if (ipc.charAt(ipc.length-1)=='*'){
		star = true
		ipc = ipc.substring(0,ipc.length-1)
	}
	error = false
	ipc = ipc.toUpperCase()
	ipc = ipc.replace(/ /g,'')
	//ipc = ipc.replace(" ","")
	if (ipc.indexOf("*")>-1){
		return ipc
	}else{
		if (ipc.indexOf("/")>-1 || ipc.indexOf(":")>-1){
			if (ipc.indexOf("/")==5 || ipc.indexOf(":")==5){
				ipc = ipc.substr(0,4) + "00" + ipc.substr(4,1) + ipc.substr(6)
			}
			if (ipc.indexOf("/")==6 || ipc.indexOf(":")==6){
				ipc = ipc.substr(0,4) + "0" + ipc.substr(4,2) + ipc.substr(7)
			}
				if (ipc.indexOf("/")==7 || ipc.indexOf(":")==7){
				ipc = ipc.substr(0,4) + "" + ipc.substr(4,3) + ipc.substr(8)
			}
		}

		switch (ipc.length) {
			case 0:
				break;
			case 1:
				error = ipc.match(/^[A-Z]$/) == null;
				break;
			case 3:
				error = ipc.match(/^[A-Z]\d{2}$/) == null;
				break;
			case 4:
				error = ipc.match(/^[A-Z]\d{2}[A-Z]$/) == null;
				break;
			case 9:
			case 10:
				error = ipc.match(/^[A-Z]\d{2}[A-Z]\d*$/) == null;
				break;
			default:
				error = true;
				break;
		}
/*			if (ipc.charCodeAt(0)>64 && ipc.charCodeAt(0)<74){
				if (ipc.length==2){
					error = true;
				}else if (ipc.length>2){
					if (isNaN(ipc.charAt(1)) || isNaN(ipc.charAt(2))){
						error = true
					}else{
						if (ipc.length>3){
							if (ipc.charCodeAt(0)>64 && ipc.charCodeAt(0)<91){
								if (ipc.length>4){
									if (ipc.substr(4).match(/^\d*$/g)){
									}else{
										error = true
									}
								}
							}else{
								error = false
							}
						}
					}
				}
			}else{
				error = true
			}*/
	}
	if (error){
		return 'error'
	}else{
		if (star){
			ipc = ipc + '*'
		}
		return ipc
	}
}

function deleteErrorList( errorName )
{
	var bConf = confirm('Are You sure You want to delete the errors of ' + errorName + '?');
	if (bConf){
		var sUrl = plbase + 'perror.deleteErrorList?p_name=' + errorName;
//		alert(sUrl);
		var ret = getTextFromServer(sUrl);
		document.location=plbase + 'perror.xmlget'
	}
}  // viewErrors( errorName )

function getTimeDifference()
{
	parent.ipc.getDifference();
}  // getTimeDifference

function link(kind){
	if ( kind=="admin" ) {
		parent.document.location="http://" + getDocumentDomain() + "/forms90/f90servlet?config=		ripcis";
	}
	if ( kind=="errors" ) {
		parent.document.location="http://" + getDocumentDomain() + "/forms90/f90servlet?config=		error_appl";
	}
}
/*
function MM_jumpMenu(targ,selObj,restore){
	if (selObj.options[selObj.selectedIndex].value.indexOf('javascript') > -1) {
		var str = selObj.options[selObj.selectedIndex].value +"";
		eval(str);
	} else {
		eval(targ +".location='" +selObj.options[selObj.selectedIndex].value +"'");
	}
}
*/
function checkResult(){
	//alert('result');
	window.opener.myRefresh();
}

function new_sendTextToServer(html){
	var a32k = get32kArray(html);
	var myItem = document.editor;
	for (var i=0;i<a32k.length;i++){
		eval('myItem.htmlsource'+i+'.value=a32k[i]');
	}
	myItem.submit();
}

function sendTextToServer(html){
	var a32k = get32kArray(html);
    var sb=getIFrameDocument('sendBox');
	sb.write ('<form action="' +plbase +'!phtml2xml.set_html" method="post" name="htmltext" id="htmltext">');
	for (var i=0;i<a32k.length;i++){
		if (i>9){
			alert('not enough fileds....')
		}else{
			sb.write ('<input name="p_htmlsource'+i+'" type="hidden" value="">')
		}
	}
	sb.write('</form>');
	sb.close();
	var myItem;
	try{
		myItem = getIFrameDocument('sendBox').all.htmltext;
	}catch(e){
		myItem = getIFrameDocument('sendBox').getElementById('htmltext');
	}
	for (var i=0;i<a32k.length;i++){
		eval('myItem.p_htmlsource'+i+'.value=a32k[i]');
	}
	getIFrameDocument('sendBox').getElementById('htmltext').submit();
	alert('File has been sent to the server');
}

function get32kArray(html){
	var c = 32000;
	var myArray = new Array();
	var i=0;
	for (i=0;html.length>c;i++){
		myArray[i]=html.substring(0,c-1);
		html = html.substring(c-1);
	}
	myArray[i]=html.substring(0);
	return myArray;
}

function getIFrameDocument(aID){
	if (document.getElementById(aID).contentDocument){
    	return document.getElementById(aID).contentDocument;
    }else{
        return document.frames[aID].document;
    }
}

function ipcPosition()
{
}

function ipcPositionSet()
{
}

function checkIPC4(myInput,useTruncation){
	var area = myInput.value
	if ( useTruncation != null ) {
		if ( area.substr(area.length-1,1) == '*' ) {
			area=area.substr(0,area.length-1);
		}
	}
	var error = !checkIPC4value(area);
	if (error){
		alert('Bad symbol!');
		//myInput.focus();
	}else{
		area = myInput.value;
		area=area.toUpperCase();
		myInput.value = area;
	}
}

function checkSectionList(myInput){
	var sections = myInput.value.toUpperCase();
	var error=false;
	for(i=0;i < sections.length;i++){
		if ( sections.substring(i,i+1)<'A' || sections.substring(i,i+1)>'I') {
			error=true;
		}
	}
	if (error){
		alert('Bad section list!');
		//myInput.focus();
	}else{
		myInput.value = sections;
	}
}

function checkIPC4value(area){
	error = false;
	area = area.toUpperCase()
		if (area.length>0) {
			if (area.charCodeAt(0)>64 && area.charCodeAt(0)<74){
				if (area.length==2){
					error = true;
				}else if (area.length>2){
					if (isNaN(area.charAt(1)) || isNaN(area.charAt(2))){
						error = true
					}else{
						if (area.length>3){
							if (area.charCodeAt(0)>64 && area.charCodeAt(0)<91){
								if (area.length>4){
									error = true
								}
							}else{
								error = false
							}
						}
					}
				}
			}else{
				error = true
			}
		}
	return !error;
}

function editTransAmendments(sProject, sArea, sOrder, sState) {
	window.parent.tabset.setDetails(sProject, sArea, sOrder, sState);
}

function fillFields(){
	for (i=0;i<personalise.length;i++){
		var myObj=document.getElementById(personalise[i])
		if (myObj!=null){
			myVal = GetCookie(personalise[i]);
			if (myVal!=null)
			myObj.value = myVal;
		}
	}
}

function savePersonalSettings(){
	var b;
	for (i=0;i<personalise.length;i++){
		SetCookie(personalise[i],document.getElementById(personalise[i]).value)
	}
}

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}

var iIPCImageDivVisLevel = 0;

function showIPCImageDiv(sSrc) {
	if (iIPCImageDivVisLevel == 0) {
		var oDiv = document.getElementById('idImgDiv');

		oDiv.style.top = document.body.scrollTop +event.y;
		oDiv.style.left = document.body.scrollLeft +event.x;
		oDiv.style.visibility = '';

		var oA = document.getElementById('idImgDiv_href1');
		oA.href = sSrc +'&fileformat=skc';
		var oA = document.getElementById('idImgDiv_href2');
		oA.href = sSrc +'&fileformat=cdx';
		var oA = document.getElementById('idImgDiv_href3');
		oA.href = sSrc +'&fileformat=bmp';
		var oA = document.getElementById('idImgDiv_href4');
		oA.href = sSrc +'&fileformat=gif';
	}
	iIPCImageDivVisLevel++;
}

function hideIPCImageDiv() {
	setTimeout('hideIPCImageDone()', 50);
}

function hideIPCImageDone() {
	iIPCImageDivVisLevel--;
	if (iIPCImageDivVisLevel == 0) {
		var oDiv = document.getElementById('idImgDiv');
		oDiv.style.visibility = 'hidden';
	}
}


function jumpLang(lang,symbol){
	var  file;
	var thisFile;

	thisFile = window.location+"";

	thisFile = thisFile.substr(thisFile.lastIndexOf('/')+1,ROOTWIDTH).toLowerCase();


	file = symbol.substr(symbol,ROOTWIDTH).toLowerCase();
	window.location = '../../'+lang.toLowerCase()+'/xml/'+file.toLowerCase() + '.xml#ipc' + symbol;
return;
}

function jumpLevel(level,symbol)
{
	var  file;
	var thisFile;

	thisFile = window.location+"";

	if( level=='advanced' ){
		file = thisFile.replace(/[\x2f]core[\x2f]/,"/"+level+"/");
	}else{
		file = thisFile.replace(/[\x2f]advanced[\x2f]/,"/"+level+"/");
	}
	window.location = file.replace(/#.*$/,"#ipc"+symbol);

return;
}

