/* debug*/
/***************************************/
function popUpProperties(inobj) {
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj) {
	op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}

/***************************************/


function tornaIndietro(cmd){
		postaLink(cmd + 'TornaIndietro&TornaIndietro=1');
}

function creaInput(form, name , value){
	var casellaTesto = document.createElement("INPUT");
	casellaTesto.id = name;
	casellaTesto.name = name;
	casellaTesto.type = "hidden";
	casellaTesto.value = value;
	if (!objId(name))
		objId(form).appendChild(casellaTesto);
	else
		objId(name).value = value;
}

function objId(id){
	return document.getElementById(id);
}

function innerText(id, valore){
	if(document.all)
		objId(id).innerText = valore;
	else
		objId(id).textContent = valore; 
}

function getInnerText(id){
	if(document.all)
		return objId(id).innerText;
	else
		return objId(id).textContent; 
}

function Trim(stringa){
	var reTrim=/\s+$|^\s+/g;
	return stringa.replace(reTrim,"");
}

function attacEvent(type, fnc){
	if (window.addEventListener) {
		window.addEventListener(type, fnc, false);
	} 
	else if(document.addEventListener) {
  		document.addEventListener(type, fnc, false);
  	}
	else {
		window.attachEvent(type, fnc);
	} 
}

function checkRequiredFields(form, noFocus) {
	var numeroCampi = objId(form).elements.length;
	
	for (i = 0; i < numeroCampi; i++) {
		var unCampo = objId(form).elements[i];
		unCampo.style.backgroundColor = "";
		if ( ((unCampo.getAttribute("required") != null) && (Trim(unCampo.value) == "") && (unCampo.disabled == false)) || EunSelectNonSelezionato(unCampo)) {
			alert("Campo obbligatorio");
			unCampo.style.backgroundColor = "#FFFF9D";
			if (!noFocus) unCampo.focus();
			return false;
		}
	}
	return true;
}

function EunSelectNonSelezionato(campo){
	var result = false;
	if ( (campo.type == "select-one") || (campo.type == "select") ) 
		result = campo.selectedIndex == -1;
		
	return result;
}

function postForm(form, url){
	if(checkRequiredFields(form, false)){
		var aForm = objId(form);
		aForm.action = url;
		aForm.submit();
	}
}

function postaForm(form, url){
	var aForm = objId(form);
	aForm.action = url;
	aForm.submit();
}

function showPress(evt) {
     evt = (evt) ? evt : ((event) ? event : null);
     if (evt) {
		if (evt.which && (evt.which == 116)){
			return false;
		}
        if(window.event && (evt.keyCode == 116)){
			evt.keyCode = 8;
			if (evt.keyCode ==  8 ){
				evt.cancelBubble = true;
				evt.returnValue = false;
				return false;
			}
		}
     }
}

function postaLink(url){
	location.href = url;
}

function ritornaIndietro(){
	return /TornaIndietro/.test(location.search);
}
//document.onkeydown = showPress;
//document.onkeyup = showPress;

function randomPassword(length){
  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  pass = "";
  for(x=0;x<length;x++){
    i = Math.floor(Math.random() * 62);
    pass += chars.charAt(i);
  }
  return pass;
}

function contaParole(stringa){
   var alpha=/[^A-za-z0-9@#*.:,;!אטילעש'-/"]/g;
   stringa=stringa.replace(alpha, " ");
   var spazi=/\s+/g;
   var risultato = stringa.replace(spazi," ");
   return risultato.split(" ").length;
}

function caratteriRiservati(stringa, pattern){
	var pat = new RegExp(pattern, 'g');
	return pat.test(stringa);
}

function controlloRegExp( text, regular ) { 
	regexp = new RegExp( regular ); 
	if (isNotNull(text)){
		return (regexp.test( text ));
	}
}

function HtmlToiso88599(strText){
	strText = strText.replace(/&#39;/g,"\'");
	strText = strText.replace(/&quot;/g,"\"");
	strText = strText.replace(/&nbsp;/g," ");
	return strText;
}

	var Keys = unescape(location).split("&");
    for (var Id in Keys)
    	Keys[Id]=Keys[Id].split("=");
    location.Key = Keys;

function QueryString(key){
   	var QSvalue;
   	for (var Id in location.Key)
   		if (location.Key[Id][0]==key)
   			return location.Key[Id][1];
}
	location.QS=QueryString;

function buildQueryById(key, id){
	var result = "";
	if(objId(id)){
		if(isNotNull(objId(id).value))
			result = "&" + key + "=" + objId(id).value;
	}
	return result;
}

function buildQuery(key, value){
	var result = "";
	if(isNotNull(value))
		result = "&" + key + "=" + value;
		
	return result;
}

function isNotNull(value){
	return value != "";
}
