//Application.js
function setHiddenBlock(idControl){
	objdiv = document.getElementById(idControl);
	if (objdiv.style.display=="block"){
		objdiv.style.display="none";
		alert('block');
		
	}else if (objdiv.style.display=="none"){
		objdiv.style.display="block";
		alert('none');
		
	}

}
function setActive(idControlComentario,idControlHiddenUser,idControlUserName,idControlUserEmail,userId,userName,userEmail){
    cComentario = document.getElementById(idControlComentario);
    cOculto = document.getElementById(idControlHiddenUser);
    cUserName = document.getElementById(idControlUserName);
    cUserEmail = document.getElementById(idControlUserEmail);
    
    cComentario.readOnly = false;
    cOculto.value = userId;
    cUserName.value = userName;
    cUserEmail.value = userEmail;
 
}
function setEmpty(idControlComentario,idControlHiddenUser,idControlUserName,idControlUserEmail){
    cComentario = document.getElementById(idControlComentario);
    cOculto = document.getElementById(idControlHiddenUser);
    cUserName = document.getElementById(idControlUserName);
    cUserEmail = document.getElementById(idControlUserEmail);
    cComentario.value = "";
    cOculto.value= "";
    cUserName.value = "";
    cUserEmail.value = "";
    
}



function shTable(idTable){
    obj = document.getElementById(idTable);
    //alert(obj.style.display);
    if (obj.style.display="block"){
        hideTable(idTable);
        //alert(obj.style.display);
    }else{
        showTable(idTable);
        
    }
    //alert(obj.style.display);
}

function hideTable(idTable){
    obj = document.getElementById(idTable);
    obj.style.display="none";

}
function showTable(idTable){
    obj = document.getElementById(idTable);
    obj.style.display="block";
    alert(obj.style.display);
}
function popup(url) {
	newwindow=window.open(url,'name','height=200,width=150');
	if (window.focus) {newwindow.focus()}
	return false;
}

function showHideFile(active,inactive1,inactive2){
	d_active = document.getElementById(active);
	d1_inactive = document.getElementById(inactive1);
	d2_inactive = document.getElementById(inactive2);
	
	d_active.style.display="block";
	d1_inactive.style.display="none";
	d2_inactive.style.display="none";

}

function showHideAct(active,inactive1){
	day = document.getElementById('UsuarioUSRFechaDesactivacionDay');
	month = document.getElementById('UsuarioUSRFechaDesactivacionMonth');
	year = document.getElementById('UsuarioUSRFechaDesactivacionYear');
	
	
	d_active = document.getElementById(active);
	d1_inactive = document.getElementById(inactive1);
	
	if (active=="especifica"){
		day.disabled = false;
		month.disabled = false;
		year.disabled = false;
	}else{
		day.disabled = true;
		month.disabled = true;
		year.disabled = true;
	}
	
	d_active.style.display="block";
	d1_inactive.style.display="none";
	
	

}


function setAreaCode(idPais,idControl){
    var array_codes = new Array();
    obj = document.getElementById(idControl);
    array_codes['AD']='376';
    array_codes['AE']='971';
    array_codes['AF']='93';
    array_codes['AD']='376';
    array_codes['AE']='971';
    array_codes['AF']='93';
    array_codes['AG']='1268';
    array_codes['AI']='1264';
    array_codes['AL']='355';
    array_codes['AM']='374';
    array_codes['AN']='599';
    array_codes['AO']='244';
    array_codes['AQ']='672';
    array_codes['AR']='54';
    array_codes['AS']='684';
    array_codes['AT']='43';
    array_codes['AU']='61';
    array_codes['AW']='297';
    array_codes['AZ']='994';
    array_codes['BA']='387';
    array_codes['BB']='1246';
    array_codes['BD']='880';
    array_codes['BE']='32';
    array_codes['BF']='226';
    array_codes['BG']='359';
    array_codes['BH']='973';
    array_codes['BI']='257';
    array_codes['BJ']='229';
    array_codes['BM']='1441';
    array_codes['BN']='673';
    array_codes['BO']='591';
    array_codes['BR']='55';
    array_codes['BS']='1242';
    array_codes['BT']='975';
    array_codes['BV']='';
    array_codes['BW']='267';
    array_codes['BY']='375';
    array_codes['BZ']='501';
    array_codes['CA']='1';
    array_codes['CC']='61';
    array_codes['CD']='243';
    array_codes['CF']='236';
    array_codes['CG']='242';
    array_codes['CH']='41';
    array_codes['CI']='225';
    array_codes['CK']='682';
    array_codes['CL']='56';
    array_codes['CM']='237';
    array_codes['CN']='86';
    array_codes['CO']='57';
    array_codes['CR']='506';
    array_codes['CU']='53';
    array_codes['CV']='238';
    array_codes['CX']='61';
    array_codes['CY']='357';
    array_codes['CZ']='420';
    array_codes['DE']='49';
    array_codes['DJ']='253';
    array_codes['DK']='45';
    array_codes['DM']='1767';
    array_codes['DO']='1809';
    array_codes['DZ']='213';
    array_codes['EC']='593';
    array_codes['EE']='372';
    array_codes['EG']='20';
    array_codes['EH']='212';
    array_codes['ER']='291';
    array_codes['ES']='34';
    array_codes['ET']='251';
    array_codes['FI']='358';
    array_codes['FJ']='679';
    array_codes['FK']='500';
    array_codes['FM']='691';
    array_codes['FO']='298';
    array_codes['FR']='33';
    array_codes['FX']='';
    array_codes['GA']='241';
    array_codes['GB']='44';
    array_codes['GD']='1473';
    array_codes['GE']='995';
    array_codes['GF']='594';
    array_codes['GH']='233';
    array_codes['GI']='350';
    array_codes['GL']='299';
    array_codes['GM']='220';
    array_codes['GN']='224';
    array_codes['GP']='590';
    array_codes['GQ']='240';
    array_codes['GR']='30';
    array_codes['GS']='';
    array_codes['GT']='502';
    array_codes['GU']='1671';
    array_codes['GW']='245';
    array_codes['GY']='592';
    array_codes['HK']='852';
    array_codes['HM']='';
    array_codes['HN']='504';
    array_codes['HR']='385';
    array_codes['HT']='509';
    array_codes['HU']='36';
    array_codes['ID']='62';
    array_codes['IE']='353';
    array_codes['IL']='972';
    array_codes['IN']='91';
    array_codes['IO']='246';
    array_codes['IQ']='964';
    array_codes['IR']='98';
    array_codes['IS']='354';
    array_codes['IT']='39';
    array_codes['JM']='1876';
    array_codes['JO']='962';
    array_codes['JP']='81';
    array_codes['KE']='254';
    array_codes['KG']='996';
    array_codes['KH']='855';
    array_codes['KI']='686';
    array_codes['KM']='269';
    array_codes['KN']='1869';
    array_codes['KP']='850';
    array_codes['KR']='82';
    array_codes['KW']='965';
    array_codes['KY']='1345';
    array_codes['KZ']='7';
    array_codes['LA']='856';
    array_codes['LB']='961';
    array_codes['LC']='1758';
    array_codes['LI']='41';
    array_codes['LK']='94';
    array_codes['LR']='231';
    array_codes['LS']='266';
    array_codes['LT']='370';
    array_codes['LU']='352';
    array_codes['LV']='371';
    array_codes['LY']='218';
    array_codes['MA']='212';
    array_codes['MC']='377';
    array_codes['MD']='373';
    array_codes['MG']='261';
    array_codes['MH']='692';
    array_codes['MK']='389';
    array_codes['ML']='223';
    array_codes['MM']='95';
    array_codes['MN']='976';
    array_codes['MO']='853';
    array_codes['MP']='1670';
    array_codes['MQ']='596';
    array_codes['MR']='222';
    array_codes['MS']='1664';
    array_codes['MT']='356';
    array_codes['MU']='230';
    array_codes['MV']='960';
    array_codes['MW']='265';
    array_codes['MX']='52';
    array_codes['MY']='60';
    array_codes['MZ']='258';
    array_codes['NA']='264';
    array_codes['NC']='687';
    array_codes['NE']='227';
    array_codes['NF']='672';
    array_codes['NG']='234';
    array_codes['NI']='505';
    array_codes['NL']='31';
    array_codes['NO']='47';
    array_codes['NP']='977';
    array_codes['NR']='674';
    array_codes['NU']='683';
    array_codes['NZ']='64';
    array_codes['OM']='968';
    array_codes['PA']='507';
    array_codes['PE']='51';
    array_codes['PF']='689';
    array_codes['PG']='675';
    array_codes['PH']='63';
    array_codes['PK']='92';
    array_codes['PL']='48';
    array_codes['PM']='508';
    array_codes['PN']='872';
    array_codes['PR']='1';
    array_codes['PT']='351';
    array_codes['PW']='680';
    array_codes['PY']='595';
    array_codes['QA']='974';
    array_codes['RE']='262';
    array_codes['RO']='40';
    array_codes['RU']='7';
    array_codes['RW']='250';
    array_codes['SA']='966';
    array_codes['SB']='677';
    array_codes['SC']='248';
    array_codes['SD']='249';
    array_codes['SE']='46';
    array_codes['SG']='65';
    array_codes['SH']='290';
    array_codes['SI']='386';
    array_codes['SJ']='79';
    array_codes['SK']='421';
    array_codes['SL']='232';
    array_codes['SM']='378';
    array_codes['SN']='221';
    array_codes['SO']='252';
    array_codes['SR']='597';
    array_codes['ST']='239';
    array_codes['SV']='503';
    array_codes['SY']='963';
    array_codes['SZ']='268';
    array_codes['TC']='1649';
    array_codes['TD']='235';
    array_codes['TF']='';
    array_codes['TG']='228';
    array_codes['TH']='66';
    array_codes['TJ']='992';
    array_codes['TK']='690';
    array_codes['TM']='993';
    array_codes['TN']='216';
    array_codes['TO']='676';
    array_codes['TP']='670';
    array_codes['TR']='90';
    array_codes['TT']='1868';
    array_codes['TV']='688';
    array_codes['TW']='886';
    array_codes['TZ']='255';
    array_codes['UA']='380';
    array_codes['UG']='256';
    array_codes['UM']='808';
    array_codes['US']='1';
    array_codes['UY']='598';
    array_codes['UZ']='998';
    array_codes['VA']='379';
    array_codes['VC']='1784';
    array_codes['VE']='58';
    array_codes['VG']='1284';
    array_codes['VI']='1340';
    array_codes['VN']='84';
    array_codes['VU']='678';
    array_codes['WF']='681';
    array_codes['WS']='685';
    array_codes['YE']='967';
    array_codes['YT']='269';
    array_codes['YU']='381';
    array_codes['ZA']='27';
    array_codes['ZM']='260';
    array_codes['ZW']='263';
    obj.value = array_codes[idPais];

}

function CheckBoxGroup() {
	this.controlBox=null;
	this.controlBoxChecked=null;
	this.maxAllowed=null;
	this.maxAllowedMessage=null;
	this.masterBehavior=null;
	this.formRef=null;
	this.checkboxWildcardNames=new Array();
	this.checkboxNames=new Array();
	this.totalBoxes=0;
	this.totalSelected=0;
	// Public methods
	this.setControlBox=CBG_setControlBox;
	this.setMaxAllowed=CBG_setMaxAllowed;
	this.setMasterBehavior=CBG_setMasterBehavior;	// all, some
	this.addToGroup=CBG_addToGroup;
	// Private methods
	this.expandWildcards=CBG_expandWildcards;
	this.addWildcardCheckboxes=CBG_addWildcardCheckboxes;
	this.addArrayCheckboxes=CBG_addArrayCheckboxes;
	this.addSingleCheckbox=CBG_addSingleCheckbox;
	this.check=CBG_check;
	}

// Set the master control checkbox name
function CBG_setControlBox(name) { this.controlBox=name; }

// Set the maximum number of checked boxes in the set, and optionally
// the message to popup when the max is reached.
function CBG_setMaxAllowed(num,msg) {
	this.maxAllowed=num;
	if (msg!=null&&msg!="") { this.maxAllowedMessage=msg; }
	}

// Set the behavior for the checkbox group master checkbox
//	All: all boxes must be checked for the master to be checked
//	Some: one or more of the boxes can be checked for the master to be checked
function CBG_setMasterBehavior(b) { this.masterBehavior = b.toLowerCase(); }

// Add checkbox wildcards to the checkboxes array
function CBG_addToGroup() {
	if (arguments.length>0) {
		for (var i=0;i<arguments.length;i++) {
			this.checkboxWildcardNames[this.checkboxWildcardNames.length]=arguments[i];
			}
		}
	}

// Expand the wildcard checkbox names given in the addToGroup method
function CBG_expandWildcards() {
	if (this.formRef==null) {alert("ERROR: No form element has been passed.  Cannot extract form name!"); return false; }
	for (var i=0; i<this.checkboxWildcardNames.length;i++) {
		var n = this.checkboxWildcardNames[i];
		var el = this.formRef[n];
		if (n.indexOf("*")!=-1) { this.addWildcardCheckboxes(n); }
		else if(CBG_nameIsArray(el)) { this.addArrayCheckboxes(n); }
		else { this.addSingleCheckbox(el); }
		}
	}


// Add checkboxes to the group which match a pattern
function CBG_addWildcardCheckboxes(name) {
	var i=name.indexOf("*");
	if ((i==0) || (i==name.length-1)) {
		searchString= (i)?name.substring(0,name.length-1):name.substring(1,name.length);
		for (var j=0;j<this.formRef.length;j++) {
			currentElement = this.formRef.elements[j];
			currentElementName=currentElement.name;
			var partialName = (i)?currentElementName.substring(0,searchString.length) : currentElementName.substring(currentElementName.length-searchString.length,currentElementName.length);
			if (partialName==searchString) {
				if(CBG_nameIsArray(currentElement)) this.addArrayCheckboxes(currentElement);
				else this.addSingleCheckbox(currentElement);
				}
			}
		}
	}

// Add checkboxes to the group which all have the same name
function CBG_addArrayCheckboxes(name) {
	if((CBG_nameIsArray(this.formRef[name])) && (this.formRef[name].length>0)) {
		for (var i=0; i<this.formRef[name].length; i++) { this.addSingleCheckbox(this.formRef[name][i]); }
		}
	}

function CBG_addSingleCheckbox(obj) {
	if (obj != this.formRef[this.controlBox]) {
		this.checkboxNames[this.checkboxNames.length]=obj;
		this.totalBoxes++;
		if (obj.checked) {
			this.totalSelected++;
			}
		}
	}

// Runs whenever a checkbox in the group is clicked
function CBG_check(obj) {
	var checked=obj.checked;
	if (this.formRef==null) {
		this.formRef=obj.form;
		this.expandWildcards();
		if (this.controlBox==null || obj.name!=this.controlBox) {
			this.totalSelected += (checked)?-1:1;
			}
		}
	if (this.controlBox!=null&&obj.name==this.controlBox) {
		if (this.masterBehavior=="all") {
			for (i=0;i<this.checkboxNames.length;i++) { this.checkboxNames[i].checked=checked; }
			this.totalSelected=(checked)?this.checkboxNames.length:0;
			}
		else {
			if (!checked) {
				obj.checked = (this.totalSelected>0)?true:false;
				obj.blur();
				}
			}
		}
	else {
		if (this.masterBehavior=="all") {
			if (!checked) {
				this.formRef[this.controlBox].checked=false;
				this.totalSelected--;
				}
			else { this.totalSelected++; }
			if (this.controlBox!=null) {
				this.formRef[this.controlBox].checked=(this.totalSelected==this.totalBoxes)?true:false;
				}
			}
		else {
			if (!obj.checked) { this.totalSelected--; }	
			else { this.totalSelected++; }
			if (this.controlBox!=null) {
				this.formRef[this.controlBox].checked=(this.totalSelected>0)?true:false;
				}
			if (this.maxAllowed!=null) {
				if (this.totalSelected>this.maxAllowed) {
					obj.checked=false;
					this.totalSelected--;
					if (this.maxAllowedMessage!=null) { alert(this.maxAllowedMessage); }
					return false;
					}
				}
			}
		}
	}

function CBG_nameIsArray(obj) {
	return ((typeof obj.type!="string")&&(obj.length>0)&&(obj[0]!=null)&&(obj[0].type=="checkbox"));
	}
	
function openLb(){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'
	
}	

function closeLb(){
	document.getElementById('overlay').style.display='none';
	//document.getElementById('lightbox').innerHTML="";
	document.getElementById('lightbox').style.display='none';
	//Element.remove($('lbContent'));
	//this.displayLightbox("none");
	//ligthbox.deactivate;

}

function cleanLb(){
	document.getElementById('lightbox').innerText=' ';

}


/* standard small functions */
function $m(quem){
 return document.getElementById(quem)
}
function remove(quem){
 quem.parentNode.removeChild(quem);
}
function addEvent(obj, evType, fn){
 // elcio.com.br/crossbrowser
    if (obj.addEventListener)
        obj.addEventListener(evType, fn, true)
    if (obj.attachEvent)
        obj.attachEvent("on"+evType, fn)
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, fn );
  } else {
    obj.removeEventListener( type, fn, false ); }
} 
/* THE UPLOAD FUNCTION */
function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){
/******
* micoxUpload - Submit a form to hidden iframe. Can be used to upload
* Use but dont remove my name. Creative Commons.
* Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
* Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
* Parametros:
* form - the form to submit or the ID
* url_action - url to submit the form. like action parameter of forms.
* id_element - element that will receive return of upload.
* html_show_loading - Text (or image) that will be show while loading
* html_error_http - Text (or image) that will be show if HTTP error.
*******/

 //testing if 'form' is a html object or a id string
 form = typeof(form)=="string"?$m(form):form;
 
 var detectWebKit = isWebKit()
 
 var erro="";
 if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.\n";}
 else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
 if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
 if(erro.length>0) {
  alert("Error in call micoxUpload:\n" + erro);
  return;
 }

 //creating the iframe
 var iframe = document.createElement("iframe");
 iframe.setAttribute("id","micox-temp");
 iframe.setAttribute("name","micox-temp");
 iframe.setAttribute("width","0");
 iframe.setAttribute("height","0");
 iframe.setAttribute("border","0");
 iframe.setAttribute("style","width: 0; height: 0; border: none;");
 
 //add to document
 form.parentNode.appendChild(iframe);
 window.frames['micox-temp'].name="micox-temp"; //ie sucks
 
 //add event
 var carregou = function() { 
   removeEvent( $m('micox-temp'),"load", carregou);
   var cross = "javascript: ";
   cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";
   
   $m(id_element).innerHTML = html_error_http;
   $m('micox-temp').src = cross;
   //del the iframe
   //setTimeout(function(){ remove($m('micox-temp'))}, 250);
   if(detectWebKit){
   	remove($m('ajax-temp'));
   }else{
	setTimeout(function(){ remove($m('micox-temp'))}, 250);
   }
  }
 addEvent( $m('micox-temp'),"load", carregou)
 
 //properties of form
 form.setAttribute("target","micox-temp");
 form.setAttribute("action",url_action);
 form.setAttribute("method","post");
 form.setAttribute("enctype","multipart/form-data");
 form.setAttribute("encoding","multipart/form-data");
 //submit
 
// alert("Entro Aqui!!");
 
 form.submit();
 
  
 
 //while loading
 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }
} 

function isWebKit(){
	return RegExp("AppleWebKit/").test(navigator.userAgent);
}


function close_lightBox(){
	document.getElementById('formu').innerHTML='Registro guardado';
	//document.getElementById('formu').style.display="none";
}

function autoResizeIframe(id){
	var altura;
	if (!window.opera && !document.mimeType && document.all && document.getElementById) {
		parent.document.getElementById(id).style.height = this.document.body.offsetHeight + "px";
	}
	else 
		if (document.getElementById) {
			altura = this.document.body.scrollHeight + 25;
			parent.document.getElementById(id).style.height = altura + "px"
		}
	
}

function resizeIframe(ifr){
	var miIframe=document.getElementById(ifr);
	var alturaPagina=miIframe.contentWindow.document.body. scrollHeight;
	miIframe.style.height=alturaPagina+25;
}
