
function intValidate(num){	
  
  num_Val = num.value
  var numStr = "0123456789";
  var thisChar;
  var counter = 0;
    for (var n = 0; n < num_Val.length; n++) {
      thisChar = num_Val.substring(n, n+1);
      if (numStr.indexOf(thisChar) < 0)      return false;
    }
  
   return true;
        
}

function isInt(field) {
	var fieldstr = field.value + "";
    if (fieldstr == "") return false;
	if(!intValidate(field)){
     	alert('Insert only numbers');
  		field.focus();
  		field.select();
		return false;    
  	}
	return true;
}

function submit() {		
	displayWait();	
  document.forms[0].submit();
}

function submitForm(fname) {	
	displayWait();
  	document.forms[fname].submit();

}

function gotoPage(idPage) {
	document.forms[0].ID.value = idPage;
	//displayWait();
  	submit();
}

function gotoMyPage(idPage, frm) {
	displayWait();
	document.forms[frm].ID.value = idPage;
  	document.forms[frm].submit();
}

function restoreSelect(select, value) {
  for (i=0;select.options[i] != null;i++){
    if (select.options[i].value == value) {
      select.options[i].selected = true;
      break;
    }    
  }
}

function checkedRadio(radioObject){
var y=radioObject.length;
for (i=0; i<y; i++)  {
        if (radioObject[i].checked)
        return i;
  }
  return false;
}

function reset(radioObject){
radioObject.options[0].selected = true
}

function cleanList(listObject){
if (listObject == null)
return;
var y=listObject.options.length;
for (i=y-1; i>=0; i--)  {
        listObject.options[i]= null;
  }
}

function MakeArray(n) {
 this.length = n;
 for (var i=1; i<=n; i++) {
     this[i] = 0
 }
 return this;
}

function SearchArray( Obj, Obj_name, Value){
 for (i=1; i<= Obj.length; i++) {
     if (Obj[i][Obj_name] == Value)
        return i;
 }
 return false;
}

function copySelected(src, dst) {
  var isrc = src.selectedIndex;
  var value = src[isrc].value;
  for (var idst = 0; idst < dst.length; idst++) {
    if (dst[idst].value == value) {
      dst[idst].selected = true;
      break;
    }
  }
}

function isInList( codCat , Lista){
	var numMax=Lista.options.length;
	for(  var scorri=0; scorri <numMax; scorri++)
	 {
		 if(  Lista.options[scorri].value == codCat ) 
				return true;
	 }
	return false;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function isEmail(field) {
    if (field.value.indexOf("@") != "-1" &&
        field.value.indexOf(".") != "-1" )
        return true;
    else return false;
}

//visualizza il messaggio di attesa (da invocare prima di effettuare una submit)
function displayWait() {
	try {
	document.getElementById("bodychooser").style.display="none";
	document.getElementById("waitmsg").style.display="block";
	// Per SNAV accrocchio forse ??????
	//document.getElementById("spacerDiv").style.display="none";
	}
	catch (e){
	;
	}
}

/* NUOVE FUNZIONI */
function mkErr(el) {
	try {
	el.style.backgroundColor="#D2E2F5";
	if(!el.errIm) {
		var ei = new Image();
		ei.src=errIcon;
		el.errIm = ei;
		ei.style.position="absolute";
		ei.style.margin="2px 0px 0px 1px";
		el.parentNode.appendChild(ei);
		//el.parentNode.style.verticalAlign="middle";
		//el.parentNode.style.whiteSpace="nowrap";
	} else {
		el.errIm.style.display="inline";
	}
	} catch(e) {;}
}

function spErr() {
	try {
	this.style.backgroundColor="";
	this.errIm.style.display="none";
	} catch(e) {;}
}

//check di una data
function chkDt(el, errmsg) {
  var ok = parseDate(el);
  el.corrfun = spErr;
  el.onfocus = el.corrfun;
  if(!ok) {
    mkErr(el);
    if(errmsg) alert(errmsg);
  } else {
    spErr(el);
  }
}

function parseDate(inputObject) {
	try {
		var re = new RegExp("(\/)|( )", "g");
                inputObject.value = inputObject.value.replace(re, "-");
		var val = inputObject.value.split("-");
		var obDate = new Date(val[2], val[1]-1, val[0]);
		if(obDate.getFullYear()!=val[2]  || obDate.getMonth() != val[1]-1 || obDate.getDate() != val[0]) {
			inputObject.value="";
			return false;
		}
	} catch (e) {
		inputObject.value="";
		return false;
	}
	return true;
}

/* from viaggio.js ricerca.js richiestapreno.js*/
function getDateFromCalendar(inputArray) {
	var references = calendarObjForForm.getHtmlElementReferences();
	references.myDate.value = inputArray.day + '-' + inputArray.month + '-' + inputArray.year;
	calendarObjForForm.hide();
}

function readDate(inputObject) {
	var val = inputObject.value.split("-");
	var obDate = new Date(val[2], val[1]-1, val[0]);
	return obDate;
}	

