/**
 * KMP 2009 Frontend Article Tools
*/
//*******************Subscribe Coments******************************************
function subscribeToComments(faqId) {
    var type ='comment';
    var content = '<div class="popup-data-holder"><form action='+base_href+'subscribeEntryComments/'+faqId+' name="articleTools" id="articleTools" method="post">'+
                    '<table class="act-mail"><tr><td>'+
                    '<span>'+i18n.lbl_email+'</span>'+
                        '<input type="text" id="email_comment" name="email_comment" style="width:200px;"/>'+
                        '<input type="hidden" id="link" name="link" value=""/>'+ 
                    '</td></tr></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_subscribe+'" name="submit" onclick = "javascript:subscribe(\''+type+'\');" 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="sub_com" onclick = "javascript:cancel();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table></div>';
    
    var title = i18n.lbl_subscribe_comments;                
    showDialog(content, title);
    document.getElementById('link').value = window.location.href;
    document.getElementById('email_comment').value = email;
}
//*******************Subscribe Updates******************************************
function subscribeToUpdates(faqId) {
    var type = 'update';
    var content = '<div class="popup-data-holder"><form action='+base_href+'subscribeEntryUpdate/'+faqId+' name="articleTools" id="articleTools" method="post">'+
                    '<table class="act-mail"><tr><td>'+
                    '<span>'+i18n.lbl_email+'</span>'+
                        '<input type="text" id="email_update" name="email_update" style="width:200px;"/>'+
                        '<input type="hidden" id="link" name="link" value=""/>'+ 
                    '</td></tr></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_subscribe+'" name="sub_cut" onclick = "javascript:subscribe(\''+type+'\');" 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="cancel" onclick = "javascript:cancel();" class="form-button"/>'+
                          '</td>'+
                          '<td class="form-btn-r"><div></div></td>'+
                        '</tr>'+
                      '</tbody>'+
                    '</table></div>';
    
    var title = i18n.lbl_subscribe_updates;                
    showDialog(content, title);
    document.getElementById('link').value = window.location.href;
    document.getElementById('email_update').value = email; 
}
//*******************Rate Article***********************************************
function rateIt(faqId) {
    // voting content
    var items_content = '';
    var content = '<div class="form-1 popup-data-holder"><div class="form-case">';
    content += '<form name="voting" method="post" action="'+base_href+'voteEntry/'+faqId+'" onsubmit="return votingValidation();">';
    content += '<input type="hidden" id="link" name="link" value=""/>';
    if (system.type == 'radio') {
        for (var i=0; i<system.items.length; i++) {
            items_content +=  '<li>'+
                '<input id="voting'+system.items[i].value+'" type="radio" name="votingValue" value="'+system.items[i].value+'" onclick="onVoteClick(this);" />'+
                '<label for="voting'+system.items[i].value+'"> <span>'+system.items[i].title+'</span> </label>'
            '</li>';
        }
        content += '<div class="vr"><ul style="margin: 0;">'+items_content+'</ul></div>';
    } else if (system.type == 'combo') {
        for (var i=0; i<system.items.length; i++) {
            items_content += '<option value="'+system.items[i].value+'">'+system.items[i].title+'</option>';
        }
        content += '<div class="vc-combo"><select name="votingValue" onchange="onVote(this);">'+items_content+'</select>';
    }
    //feedback           
    content += '<ul id="feedback" style="display: none;">'+
                        '<li>'+
                        	'<span>'+i18n.lbl_type_email+'</span>'+
                        	'<input id="votingFeedBackEmail" type="text" name="votingFeedBackEmail" />'+
                        '</li>'+
                        '<li>'+
                        	'<span>'+i18n.lbl_type_feedback+'</span>'+
                        	'<textarea id="votingFeedBackText" name="votingFeedBackText" onfocus="javscript:onFeedFocus(this);" onblur="javscript:onFeedBlur(this);" style="color: #666;">'+i18n.msg_improve_article+'</textarea>'+
                        '</li>'+
                    '</ul>';
    //button
                   
    content += '</div>';
	
	content += '<div class="clear"></div>';
	
    content += '<table class="form-btn-1">'+
                      '<tbody>'+
                        '<tr>'+
                          '<td class="form-btn-l"><div></div></td>'+
                          '<td class="form-btn-c">'+
                              '<input type="submit" value="'+i18n.lbl_vote+'" name="vote" 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></form>';
	

                    
    var title = system.header;
    showDialog(content, title);
    document.getElementById('link').value = window.location.href;
    document.getElementById('votingFeedBackEmail').value = email;
}                 	
    var needValidation = false;                     
//-----------voting tools----
function onVoteClick(obj) {
    var feedBlock  = document.getElementById('feedback');
    if (obj.checked && obj.value < 50) {
        feedBlock.style.display = 'block';
        if (useVoitingCom) needValidation = true;
        else needValidation = false;
    } else {
        feedBlock.style.display = 'none';
        needValidation = false;
    }
}
function onVote(obj) {
    if (obj.value < 50) {
        document.getElementById('feedback').style.display = 'block';
        if (useVoitingCom) needValidation = true;
        else needValidation = false;
    } else {
        document.getElementById('feedback').style.display = 'none';
        needValidation = false;
    }
}
function votingValidation() {
    var feedMail   = document.getElementById('votingFeedBackEmail').value;
    var message = i18n.msg_invalid_email;
    if (!checkEmail(feedMail) && needValidation) {
        alert(message);
        return false;
    }
    if (needValidation && (document.getElementById('votingFeedBackText').value == "" || document.getElementById('votingFeedBackText').value == i18n.msg_improve_article)) {
        alert(i18n.lbl_fill_feedback);
        return false;
    }
    if (document.getElementById('votingFeedBackText').value == i18n.msg_improve_article) document.getElementById('votingFeedBackText').value = '';
    return true;
}
//******************* Email to friend ******************************************
function emailToFriend(faqId) {
    var content = '<div class="form-1 popup-data-holder mail-to-friend"><form action="'+base_href+'emailEntryToFriend" name="emailEntryToFriend" id="emailEntryToFriend" method="post">'+
                    '<table class="act-mail"><tbody><tr><td>'+
                    '<span>'+i18n.lbl_type_email+'</span>'+
                        '<input type="text" name="from_email" id="from_email" value="" />'+
                        '<input type="hidden" id="link" name="link" value="" />'+ 
                    '<span>'+i18n.lbl_type_your_friend_name+'</span>'+
                        '<input type="text" name="name" id="name" value="" />'+
                    '<span>'+i18n.lbl_type_your_friend_email+'</span>'+
                        '<input type="text" name="to_email" id="to_email" value="" />'+
                    '<span>'+i18n.lbl_type_your_message+'</span>'+
						'<textarea name="message">'+i18n.lbl_email_to_friend_message+' '+window.location.href+'</textarea>'+
                    '</td></tr></tbody></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_send+'" name="submit" onclick = "javascript:sendemail();"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.lbl_email_to_friend;                
    showDialog(content, title);
    document.getElementById('link').value = window.location.href;
    document.getElementById('from_email').value = email;
    document.getElementById('name').value = name;
}
//****************Delete Article************************************************
function deleteArticle(faqId) {
    var content = '<div class="form-1 popup-data-holder mail-to-friend"><form action="'+base_href+'deleteArticle" name="deleteArticle" id="deleteArticle" method="post">'+
                    '<span>'+i18n.msg_delete_article+'</span>'+
                    '<input type="hidden" id="faqId" name="faqId" value="'+faqId+'" />'+ 
                    '</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:deleteFAQ();"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.lbl_delete_article;                
    showDialog(content, title);
    
    
}
//**********subscribe****************************************************
function deleteFAQ() {
    document.getElementById('deleteArticle').submit();
    return true;   
}
//****************Dialog********************************************************
function showDialog(content, title){
    window.layer = new Layer({
        alignEl: 'tool-button-2',
        alignment: 'bottom',
        title: title,
        content: content,
        visible: true
    });
}
//******************************************************************************
function cancel(){
    window.layer.close();   
}
//**********subscribe****************************************************
function subscribe(type){
    if (type == 'comment') {
        var email = document.getElementById('email_comment').value;
    } else {
        var email = document.getElementById('email_update').value;    
    }
    var message = i18n.msg_invalid_email;
    if (checkEmail(email)) {
        document.getElementById('articleTools').submit();
        return true;
    } else {
        alert(message);
        return false;
    }   
}
//**********sendemail****************************************************
function sendemail(){
    var fromemail = document.getElementById('from_email').value;
    var toemail = document.getElementById('to_email').value;
    var message = i18n.msg_invalid_email;
    if (checkEmail(fromemail) && checkEmail(toemail)) {
        document.getElementById('emailEntryToFriend').submit();
        return true;
    } else {
        alert(message);
        return false;
    }
}
//**********email validation****************************************************
function checkEmail(email){
    var pattern = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;    
    var valid = email.match(pattern);
    if (valid != null) return true
    return false   
}
//**********teep for rait*******************************************************
function onFeedFocus(obj) {
    if (obj.value == i18n.msg_improve_article) {
        obj.value = '';
        obj.style.color = '#000';
    }
}
function onFeedBlur(obj) {
    if (obj.value == '') {
        obj.value = i18n.msg_improve_article;
        obj.style.color = '#666';
    }
}
