/* $Header: /usr/local/cvsmm/any_wizard/templates/js_wizard.tpl,v 1.7 2004/12/14 17:06:17 bas Exp $ */



// Function:	getObj
// Parameters:	name of object
// Returns:		object
// Description:	creates object with right DOM
//
function getObj(name)
{
  if (document.getElementById && document.getElementById(name))
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all && document.all[name])
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers && document.layers[name])
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

// Function:	getObjNN4
// Parameters:	name of object
// Returns:		layer
// Description:	creates object with right nested layers for NN4 
//
function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// Function:	trim
// Parameters:	strText (string)
// Returns:		string without leading and trailing spaces
// Description:	trims leading and trailing spaces from a given string
//
function trim(strText) 
{ 
	while (strText.substring(0,1) == ' ') 
    	strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
    	strText = strText.substring(0, strText.length-1);
	return strText;
}


// Function:	Browser
// Parameters:	-
// Returns:		bi		browser check object
// Description:	Browsercheck
//
function Browser() {
	this.name = navigator.appName;
	if (this.name == "Netscape") this.name = "nn";
	else if (this.name == "Microsoft Internet Explorer") this.name = "ie";
	this.version = parseFloat(navigator.appVersion);
	this.ie = (this.name == "ie");
	this.nn = (this.name == "nn");
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0);
	if (!this.ie4) {this.ie4 = (navigator.userAgent.indexOf('msie 4')>0);}
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0);
	if (!this.ie5) {this.ie5 = (navigator.userAgent.indexOf('msie 5')>0);}
	this.ie6 = (navigator.userAgent.indexOf('MSIE 6')>0);		
	this.saf = (navigator.userAgent.indexOf("Safari")>0);
	this.nn4 = ((this.name == "nn") && (this.version >= 4.08) && (this.version < 5));
	this.nn45 = ((this.name == "nn") && (this.version >= 4.5) && (this.version < 5));		
	this.nn6 = ((this.name == "nn") && (this.version >= 5));

	this.mac = (navigator.appVersion.indexOf("Macintosh") > 0);
	this.win = (navigator.appVersion.indexOf("Win") > 0);
	this.ie5mac = (this.ie5 && this.mac);
}
var bi = new Browser();



// Function:	setdirty
// Prarameters:	-
// Returns:		-
// Description:	sets a formfield to dirty if changed
//
function setdirty()
{
	form = document.forms["wizard"];
	if (form != null)
	{
		form.elements["u_thg[dirty]"].value = "1";
	}
}

// Function:	isdirty
// Parameters:	-
// returns:		dirty flag
// Description:	checks if the form is changed
//
function isdirty()
{
	form = document.forms["wizard"];
	return form != null && form.elements["u_thg[dirty]"].value == "1";
}


// Function:	manage tabs
// Parameters:	tab			active tab
// returns:		-
// Description:	manage tabs of edit forms
//
function setTab(tab)
{
	var oldobj  = new getObj(acttab+'menuitem');
	var actobj  = new getObj(tab+'menuitem');

	oldobj.obj.className   = acttab+"menuitem";
	actobj.obj.className   = tab+"menuitemact";

	acttab = tab;
}

function swapHideShowObj(o) {
	var x = new getObj(o);
	if(x.style.display == "block")
		hideObj(o)
	else 
		showObj(o);
}

function showObj(o)
{
	var x = new getObj(o);
	if (x.obj) x.style.display = "block";
}

function hideObj(o) 
{
	var x = new getObj(o);
	if (x.obj) x.style.display = "none";
}

function hideAllTabs() 
{
	for (i=0;i<maxtab;i++) hideObj('tab'+i);
}

var hastabs = false;
function init()
{
	if (hastabs)
	{
		setTab(acttab);
		for (i=0;i<maxtab;i++)
		{
			 showObj('tab'+i);
		}
		hideAllTabs();
		setTab(acttab);
		showObj(acttab);
		showObj('tabs');
	}
}

/*****************************************/
/** Modifier Keys 1.0, augustus 2003    **/
/** ppk, http://www.quirksmode.org      **/
/*****************************************/

var W3CDOM = (document.getElementsByTagName && document.createElement);

function initializeModifierKeys()
{
	if (!W3CDOM) return;
	document.onkeydown = initModifierKey;
}

var keyPressed;
function initModifierKey(e)
{
	if (!e) var e = window.event;
	keyPressed = e.keyCode;
}

function action_on_enter(u_cmd, s_cmd, t_cmd, s_colnr, params)
{
	if (keyPressed == 13)
	{
		any_action (u_cmd, s_cmd, t_cmd, s_colnr, params);
	}
}

// Function:	any_action
// Parameters:	u_cmd: update command
//				s_cmd: stack cpommand
//				s_colnr: column nr of the stack
//				t_cmd: target command
//				params: array of xtra parameters
// Returns:		submits a form
// Description:	if there's a u_cmd submit any_action form with method post
//				else submit any_get form with method post
//

var gototab		= 'tab1';
var gotofield	= 'x';

function any_action (u_cmd, s_cmd, t_cmd, s_colnr, params)
{
	controller_name = "wizard.php";
	form			= document.forms["wizard"];
	f_submit		= true;
	
	if (form.elements["u_thg[has_lang]"])
	{
		var has_lang	= form.elements["u_thg[has_lang]"].value;
	}

	if (u_cmd != null)/* && isdirty()*/
	{
		if (!validate())
		{
			if (hastabs)
			{
				hideAllTabs();
				setTab(gototab);
				showObj(gototab);
			}
			if (gotofield != 'x')
			{
				gotofield.focus();
			}
			else
			{
				window.scroll(0,0);
			}
			f_submit = false;
		}
	}

	if (f_submit == true)
	{
		if (params)
		{
			var params = params.split(',');
			for (i=0;i<params.length;i++)
			{
				var par = params[i].split('=');
				var n = trim(par[0]);
				var v = trim(par[1]);
				
				if (form.elements[n])
				{
					if (n == "t_list_search[kind]" || n == "t_connect")
					{
						v = v.replace(/;/g, ",");
					}
//					alert(n+"  "+v);
					form.elements[n].value = v;
				}
			}
		}


		if (form.elements["u_thg[name][last]"])
		{
			// set title for person
			// title = firstname + voorvoegsel + lastname
			var firstname	= form.elements["u_thg[name][first]"];
			var voorvoegsel = form.elements["u_thg[name][voorvoegsel]"];
			var lastname	= form.elements["u_thg[name][last]"];
			if (firstname && voorvoegsel && lastname)
			{
				var firstname	= getvalue(firstname);
				var voorvoegsel = getvalue(voorvoegsel);
				var lastname	= getvalue(lastname);
				if (isSizeSmaller(firstname, 1)) f_space = "";
				else f_space = " ";
				if (isSizeSmaller(voorvoegsel, 1)) t_space = "";
				else t_space = " ";
				form.elements["u_thg[lang]["+has_lang+"][title]"].value = ""+firstname+f_space+voorvoegsel+t_space+lastname+"";
			}
		}

		if (form.elements["only_date_start_year"])
		{
			var onlydateyear	= form.elements["only_date_start_year"];
			if(onlydateyear)
			{
				var onlydatestartyear = getvalue(onlydateyear);
				var datestartyear	  = getvalue(form.elements["u_thg[date_start_year]"]);
				var dateendyear	      = getvalue(form.elements["u_thg[date_end_year]"]);

				if(isSizeSmaller(onlydatestartyear,1))
				{
					form.elements["u_thg[date_start_year]"].value = datestartyear;
					form.elements["u_thg[date_end_year]"].value   = dateendyear;
				}
				else
				{
					form.elements["u_thg[date_start_year]"].value = onlydatestartyear;
					form.elements["u_thg[date_end_year]"].value   = dateendyear;
				}
			}
		}
		form.elements["u_cmd"].value 	= u_cmd;
		form.elements["s_cmd"].value 	= s_cmd;
		form.elements["t_cmd"].value 	= t_cmd;
	
		if (t_cmd == 'edit' && s_cmd == 'open')
		{
			form.elements["t_tab"].value = gototab;
		}
		else if(t_cmd != 'new')
		{
			t = getvalue(form.elements["t_tab"]);
			if (isSizeSmaller(t, 1) && acttab)
			{
				form.elements["t_tab"].value = acttab;
			}
		}
		form.submit();
	}	
}


// Function:	validate
// Parameters:	form
//				save
// Returns:		true if from is valid
// Description:	validate a form
//
function validate()
{
	var form		= document.forms["wizard"];
	if (form.elements["u_thg[has_lang]"])
	{
		var has_lang	= form.elements["u_thg[has_lang]"].value;
	}
	
	var msg			= "";
	var itsName		= "";
	var itsValue	= "";
	var isValid		= true;
	var checkmsg	= "";

	if (form)
	{
		// Check title
		var title = form.elements["u_thg[lang]["+has_lang+"][title]"];
		if (title)
		{
			itsValue = getvalue(title);
			if (isSizeSmaller(itsValue, 1))
			{	
				gotofield  = title;
				msg		  += "De titel is een verplicht veld\n";
				gototab	   = 'tab1';
			}
		}

		// check lastname for a person
		var lastname = form.elements["u_thg[name][last]"];
		if (lastname)
		{
			itsValue = getvalue(lastname);
			if (isSizeSmaller(itsValue, 1))
			{	
				gotofield  = lastname;
				msg		  += "De achternaam is een verplicht veld\n";
				gototab	   = "tab1";
			}
		}

		if (msg.length>0) isValid=false;

	}	
	if (!isValid)
	{
		alert(msg);
	}
	return isValid;	
}


// Function:	addHiddenField
// Parameters:	n name of hidden field
//				v value of hidden field
// Returns:		hf		hidden field string
// Description:	creates hidden field
//
function addHiddenField(n,v)
{	
	var hf='<input type="hidden" name="'+n+'" value="'+v+'"> ';
	return hf;
}


// Function:	changeHeightTextarea
// Parameters:	ta	textarea_id
//				hf  hidden field which will send the new array number
// Returns:		null when ok
// Description:	sets height of teh given area depending on a given array
//
function changeHeightTextarea(ta,hf)
{
	if (eval(ta+'_act == '+ta+'_Array.length')) eval(ta+'_act = 0');
	var ta_o = new getObj(ta);
	ta_o.style.height = eval(ta+'_Array['+ta+'_act]');
	document.forms["wizard"].elements["u_thg[edit]["+hf+"]"].value = eval(ta+"_act");
	eval(ta+'_act++');
}

function getvalue(ffield)
{
  v="";
  t=ffield.type;
  if(!t)t=ffield[0].type;
  if(t=="checkbox")if(ffield.checked)v=ffield.value;
  if(t=="file") v=ffield.value;
  if(t=="hidden") v=ffield.value;
  if(t=="password") v=ffield.value;

  if(t=="radio")
  {
  	if(ffield.length){for(gv_i=0;gv_i<ffield.length;gv_i++)if(ffield[gv_i].checked) v=ffield[gv_i].value;}  
    else{if(ffield.checked) v=ffield.value;}	
  }
    
  if(t=="select-multiple")  v=ffield.options[ffield.selectedIndex].value;
  if(t=="select-one")  v=ffield.options[ffield.selectedIndex].value;
  if(t=="text") v=ffield.value;
  if(t=="textarea") v=ffield.value;
  
  return v;
}

function isSizeSmaller(theValue, size)
{
	if (theValue.length < size) return true;
	else return false;
}



