var DHTML = 0, DOM = 0, MS = 0, NS = 0, OP = 0, browserOK = 1;

function DHTML_init() {

 if (window.opera) {
     OP = 1;
 }
 if(document.getElementById) {
   DHTML = 1;
   DOM = 1;
 }
 if(document.all && !OP) {
   DHTML = 1;
   MS = 1;
 }
if (window.netscape && window.screen && !DOM && !OP){
   DHTML = 1;
   NS = 1;
 }
}

function getElem(p1,p2,p3) {
 var Elem;
 
 if(DOM) {
   if(p1.toLowerCase()=="id") {
     if (typeof document.getElementById(p2) == "object")
     Elem = document.getElementById(p2);
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="name") {
     if (typeof document.getElementsByName(p2) == "object")
     Elem = document.getElementsByName(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="tagname") {
     if (typeof document.getElementsByTagName(p2) == "object" ||
        (OP && typeof document.getElementsByTagName(p2) == "function"))
     Elem = document.getElementsByTagName(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
 else if(MS) {
   if(p1.toLowerCase()=="id") {
     if (typeof document.all[p2] == "object")
     Elem = document.all[p2];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="tagname") {
     if (typeof document.all.tags(p2) == "object")
     Elem = document.all.tags(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="name") {
     if (typeof document[p2] == "object")
     Elem = document[p2];
     else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
 else if(NS) {
   if(p1.toLowerCase()=="id" || p1.toLowerCase()=="name") {
   if (typeof document[p2] == "object")
     Elem = document[p2];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="index") {
    if (typeof document.layers[p2] == "object")
     Elem = document.layers[p2];
    else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
 else return void(0);  /* FAbiola */
}


function getCont(p1,p2,p3) {
   var Cont;
   if(DOM && getElem(p1,p2,p3) && getElem(p1,p2,p3).firstChild) {
     if(getElem(p1,p2,p3).firstChild.nodeType == 3)
       Cont = getElem(p1,p2,p3).firstChild.nodeValue;
     else
       Cont = "";
     return(Cont);
   }
   else if(MS && getElem(p1,p2,p3)) {
     Cont = getElem(p1,p2,p3).innerText;
     return(Cont);
   }
   else return void(0);
}

function getAttr(p1,p2,p3,p4) {
   var Attr;
   if((DOM || MS) && getElem(p1,p2,p3)) {
     Attr = getElem(p1,p2,p3).getAttribute(p4);
     return(Attr);
   }
   else if (NS && getElem(p1,p2)) {
       if (typeof getElem(p1,p2)[p3] == "object")
        Attr=getElem(p1,p2)[p3][p4]
       else
        Attr=getElem(p1,p2)[p4]
         return Attr;
       }
   else return void(0);
}

function setCont(p1,p2,p3,p4) {
   if(DOM && getElem(p1,p2,p3) && getElem(p1,p2,p3).firstChild)
     getElem(p1,p2,p3).firstChild.nodeValue = p4;
   else if(MS && getElem(p1,p2,p3))
     getElem(p1,p2,p3).innerText = p4;
   else if(NS && getElem(p1,p2,p3)) {
     getElem(p1,p2,p3).document.open();
     getElem(p1,p2,p3).document.write(p4);
     getElem(p1,p2,p3).document.close();
   }
}

DHTML_init();


var pics = new Array();
var objCount = 0; // number of (changing) images on web-page
var activePic = '';

function preload(name, first, second) {  

  // preload images and place them in an array

  if (browserOK) {     
    pics[objCount] = new Array(3);
    pics[objCount][0] = new Image();
    pics[objCount][0].src = first;
    pics[objCount][1] = new Image();
    pics[objCount][1].src = second;
    pics[objCount][2] = name;
    objCount++;
  }
}

function on(name){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      if (document.images[pics[i][2]] != null)
        if ((name != pics[i][2]) & (document.images[pics[i][2]] != activePic)) { 
          // set back all other pictures
          document.images[pics[i][2]].src = pics[i][0].src;
        } else {
           // show the second image because cursor moves across this image
           document.images[pics[i][2]].src = pics[i][1].src;
        }
    }
  }
}

function off(){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      // set back all pictures
      if ((document.images[pics[i][2]] != null) & (document.images[pics[i][2]] != activePic))
        document.images[pics[i][2]].src = pics[i][0].src;
    }
  }
}

function loadpage(url) { //v3.0
  window.location.href=url;
}

function toggleAllCheckboxesInForm(formName, isChecked) {
  var targetForm = document.forms[formName];
	var earray = document.forms[formName].elements;
	for (var i = 0; i < earray.length; i++) {
		if (earray[i].type == 'checkbox' && earray[i].disabled == false) {
		    earray[i].checked = isChecked;
		}
	}  
}

function toggleIFrame(containerId, iframeUrl) {
	var container = document.getElementById("container_"+containerId);
	var iframe = document.getElementById("iframe_"+containerId);
	// Toggle the visibility of the container
	container.style.display = (container.style.display=="block" ? "none" : "block");
//		alert("#"+iframe.src+"#");
	if (iframe.src=="") {
		iframe.src=iframeUrl;
	}
}

function openwin(x,y){ var PD = open(x,y,'top=0,left=10,width=780,height=550'); }
function openwin2(x,y){ var PD = open(x,y,'top=0,left=10,width=780,height=400,scrollbars=yes'); }
function openwin3(x,y){ var PD = open(x,y,'top=0,left=10,width=780,height=500,scrollbars=yes'); }
function openinfo(x,y){ var PD = open(x,y,'top=0,left=10,width=730,height=260,scrollbars=yes'); }
function openwin(x,y,w,h){ var PD = open(x,y,"top=0,left=10,width="+w+",height="+h); }

function openwinassist(x,y){ var PD = open(x,y,'top=0,left=10,width=900,height=710,scrollbars=yes'); }

function setHelpText(text) {
	document.getElementById("helptext").innerHTML=text;
}

function validatePositiveNumber(inputId) {
  var field = document.getElementById(inputId);
	var value = field.value;

	if (value.indexOf(".")>=0 || value.indexOf(",")>=0) {
		alert("Per favore introducete un valore senza decimali.");
		field.focus();
		return false;
	}
	else if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
		alert("Numero non valido");
		field.focus();
		return false;
	}
	else if (value < 0) {
		alert("Il valore immesso non puņ essere minore di 0");
		field.focus();
		return false;
	}
	return true;
}

function validatePositiveNumberTKIT(inputId) {
  var field = document.getElementById(inputId);
	var value = field.value;

	if (value.indexOf(".")>=0 || value.indexOf(",")>=0) {
		alert("Per favore introducete un valore senza decimali.");
		field.focus();
		return false;
	}
	else if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
		alert("Inserire un consumo compreso tra 1 e 100000 kWh/anno");
		field.focus();
		return false;
	}
	else if (value < 0) {
		alert("Il valore immesso non puņ essere minore di 0");
		field.focus();
		return false;
	}
	return true;
}

function validatePositiveNumberWithComma(inputId) {
  var field = document.getElementById(inputId);
	var value = field.value;

	if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
		alert("Valore non valido");
		field.focus();
		return false;
	}
	else if (value <= 0) {
		alert("Il valore immesso non puņ essere minore di 0");
		field.focus();
		return false;
	}
	return true;
}

function validatePower(inputId) {
  var field = document.getElementById(inputId);
        var value = field.value;

        if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
                alert("Valore non valido");
                field.focus();
                return false;
        }
        else if (value < 1.5) {
                alert("Il valore immesso non puņ essere minore di 1.5");
                field.focus();
                return false;
        }
        return true;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function setInfoText(htmlContent) {
	document.getElementById('infobox').innerHTML=htmlContent;
}

// sets the one time discount option to false if the discount option is set to false
function setDiscountOption(){
	var discount = document.getElementById("discount_rec_select").value;
	var discount_sel=document.getElementById("discount_select");
	var discount_label=document.getElementById("discount_label");
	if(discount=="false"){		
		discount_sel.value="false";
		//discount_sel.style.visibility = 'hidden';
		//discount_label.style.visibility = 'hidden';
	}
	else if(discount=="true"){
		discount_sel.value="true";
		//discount_sel.style.visibility = 'visible';
		//discount_label.style.visibility = 'visible';
	}
}
// sets the discount option to false if the one time discount option is set to false
function setDiscountRecOption(){
	var discount = document.getElementById("discount_select").value;
	
	if(discount=="true"){
		document.getElementById("discount_rec_select").value="true";		
	}
}

function trim(str) { 
	return str.replace(/^\s+|\s+$/g, ''); 
}

function limitToNumberAndLength(f, length){
        if (!/^\d*$/.test(f.value)) {
        
        f.value = f.value.replace(/[^\d]/g,"");
        }
        if(f.value.length>length){
                f.value = f.value.substring(0,length);
        }
} 

