/*****************************************************
* ypSlideOutMenu
* 3/04/2001
*
* a nice little script to create exclusive, slide-out
* menus for ns4, ns6, mozilla, opera, ie4, ie5 on
* mac and win32. I've got no linux or unix to test on but
* it should(?) work...
*
* --youngpup--
*****************************************************/
ypSlideOutMenu.Registry = []
ypSlideOutMenu.aniLen = 200
ypSlideOutMenu.hideDelay = 100
ypSlideOutMenu.minCPUResolution = 10

function ypSlideOutMenu(id, dir, left, top, width, height){

	this.ie = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0
	if (this.ie || this.ns4 || this.dom) {
  		this.myX=left
		this.id = id
		this.dir = dir
		this.orientation = dir == "left" || dir == "right" ? "h" : "v"
		this.dirType = dir == "right" || dir == "down" ? "-" : "+"
		this.dim = this.orientation == "h" ? width : height
		this.hideTimer = false
		this.aniTimer = false
		this.open = false
		this.over = false
		this.startTime = 0
		this.gRef = "ypSlideOutMenu_"+id
		eval(this.gRef+"=this")
		ypSlideOutMenu.Registry[id] = this
		var d = document
		d.write('<style type="text/css">')
		d.write('#' + this.id + 'Container { visibility:hidden; ')
		d.write('left:' + left + 'px; ')
		d.write('top:' + top + 'px; ')
		d.write('overflow:hidden; }')
		d.write('#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; ')
		d.write('width:' + width + 'px; ')
		d.write('height:' + height + 'px; ')
		d.write('clip:rect(0 ' + width + ' ' + height + ' 0); ')
		d.write('}')
		d.write('</style>')
		//alert(d.style.top)
		this.load()
	}
}

ypSlideOutMenu.prototype.load = function() {
	var d = document
	var lyrId1 = this.id + "Container"
	var lyrId2 = this.id + "Content"
	var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	var temp
	if (!obj1 || !obj2) 
		window.setTimeout(this.gRef + ".load()", 100)
	else {
		this.container = obj1
		this.menu = obj2
		this.style = this.ns4 ? this.menu : this.menu.style
		this.container.style["pixelLeft"]=this.myX
		//alert(this.container.style["pixelLeft"]);
		this.homePos = eval("0" + this.dirType + this.dim)
		this.outPos = 0
		this.accelConst = (this.outPos - this.homePos) / ypSlideOutMenu.aniLen / ypSlideOutMenu.aniLen
		if (this.ns4) 
			this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("ypSlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("ypSlideOutMenu.hideMenu('" + this.id + "')")
		this.endSlide()
	}
}

ypSlideOutMenu.showMenu = function(id){
	var reg = ypSlideOutMenu.Registry
	var obj = ypSlideOutMenu.Registry[id]
	if (obj.container) {
		obj.over = true
	for (menu in reg) if (id != menu)
		ypSlideOutMenu.hide(menu)
	if (obj.hideTimer) {
		reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }
		if (!obj.open && !obj.aniTimer)
			reg[id].startSlide(true)
	}
}

ypSlideOutMenu.hideMenu = function(id)
{
	var obj = ypSlideOutMenu.Registry[id]
	if (obj.container) {
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("ypSlideOutMenu.hide('" + id + "')", ypSlideOutMenu.hideDelay);
	}
}

ypSlideOutMenu.hide = function(id)
{
var obj = ypSlideOutMenu.Registry[id]
obj.over = false
if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
obj.hideTimer = 0
if (obj.open && !obj.aniTimer) obj.startSlide(false)
}
ypSlideOutMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]()
	this.open = open
	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime() 
	this.aniTimer = window.setInterval(this.gRef + ".slide()", ypSlideOutMenu.minCPUResolution)
}
ypSlideOutMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > ypSlideOutMenu.aniLen)
		this.endSlide()
	else{
		var d = Math.round(Math.pow(ypSlideOutMenu.aniLen-elapsed, 2) * this.accelConst)
		if (this.open && this.dirType == "-") 
			d = -d
		else if (this.open && this.dirType == "+") 
			d = -d
		else if (!this.open && this.dirType == "-") 
			d = -this.dim + d
		else 
			d = this.dim + d
		this.moveTo(d)
	}
}
ypSlideOutMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer)
	this.moveTo(this.open ? this.outPos : this.homePos)
	if (!this.open) this.setVisibility(false)
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over)
	}
}

ypSlideOutMenu.prototype.setVisibility = function(bShow) {
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}

ypSlideOutMenu.prototype.moveTo = function(p) {
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
ypSlideOutMenu.prototype.getPos = function(c) {
return parseInt(this.style[c])
}
ypSlideOutMenu.prototype.onactivate = function() { }
ypSlideOutMenu.prototype.ondeactivate = function() { }

/**********************************
End of drop down menus
**********************************/

var isNS4=document.layers?true:false;
var isIE=document.all?true:false;
var isNS6=!isIE&&document.getElementById?true:false;
var isDOM = (document.getElementById) ? true : false;

//----------------------------------------------------------------------


function showScheda(ID,Nome,swf,pdf){
    if (isDOM)
        showDOM(ID, Nome, swf, pdf);
    else
        if (isNS6)
            addProdNS6(index);
        else
            addProdNS(index);	
}

function refresh1(){

    if (isDOM){
		X=(document.body.clientWidth>778) ? (document.body.clientWidth-778)/2 : 0;
        if (X>1){
			moveDOM();
		}
	}else{
		if (isIE)
			moveIE();
		else
			moveNS();
	}
}

function refresh2(){
    //alert('refresh');
	if (isDOM){
		X=(document.body.clientWidth>778) ? (document.body.clientWidth-778)/2 : 0;
        if (X>1){
			moveDOM();
		}
		document.getElementById("scheda").style.pixelLeft=590 + X;
	}else{
		if (isIE)
			moveIE();
		else
			moveNS();

	}
	
}

function showDOM(ID, Nome, img, pdf){
	mL=document.getElementById("scheda");

	myI= new Image;
	myI.src="images/gallery/" + img;

	s='<table width="182" border="1" cellspacing="1" cellpadding="2" bordercolor="#FFFFFF">';
	s+='<tr><td valign="middle" align="center">';
	s+='<img name="galImg" src="images/gallery/loading.gif" width="180"/>';
	//s+='<img name="galImg" src="images/gallery/' + img + '" width="180"/>';
	s+='</td></tr><tr>';
	s+='<td valign="middle" bgcolor="#790000" height="30" class="verdana_why_11b">';
	s+=Nome;
	s+='</td></tr>';

	if (ID>0){
		s+='<tr>';
		s+='<td valign="middle" bgcolor="#DADBDC" height="30" class="verdana_blk_11b">';
		s+='<a href="main.php?pag=scheda&amp;ID=' + ID + '&amp;menuSX=520">View Project Panel</a>';
		s+='</td></tr>';
	}

	s+='<tr><td valign="middle" bgcolor="#DADBDC" height="30" class="verdana_why_11b">';
	s+='<a href="upload/Projects/' + pdf + '">Download Project Panel</a>';
	s+='</td></tr></table>';

	mL.innerHTML=s;
	X=(document.body.clientWidth>780) ? (document.body.clientWidth-780)/2 : 0;

	mL.style.pixelTop=120+160;
	mL.style.pixelLeft=590 + X;
	mL.style.visibility = "visible";
	refresh2();

	myI.onLoad=document.getElementById("galImg").src=myI.src;
}

function mostraPrj(n){
	if (prjN > 0)
		nascondiPrj(prjN);

	prjN=n;
	
    if (isIE){
        var layer="prj" + n;
        var mL=document.getElementById(layer);
        mL.style.visibility = "visible";
        moveIE(layer,600,120);
    }
    else
        if (isNS6){
            var layer="prj" + n;
            var mL=document.getElementById(layer);
            mL.style.visibility = "visible";
            moveDOM(layer,600,60);
        }
        else
            mostraNS("layer" + n);
}

function nascondiPrj(n){
	
    if (isIE || isNS6){
        var prj="prj" + n;
        var mL=document.getElementById(prj);
        mL.style.visibility = "hidden";
    }
    else
        mostraNS("prj" + n);
}

function moveDOM(){
	document.getElementById("whtsx").style.pixelLeft=X-7;
	document.getElementById("whtdx").style.pixelLeft=X+778;
	document.getElementById("whtsx").style.pixelTop=0;
	document.getElementById("whtdx").style.pixelTop=0;
}

function moveIE(mLayer,x,y){
	X=(document.body.clientWidth>780) ? (document.body.clientWidth-780)/2 : 0;
	var mL=document.getElementById(mLayer);
    mL.style.top=y;
    mL.style.left=x + X;
}


//----------------------------------------------------------------------

function oggi(element){
    var mD = new Date();
    var el=document.getElementById(element);
    //el.value=mD.getFullYear() + "-" + (mD.getMonth()+1) + "-" + mD.getDate();
	el.value=mD.getDate() + "/" + (mD.getMonth()+1) + "/" + mD.getFullYear();
}


function addOption(element){
    var el=document.getElementById(element);
	var index=el.options.length;
	alert(index);
	var mCat=window.prompt("Nuova categoria?","");
	if (!mCat==""){
		el.options[index]=new Option(mCat,mCat,1,1);
	}
}

function doPassVar(pag){
    HM_DOM = (document.getElementById) ? true : false;
    HM_NS4 = (document.layers) ? true : false;
    HM_IE = (document.all) ? true : false;

	//alert('ciao');
    
	if (HM_IE){   //IE
		//alert('IE');
		var mF = document.getElementById("myFlash");
		mF.SetVariable("menu", pag);        
    }else{
        if (HM_DOM){
			var mF = document.getElementById("myFlash");
			mF.SetVariable("menu", pag);
            alert('DOM');
        }else{  //Netscape
			alert('NS');
            var mF=document.myFlash;
			mF.SetVariable("menu", pag);
			alert('NS');
        }
    }	
}

function delRec(tab, ID, pag){
	//alert("tab: " + tab + " -ID: " + ID + " -pag: " + pag);
	if (window.confirm('Sei sicuro di voler cancellare il Record?'))
		window.location="main.php?cmd=delRec&ID=" + ID + "&tab=" + tab + "&pag=" + pag;
}

function delCheck(){
	return window.confirm('Sei sicuro di voler cancellare il Record ?');
}

function isblank(s){
	for (var i=0; i < s.length; i++){
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\t')) return false;
	}
	return true;
}

function checkMail(src) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}

function checkForm(f){
	var error="";
	for(var i=0; i< f.length; i++){
		var e=f.elements[i];
		//alert(e.name + ":" + e.value);
		if (e.name=="Nome") if ((e.value==null ) || (e.value=="")) error +=" - Name not present\n";
		//if (e.name=="cognome") if ((e.value==null ) || (e.value=="")) error +=" - Surname not present\n";
		
		if (e.name=="email")
			if ((e.value==null ) || (e.value=="") || !checkMail(e.value)) error +=" - '" + e.value + "'isn't a correct E-mail address\n";
		if (e.name=="privacy")
			if (!(e.checked )) error +=" - Privacy conditions not accepted\n";
		


	}
	if (error){
		var s="Form incorrect due to the following errors:\n";
		s +=error;
		s +="\nCorrect errors and try again";
		alert(s);
		return false;
	}else{
		return true;
	}
}

function checkResume(f){
	var error="";
	for(var i=0; i< f.length; i++){
		var e=f.elements[i];
		//alert(e.name + ":" + e.value);
		if (e.name=="Email")
			if ((e.value==null ) || (e.value=="") || !checkMail(e.value)) error +=" - '" + e.value + "' isn't a correct E-mail address\n";
		if (e.name=="category")
			if (e.value=="-") error +=" - Job category's not been selected\n";
		if (e.name=="area")
			if (e.value=="-") error +=" - Area's not been selected\n";
	}
	if (error){
		var s="Form incorrect due to the following errors:\n";
		s +=error;
		s +="\nCorrect errors and try again";
		alert(s);
		return false;
	}else{
		return true;
	}
}

function subMenu(){
	message="ciao dx";
	if (isDOM) {
		if (event.button == 2) {
			showDropMenu();
			return false;
		}
	}
}

function showDropMenu(){
	dm=document.getElementById("dropMenu");
	dm.style.pixelTop=window.event.y+document.body.scrollTop;
	dm.style.pixelLeft=window.event.x+document.body.scrollLeft;
	dm.style.visibility = "visible";
}

function xtree(){
	if (document.getElementById) {
		var tree = new WebFXTree('CRM');
		tree.setBehavior('classic');
		var a = new WebFXTreeItem('Primo');
		tree.add(a);
		var b = new WebFXTreeItem('1.1');
		a.add(b);
		b.add(new WebFXTreeItem('1.1.1'));
		b.add(new WebFXTreeItem('1.1.2'));
		b.add(new WebFXTreeItem('1.1.3'));
		var f = new WebFXTreeItem('1.1.4');
		b.add(f);
		f.add(new WebFXTreeItem('1.1.4.1'));
		f.add(new WebFXTreeItem('1.1.4.2'));
		f.add(new WebFXTreeItem('1.1.4.3'));
		var c = new WebFXTreeItem('1.2');
		a.add(c);
		c.add(new WebFXTreeItem('1.5.1'));
		c.add(new WebFXTreeItem('1.5.2'));
		c.add(new WebFXTreeItem('1.5.3'));
		a.add(new WebFXTreeItem('1.3'));
		a.add(new WebFXTreeItem('1.4'));
		a.add(new WebFXTreeItem('1.5'));
		var d = new WebFXTreeItem('Secondo');
		tree.add(d);
		var e = new WebFXTreeItem('2.1');
		d.add(e);
		e.add(new WebFXTreeItem('2.1.1'));
		e.add(new WebFXTreeItem('2.1.2'));
		e.add(new WebFXTreeItem('2.1.3'));
		d.add(new WebFXTreeItem('2.2'));
		d.add(new WebFXTreeItem('2.3'));
		d.add(new WebFXTreeItem('2.4'));
		document.write(tree);
	}
}

function setid(){
	document.getElementById('f-RifProgetto').value=setid.arguments[0];
	document.getElementById('f-RifTask').value=setid.arguments[1];
	//alert(ID);
}

function aggiorna(){
    if (isIE){
		document.URL=document.URL;
	}else{
		window.location=document.URL;
	}
}

function modUser(id){

	var myF = document.getElementById('modWin');

	myF.style.visibility='visible';
	//alert(document.getElementById('f-Name').value);
	//alert(document.getElementById('name12').value);
	document.updateForm.elements['f-Name'].value=document.getElementById('name'+id).value;
	document.updateForm.elements['f-Psw'].value=document.getElementById('psw'+id).value;
	document.updateForm.elements['f-Scadenza'].value=document.getElementById('scadenza'+id).value;
	document.updateForm.elements['ID'].value=id;

	/*document.getElementById('f-Name').value=document.getElementById('name'+id).value;
	document.getElementById('f-Psw').value=document.getElementById('psw'+id).value;
	document.getElementById('f-Scadenza').value=document.getElementById('scadenza'+id).value;
	document.getElementById('ID').value=id;
	document.getElementById('cmd').value='update';
	show(document.getElementById('cmd').value='update');
	*/
}

function newUser(form){
	document.getElementById('finestra').style.visibility='visible';
	//alert(form.cmd.value);
	form.cmd.value="newUser";
}

function checkDelSelected(form){
	//usata onSubmit per delSelected....
	if(window.confirm("Deleting selected records. Are you sure?"))
		document.delForm.submit();

}

function showFinestra(nome){
	var mF=isDOM?document.getElementById(nome).style: isNS4 ? document.layers[nome]: document.all[nome].style;
	mF.visibility =  "visible";
}

function closeFinestra(nome){
	var mF=isDOM?document.getElementById(nome).style: isNS4 ? document.layers[nome]: document.all[nome].style;
	mF.visibility =  "hidden";
}

function setTitolo(tit){
	var mF=document.getElementById('titolo');
	mF.innerHTML=tit;
}

function setMenuSx(content,pag,pp){
	//var mF=isDOM?document.getElementById('menuSx'): isNS4 ? document.layers['menuSx']: document.all['menuSx'];
	var mF=document.getElementById('menuSx');
	s='<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">';
	for(i=0; i<content.length;i++){
		if(i==(pp-1)){
			s+='<tr>';
				s+='<td width="10"><img src="images/menu/freccia_menu_o.gif"/></td>';
				s+='<td width="127" class="menuSXsel">'+content[i]+'</td>';
			s+='</tr>';
		}else{
			s+='<tr>';
			s+='<td ><img src="images/menu/freccia_menu.gif"/></td>';
			s+='<td class="menuSX"><a href="main.php?pag='+pag+'&amp;pp='+(i+1)+'" style="color:#ffffff">'+content[i]+'</a></td>';
			s+='</tr>';
		}
		s+='<tr><td colspan="2" height="4"/></tr>';
	}
			//s+='<tr><td><a href="main.php?pag='+pag+'&amp;pp='+(i+1)+'" style="color:#ffffff">'+content[i]+'</a></td></tr>';
	s+="</table>";
	mF.innerHTML=s;
}


function setMenuSxAlfa(content,pag,pp){
	//var mF=isDOM?document.getElementById('menuSx'): isNS4 ? document.layers['menuSx']: document.all['menuSx'];
	var mF=document.getElementById('menuSx');
	s='<div style="margin-left:22px;">';
	for(i=0; i<content.length;i++){
		if(content[i]==pp)
			s+='<div class="menuAlfaSel">'+content[i]+'</div>';
		else
			s+='<div class="menuAlfa" align="center"><a href="main.php?pag='+pag+'&amp;pp=' + content[i] +'" style="color:#ffffff">'+content[i]+'</a></div>';
	}
	s+='</div>';
	mF.innerHTML=s;

}

function formQuiz(){
	//var mF=isDOM?document.getElementById('menuSx'): isNS4 ? document.layers['menuSx']: document.all['menuSx'];
	var mF=document.getElementById('menuSx');
	s='<div style="margin-left:10px;" class="verdana_wht_11">';
	s+='Seleziona le risposte corrette e premi il tasto verifica.<br/><br/>';
	s+='<input type="button" name="Verifica" value="Verifica" onClick="quiz.submit()"/>';
	s+='</div>';
	mF.innerHTML=s;

}

function formElaborati(){
	//var mF=isDOM?document.getElementById('menuSx'): isNS4 ? document.layers['menuSx']: document.all['menuSx'];
	var mF=document.getElementById('menuSx');
	s='<br/>'
	s+='<div style="margin:5px;border:solid 1px #ffffff;" class="verdana_wht_11">';
	s+="Le soluzioni ai CASI/ESERCIZI saranno disponibili solo dopo l'invio del proprio elaborato al Tutor.<br/><br/>";
	s+='</div>';
	mF.innerHTML+=s;
}

function msgSx(msg){
	//var mF=isDOM?document.getElementById('menuSx'): isNS4 ? document.layers['menuSx']: document.all['menuSx'];
	var mF=document.getElementById('menuSx');
	s='<br/>'
	s+='<div style="margin:5px;padding:2px;border:solid 1px #ffffff;" class="verdana_wht_11">';
	s+=msg;
	s+='</div>';
	mF.innerHTML+=s;
}
function focusOn(id){
	var mF=document.getElementById(id);
	mF.focus();
}


function resize(){
	var mF = document.getElementById('foto');
	//alert(window.innerWidth+' - ' + window.innerHeight);
	//window.resizeTo(mF.width+10,mF.height+10)
	//alert(window.innerWidth+' - ' + window.innerHeight);

	if (window.innerWidth)
	{
		//alert('x');
		window.innerWidth=mF.width+200;
		window.innerHeight=mF.height+120;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		//alert('x');
		document.documentElement.clientWidth=mF.width+100;
		document.documentElement.clientHeight=mF.height;
	}
	else if (document.body)
	{
		window.resizeTo(mF.width+112,mF.height+110);
	}
}

function delImg(selID){
	if(confirm("Sei sicuro?")){
		new Ajax.Request('main.php',
		  {
		    method:'post',
			 parameters: {cmd:'delImg',id: selID},
		    onSuccess: function(transport){
		      var resp = transport.responseText || "no response text";
				if(resp=='ok'){
					var x=selID.split('-');
					$('thumb'+x[1]).remove();
				}else
		      	alert("Errore! \n\n" + resp);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
		  });
  		
	}
}
