function showEditBoxes(contact)
{
    form = document.getElementById(contact+'form');
    if (contact == 'nsList') {
        document.getElementById('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') {
        document.getElementById('td'+contact).innerHTML = '<a class="liveupdate" href="javascript:saveDNS(\''+contact+'\');">Save</a>&nbsp;&nbsp;<a class="liveupdate" href="javascript:cancelEditBoxes(\''+contact+'\'); ">Cancel</a>';
    } else {
        document.getElementById('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;
        document.getElementById('view_'+form.elements[i].name).style.display='none';
        document.getElementById('edit_'+form.elements[i].name).style.display='';
    }
}

function cancelEditBoxes(contact)
{
    form = document.getElementById(contact+'form');
    document.getElementById('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;
        document.getElementById('view_'+form.elements[i].name).style.display='';
        document.getElementById('edit_'+form.elements[i].name).style.display='none';
    }
}

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

function saveEditBoxes(contact)
{
    form = document.getElementById(contact+'form');
    document.getElementById('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;
        }
        document.getElementById('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        document.getElementById('view_'+form.elements[i].name).style.display='';
        document.getElementById('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 = document.getElementById('nsListform');
    document.getElementById('tdnsList').innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\'nsList\');">Edit</a>';
    document.getElementById('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;
            document.getElementById('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        } else {
            post += '&' + form.elements[i].name + '=';
            if (document.getElementById('nsList_default_yes') != null && document.getElementById('nsList_default_yes').checked) {
                post += 'yes';
            } else {
                post += 'no';
            }
        }
        document.getElementById('view_'+form.elements[i].name).style.display='';
        document.getElementById('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 = document.getElementById('DNSform');
    document.getElementById('tdDNS').innerHTML = '<a class="liveupdate" href="javascript:showEditBoxes(\'DNS\');">Edit</a>';
    document.getElementById('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;
            document.getElementById('view_'+form.elements[i].name).innerHTML = form.elements[i].value;
        } else {
            post += '&' + form.elements[i].name + '=' + selectedValue(document.getElementById(form.elements[i].name));
            document.getElementById('view_'+form.elements[i].name).innerHTML = selectedValue(document.getElementById(form.elements[i].name));
        }
        document.getElementById('view_'+form.elements[i].name).style.display='';
        document.getElementById('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);
    document.getElementById('nsListResults').innerHTML = t.responseText;
}

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

function checkNSStatus()
{
    document.getElementById('nsCheckResults').innerHTML = '';
    form = document.getElementById('nsCheckform');
    if (form.elements[0].value == '') return;
    
    document.getElementById('checkNSLink').style.display = 'none';
    document.getElementById('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=clients&view=CheckNSStatus', opt);
}

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

function registerNS()
{
    document.getElementById('nsRegisterResults').innerHTML = '';
    form = document.getElementById('nsRegisterform');
    document.getElementById('registerNSLink').style.display = 'none';
    document.getElementById('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);
    document.getElementById('registerNSLink').style.display = '';
    document.getElementById('registerNSWorking').style.display = 'none';
    document.getElementById('nsRegisterResults').innerHTML = t.responseText;
    
}

function editNS()
{
    document.getElementById('nsEditResults').innerHTML = '';
    form = document.getElementById('nsEditform');
    document.getElementById('editNSLink').style.display = 'none';
    document.getElementById('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);
    document.getElementById('editNSLink').style.display = '';
    document.getElementById('editNSWorking').style.display = 'none';
    document.getElementById('nsEditResults').innerHTML = t.responseText;
    
}

function deleteNS()
{
    document.getElementById('nsDeleteResults').innerHTML = '';
    form = document.getElementById('nsDeleteform');
    document.getElementById('deleteNSLink').style.display = 'none';
    document.getElementById('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);
    document.getElementById('deleteNSLink').style.display = '';
    document.getElementById('deleteNSWorking').style.display = 'none';
    document.getElementById('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('|');
    document.getElementById('errMessages').innerHTML = ret[0];
    fetchList(ret[1],ret[2],ret[3],ret[4],ret[5]);
}

function fetchPage(page)
{
    if (window.domainid === undefined) domainid = 0;
    document.getElementById('working').style.display = '';
    document.getElementById('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)
{
    document.getElementById('managePage').innerHTML = t.responseText;
    document.getElementById('managePage').style.display = '';
    document.getElementById('working').style.display = 'none';
    unsetStatus('', 1);
}

function fetchList(page,sortby,order,num,filter)
{
    if (window.domainid === undefined) domainid = 0;
    document.getElementById('working').style.display = '';
    document.getElementById('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=clients&view=DomainsList', opt);
}

function fetchListSuccess(t)
{
    document.getElementById('managePage').innerHTML = t.responseText;
    document.getElementById('managePage').style.display = '';
    document.getElementById('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()
{
    document.getElementById('autorenewtext').style.display='none';
    document.getElementById('autorenewdropdown').style.display='';
}

function saveAutoRenew(domainid)
{
    document.getElementById('autorenewtext').style.display='';
    document.getElementById('autorenewdropdown').style.display='none';
    
    if (selectedValue('chAutoRenew') == -1) return;
    
    if (selectedValue('chAutoRenew') == 1) {
        document.getElementById('autorenewtext').innerHTML = lang('Yes');
    } else {
        document.getElementById('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)
{
    document.getElementById('errMessages').innerHTML = t.responseText;
    unsetStatus('', 1);
}

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

function saveLock(domainid)
{
    document.getElementById('locktext').style.display='';
    document.getElementById('lockdropdown').style.display='none';
    
    if (selectedValue('chLocked') == -1) return;
    
    if (selectedValue('chLocked') == 1) {
        document.getElementById('locktext').innerHTML = lang('Locked');
    } else {
        document.getElementById('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)
{
    document.getElementById('transferkey').style.display='none';
    document.getElementById('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('');
    document.getElementById('errMessages').innerHTML=t.responseText;
    document.getElementById('transferkey').style.display='';
    document.getElementById('transferworking').style.display='none';
}

function addDNSRow()
{
    nextid = document.getElementById('_nextid').value;
    lastid = nextid - 1;
    
    var tr = document.getElementById('hosts').insertRow(-1);
    tr.className=document.getElementById('_nextclass').value;
    if (document.getElementById('_nextclass').value == 'primarycolor') {
        document.getElementById('_nextclass').value = 'secondarycolor';
    } else {
        document.getElementById('_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">'+document.getElementById('_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">'+document.getElementById(lastid+'_add').innerHTML+'</span></span>';
    td.width=60;
    td.align='left';
    document.getElementById(lastid+'_add').style.display='none';
    nextid++;
    document.getElementById('_nextid').value = nextid;
}
