﻿var popupEnabled = false;
var validationVisible = false;
var valSumContainers= new Array();
var valSums = new Array();

//Scrolls to the Top Position when in Popup
function fitMarginTop() {	    
	var version = navigator.appVersion;   
    if(version.indexOf("MSIE 6") != -1 && navigator.appName == "Microsoft Internet Explorer" && popupEnabled)
	    window.setTimeout(ScrollTop, 100);		
	else
	    window.scroll(0, 0);  
}

function ScrollTop()
{
    window.scroll(0, 0);    
}

// Opens a Popup via Url
function showPopup(key, queryParams, additionalQueryString) {   
	var path = new String();	
	path = window.location.href;
	if(path.indexOf('?') != -1)
		path = path.substring(0, path.indexOf('?'));
	
	if(path.charAt(path.length-1) == '/')
		path = path + "default.aspx";
	   
	
	if(additionalQueryString != null && additionalQueryString != "") {
		var query = window.location.search;
		
		if(query.indexOf('?') != -1)		
			query= query.replace('?', '&');
		    
		window.location.replace(path +"?Popup=" + key  + query + "&" + additionalQueryString + (queryParams != null ? "&PopupParams=" + queryParams : ""));
	}
	else if(query == null || query == "") {		
		window.location.replace(path +"?Popup=" + key  + (queryParams != null ? "&PopupParams=" + queryParams : ""));
	} else {
		window.location.replace(path +"?Popup=" + key  + (queryParams != null ? "&PopupParams=" + queryParams : "") + query.replace("?", "&"));		
	}				
}
	
// Loads the Popup Style	
function enablePopup() {		   
	if(popupEnabled) return;	 
	popupEnabled = true;	
	document.body.scroll = "no";	
	document.body.style.overflow = "hidden";
	LoadCSS("css/popup.css");
}

// Disables the Popup Style
function disablePopup() {	  
	popupEnabled = false;		
	document.body.scroll = "auto";
	document.body.style.overflow = "auto";	
	UnloadCSS("css/popup.css");	
}

//Used For Ajax Calls
function CheckValidation() {
	if(popupEnabled && typeof(valSumContainers) != 'undefined') {
		//Hinweis: derzeit ist die Validierung in Popups nicht über inner Popups gelöst -> keine eigene ValidationSummary verwenden
		for (var i = 0; i < valSums.length; i++) {
			if(document.getElementById(valSums[i]) != null) {
				if(document.getElementById(valSums[i]).style.display != "none") ShowValidation(valSumContainers[i]);
			}
		}		
	}
}

function ValidationCollection(ValidationSummary, ValidationKey)
{
    this.ValidationSummary = ValidationSummary;
    this.ValidationKey = ValidationKey;
}

function ShowValAsnyc()
{   
    for(var i=0; i < valSums.length; i++)
    {
        if(document.getElementById(valSums[i].ValidationSummary) != null)
        {      
            if(document.getElementById(valSums[i].ValidationSummary).style.display != "none")   //Check if the Current ValidationSummary is Invalid
            {   
	            ShowValidation(valSums[i].ValidationKey);
	            currentValidationCtrl = valSums[i].ValidationKey;
            }
        }        
    }
}

function fitValidation(clientID) {
	if (validationVisible) {
		window.scroll(0, 0);
		var Knoten = document.getElementById(clientID + 'Container').cloneNode(true);
		document.getElementById(clientID + 'Container').innerHTML = "";
		document.getElementById('ValidationContainer').appendChild(Knoten);
	}
}

function ShowValidation(clientID) {    
	validationVisible = true;	
	document.getElementById(clientID+"Container").style.display = "";
	document.getElementById(clientID+"Container").style.visibility  = "";		
	fitValidation(clientID);
	
    document.body.scroll = "no";	
    document.body.style.overflow = "hidden";
	LoadCSS("css/Validation.css");
}

function HideValidation(clientID) {	
	validationVisible =false;	
	UnloadCSS("css/Validation.css");
	
	document.getElementById(clientID+"Container").style.display = "none";
	document.getElementById(clientID+"Container").style.visibility  = "hidden";			
	
	document.body.scroll = "auto";
	document.body.style.overflow = "auto";
}

function LoadCSS(cssName)
{
	if(document.createStyleSheet) {
		document.createStyleSheet(LinkBaseDir+cssName);			
	}
	else {   
		var newSS=document.createElement('link');
		newSS.rel='stylesheet';
		newSS.href=LinkBaseDir+cssName;
		document.getElementsByTagName("head")[0].appendChild(newSS);									
	}
}

function UnloadCSS(cssName)
{
	var cssLinks = document.getElementsByTagName("link");
	for(var i = 0; i< cssLinks.length; i++) {	  
		if(cssLinks[i].href.indexOf(LinkBaseDir +cssName) != -1) 	{				
			document.getElementsByTagName("head")[0].removeChild(cssLinks[i]);
		}
	}	
}