/************************************************************************************************************
(C) www.dhtmlgoodies.com, June 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

//Variables to objects position in the page
var DefaultScreenWidth = 1024;
var DefaultScreenHeight = 768;
//
var StartExecution = false;
var ScreenWidth = 0;
var ScreenHeight = 0;
var ContentWidth = 0;
var ContentHeight = 0;

alertSize();

var OldScreenWidth = 0;
var OldScreenHeight = 0;

//
var DifWidth = 0;
if (ScreenWidth > DefaultScreenWidth){
	DifWidth = ((ScreenWidth - DefaultScreenWidth)/2)+20;
}
var DifHeight = 0;
//
var BeginLimitTop = 0; //30 + DifHeight;
var EndLimitTop = 5000;
var BeginLimitLeft = 0; //30 + DifWidth;
var EndLimitLeft = 5000;

var ArrayDragDiv=new Array();
var dynamicContent_ajaxObjects = new Array(); 
var jsCache = new Array();
var enableCache = false; 
var EventChangePosition = 'changepositionoff';

var TopPositionNext = 0;
var LeftPositionNext = 21;

alertSize();
initializeSize();

function AdjustLeftHomePage(){

	if (document.content.ConHomePageYN.value == "" || document.content.ConHomePageYN.value == "N"){
		BeginLimitLeft = 30 + DifWidth;
	}
	else{
		BeginLimitLeft = 30 + DifWidth;
	}
}
function initializeSize(){

	//
	DifWidth = 0;
	if (ScreenWidth > DefaultScreenWidth){DifWidth = (ScreenWidth - DefaultScreenWidth)/2};
	
	DifContentWidth = 0;
	DifContentHeight = 0;
	
	if (ScreenWidth < DefaultScreenWidth){ScreenWidth=DefaultScreenWidth;}
	
	//alert(" ScreenWidth  = " + ScreenWidth + " - ContentWidth = " + ContentWidth);
	
	DifContentWidth = 0; //((ScreenWidth - ContentWidth)/2);
	DifHeight = 0;
	//
	//BeginLimitTop = 230 + DifHeight;
	EndLimitTop = 5000;
	//BeginLimitLeft = 240 + DifWidth;
	EndLimitLeft = 5000;

}
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  ScreenWidth = myWidth;
  ScreenHeight = myHeight;
}

function ShowHidePersonalized(showhide)  //This is to hide the Personalized region in Dept pages and show in the home page
{

	obj = document.getElementById('dhtmlgoodies_q1');
	if (obj != null){
		if (!showhide){


			obj.style.visibility = "hidden";
			obj.style.width = "0";
			obj.style.height = "0";

			obj = document.getElementById("dhtmlgoodies_a1");
			obj.style.visibility = "hidden";
			obj.style.width = "0";
			obj.style.height = "0";
			
			
			if(document.getElementById("PagePersonalized") != null && document.getElementById("PagePersonalized") != 'undefined'){
				obj = document.getElementById("PagePersonalized");
				obj.style.visibility = "hidden";
				obj.style.width = "0";
				obj.style.height = "0";
				
				obj = document.getElementById("LinePersonalized");
				obj.style.visibility = "hidden";
				obj.style.width = "0";
				obj.style.height = "0";
			}
			if(document.getElementById("MyOsgPersonalized") != null && document.getElementById("MyOsgPersonalized") != 'undefined'){
				obj = document.getElementById("MyOsgPersonalized");
				obj.style.visibility = "hidden";
				obj.style.width = "0";
				obj.style.height = "0";
			}
			
		}else{
		
			obj.style.visibility = "visible";
			obj.style.display = "block";
			obj.style.width = "98%";

			obj = document.getElementById("dhtmlgoodies_a1");
			obj.style.width = "100%";
			
			if(document.getElementById("PagePersonalized") != null && document.getElementById("PagePersonalized") != 'undefined'){
				//obj = document.getElementById("PagePersonalized");
				//obj.style.visibility = "visible";
				//obj.style.display = "block";
				//obj.style.width = "99%";
				//obj.style.height = "200";
				
				obj = document.getElementById("LinePersonalized");
				obj.style.visibility = "visible";
				obj.style.display = "block";
				obj.style.width = "100%";
				//obj.style.height = "200";
			}
			
			if(document.getElementById("MyOsgPersonalized") != null && document.getElementById("MyOsgPersonalized") != 'undefined'){
				obj = document.getElementById("MyOsgPersonalized");
				obj.style.visibility = "visible";
				obj.style.display = "block";
				obj.style.width = "99%";
				//obj.style.height = "200";
			}
			
			
		}
	}

}

function ShowColumnMiddleSide(showhide){     //If it is not a lateral menu hide the column separator for the vertical menu

/*
		if (showhide){
			var obj = document.getElementById('ColumnLeftSide');
			obj.style.width = "219";

			obj = document.getElementById('ColumnMiddleSide');
			obj.className = "SeparatorLeftMidle";
			
		}
		else{
		
			var obj = document.getElementById('ColumnLeftSide');
			obj.style.width = "0";

			obj = document.getElementById('ColumnMiddleSide');
			obj.className = "";
		
		}
*/

}


function hide_iframe(TypeWindow)  //For Admin pages
{


  //document.content.UpcConUserID.value = "0";
  ShowHidePersonalized(false);
  ShowHidePersonalizedPage(false);
  
  
  document.getElementById('content_person').style.display = "none";
  document.getElementById('content_person').style.height = "0";
  
  toggleBox('diviframe01',0); 	
  document.getElementById('diviframe01').style.display = "none";
  document.getElementById('diviframe01').style.height = "0";
  toggleBox('content_one',1);	
  
  
  if (TypeWindow != 'undefined' && TypeWindow == 'NM'){
	  // Show Menu
	  var obj = document.getElementById("DivLateralMenu");
	  obj.style.visibility = "hidden";
	  obj.style.width = "0";
	  ShowColumnMiddleSide(false);
  }
  else{
  
	  // Show Menu
	  var obj = document.getElementById("DivLateralMenu");
	  obj.style.visibility = "visible";
	  obj.style.width = "220";
	  ShowColumnMiddleSide(true);
  }


}

function ajax_loadContent(divId,pathToFile,contentID, dragable, CallNext)  //load the content of all portlets in the DIV
{

  //Set up the Content
  if (contentID != null)
  {

  	document.content.ConID.value = contentID;
  }
  if(enableCache && jsCache[pathToFile]){
  
      document.getElementById(divId).innerHTML = jsCache[pathToFile];
    return;
  }
  
  //document.getElementById(divId).style.width = 
  //document.getElementById(divId).style.height = 
  
  var obj = document.getElementById("content_one");
  obj.style.width = "";
  obj.style.height = "";
  
  
  var ajaxIndex = dynamicContent_ajaxObjects.length;
  
  var RunningImage = true;

	if (EventChangePosition != ''){                            //check if Position Off is On
		if (EventChangePosition == 'changepositionoff' 
				|| EventChangePosition == 'changepositionon'
					|| EventChangePosition == 'save'){
			EventChangePosition = '';
			RunningImage = false;
		}
	}
	
  if (RunningImage){
  	document.getElementById(divId).innerHTML = '<img src="../images/ajax/running.gif" border="0">&nbsp;<span class="LoadingContent">Loading content...</span>';
  }
  
 

  dynamicContent_ajaxObjects[ajaxIndex] = new sack();
  dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;
  dynamicContent_ajaxObjects[ajaxIndex].onCompletion = 
  function(){ 
  ajax_showContent(divId,ajaxIndex,pathToFile);  //This is the point where CMS00230portlet.jsp is called to show the portlets



	//Initialize Array
	//AdjustLeftHomePage();
	for (var i=0; i<ArrayDragDiv.length; i++) {
	
		//alert("ArrayDragDiv[i] = " + ArrayDragDiv[i]);
	
		var aThumb = document.getElementById(ArrayDragDiv[i]);
		
		//alert("ConHomePageYN = ");
		//alert("ConHomePageYN = " + document.content.ConHomePageYN.value);
		//alert("ConOwnerOnlyYN = " + document.content.ConOwnerOnlyYN.value);
		
		if (document.content.ConOwnerOnlyYN.value == "Y"){
			Drag.init(aThumb, null, BeginLimitLeft, EndLimitLeft, BeginLimitTop-70, EndLimitTop);  //this is to restrict the drag & drop on the page
		}
		else if (document.content.ConHomePageYN.value == "Y"){
			Drag.init(aThumb, null, BeginLimitLeft, EndLimitLeft, BeginLimitTop-70, EndLimitTop);
		}else{
			Drag.init(aThumb, null, BeginLimitLeft, EndLimitLeft, BeginLimitTop, EndLimitTop);
		}
		
		
	}
	ArrayDragDiv=new Array();
	AdjustModePixel();

	if (dragable){
	   initdragableElements();
	}
  
  	//Resize Frame
 	
  	ResizePageDiv(contentID);   //Resizing of page to determine the Admin Options on the bottom
  	setTimeout("ResizePageDiv("+contentID+")",800);
 	
  	//alert("document.content.ConHomePageYN.value = " + document.content.ConHomePageYN.value);
  	
  	if (document.content.ConHomePageYN.value == "N"){
  		ShowHidePersonalized(false);
  	}
  	else{
  	
  	    //alert("ajax_loadContent - ShowPersonalize");
  		//ShowHidePersonalized(true);
  	}
  	
  	if (document.content.UpcConUserID.value == "0"){
  		ShowHidePersonalizedPage(false);
  	}
  };  

  dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
  
  //Set up the Content Managers on the left menu
  if (contentID != null){
  	ajax_loadContentManager('content_manager','../cms00000/cms00000content_manager.jsp?ConID='+contentID);
  }
  

} 

function ajax_showContent(divId,ajaxIndex,pathToFile)  //dynamically loading the content in the page
{
  document.getElementById(divId).innerHTML =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[pathToFile] = 
    dynamicContent_ajaxObjects[ajaxIndex].response;
  }
  dynamicContent_ajaxObjects[ajaxIndex] = false;
}


window.onresize= message;
function message() {
	  AdjustModePixel();
}

function CallContent(ConID,MenuOption,CatID,CallNext){       //calling another content using AJAX without re-loading the whole page
 
	hide_iframe(MenuOption);
	var EventBackPreviousPage = "";
	
	if (CatID != null){
		buildMenu('content_tree_menu','cms00000tree_menu.jsp?CatID='+CatID,'cms_tree',ConID);
		dhtmlHistory.add(ConID+',SW,'+CatID,'');
	}
	ajax_loadContent('content_one','../cms00230/cms00230portlet.jsp?ConID='+ConID+EventBackPreviousPage,ConID,false,CallNext);
	

}

function CallContentSearch(ConID,MenuOption,CatID){

	dhtmlHistory.add('search','');
	window.setTimeout("CallContent("+ConID+",'"+MenuOption+"',"+CatID+")",500);
}

function buildMenu(ajax_content, path_file, tree_menu, ConID){


	idOfFolderTrees = [tree_menu];
	ajax_loadContent_tree(ajax_content,path_file);
	document.content.ConID.value = ConID;
	//ajax_loadContent('content_one','../cms00010/cms00010index.jsp?ConID='+ConID);


}

function CallHomePage(){

	document.content.ConHomePageYN.value = "Y";
	var ConID = 1;
	var CatID = 10;
	var EventBackPreviousPage = "";

	
	hide_iframe('NM');
	ajax_loadContent('content_one','../cms00230/cms00230portlet.jsp?ConID='+ConID+EventBackPreviousPage,ConID);
	dhtmlHistory.add('index');

	
    ShowHidePersonalizedPage(true);
	ShowHidePersonalized(true);

	var obj = document.getElementById('content_person');	
	document.getElementById('content_person').style.display = "block";
    obj.style.visibility = "visible";
    obj.style.width = "100%";
    
    ClearMenuTabColor();
    
}

function ClearMenuTabColor(){

	$("#main-nav").css("background-position", "0 0");
	position_clicked = null;
	
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{

	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function checkEnter(e, forms_id, txtEmail){ //e is event object passed from function invocation


	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		
		
		
		if (checkEmail(txtEmail)){
			var form_element = document.getElementById(forms_id);
			//form_element.submit(); //submit the form
			//return false ;
			
			//alert('form_element.txtEmail.value='+form_element.txtEmail.value);

			setCookie('email',txtEmail,1);
			CallContent(2993,'SW',24);
			return false ;
		}
		else{
			return false;
		}
		
	}
	else{
		return true;
	}

}

function checkEmail (txtEmail){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var StaValid = true;
    
    if (StaValid && reg.test(txtEmail) == false) {
      alert('Invalid Email Address');
      StaValid = false;
    }
    
    return StaValid;
}
