/**
 * KMP 2009 Frontend Glossary Tools
*/
//*******************Add/Edit Term******************************************
var action = false;
var addContent = '';
var content = '';
var tr = false, td = false; before = false;
function addTerm() {
    action = 'add';
    addContent = '<div class="popup-data-holder" style="width: 400px;"><form action='+base_href+'addTerm name="addTerm" id="addTerm" method="post" onsubmit="javascript:save();">'+
                    '<table class="act-mail">'+
						'<tr id="message">'+
							'<td style="width: 1%;">'+
								'<span>'+i18n.lbl_term+':</span>'+
							'</td>'+
							'<td>'+
							    '<input type="text" id="term" name="term"  style="width:100%; font-size: 16px; position: relative;"/>'+
								'<input type="hidden" id="link" name="link" value=""/>'+
							'</td>'+
						'</tr>';

    addContent +=  '<tr>'+
						'<td colspan="2">'+
							'<textarea class="description" style="width:100%;height:100px; margin-top: 2px; position: relative;" id="description" name="description" onfocus="javscript:onTermDerscrFocus(this);" onblur="javscript:onTermDerscrBlur(this);" style="color: #666;">'+i18n.lbl_desc+'</textarea>'+
						'</td>'+
					'</tr>'+
				'</table></form>';
      // button
    addContent += '<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_save+'" name="submit" onclick = "javascript:save();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table>';

     addContent +=	'<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_cancel+'" name="submit" onclick = "javascript:cancel();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table></div>';

    var title = i18n.msg_title_add_term;
    showDialog(addContent, title);
    document.getElementById('link').value = window.location.href;
}
//*******************Edit Term******************************************
function editTerm(termId) {
    if (window.layer) {
	  cancel();
    }
    action = 'edit';
    content = '<div class="popup-data-holder" style="width: 400px;"><form action='+base_href+'editTerm name="editTerm" id="editTerm" method="post" onsubmit="javascript:save();">'+
                    '<table class="act-mail"><tr><td  style="width: 1%;">'+
                    '<span>'+i18n.lbl_term+':</span></td><td>'+
                        '<input type="text" id="term" name="term" style="width:100%; font-size: 16px; position: relative;"/>'+
			'<input type="hidden" id="id" name="id" value="'+termId+'"/>'+
			'<input type="hidden" id="link" name="link" value=""/>'+
                    '</td></tr>';

    content +=  '<tr><td colspan="2">'+
    '<textarea class="description" style="width:100%;height:100px; margin-top: 2px; position: relative;" id="description" name="description" onfocus="javscript:onTermDerscrFocus(this);" onblur="javscript:onTermDerscrBlur(this);" style="color: #666;">'+i18n.lbl_desc+'</textarea>'+
    '</td></td></table></form>';
      // button
    content += '<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_save+'" name="submit" onclick = "javascript:save();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table>';

     content +=	'<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_cancel+'" name="submit" onclick = "javascript:cancel();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table></div>';

    new Ajax.Request(index_url+"cliterms/getTerm", {
		group: 'TermDialog',
		method: 'post',
		postBody: 'termId='+termId,
		onSuccess: function(an) {
		    eval('var response='+an.responseText);
		    if (response.done) {
			var title = i18n.msg_title_edit_term;
			showDialog(content, title);
			document.getElementById('link').value = window.location.href.replace(base_href, '');
			document.getElementById('term').value = response.term;
			document.getElementById('description').value = response.term_description;
		    }
		}
    });
}
//****************Delete Term***********************************************
function deleteTerm(termId) {
    var content = '<div class="form-1 popup-data-holder mail-to-friend"><form action="'+base_href+'deleteTerm" name="deleteTerm" id="deleteTerm" method="post">'+
                    '<span>'+i18n.msg_delete_message+'</span>'+
                    '<input type="hidden" id="termId" name="termId" value="'+termId+'" />'+
		    '<input type="hidden" id="link" name="link" value=""/>'+
                    '</form>';
      // button
    content += '<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_delete+'" name="submit" onclick = "javascript:confDeleteTerm();"class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table>';

    content +=	'<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="button" value="'+i18n.lbl_cancel+'" name="submit" onclick = "javascript:cancel();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table>';

	content +="</div>";

    var title = i18n.msg_confirm_mes;
    showDialog(content, title);
    document.getElementById('link').value = window.location.href.replace(base_href, '');

}
//**********subscribe****************************************************
function confDeleteTerm() {
    document.getElementById('deleteTerm').submit();
    return true;
}
//****************Dialog********************************************************
function showDialog(content, title){
    window.layer = new Layer({
        title: title,
        content: content,
        visible: true,
        rtl: (rtl=='rtl')
    });
}
//******************************************************************************
function cancel(){
    window.layer.close();
}
// ***** check term ********************************************************************************
function save(){
	if (action == 'add') {
		if (document.getElementById('term').value == '' || document.getElementById('description').value == '' || document.getElementById('description').value == i18n.lbl_desc) {
			alert(i18n.msg_error_yellow);
			return false;
		}
	    new Ajax.Request(index_url+"cliterms/check", {
		group: 'TermDialog',
		method: 'post',
		postBody: 'term='+document.getElementById('term').value,
		onSuccess: function(an) {
		    eval('var response='+an.responseText);
		    if (response.done) {
				if (document.getElementById('term').value =='' || document.getElementById('description').value =='') {
				    alert(i18n.msg_error_yellow);
				    return false;
				} else {
				    document.getElementById('addTerm').submit();
				    return true;
				}
		    }
		    else {
			showMessage(response.id);
			return false;
		    }
		}
	    });
	} else if (action == 'edit') {
	    if (document.getElementById('term').value =='' || document.getElementById('description').value == '' || document.getElementById('description').value == i18n.lbl_desc) {
			alert(i18n.msg_error_yellow);
			return false;
		} else {
			document.getElementById('editTerm').submit();
			return true;
		}
	}
}
/*
*/
// ***** check term ********************************************************************************
function showMessage(id) {
	if (!tr) {
	    tr = document.createElement('tr');
	    td = document.createElement('td');
		td.colSpan = "2";
	    before = document.getElementById('message');
	    before.parentNode.insertBefore(tr,before);
	    tr.appendChild(td);
	}
	var text = i18n.msg_exist_term+' Click <a href="javascript:editTerm('+id+')">here</a> if you want to edit it.<br/>';
	td.innerHTML = text;
}
//**********teep for description*******************************************************
function onTermDerscrFocus(obj) {
    if (obj.value == i18n.lbl_desc) {
        obj.value = '';
        obj.style.color = '#000';
    }
}
function onTermDerscrBlur(obj) {
    if (obj.value == '') {
        obj.value = i18n.lbl_desc;
        obj.style.color = '#666';
    }
}

