//0 means disabled; 1 means enabled;  
var popupStatus = 0;  
var popupStatusSend = 0;  
var popupStatusConfirm = 0;


$(document).ready(function()
{  
    $("#ctl00_m_cpPopUp_ConfiHistTx[disabled='disabled']").css("background","#d5d1ce");
    $("#ctl00_m_cpPopUp_ConfiHistTx[disabled='disabled']").css("color","#000000");

    
    $("#ctl00_m_cpPopUp_TXBemConfirm[disabled='disabled']").css("background","#d5d1ce");
    $("#ctl00_m_cpPopUp_TXBemConfirm[disabled='disabled']").css("color","#000000");
        
    
    $("#Zusammenfassung").hide();
    $("#ctl00_m_cpPopUp_btninquiry").hide();
    $("#ctl00_m_cpPopUp_btnInqBack").hide();
    
    // Email TXT Input disable bzw. enable abhängig von Checkbox 
    if ($('#ctl00_m_cpPopUp_IdCC:checked').length)
    {
        $("#ctl00_m_cpPopUp_TxEMail").removeAttr("disabled");
        $("#ctl00_m_cpPopUp_TxEMail").css("border","solid 1px #000");
        $("#ctl00_m_cpPopUp_TxEMail").css("background","#fff");              
    }
    else
    {
        $("#ctl00_m_cpPopUp_TxEMail").attr("disabled","disabled");        
        $("#ctl00_m_cpPopUp_TxEMail").css("border","solid 1px #000");
        $("#ctl00_m_cpPopUp_TxEMail").css("background","#ccc");              
    }

    // Email TXT Input disable bzw. enable abhängig von Checkbox 
   $("#ctl00_m_cpPopUp_IdCC").click(function()
   {  
        if ($('#ctl00_m_cpPopUp_IdCC:checked').length)
        {
            $("#ctl00_m_cpPopUp_TxEMail").removeAttr("disabled");
            //$("#wrapTxEMailCC").removeClass("disabled");
            $("#ctl00_m_cpPopUp_TxEMail").removeAttr("disabled");
            $("#ctl00_m_cpPopUp_TxEMail").css("border","solid 1px #000");
            $("#ctl00_m_cpPopUp_TxEMail").css("background","#fff");                  
        }
        else
        {
            $("#ctl00_m_cpPopUp_TxEMail").attr("disabled","disabled");        
            //$("#wrapTxEMailCC").addClass("disabled");
            $("#ctl00_m_cpPopUp_TxEMail").attr("disabled","disabled");     
            $("#ctl00_m_cpPopUp_TxEMail").css("border","solid 1px #000");
            $("#ctl00_m_cpPopUp_TxEMail").css("background","#ccc");               

        }
   });  

              
    // merker für Mail send --> Bestätigung --> anzeigen
    var a = $("input[name$='HF_MailSend']").get(0).value;
    //LOADING POPUP MailSend      
    if(a=='1')
    {
        popupStatusSend = 1;
        centerPopup("popupMailSend","BackgroundPopUpSend");  
        loadPopup("popupMailSend","BackgroundPopUpSend");  

        $("#popupContactClose").click(function(){  
        disablePopup();  
        });  
        
        $("#popupContactCloseSend").click(function(){  
        disablePopup();  
        });              
           
        $("#BackgroundPopUpSend").click(function(){  
        disablePopup();  
        });  
        
    };
    
   //LOADING Normal POPUP  
   $("#DivBtnInquiry").click(function(){  

       $("#ctl00_m_cpPopUp_btninquiry").hide();
       $("#ctl00_m_cpPopUp_btnInqBack").hide();
       
       $("#ctl00_m_cpPopUp_btnInqGo").show();       
       centerPopup("popupContact","backgroundPopup");  
       loadPopup("popupContact","backgroundPopup");  
   });  
   
    $("#popupContactClose").click(function(){  
    disablePopup();  
    });  
    
    $("#popupContactCloseSend").click(function(){  
    disablePopup();  
    });      
    
    $("#backgroundPopup").click(function(){  
    disablePopup();  
    });  

    $("#ctl00_m_panelPopUpConfirmation_BtnMailSendClose").click(function(){  
    disablePopup();  
    return false;
    });  
   
    $("#ctl00_m_cpPopUp_btnClosePopUp").click(function(){  
    disablePopup();  
    return false;
    });  
   
  

  $(document).keypress(function(e)
  {  
    if(e.keyCode==27 && popupStatus==1)
    {  
        disablePopup();  
    }  
  });     
  
  
  
//  $("#ctl00_m_cpPopUp_TxBem").keyup(function(){  
//  limitChars('ctl00_m_cpPopUp_TxBem', 500, 'charlimitinfo');
//  });
  
  $("#ctl00_m_cpPopUp_btnInqBack").click(function(){  
          $("#Zusammenfassung").hide();
          $("#kontaktDaten").show();
          $("#ctl00_m_cpPopUp_btnInqBack").hide();
          $("#ctl00_m_cpPopUp_btnInqGo").show();
          $("#ctl00_m_cpPopUp_btninquiry").hide();
          return false;
   });
          
   //  initialisieren der Labels auf der Confirm Seite/Pop Up
   $("#ctl00_m_cpPopUp_btnInqGo").click(function(){  
        $("#kontaktDaten").hide();
        $("#ctl00_m_cpPopUp_btninquiry").show();
        $("#ctl00_m_cpPopUp_btnClosePopUp").show();
        $("#ctl00_m_cpPopUp_LabelVornameConf").html($("#ctl00_m_cpPopUp_TxVorname").val());
        $("#ctl00_m_cpPopUp_LabelNameConf").html($("#ctl00_m_cpPopUp_TxName").val());
        $("#ctl00_m_cpPopUp_LabelFirmaConf").html($("#ctl00_m_cpPopUp_TxFirma").val());
        $("#ctl00_m_cpPopUp_LabelPlzConf").html($("#ctl00_m_cpPopUp_Txplz").val());
        $("#ctl00_m_cpPopUp_LabelLandConf").html($("#ctl00_m_cpPopUp_TxLand").val());
        $("#ctl00_m_cpPopUp_LabelOrtConf").html($("#ctl00_m_cpPopUp_TxOrt").val());
        $("#ctl00_m_cpPopUp_LabelStrConf").html($("#ctl00_m_cpPopUp_TxStr").val());
        if ($('#ctl00_m_cpPopUp_IdCC:checked').length)
        {
            $("#ctl00_m_cpPopUp_LabelEMailConf").html($("#ctl00_m_cpPopUp_TxEMail").val());
            $("#ctl00_m_cpPopUp_LabelEMailConf").show();
            $("#ctl00_m_cpPopUp_LabelEMail2").show();
        }
        else
        {
            $("#ctl00_m_cpPopUp_LabelEMailConf").hide();
            $("#ctl00_m_cpPopUp_LabelEMail2").hide();
        }    

        $("#ctl00_m_cpPopUp_LabelTelConf").html($("#ctl00_m_cpPopUp_TxTel").val());
        $("#ctl00_m_cpPopUp_TXBemConfirm").html($("#ctl00_m_cpPopUp_TxBem").val());
//        $("#DivTXBemConfirm").html($("#ctl00_m_cpPopUp_TxBem").val());
//        $("#ConfiHistTx").html($("#ctl00_m_cpPopUp_ConfiHistTxVal").html());
        
        $("#Zusammenfassung").show();
        $("#ctl00_m_cpPopUp_btnInqGo").hide();
        $("#ctl00_m_cpPopUp_btninquiry").show();
        $("#ctl00_m_cpPopUp_btnInqBack").show();        
        
        return false;
   });  
});  




function loadPopup(popupDiv,backgroundDiv)
{  
    popupDiv = '#' + popupDiv;
    backgroundDiv = '#' + backgroundDiv;
    
    if(popupStatus==0)
    {  
        $(backgroundDiv).css({  "opacity": "0.2" });  
        $(backgroundDiv).fadeIn("slow");  
        $(popupDiv).animate({height: 'show', width: 'show', opacity: 'show'}, 'slow');
        popupStatus = 1;  
    }  
}  



function disablePopup()
{  
    if(popupStatus==1)
    {  
        $("#ctl00_m_cpPopUp_btninquiry").hide();
        $("#ctl00_m_cpPopUp_btnInqBack").hide();
       
        $("#popupContact").animate({height: 'hide', width: 'hide', opacity: 'hide'}, 'slow');
        $("#backgroundPopup").animate({height: 'hide', width: 'hide', opacity: 'hide'}, 'slow');
        popupStatus = 0;  
    }  
    if(popupStatusSend==1)
    {
        $("#popupMailSend").animate({height: 'hide', width: 'hide', opacity: 'hide'}, 'slow');
        $("#BackgroundPopUpSend").animate({height: 'hide', width: 'hide', opacity: 'hide'}, 'slow');
        popupStatusSend = 0;      
    }
        $("#Zusammenfassung").hide();
        $("#kontaktDaten").show();       
}  


function centerPopup(popupDiv,backgroundDiv)
{  
    popupDiv = '#' + popupDiv;
    backgroundDiv = '#' + backgroundDiv;
    var windowWidth ;
    var windowHeight ;
    
    if (typeof window.innerWidth != 'undefined')
     {
          windowWidth = window.innerWidth,
          windowHeight = window.innerHeight
     }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
     else if (typeof document.documentElement != 'undefined'
              && typeof document.documentElement.clientWidth !=
              'undefined' && document.documentElement.clientWidth != 0)
     {
           windowWidth = document.documentElement.clientWidth,
           windowHeight = document.documentElement.clientHeight
     }
     // older versions of IE
     else
     {
           windowWidth = document.getElementsByTagName('body')[0].clientWidth,
           windowHeight = document.getElementsByTagName('body')[0].clientHeight
     }

    var popupHeight = $(popupDiv).height();  
    var popupWidth = $(popupDiv).width();  
    
//centering  
    $(popupDiv).css({  
      "position": "absolute",
      "top": windowHeight / 2 - popupHeight / 2,
      "left": windowWidth / 2 - popupWidth / 2
    });  
//only need force for IE6  
    $(backgroundDiv).css({  
    "height": windowHeight  
    });  

}


function limitChars(textid, limit, infodiv)
{
    var text = $('#'+textid).val(); 
    var textlength = text.length;
    if(textlength > limit)
    {
        //$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#' + infodiv).html('0');

        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else
    {
        //$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
        $('#' + infodiv).html((limit - textlength));
        return true;
    }
}
