function updateselect(id, fields)
{
	varindex =getindex(id);
	vars = new Array();
	for (x=0; x<fields; x++)
	{
		varname = id + "vardata" +x;
		//alert ("test");
		vars= getval(varname).split("::");
		//alert ("test");
		varfield = id + "var" +x;
		if (varindex>0)
		{
		//alert (varfield);
		value = vars[varindex-1].substring(1, vars[varindex-1].length-1);
		setval (varfield, value);
		//alert ("test");
		}
		else
		{
		setval (varfield, "");
		}
	}
}

function xmladdto(name, nvars)
{
	basename = "xat" + name;
	index_name = basename +"select";
	//alert(index_name);
	index = getindex(index_name);
	//alert(index);
	vars= new Array();
	temp = new Array();
	output = "";
	for (x = 0;x<nvars; x++)
	{
		varname = basename + "vardata" +x;
		//alert(varname);
		temp = getval(varname).split("::");
		temp_string = temp[index].substring(1, temp[index].length-1);
		if (output == "")
		{
			output = (temp_string);
		}
		else if (contact != "" && temp_string != "")
		{
			output = output + " / " + temp_string;
		}
	}
	current = getval(name);
	if (current != "") output = current + " -- " + output;
	end  = loselect(index_name);
	setval (name, output);
}

function timeupdate(name)
{
	hour = getoption("hour"+name,getindex("hour"+name));
	min = getoption("min"+name,getindex("min"+name));
	ampm = getoption("ampm"+name,getindex("ampm"+name));
	time="";
	if (ampm == "AM")
	{
		if (hour == "12") hour = "00";
		if (parseInt(hour)<12)
		{
			time = hour+min;
		}
		else
		{
			time = "00"+min;
		}
	}
	else
	{
//alert("PM");
		if (parseInt(hour)<12)
		{
//alert(parseInt(hour));
			if (hour < 10)
			{
			hour = 12+parseInt(hour.substring(1,2));
			}
			else
			{
			hour = 12+parseInt(hour);
			}
			//if (hour<10) hour = "0" + hour;
			time = hour+min;
//alert(hour);
		}
		else
		{
			time = hour+min;
		}
	}
	setval (name, time);
}

function dropfile(name, item)
{
    was = getval(name);
	isat = was.indexOf(item);
	before = was.substr(0, isat);
	after = was.substr(isat+item.length)
    all = before+after;
	all = all.replace("::::", "::");
	front = all.substr(0,2);
	//alert(front);
	if (front ==  "::") all=all.substr(2, all.length);
	back = all.substr(all.length-2, all.length);
	//alert(back);
	if (back == "::") all = all.substr(0, all.length-2);
	//alert(all);
	allis = all.split("::");
	
	output ="";
	for (x=0; x<allis.length; x++)
	{
		if (allis[x] != "")
		{
			output = output+ allis[x] + "<input type='button' value='X' onclick='dropfile("+'"'+name+'","'+allis[x]+'"'+")'><br/>"; 
		}
	}
	document.getElementById('myold' + name).innerHTML = output;
	setval(name, all);
}

months = new Array('Jan', 'Feb', 'Mar', 'Apl', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
nodays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function changemonth(ts, thiscal)
{
	nameis = thiscal.substring(3, thiscal.length);
	//alert(nameis);
	time = ts.split(",");
	seldate = getval("show"+nameis).split("-");
        //alert (seldate[0]);
	//now = new Date();
	now_year = seldate[0];//now.getFullYear();
	now_month = parseInt(seldate[1])-1;//now.getMonth();
	//alert(now_month);
	now_day = parseInt(seldate[2]);//now.getDate();
	//alert (now);
	//alert (ts);
	
	thisdate = new Date();
	//alert (time);
	thisdate.setFullYear(parseInt(time[0]),(parseInt(time[1])-1),parseInt(time[2]));
	//alert (now+" - "+thisdate);
	month = thisdate.getMonth();
        //alert(month);
	year = thisdate.getFullYear();
	//alert (month);
	//alert (year);
	fdom = new Date;
	fdom.setFullYear(parseInt(time[0]), (parseInt(time[1])-1), 1);
	//alert (fdom);
	fdow = fdom.getUTCDay();
	//alert (fdow);
	monthname = months[thisdate.getUTCMonth()] + " " + thisdate.getFullYear();
	//alert (monthname);
	setval(thiscal+'monthname', monthname);
	setval(thiscal+'lastmonth_ts', thisdate.getFullYear() +',' +(thisdate.getMonth()) + ',' + thisdate.getDate());
	setval(thiscal+'nextmonth_ts', thisdate.getFullYear() +',' +(thisdate.getMonth()+2) + ',' + thisdate.getDate());
	
	count = 1;
	day = 1-fdow;
	//alert (day);
	nod = nodays[month];
	//alert (loc);
	for (w=0; w<6; w++)
	{
		for (d=0; d<7; d++)
		{
			loc = thiscal + count;
			if (day > 0 && day < (nod +1))
			{
			document.getElementById(loc).innerHTML = "<input type='button' class='blankbutton' value='" + day +"' onclick='setdate("+'"'+nameis+'","'+day+'","'+loc+'"'+")' style='text-align:center; margin-bottom:0; width: 2em; '>";
			}
			else
			{
			document.getElementById(loc).innerHTML = '';
			}
			document.getElementById(loc).style.background ='white';
			document.getElementById(loc).style.color ='black';
			count++;
			day++;
		}
	}
	if (month == now_month && year == now_year)
	{
		//alert('test');
		wherenow = fdow + now_day;
		//alert (wherenow);
		loc = thiscal + wherenow;
                //alert (loc);
		document.getElementById(loc).style.background ='green';
		document.getElementById(loc).style.color ='black';
	}
	setval(thiscal + 'month', month+1);
	setval(thiscal + 'year', year);
}

function resetcal ()
{
	now = new Date();
	year = now.getFullYear();
	month = now.getMonth();
	day = now.getDate();
	changemonth (year +','+month+','+day, 'yar'); 
}

function setdate (name, day, id)
{
	mname = 'cal' +name+'month';
	//alert (mname);
	month = getval(mname);
        if (month.length < 2) month = "0"+month;
	//alert (month);
	year = getval ('cal' +name+'year');
	//alert (year);
        if (day < 10) 
        {
	date = year +'-'+month+'-'+'0'+day;
        //alert ('test');
        }
        else
        {
        date = year +'-'+month+'-'+day;
        }
	//alert (date);
	setval(name, date);
	setval('show' + name, date);
	clearcolors(name);
	setBGcolor (id, "green");
}

function clearcolors (name)
{
	count = 1;
	for (w=0;w<6;w++)
	{
		for(d=0;d<7;d++)
		{
			id= 'cal' + name + count;
			setBGcolor (id, "white");
			count++;
		}
	}
}

function setfile()
{
	dir = '$dir';
	name = '$name';
	filelist = 'filelist' + name;
	index = document.getElementById(filelist).selectedIndex;
	document.getElementById(name).value = dir+ document.getElementById(filelist).options[index].text;
}

