/**********************************************************
				Constants
**********************************************************/
var OBJECT_TYPE_PANE = "PANE";
var OBJECT_TYPE_TAB = "TAB";
var OBJECT_TYPE_MODULE = "MDL";

var OBJECT_TYPE_SHOP_PRODUCT_ITEM = "SHP_PROD_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";


/**********************************************************
				General Functions
**********************************************************/

function ShowAdminHelp(topicId){
	window.open("help/help.aspx?ID=" + topicId,null,"height=600,width=800,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	return false;
}

/* Is used from the WebSite area, so it is neccessary to have the prefix admin in path*/
function ShowAdminHelp2(topicId){
	window.open("admin/help/help.aspx?ID=" + topicId,null,"height=600,width=800,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	return false;
}

function openLargeWindowWithURL(URL, iWindowWidth, iWindowHeight){
	popupWindowCentered(URL, 'Content', 20, 20, iWindowWidth, iWindowHeight);
}

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);
  
 var g_SubmitBtn;
 
function MapCrKeyPressForSubmit (evt) { 
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
    if(keyCode=='13')
		if(g_SubmitBtn!=null)
			g_SubmitBtn.focus();
}

function ButtonLinkClick(url, target){
	if(target.length==0)
		location.href = url;
	else
		popupWindow(url, '', 800, 600);
}

function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0, j = 0; i < elsLen; i++) {
	if ( pattern.test(els[i].className) ) {
	classElements[j] = els[i];
	//oElement = document.getElementById(classElements[j].id);
	//els[i].onclick="return false;"
	j++;
	}
	}
	return classElements;
}


/**********************************************************
				Popup Functions
**********************************************************/

var thediv;
var theiframe;
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);
}

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, objId + '_ifr', 0);		
	} catch(e){}
}

function setLayerZ(objDiv){
	if ( is_nav4 )
		objDiv.zIndex= value;
	else
		objDiv.style.zIndex= value;
}	

function __toggleIFrameShim(divID, show){

	/*
	
	Toggles an IFrame to shim a layer against underlying Select boxes
	
	divID:			ID of the Layer to shim
	show:			The toggle
	*/
	
	var IfrRef = document.getElementById('shimframe');
	
	theiframe = IfrRef;
	
	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;
}

/**********************************************************
				Admin Bar Functions
**********************************************************/

/* --- Module Admin Bar --- */

function mab_editTemplate(moduleId, moduleState, fromadmin){
	var url = "EditTemplates/EditTemplates.aspx?moduleId=" + moduleId + "&moduleState=" + moduleState;
	if(arguments.length==2)
		url = "admin/" + url;
	popupWindowCentered(url, "Template_Editor", 0, 0, 1024, 800); 	
}

/* --- Pane Admin bar --- */

function pab_main_OnMouseOver(index, pane){
	pop_admin_menu_clicked = false;
	pab_main_OnMouseOut(index, pane); 
	__popupMenu('map_imgAnchor_' + index + '_' + pane, 'map_div_sub_' + index + '_'  + pane);	
}

function pab_main_OnMouseOut(index, pane){
	if(TimeoutAdminPopUpHide != null)
		clearTimeout(TimeoutAdminPopUpHide);
	hideLayerAfterDelay('map_div_sub_' + index + '_' + pane);
}

function pab_Sub_OnMouseOver(layerId){
	// clear timerout from hideLayerAfterDelay in pab_main_OnMouseOut
	//alert(TimeoutAdminPopUpHide);
	if(TimeoutAdminPopUpHide != null)
		clearTimeout(TimeoutAdminPopUpHide);
	// Show layer
	showLayer(layerId);
}

function pab_Sub_OnMouseOut(pane){
	hideLayerNow('map_div_sub_1_' + pane);
	hideLayerNow('map_div_sub_2_' + pane);
	hideLayerNow('map_div_sub_3_' + pane); 
}

/****************************************/
/*	Visibility Elements Eventhandling	*/
/****************************************/

function cvis_Toggle(keyname, tabId){
	location.href= 'default.aspx?tabId=' + tabId + '&evt=cvistoggle&key=' + keyname + '&backurl= ' + escape(location.search);
}

/********************************/
/*	ContentAdmin Eventhandling	*/
/********************************/

/* Global variables initialized on click on the ContentAdminButton */
var cab_editModuleDefID;
var cab_tabID;
var cab_contentID;
var cab_contentType;
var cab_contentIsVisible;
var cab_moduleID;
var cab_editURLParams;
var cab_deleteURLParams;
var cab_delSprocName;

function cab_OnContentAdminButtonClick(anchorId, contentID, object_is_visible, show_edit_button, show_visibility_button, show_delete_button, object_type, tabID, moduleID, editModuleDefID, editURLParams, deleteURLParams, delSprocName){
	
	cab_contentID = contentID;
	cab_contentType = object_type;
	cab_tabID = tabID;
	cab_moduleID = moduleID;
	cab_editModuleDefID = editModuleDefID;	
	cab_editURLParams = editURLParams;
	cab_deleteURLParams = deleteURLParams;
	cab_delSprocName = delSprocName;
	cab_contentIsVisible = object_is_visible;
	
	if(cab_contentIsVisible=='True')
		cab_contentIsVisible = 1;
	else
		cab_contentIsVisible = 0;
				
	var tr_edit = getObject('cap_tr_edit');
	tr_edit.style.display = 'none';
	var tr_visiblility = getObject('cap_tr_visiblility');
	tr_visiblility.style.display = 'none';
	var tr_delete = getObject('cap_tr_delete');
	tr_delete.style.display = 'none';	
	
	var div_cap_Show = getObject('div_cap_Show');
	div_cap_Show.style.display = 'none';
	var div_cap_Hide = getObject('div_cap_Hide');
	div_cap_Hide.style.display = 'none';
	
	var cab_imgVisiblity = getObject('cab_imgVisiblity');
	
	if(show_edit_button=='True')
		tr_edit.style.display = 'inline';
	
	if(show_visibility_button=='True')
		tr_visiblility.style.display = 'inline';
	
	if(show_delete_button=='True')
		tr_delete.style.display = 'inline';
		
	if(cab_contentIsVisible==1){
		div_cap_Hide.style.display = 'inline';
		cab_imgVisiblity.src = 'admin/Images/content_hidden.gif';
	} else {
		div_cap_Show.style.display = 'inline';
		cab_imgVisiblity.src = 'admin/Images/content.gif';
	}		
	
	// Popup the menu
	__popupAdminMenu(anchorId, 'popContentAdmin', 5, 5);
}

function cab_EditContent(){
	location.href='admin/Default.aspx?tabId=' + cab_tabID + '&moduleId=' + cab_moduleID + '&editModuleDefId=' + cab_editModuleDefID + '&contentId=' + cab_contentID + '&contentType=' + cab_contentType + '&' + cab_editURLParams;
}

function cab_DeleteContent(){
	if(!ConfirmDeleteEntry())
		return;
	location.href= 'Default.aspx?evt=cap&backurl= ' + escape(location.search) + '&tabId=' + cab_tabID + '&delContentId=' + cab_contentID + '&contentType=' + cab_contentType + '&delSprocName=' + cab_delSprocName + '&deleteURLParams=' + cab_deleteURLParams;
	return false;
}

function cab_ToggleContentVisibility(){
	location.href= 'Default.aspx?evt=cap&backurl= ' + escape(location.search) + '&tabId=' + cab_tabID + '&visContentId=' + cab_contentID + '&contentType=' + cab_contentType + '&contentIsVisible=' + cab_contentIsVisible;
	return false;
}

function cab_AddContent(CurrentTabId, ModuleID, editModuleDefId, editURLParams){
	location.href='admin/Default.aspx?tabId=' + CurrentTabId + '&moduleId=' + ModuleID + '&editModuleDefId=' + editModuleDefId + '&itemId=-1&' + editURLParams + '&menuId=' + ModuleID;
}


/**********************************************************
			Menu and Admin-Menu Popup Functions
**********************************************************/

function wm_MenuItemGetLink(tabId, target){ 
	if(arguments.length==1)
		var target = '';			
	if(target.length == 0)
		return 'Default.aspx?tabId=' + tabId;
	else if(target == 'signout')
		return 'Default.aspx?evt=' + target;
	else
		return 'Default.aspx?showmodule=' + target;

}

function wm_MenuItemClicked(tabId, target){ 
	if(!g_MenuAdminButtonClicked){		
		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
		return false;
}

function wm_MenuItemClicked_Rewrite(tabId, alias, target){ 
	if(!g_MenuAdminButtonClicked){
		
		var sredir = '';	
		
		if(alias=='')
			alias = 'index';
		if(target=='')
			sredir = alias + '_' + tabId + '.html';	
		else
			sredir = target + ".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);
}

var menu_tr_parent;
var menu_tr;
var menu_tr_selected;

// Highlight select top menu
if(menu_tr_selected!=null)
	menu_rollover(menu_tr_selected)

function menu_rollover(tr) {
	menu_tr = tr;	
	if(menu_tr!=null)
		menu_tr.className = 'td_nav_selected';
	menu_tr_parent = menu_tr;		
}

function menu_rollout(tr) {
	if(tr==menu_tr_selected)
		return;
	if(menu_tr != null)
		menu_tr.className = 'td_nav_unselected';			
}

function submenu_rollover(tr) {
	if(menu_tr_parent != null)
		menu_tr_parent.className = 'td_nav_selected';	
	tr.className = 'td_nav_selected';
}

function submenu_rollout(tr) {
	if(tr!=null)	
		tr.className = 'td_nav_unselected';
	if(menu_tr_parent==menu_tr_selected)
		return;
	if(menu_tr_parent != null)
		menu_tr_parent.className = 'td_nav_unselected';	
}

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]);
	}
	// Unselect parent menuitem
	submenu_rollout(null);
}

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) 
{ 
document.Form1.action = sNewFormAction; 
document.Form1.__VIEWSTATE.name = 'NOVIEWSTATE'; 
document.Form1.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;
}


/********************************************************
		Degugging / Stacktracing Functions
********************************************************/

function resizeContentArea(editor)   
   {   
    if (editor.InitialHeight == -1)   
    {   
     editor.InitialHeight = editor.Document.body.clientHeight;   
    }   
    var targetHeight = editor.Document.body.scrollHeight;   
    if (targetHeight > editor.InitialHeight)   
    {   
     var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);   
     theIFrame.style.height = parseInt(targetHeight) + "px";   
    }   
   }   
  
   function attachResizeEvents(editor)   
   {   
    editor.InitialHeight = editor.Document.body.clientHeight;   
  
    editor.GetContentArea().style.overflow = "hidden";   
    editor.Document.body.scroll = "no";   
    var resizeFnRef = function anon(){resizeContentArea(editor)};   
    editor.AttachEventHandler("RADEVENT_SEL_CHANGED", resizeFnRef)   
    editor.AttachEventHandler("keydown", resizeFnRef)   
   }   


