// error s in field f
function um_form_napaka(s,f) {
	alert(s);
	if ( f.focus ) f.focus();
}


// new popup window
function um_form_okno(url) {

	var n = ( arguments[1] || 'Popup' );
	var x = ( arguments[2] || 0 );
	var y = ( arguments[3] || 0 );
	var p = ( arguments[4] || 'width=300,height=400,toolbar=0,resize=1,location=0,status=0,menubar=0,titlebar=0,scrollbars=1,resizable' ); //,alwaysRaised,dependent' );

	p = 'left=' + x + ',screenX=' + x + ',top=' + y + ',screenY=' + y + ',' + p;
	var win = window.open(url, n, p);
	win.opener = self;

	win.focus();

}

// file selector/manager
var um_form_fileControl = null;

function um_form_filebrowser(url) {
	um_form_fileControl = ( arguments[1] || null );
	um_form_okno( url, arguments[2], arguments[3], arguments[4], arguments[5] );
}

// new HTML editor
function um_form_html(f) {


	//var n = ( arguments[1] || 'Popup' );
	//var x = ( arguments[2] || 0 );
	//var y = ( arguments[3] || 0 );
	var p = ( arguments[4] || 'width=740,height=592,toolbar=0,resize=1,location=0,status=1,menubar=0,titlebar=0,scrollbars=1,resizable' ); //,alwaysRaised,dependent' );

	//p = 'left=' + x + ',screenX=' + x + ',top=' + y + ',screenY=' + y + ',' + p;

	window.um_form_html_edit = f;
	
	// starting value will be set in editor - opener.inEdit

	//var win = window.open("u.asp", n, p);
	//win.opener = self;
	//win.focus();

	um_form_okno("/admin/aurora/edit.asp?action=edit", "Editor", arguments[2], arguments[3], p);

}

// multiple_values join
function um_form_submit(f) {
	// travel each and every field,
	// if name ends with "_x_" join all values into one "|"-separated string
	// and assign it to field with same name but w/o "_x_"

	var s = "", n = "";

	for ( var i = 0; i < f.elements.length; i++ ) {
		if ( f.elements[i].name.substr(0,3) == "_x_" && f.elements[i].type.substr(0,6) == "select" ) {
			n = f.elements[i].name.substr(3);
			for ( j = 0; j < f.elements[i].options.length; j++ )
			  s += '|' + f.elements[i].options[j].text;
		}
	}


	// transfer all DIV's
	// if name ends with _a_ transfer rich text control content into hidden field with same name but w/o "_a_"
	var fDIVs = document.getElementsByTagName("DIV");
	for ( var i = 0; i < fDIVs.length; i++ )
	{
		
		if ( fDIVs.item(i).name )
		{
			if ( fDIVs.item(i).name.substr(fDIVs.item(i).name.length-3,3) == "_a_" )
				f.elements[fDIVs.item(i).name.substr(0,fDIVs.item(i).name.length-3)].value = fDIVs.item(i).innerHTML;
		}
	}
	


	s = s.substr(1);
	if ( f.elements[n] ) f.elements[n].value = s;


	// can add additional validation, based on form name???

	// now everything is always fine
	return true;

}


// add value of f to list l
function um_form_dodajX(f,l) {
	if ( f.value != '' ) {
		var n = new Option( f.value, f.value, false, true );
		with ( l ) {
			options[length] = n;
		}
	} else {
		um_form_napaka( 'Vnesi vrednost!', f );
	}
}


// remove selected value(s) from list l
function um_form_odstraniX ( l ) {
	var del = false;

	with ( l ) {
		for ( var i = 0; i<length; i++ ) {
			if ( options[i].selected ) {
				options[i] = null;
				del = true;
			}
		}
	}

	if ( !del ) um_form_napaka( 'Najprej izberi vrednost', l );
}


// copy selected value of l in text field
function um_form_izberiX( l ) {
	var f = l.name.substr(3);
	if ( l.form.elements[f].value ) l.form.elements[f].value = l.options[l.selectedIndex].text;
}


// moves selected item(s) of list l in up or down direction
function um_form_premakniX( l, d ) {

	with ( l ) {
		if ( d ) {
			// up
			for ( var i = 1; i < length; i++ ) {
				if ( options[i].selected ) {
					t = options[i-1].text;
					options[i-1].text = options[i].text;
					options[i-1].selected = true;
					options[i].text = t;
					options[i].selected = false;
				}
			}
		} else {
			// down
			for ( var i = length-2; i >= 0; i-- ) {
				if ( options[i].selected ) {
					t = options[i+1].text;
					options[i+1].text = options[i].text;
					options[i+1].selected = true;
					options[i].text = t;
					options[i].selected = false;
				}
			}
		}
	}

}


// sort options of list l
function um_form_urediX ( l ) {
	var a = new Array();
	var i = 0;

	with ( l ) {
		for ( i = 0; i < length; i++ ) a[i] = options[i].text;
		a.sort(um_form_primerjajS);
		for ( i = 0; i < length; i++ ) options[i].text = a[i];
	}

}

// helper sort function for case insensitive sort
function um_form_primerjajS(a, b) {
	var anew = a.toLowerCase();
	var bnew = b.toLowerCase();
	if (anew < bnew) return -1;
	if (anew > bnew) return 1;
	return 0;
}




// form calendar helper function
function um_form_koledar(f, time)
{
	var cal = new UMClientCalendar("si", time);
	cal.showYearNavigation();
	cal.setFormField( f );
	var d = f.value.split(".", 3);
	if ( d.length == 3 )
		if ( d[2].length > 4 )
		{
			var h = d[2].substr(5).split(":");
			cal.setDate( d[2].substr(0,4), d[1], d[0], h[0], h[1] );
		}
		else
			cal.setDate( d[2], d[1], d[0] );

	cal.showCalendar( 100, 100 );
	return true;
}

/* *******************************************

	UMClientCalendar Object
	---
	client side javascript popup calendar
	- requires:
		CALENDAR_CSS - cascading style sheet

 ******************************************* */

var CALENDAR_CSS = "/inc/form.css";

// Constructor
function UMClientCalendar() {

	// references to popups...
	if ( !window.calendarPopupIndex ) { window.calendarPopupIndex = 0; }
	if ( !window.calendarPopups ) { window.calendarPopups = new Array(); }
	this.index = calendarPopupIndex++;
	calendarPopups[this.index] = this;

	// other language can be passed as param
	this.language = arguments[0] || 'en';
	this.showTime = arguments[1] || false;

	switch ( this.language ) {
		case 'si':
			this.monthNames = new Array('januar','februar','marec','april','maj','junij','julij','avgust','september','oktober','november','december');
			this.dayHeaders = new Array('N','P','T','S','È','P','S');
			this.weekStartDay = 1;
			this.words = new Array('windows-1250', 'Koledar', 'danes');
			break;

		default:
			this.monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
			this.dayHeaders = new Array('S','M','T','W','T','F','S');
			this.words = new Array('windows-1251', 'Calendar', 'Today');
			this.weekStartDay = 0;
	}

	this.isShowYearNavigation = false;
	this.calendarPopup = null;
	this.theDate = new Date(); this.theDate.setHours(0,0);
	this.formField = null;

	// Method mappings
	this.setDate = UMClientCalendar_setDate;
	this.setFormField = UMClientCalendar_setFormField;
	this.dateSelected = UMClientCalendar_dateSelected;
	this.setMonthNames = UMClientCalendar_setMonthNames;
	this.setDayHeaders = UMClientCalendar_setDayHeaders;
	this.setWeekStartDay = UMClientCalendar_setWeekStartDay;
	this.showYearNavigation = UMClientCalendar_showYearNavigation;
	this.showCalendar = UMClientCalendar_showCalendar;
	this.hideCalendar = UMClientCalendar_hideCalendar;
	this.refreshCalendar = UMClientCalendar_refreshCalendar;
	this.getCalendar = UMClientCalendar_getCalendar;
	this.getClock = UMClientCalendar_getClock;
	this.setTime = UMClientCalendar_setTime;

}


// callback...
function UMClientCalendar_dateSelected(i, y, m, d) {
	var f = window.calendarPopups[i].formField;
	var calDate = window.calendarPopups[i].theDate;
	var h = calDate.getHours();
	var min = calDate.getMinutes();

	switch ( window.calendarPopups[i].language ) {
		case "si":
			if (h != 0 || min != 0)
				f.value =  d + "." + m + "." + y + " " + h + ( min < 10 ? ":0" : ":" ) + min;
			else
				f.value =  d + "." + m + "." + y;
			break;

		default:
			if (h != 0 || min != 0)
				f.value =  m + "/" + d + "/" + y + " " + h + ( min < 10 ? ":0" : ":" ) + min;
			else
				f.value =  m + "/" + d + "/" + y;
	}

	UMClientCalendar_hideCalendar( i );
}



// Set the default date to display...
function UMClientCalendar_setDate() {
	var y = 0 + arguments[0];
	var m = 0 + arguments[1];
	var d = 0 + arguments[2];
	var h = 0 + arguments[3];
	var min = 0 + arguments[4];
	if ( y > 0 && m > 0 && m < 13 && d > 0 && d < 32 )
	{
		if ( h > 0 || min > 0 )
			this.theDate = new Date(y, m-1, d, h, min)
		else
			this.theDate = new Date(y, m-1, d)
	}
}


// Set the name of the form field to receive selected date
function UMClientCalendar_setFormField() {
	if ( arguments[0] ) this.formField = arguments[0];
}

// Set the name of the function to call to get the clicked date
function UMClientCalendar_setReturnFunction(name) {
	this.returnFunction = name;
}

// Over-ride the built-in month names
function UMClientCalendar_setMonthNames() {
	for (var i=0; i<arguments.length; i++) {
		this.monthNames[i] = arguments[i];
	}
}

// Over-ride the built-in column headers for each day
function UMClientCalendar_setDayHeaders() {
	for (var i=0; i<arguments.length; i++) {
		this.dayHeaders[i] = arguments[i];
	}
}

// Set the day of the week (0-7) that the calendar display starts on
// This is for countries other than the US whose calendar displays start on Monday(1), for example
function UMClientCalendar_setWeekStartDay(day) {
	this.weekStartDay = day;
}

// Show next/last year navigation links
function UMClientCalendar_showYearNavigation() {
	this.isShowYearNavigation = true;
}

// Populate the calendar and display it
function UMClientCalendar_showCalendar(px, py) {

	var x = ( px || 0 );
	var y = ( py || 0 );
	var p = "";
	if ( this.showTime )
		var p = "width=250,height=300,toolbar=0,resize=0,location=0,status=0,menubar=0,titlebar=0,scrollbars=0,resizable,alwaysRaised,dependent";
	else
		var p = "width=250,height=180,toolbar=0,resize=0,location=0,status=0,menubar=0,titlebar=0,scrollbars=0,resizable,alwaysRaised,dependent";

	p = 'left=' + x + ',screenX=' + x + ',top=' + y + ',screenY=' + y + ',' + p;
	this.calendarPopup = window.open('about:blank', 'Calendar' + this.index, p);

	with ( this.calendarPopup ) {
		document.open("text/html");
		document.writeln( this.getCalendar( this.theDate.getFullYear(), this.theDate.getMonth() ) );
		if ( this.showTime ) document.writeln( this.getClock() );
		document.writeln("</body></html>");
		document.close();
		focus();
	}

}

// Hide a calendar object
function UMClientCalendar_hideCalendar( i ) {
	window.calendarPopups[i].calendarPopup.close();
	window.calendarPopups[i].calendarPopup = null;
}

// Refresh the contents of the calendar display
function UMClientCalendar_refreshCalendar( i ) {
	with ( window.calendarPopups[i] ) {
		calendarPopup.document.open();
		if ( arguments.length>1 ) {
			calendarPopup.document.writeln( getCalendar( arguments[1], arguments[2] ) );
		} else {
			calendarPopup.document.writeln( getCalendar() );
		}
		if ( showTime ) calendarPopup.document.writeln( getClock() );
		calendarPopup.document.close();
	}
}

// returns calendar html code
function UMClientCalendar_getCalendar() {
	var now = new Date();

	var dt_datetime = new Date(
		arguments[0] || now.getFullYear(),
		arguments[1] || now.getMonth(),
		arguments[2] || now.getDate()
	);

	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-this.weekStartDay)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);


	var dt_current_day = new Date(dt_firstday);
	result = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="+this.words[0]+"\">";
	result += "<title>"+this.words[1]+"</title><link rel=\"stylesheet\" href=\"" + CALENDAR_CSS + "\" type=\"text/css\"></head>";
	result += "<body marginwidth=0 marginheight=0 topmargin=0 rightmargin=0 leftmargin=0>";
	result += "<center><table width=100% border=0 cellspacing=0 cellpadding=2>";


	// navigation
	result += "<tr valign=\"middle\">";
	if ( this.isShowYearNavigation ) {
		result += "<td class=\"cal\" align=\"left\">&nbsp;<b><a class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar(" + this.index + "," + dt_prev_month.getFullYear() + "," + dt_prev_month.getMonth() + ");\">&lt;&lt;</a></b></td>";
		result += "<td class=\"cal\" align=\"center\">"+this.monthNames[ dt_datetime.getMonth() ]+"</td>";
		result += "<td class=\"cal\" align=\"right\"><b><a class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar(" + this.index + "," + dt_next_month.getFullYear() + ","+ dt_next_month.getMonth() + ");\">&gt;&gt;</a></b>&nbsp;</td>";
		result += "<td class=\"cal\" align=\"right\">&nbsp;<b><a class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar(" + this.index + ","+ (dt_datetime.getFullYear()-1) +","+ dt_datetime.getMonth() + ");\">&lt;&lt;</a></b>";
		result += "&nbsp;&nbsp;"+dt_datetime.getFullYear()+"&nbsp;&nbsp;";
		result += "<B><A class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar("+this.index+"," + (dt_datetime.getFullYear()+1) + ","+dt_datetime.getMonth()+");\">&gt;&gt;</a></b>&nbsp;</td>";
		result += "</tr><tr><td class=\"cal\" colspan=4 align=center>";
	} else {
		result += "<td class=\"cal\" align=center>&nbsp;<b><a class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar(" + this.index + ","+ dt_prev_month.getFullYear() + "," + dt_prev_month.getMonth() + ");\">&lt;&lt;</a></b></td>";
		result += "<td class=\"cal\" width=60% align=center>"+this.monthNames[ dt_datetime.getMonth() ]+" "+ dt_datetime.getFullYear() +"</TD>";
		result += "<td class=\"cal\" align=center><b><a class=\"cal\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar(" + this.index + ","+ dt_next_month.getFullYear() +","+ dt_next_month.getMonth() +");\">&gt;&gt;</a></b>&nbsp;</td>";
		result += "</tr><tr><td class=\"cal\" colspan=3 align=center>";
	}

	// - to set and hide...
	// result += "<A CLASS="todaylink" HREF="javascript:window.opener."+this.returnFunction+"(\""+now.getFullYear()+"\",\""+(now.getMonth()+1)+"\",\""+now.getDate()+"\");window.opener.UMClientCalendar_hideCalendar("+this.index+");">"+this.words[2]+"</A>\n";
	// - to reset calendar...
	result += "<a class=\"todaylink\" href=\"javascript:window.opener.UMClientCalendar_refreshCalendar("+this.index+","+now.getFullYear()+","+now.getMonth()+");\">"+this.words[2]+"</a>";
	result += "</td></tr></table>\n";


	// print weekdays titles
	result += "<table width=100% border=0 cellspacing=2 cellpadding=2><tr>";
	for (var n=0; n<7; n++)
		result += "<td class=\"caldayh\">" + this.dayHeaders[ (this.weekStartDay + n) % 7 ] + "</td>";
	// print calendar table
	result += "</tr>";
	for ( var n = 0; n < 6; n++ ) {
	//while (dt_current_day.getMonth() == dt_datetime.getMonth() || dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		result += "<tr>";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getFullYear() == this.theDate.getFullYear() && dt_current_day.getDate() == this.theDate.getDate() && dt_current_day.getMonth() == this.theDate.getMonth())
					// mark stored date
					result += "<td class=\"calmark\" align=\"right\">";
				else if (dt_current_day.getFullYear() == now.getFullYear() && dt_current_day.getDate() == now.getDate() && dt_current_day.getMonth() == now.getMonth())
					// mark current date
					result += "<td class=\"caltoday\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// mark weekend days
					result += "<td class=\"calweekend\" align=\"right\">";
				else
					// mark working days of current month
					result += "<td class=\"calwork\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					//result += "<a class=\"calthismonth\" href=\"javascript:window.opener."+this.returnFunction+"(" + dt_current_day.getFullYear() + "," +
					//  (dt_current_day.getMonth()+1) + "," + dt_current_day.getDate() + ","+this.formField+");window.opener.UMClientCalendar_hideCalendar(" + this.index + ");\">";
					result += "<a class=\"calthismonth\" "

				else
					// print days of other months
					result += "<a class=\"calothermonth\" "

//				result += "href=\"javascript:window.opener."+this.dateSelected+"(" + dt_current_day.getFullYear() + "," +
				result += "href=\"javascript:window.opener.UMClientCalendar_dateSelected(" + this.index + "," + dt_current_day.getFullYear() + "," +
					  (dt_current_day.getMonth()+1) + "," + dt_current_day.getDate() + ");\">" + dt_current_day.getDate() + "</a></td>\n";

				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		result += "</tr>\n";
	}

	result += "</table></center>";

	return result;

}

function UMClientCalendar_setTime(i, h, m) {

	if ( h > -1 ) {
		window.calendarPopups[i].theDate.setHours(h);
	}

	if ( m > -1 ) {
		window.calendarPopups[i].theDate.setMinutes(m);
	}

	with (window.calendarPopups[i])
		calendarPopup.document.getElementById("um_clientcalendar_timeselect").innerHTML = theDate.getHours() + ( theDate.getMinutes() < 10 ? ":0" : ":" ) + theDate.getMinutes();

}

// returns clock html code
function UMClientCalendar_getClock() {

	// hh:mm navigation
	result = "<table width=100% border=0 cellspacing=2 cellpadding=2><tr>";
	result += "<td colspan=12 class=\"cal\" align=\"center\"><span id=\"um_clientcalendar_timeselect\">" + this.theDate.getHours() + (this.theDate.getMinutes() < 10 ? ":0" : ":") + this.theDate.getMinutes() + "</span></td>";
	result += "</tr><tr><td colspan=12 class=\"caldayh\">ura</td></tr><tr>";

	for ( var n = 0; n < 24; n++ )
	{
		result += "<td align=\"right\"><a class=\"calthismonth\" href=\"javascript:window.opener.UMClientCalendar_setTime(" + this.index + "," + n + ", -1);\">" + n + "</a></td>"
		if (n == 11) result += "</tr><tr>";
	}

	result += "</tr></table><table width=100% border=0 cellspacing=2 cellpadding=2><tr><td colspan=12 class=\"caldayh\">minuta</td></tr><tr>";

	for ( var n = 0; n < 60; n+=5 )
		if ( n < 10 )
			result += "<td align=\"right\"><a class=\"calthismonth\" href=\"javascript:window.opener.UMClientCalendar_setTime(" + this.index + ", -1, " + n + ");\">0" + n + "</a></td>";
		else
			result += "<td align=\"right\"><a class=\"calthismonth\" href=\"javascript:window.opener.UMClientCalendar_setTime(" + this.index + ", -1, " + n + ");\">" + n + "</a></td>";

	result += "</tr></table></center>";

	return result;

}


