﻿/*
 * Date         User            Description
 * 
 * 10.04.06     tott            • creation
 * 25.10.06		dp				SetMaximalHeight added.
 * 15.02.07     mbusch          CheckMaxLength added
 * 21.04.07     t0tt            • update WNMainPageClass_TransferToPage
 * 03.09.08     t0tt            • update RedirectToErrorPage --> use of WNMainPageClass_TransferToPage to support nested iframes
 *                              • update WNMainPageClass_TransferToPage --> transfer top frame if frame is not found
*/

/********************************* WNMainPage Object *****************************/
function WNMainPageClass()
{
    try
    {
        /*Page Properties*/
        this.BrowserName            = "";                                           // name of the browser                           
        this.RootPath               = "";                                           // path of the application
        this.FormId                 = "";                                           // id of the form
        this.OnErrorRedirectFrameId = "";                                           // id of the frame used to present the error page
        this.OnErrorRedirectUrl     = "";                                           // url of the page used to present the errors
        this.PageSectionName        = "undefined";                                  // name of the current Section (content, user ...)
        
        /*Page controls*/
        
        /*Page functions*/
        this.TransferToPage                     = WNMainPageClass_TransferToPage;                   // tansfer current page to transmitted url
        this.RedirectToErrorPage                = WNMainPageClass_RedirectToErrorPage;              // redirect to the registered errorpage
        this.RedirectToErrorPage_ServerAnswer   = WNMainPageClass_RedirectToErrorPage_ServerAnswer; // ajax answer from the server after logging the error
        this.AppendUrlParameter                 = WNMainPageClass_AppendUrlParameter;               // add parameters to an url
        this.SetMaximalHeight					= WNMainPageClass_SetMaximalHeight;					// Sets the height of n tablecells in one row to the height of the highest one
        this.CheckMaxLength                     = WNMainPageClass_CheckMaxLength;                   // Checks TextAreas for MaxLength of chars
        this.GetElementByName                   = WNMainPageClass_GetElementByName;                 // Finds a element in the dom of all frames
        this.GetUrlParameter                    = WNMainPageClass_GetUrlParameter;                  // return one parameter by name
        this.GetUrlParameters                   = WNMainPageClass_GetUrlParameters;                 // return list of parameters
    }
    catch (e) 
	{
	    parent.status = "Error of Creation of the WNPageBaseClass JS Objects!";
	}
}

/********************************* Redirecting  *************************************/

// set the src string of page to transfer the page for examble to default.aspx if session timeout in the explorer iframe occures
function WNMainPageClass_TransferToPage(strUrl, strTarget)
{
    try
    {
        if(arguments[1] != null && strTarget != "")
        {
            switch (strTarget) 
            {
                case "_self":
                {
                    self.location = strUrl;
                    break;
                }
                case "_top":
                {
                    top.location = strUrl;
                    break;
                }
                case "_blank":
                {
                    var newWindow = window.open(strUrl);
                    newWindow.focus();
                    break;
                }
                default:
                {
                    var frame = this.GetElementByName(strTarget, top);
                    if(frame != null)
                    {
                        frame.src = strUrl;
                    }
                    else
                    {
                        top.location = strUrl;
                    }

                    break;
                }
            }
            
            
        }
        else
        {
            window.location = strUrl;
        }
    }
    catch (e) 
	{
	    parent.status = "Fehler bei WNMainPage_TransferToPage WNMainPage.js!";
	}
}

function WNMainPageClass_GetElementByName(strName, parent)
{
    var element;
    
    try
    {
        element = parent.document.getElementsByName(strName)[0];
        if(element == null)
        {
            for (var intCounter = 0; intCounter < parent.frames.length; intCounter++)
            {
                if(parent.frames[intCounter].document.getElementsByName(strName)[0] != null)
                {
                    element = parent.frames[intCounter].document.getElementsByName(strName)[0];
                    break;
                }
            
                element = this.GetElementByName(strName, parent.frames[intCounter]);  
                
                if(element != null)
                {
                    break;
                }
            }
        }
    }
    catch (e) 
	{
	    parent.status = "Fehler bei WNMainPageClass_GetElementByName in WNMainPage.js!";
	}
	
	return element;
}

// ajax answer from the server after logging the error
function WNMainPageClass_RedirectToErrorPage_ServerAnswer(answer)
{
    try
    {
        objWNMainPage.RedirectToErrorPage("ticketId='" + answer.value + "'")
    }
    catch (e) 
	{
	    parent.status = "Fehler bei WNMainPageClass_GetFrame WNMainPage.js!";
	}
}

// search for a Frame in the whole HMTL and update the src
function WNMainPageClass_RedirectToErrorPage()
{
    var parameter = "";
    var strUrl;
    var evalArguments;

    try
    {
        if(this.OnErrorRedirectUrl.indexOf("?") != -1)
        {
            strUrl = this.OnErrorRedirectUrl + "&pageSectionName=" + this.PageSectionName;
        }
        else
        {
            strUrl = this.OnErrorRedirectUrl + "?pageSectionName=" + this.PageSectionName;
        }
        // get current parameter out of transmitted url
        if(strUrl.indexOf("?") != -1)
        {
            parameter = strUrl.substring(strUrl.indexOf("?"), strUrl.length);        
            strUrl = strUrl.substring(0, strUrl.indexOf("?"));
        }
        
        // add optional parameters transmitted as additional arguments
        for (var intCounter = 0; intCounter < arguments.length; intCounter++)
        {
            if(parameter.indexOf("?") != -1)
            {
                if(arguments[intCounter] != "")
                {
                    parameter += "&" + arguments[intCounter]; 
                }
            }
            else
            {
                if(arguments[intCounter] != "")
                {
                    parameter += "?" + arguments[intCounter]; 
                }
            }    
        }
        
        strUrl = strUrl + parameter;
        
        // update browser
        if(this.OnErrorRedirectFrameId != "")
        {
            this.TransferToPage(strUrl, this.OnErrorRedirectFrameId);
        }
        else
        {
            window.top.location = strUrl;
        }
    }
    catch (e) 
	{
	    parent.status = "Fehler bei WNMainPageClass_RedirectToErrorPage WNMainPage.js!";
	}
}

function WNMainPageClass_AppendUrlParameter(strUrl)
{
    try
    {
        // get current parameter out of transmitted url
        if(strUrl.indexOf("?") != -1)
        {
            parameter = strUrl.substring(strUrl.indexOf("?"), strUrl.length);        
        }
        
        // add optional parameters transmitted as additional arguments
        for (var intCounter = 1; intCounter < arguments.length; intCounter++)
        {
            if(parameter.indexOf("?") != -1)
            {
                if(arguments[intCounter] != "")
                {
                    parameter += "&" + arguments[intCounter]; 
                }
            }
            else
            {
                if(arguments[intCounter] != "")
                {
                    parameter += "?" + arguments[intCounter]; 
                }
            }    
        }
        
        strUrl = strUrl + parameter;
    }
    catch (e) 
	{
	    parent.status = "Fehler bei WNMainPage_TransferToPage WNMainPage.js!";
	}
	
	return strUrl;
}

function WNMainPageClass_GetUrlParameters() {
   var GET = new Array();
   if(location.search.length > 0) {
      var get_param_str = location.search.substring(1, location.search.length);
      var get_params = get_param_str.split("&");
      for(i = 0; i < get_params.length; i++) {
         var key_value = get_params[i].split("=");
         if(key_value.length == 2) {
            var key = key_value[0];
            var value = key_value[1];
            GET[key] = value;
         }
      }
   }
   return(GET);
}
 
function WNMainPageClass_GetUrlParameter(key) {
   var get_params = this.GetUrlParameters();
   if(get_params[key])
      return(get_params[key]);
   else
      return false;
}



/********************************* control handling  *************************************/

// Sets the height of n tablecells in x rows to the height of the highest one.
// This thing was built to support a DataList control.
//	preIdString: id part before the counter
//	postIdString: id part after the counter
//	repeatColumns: how many cells holds one row
function WNMainPageClass_SetMaximalHeight(preIdString, postIdString, repeatColumns)
{
	var cell = 1;
	for (var i = 0; i < 9999 && cell != null; i += repeatColumns) 
	{
		var cellHeight = 0;
		for (var j = 0; j < repeatColumns && cell != null; j++) 
		{
			cell = document.getElementById(preIdString + (i + j) + postIdString);
			if (cell != null) 
			{
				cellHeight = Math.max(cell.offsetHeight, cellHeight);
			}
		}
		for (var j = 0; j < repeatColumns && cell != null; j++)
		{
			cell = document.getElementById(preIdString + (i + j) + postIdString);
			if (cell != null) 
			{
				cell.height = cellHeight;
			}		
		}
	}
}

function WNMainPageClass_CheckMaxLength(e, textarea_id, max) 
{
    var theTextArea = document.getElementById(textarea_id);
    if (!e.which) 
    {
        keyCode = event.keyCode; // ie5+ op5+
    }
    else
    { 
        keyCode = e.which; // nn6+
    }

    if (theTextArea.value.length>max)
    {
        theTextArea.value = theTextArea.value.substring(0,max);
    }
}

