﻿function changeStatusClicked(id, on, of, error, statusOn)
{
    jQuery.ajax({
        type: "get"
        , data: ({'id' : id})
        , dataType: "json"
        , url: "/help/remarks/status"
        , success: function(data) {
            if(data['status'] == statusOn)
            {
                jQuery('#statusDiv_'+id).html(on);
            }else
            {
                jQuery('#statusDiv_'+id).html(of);
            }
          }
        , error: function() { jQuery('#statusDiv_'+id).html(error); }
    });
}

function SubmitIfEnter(e)
{
    // IE or NN/FF
    keyCode = window.event ? e.keyCode : e.which ? e.which : 0;
    //alert("Pressed: " + keyCode);
    if( keyCode == 13 )
    {
        getHelpAjax('/help/user/login', 'email=' + jQuery('#email').val() + '&password=' +  jQuery('#password').val() + '&returnUrl=', 'center', 'POST'); return false;
        return false;
    }
    return true;
}
function SubmitIfEnterCheck(e)
{
    // IE or NN/FF
    keyCode = window.event ? e.keyCode : e.which ? e.which : 0;
    //alert("Pressed: " + keyCode);
    if( keyCode == 13 )
    {
        return true;
    }
    return false;
}

/*
 * for updating div in main page template
 */
function LoadSfAjax(link, parameters, divId, errorMessage)
{
    jQuery.ajax({
        url: link
        , data: parameters
        , error: function(){
            OnLoadSfAjaxFailed(divId, errorMessage);
        }
        , success: function(data){
            OnFaqAjaxCompleted(data, divId);
            UpdateMenu();
        }
    });
    return true;
}
function OnLoadSfAjaxFailed(divId, errorMessage)
{
    jQuery('#' + divId).html(errorMessage);
    jQuery('#' + divId).show();
}
/*
 * Function to add confirmation on delete buttons
 * 
 * 
 */
function DeleteIfConfirmed(url, data, div, message, type)
{
	 if( confirm(message) )
	  {
		 getHelpAjax(url, data, div, type);
	  }
}
/*
 * get content faq with ajax
 * 
 * set faq text in the div
 * 
 * return true or false
 */
function getFaqAjax(link, parameters, divId)
{
	$.ajax({
		url: link
		, data: parameters
		//, dataType: html
		, error: function(){
		$('#' + divId).html('Nepavyko pakrauti dukus. Bandykite dar karta.');
		$('#' + divId).show();
		}
		, success: function(data){
			OnFaqAjaxCompleted(data, divId);
			UpdateMenu();
		}
	});
	
	return true;
}
function OnFaqAjaxCompleted(data, divId)
{
	$('#' + divId).html(data);
	$('#' + divId).show();
}

/*
 * get faq answer with ajax
 * 
 * set answer text in the div
 * 
 * return true or false
 */
function getFaqAnswerAjax(link, parameters, divId)
{
	$.ajax({
		url: link
		, data: parameters
		//, dataType: html
		, error: function(){
		$('#' + divId).html('Nepavyko pakrauti atsakymą. Bandykite dar karta.');
		}
		, success: function(data){
			OnFaqAnswerAjaxCompleted(data, divId);
		}
	});
	
	return true;
}

function OnFaqAnswerAjaxCompleted(data, divId)
{
	$('#' + divId).html(data);
	$('#' + divId).show();
}

/*
 * get faq remarks with ajax
 * 
 * set remarks text in the div
 * 
 * return true or false
 */
function getRemarksAjax(link, parameters, divId)
{
	$.ajax({
		url: link
		, data: parameters
		//, dataType: html
		, error: function(data){
		$('#' + divId).html('Nepavyko pakrauti komentarus. Bandykite dar karta.');
		}
		, success: function(data){
			$('#' + divId).html(data);
			$('#' + divId).show();
			UpdateMenu();
		}
	});
	
	return true;
}

/*
 * get form for write remark with ajax
 * 
 * return true or false
 */
function getFormRemarkAjax(link, parameters, divId)
{
	$.ajax({
		url: link
		, data: parameters
		//, dataType: "script"
		//, dataType: html
		, error: function(data){
		$('#' + divId).html('Nepavyko pakrauti formą komentarams. Bandykite dar karta.');
		}
		, success: function(data){
			$('#' + divId).html(data);
			$('#' + divId).show();
		}
	});
	
	return true;
}

/*
 * write remark with ajax
 * 
 * return true or false
 */
function writeRemarkAjax(link, formId, divId)
{
	$.ajax({
		url: link
		, type: 'POST'
		, data: formId

		//, dataType: html
		, error: function(data){
		$('#' + divId).html('Nepavyko įrašyti komentarą. Bandykite dar karta.');
		}
		, success: function(data){
			OnWriteRemarkAjaxCompleted(data, divId);
		}
	});
	
	return true;
}

function OnWriteRemarkAjaxCompleted(data, divId)
{
	jQuery('#' + divId).html(data);
	jQuery('#' + divId).show();
}


/*--- functions for "vartotojo zona" ---*/




/*
 * get content "vartotojo zona" with ajax
 * 
 * set "vartotojo zona" text in the div
 * 
 * return true or false
 */
function getHelpAjax(link, parameters, divId, type)
{
	if( type == null )
	{
		type = 'GET';
	}
	$.ajax({
		url: link
		, type: type
		, data: parameters
		//, dataType: html
		, error: function(xhr, textStatus, errorThrown){
						$('#' + divId).html('Nepavyko pakrauti puslapį. Bandykite dar karta.');
						$('#' + divId).show();
					}
		, success: function(data){
			//alert('success');
			//alert(data);
			OnHelpAjaxCompleted(data, divId);
			UpdateMenu();
		}
	});
	
	return false;
}

function OnHelpAjaxCompleted(data, divId)
{
	$('#' + divId).html(data);
	$('#' + divId).show();
}
function UpdateMenu()
{
	$.ajax({
		url: '/help/faq/checkuserauthenticated'
		, error: function(data){
			alert('Klaida');
			return false;
		}
		, success: function(data){
				$('#UserLeftMenuDiv').html(data);
				$('#UserLeftMenuDiv').show();
			}
		});
}
function getComentFormAjax(link, parameters, divId, type)
{
	//tinyMCE.execCommand('mceRemoveControl', false, 'text');
	//tinyMCE.execCommand('mceRemoveControl', false, 'name');
	if( type == null )
	{
		type = 'GET';
	}
	jQuery.ajax({
		url: link
		, type: type
		, data: parameters
		//, dataType: "script"
		//, dataType: html
		, error: function(data){
			jQuery('#' + divId).html('Nepavyko atvaizduoti formos komentarams. Bandykite dar kartą.');
		}
		, success: function(data){
			//OnLoadRemarksFormCompleted(data, divId); 
			jQuery('#' + divId).html(data);
			jQuery('#' + divId).show();
		}
	});
	
	return true;
}
function OnLoadRemarksFormCompleted(data, divId)
{
	jQuery('#' + divId).html(data);
	tinyMCE.init({
		mode : "none",
        theme : "advanced",
        theme_advanced_buttons1 : "bold,italic,underline",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        width : "400px",
        height : "200px",
        cleanup_on_startup : true,
        verify_html : false,
        cleanup : true
	});
	tinyMCE.execCommand('mceAddControl', false, 'text');
	tinyMCE.execCommand('mceAddControl', false, 'name');
}

/*
 *
 * 
 * for swf uloader init
 */
function getAskajax(link, parameters, divId, type)
{
	if( type == null )
	{
		type = 'GET';
	}
	$.ajax({
		url: link
		, type: type
		, data: parameters
		, error: function(xhr, textStatus, errorThrown){
						$('#' + divId).html('Nepavyko pakrauti puslapį. Bandykite dar karta.');
						$('#' + divId).show();
					}
		, success: function(data){
			OnHelpAjaxCompleted(data, divId);
			UpdateMenu();
		}
	});
	
	return false;
}
/*
 * post data befor uploading photos
 */
function textAndPhotos(link, parameters, divId, type)
{
	if( !jQuery('#name').val() )
	{
		jQuery('#errors').html('error, no name!');
		return false;
	}
	if( type == null )
	{
		type = 'GET';
	}
	$.ajax({
		url: link
		, type: type
		, data: parameters
		, error: function(xhr, textStatus, errorThrown){
						$('#' + divId).html('Nepavyko pakrauti puslapį. Bandykite dar karta.');
						$('#' + divId).show();
					}
		, success: function(data){
			//OnHelpAjaxCompleted(data, divId);
			swfu.uploadStart();
			UpdateMenu();
		}
	});
	
	return false;
}

