var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
};

function toggleShow(id, link) {
	$(id).toggle();
	if($(link).textContent == 'Show'){
		$(link).update('Hide');
	}
	else{
		$(link).update('Show');
	}
}



function openDir( form ) { 

	var newIndex = form.per_page.selectedIndex; 

	cururl = form.per_page.options[ newIndex ].value; 

	window.location.assign( cururl ); 
 

}



function switchAutoRenew(img_elem, id, type){
	if(window.console) console.debug(img_elem);
	new Ajax.Request('ajax/auto_renew.php?id=' + id + '&type=' + type, {
		onSuccess: function(transport) {
			if(transport.responseText == 'YES'){
				img_elem.src = '/control/template/dns_template/images/tick-small.png';
				img_elem.alt = 'Yes';
			}
			else{
				img_elem.src = '/control/template/dns_template/images/cross-small.png';
				img_elem.alt = 'No';
			}
		}
	});
}

function toggleInvoiceItems(a_elem, id){
	if($('detail' + id) == null){
		var tr = a_elem.parentNode.parentNode;
		var new_row = new Element('tr', {'id':'detail' + id, 'class':tr.className});
		var new_td = new Element('td', {'colspan':'7'});
		new Ajax.Request('ajax/invoice_list_items.php?invoice_id=' + id, {
			onSuccess: function(transport) {
				new_td.update(transport.responseText);
			}
		});
		new_row.update(new_td);
		tr.parentNode.insertBefore(new_row, tr.nextSibling);
	}
	else{
		$('detail' + id).remove();
	}
}

function updateTable(domain_id){
	new Ajax.Updater('dnstable', 'ajax/dns.php', {
		parameters: { display : 'true', domain : domain_id }
	});
}

function displayDNSEntry(domain_id){
	new Ajax.Updater('dnsentrypane', 'ajax/dns.php?domain=' + domain_id, {
		parameters: { add_new : 'true' },
		onComplete: function(transport){
			$('addnewform').observe('submit', function(event){
				showLoading();
				new Ajax.Request('ajax/dns.php?save_new&domain=' + domain_id + '&save_new_record=' + domain_id, {
					method : 'POST',
					parameters : $('addnewform').serialize(),
					onSuccess: function(transport) {
						hideLoading();
						$('dnsentrypane').update(transport.responseText);
						updateTable(domain_id);
					}
				});
				event.stop();
			});
		}
	});
}

function editDNSEntry(domain_id, dns_id){
	new Ajax.Updater('dnsentrypane', 'ajax/dns.php?domain=' + domain_id, {
		parameters: { edit : dns_id },
		onComplete: function(transport){
			$('editform').observe('submit', function(event){
				showLoading();
				new Ajax.Request('ajax/dns.php?save_edit&domain=' + domain_id + '&save=' + dns_id, {
					method : 'POST',
					parameters : $('editform').serialize(),
					onSuccess: function(transport) {
						hideLoading();
						$('dnsentrypane').update(transport.responseText);
						updateTable(domain_id);
					}
				});
				event.stop();
			});
		}
	});
}

function deleteDNSEntry(domain_id, id){
	showLoading();
	new Ajax.Request('ajax/dns.php', {
		parameters: { 'delete' : id, 'domain' : domain_id },
		onSuccess : function(transport){
			hideLoading();
			updateTable(domain_id);
		}
	});
	return false;
}

function addForwarderField(){
	var list = $('forward_list');
	var input = new Element('input', {type:'text', name:'forward[]', disabled:'disabled'});
	list.insert(new Element('li').update(input));
	input.form = $('addaccount');
}


function toggleForwardingForm(checkbox) {
	if(checkbox.checked){
		$('password').disable();
		$('confirm_password').disable();
		//$('goto').enable();
		Element.extend(checkbox.form).getInputs('text', 'forward[]').invoke('enable');
	}
	else{
		$('password').enable();
		$('confirm_password').enable();
		//$('goto').disable();
		Element.extend(checkbox.form).getInputs('text', 'forward[]').invoke('disable');
	}
}

function setMailboxOn(domain){
	var mailbox_status = $('mailboxstatus');
	new Ajax.Request('ajax/mail.php', {
		parameters: { 'domain' : domain, 'enabled' : enabled },
		onSuccess : function(transport){
			//console.debug(enabled);
			if(!enabled){
				mailbox_status.update('Mail turned off for this domain: <input type="submit" name="mailbox" value="Turn On">');
				mailbox_status.className = 'warning';
			}
			else{
				mailbox_status.update('<input type="submit" name="mailbox" value="Turn Off">');
				mailbox_status.className = 'okay';
			}
			enabled = !enabled;
		}
	});
}
/*
*Code for Suggestor
*/


/*
 * 1stDNS Javascript for control panel
 */

/* send request (One way request, update a database with information.
 *
 * add_item(item, action)
 *
 */
function dns_send_request(item, action, url) {
	var url = url;
	var params = 'item=' + item + '&action=' + action;
	var ajax = new Ajax.Updater(
			{success: 'cartResult'},
			url,
			{method: 'get', parameters: params, onFailure: reportError});
}

function reportError(request) {
	$F('cartResult') = 'An error occurred';
}

function update_basket() {
	dns_get_request('getbasket.php', 'basket');
}

/* Retreive request */
function dns_get_request(url, container){
	var ajaxRequest;
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.open('GET', url, true);
	ajaxRequest.send(null);
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById(container).innerHTML = ajaxRequest.responseText;
		}
	}
}
 
function check_domains(value) {
	dns_get_request("checkdomain.php?item=" + value, value + "a");
}
 
 
 
var state = 'none';
 
function showhide(layer_ref) {
	 
	if (state == 'block') { 
		state = 'none'; 
	} else { 
		state = 'block'; 
	} 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval( "document.all." + layer_ref + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[layer_ref].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		hza = document.getElementById(layer_ref); 
		hza.style.display = state; 
	} 
} 


function dns_hidediv(val) { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(val).style.visibility = 'hidden'; 
	} 
/*	else { 
		if (document.layers) { // Netscape 4 
			document.var.visibility = 'hidden'; 
		} 
		else { // IE 4 
			document.all.var.style.visibility = 'hidden'; 
		} 
	} */
}

function dns_showdiv(val) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(val).style.visibility = 'block';
	}
}
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
 
function showLoading(){
	var overlay = new Element('div', {'id':'loading-overlay'});
	$(document.body).insert(overlay);
}

function hideLoading(){
	$('loading-overlay').remove();
}
