/*
function TableHeight() {
    var myWidth;
    var myHeight;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight - 260
  //hiY = document.write(myHeight - 150);
}
*/

function clearFileInputField(tagId) {
    document.getElementById(tagId).innerHTML = 
                    document.getElementById(tagId).innerHTML;
}

function show_hide_upload(rowAllegato_id, rowDettaglio_id, cntl_file_id, cntl_fileName_id) {
	var rowAll = document.getElementById(rowAllegato_id);
	var rowDet = document.getElementById(rowDettaglio_id);
	var cntl_file = document.getElementById(cntl_file_id);
	var cntl_fileName = document.getElementById(cntl_fileName_id);
	
	if(rowAll.style.display == 'block' || rowAll.style.display == '') {
		rowDet.style.display = 'block';
		rowAll.style.display = 'none';
		
		var fileName = cntl_file.value;
		
		var from = fileName.lastIndexOf("\\") + 1;
		var to = fileName.lenght;
		
		cntl_fileName.value = fileName.substring(from, to);
	} else {
		rowDet.style.display = 'none';
		rowAll.style.display = 'block';
		clearFileInputField("uploadFile_div");
	}
}

function soloNumeri() {
	//alert(event.keyCode);
	if (event.keyCode < 48 || event.keyCode > 57)
	{
		alert("Inserire solo valori numerici.");
		
		event.cancelBubble = true;
		event.returnValue = false;
	}
}

function isValidDate(dateStr, format) {
	if (format == null) { format = 'MDY'; }
	format = format.toUpperCase();
	
	if (format.length != 3) { format = 'MDY'; }
	if ( (format.indexOf('M') == -1) || (format.indexOf('D') == -1) || (format.indexOf('Y') == -1) ) { format = 'MDY'; }
	if (format.substring(0, 1) == 'Y') { 
		var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
		var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
	} else if (format.substring(1, 2) == 'Y') { 
		var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/;
		var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/;
	} else { 
		var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/;
		var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	}
	if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
	var parts = dateStr.split(RegExp.$1);
	if (format.substring(0, 1) == 'M') { var mm = parts[0]; } else if (format.substring(1, 2) == 'M') { var mm = parts[1]; } else { var mm = parts[2]; }
	if (format.substring(0, 1) == 'D') { var dd = parts[0]; } else if (format.substring(1, 2) == 'D') { var dd = parts[1]; } else { var dd = parts[2]; }
	if (format.substring(0, 1) == 'Y') { var yy = parts[0]; } else if (format.substring(1, 2) == 'Y') { var yy = parts[1]; } else { var yy = parts[2]; }
	if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
	if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
	var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
	if (parseFloat(dd) != dt.getDate()) { return false; }
	if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
	return true;
}

function checkForm() {
	var error = 0;
	var message = "Ci sono errori nel modulo.\nI campi contrassegnati con l\'asterisco sono obbligatori.";
	
	// controllo campi obbligatori
	var ckbAuth = document.getElementById("ckbAutorizzazione");
	var txtFile = document.getElementById("txtFile");
	var txtVotazione = document.getElementById("txtVotazione");
	var txtDataConseguimento = document.getElementById("txtDataConseguimento");
	var ddlTitoloStudio = document.getElementById("ddlTitoloStudio");
	var txtEmail = document.getElementById("txtEmail");
	var txtLocalita = document.getElementById("txtLocalita");
	var txtDataNascita = document.getElementById("txtDataNascita");
	var txtCognome = document.getElementById("txtCognome");
	var txtNome = document.getElementById("txtNome");
	
	if(txtVotazione && txtVotazione.value.length <= 0) {
		error += 1;
		txtVotazione.focus();
	}
	
	if(txtDataConseguimento && txtDataConseguimento.value.length <= 0) {
	} else {
		if(isValidDate(txtDataConseguimento.value, 'DMY') == false && error == 0) {
			alert('Data non valida.');
			txtDataConseguimento.select();
			txtDataConseguimento.focus()
			
			return false
		}
	}
	
	if(ddlTitoloStudio && ddlTitoloStudio.options[ddlTitoloStudio.selectedIndex].value.length <= 0) {
		error += 1;
		ddlTitoloStudio.focus();
	}
	
	if(txtEmail && txtEmail.value.length <= 0) {
		error += 1;
		txtEmail.focus();
	}
	
	if(txtLocalita && txtLocalita.value.length <= 0) {
		error += 1;
		txtLocalita.focus();
	}
	
	if(txtDataNascita && txtDataNascita.value.length <= 0) {
	} else {
		if(isValidDate(txtDataNascita.value, 'DMY') == false && error == 0) {
			alert('Data non valida.');
			txtDataNascita.select();
			txtDataNascita.focus()
			
			return false
		}
	}
	
	if(txtCognome && txtCognome.value.length <= 0) {
		error += 1;
		txtCognome.focus();
	}
	
	if(txtNome && txtNome.value.length <= 0) {
		error += 1;
		txtNome.focus();
	}
	
	if(error > 0) {
		alert(message);
	} else {
		// autorizzazione trattamento dati personali
		if (ckbAuth && ckbAuth.checked == false) {
			alert('Autorizzare il trattamento dei dati personali per continuare.')
			ckbAuth.focus();
						
			return false
		} else {
			if(txtFile && txtFile.value.length > 0 && error == 0) {
				return checkFileExt(txtFile);
			}
		}
	}
	
	return (error == 0)
}

function checkFileExt(ctrl) 
{
    //set the name of our form
    var form = document.frmLavoraConNoi;
    //retrieve our control
    var file = ctrl.value; //DOMCall(ctrl).value;
    var type = "";
    //create an array of acceptable files
    var validExtensions = new Array(".doc", ".docx", ".rtf", ".pdf", ".zip", ".rar");
    var allowSubmit = false;
    //if our control contains no file then alert the user
    if (file.indexOf("\\") == -1)
    {
        //alert("You must select a file before hitting the Submit button");
        return true;;
    }
    else
    {
        //get the file type
        type = file.slice(file.indexOf("\\") + 1);
        var ext = file.slice(file.lastIndexOf(".")).toLowerCase();
        //loop through our array of extensions
        for (var i = 0; i < validExtensions.length; i++) 
        {
            //check to see if it's the proper extension
            if (validExtensions[i] == ext) 
            { 
                //it's the proper extension
                allowSubmit = true; 
            }
        }
    }
    //now check the final bool value
    if (allowSubmit == false)
    {
        //let the user know they selected a wrong file extension
        alert("E\' possibilie allegare file:\n     " + (validExtensions.join("\n     ").toLowerCase()));
        return false;
    }
    else
    {
        return true
    }       
    return allowSubmit;
}

function setRowContentHeight(rowId) {
	var row = document.getElementById(rowId);

    if (row) {
        //row.style.setExpression('height', 'document.body.clientHeight - 260');
        row.style.height = document.body.clientHeight - 260 + 'px'
    }
}

var DDSPEED = 2;
var DDTIMER = 2;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}


var accordion=function(){
	var tm=sp=10;
	function slider(n){this.nm=n; this.arr=[]}
	slider.prototype.init=function(t,c,k){
		var a,h,s,l,i; a=document.getElementById(t); this.sl=k?k:'';
		h=a.getElementsByTagName('dt'); s=a.getElementsByTagName('dd'); this.l=h.length;
		for(i=0;i<this.l;i++){var d=h[i]; this.arr[i]=d; d.onclick=new Function(this.nm+'.pro(this)'); if(c==i){d.className=this.sl}}
		l=s.length;
		for(i=0;i<l;i++){var d=s[i]; d.mh=d.offsetHeight; if(c!=i){d.style.height=0; d.style.display='none'}}
	}
	slider.prototype.pro=function(d){
		for(var i=0;i<this.l;i++){
			var h=this.arr[i], s=h.nextSibling; s=s.nodeType!=1?s.nextSibling:s; clearInterval(s.tm);
			if(h==d&&s.style.display=='none'){s.style.display=''; su(s,1); h.className=this.sl}
			else if(s.style.display==''){su(s,-1); h.className=''}
		}
	}
	function su(c,f){c.tm=setInterval(function(){sl(c,f)},tm)}
	function sl(c,f){
		var h=c.offsetHeight, m=c.mh, d=f==1?m-h:h; c.style.height=h+(Math.ceil(d/sp)*f)+'px';
		c.style.opacity=h/m; c.style.filter='alpha(opacity='+h*100/m+')';
		if(f==1&&h>=m){clearInterval(c.tm)}else if(f!=1&&h==1){c.style.display='none'; clearInterval(c.tm)}
	}
	return{slider:slider}
}();
