/**********************************************************
				Constants
**********************************************************/
var OBJECT_TYPE_PANE = "PANE";
var OBJECT_TYPE_TAB = "TAB";
var OBJECT_TYPE_MODULE = "MDL";

var OBJECT_TYPE_HTML = "HTML";
var OBJECT_TYPE_SHOP_PRODUCT_ITEM = "SHP_PROD_ITM";
var OBJECT_TYPE_SHOP_CAMPAING = "SHP_CAMP";
var OBJECT_TYPE_SHOP_CAMPAIGN_ITEM = "SHP_CAMP_ITM";
var OBJECT_TYPE_BRANCH = "BRC";
var OBJECT_TYPE_HVW_HOME = "HOME";
var OBJECT_TYPE_GAL_GALLERY = "GAL";
var OBJECT_TYPE_CONTENT_ITEM = "CNT_ITM";
var OBJECT_TYPE_OBJECTLINK = "OLNK";

var MODULE_DEF_ID_EDITTAB = 26; 
var MODULE_DEF_ID_EDITMODULE = 29; 
var MODULE_DEF_ID_EDITSINGLEMODULE = 225;

var MODULE_DEF_ID_HTML = 5;


var MODULE_DEF_ID_SHP_BASKET_SMALL = 63;
var MODULE_DEF_ID_SHP_NOTEPAD_SMALL = 220;
var MODULE_DEF_ID_EMPTY_MODULE = 222;

var EVENT_DND_MOVE_MODULE = "movemodule";
var EVENT_DND_MOVE_MODULE_UP = "movemoduleup";
var EVENT_DND_MOVE_MODULE_DOWN = "movemoduledown";
var EVENT_DND_ASSIGN_TAB = "assigntab";
var EVENT_DND_MOVE_TAB_UP = "movetabup";
var EVENT_DND_MOVE_TAB_DOWN = "movetabdown";
var EVENT_DND_MOVE_CONTENT_UP = "movecontentup";
var EVENT_DND_MOVE_CONTENT_DOWN = "movecontentdown";

/**********************************************************
				General Functions
**********************************************************/

function toggleElementVisibility(elementId) {
  var o = document.getElementById(elementId);
  if(o==null)
	return;
  if(o.style.display=='none'){
    o.style.display='';
    return true;
  }else{
    o.style.display='none';
    return false;
  }
}

function setElementVisibility(elementId, visible) {
  var o = document.getElementById(elementId);
  if(o==null)
	return;
  if(visible){
    o.style.display='';
    return true;
  }else{
    o.style.display='none';
    return false;
  }
}

function getElementVisibility(elementId) {
  var o = document.getElementById(elementId);
  if(o==null)
	return false;
  return o.style.display!='none';  
}

function openWindow(URL,windowName,features) {
  if(features=="StylePreview") {
    features="width=470, height=310, toolbar=no, status=no, resizable=no, menubar=no, location=no, scrollbars=no";
    windowName="StylePreview";
   }
  if(features=="Help") {
	features="height=600,width=800,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no";
    windowName="Help";
   }
  newwin=window.open(URL,windowName,features);
  newwin.focus();
}

var g_MenuAdminButtonClicked = false;

function __body_OnClick(){
	// Hide WebMenu Submenus
	// In EditMode the collapsing is disabled, so we need to do that manually
	if(self.cswmHide && g_MenuAdminButtonClicked==false)
		cswmHide();
		
	// Hide all open Admin Menus
	if(!pop_admin_menu_clicked)
		__hideAllOpenAdminMenus();			
}

function ShowSavePopup(){
	var o = getObject('div_pleasewait');
	if (o) {
	
	    var winl = (document.body.clientWidth - o.offsetWidth) / 2;
	    var wint = (document.body.clientHeight - o.offsetHeight) / 2;	
	
		moveObj(o, winl, wint);
		showLayer('div_pleasewait');
		__toggleIFrameShim('div_pleasewait', 1);
	}	
}

function HideSavePopup(){
	var o = getObject('div_pleasewait');
	if(o){
		hideLayerNow('div_pleasewait');
		__toggleIFrameShim('div_pleasewait', 0);
	}
}

function ShowAdminHelp(topicId, frompage){
	var url = 'help/help.aspx';
	if(frompage!=null)
		url = 'admin/' + url;
	openWindow(url + '?ID=' + topicId, '', "Help");
	return false;
}

function ShowAdminHelp2(topicId){
	ShowAdminHelp(topicId, true);
	return false;
}

function openLargeImagePopup(ModuleDefId,objectId,objectType,ImageId,CurrentPage,scrollbars,ModuleId){
	if(arguments.length==6)
		ModuleId = -1
	openVionePopup(ModuleDefId, 'objectId=' + objectId + '&objectType=' + objectType + '&ModuleId=' + ModuleId + '&currentPage=' + CurrentPage + '&ImageId=' + ImageId, 'ProductImage', 0, 0, scrollbars);
}

function openVionePopup(moduleDefId, UrlParams, name, width, height, scrollbars){
	var url = "/Popup.aspx?moduleDefId=" + moduleDefId;
	if(UrlParams.length > 0)
		url += "&" + UrlParams;
	popupWindow(url, name, width, height, scrollbars);
}

function popupWindow(url, name, width, height, scrollbars){
	if(arguments.length==4)
		scrollbars = "yes"
	var mywin = window.open(url, name, "width=" + width + ",height=" + height + ",scrollbars=" + scrollbars);
	mywin.focus();
	return false;
}

function popupWindowWithContent(text, width, height){
	var mywin = window.open('dummy.htm', 'dummy', "width=" + width + ",height=" + height + ",scrollbars");
	mywin.document.write(text);
	mywin.document.close();
	mywin.focus();
	return false;
}

function popupWindowCentered(url, name, xoffset, yoffset, width, height){
	var winl = (screen.width - xoffset - width) / 2; 
	var wint = (screen.height - yoffset - height) / 2;
	var paras = 'top=' + wint + ',left=' + winl + ',width=' + width + ',height=' + height + ',scrollbars';
	var mywin = window.open(url, name, paras);
	mywin.focus();
	return false;
}

function popTplInfo(path){
	var mywin = window.open("TplInfo.aspx?path=" + path, "TplInfo", "width=400,height=150,scrollbars");
	mywin.focus();
	return false;
}

if (document.layers)
  document.captureEvents(Event.KEYDOWN);
  

function ButtonLinkClick(url, target){
	if(target.length==0)
		location.href = url;
	else
		popupWindow(url, '', 800, 600);
}

/**********************************************************
				Popup Functions
**********************************************************/

var thediv;
var TimeoutAdminPopUpHide = null;
var arrAllAdminDivs = Array();
var countAdminDivs=0;

var TimeoutMenuPopUpHide = null;
var arrAllMenuDivs = Array();
var countMenuDivs=0;

var delaytime = 3000; // Standard Delay in ms

function getObject(objId) {
  // Returns a reference to the object with given ObjectId
  if (document.layers) { //checks for Netscape 4
     return myObj = eval('document.' + objId);
  }
  else if (document.all && !document.getElementById) { //checks for IE 4
     return myObj = eval('document.all.' + objId);
  }
  else if (document.getElementById) { //Checks for Netscape 6 & IE 5
     return myObj = document.getElementById(objId);
  }
  else {
     alert('This website uses DHTML. We recommend you upgrade your browser.');
  }
}

function moveObj(object,xPos,yPos) {
  if (document.layers) { //checks for Netscape 4
     object.top = yPos;
     object.left = xPos;
  }
  else if (document.all && !document.getElementById) { //checks for IE 4
     object.style.pixelTop = yPos;
     object.style.pixelLeft = xPos;
  }
  else if (document.getElementById) { //Checks for Netscape 6 & IE 5
     object.style.top = yPos +"px";
     object.style.left = xPos + "px";
  }  
}

function __popupDiv(anchorname, popMenuId, x_offet, y_offset, delay){	
	// Get Anchorposition for popup
	var c = getAnchorPosition(anchorname);
	// Get object reference
	var o = getObject(popMenuId);	
	if(c==null || o==null)
		return;
	// Move div to target location
	var y_browser_corr = 0;
	if(is_nav4)
		y_browser_corr = -5;
	if(is_nav5up)
		y_browser_corr = -5;
	moveObj(o, c.x + x_offet, c.y + y_offset + y_browser_corr);
	var cnew = getAnchorPosition(popMenuId);
	//alert(popMenuId + ' x:' + cnew.x + ',y:' + cnew.y);
	// Show layer
	showLayer(popMenuId);
	// Hide layer after delay
	if(arguments.length==5)
		hideLayerAfterDelay(popMenuId, delay);
				
	//__toggleIFrameShim(popMenuId, 1);
}

function showLayer(objId){ 
	var objDiv = getObject(objId);
	thediv = objDiv;
	if(TimeoutAdminPopUpHide != null)
		clearTimeout(TimeoutAdminPopUpHide); 
	if( is_nav4 )
		objDiv.visibility = "show";
	else
		objDiv.style.visibility = "visible";
		
	//__toggleIFrameShim(objId, 1);
}

function hideLayerAfterDelay(objId, delay){ 
	var objDiv = getObject(objId);
	if(arguments.length==1)
		delay = delaytime;
	thediv = objDiv;	
	var dothis;	
	if( is_nav4 ){
		dothis = "thediv.visibility = 'hide';"
	} else {
		dothis = "thediv.style.visibility = 'hidden';";
	}
	
	dothis += "__toggleIFrameShim('" + objId + "', 0);";
	
	TimeoutAdminPopUpHide = setTimeout(dothis,delay);
}

var TimeoutAdminPopUpHide2 = null;

function hideLayerAfterDelay2(objId, delay){ 
	if(TimeoutAdminPopUpHide2 != null)
		clearTimeout(TimeoutAdminPopUpHide2);
	var objDiv = getObject(objId);
	if(arguments.length==1)
		delay = delaytime;
	thediv = objDiv;	
	var dothis;	
	dothis = "setElementVisibility('" + objId + "', false);"			
	TimeoutAdminPopUpHide2 = setTimeout(dothis,delay);
}

function hideLayerNow(objId){
	if(objId==null)
		return;
	try {
		objDiv = getObject(objId);
		if(objDiv==null)
			return;
		if( is_nav4 ){
			objDiv.visibility = 'hide';
		} else {
			objDiv.style.visibility = 'hidden';
		}
		__toggleIFrameShim(objId, 0);		
	} catch(e){}
}

function setLayerZ(objDiv){
	if ( is_nav4 )
		objDiv.zIndex= value;
	else
		objDiv.style.zIndex= value;
}	

function __toggleIFrameShim(divID, show, shimframe){

	var IfrRef = document.getElementById(shimframe);
	
	if(IfrRef==null)
		IfrRef = document.getElementById('shimframe');
		
	if(IfrRef==null)
		return;
	
	var oBrotherDiv = getObject(divID);
	
	if(show==1){
		IfrRef.style.width = oBrotherDiv.offsetWidth;
		IfrRef.style.height = oBrotherDiv.offsetHeight;
		IfrRef.style.top = oBrotherDiv.style.top;
		IfrRef.style.left = oBrotherDiv.style.left;
		IfrRef.style.zIndex = oBrotherDiv.style.zIndex - 1;
		IfrRef.style.display = "block";
	} else {
		IfrRef.style.display = "none";
	}
}


function OpenTranslatePopup(key){	
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2){
		if(confirm('Do you want to translate this item?')){
			var mywin;
			if(location.href.indexOf('/admin/')==-1 && location.href.indexOf('/padmin/')==-1)
				mywin = window.open("HelperComponents/Globalization/TranslateResource.aspx?key=" + key, "TplInfo", "width=400,height=250,scrollbars");
			else
				mywin = window.open("../HelperComponents/Globalization/TranslateResource.aspx?key=" + key, "TplInfo", "width=400,height=250,scrollbars");
			mywin.focus();
		}		
		return false;
	}
	return true;
}


function _setElementVisibility(id, visible){
	var o = getObject(id);
	o.style.display = 'none';
	if(visible)
		o.style.display = 'inline';		
}


/**********************************************************
			Menu and Admin-Menu Popup Functions
**********************************************************/

function wm_MenuItemClicked(tabId, target, externalURL, showInNewWindow){
	if(!g_MenuAdminButtonClicked){				
		if(externalURL == null || externalURL.length == 0){	
			if(arguments.length==1)
				var target = '';			
			if(target.length == 0)
				location.href = 'Default.aspx?tabId=' + tabId;
			else if(target == 'signout')
				location.href = 'Default.aspx?evt=' + target;
			else
				location.href = 'Default.aspx?showmodule=' + target;
		} 
		else {
			if(showInNewWindow=='False')
				location.href = externalURL;
			else
				window.open(externalURL);	
		}	
		return false;	
	}
	else
		return false;
}

function wm_MenuItemClicked_Rewrite(filename, tabId, showmodule){ 
	if(!g_MenuAdminButtonClicked){			
		if(showmodule=='')
			sredir = filename + ',tabId,' + tabId +  '.html';	
		else
			sredir = filename + ',tabId,' + tabId + ',showmodule,' + showmodule + ".html";			
		location.href = sredir;				
	}
	else
		return false;
}

function wm_MenuItemOnDragOver(code){
	var s = new String(code);
	s = s.replace(/function anonymous\(\)/gi, '');
	s = s.replace(/[{}]/gi, '');
	eval(s);
}

function menu_rollover(classname, selected, o){
	if(o!=null){
		try {			
			o.className = classname + '_hover' + selected;
		} catch(e){}
	}
}

function menu_rollout(classname, selected, o){
	if(o!=null){
		try {
			o.className = classname + selected;
		} catch(e){}
	}
}


var submenu_popup_xoffset = 0;
var submenu_popup_yoffset = 0;

var pop_admin_menu_clicked = false;

function __popupAdminMenu(anchorname, popMenuId, x_offet, y_offset, delay){
	
	/*
	
	Popup an Admin layer next to the anchor
	
	anchorname:		The ID of the Anchor
	ifrID:			The ID of the Admin Popup
	x_offet:		X-Offset of the Popup relative to the Anchor-Position
	y_offset:		Y-Offset of the Popup relative to the Anchor-Position
	delay:			Delay Time till Popup closes automatically
	*/
	
	// prevent event from bubbling up
	window.event.cancelBubble = true;
	
	pop_admin_menu_clicked = true;
	
	// Set default popup time if none was given in
	if(arguments.length==4)
		delay = delaytime;
	// Hide all open Menus now
	arrAllAdminDivs[countAdminDivs] = popMenuId;
	countAdminDivs++;
	__hideAllOpenAdminMenus();
	__popupDiv(anchorname, popMenuId, x_offet, y_offset, delay);
	
	return false;
		
}

function __popupMenu(anchorname, popMenuId){	
	
	arrAllMenuDivs[countMenuDivs] = popMenuId;
	countMenuDivs++;
	// Hide all open Menus now
	__hideAllOpenMenus();
	__popupDiv(anchorname, popMenuId, submenu_popup_xoffset, submenu_popup_yoffset);	
}

function __hideAllOpenAdminMenus(){
	// Hide all open Menus
	for(i=0; i < arrAllAdminDivs.length; i++){
		hideLayerNow(arrAllAdminDivs[i]);
	}
}

function __hideAllOpenMenus(){
	// Hide all open Menus
	for(i=0; i < arrAllMenuDivs.length; i++){
		hideLayerNow(arrAllMenuDivs[i]);
	}	
}

function navigate_to(tabid){
	location.href='Default.aspx?tabId=' + tabid;
}
	
/**********************************************************
			Category tree Functions
**********************************************************/

function categ_rollover(table) {
	table.className = 'td_nav_selected';
}

function categ_rollout(table) {
	table.className = 'td_nav_unselected';
}

/**********************************************************
	functions used to change the 'action' destination of
	the main Form on page. 
**********************************************************/
function noPostBack(sNewFormAction, chkAGBID) 
{ 
	var chk = getObject(chkAGBID);
	var goWorldPay = 1;
	
	if(chk!=null)
		chk.checked ? goWorldPay = 1 : goWorldPay = 0;
		
	if(!goWorldPay)
		return;
		
	document.Form1.action = sNewFormAction; 
	document.Form1.__VIEWSTATE.name = 'NOVIEWSTATE'; 
}

function noPostBackWithSubmit(sNewFormAction)
{
    var theForm = document.forms['aspnetForm'];
    if (!theForm)
    {
        theForm = document.aspnetForm;
    }

    theForm.action = sNewFormAction;
    theForm.__VIEWSTATE.name = 'NOVIEWSTATE';
    theForm.submit();
} 

/********************************************************
		Degugging / Stacktracing Functions
********************************************************/

function funcname(f) {
 var s = f.toString().match(/function (\w*)/)[1];
 if ((s == null) || (s.length==0)) return "anonymous";
 return s;
}

function stacktrace() {
 var s = "";
 for (var a = arguments.caller; a !=null; a = a.caller) {
   s += "->"+funcname(a.callee) + "\n";
   if (a.caller == a) {s+="*"; break;}
 }
 return s;
}


/*************************************
		Clipboard
*************************************/

function Clipboard()
{
	if ( is_ie )
    {
        this.clipboard = document.createElement( 'INPUT' );
        with ( this.clipboard.style )
        {
            position = 'absolute';
            left = '0px';
            top = '0px';
            visibility = 'hidden';
        }
        document.body.appendChild( this.clipboard );
    }    

    this.copy = Clipboard_copy;
    this.paste = Clipboard_paste;
}

/**
 * Copies a text to the system clipboard. Priveleges are necessary.
 * @param text the text
 */
function Clipboard_copy( text )
{
    if ( is_ie )
    {
        this.clipboard.value = text;
        this.clipboard.select();
        var textRange = this.clipboard.createTextRange();
        textRange.execCommand( 'copy' );
    }   
}

/**
 * Gets the current text in the system clipboard.
 * @return the text
 */
function Clipboard_paste()
{
    if ( is_ie )
    {
        this.clipboard.value = '';
        var textRange = this.clipboard.createTextRange();
        textRange.execCommand( 'paste' );

        return this.clipboard.value;
    }   
}


/*************************************
		Misc. Functions
*************************************/

function insertEmoticon(eTag, textbox_id) {
  var input = getObject(textbox_id);
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start;
    } else {
      pos = start + insText.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + insText + eTag + input.value.substr(pos);
  }
}

function URLencode(sStr) {
    return escape(sStr).
             replace(/\+/g, '%2B').
                replace(/\"/g,'%22').
                   replace(/\'/g, '%27');
}

function ajx_wrapXML(xml)
{
  var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
  var rNode = xmldoc.createNode("element", "root", "");
  xmldoc.appendChild(rNode);  
  var cNode = xmldoc.createNode("element", "content", "");  
  var  CData = xmldoc.createCDATASection(xml);  
  cNode.appendChild(CData);
  rNode.appendChild(cNode);
  return xmldoc.xml;
}

function ajx_SendRequest(method, url, body, async, callback, getxml){	
	
	var xmlhttp = getXMLRequester();
	
	if(xmlhttp){
		ShowSavePopup();
		xmlhttp.open(method, url, async);
		if(method=="POST")
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(body);			
		xmlhttp.onreadystatechange=function() {			
			if (xmlhttp.readyState==4) {
				if(callback){
					if(getxml)
						eval(callback + "(xmlhttp.responseXML)");			
					else
						eval(callback + "(xmlhttp.responseText)");
				}
				HideSavePopup();
			}
		}	
		if(!async){
			HideSavePopup();
			if(getxml)
				return xmlhttp.responseXML;			
			else
				return xmlhttp.responseText;			
		}
	} else
		alert('Error: Failed to instantiate XMLHTTP object!');			
}

function getXMLRequester()
{
    var xmlHttp = false;
            
    // try to create a new instance of the xmlhttprequest object        
    try
    {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 5; i; i-- )
            {
                try
                {
                    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if( i == 2 )
                    {
                        xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    // loading of xmlhttp object failed
    catch( excNotLoadable )
    {
        xmlHttp = false;
    }
    return xmlHttp ;
}

function ajx_callback_test(txt){
	alert(txt);
}


