var timeout	= 700;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id) {	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	if(ddmenuitem) ddmenuitem.style.visibility = 'visible';
}
// close showed layer
function mclose() {
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;

// Go Location in Opener

function goURI(adress) {
	window.opener.location.href = adress;
	window.close;
	window.opener.focus();
}

function openInPopup(url) {
	popup = window.open(url,'veoliaPopup','width=620,height=650,resizable=no,scrollbars=yes');
	popup.focus();
}

function showBox() {
	Element.setStyle($('APOVERLAY'),{
        visibility: 'visible'
	});
}
function hideBox() {
	Element.setStyle($('APOVERLAY'),{
        visibility: 'hidden'
	});
} 


/* MOOTOOLS Variante - Veraltet */

/*function showBox() { $('APOVERLAY').setStyle('visibility','visible'); }
function hideBox() { $('APOVERLAY').setStyle('visibility','hidden'); } */

/*
window.addEvent('domready', function() {
	allPopupLinks = $$('#CONTAINER a.PopupLink');
	
	if(allPopupLinks) {
		count = allPopupLinks.length;
		
		for(i = 0; i < count; i++) {
			if(allPopupLinks[i].addEvent) {
				allPopupLinks[i].addEvent('click', function(event) {
					event.stop();
					
					if (Browser.Engine.trident) { //...do something for all other version of IE
						openInPopup(this.get("href"));
					} else { //...everyone else gets some standard behavior
						openInPopup(this.get("href"));
					}
				});
			}
		}
	}
});
*/

/**
 *	Ansprechpartner Overlay "Ajax" Request Script
 *
 *

function checkForString(formid, input, targetDiv){
	
	var userInput = $(input).getValue();
	alert(userInput);
	if(isNumeric($(input))){
		//$(input).name = "ZipZipcode";
		//alert("Number found!");
		window.location = "/plz-ansprechpartner/Zipcode/SearchForm?ZipZipcode="+userInput
	}else{
		new Ajax.Updater(targetDiv, '/ZipHelper/process/'+userInput, {
			//parameters: { text: $F('text') }
		});



	}
}
 */
function ReadCookie(cookieName) {
 var theCookie=" "+document.cookie;
 var ind=theCookie.indexOf(" "+cookieName+"=");
 if (ind==-1) ind=theCookie.indexOf(";"+cookieName+"=");
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(";",ind+1);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+2,ind1));
}

/**
 *  checkForString
 *  @param fromid String Form which calls function
 *  @param tragetDiv String ID of the Div which receives UL List.
 *  @return HTML UL List with Links
 */
function checkForString(formid, targetDiv){
	
	var userInput = document.forms[formid].elements['ZipZipcode'].value;
    //alert(ReadCookie("PHPSESSID"));
    
    /**
     *  Ermöglicht eine Suche ohne Eingabe, alle
     *  Einträge werden dann gezeigt
     */
    if(userInput.length >= 1){
    
        if(document.forms[formid].elements['SecurityID']){
            var secID = document.forms[formid].elements['SecurityID'].value;
        }else{
            var secID = '';
        }
        
        if(isNumeric(document.forms[formid].elements['ZipZipcode'])){
            // Es ist ein Zahl
            //document.forms[formid].submit();
            if(secID != ''){
                window.location = "/plz-ansprechpartner/Zipcode/SearchForm?ZipZipcode="+userInput+"&SecurityID="+secID;
            }else{
                window.location = "/plz-ansprechpartner/Zipcode/SearchForm?ZipZipcode="+userInput;
            }
        }else{
            //alert(userInput);
            //alert('/ZipHelper/process/ASP/'+encodeURI(userInput)+'&s='+secID);
            if(secID != ''){
                new Ajax.Updater(targetDiv, '/ZipHelper/process/ASP/'+encodeURI(userInput)+'&s='+secID, {
                    //parameters: { text: $F('text') }
                    
                });
                if(formid == "Form_SearchForm"){
                    if(document.getElementById("plzSearchResults")){
                        document.getElementById("plzSearchResults").style.display = "none";
                    }
                }
            }else{
                new Ajax.Updater(targetDiv, '/ZipHelper/process/ASP/'+encodeURI(userInput), {
                    //parameters: { text: $F('text') }
                    
                });
                if(formid == "Form_SearchForm"){
                    if(document.getElementById("plzSearchResults")){
                        document.getElementById("plzSearchResults").style.display = "none";
                    }
                }
            }
        }
        
    } else {
        window.location = "/plz-ansprechpartner/Zipcode/SearchForm?ZipZipcode=";
    }
    
}

// If the element's string matches the regular expression it is all numbers
function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}

/**
 *  getPLZforStandort
 *  @param formID String ID of Form which calls this function
 *  @param targetDiv String ID of Div which will display the HTML List
 *  @return HTML UL List with Links
 */
function getPLZforStandort(formID, targetDiv){
    // Get Value from Input Field
    var userInput = document.forms[formID].elements['StandortSuche'].value;
    var userSelect = document.forms[formID].elements['Dienstleistungsbereich'].value;
    
    if(isNumeric(document.forms[formID].elements['StandortSuche'])){
        // Redirect on ZIPcode Input
        window.location = "/standort-ergebnisse/?Dienstleistungsbereich="+userSelect+"&StandortSuche="+userInput;
    }else{
        if(userInput.length == 0 || userInput == 'Ihre PLZ oder Ort'){
            window.location = "/standort-ergebnisse/?Dienstleistungsbereich="+userSelect+"&StandortSuche=";
            return true;
        }
        // Make Ajax Request
        new Ajax.Updater(targetDiv, '/ZipHelper/process/STD/'+encodeURI(userInput)+'/'+encodeURI(userSelect), {
            
		});
        if(document.getElementById("standortSearchResults")){
            document.getElementById("standortSearchResults").style.display = "none";
        }
    }
}





