function showEditBoxes(contact)
{
    form = $(contact+'form');
    if (contact == 'nsList') {
        $('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:saveNameServers(\''+contact+'\');">Save</a>&nbsp;&nbsp;<a class="liveupdate" href="javascript:cancelEditBoxes(\''+contact+'\'); ">Cancel</a>';        
    } else if (contact == 'DNS') {
        $('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:saveDNS(\''+contact+'\');">Save</a>&nbsp;&nbsp;<a class="liveupdate" href="javascript:cancelEditBoxes(\''+contact+'\'); ">Cancel</a>';
    } else {
        $('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:saveEditBoxes(\''+contact+'\');">Save</a>&nbsp;&nbsp;<a class="liveupdate" href="javascript:cancelEditBoxes(\''+contact+'\'); ">Cancel</a>';
    }
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name.substring(0,1) == '_') continue;
        $('view_'+form.elements[i].name).style.display='none';
        $('edit_'+form.elements[i].name).style.display='';
    }
}

function cancelEditBoxes(contact)
{
    form = $(contact+'form');
    $('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\''+contact+'\');">Edit</a>';
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name.substring(0,1) == '_') continue;
        $('view_'+form.elements[i].name).style.display='';
        $('edit_'+form.elements[i].name).style.display='none';
    }
}

function toggleFields(contact)
{
    form = $(contact+'form');
    display = '';
    radioyes = $(contact+'_default_yes');
    if (radioyes.checked) {
        display = 'none';
    }
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name != contact+'_default') {
            $('tr'+form.elements[i].name).style.display=display;
        }
    }
}

function saveEditBoxes(contact)
{
    form = $(contact+'form');
    $('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\''+contact+'\');">Edit</a>';
    post = '';
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name != contact + '_default' || form.elements[i].checked) {
            post += '&' + form.elements[i].name + '=' + form.elements[i].value;
        }
        $('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        $('view_'+form.elements[i].name).style.display='';
        $('edit_'+form.elements[i].name).style.display='none';
    }
    setStatus('Saving...');
	var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + '&type=' + contact + post,
		onSuccess: updateContactInformationSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=UpdateContactInformation', opt);
}

function saveNameServers(form)
{
    form = $('nsListform');
    $('tdnsList').innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\'nsList\');">Edit</a>';
    $('nsListResults').innerHTML = '';
    post = '';
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name != 'nsList_default') {
            post += '&' + form.elements[i].name + '=' + form.elements[i].value;
            $('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        } else {
            post += '&' + form.elements[i].name + '=';
            if ($('nsList_default_yes') != null && $('nsList_default_yes').checked) {
                post += 'yes';
            } else {
                post += 'no';
            }
        }
        $('view_'+form.elements[i].name).style.display='';
        $('edit_'+form.elements[i].name).style.display='none';
    }
    setStatus('Saving...');
	var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + post,
		onSuccess: updateNameServersSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=UpdateNameServers', opt);
}

function saveDNS(form)
{
    form = $('DNSform');
    $('tdDNS').innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\'DNS\');">Edit</a>';
    $('nsListResults').innerHTML = '';
    post = '';
    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name.substring(0,1) == '_') continue;
        if (form.elements[i].name.match('_type') == null) {
            post += '&' + form.elements[i].name + '=' + form.elements[i].value;
            $('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        } else {
            post += '&' + form.elements[i].name + '=' + selectedValue($(form.elements[i].name));
            $('view_'+form.elements[i].name).innerHTML = selectedValue($(form.elements[i].name));
        }
        $('view_'+form.elements[i].name).style.display='';
        $('edit_'+form.elements[i].name).style.display='none';
    }
    setStatus('Saving...');
	var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + post,
		onSuccess: updateNameServersSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=UpdateDns', opt);
}

function updateNameServersSuccess(t)
{
    unsetStatus('', 1);
    $('nsListResults').innerHTML = t.responseText;
}

function updateContactInformationSuccess(t)
{
    unsetStatus('', 1);
    $('nsListResults').innerHTML = t.responseText;
}

function checkNSStatus()
{
    $('nsCheckResults').innerHTML = '';
    form = $('nsCheckform');
    if (form.elements[0].value == '') return;
    
    $('checkNSLink').style.display = 'none';
    $('checkNSWorking').style.display = '';
    setStatus('Checking...');
    
	var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + '&ns=' + form.elements[0].value + '.' + domainname,
		onSuccess: checkNSStatusSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&view=CheckNSStatus', opt);
}

function checkNSStatusSuccess(t)
{
    unsetStatus('', 1);
    $('checkNSLink').style.display = '';
    $('checkNSWorking').style.display = 'none';
    $('nsCheckResults').innerHTML = t.responseText;
}

function registerNS()
{
    $('nsRegisterResults').innerHTML = '';
    form = $('nsRegisterform');
    $('registerNSLink').style.display = 'none';
    $('registerNSWorking').style.display = '';
    setStatus('Registering...');
    
    var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + '&ns=' + form.nsRegisterName.value + '.' + domainname + '&ip=' + form.nsRegisterIP.value,
		onSuccess: registerNSSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=RegisterNS', opt);
}

function registerNSSuccess(t)
{
    unsetStatus('', 1);
    $('registerNSLink').style.display = '';
    $('registerNSWorking').style.display = 'none';
    $('nsRegisterResults').innerHTML = t.responseText;
    
}

function editNS()
{
    $('nsEditResults').innerHTML = '';
    form = $('nsEditform');
    $('editNSLink').style.display = 'none';
    $('editNSWorking').style.display = '';
    setStatus('Editing...');
    
    var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + '&ns=' + form.nsEditName.value + '.' + domainname + '&newip=' + form.nsEditNewIP.value + '&oldip=' + form.nsEditOldIP.value,
		onSuccess: editNSSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=EditNS', opt);
}

function editNSSuccess(t)
{
    unsetStatus('', 1);
    $('editNSLink').style.display = '';
    $('editNSWorking').style.display = 'none';
    $('nsEditResults').innerHTML = t.responseText;
    
}

function deleteNS()
{
    $('nsDeleteResults').innerHTML = '';
    form = $('nsDeleteform');
    $('deleteNSLink').style.display = 'none';
    $('deleteNSWorking').style.display = '';
    setStatus('Deleting...');
    
    var opt = {
		method: 'post',
		postBody: 'domainid=' + domainid + '&ns=' + form.nsDeleteName.value + '.' + domainname,
		onSuccess: deleteNSSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=DeleteNS', opt);
}

function deleteNSSuccess(t)
{
    unsetStatus('', 1);
    $('deleteNSLink').style.display = '';
    $('deleteNSWorking').style.display = 'none';
    $('nsDeleteResults').innerHTML = t.responseText;
    
}

function deleteDomains(page,sortby,order,num,filter)
{
    if(confirm(lang("Click OK if you are sure you want to delete these domains?")))
    {
        setStatus('Deleting...');
        
        form = document.forms['domains'];
        items = "";
        for(x=1;x<=form.elements.length;x++){
            if (form.elements[x-1].checked){
                if (form.elements[x-1].value!=0){
                    items += form.elements[x-1].value;
                    items += " ";
                }
            }
        }
        
        var opt = {
    		method: 'post',
    		postBody: 'items=' + items + '&ret=' + page + '|' + sortby + '|' + order + '|' + num + '|' + filter,
    		onSuccess: deleteDomainsSuccess
    	}	
    	new Ajax.Request('index.php?fuse=domains&action=DeleteDomains_Async', opt);
    }
}

function deleteDomainsSuccess(t)
{
    unsetStatus('', 1);
    ret = t.responseText.split('|');
    $('errMessages').innerHTML = ret[0];
    fetchList(ret[1],ret[2],ret[3],ret[4],ret[5]);
}

function fetchPage(page)
{
    if (window.domainid === undefined) domainid = 0;
    $('working').style.display = '';
    $('managePage').style.display = 'none';
    setStatus('Loading...');
	var opt = {
		method: 'post',
		postBody: 'domainid='+domainid,
		onSuccess: fetchPageSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&view='+page, opt);
}

function fetchPageSuccess(t)
{
    $('managePage').innerHTML = t.responseText;
    $('managePage').style.display = '';
    $('working').style.display = 'none';
    unsetStatus('', 1);
}

function fetchList(page,sortby,order,num,filter)
{
    if (window.domainid === undefined) domainid = 0;
    $('working').style.display = '';
    $('managePage').style.display = 'none';
    setStatus('Loading...');
	var opt = {
		method: 'post',
		postBody: 'page='+page+'&sortby='+sortby+'&order='+order+'&num='+num+'&filter='+filter,
		onSuccess: fetchListSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&view=DomainsList', opt);   
}

function fetchListSuccess(t)
{
    $('managePage').innerHTML = t.responseText;
    $('managePage').style.display = '';
    $('working').style.display = 'none';
    unsetStatus('', 1);
}

function jumpTo(num, numPages, registrar)
{
    if (num >= 1 && num <= numPages) {
        fetchPage('GetDomainList&registrar='+registrar+'&page='+num);
    } else {
        setStatus('Invalid Page Number', 1000);
    }
}

function editAutoRenew()
{
    $('autorenewtext').style.display='none';
    $('autorenewdropdown').style.display='';
}

function saveAutoRenew(domainid)
{
    $('autorenewtext').style.display='';
    $('autorenewdropdown').style.display='none';
    
    if (selectedValue('chAutoRenew') == -1) return;
    
    if (selectedValue('chAutoRenew') == 1) {
        $('autorenewtext').innerHTML = lang('Yes');
    } else {
        $('autorenewtext').innerHTML = lang('No');
    }
    
    setStatus('Working...');
	var opt = {
		method: 'post',
		postBody: 'domainid='+domainid+'&autorenew='+selectedValue('chAutoRenew'),
		onSuccess: saveAutoRenewSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=UpdateAutorenew', opt);
}

function saveAutoRenewSuccess(t)
{
    $('errMessages').innerHTML = t.responseText;
    unsetStatus('', 1);
}

function editLock()
{
    $('locktext').style.display='none';
    $('lockdropdown').style.display='';
}

function saveLock(domainid)
{
    $('locktext').style.display='';
    $('lockdropdown').style.display='none';
    
    if (selectedValue('chLocked') == -1) return;
    
    if (selectedValue('chLocked') == 1) {
        $('locktext').innerHTML = lang('Locked');
    } else {
        $('locktext').innerHTML = lang('Unlocked');
    }
    
    setStatus('Working...');
	var opt = {
		method: 'post',
		postBody: 'domainid='+domainid+'&lock='+selectedValue('chLocked'),
		onSuccess: saveAutoRenewSuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=UpdateLock', opt);
}

function sendTransferKey(domainid)
{
    $('transferkey').style.display='none';
    $('transferworking').style.display='';
    setStatus('Working...');
	var opt = {
		method: 'post',
		postBody: 'domainid='+domainid,
		onSuccess: sendTransferKeySuccess
	}		
	new Ajax.Request('index.php?fuse=domains&action=SendTransferKey', opt);
}

function sendTransferKeySuccess(t)
{
    setStatus('');
    $('errMessages').innerHTML=t.responseText;
    $('transferkey').style.display='';
    $('transferworking').style.display='none';
}

function addDNSRow()
{
    nextid = $('_nextid').value;
    lastid = nextid - 1;
    
    var tr = $('hosts').insertRow(-1);
    tr.className=$('_nextclass').value;
    if ($('_nextclass').value == 'primarycolor') {
        $('_nextclass').value = 'secondarycolor';
    } else {
        $('_nextclass').value = 'primarycolor';
    }
    
    var td = tr.insertCell(0);
    td.innerHTML = '<span id="view_'+nextid+'_hostname" style="display:none"></span><span id="edit_'+nextid+'_hostname" ><input name="'+nextid+'_hostname" type="text" value=""/></span>';
    td.width=50;
    var td = tr.insertCell(1);
    td.innerHTML = '<span id="view_'+nextid+'_type" style="display:none"></span><span id="edit_'+nextid+'_type"><select id="'+nextid+'_type" name="'+nextid+'_type">'+$('_option').value+'</select></span>';
    td.width=90;
    td = tr.insertCell(2);
    td.innerHTML = '<span id="view_'+nextid+'_address" style="display:none"></span><span id="edit_'+nextid+'_address"><input name="'+nextid+'_address" type="text" value=""/><span id="'+nextid+'_add">'+$(lastid+'_add').innerHTML+'</span></span>';
    td.width=60;
    td.align='left';
    $(lastid+'_add').style.display='none';
    nextid++;
    $('_nextid').value = nextid;
}
