var compressExecuted=false;
window.onloadFuncs=[];
var originalOnload=window.onload;
if (typeof originalOnload!='function') originalOnload=function() { }
window.onload = function() {
	originalOnload();
	while (window.onloadFuncs.length>0) {
		var func=window.onloadFuncs.shift();
		if (func && typeof func=='function') {
			func.call();
		}
	}
}

function compressForm(parentElement) {
	
	eLabels=parentElement.getElementsByTagName("label");
	eInputs=parentElement.getElementsByTagName("input");
	
	for (var i=0;i<eInputs.length;i++) {
		if (eInputs[i].getAttribute("rel")=="compressed" && eInputs[i].getAttribute("id")) {
			for (var j=0;j<eLabels.length;j++) {
				if (eLabels[j].attributes["for"].value==eInputs[i].getAttribute("id")) {
					if (eInputs[i].processed) continue;
					eInputs[i].processed=true;
					
					eLabels[j].style.display="none";
					
					if (eInputs[i].type=="password") {
						var redundantInput=document.createElement("input");
						redundantInput.type="text";
						redundantInput.value=eLabels[j].innerHTML;
						redundantInput.realElement=eInputs[i];
						redundantInput.style.color="#909090";
						if (eInputs[i].id) redundantInput.id="real_"+eInputs[i].id;
						eInputs[i].redundantElement=redundantInput;
						eInputs[i].style.display="none";
						
						redundantInput.onfocus=function() {
							this.style.display="none";
							this.realElement.value="";
							this.realElement.style.display="";
							this.realElement.focus();
						}
						eInputs[i].onblur=function() {
							if (this.value=="") {
								this.style.display="none";
								this.redundantElement.style.display="";
							}
						}
						
						
						eInputs[i].onblur();
						parentElement.insertBefore(redundantInput,eInputs[i]);
						
						
					} else {
						if (!eInputs[i].value) {
							if (eInputs[i].realElement) {
								eInputs[i].realElement.value=eLabels[j].innerHTML;
								eInputs[i].realElement.title=eLabels[j].innerHTML;
								eInputs[i].title=eLabels[j].innerHTML;
							} else {
								eInputs[i].value=eLabels[j].innerHTML;
								eInputs[i].title=eLabels[j].innerHTML;
							}
						}
						eInputs[i].labelValue=eLabels[j].innerHTML;
						eInputs[i].onfocus=function() {
							if (this.labelValue==this.value) this.value="";
							if (this.style) this.style.color="";
						}
						eInputs[i].onblur=function() {
							if (!this.value.length || this.value==this.labelValue) {
								if (this.oldType) this.type="text";
								this.value=this.labelValue;
								this.style.color="#909090";
							}
						}
						eInputs[i].onblur();
					}
					
				}
			}
		}
	}
	
	parentElement.onsubmit=function() {
		var arr=parentElement.getElementsByTagName("input");
		for (var i=0;i<arr.length;i++) {
			if (arr[i].labelValue && arr[i].labelValue==arr[i].value) {
				arr[i].value="";
			}
		}
	}
	window.onunload=parentElement.onsubmit;
}

window.appendOnload=function(func) {
	if (func && typeof func=='function') {
		window.onloadFuncs.push(func);
	}
}

window.prependOnload=function(func) {
	if (func && typeof func=='function') {
		window.onloadFuncs.unshift(func);
	}
}


// Stoopd bloody IE
function select_innerHTML(objeto,innerHTML){
/******
* select_innerHTML - corrige o bug do InnerHTML em selects no IE
* Veja o problema em: http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
* Versão: 2.1 - 04/09/2007
* Autor: Micox - Náiron José C. Guimarães - micoxjcg@yahoo.com.br
* @objeto(tipo HTMLobject): o select a ser alterado
* @innerHTML(tipo string): o novo valor do innerHTML
*******/
    objeto.innerHTML = ""
    
    if (innerHTML.replace(/^\s*/, "").replace(/\s*$/, "")=="") return;
    
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.indexOf("<option")<0){//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}
