/*
* disc.js
* contais code from Lert v1.0
*
* by Jeffrey Sambells - http://JeffreySambells.com
* For more information on this script, visit http://JeffreySambells.com/openprojects/lert/
* Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
* Icons from Tango Desktop Project http://tango.freedesktop.org/Tango_Desktop_Project
*/
function Lert(message, buttons, options) {
	this.message_ = message;
	this.buttons_ = buttons;
	this.defaultButton_ = options.defaultButton || this.buttons_[0];
	this.icon_ = options.icon || null;
  if(options.hasOverlay == undefined){
          this.hasOverlay_ = true;
  }else{
          this.hasOverlay_ = options.hasOverlay;
  }
}

Lert.prototype.display = function() {
	var body = document.getElementsByTagName ('BODY')[0];
	var pageScroll = getPageScroll();
	var pageSize = getPageSize();

	//create the overlay if necessary
	var overlay = document.getElementById('lertOverlay');
	if(!overlay) {
		var overlay = document.createElement("div");
		overlay.setAttribute('id','lertOverlay');
		overlay.style.display = 'none';
		body.appendChild(overlay);
	}

	//position and show the overlay
	overlay.style.height=pageSize[1]+'px';
  if(this.hasOverlay_){
  overlay.style.display='block';
  }
	//create the container if necessary
	var container = document.getElementById('lertContainer');
	if(!container) {
		var container = document.createElement("div");
		container.setAttribute('id','lertContainer');
		container.style.display = 'none';
		body.appendChild(container);
	}

	//position and show the container
	container.style.top = ( pageScroll[1] + (pageSize[3] / 2) - 40 ) + 'px';
	container.style.display = 'block';

	//create the window
	var win = document.createElement('div');
	win.setAttribute('id','lertWindow');

	//create the optional icon
	if(this.icon_ != null) {
		var icon = document.createElement('img');
		icon.setAttribute('src',this.icon_);
		icon.setAttribute('id','lertIcon');
		icon.setAttribute('alt','');
		win.appendChild(icon);
	}

	//create the message space
	var message = document.createElement('p');
	message.setAttribute('id','lertMessage');
	message.innerHTML = this.message_;
	win.appendChild(message);

	//create the button space
	var buttons = document.createElement('div');
	buttons.setAttribute('id','lertButtons');
	

	var oldKeyDown = document.onkeydown;

	//add each button
	for(i in this.buttons_) {
		var button = this.buttons_[i];
		if(button.getDom) {
			var domButton = button.getDom(function() {
				container.style.display = 'none';
				overlay.style.display = 'none';
				document.onkeydown=oldKeyDown;
				container.innerHTML = '';
				button.onclick_;
			},this.defaultButton_);
			buttons.appendChild(domButton);
		}
	}
	win.appendChild(buttons);

	document.onkeydown = this.keyboardControls;

	//append the window
	container.appendChild(win);
}

Lert.prototype.keyboardControls = function(e) {
	if (e == null) { keycode = event.keyCode; } // ie
	else { keycode = e.which; } // mozilla
	if(keycode==13) { document.getElementById('lertDefaultButton').onclick(); }
}

function LertButton(label, event, options) {
	this.label_ = label;
	this.onclick_ = event;
	this.eventClick = function() {};
}

LertButton.prototype.getDom = function(eventCleanup,defaultButton) {
	var button = document.createElement('a');
	button.setAttribute('href','javascript:void(0);');
	button.className = 'lertButton';
	if(this == defaultButton) button.setAttribute('id','lertDefaultButton');
	button.innerHTML = this.label_;

	//thanks to Karel Hajek for this line I forgot
	//required for IE
	button.setAttribute('href','#');

	var eventOnclick =  this.onclick_;
	button.onclick = function() {
		eventCleanup();
		eventOnclick();
	}
	this.eventClick = button.onclick;
	return button;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function openHelpWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function submitSelectedExt(url,action)
{
	exts = document.getElementsByTagName('input');
	getP = new String("&exts=");
	checkCount = 0;

	for(i=0;i<exts.length;i++)
	{
		if(exts[i].checked == true)
		{
			getP+=exts[i].name + "-";
			checkCount++;
		}
	}

	switch(action)
	{
		case "edit":
			if(checkCount < 1)
			{
				alert("Selecione ao menos um ramal para editar.");
			}else{
				location.href=url + getP;
			}
			break;
		case "delete":
			if(checkCount < 1)
			{
				alert("Selecione ao menos um ramal para deletar.");
			}else{
				if(confirm("Você está prestes a deletar os ramais selecionados.\n" + 
						   "CUIDADO: Ésta ação é irreversível!") == true)
				{
					location.href=url + getP;
				}
			}
			break;
	} 
}

function submitSelectedRecord(url)
{
	recs = document.getElementsByTagName('input');
	getP = new String("&recs=");
	checkCount = 0;

	for(i=0;i<recs.length;i++)
	{
		if(recs[i].checked == true)
		{
			getP+=recs[i].name + "@";
			checkCount++;
		}
	}	

	if(checkCount < 1)
	{
		alert("Selecione ao menos uma gravação para deletar.");
	}else{
		if(confirm("Você está prestes a deletar as gravações selecionadas.\n" +
				   "CUIDADO: Ésta ação é irreversível!") == true)
		{
			location.href=url + getP;
		}
	}	
}

function SelectAll(type)
{

	exts = document.getElementsByTagName('input');
	for(i = 0;i < exts.length;i++)
	{
		if(exts[i].type == type)
		{
			if(exts[i].checked)
			{
				exts[i].checked = false;
			}else{
				exts[i].checked = true;
			}
		}
	}
}

/*
function removeAllOptions(selectbox)
{
  var i;
  for( i = selectbox.options.length-1; i>=0 ; i--)
  {
     selectbox.remove(i);
   }
}

function addOption(selectbox,text,value,selected )
{
   var optn = document.createElement("OPTION");
   optn.text = text;
   optn.value = value;
   if(selected)
   {
      optn.selected = true;
   }
   selectbox.options.add(optn);
}

var name_options = new Array();
var value_options = new Array();
var select2_selected;
function PopulateSelect2(select1_name, select2_name)
{
  index = document.getElementById(select1_name).selectedIndex;
  removeAllOptions(document.getElementById(select2_name));
  for(var i = 0; i < name_options[index].length - 1; i++)
  {
    if( select2_selected == value_options[index][i])
    {
       addOption(document.getElementById(select2_name), name_options[index][i], value_options[index][i],true);
    }else{
       addOption(document.getElementById(select2_name), name_options[index][i], value_options[index][i],false);
    }
  }
}*/
