// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();

var urlwindow;
var editwindow;
var targetlink;


// these variables holds the targetnames for the form in which the values
// should be placed
var f_width;
var f_height;
var f_src;
var f_alt;

// these variables holds the values that are to be passed
var a_width;
var a_height;
var a_src;

// a variable for the form in a parentwindow
var parentformname;

// a variable for the palette window
var cpalettewindow;
var cpalettewindow2;

// previewwindow
var previewwindow;

// media window
var getMediaWindow;

// if page is loaded
var pageIsLoaded = false;

//these are the parameters used to open the window
var openwindowstring = ',scrollbars=yes,resizable=yes,status=yes,toolbar=no,menubar=no';
var openwindowstring2 = ',scrollbars=no,resizable=no,status=no,toolbar=no,menubar=no';

// determines if windows should close
var windowclose = true;

// detect browser
function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

//function toggle(target)
//{
//	obj=document.getElementById(target);
//	obj.style.display=( (obj.style.display=='none') ? '' : 'none');
//}
//
function hide(target)
{
	obj=document.getElementById(target);
	obj.style.display='none';
}

function show(target)
{
	obj=document.getElementById(target);
	obj.style.display='';
	
}

function tabHide(target)
{
	obj=document.getElementById('con'+target);
	obj.style.display='none';
	obj=document.getElementById('tab'+target);
	obj.className = 'tab2';
}

function tabShow(target)
{
	obj=document.getElementById('con'+target);
	obj.style.display='';
	obj=document.getElementById('tab'+target);
	obj.className = 'activeTab tab2';
}

function disable(target)
{
	//var tempx = new getObj(target);
	
	//tempx.disabled = 'disabled';

	
	obj= self.document.getElementById(target);
	if (obj != null){
		obj.disabled='disabled';
	}
}

function enable(target)
{
	obj=document.getElementById(target);
	if (obj != null){
		obj.disabled='';
	}
}

//function toggleimage(a,b,c){
//	if(document.images)
//	{
//		if ((document.images[a].src).toString()==b)
//		{
//			//document.images[a].src=c;
//		}
//	else
//		{
//			//document.images[a].src=b;
//		}
//	}
//}	

function form_submit(formname){
	
	temp = getRef(formname);
	temp.submit();
}

function checkUrl () {
	if (urlwindow)
	{
		urlwindow.close();
	}
}

function fetchurl(urlvar, targetlink2){
	//alert('test');
	checkUrl();
	urlwindow = window.open('/admin/media/fetchurl/index.html', 'Fetch','width=800,height=600');
	
	if (urlwindow.body.location.href != null){
		urlwindow.body.location.href = urlvar;
	}
	if (urlwindow.head.document.formName.textName.value != null){
		urlwindow.head.document.formName.textName.value = urlvar;;
	}
	targetlink = this.targetlink2;
}

function sendbackurl(thisurl){
	document.pageedit.targetlink = toString(urlwindow.body.location.href);
	urlwindow.close();
}

// this function is used to flip classes in the stylesheet

function getRef(obj){
	
	//alert(document.getElementById);
	
	if(typeof obj == "string")
	{
		obj = document.getElementById((obj.toString()));
		//alert(typeof obj);
	}
	else
	{
		//alert('possible error');
	
	}
	return obj;
}

// this function is used to flip classes in the stylesheet

function setClassName(obj, newclass){
    
    if (obj != null)
    {
    temp = getRef(obj);
    
    //alert('setClassName ('+obj+','+newclass+')'); 
   // temp.className = newclass;
    
    temp.className = newclass;

    	
    }
    

    

}

// this function opens the edit window for a single component

function checkComponentWindow (){
	if (editwindow)
	{
		editwindow.close();
	}
}

function editComponent(path){
	//checkComponentWindow();
	editwindow = window.open(path, 'Edit','width=700,height=600'+openwindowstring);
	editwindow.focus();

}


// this function refreshes the window

function refresh()
{
	window.location.reload( false );
}

function checkComponentPaletteWindow ()
{
	if (cpalettewindow)
	{
		cpalettewindow.close();
	}
}

function getComponent(path,aformname)
{
	//checkComponentPaletteWindow();
	cpalettewindow = window.open(path, 'Edit','width=700,height=600'+openwindowstring);
	cpalettewindow.focus();
	//formname = aformname;
	//alert (formname);
}


function getNewWindow(path,xSize,ySize,w_id)
{
	
	if(!xSize)
	{
		xSize=500;
	}
	
	if(!ySize)
	{
		ySize=450;
	}
	
	var xMax = screen.width;
	var yMax = screen.height;
	
	var xOffset = (xMax - xSize)/2;
	var yOffset = (yMax - ySize)/2;
	

	cpalettewindow = window.open(path, 'Edit','width='+xSize+',height='+ySize+',screenX='+xOffset+',screenY='+yOffset+',left='+xOffset+',top='+yOffset+openwindowstring);

	
	cpalettewindow.focus();
	//formname = aformname;
	//alert (formname);
	
	return void(0);
	

}

function getNewWindow2(path,xSize,ySize,w_id)
{
	
	if(!xSize)
	{
		xSize=500;
	}
	
	if(!ySize)
	{
		ySize=450;
	}
	
	var xMax = screen.width;
	var yMax = screen.height;
	
	var xOffset = (xMax - xSize)/2;
	var yOffset = (yMax - ySize)/2;
	
	cpalettewindow2 = window.open(path, 'Edit2','width='+xSize+',height='+ySize+',screenX='+xOffset+',screenY='+yOffset+',left='+xOffset+',top='+yOffset+openwindowstring2);

	cpalettewindow2.focus();
	//formname = aformname;
	//alert (formname);
	
	return void(0);
	

}


function getMediaWindowCheck ()
{
	if (getMediaWindow)
	{
		getMediaWindow.close();
	}
}

function getMedia(path)
{
	//getMediaWindowCheck();
	getMediaWindow = window.open(path, 'Windowmedia','width=700,height=600'+openwindowstring);
	getMediaWindow.focus();
	//formname = aformname;
	//alert (formname);
}

var getHelpWindow;

function getHelp(path)
{
	//getMediaWindowCheck();
	getHelpWindow = window.open(path, '','width=400,height=300'+openwindowstring);
	getHelpWindow.focus();
	//formname = aformname;
	//alert (formname);
}

var checkUpdateWindow;

function checkUpdate(path)
{

	checkUpdateWindow = window.open(path, 'checkUpdate','width=460,height=510'+openwindowstring);
	checkUpdateWindow.focus();
	//formname = aformname;
	//alert (formname);
}

function setMedia(argWidth,argHeight,argSrc)
{
	// from the openerform we have the values
	// that are supposed to be changed
	
	//alert('test');
	
	var namethis = self.opener.document.f_width;		
	
	//alert(namethis);
	
	var a =  window.opener.document.getElementById(namethis);
	
	//alert(a);
	
	a.value = argWidth;

	var namethis =  window.opener.document.f_height;		
	
	var openerform =  window.opener.document.getElementById(namethis);
	
	openerform.value = argHeight;
	
	var namethis =  window.opener.document.f_src;		
	
	var openerform =  window.opener.document.getElementById(namethis);
	
	openerform.value = argSrc;
	
	opener.document.pageedit.submit();
	
	if(this.windowclose){
	
		this.close();
	
	}
	
}


function checkPreviewWindow ()
{
	if (previewwindow)
	{
		previewwindow.close();
	}
}

function preview(path,widtharg,heightarg)
{
	checkPreviewWindow();
	previewwindow = window.open(path, 'Preview','width='+widtharg+',height='+heightarg+openwindowstring);
	previewwindow.focus();
	//formname = aformname;
	//alert (formname);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}


function setTemplateTo(atemplatename)
{
	//alert(opener.document.varformname);
	
	//alert(atemplatename);
	
	
	parentformname =  getQueryVariable('parentformname') ;
	
	var openerform = self.opener.document.getElementById(parentformname);
	
	//alert(openerform);
	
	//alert(opener.document.pageIsLoaded);
	
	openerform.nt.value = atemplatename;
	
	
	//alert (parentformname);
	//openerform.nt.value = atemplatename;
	openerform.submit();
	if(windowclose){
		this.close();
	}
}

var a_activelayer;
var b_activelayer;
var locklayer = false;

function hideToolBox ()
{
	//alert('hideToolBoxDelay ('+a+','+b+')');
	// only start the hide process if it is not already started
	
	// we therefore check if class type of a is hilited, if it is we don't want the 
	// timer started

	setClassName(a_activelayer,'editmodelo');	
	setClassName(b_activelayer,'edittoolslo');

	//temp = getRef(a);
	
	a_activelayer = null;
	b_activelayer = null;
	
	//if (temp.className == 'editmodehi'){
	//	setClassName(a,'editmodewlo');
	//	setTimeout('hideToolBoxDelay ('+a+','+b+');', 3000);
	//}
}

function showToolBox (a, b)
{
	if (!locklayer)
	{
		if (a != a_activelayer)
		{
			
			hideToolBox();
			
			//alert(b.siblingAbove);
			
			setClassName(a,'editmodehi');
			setClassName(b,'edittoolshi');
			//var browser = new Browser();
			
			a_activelayer = a;
			b_activelayer = b;
		
			//setTimeout('hideToolBox ();', 100000);
			
			locklayer = true;
			setTimeout('locklayer=false;', 200);
		}
	}


	//a.className='editmodehi';
	//temp = getRef(a);
	
	//if (temp.className == 'editmodelo'){
	//	setClassName(a,'editmodehi');
	//	setClassName(b,'edittoolshi');
	//}
	//alert ('hello');
	return void(0);
}

// following functions are used for the zandale_view selector

function firstView(){
	document.zandale_view.SHOW_FROM.value=0;
	//document.zandale_view.SHOW_ROWS.value=20;
	document.zandale_view.submit();

}

function resetView(){
	document.zandale_view.SHOW_FROM.value=0;
	//document.zandale_view.SHOW_ROWS.value=20;
	//document.zandale_view.submit();

}

function prevView(){
	
	// show previous resultset
	
	if (parseInt(document.zandale_view.SHOW_FROM.value) < parseInt(document.zandale_view.SHOW_ROWS.value)  )
	{
		document.zandale_view.SHOW_FROM.value=0;
	}
	else
	{
		document.zandale_view.SHOW_FROM.value = (parseInt(document.zandale_view.SHOW_FROM.value)-parseInt(document.zandale_view.SHOW_ROWS.value));
	}
	

	//document.zandale_view.SHOW_ROWS.value=(parseInt(document.zandale_view.SHOW_FROM.value)+20);

	document.zandale_view.submit();
}

function nextView(maxresults){
	
	// show previous resultset
	
	if ( ( parseInt(document.zandale_view.SHOW_FROM.value) + parseInt(document.zandale_view.SHOW_ROWS.value) ) > (parseInt(maxresults)))
	{
		//document.zandale_view.SHOW_FROM.value = ( parseInt(maxresults) - parseInt(document.zandale_view.SHOW_ROWS.value) );
	}
	else
	{
		document.zandale_view.SHOW_FROM.value = (parseInt(document.zandale_view.SHOW_FROM.value)+parseInt(document.zandale_view.SHOW_ROWS.value));
	}
	

	//document.zandale_view.SHOW_ROWS.value=(parseInt(document.zandale_view.SHOW_FROM.value)+20);

	document.zandale_view.submit();
	

}

function lastView(maxresults)
{
	
	//alert('results'+parseInt(maxresults)+' - show rows '+parseInt(document.zandale_view.SHOW_ROWS.value));
	a =(parseInt(maxresults))-parseInt(document.zandale_view.SHOW_ROWS.value);
	if (a<0){
		a = 0;
	}
	document.zandale_view.SHOW_FROM.value = a;
	document.zandale_view.submit();

}

function initZandaleView(maxresults)
{
	if (maxresults == null){
	
	  	maxresults = 0;
		
	}
	
	if (parseInt(document.zandale_view.SHOW_FROM.value) != 0 )
	{
		enable('zandale_view_first');
		enable('zandale_view_prev');

	}

	if ( ( parseInt(document.zandale_view.SHOW_FROM.value) + parseInt(document.zandale_view.SHOW_ROWS.value) ) >= (parseInt(maxresults)) )
	{

	}
else
	{
		enable('zandale_view_next');
		enable('zandale_view_last');
	}		

}


// Browser Detection
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;


function SafeAddOnload(f)
{

	if (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
	{
		window.onload = f;
	}
}

function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}

// javascripts used for component edit buttons

function ceditMoveUp(cid)
{
	
}
		