﻿// JQuery-Definition der interaktiven Elemente.
// $(document).ready(function(){ // GP 20110901: hinzugefuegt
$(document).ready(InitJQuery);
function pageLoad(sender, args) { if (args.get_isPartialLoad()) { ReBindJQuery(); } }

function InitJQuery() {
    // Klassen zum Verstecken werden erst hier hinzugefuegt, damit nichts verschwindet, wenn es kein Skripting gibt
    $("div.clickable:not(.open), div.box_pflichtfelder:not(.open)").addClass("closed");

    ReBindJQuery();
}

function ReBindJQuery() {
			// *********************
			// FIRMEN A-Z SUCHE ****
			// *********************

			// Suchoptionen Auswahl
			$("div.suche_optionen ul li a").click(function(){
				$(this).closest("ul").children("li").removeClass("aktiv");
				$(this).closest("li").addClass("aktiv");
			});

			// Suchstil Auswahl
			$("ul.suche_stil li a").click(function(){
			    $("div.suchformular div.suchstil").removeClass("aktiv"); // Original: slideUp(400).removeClass("aktiv")
				// $("div.suchstil input.input_text").attr('value', ''); // leert die Textboxen
				// $("div.suchstil span.link_zertifikate").html('Über Auswahl hinzufügen'); // schreibt Standardtext zurueck in Zertifikate
				if($(this).hasClass("suchstil_einfach")){
				    $("div.suchformular div.suche_einfach").addClass("aktiv"); // Original: slideDown(400).addClass("aktiv")
				}
				if($(this).hasClass("suchstil_erweitert")){
				    $("div.suchformular div.suche_erweitert").addClass("aktiv"); // Original: slideDown(400).addClass("aktiv")
				}
				if($(this).hasClass("suchstil_experte")){
				    $("div.suchformular div.suche_experte").addClass("aktiv"); // Original: slideDown(400).addClass("aktiv")
				}
				if($.browser.msie && $.browser.version <= 7 ) { // behebt IE7 Problem
					$("div.suchformular > div.aktiv").fadeTo(0,0.99);
					$("div.suchformular > div.aktiv").fadeTo(0,1);
				}
			});


			// ***************************
			// FIRMEN A-Z SUCHE POPUP ****
			// ***************************

			// Popup aufrufen
			$(".link_popup").click(function () {   // Original (Änderung wg. Task 14181): $(".suchformular .link_popup").click(function(){
				var winheight = $(window).height();
                var bodyheight = $("body").height();
                if (bodyheight > (winheight - 20)) { // verhindert ein Springen des Hauptfensters beim Ausblenden des Scrollbars
                    $("html").addClass("no_scrolling"); // verhindert Scrolling im Hauptfenster
                    if ($.browser.mozilla || $.browser.opera) { // Firefox + Opera
                        $("body").css('margin-right', '16px'); // liefert Abstand anstelle Scrollbar
                    }
                    else {
                        $("body").css('margin-right', '17px'); // IE, Safari + Rest
                    }
                }
                
                $(".suche_popup_opacity").fadeTo(600,0.8);
				$(".suche_popup").fadeTo(600,1);
				// $("html").addClass("no_scrolling"); // verhindert Scrolling im Hauptfenster
			});
            
			// Schliessen Buttons
			$(".suche_popup .close_button, .suche_popup .link_cancel, .suche_popup_footer input.button").click(function(){
				$(".suche_popup_opacity").fadeOut(600);
				$(".suche_popup").fadeOut(600, function () {
                    $("html").removeClass("no_scrolling"); // fuegt Scrolling im Hauptfenster wieder hinzu
                    $("body").css('margin-right', '0'); // entfernt Abstand anstelle Scrollbar                
                });
				// $("html").removeClass("no_scrolling"); // verhindert Scrolling im Hauptfenster
            });

            // Cleanup beim Popup-Schließen.
            $(".suche_popup .close_button").click(function () {
                eval($(".suche_popup .link_cancel").attr("href"));
            });
            
			// Aufklapplisten
			$("div.aufklappen").click(function(){
				$("ul.auswahl_liste li.dropnav_folder").addClass("offen");
			});
			$("div.zuklappen").click(function(){
				$("ul.auswahl_liste li.dropnav_folder").removeClass("offen");
			});
			$("li.dropnav_folder > div.drop_icon, li.dropnav_folder > div.drop_text").click(function(){
				$(this).parent("li.dropnav_folder").toggleClass("offen");
			});


			// ***********************
			// FIRMEN A-Z WARTUNG ****
			// ***********************

			// Klassen zum Verstecken werden erst hier hinzugefuegt, damit nichts verschwindet, wenn es kein Skripting gibt
			// $("div.clickable:not(.open), div.box_pflichtfelder:not(.open)").addClass("closed");

			// Box oeffnen / schliessen
			$("div.clickable > h3").click(function(){
				$(this).parent("div.clickable").toggleClass("closed");
			});

			// Info anzeigen / verbergen
			$("a.info").click(function(){
				$(this).children(".info_message").fadeIn(200);
			});
			$("a.info").mouseleave(function(){
				$(this).children(".info_message").fadeOut(200);
			});

			// Delete anzeigen / verbergen
			$("div.link_delete span.delete_text").click(function(){
				$(".delete_message").fadeOut(200);
				$(this).next(".delete_message").fadeIn(200);
			});
			$("a, input, select, div.clickable h3").click(function(){
				$(".delete_message").fadeOut(200);
			});

			// Bild bearbeiten anzeigen / verbergen
			$("div.logo_admin div.logo_text").click(function(){
				$(".logo_linkbox").fadeOut(1);
				$(this).next(".logo_linkbox").fadeIn(200);
			});
			$("div.logo_admin div.logo_link > div.logo_image_frame img:not(.logo_is_new)").click(function(){
				$(".logo_linkbox").fadeOut(1);
				$(this).parent(".logo_image_frame").next(".logo_linkbox").fadeIn(200);
			});
            $("a, div.clickable h3").click(function () {
            //$("a, input, div.clickable h3").click(function(){			
            	$(".logo_linkbox").fadeOut(200);
			});

			// Volle Offenlegung anzeigen / verbergen
			if($("div.box_offenlegungspflicht input[type=checkbox]").attr("checked")) {
				$("div.box_pflichtfelder").removeClass("closed");
			}
			$("div.box_offenlegungspflicht input[type=checkbox]").click(function(){
				$("div.box_pflichtfelder").toggleClass("closed");
			});

			// Produkte & Leistungen Checkboxes
			var elements = $("div.box_edit_inhalt input:checked");	// Checkboxes selektieren
			$.each(elements, function(index, item) {				// Schleife über die einzelnen Elemente
				$(this).parent("li").addClass("aktiv");
			});
			$("div.box_edit_inhalt input[type=checkbox]").click(function(){
				$(this).parent("li").toggleClass("aktiv");
			});

			//Zertifikate, ECG: letzten bottom-border + padding-bottom der jeweiligen Box entfernen
			$("body#faz_zertifikate .box_zertifizierungen .box0").last().css('border-bottom', 'none').css('padding-bottom', '0');
			$("body#faz_ecg .box_unternehmensdaten .box0").last().css('border-bottom', 'none').css('padding-bottom', '0');
			$("body#faz_ecg .box_kontaktinfos .box0").last().css('border-bottom', 'none').css('padding-bottom', '0');
			$("body#faz_ecg .box_pflichtfelder .box0").last().css('border-bottom', 'none').css('padding-bottom', '0');

			/* Kalender */
			if($("body").attr("id") == ("faz_zertifikate")) {	// nur auf Zertifikate Seite
				$(".datepicker_simple").datepicker({
					changeMonth: true,
					changeYear: true,
					showWeek: true,
					showOn: "button", buttonImage: "../Library/img/calendar/calendar.png", buttonImageOnly: true, buttonText:"Datum einfügen",
					showAnim:"blind",
					numberOfMonths: 1
				});
			}

}



// Variables
var STANDARD_PARAMS = "status=no,resizable=no,status=no,toolbar=no,location=no"

var IMAGE_UPLOAD_NEW_PARAMETERS = "Height=260,Width=420,scrollbars=no," + STANDARD_PARAMS;
var IMAGE_UPLOAD_EDIT_PARAMETERS = "Height=590,Width=420,scrollbars=no," + STANDARD_PARAMS;

var GEOCOORDS_TUNING_PARAMETERS = "Height=632,Width=520,scrollbars=no," + STANDARD_PARAMS;

var BRANCHEN_PARAMETERS = "Height=584,Width=680,scrollbars=yes," + STANDARD_PARAMS;
var STANDORT_PARAMETERS = "Height=584,Width=760,scrollbars=yes," + STANDARD_PARAMS;
var PRODUKT_PARAMETERS = "Height=584,Width=660,scrollbars=yes," + STANDARD_PARAMS;
var ZERTIFIKAT_PARAMETERS = "Height=584,Width=584,scrollbars=no," + STANDARD_PARAMS;
var THEMEN_PARAMETERS = "Height=584,Width=584,scrollbars=yes," + STANDARD_PARAMS;

var HELP_PARAMETERS = "Height=190,Width=590," + "status=no,resizable=no,scrollbars=yes,status=no,toolbar=no";
var EMAIL_PARAMETERS = "scrollbars=yes,status=no,resizable=no,status=no,toolbar=no,width=400,height=590";

// Popups
function openBranchenPopup(strLocation){            
    window.open(strLocation, "BranchenPopup", BRANCHEN_PARAMETERS); 
}

function openStandortPopup(strLocation) {            
    window.open(strLocation, "StandortPopup", STANDORT_PARAMETERS); 
}

function openProduktPopup(strLocation) {            
    window.open(strLocation, "ProduktPopup", PRODUKT_PARAMETERS); 
}

function openZertifikatsPopup(strLocation) {            
    window.open(strLocation, "ZertifikatsPopup", ZERTIFIKAT_PARAMETERS); 
}

function openThemenPopup(strLocation){
        
    window.open(strLocation, "ThemenPopup", THEMEN_PARAMETERS); 
}

function openImageUploadPopupNew(strLocation){

    window.open(strLocation, "ImageUploaderPopupNew", IMAGE_UPLOAD_NEW_PARAMETERS);
}

function openImageUploadPopupEdit(strLocation){

    window.open(strLocation, "ImageUploaderPopupEdit", IMAGE_UPLOAD_EDIT_PARAMETERS);
}

function openGeoCoordsTuningPopup(strLocation){

    window.open(strLocation, "GeoCoordsTuningPopup", GEOCOORDS_TUNING_PARAMETERS);
}

function openHelpPopup(strLocation){

    window.open(strLocation, "HelpPopup", HELP_PARAMETERS);
}

function openEmailPopup(strLocation){
    window.open(strLocation, "EmailForm", EMAIL_PARAMETERS);
}

// Treeview
/*
function postBackByTreeviewCheckbox()
{
    var o = window.event.srcElement;
    if (o.tagName == "INPUT" && o.type == "checkbox")
    {
        o.checked = !o.checked;
        //__doPostBack("","");
    }
}
*/

function postBackByTreeviewCheckbox(e) 
{
    var objElement;

    try 
    {        
        objElement = window.event.srcElement;
    }
    catch (Error) 
    {
    }

    if (objElement != null) 
    {
        if (objElement.tagName == "INPUT" && objElement.type == "checkbox") 
        {
            // __doPostBack("", "");                        
            var theChecboxEvent = $(objElement).siblings("a").attr("href");                                           
            eval(theChecboxEvent);
        }
    }
    else 
    {
        if (e != null) 
        {
            if (e.target.toString() == "[object HTMLInputElement]") 
            {
                // __doPostBack("", "");                
                var theChecboxEvent = $(e.target).siblings("a").attr("href");
                eval(theChecboxEvent);
            }
        }
    }
}

function popupClose()
{
    if (opener != null) { opener.focus(); }
    window.close();
}

function popupCloseAndPostbackMainPage()
{           
    if(opener!=null && !opener.closed){ 
        opener.focus(); 
    }
    if(opener.postBackOnMainPage){  
        opener.postBackOnMainPage(); //Dominik Amon, 2007-01-02, Check if that method exists! 
    } 
    
    self.close();       
}

// Misc
function defaultAction() 
{ 
    window.focus(); 
    // this.focus();
}

function setFormDefaultValues()	
{
    var theForm = document.forms['aspnetForm'];
    if (!theForm) 
    {
        theForm = document.aspnetForm;
    }

    for (var i = 0; i < theForm.elements.length; i++)		
    {    		
        var e = theForm.elements[i];
        
        if (e.type == 'text' && e.value == '')
        {                           
            valueBlur(e);  
        }
    }
}

function clearFormDefaultValues()	
{
    var theForm = document.forms['aspnetForm'];
    if (!theForm) 
    {
        theForm = document.aspnetForm;
    }

    for (var i = 0; i < theForm.elements.length; i++)		
    {    		
        var e = theForm.elements[i];
                
        if (e.type == 'text' && e.value == e.title)
        {
            e.value = '';
        }
    }
}

function valueFocus(e) 
{
    if(e.value == e.title) 
    {
        e.value = '';  
        addRemoveClass(e, 'blur', true);
    }
}

function valueBlur(e) 
{
    if (e.value == '') 
    {
       e.value = e.title; 
       addRemoveClass(e, 'blur', false);
    }
}

function addRemoveClass(e, strClass, bitRemoveOnly) 
{     
   if (e.className)
   {
        var arrList = e.className.split(' ');
        var strClassUpper = strClass.toUpperCase();
                
        for (var i = 0; i < arrList.length; i++)
        {           
           if (arrList[i].toUpperCase() == strClassUpper)
           {              
               arrList.splice(i, 1);               
               i--;
           }
        }     
          
        if (!bitRemoveOnly)
        {           
           arrList[arrList.length] = strClass;           
        }
        
        e.className = arrList.join(' ');
  }   
  else if (!bitRemoveOnly) 
  {
     e.className = strClass;   
  }     
}

// Catch the Key Event
function catchKeyEvent(btnID, event, keyCode)
{
	btn = findObj(btnID);
	if (document.all)
	{
		if (event.keyCode == keyCode)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
	else if (document.getElementById)
	{
		if (event.which == keyCode)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.focus();
			btn.click();
		}
	}
	else if(document.layers)
	{
		if(event.which == keyCode)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.focus();
			btn.click();
		}
	}
}

// Find the Button
function findObj(n, d) 
{ 
	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=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

// Validation
function DangerousInputValidation(sender, args) 
{                               
    var value = ValidatorGetValue(sender.controltovalidate);
                    
    if (ValidatorTrim(value).length == 0)
        return true;   
             
    var regex = new RegExp("<(.|\n)");
    var matches = regex.exec(value);
                            
    args.IsValid = !(matches != null && matches.length > 0);
}
