/**
 * ALERTS
 */
function confirmDelete(type,name) {
	var text = type;
	if (name)
		text = "Delete "+type+" '"+name+"' ?";
	var ok = confirm(text);
	if (ok == true)
		return true;
	return false;
}

/**
 * POPUPS
 */
function popupPage(page,width,height,name) {
	if (name == undefined)
		name = 'aPopup';
	if (width == undefined)
		width = 800;
	if (height == undefined)
		height = 600;

	props='height='+height+',width='+width+',location=no,scrollbars=no,menubars=no,toolbars=no,resizable=yes';
	var f1 = window.open(page,name,props);
	f1.focus();
}

function closePopup() {
	window.close('aPopup');
}

/**
 * FORM SUBMIT
 */
function submitForm() {
	return submitFormById(0);
}

function submitFormById(id) {
	document.forms[id].submit();
	return false;
}

function submitFormWithAction(action_name) {
	document.forms[0].action.value = action_name;
	document.forms[0].submit();
	return false;
}

function submitFormWithURL(url,name) {
	var form = document.forms[0];
	if (name)
		form = document[name]
	form.action = url;
	form.submit();
	return false;
}

/**
 * Resize a textarea to fit text content
 * @param id id of textarea element
 */
function resizeTextArea(id,min_col,min_row) {
	var MAX_COLS = 100;

	var element = document.getElementById(id);

	var contentRows	= element.value.split("\n");
	var rows		= 0;
//	var cols		= 0;
	// count rows, get max cols
	for(var i=0; i<contentRows.length; i++){
		var rowLength = contentRows[i].length;
		// normal line break row
		rows++;
		// row when text is longer than textarea cols
	//	if(rowLength > MAX_COLS) {
		//	cols = MAX_COLS;
			rows += Math.floor(rowLength/MAX_COLS);
	//	}else {
		//	cols = cols < rowLength ? rowLength : cols;
	//	}
	}

	rows += 1;
//	cols += 1;

//	if (cols < min_col)
	//	cols = min_col;
	if (rows < min_row)
		rows = min_row;

//	element.cols = cols;
	element.rows = rows ;
}

function resetDateTimeSelect(name) {
	setDateTimeSelectValue(name,"","","","","");
}

function setDateTimeSelectValueToNow(name) {
	var today = new Date();
	var day 	= addDateValueZero(today.getDate());
	var year 	= addDateValueZero(today.getYear());
	var month 	= addDateValueZero(today.getMonth()+1);
	var hour 	= addDateValueZero(today.getHours());
	var min 	= addDateValueZero(today.getMinutes());
	setDateTimeSelectValue(name,year,month,day,hour,min);
}

function addDateValueZero(value) {
	return (value < 10) ? "0"+""+value : value;
}
function setDateTimeSelectValue(name,year,month,day,hour,min) {
	var day_select = document.getElementById(name+"_day");
	day_select.value = day;
	var month_select = document.getElementById(name+"_month");
	month_select.value = month;
	var year_select = document.getElementById(name+"_year");
	year_select.value = year;
	var hour_select = document.getElementById(name+"_hour");
	hour_select.value = hour;
	var min_select = document.getElementById(name+"_min");
	min_select.value = min;
}

/**
 * IMAGE PRELOADING
 */

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}