function manageUser(id,status) {
	opt = {
    	data: 'id='+id+'&status='+status,
    	onSuccess: function(r) {
    		if(r.responseText!=1) alert(r.responseText);
    	}
    }
   	
	new Request.Do(URL+'/Users/Manage',opt);
}

function deleteUser(id) {
	opt = {
    	data: 'id='+id,
    	onSuccess: function(r) {
    		if(r.responseText!=1) alert(r.responseText);
    		else $('tr'+id).style.display = 'none';
    	}
    }
   	if(confirm(msgConfirmDel)==true)
	new Request.Do(URL+'/Users/Delete',opt);
}



function addText(Section) {
	var data = readForm($('textForm'));
	opt = {
    	data: data,
    	onSuccess: function(r) {
    		if(isNaN(r.responseText)) alert(r.responseText); 
    		else if($('textForm').ID.value=='') {
    			$('textForm').ID.value = r.responseText;
    			alert(msgAdded);
    		} else alert(msgUpdated);
    		
    		$('textForm').submit.disabled = false;
    	}
    }
   	
      $('textForm').submit.disabled = true;
	  new Request.Do(URL+'/Main/'+Section+'/Add',opt);
   	
}

function delText(id) {
	opt = {
    	data: 'ID='+id,
    	onSuccess: function(r) {
    		if(r.responseText!=1) alert(r.responseText);
    		else if($('text_'+id)) $('text_'+id).style.display = 'none';
    		else alert(msgDeleted);
    	}
    }
   	if(confirm(msgConfirmDel)==true)
	new Request.Do(URL+'/Main/Text/Del',opt);
}

function addComment() {
	f = $('CommentForm');
	var data = readForm(f);
	opt = {
    	data: data,
    	onSuccess: function(r) {
    		answ = r.responseText.split('::');
    		if(answ[0]>0) {
    			
    			tr = $('commentTbl').insertRow(1);
    			tr.colspan = 2;
    			tr.className = 'tdCommentTop';
    			td = tr.insertCell(0);
    			td.innerHTML = '<div style="float:right">'+answ[2]+'</div>'+answ[1];
    			
    			tr = $('commentTbl').insertRow(2);
    			tr.colspan = 2;
    			td = tr.insertCell(0);
    			td.innerHTML = f.Comment.value;
    			td.className = 'tdComment';
    			
    			f.Comment.value = '';
    			
    		} else alert(r.responseText);
    	}
    }
    
    $('addComment').style.display = 'none';
    
	new Request.Do(URL+'/Comments/Add/',opt);
}

function pollEdit(id) {
	opt = {
    	data: 'id='+id,
    	onSuccess: function(r) {
    		$('pollEdit').innerHTML = r.responseText;
    	}
    }
	new Request.Do(URL+'/Polls/Add',opt);
}


function pollSave() {
	f = $('PollForm');
	data = readForm(f);

	opt = {
    	data: data,
    	onSuccess: function(r) {
    		if(isNaN(r.responseText)) alert(r.responseText);
    		else {
    			if(r.responseText>0) {
    				f.IDPoll.options[f.IDPoll.options.length] = new Option(f.NameLV.value,r.responseText);
    				f.IDPoll.value = r.responseText;
    			}
    			alert(msgSaved);
    		}
    	}
    }
    
    if(f.NameLV.value == '') {
    	alert('Enter poll name');
    	f.NameLV.focus();
    	return;
    }
    if(f.NameRU.value == '') {
    	alert('Enter poll name');
    	f.NameRU.focus();
    	return;
    }
    	
    	
	new Request.Do(URL+'/Polls/Add',opt);
}

function pollDelete() {
	f = $('PollForm');
	data = 'id='+f.IDPoll.value;

	opt = {
    	data: data,
    	onSuccess: function(r) {
    		if(isNaN(r.responseText)) alert(r.responseText);
    		else {
    		  pollEdit();
    		}
    	}
    }
    
    if(f.NameLV.value == '') {
    	alert('Enter poll name');
    	f.NameLV.focus();
    	return;
    }
    if(f.NameRU.value == '') {
    	alert('Enter poll name');
    	f.NameRU.focus();
    	return;
    }
    	
    	
	new Request.Do(URL+'/Polls/Del',opt);
}

function pollAnswerSave() {
	f = $('PollAnswerForm');
	data = readForm(f);
	data += '&IDPoll='+$('PollForm').IDPoll.value;

	opt = {
    	data: data,
    	onSuccess: function(r) {
    		if(isNaN(r.responseText)) alert(r.responseText);
    		else {
    			if(r.responseText>0) {
    				row = $('pollAnswers').insertRow(1);
    				row.id = 'answer_'+r.responseText;
    				
    				cell = row.insertCell(0); cell.innerHTML = r.responseText;
    				cell = row.insertCell(1); cell.innerHTML = f.AnswLV.value;
    				cell = row.insertCell(2); cell.innerHTML = f.AnswRU.value;
    				cell = row.insertCell(3); cell.innerHTML = '<a class="edit" href="javascript:;" onclick="pollAnswerEdit('+r.responseText+')">'+btnEdit+'</a>&nbsp;&nbsp;'+
    				                                           '<a class="edit" href="javascript:;" style="color:red" onclick="pollAnswerDel('+r.responseText+')">'+btnDel+'</a>';
    				f.AnswLV.value = '';
    				f.AnswRU.value = '';
    				f.AnswLV.focus();                                           
    			} else {
    			 	row = $('answer_'+f.IDAnsw.value);
    			 	row.cells[1].innerHTML = f.AnswLV.value;
    			 	row.cells[2].innerHTML = f.AnswRU.value;
    			 	alert(msgSaved);
    			}
    			
    			
    		}
    	}
    }
    
    if($('PollForm').IDPoll.value == 0) {
    	alert('Select a poll');
    	return;
    }
    if(f.AnswLV.value == '') {
    	alert('Enter answer name');
    	f.AnswLV.focus();
    	return;
    }
    if(f.AnswRU.value == '') {
    	alert('Enter answer name');
    	f.AnswRU.focus();
    	return;
    }
    
    	
    	
	new Request.Do(URL+'/Polls/A/Add',opt);
}

function pollAnswerEdit(id) {
	row = $('answer_'+id);
	f = $('PollAnswerForm');
	
     f.IDAnsw.value = row.cells[0].innerHTML;	
     f.AnswLV.value = row.cells[1].innerHTML;
     f.AnswRU.value = row.cells[2].innerHTML;
}

function pollAnswerDel(id) {
	opt = {
    	data: 'id='+id,
    	onSuccess: function(r) {
    		if(r.responseText==1) $('answer_'+id).style.display='none';
    		else alert(r.responseText);
    	}
    }
	new Request.Do(URL+'/Polls/A/Del',opt);
}

function delFile(id) {
	opt = {
    	data: 'id='+id,
    	onSuccess: function(r) {
    		if(r.responseText==1) alert(msgDeleted);
    		else alert(r.responseText);
    	}
    }
	new Request.Do(URL+'/Files/Del/',opt);
}

function delComment(id) {
	opt = {
    	data: 'id='+id,
    	onSuccess: function(r) {
    		if(r.responseText==1) {
    			$('comment1_'+id).style.display='none';
    			$('comment2_'+id).style.display='none';
    		}
    		else alert(r.responseText);
    	}
    }
	new Request.Do(URL+'/Comments/Del',opt);
}
