/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++ OeKB - IIC AUSTRIA +++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


/******************************************************************/
/**   Dokument search forms functions   				*/
/******************************************************************/

function changeInput(invisible, visible) {
   document.getElementById(invisible).style.display = "none";
   document.getElementById(visible).style.display = "inline";
   if(invisible == 'searchchange-type-select'){
	   document.getElementById('searchForm:emittentNameSuche').value = 'true';
   }else{
   	    document.getElementById('searchForm:emittentNameSuche').value = 'false';	
   }
}


function toggleTabLeft(){
		document.getElementById('tab-left').className='active-left';
		document.getElementById('tab-right').className='inactive-right';
		document.getElementById('search-stichtwort').style.display='none';
		document.getElementById('search-status').style.display='none';		
		document.getElementById('search-isinbezug').style.display='none';			
		document.getElementById('search-time2').style.display='none';					
		document.getElementById('searchForm:erweitereSuche').value = 'false';
		
		setMessagesDisplay('none');
		
	};
	
	function toggleTabRight(){
		document.getElementById('tab-left').className='inactive-left';
		document.getElementById('tab-right').className='active-right';
		document.getElementById('search-stichtwort').style.display='block';	
		document.getElementById('search-status').style.display='block';	
		document.getElementById('search-isinbezug').style.display='block';	
		document.getElementById('search-time2').style.display='block';			
		document.getElementById('searchForm:erweitereSuche').value = 'true';		

		setMessagesDisplay('');

	}
	
	function setMessagesDisplay(disp){
		var messageList = document.getElementById('oekb-messages');
		var ul = messageList.getElementsByTagName('ul');
		if(ul[0] != null){
			li = ul[0].getElementsByTagName('li');
			if(li != null){
				for(x = 0; x < li.length ; x++){
					if(li[x].id == null || li[x]=='' || !li[x].id.match('js_error:zeitraum')){
						li[x].style.display = disp;
					} 
				}
			}		
		}
	}
	
	function sortDownImg(ddiv){
		document.getElementById(ddiv).style.backgroundImage = "url('images/list_sort_down.gif')";
		document.getElementById(ddiv).style.fontWeight = 'bold';
	}
	
	function sortUpImg(ddiv){
		document.getElementById(ddiv).style.backgroundImage = "url('images/list_sort_up.gif')";
		document.getElementById(ddiv).style.fontWeight = 'bold';
	}		


function toggleInfo(e, openMsg, closeMsg) {
	/* Da IE und Mozilla verschiedene Abfrage bez des class-Attributes verwenden, muss über eine Variable (cls) auf dieses Attribut zugegriffen werden */
        var cls = "class";
        if(navigator.userAgent.indexOf("MSIE")>0) {
                cls = "className";
                if(navigator.userAgent.indexOf("MSIE 8")>0) {
                    cls = "class";
                }
                else if(navigator.userAgent.indexOf("MSIE 9")>0) {
                    cls = "class";
                }
                else if(navigator.userAgent.indexOf("MSIE 1")>0) {
                    cls = "class";
                }
        }

        var node = e.parentNode;
        
		// get the right parentNode
		var counter = 0;
		while(counter < 5) {
			if (node == null) {
				//node = node.previousSibling;
				counter++;
			}	
			else if (node.nodeType != 3 && node.getAttribute(cls) == "mic-message-container") {
                counter = 5;
			}
			else {
				node = node.previousSibling;
                counter++;
			}
		}
				
		
		var displaynodelong = null;
		if(node != null){ 
			displaynodelong = node.lastChild;
		}
		//get the long message node
		
		var counter = 0;
		while(counter < 3) {
			if (displaynodelong == null) {
				//displaynodelong = displaynodelong.previousSibling;
				counter++;
			}	
			else if (displaynodelong.nodeType != 3 && displaynodelong.getAttribute(cls) == "mic-message-long") {
				counter = 3;
			}
            
            else {
				counter++;
				displaynodelong = displaynodelong.previousSibling;
			}
		}
		
		
		
		var displaynodeshort = null;
		if(displaynodelong != null){
			displaynodeshort = displaynodelong.previousSibling;
		}
		//get the short message node
		var counter = 0;
		while(counter < 3) {
			if (displaynodeshort == null) {
				//displaynodeshort = displaynodeshort.previousSibling;
				counter++;
			}	
			else if (displaynodeshort.nodeType != 3 && displaynodeshort.getAttribute(cls) == "mic-message-short") {
				counter = 3;
			}
            else {
				counter++;
				displaynodeshort = displaynodeshort.previousSibling;
            }
		}
		
		
	if (displaynodelong != null && displaynodelong.style.display == "none") {
		displaynodeshort.style.display = "none";
		displaynodelong.style.display = "block";
		e.firstChild.nodeValue = closeMsg;
		e.parentNode.style.backgroundImage = "url('images/toggle_info_up.gif')";
		return;
	}
	
	if (displaynodelong != null && displaynodelong.style.display == "block") {
		displaynodeshort.style.display = "block";
		displaynodelong.style.display = "none";
		e.firstChild.nodeValue = openMsg;
		e.parentNode.style.backgroundImage = "url('images/toggle_info.gif')";
		return;
	}
	
}

/******************************************************************/
/**  END Dokument search forms functions   				*/
/******************************************************************/

function toggleLogoutHeader(table_id, headerimg, klikk){
	var obj = document.getElementById(table_id);
	var opened = getCookie('logoutOpened');
	if(opened != null && opened == 'true' && klikk == false){
		obj.style.display = '';
		document.getElementById(headerimg).src = 'images/h_logged_01.gif'		
	}
	if(klikk == true){ 
		if(obj.style.display == 'none'){
			obj.style.display = '';
			document.getElementById(headerimg).src = 'images/h_logged_01.gif'
			setCookie('logoutOpened','true');
		}
		 else
		{
			obj.style.display = 'none';
			document.getElementById(headerimg).src = 'images/h_logged_02.gif'
			deleteCookie('logoutOpened');
		}	
	} 
}

function toggleContactInfo(e) {


        /* Da IE und Mozilla verschiedene Abfrage bez des class-Attributes verwenden, muss über eine Variable (cls) auf dieses Attribut zugegriffen werden */
        var cls = "class";
        if(navigator.userAgent.indexOf("MSIE")>0) {
                cls = "className";
        }

        var node = e.parentNode;
        var displaynode = node.nextSibling;


        /*      erste While-Schleife setzt die displaynode (die zu-/aufklappbare Box). IE und Mozilla bilden verschiedne DOM-Bäume.
                Daher muss auf den Kontentyp auf einen Textknoten (3) überprüft werden (else if).
        */
        var counter = 0;
        while(counter < 3) {
                if (displaynode == null) {
                        displaynode = displaynode.nextSibling;
                        counter++;
                }
                else if (displaynode.nodeType != 3 && displaynode.getAttribute(cls) == "toggle") {
                        counter = 3;
				}
                else {
                        displaynode = displaynode.nextSibling;
                        counter++;
                }
        }

        /*      zweite while-Schleife setzt nun die Sichtbarkeit und das Hintergrundbild */

        var count = 0;
		
        while(count < 3){
                if (displaynode.getAttribute(cls) == "toggle") {
                        if (displaynode.style.display == "none") {
                              displaynode.style.display = "block";
                              e.style.backgroundImage = "url('images/toggle_info_up.gif')";
							  e.firstChild.nodeValue = "Kontaktdetails ausblenden";
								
                                return;
                        }
                        if (displaynode.style.display == "block") {
                                displaynode.style.display = "none";
                                e.style.backgroundImage = "url('images/toggle_info.gif')";
							   
								e.firstChild.nodeValue = "Kontaktdetails einblenden";
                                return;
                        }
                }
                else{
                        node = node.parentNode;
                        count++;
                }
        }

}



function clickLink(linkId)
{
  var fireOnThis = document.getElementById(linkId)
  if (document.createEvent)
  {
    var evObj = document.createEvent('MouseEvents')
    evObj.initEvent( 'click', true, false )
    fireOnThis.dispatchEvent(evObj)
  }
  else if (document.createEventObject)
  {
    fireOnThis.fireEvent('onclick')
  }
}



function showBlockerWindow() {
	//document.body.style.overflow = 'hidden';

	var maskWidth = document.body.scrollWidth;
	
	if ( maskWidth < getWindowWidth() ) {
		maskWidth = getWindowWidth();		
	}

	var maskHeight = document.body.scrollHeight;
	if ( maskHeight < getWindowHeight() ) {
		maskHeight = getWindowHeight();
	}

	var maskStyle = document.getElementById( 'maskLayer' ).style;
	maskStyle.left = '0px';
	maskStyle.top = '0px';
	maskStyle.width = '' + maskWidth + 'px';
	maskStyle.height = '' + maskHeight + 'px';
	maskStyle.display = 'block';
	var wndStyle = document.getElementById( 'windowLayer' ).style;
	wndStyle.display = 'block';
}

function hideBlockerWindow() {
	var wndStyle = document.getElementById( 'windowLayer' ).style;
	wndStyle.display = 'none';
	var maskStyle = document.getElementById( 'maskLayer' ).style;
	maskStyle.display = 'none';
	//document.body.style.overflow = 'auto';
}

	function getWindowWidth() {
		  var myWidth = 0, myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
		  }
		  return myWidth;
		}

		function getWindowHeight() {
		  var myWidth = 0, myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
		  }
		  return myHeight;
	}
	

function formatDate(field){
	if(field.value == null || trim(field.value) == ''){
		return true;
	}
	var value = field.value.split('\.');
	var now = new Date();
	var curryear = now.getFullYear().toString().substr(2);
	
	var currmonth = zf(now.getMonth()+1, 2);

	if(value.length >= 3){
		curryear = zf(value[2],2);
	}
	if(value.length >= 2){
		currmonth = zf(value[1],2);
	}
	if(value[0].length > 6){
			return false;							
	}
	var day = zf(now.getDate(), 2);
	if(value[0].length >= 5){
		curryear = zf(value[0].substr(4),2);
	}
	if(value[0].length >= 3){
	    currmonth = zf(value[0].substr(2,2),2);
	}
	day = zf(value[0].substr(0,2),2);
	
	var int_year = parseInt(curryear,10);
	var int_month = parseInt(currmonth,10);
	var int_day = parseInt(day,10);
	
	var year = int_year + 2000;
	if(	!curryear.match(/^\d+$/)
		|| !currmonth.match(/^\d+$/)
		|| !day.match(/^\d+$/)
		|| int_year == 0
		|| int_month == 0
		|| int_day ==0
		|| int_month > 12 				
		|| currmonth.length > 2 
		|| int_day > daysInMonth(int_month, year) 
		|| day.length > 2){
				
		return false;
	}
	
	var format = day + '.' + currmonth + '.' + curryear;	
	field.value = format;
	return true;
}

function zf(num, width) {
  num = num.toString();
  while (num.length < width){
	  num = '0' + num;
	 }
  return num;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function daysInMonth(month,year) {
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 2) return m[month - 1];
	if (year%4 != 0) return m[1];
	if (year%100 == 0 && year%400 != 0) return m[1];
	return m[1] + 1;
}




	function validateDate(field,labelId,fieldName,p){
		    var messageid = 'js_error:'+labelId;
			if(!formatDate(field)){
				field.focus();
				document.getElementById(labelId).className = 'oekb-errortext';
				
				var li0 = document.getElementById(messageid);
				
				if(li0 == null){
					var messageList = document.getElementById('oekb-messages');
					var ul = messageList.getElementsByTagName('ul');
					if(ul[0] == null){
						var ul0 = document.createElement('UL');
						messageList.appendChild(ul0);
						ul = messageList.getElementsByTagName('ul');
					}
					var li = document.createElement('LI');
					li.id = messageid;
					var span = document.createElement('SPAN');
					span.className = 'messages';
					var message = document.createTextNode(fehler + ' ' + fieldName + ' - ' + dateerror );
					span.appendChild(message);
					li.appendChild(span)
					ul[0].appendChild(li);
				}
				return false;	
			} else { 
			    document.getElementById(labelId).className = '';
				var messageList = document.getElementById('oekb-messages');
				var ul = messageList.getElementsByTagName('ul');
				if(ul[0] != null){
					var li0 = document.getElementById(messageid);
					if(li0 != null){
						ul[0].removeChild(li0);
					}
				}							 
			}
			return true;   			
		}
		
		function formatDates(){
			var returnVal = false;
			var ret = true;
			returnVal = validateDate(document.getElementById('searchForm:fakeZeitraumVon'), 'zeitraumLabelVon','#{msg.search_meldedatum_von}');
			if(returnVal){
				document.getElementById('searchForm:zeitraumVon').value = document.getElementById('searchForm:fakeZeitraumVon').value;
			} else {
				ret = false;
			}
			returnVal = validateDate(document.getElementById('searchForm:fakeZeitraumBis'),'zeitraumLabelBis','#{msg.search_meldedatum_bis}');
			if(returnVal){
				document.getElementById('searchForm:zeitraumBis').value = document.getElementById('searchForm:fakeZeitraumBis').value;
			} else {
				ret = false;
			}
			var es = document.getElementById('searchForm:erweitereSuche').value == 'true';

			if(es){			
				returnVal = validateDate(document.getElementById('searchForm:fakegultigkeitsVon'), 'gueltigkeitsLabelVon','#{msg.search_gultigkeitsdatum_von}');
				if(returnVal){
					document.getElementById('searchForm:gultigkeitsVon').value = document.getElementById('searchForm:fakegultigkeitsVon').value;		
				} else {
					ret = false;
				}
				returnVal = validateDate(document.getElementById('searchForm:fakegultigkeitsBis'),'gueltigkeitsLabelBis','#{msg.search_gultigkeitsdatum_bis}');
				if(returnVal){
					document.getElementById('searchForm:gultigkeitsBis').value = document.getElementById('searchForm:fakegultigkeitsBis').value;				
				} else {
					ret = false;
				}
			} else {
				document.getElementById('searchForm:gultigkeitsVon').value = '';
				document.getElementById('searchForm:gultigkeitsBis').value = '';
			}
			 
			if(ret){
				showBlockerWindow();
			}

			return ret;
		}
		

function openDetail(id){
	document.getElementById('dokId').value = id;
	w = window.open('','mywin','width=655,height=470,menubar=yes');
	document.getElementById('detailForm').target = 'mywin';	
	clickLink('detailLink');
}	
