//
// Javascripts utilizzati da Campusnet
//

// validate date
function validDate(obj) {

var bg=obj.style.background;

if (!obj.value.length) {
	return true;
}

for (i=0;i<obj.value.length;i++) {
	if (!isDigit(obj.value.charAt(i))) {
		obj.style.background='yellow';
		alert("Solo caratteri numerici in campo data.");
		obj.value = obj.defaultValue;
		obj.style.background=bg;
		obj.focus();
		return false;
	}
}

var field = obj.name.replace(/_year|_month/, '');

var year =eval('document.' + obj.form.name + '.' + field + "_year");
var month=eval('document.' + obj.form.name + '.' + field + "_month");
var day  =eval('document.' + obj.form.name + '.' + field);

if (year.value < 100) { year.value = "20" + year.value; }
if (year.value < 1000){ year.value = "2"  + year.value; }

if (month.value.length && (month.value < 1 || month.value > 12)) {
	month.style.background='yellow';
	alert("Mese non valido in campo data.");
	month.value = month.defaultValue;
	month.style.background=bg;
	month.focus();
	return false;
}
if (month.value.length == 1) {
	month.value = "0" + month.value;
}

var days = new Array(31, ((year.value % 4 == 0 && year.value % 100 != 0) || year.value % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (day.value.length && (day.value < 1 || day.value > days[month.value-1])) {
	day.style.background='yellow';
	alert("Giorno non valido in campo data.");
	day.value = day.defaultValue;
	day.style.background=bg;
	day.focus();
	return false;
}
if (day.value.length == 1) {
	day.value = "0" + day.value;
}

return true;
}       


function isDigit(num) {
var string="1234567890";
if (string.indexOf(num) != -1) {
	return true;
}
return false;
}

// Mostra e nasconde campi nella main form
function toggleFormField(field,status) {

	var pn = field.parentNode;
	while (pn.tagName != 'TR') {
		pn = pn.parentNode;
	}
	if (status == true) {
		pn.style.display = '';
	}
	else {
		pn.style.display = 'none';
	}
}


function openFileManager(script,dir,file,mode) {
	if (!dir) {dir=''}
	if (!file) {file=''}
	if (!mode) {mode=''}

	if (mode == 'editor') {
		path = script + '/AdmFileMan?action=edit;dir=' + dir + ';file=' + file;
		size='width=730,height=700';
	}
	else if (mode == 'image')	{
		path = script + '/AdmFileMan?action=start;dir=' + dir + ';file=' + file + ';view=image';
		size='width=870,height=620';
	}
	else {
		path = script + '/AdmFileMan?action=start;dir=' + dir + ';file=' + file;
		size='width=870,height=620';
	}

	ewin=window.open(path,'_editor', 'status=0 toolbar=no,location=0,directories=no,menubar=0,modal=no,scrollbars=1,resizable=1,' + size);
	ewin.focus();
}


//
// FUNZIONI PER LA GESTIONE DEI COOKIES //
//

function SetCookie (name, value) { 
var argv = SetCookie.arguments; 
var argc = SetCookie.arguments.length; 
var expires = (argc > 2) ? argv[2] : null; 
var path = (argc > 3) ? argv[3] : null; 
var domain = (argc > 4) ? argv[4] : null; 
var secure = (argc > 5) ? argv[5] : false; 

document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}


function GetCookie (name) { 
var arg = name + "="; 
var alen = arg.length; 
var clen = document.cookie.length; 
var i = 0; 

while (i < clen) { 
	var j = i + alen; 
	if (document.cookie.substring(i, j) == arg) return getCookieVal (j); 
	i = document.cookie.indexOf(" ", i) + 1; 
	if (i == 0) break; 
} 
return null;
}

function getCookieVal (offset) { 
var endstr = document.cookie.indexOf (";", offset); 
if (endstr == -1) 
endstr = document.cookie.length; 
return unescape(document.cookie.substring(offset, endstr));
}


function DeleteCookie (name) { 
var exp = new Date(); 
exp.setTime (exp.getTime() - 1); 
var cval = GetCookie (name); 
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


//
// FUNZIONI PER BOX E MENU DINAMICI //
//

function toggleLinkBox(id) {
  
	var menu = document.getElementById('menu' + id);

	if (menu.style.display != 'none') {
		setLinkBox(id,'off');
	}
	else {
		setLinkBox(id,'on');
	}
}


function setLinkBox(id,status) {
  
	var box = document.getElementById('box' + id);
	var image = document.getElementById('ctrl' + id);
	var text = document.getElementById('text' + id);
	var menu = document.getElementById('menu' + id);

	if (status == 'off') {
		if (isObj(menu)) {menu.style.display = 'none'}
		if (isObj(text)) {text.style.display = 'none'}
		image.src=image.src.replace('collapse', 'expand');
		image.alt="Espandi";
		image.title="Espandi";
		SetCookie('box' + id, 'off', null, '/');
	}
	else {
		if (isObj(menu)) {menu.style.display = ''}
		if (isObj(text)) {text.style.display = ''}
		image.src=image.src.replace('expand', 'collapse');
		image.alt="Riduci";
		image.title="Riduci";
		SetCookie('box' + id, 'on', null, '/');
	}

}


function toggleMenuItems(id,x,n) {

	var item = document.getElementById('item' + (x+1) + id);

	var status = 'none';
	if (isObj(item)) {
		status = item.style.display;
	}

	if (status == 'none') {
		setMenuItems(id,'on',x,n);
	}
	else {
		setMenuItems(id,'off',x,n);
	}
}

function setMenuItems(id,status,x,n) {

	var display;
	var item;
	var item_a = document.getElementById('a' + x + id);
	
	if (status == 'off') {
		display = 'none';
		item_a.style.backgroundImage = item_a.style.backgroundImage.replace('collapse', 'expand');
		item_a.title = 'Espandi';
		SetCookie('item' + x + id, 'off', null, '/');
	} else {
		display='';
		item_a.style.backgroundImage = item_a.style.backgroundImage.replace('expand', 'collapse');
		item_a.title = 'Riduci';
		SetCookie('item' + x + id, 'on', null, '/');
	}

	for (var i=x+1; i<=x+n; i++)	{
		item = document.getElementById('item' + i + id);
		if (isObj(item)) {
			item.style.display = display;
		}
	}


return;
}


function isObj(obj) {return (typeof obj == "undefined" || obj == null) ? false : true;}



// Cattura tutti i valori dei campi di una form
function getFormString( formRef, oAndPass ) {
	for( var x = 0, oStr = '', y = false; formRef.elements[x]; x++ ) {
		if( formRef.elements[x].type ) {
			var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
			if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) ) {
				oStr += ( y ? ',' : '' ) + oE.value.replace(/%/g,'%p').replace(/,/g,'%c');
				y = true;
			} else if( oT == 'radio' || oT == 'checkbox' ) {
				oStr += ( y ? ',' : '' ) + ( oE.checked ? '1' : '' );
				y = true;
			} else if( oT == 'select-one' ) {
				oStr += ( y ? ',' : '' ) + oE.selectedIndex;
				y = true;
			} else if( oT == 'select-multiple' ) {
				for( var oO = oE.options, i = 0; oO[i]; i++ ) {
					oStr += ( y ? ',' : '' ) + ( oO[i].selected ? '1' : '' );
					y = true;
				}
			}
		}
	}
	return oStr;
}

// Imposta tutti i valori dei campi di una form
function recoverInputs( formRef, oStr, oAndPass ) {
	if( oStr ) {
		oStr = oStr.split( ',' );
		for( var x = 0, y = 0; formRef.elements[x]; x++ ) {
			if( formRef.elements[x].type ) {
				var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
				if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) ) {
					oE.value = oStr[y].replace(/%c/g,',').replace(/%p/g,'%');
					y++;
				} else if( oT == 'radio' || oT == 'checkbox' ) {
					oE.checked = oStr[y] ? true : false;
					y++;
				} else if( oT == 'select-one' ) {
					oE.selectedIndex = parseInt( oStr[y] );
					y++;
				} else if( oT == 'select-multiple' ) {
					for( var oO = oE.options, i = 0; oO[i]; i++ ) {
						oO[i].selected = oStr[y] ? true : false;
						y++;
					}
				}
			}
		}
	}
}


// Inizializza tinyMCE (textarea html editor) con diverse funzionalita

function StartTinyMCE(areaID, format, style) {

if (format == "basic") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,
		theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist, numlist, outdent, indent,separator,cleanup,removeformat,separator,help,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
} else if (format == "advanced") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,
		plugins : "preview,inserttabs",
		theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,separator,undo,redo,separator,charmap,hr,link, unlink, image,insernewtab,insertclosetabs,separator,help,code",
		theme_advanced_buttons2 : "formatselect,sup,sub,separator,bullist, numlist, outdent, indent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator, cleanup,removeformat,separator,preview",
		theme_advanced_buttons3 : "",
		extended_valid_elements : "a[name|href|target|title|onclick]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
} else if (format == "advanced_with_font_control") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,		
		plugins : "preview,inserttabs",
		theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,separator,undo,redo,separator,bullist, numlist, outdent, indent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,preview,help,code",
		theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,sup,sub,separator,forecolor,backcolor,separator,charmap,hr,link, unlink, image,insernewtab,insertclosetabs,separator,cleanup,removeformat",
		theme_advanced_buttons3 : "",
		extended_valid_elements : "a[name|href|target|title|onclick],font[face|size|color|style],span[class|align|style]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
} else if (format == "advanced_with_tables") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,		
		plugins : "table,preview,inserttabs",
		theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,separator,undo,redo,separator,bullist, numlist, outdent, indent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,preview,help,code",
		theme_advanced_buttons2 : "formatselect,sup,sub,separator,tablecontrols,separator,charmap,hr,link, unlink, image,insernewtab,insertclosetabs,separator,cleanup,removeformat",
		theme_advanced_buttons3 : "",
		extended_valid_elements : "a[name|href|target|title|onclick]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
} else if (format == "advanced_with_font_control_and_tables") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,		
		plugins : "table,preview,inserttabs",
		theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,separator,undo,redo,separator,bullist, numlist, outdent, indent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,preview,help,code",
		theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,sup,sub,separator,forecolor,backcolor,separator,charmap,hr,link, unlink, image,insernewtab,insertclosetabs",
		theme_advanced_buttons3 : "tablecontrols,separator,cleanup,removeformat",
		extended_valid_elements : "a[name|href|target|title|onclick],font[face|size|color|style],span[class|align|style]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
} else if (format == "full_featured") {
	tinyMCE.init({
		mode : "exact",
		theme : "advanced",
		language : "it",
		elements : areaID,		
		plugins : "table,advhr,advimage,advlink,insertdatetime,preview,zoom,searchreplace,print,paste,contextmenu,inserttabs,insertajaxquery,style,fullscreen,layer",
		theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,pasteword,separator,search,replace,separator,undo,redo,separator,bullist, numlist, outdent, indent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,preview,print,help,code",
		theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,forecolor,backcolor,separator,charmap,advhr,link, unlink, image,insernewtab,insertclosetabs,anchor",
		theme_advanced_buttons3 : "tablecontrols,separator,insertajaxquery,insertlayer,moveforward,movebackward,absolute,separator,sup,sub,separator,insertdate,inserttime,zoom,cleanup,removeformat,styleprops,fullscreen",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		content_css : style,
		external_link_list_url : "AdmFileMan?action=tinyMCElinklist",
		external_image_list_url : "AdmFileMan?action=tinyMCEimagelist",
		remove_linebreaks : false,
		auto_cleanup_word : true,
		convert_urls : false,
		relative_urls : true
	});
}

return;
}


// Alterna i Tabs
function ShowTab(id) {

var panelid;
var tabid = document.getElementById(id);
var tabs = tabid.parentNode.childNodes;

for (var i = 0; i < tabs.length; i++) {
	if (tabs[i].nodeName == 'LI') {
		tabs[i].className = 'tab-off';
		panelid = tabs[i].id.replace('tab', 'panel');
		panelobj = document.getElementById(panelid);
		panelobj.className = 'panel-off';
		panelobj.style.visibility = 'hidden';
		panelobj.style.position = 'absolute';
		panelobj.style.top = '0px';
	}
}
tabid.className = 'tab-on';
panelid = tabid.id.replace('tab', 'panel');
panelobj = document.getElementById(panelid);
panelobj.className = 'panel-on';
panelobj.style.visibility = '';
panelobj.style.position = '';
panelobj.style.top = '';

return;
}


function gowap (url){

    url = url.replace(/http:\/\//, '');
    url=escape(url);
    url="http://emulator.tagtag.com/wapemulator.cgi?O__SKIN=S1&wurl=" + url;
    params ='width=270,height=340,toolbar=yes,location=no,scrollbars=no,resizable=yes';
    window.open(url,'WAP emulator',params);

return;
} 
