
function login()
{
      $('#loginForm').on('submit',function(e){
         e.preventDefault();
		  httpHost = $(this).attr('action');
         $.ajax({
              type : 'POST',
              url  :  httpHost+'/core/api/auth/log/login',
              data : $(this).serialize(),
              dataType : 'json',
              beforeSend : function (response)
              {
				$('#resp').html("<img src='assets/images/typing.svg'>");
              },
              success : function(response){
				  console.log(response);
                if(response.status =="success") 
                {
                  $('#resp').html("<h3 style='color:green;'>"+response.message+"</h3>");
                  setTimeout(function(){
					location.reload();
				   },3000);
                  
                }
                else
                {
                  $('#resp').html("<h5 style='color:red;'>"+response.message+"</h5>");
                }
              }

         })
		 $(this).unbind('submit');
      })
}




function logoff(httpHost)
{
   $.ajax({
      type : 'POST',
      url  : httpHost+'/core/api/auth/log/signout',
      success : function(response){
          location.href = httpHost;
      }
   })

}
$(document).ready(function() {

    var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.avatars').attr('src', e.target.result);
            }
    
            reader.readAsDataURL(input.files[0]);
        }
    }
    
     
    $(".file-upload").on('change', function(){
        readURL(this);
    });
});

function upstatus(id,type,httpHost){
	
	$.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/active',
		data: 'id='+id,
		success: function(result){
			//console.log(result);
		if (result == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "updated successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			 }, 1000);
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to update"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
		//$("#loads").load(" #loads");
	  }
	  });
}


function Save(type,httpHost){
			var events = $("#signupForm" );
			$( "#signupForm" ).validate( {
				rules: {
					name: "required",
					description: "required",
					status: "required",
					title: "required",
					password: "required",
					username: {
                            required: true,
                            email: true
                        },
                					
				},
				submitHandler: function (form) {
				   $.ajax({
					type     : 'POST',
					url      : httpHost+'/core/api/0/'+type+'/add',
					data     : new FormData($(form)[0]),
					mimeType : "multipart/form-data",
				    success: function(result){
					console.log(result);
					if (result == "success") {
						$.notify({
						title: "<strong>Success:</strong> ",
						icon: 'glyphicon glyphicon-user',
						message: type+" created successfully"
					
						},{animate:{
						enter: "animated fadeInUp",
						exit: "animated fadeOutDown"
						},type:'success'});
						setInterval(function () {
						location.reload();
						 }, 3000);
						events[0].reset();
					}else{
						$.notify({
						title: "<strong>Failed:</strong> ",
						icon: 'glyphicon glyphicon-user',
						message: result
					
						},{animate:{
						enter: "animated fadeInUp",
						exit: "animated fadeOutDown"
						},type:'danger'});
					}
				  },
				  cache: false,
				  contentType: false,
			      processData: false
				  
				  });
				$(this).unbind('submit');
			
				},
				messages: {
					name: "This field is required.",
					username:{
						required: "This field is required.",
						email: "Please enter a valid email address."
					},
					status: "This field is required.",	
					password: "This field is required.",
					title: "This field is required.",
				
					
				},
				errorElement: "em",
				errorPlacement: function ( error, element ) {
					// Add the `help-block` class to the error element
					error.addClass( "help-block" );

					if ( element.prop( "type" ) === "file" ) {
						error.insertAfter( element.parent( "label" ) );
					} else {
						error.insertAfter( element );
					}
				},
				highlight: function ( element, errorClass, validClass ) {
					$( element ).parents( ".colsm" ).addClass( "has-error" ).removeClass( "has-success" );
				},
				unhighlight: function (element, errorClass, validClass) {
					$( element ).parents( ".colsm" ).addClass( "has-success" ).removeClass( "has-error" );
				}
				
			} );
			
} 

function del(id,type,httpHost){
	if(confirm("Are you sure to delete this ?")){
	$.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/delete',
		data: 'id='+id,
		success: function(result){
		if (result == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "deleted successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			 }, 2000);
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to delete"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
		//$("#loads").load(" #loads");
	  }
	  });		
	
	}
}

function update(type,httpHost)
{
  //$('#updateForms').on('submit',function(e){
  $(document).on('submit','#updateForms',function(e){
	 e.preventDefault();
	  var events = $(this);
	 $.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/update',
		data     : new FormData($(this)[0]),
		mimeType : "multipart/form-data",
		success: function(result){
		$('.modal').modal('hide');
		//console.log(result);
		if (result == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: type+" updated successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			}, 3000);
			events[0].reset();
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to update "+type
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
	  },
	  cache: false,
	  contentType: false,
	  processData: false
	  
	  });
  });
}

function updatepass(type,httpHost)
{
	//alert('fdsfs');
   $('#updateForms').on('submit',function(e){ 
    //alert(httpHost+"/"+$(this).attr('action'));   
        e.preventDefault();
          var events = $(this);
	 $.ajax({
		type     : 'POST',
		url      : httpHost+'core/api/0/'+type+'/update',
		data     : new FormData($(this)[0]),
		mimeType : "multipart/form-data",
		dataType : 'json',
		success: function(result){
		$('.modal').modal('hide');
		//console.log(result);
		if (result.status == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: result.message
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			}, 3000);
			events[0].reset();
		}else if (result.status == "notmatch") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: result.message
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'warning'});
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to update "+type
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
	  },
	  cache: false,
	  contentType: false,
	  processData: false
	  
	  });
        $(this).unbind('submit');
   }) 
}

function updateServerkey(type,httpHost)
{
    //alert(httpHost+"/"+$(this).attr('action'));  
   $('#updateFormskey').on('submit',function(e){ 
     
        e.preventDefault();
          var events = $(this);
	 $.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/updateserver',
		data     : new FormData($(this)[0]),
		mimeType : "multipart/form-data",
		dataType : 'json',
		success: function(result){
		$('.modal').modal('hide');
		//console.log();
		if (result.status == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: type+" updated successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			}, 3000);
			events[0].reset();
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to update "+type
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
	  },
	  cache: false,
	  contentType: false,
	  processData: false
	  
	  });
        $(this).unbind('submit');
   }) 
}


$(document).ready(function() {

    var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.avatars').attr('src', e.target.result);
            }
    
            reader.readAsDataURL(input.files[0]);
        }
    }
    
     
    $(".file-upload").on('change', function(){
        readURL(this);
    });
});
$(document).ready(function() {
    $("body").on("click", "#cimage .dropify-clear",function(){
         $('#old_cover').val('');  
    });
});

$(document).ready(function() {
    $("body").on("click", "#bimage .dropify-clear",function(){
         $('#old_background').val('');  
    });
});