// This file must be in ANSI format.Netscape browser does NOT like javascript in unicode!

// Calculate the date 2 weeks from today (used to set cookie's expiration date)
exp2weeks = new Date(); exp2weeks.setTime(exp2weeks.getTime() + (14 *24*60*60*1000));
updateTimer = null;

ie = document.all?1:0;
ns4 = document.layers?1:0;


function Goto( url, target ){
	window.open( url , target, 'menubar=1,location=1,resizable=1,scrollbars=1,status=1,toolbar=1,titlebar=1screenX=1,screenY=1,height=400,width=750,left=1,top=1' );	
}


function StartAutoUpdate() {
	if( updateTimer != null ){
		clearInterval( updateTimer );
		updateTimer = null;
	}
	
	autoUpdate = GetListBoxValue(frm.autoUpdate);
	if( autoUpdate != 0 ){
		updateTimer = setInterval( "UpdateDirectory()", autoUpdate * 1000 );
	}
}


function UpdateDirectory(){
	for( var i=0; i<window.frames.length; i++ ){
		var w = window.frames[i];
		if( w.name == 'iframeAutoUpdate' ){
			w.window.location = "directoryupdate.php?lastUpdate=" + lastUpdate;
		}
	}
}


function Reload(){
	frm.task.value = "reload";
	// use location.replace instead of submit so that
	// browser won't add an entry in the BACK list
	//frm.submit();
	var param="";
	for( var i=0; i<frm.elements.length; i++ ){
		var e = frm.elements[i];
		if( !( e.type == 'radio' && !e.checked )){
			param += e.name + "=" + escape(e.value) + "&";
		}
	}
	window.location.replace( "index.php?" + param );
}


function Enter(userId){
	frm.userId.value = userId;
	frm.task.value = "enter";

	if( GetRadioGroupValue( frm.mode ) == "sendinvitation" && GetListBoxValue( frm.invitFormat) == "email" ){
		window.open( 
			frm.action
			+ "?task=" + frm.task.value
			+ "&userId=" + frm.userId.value
			+ "&language=" + GetListBoxValue( frm.language )
			+ "&mode=" + GetRadioGroupValue( frm.mode )
			+ "&invitFormat=" + GetListBoxValue( frm.invitFormat),
			"_blank", "menubar=0,location=0,resizable=1,scrollbars=1,status=0,toolbar=0,titlebar=1screenX=1,screenY=1,height=100,width=400,left=1,top=1" );
		return;
	}

	frm.submit();
}



function GetRadioGroupValue( ctrl ){
	for( var i = 0; i < ctrl.length; i++ ){
		if( ctrl[i].checked ) {
			return ctrl[i].value;
		}
	}
	return "";
}


function GetListBoxValue( ctrl ){
	for( var i = ctrl.length-1; i >= 0 ; i-- ){
		if( ctrl[i].selected ) {
			return ctrl[i].value;		
		}
	}
	return 0;
}



function SavePreference(){
	SetCookie( "autoUpdate", GetListBoxValue(frm.autoUpdate), exp2weeks );
	SetCookie( "invitFormat", GetListBoxValue(frm.invitFormat), exp2weeks );
	SetCookie( "language", GetListBoxValue(frm.language), exp2weeks );
	SetCookie( "column", GetListBoxValue(frm.column), exp2weeks );
	SetCookie( "direction", GetListBoxValue(frm.direction), exp2weeks );
	SetCookie( "mode", GetRadioGroupValue(frm.mode), null );
}



function ChangeInvitFormat(){
	SavePreference();
}


function ChangeLanguage( sLang ){
	if( sLang )
		frm.language.value = sLang;
	SavePreference();
	Reload();
}


function ChangeColumn(){
	SavePreference();
	Reload();
}

function ChangeDirection(){
	SavePreference();
	Reload();
}

function ChangeAutoUpdate(){
	SavePreference();
	StartAutoUpdate();
	UpdateDirectory();
}


function ChangeMode(){
	// update selection highlight	
	for( var i=0; i<frm.elements.length; i++ ){
		var e = frm.elements[i];
		if(( e.type == 'radio' )&&( e.name == 'mode' )){
			if(e.checked)
				ChangeClass(e,'P','actionHighlight');
			else
				ChangeClass(e,'P','');
		}
	}
	SavePreference();
}	



function SetCookie (name, value, expires) {	
	document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
}


function ChangeClass(E,T,C){
	if (ie){
		while (E.tagName!=T)
			E=E.parentElement;
	} else {
		while (E.tagName!=T)
			E=E.parentNode;
	}
	E.className = C;
}


function findObj(n, d) {
	var p,i,x;	
	if(!d) 
		d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) 
		x=document.getElementById(n); 
	return x;
}


function CT(id) {
  var obj = findObj(id+"col");
  if (obj && obj.style) {
	if (obj.style.display == "none"){
	  obj.style.display = "block";
	  obj = findObj(id+"exp");
	  obj.style.display = "none";
	} else {
	  obj.style.display = "none";
	  obj = findObj(id+"exp");
	  obj.style.display = "block";
	}
  }


}

